summaryrefslogtreecommitdiffstats
path: root/chromeos/network/network_device_handler.h
blob: 0b90f04641cc12a3cf6f1f277b803f9ae6fb2ac1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_NETWORK_NETWORK_DEVICE_HANDLER_H_
#define CHROMEOS_NETWORK_NETWORK_DEVICE_HANDLER_H_

#include <map>
#include <set>
#include <string>

#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "chromeos/chromeos_export.h"
#include "chromeos/dbus/dbus_method_call_status.h"
#include "chromeos/dbus/shill_property_changed_observer.h"

namespace base {
class DictionaryValue;
class ListValue;
class Value;
}

namespace chromeos {

class CHROMEOS_EXPORT NetworkDeviceHandler
    : public ShillPropertyChangedObserver {
 public:
  struct Device {
    Device();
    ~Device();
    std::string type;
    bool powered;
    bool scanning;
    int scan_interval;
  };
  typedef std::map<std::string, Device> DeviceMap;

  class Observer {
   public:
    typedef NetworkDeviceHandler::DeviceMap DeviceMap;

    // Called when devices are updated. Passes the updated map of devices.
    virtual void NetworkDevicesUpdated(const DeviceMap& devices) = 0;

   protected:
    virtual ~Observer() {}
  };

  virtual ~NetworkDeviceHandler();

  // Manage the global instance. Must be initialized before any calls to Get().
  static void Initialize();
  static void Shutdown();
  static NetworkDeviceHandler* Get();

  // Add/remove observers.
  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  bool devices_ready() const { return devices_ready_; }
  const DeviceMap& devices() const { return devices_; }

  // ShillPropertyChangedObserver overrides
  virtual void OnPropertyChanged(const std::string& key,
                                 const base::Value& value) OVERRIDE;

 private:
  friend class NetworkDeviceHandlerTest;
  NetworkDeviceHandler();
  void Init();

  void ManagerPropertiesCallback(DBusMethodCallStatus call_status,
                                 const base::DictionaryValue& properties);
  void DevicePropertyChanged(const base::ListValue* devices);
  void DevicePropertiesCallback(const std::string& device_path,
                                DBusMethodCallStatus call_status,
                                const base::DictionaryValue& properties);
  void NetworkPropertiesCallback(const std::string& device_path,
                                 const std::string& network_path,
                                 DBusMethodCallStatus call_status,
                                 const base::DictionaryValue& properties);
  void GetDeviceProperties(const std::string& device_path,
                           const base::DictionaryValue& properties);

  // True when the device list is up to date.
  bool devices_ready_;

  // Map of Device structs, valid when |devices_ready_| is true.
  DeviceMap devices_;

  // Map of pending devices.
  std::set<std::string> pending_device_paths_;

  // Observer list
  ObserverList<Observer> observers_;

  // For Shill client callbacks
  base::WeakPtrFactory<NetworkDeviceHandler> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(NetworkDeviceHandler);
};

}  // namespace chromeos

#endif  // CHROMEOS_NETWORK_NETWORK_DEVICE_HANDLER_H_