OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/dbus/bluetooth_node_client.h" | |
6 | |
7 #include <map> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/logging.h" | |
11 #include "base/stl_util.h" | |
12 #include "chrome/browser/chromeos/dbus/bluetooth_device_client.h" | |
13 #include "chrome/browser/chromeos/dbus/bluetooth_property.h" | |
14 #include "dbus/bus.h" | |
15 #include "dbus/message.h" | |
16 #include "dbus/object_path.h" | |
17 #include "dbus/object_proxy.h" | |
18 #include "third_party/cros_system_api/dbus/service_constants.h" | |
19 | |
20 namespace chromeos { | |
21 | |
22 BluetoothNodeClient::Properties::Properties(dbus::ObjectProxy* object_proxy, | |
23 PropertyChangedCallback callback) | |
24 : BluetoothPropertySet(object_proxy, | |
25 bluetooth_node::kBluetoothNodeInterface, | |
26 callback) { | |
27 RegisterProperty(bluetooth_node::kNameProperty, &name); | |
28 RegisterProperty(bluetooth_node::kDeviceProperty, &device); | |
29 } | |
30 | |
31 BluetoothNodeClient::Properties::~Properties() { | |
32 } | |
33 | |
34 | |
35 // The BluetoothNodeClient implementation used in production. | |
36 class BluetoothNodeClientImpl: public BluetoothNodeClient, | |
37 private BluetoothDeviceClient::Observer { | |
38 public: | |
39 BluetoothNodeClientImpl(dbus::Bus* bus, | |
40 BluetoothDeviceClient* device_client) | |
41 : weak_ptr_factory_(this), | |
42 bus_(bus) { | |
43 DVLOG(1) << "Creating BluetoothNodeClientImpl"; | |
44 | |
45 DCHECK(device_client); | |
46 device_client->AddObserver(this); | |
47 } | |
48 | |
49 virtual ~BluetoothNodeClientImpl() { | |
50 // Clean up Properties structures | |
51 for (ObjectMap::iterator iter = object_map_.begin(); | |
52 iter != object_map_.end(); ++iter) { | |
53 Object object = iter->second; | |
54 Properties* properties = object.second; | |
55 delete properties; | |
56 } | |
57 } | |
58 | |
59 // BluetoothNodeClient override. | |
60 virtual void AddObserver(BluetoothNodeClient::Observer* observer) | |
61 OVERRIDE { | |
62 DCHECK(observer); | |
63 observers_.AddObserver(observer); | |
64 } | |
65 | |
66 // BluetoothNodeClient override. | |
67 virtual void RemoveObserver(BluetoothNodeClient::Observer* observer) | |
68 OVERRIDE { | |
69 DCHECK(observer); | |
70 observers_.RemoveObserver(observer); | |
71 } | |
72 | |
73 // BluetoothNodeClient override. | |
74 virtual Properties* GetProperties(const dbus::ObjectPath& object_path) | |
75 OVERRIDE { | |
76 return GetObject(object_path).second; | |
77 } | |
78 | |
79 private: | |
80 // We maintain a collection of dbus object proxies and properties structures | |
81 // for each node binding. | |
82 typedef std::pair<dbus::ObjectProxy*, Properties*> Object; | |
83 typedef std::map<const dbus::ObjectPath, Object> ObjectMap; | |
84 ObjectMap object_map_; | |
85 | |
86 // BluetoothDeviceClient::Observer override. | |
87 virtual void NodeCreated(const dbus::ObjectPath& device_path, | |
88 const dbus::ObjectPath& object_path) OVERRIDE { | |
89 } | |
90 | |
91 // BluetoothDeviceClient::Observer override. | |
92 virtual void NodeRemoved(const dbus::ObjectPath& device_path, | |
93 const dbus::ObjectPath& object_path) OVERRIDE { | |
94 RemoveObject(object_path); | |
95 } | |
96 | |
97 // Ensures that we have an object proxy and properties structure for | |
98 // a node binding with object path |object_path|, creating it if not and | |
99 // storing it in our |object_map_| map. | |
100 Object GetObject(const dbus::ObjectPath& object_path) { | |
101 ObjectMap::iterator iter = object_map_.find(object_path); | |
102 if (iter != object_map_.end()) | |
103 return iter->second; | |
104 | |
105 // Create the object proxy. | |
106 DCHECK(bus_); | |
107 dbus::ObjectProxy* object_proxy = bus_->GetObjectProxy( | |
108 bluetooth_node::kBluetoothNodeServiceName, object_path); | |
109 | |
110 // Create the properties structure. | |
111 Properties* properties = new Properties( | |
112 object_proxy, | |
113 base::Bind(&BluetoothNodeClientImpl::OnPropertyChanged, | |
114 weak_ptr_factory_.GetWeakPtr(), object_path)); | |
115 | |
116 properties->ConnectSignals(); | |
117 properties->GetAll(); | |
118 | |
119 Object object = std::make_pair(object_proxy, properties); | |
120 object_map_[object_path] = object; | |
121 return object; | |
122 } | |
123 | |
124 // Removes the dbus object proxy and properties for the node binding with | |
125 // dbus object path |object_path| from our |object_map_| map. | |
126 void RemoveObject(const dbus::ObjectPath& object_path) { | |
127 ObjectMap::iterator iter = object_map_.find(object_path); | |
128 if (iter != object_map_.end()) { | |
129 // Clean up the Properties structure. | |
130 Object object = iter->second; | |
131 Properties* properties = object.second; | |
132 delete properties; | |
133 | |
134 object_map_.erase(iter); | |
135 } | |
136 } | |
137 | |
138 // Returns a pointer to the object proxy for |object_path|, creating | |
139 // it if necessary. | |
140 dbus::ObjectProxy* GetObjectProxy(const dbus::ObjectPath& object_path) { | |
141 return GetObject(object_path).first; | |
142 } | |
143 | |
144 // Called by BluetoothPropertySet when a property value is changed, | |
145 // either by result of a signal or response to a GetAll() or Get() | |
146 // call. Informs observers. | |
147 void OnPropertyChanged(const dbus::ObjectPath& object_path, | |
148 const std::string& property_name) { | |
149 FOR_EACH_OBSERVER(BluetoothNodeClient::Observer, observers_, | |
150 NodePropertyChanged(object_path, property_name)); | |
151 } | |
152 | |
153 // Weak pointer factory for generating 'this' pointers that might live longer | |
154 // than we do. | |
155 base::WeakPtrFactory<BluetoothNodeClientImpl> weak_ptr_factory_; | |
156 | |
157 dbus::Bus* bus_; | |
158 | |
159 // List of observers interested in event notifications from us. | |
160 ObserverList<BluetoothNodeClient::Observer> observers_; | |
161 | |
162 DISALLOW_COPY_AND_ASSIGN(BluetoothNodeClientImpl); | |
163 }; | |
164 | |
165 // The BluetoothNodeClient implementation used on Linux desktop, which does | |
166 // nothing. | |
167 class BluetoothNodeClientStubImpl : public BluetoothNodeClient { | |
168 public: | |
169 // BluetoothNodeClient override. | |
170 virtual void AddObserver(Observer* observer) OVERRIDE { | |
171 } | |
172 | |
173 // BluetoothNodeClient override. | |
174 virtual void RemoveObserver(Observer* observer) OVERRIDE { | |
175 } | |
176 | |
177 // BluetoothNodeClient override. | |
178 virtual Properties* GetProperties(const dbus::ObjectPath& object_path) | |
179 OVERRIDE { | |
180 VLOG(1) << "GetProperties: " << object_path.value(); | |
181 return NULL; | |
182 } | |
183 }; | |
184 | |
185 BluetoothNodeClient::BluetoothNodeClient() { | |
186 } | |
187 | |
188 BluetoothNodeClient::~BluetoothNodeClient() { | |
189 } | |
190 | |
191 BluetoothNodeClient* BluetoothNodeClient::Create( | |
192 DBusClientImplementationType type, | |
193 dbus::Bus* bus, | |
194 BluetoothDeviceClient* adapter_client) { | |
195 if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) | |
196 return new BluetoothNodeClientImpl(bus, adapter_client); | |
197 DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); | |
198 return new BluetoothNodeClientStubImpl(); | |
199 } | |
200 | |
201 } // namespace chromeos | |
OLD | NEW |