OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" | 5 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter_chromeos.h" |
6 | |
7 #include <string> | |
6 | 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/lazy_instance.h" | |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
10 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
11 #include "base/values.h" | 12 #include "base/values.h" |
12 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" | 13 #include "chrome/browser/chromeos/bluetooth/bluetooth_device_chromeos.h" |
13 #include "chromeos/dbus/bluetooth_adapter_client.h" | 14 #include "chromeos/dbus/bluetooth_adapter_client.h" |
14 #include "chromeos/dbus/bluetooth_device_client.h" | 15 #include "chromeos/dbus/bluetooth_device_client.h" |
15 #include "chromeos/dbus/bluetooth_manager_client.h" | 16 #include "chromeos/dbus/bluetooth_manager_client.h" |
16 #include "chromeos/dbus/bluetooth_out_of_band_client.h" | 17 #include "chromeos/dbus/bluetooth_out_of_band_client.h" |
18 #include "chromeos/dbus/bluetooth_out_of_band_pairing_data.h" | |
17 #include "chromeos/dbus/dbus_thread_manager.h" | 19 #include "chromeos/dbus/dbus_thread_manager.h" |
18 #include "dbus/object_path.h" | 20 #include "dbus/object_path.h" |
19 | 21 |
20 namespace { | |
21 | |
22 // Shared default adapter instance, we don't want to keep this class around | |
23 // if nobody is using it so use a WeakPtr and create the object when needed; | |
24 // since Google C++ Style (and clang's static analyzer) forbids us having | |
25 // exit-time destructors we use a leaky lazy instance for it. | |
26 base::LazyInstance<base::WeakPtr<chromeos::BluetoothAdapter> >::Leaky | |
27 default_adapter = LAZY_INSTANCE_INITIALIZER; | |
28 | |
29 } // namespace | |
30 | |
31 namespace chromeos { | 22 namespace chromeos { |
32 | 23 |
33 BluetoothAdapter::BluetoothAdapter() : track_default_(false), | 24 BluetoothAdapterChromeOs::BluetoothAdapterChromeOs() : track_default_(false), |
34 powered_(false), | 25 powered_(false), |
35 discovering_(false), | 26 discovering_(false), |
36 weak_ptr_factory_(this) { | 27 weak_ptr_factory_(this) { |
37 DBusThreadManager::Get()->GetBluetoothManagerClient()-> | 28 DBusThreadManager::Get()->GetBluetoothManagerClient()-> |
38 AddObserver(this); | 29 AddObserver(this); |
39 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | 30 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> |
40 AddObserver(this); | 31 AddObserver(this); |
41 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> | 32 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> |
42 AddObserver(this); | 33 AddObserver(this); |
43 } | 34 } |
44 | 35 |
45 BluetoothAdapter::~BluetoothAdapter() { | 36 BluetoothAdapterChromeOs::~BluetoothAdapterChromeOs() { |
46 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> | 37 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> |
47 RemoveObserver(this); | 38 RemoveObserver(this); |
48 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | 39 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> |
49 RemoveObserver(this); | 40 RemoveObserver(this); |
50 DBusThreadManager::Get()->GetBluetoothManagerClient()-> | 41 DBusThreadManager::Get()->GetBluetoothManagerClient()-> |
51 RemoveObserver(this); | 42 RemoveObserver(this); |
52 | 43 |
53 STLDeleteValues(&devices_); | 44 STLDeleteValues(&devices_); |
54 } | 45 } |
55 | 46 |
56 void BluetoothAdapter::AddObserver(Observer* observer) { | 47 void BluetoothAdapterChromeOs::AddObserver( |
48 BluetoothAdapter::Observer* observer) { | |
57 DCHECK(observer); | 49 DCHECK(observer); |
58 observers_.AddObserver(observer); | 50 observers_.AddObserver(observer); |
59 } | 51 } |
60 | 52 |
61 void BluetoothAdapter::RemoveObserver(Observer* observer) { | 53 void BluetoothAdapterChromeOs::RemoveObserver( |
54 BluetoothAdapter::Observer* observer) { | |
62 DCHECK(observer); | 55 DCHECK(observer); |
63 observers_.RemoveObserver(observer); | 56 observers_.RemoveObserver(observer); |
64 } | 57 } |
65 | 58 |
66 bool BluetoothAdapter::IsPresent() const { | 59 const std::string& BluetoothAdapterChromeOs::address() const { |
60 return address_; | |
61 } | |
62 | |
63 const std::string& BluetoothAdapterChromeOs::name() const { | |
64 return name_; | |
65 } | |
66 | |
67 bool BluetoothAdapterChromeOs::IsPresent() const { | |
67 return !object_path_.value().empty() && !address_.empty(); | 68 return !object_path_.value().empty() && !address_.empty(); |
68 } | 69 } |
69 | 70 |
70 bool BluetoothAdapter::IsPowered() const { | 71 bool BluetoothAdapterChromeOs::IsPowered() const { |
71 return powered_; | 72 return powered_; |
72 } | 73 } |
73 | 74 |
74 void BluetoothAdapter::SetPowered(bool powered, | 75 void BluetoothAdapterChromeOs::SetPowered(bool powered, |
75 const base::Closure& callback, | 76 const base::Closure& callback, |
76 const ErrorCallback& error_callback) { | 77 const ErrorCallback& error_callback) { |
77 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | 78 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> |
78 GetProperties(object_path_)->powered.Set( | 79 GetProperties(object_path_)->powered.Set( |
79 powered, | 80 powered, |
80 base::Bind(&BluetoothAdapter::OnSetPowered, | 81 base::Bind(&BluetoothAdapterChromeOs::OnSetPowered, |
81 weak_ptr_factory_.GetWeakPtr(), | 82 weak_ptr_factory_.GetWeakPtr(), |
82 callback, | 83 callback, |
83 error_callback)); | 84 error_callback)); |
84 } | 85 } |
85 | 86 |
86 bool BluetoothAdapter::IsDiscovering() const { | 87 bool BluetoothAdapterChromeOs::IsDiscovering() const { |
87 return discovering_; | 88 return discovering_; |
88 } | 89 } |
89 | 90 |
90 void BluetoothAdapter::SetDiscovering(bool discovering, | 91 void BluetoothAdapterChromeOs::SetDiscovering(bool discovering, |
91 const base::Closure& callback, | 92 const base::Closure& callback, |
92 const ErrorCallback& error_callback) { | 93 const ErrorCallback& error_callback) { |
93 if (discovering) { | 94 if (discovering) { |
94 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | 95 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> |
95 StartDiscovery(object_path_, | 96 StartDiscovery(object_path_, |
96 base::Bind(&BluetoothAdapter::OnStartDiscovery, | 97 base::Bind(&BluetoothAdapterChromeOs::OnStartDiscovery, |
97 weak_ptr_factory_.GetWeakPtr(), | 98 weak_ptr_factory_.GetWeakPtr(), |
98 callback, | 99 callback, |
99 error_callback)); | 100 error_callback)); |
100 } else { | 101 } else { |
101 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | 102 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> |
102 StopDiscovery(object_path_, | 103 StopDiscovery(object_path_, |
103 base::Bind(&BluetoothAdapter::OnStopDiscovery, | 104 base::Bind(&BluetoothAdapterChromeOs::OnStopDiscovery, |
104 weak_ptr_factory_.GetWeakPtr(), | 105 weak_ptr_factory_.GetWeakPtr(), |
105 callback, | 106 callback, |
106 error_callback)); | 107 error_callback)); |
107 } | 108 } |
108 } | 109 } |
109 | 110 |
110 BluetoothAdapter::DeviceList BluetoothAdapter::GetDevices() { | 111 BluetoothAdapter::DeviceList BluetoothAdapterChromeOs::GetDevices() { |
111 ConstDeviceList const_devices = | 112 ConstDeviceList const_devices = |
112 const_cast<const BluetoothAdapter *>(this)->GetDevices(); | 113 const_cast<const BluetoothAdapterChromeOs *>(this)->GetDevices(); |
113 | 114 |
114 DeviceList devices; | 115 DeviceList devices; |
115 for (ConstDeviceList::const_iterator i = const_devices.begin(); | 116 for (ConstDeviceList::const_iterator i = const_devices.begin(); |
116 i != const_devices.end(); ++i) | 117 i != const_devices.end(); ++i) |
117 devices.push_back(const_cast<BluetoothDevice *>(*i)); | 118 devices.push_back(const_cast<BluetoothDevice *>(*i)); |
118 | 119 |
119 return devices; | 120 return devices; |
120 } | 121 } |
121 | 122 |
122 BluetoothAdapter::ConstDeviceList BluetoothAdapter::GetDevices() const { | 123 BluetoothAdapter::ConstDeviceList BluetoothAdapterChromeOs::GetDevices() const { |
123 ConstDeviceList devices; | 124 ConstDeviceList devices; |
124 for (DevicesMap::const_iterator iter = devices_.begin(); | 125 for (BluetoothAdapterChromeOs::DevicesMap::const_iterator iter = |
125 iter != devices_.end(); ++iter) | 126 devices_.begin(); |
127 iter != devices_.end(); | |
128 ++iter) | |
126 devices.push_back(iter->second); | 129 devices.push_back(iter->second); |
127 | 130 |
128 return devices; | 131 return devices; |
129 } | 132 } |
130 | 133 |
131 BluetoothDevice* BluetoothAdapter::GetDevice(const std::string& address) { | 134 BluetoothDevice* BluetoothAdapterChromeOs::GetDevice( |
135 const std::string& address) { | |
132 return const_cast<BluetoothDevice *>( | 136 return const_cast<BluetoothDevice *>( |
133 const_cast<const BluetoothAdapter *>(this)->GetDevice(address)); | 137 const_cast<const BluetoothAdapterChromeOs *>(this)->GetDevice(address)); |
134 } | 138 } |
135 | 139 |
136 const BluetoothDevice* BluetoothAdapter::GetDevice( | 140 const BluetoothDevice* BluetoothAdapterChromeOs::GetDevice( |
137 const std::string& address) const { | 141 const std::string& address) const { |
138 DevicesMap::const_iterator iter = devices_.find(address); | 142 BluetoothAdapterChromeOs::DevicesMap::const_iterator iter = |
143 devices_.find(address); | |
139 if (iter != devices_.end()) | 144 if (iter != devices_.end()) |
140 return iter->second; | 145 return iter->second; |
141 | 146 |
142 return NULL; | 147 return NULL; |
143 } | 148 } |
144 | 149 |
145 void BluetoothAdapter::ReadLocalOutOfBandPairingData( | 150 void BluetoothAdapterChromeOs::ReadLocalOutOfBandPairingData( |
146 const BluetoothOutOfBandPairingDataCallback& callback, | 151 const BluetoothOutOfBandPairingDataCallback& callback, |
147 const ErrorCallback& error_callback) { | 152 const ErrorCallback& error_callback) { |
148 DBusThreadManager::Get()->GetBluetoothOutOfBandClient()-> | 153 DBusThreadManager::Get()->GetBluetoothOutOfBandClient()-> |
149 ReadLocalData(object_path_, | 154 ReadLocalData(object_path_, |
150 base::Bind(&BluetoothAdapter::OnReadLocalData, | 155 base::Bind(&BluetoothAdapterChromeOs::OnReadLocalData, |
151 weak_ptr_factory_.GetWeakPtr(), | 156 weak_ptr_factory_.GetWeakPtr(), |
152 callback, | 157 callback, |
153 error_callback)); | 158 error_callback)); |
154 } | 159 } |
155 | 160 |
156 void BluetoothAdapter::TrackDefaultAdapter() { | 161 void BluetoothAdapterChromeOs::TrackDefaultAdapter() { |
157 DVLOG(1) << "Tracking default adapter"; | 162 DVLOG(1) << "Tracking default adapter"; |
158 track_default_ = true; | 163 track_default_ = true; |
159 DBusThreadManager::Get()->GetBluetoothManagerClient()-> | 164 DBusThreadManager::Get()->GetBluetoothManagerClient()-> |
160 DefaultAdapter(base::Bind(&BluetoothAdapter::AdapterCallback, | 165 DefaultAdapter(base::Bind(&BluetoothAdapterChromeOs::AdapterCallback, |
161 weak_ptr_factory_.GetWeakPtr())); | 166 weak_ptr_factory_.GetWeakPtr())); |
162 } | 167 } |
163 | 168 |
164 void BluetoothAdapter::FindAdapter(const std::string& address) { | 169 void BluetoothAdapterChromeOs::FindAdapter(const std::string& address) { |
165 DVLOG(1) << "Using adapter " << address; | 170 DVLOG(1) << "Using adapter " << address; |
166 track_default_ = false; | 171 track_default_ = false; |
167 DBusThreadManager::Get()->GetBluetoothManagerClient()-> | 172 DBusThreadManager::Get()->GetBluetoothManagerClient()-> |
168 FindAdapter(address, | 173 FindAdapter(address, |
169 base::Bind(&BluetoothAdapter::AdapterCallback, | 174 base::Bind(&BluetoothAdapterChromeOs::AdapterCallback, |
170 weak_ptr_factory_.GetWeakPtr())); | 175 weak_ptr_factory_.GetWeakPtr())); |
171 } | 176 } |
172 | 177 |
173 void BluetoothAdapter::AdapterCallback(const dbus::ObjectPath& adapter_path, | 178 void BluetoothAdapterChromeOs::AdapterCallback( |
174 bool success) { | 179 const dbus::ObjectPath& adapter_path, bool success) { |
bryeung
2012/09/18 19:18:39
arguments on separate lines please
youngki
2012/09/19 01:13:55
Done.
| |
175 if (success) { | 180 if (success) { |
176 ChangeAdapter(adapter_path); | 181 ChangeAdapter(adapter_path); |
177 } else if (!object_path_.value().empty()) { | 182 } else if (!object_path_.value().empty()) { |
178 RemoveAdapter(); | 183 RemoveAdapter(); |
179 } | 184 } |
180 } | 185 } |
181 | 186 |
182 void BluetoothAdapter::DefaultAdapterChanged( | 187 void BluetoothAdapterChromeOs::DefaultAdapterChanged( |
183 const dbus::ObjectPath& adapter_path) { | 188 const dbus::ObjectPath& adapter_path) { |
184 if (track_default_) | 189 if (track_default_) |
185 ChangeAdapter(adapter_path); | 190 ChangeAdapter(adapter_path); |
186 } | 191 } |
187 | 192 |
188 void BluetoothAdapter::AdapterRemoved(const dbus::ObjectPath& adapter_path) { | 193 void BluetoothAdapterChromeOs::AdapterRemoved( |
194 const dbus::ObjectPath& adapter_path) { | |
189 if (adapter_path == object_path_) | 195 if (adapter_path == object_path_) |
190 RemoveAdapter(); | 196 RemoveAdapter(); |
191 } | 197 } |
192 | 198 |
193 void BluetoothAdapter::ChangeAdapter(const dbus::ObjectPath& adapter_path) { | 199 void BluetoothAdapterChromeOs::ChangeAdapter( |
200 const dbus::ObjectPath& adapter_path) { | |
194 if (object_path_.value().empty()) { | 201 if (object_path_.value().empty()) { |
195 DVLOG(1) << "Adapter path initialized to " << adapter_path.value(); | 202 DVLOG(1) << "Adapter path initialized to " << adapter_path.value(); |
196 } else if (object_path_.value() != adapter_path.value()) { | 203 } else if (object_path_.value() != adapter_path.value()) { |
197 DVLOG(1) << "Adapter path changed from " << object_path_.value() | 204 DVLOG(1) << "Adapter path changed from " << object_path_.value() |
198 << " to " << adapter_path.value(); | 205 << " to " << adapter_path.value(); |
199 | 206 |
200 RemoveAdapter(); | 207 RemoveAdapter(); |
201 } else { | 208 } else { |
202 DVLOG(1) << "Adapter address updated"; | 209 DVLOG(1) << "Adapter address updated"; |
203 } | 210 } |
(...skipping 15 matching lines...) Expand all Loading... | |
219 } | 226 } |
220 | 227 |
221 PoweredChanged(properties->powered.value()); | 228 PoweredChanged(properties->powered.value()); |
222 DiscoveringChanged(properties->discovering.value()); | 229 DiscoveringChanged(properties->discovering.value()); |
223 DevicesChanged(properties->devices.value()); | 230 DevicesChanged(properties->devices.value()); |
224 | 231 |
225 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 232 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
226 AdapterPresentChanged(this, true)); | 233 AdapterPresentChanged(this, true)); |
227 } | 234 } |
228 | 235 |
229 void BluetoothAdapter::RemoveAdapter() { | 236 void BluetoothAdapterChromeOs::RemoveAdapter() { |
230 const bool adapter_was_present = IsPresent(); | 237 const bool adapter_was_present = IsPresent(); |
231 | 238 |
232 DVLOG(1) << "Adapter lost."; | 239 DVLOG(1) << "Adapter lost."; |
233 PoweredChanged(false); | 240 PoweredChanged(false); |
234 DiscoveringChanged(false); | 241 DiscoveringChanged(false); |
235 ClearDevices(); | 242 ClearDevices(); |
236 | 243 |
237 object_path_ = dbus::ObjectPath(""); | 244 object_path_ = dbus::ObjectPath(""); |
238 address_.clear(); | 245 address_.clear(); |
239 name_.clear(); | 246 name_.clear(); |
240 | 247 |
241 if (adapter_was_present) | 248 if (adapter_was_present) |
242 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 249 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
243 AdapterPresentChanged(this, false)); | 250 AdapterPresentChanged(this, false)); |
244 } | 251 } |
245 | 252 |
246 void BluetoothAdapter::OnSetPowered(const base::Closure& callback, | 253 void BluetoothAdapterChromeOs::OnSetPowered(const base::Closure& callback, |
247 const ErrorCallback& error_callback, | 254 const ErrorCallback& error_callback, |
bryeung
2012/09/18 19:18:39
looks like whitespace is off here
youngki
2012/09/19 01:13:55
Done.
| |
248 bool success) { | 255 bool success) { |
249 if (success) | 256 if (success) |
250 callback.Run(); | 257 callback.Run(); |
251 else | 258 else |
252 error_callback.Run(); | 259 error_callback.Run(); |
253 } | 260 } |
254 | 261 |
255 void BluetoothAdapter::PoweredChanged(bool powered) { | 262 void BluetoothAdapterChromeOs::PoweredChanged(bool powered) { |
256 if (powered == powered_) | 263 if (powered == powered_) |
257 return; | 264 return; |
258 | 265 |
259 powered_ = powered; | 266 powered_ = powered; |
260 | 267 |
261 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 268 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
262 AdapterPoweredChanged(this, powered_)); | 269 AdapterPoweredChanged(this, powered_)); |
263 } | 270 } |
264 | 271 |
265 void BluetoothAdapter::OnStartDiscovery(const base::Closure& callback, | 272 void BluetoothAdapterChromeOs::OnStartDiscovery( |
266 const ErrorCallback& error_callback, | 273 const base::Closure& callback, |
267 const dbus::ObjectPath& adapter_path, | 274 const ErrorCallback& error_callback, |
268 bool success) { | 275 const dbus::ObjectPath& adapter_path, |
276 bool success) { | |
269 if (success) { | 277 if (success) { |
270 DVLOG(1) << object_path_.value() << ": started discovery."; | 278 DVLOG(1) << object_path_.value() << ": started discovery."; |
271 | 279 |
272 // Clear devices found in previous discovery attempts | 280 // Clear devices found in previous discovery attempts |
273 ClearDiscoveredDevices(); | 281 ClearDiscoveredDevices(); |
274 callback.Run(); | 282 callback.Run(); |
275 } else { | 283 } else { |
276 // TODO(keybuk): in future, don't run the callback if the error was just | 284 // TODO(keybuk): in future, don't run the callback if the error was just |
277 // that we were already discovering. | 285 // that we were already discovering. |
278 error_callback.Run(); | 286 error_callback.Run(); |
279 } | 287 } |
280 } | 288 } |
281 | 289 |
282 void BluetoothAdapter::OnStopDiscovery(const base::Closure& callback, | 290 void BluetoothAdapterChromeOs::OnStopDiscovery( |
283 const ErrorCallback& error_callback, | 291 const base::Closure& callback, |
284 const dbus::ObjectPath& adapter_path, | 292 const ErrorCallback& error_callback, |
285 bool success) { | 293 const dbus::ObjectPath& adapter_path, |
294 bool success) { | |
286 if (success) { | 295 if (success) { |
287 DVLOG(1) << object_path_.value() << ": stopped discovery."; | 296 DVLOG(1) << object_path_.value() << ": stopped discovery."; |
288 callback.Run(); | 297 callback.Run(); |
289 // Leave found devices available for perusing. | 298 // Leave found devices available for perusing. |
290 } else { | 299 } else { |
291 // TODO(keybuk): in future, don't run the callback if the error was just | 300 // TODO(keybuk): in future, don't run the callback if the error was just |
292 // that we weren't discovering. | 301 // that we weren't discovering. |
293 error_callback.Run(); | 302 error_callback.Run(); |
294 } | 303 } |
295 } | 304 } |
296 | 305 |
297 void BluetoothAdapter::DiscoveringChanged(bool discovering) { | 306 void BluetoothAdapterChromeOs::DiscoveringChanged(bool discovering) { |
298 if (discovering == discovering_) | 307 if (discovering == discovering_) |
299 return; | 308 return; |
300 | 309 |
301 discovering_ = discovering; | 310 discovering_ = discovering; |
302 | 311 |
303 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 312 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
304 AdapterDiscoveringChanged(this, discovering_)); | 313 AdapterDiscoveringChanged(this, discovering_)); |
305 } | 314 } |
306 | 315 |
307 void BluetoothAdapter::OnReadLocalData( | 316 void BluetoothAdapterChromeOs::OnReadLocalData( |
308 const BluetoothOutOfBandPairingDataCallback& callback, | 317 const BluetoothOutOfBandPairingDataCallback& callback, |
309 const ErrorCallback& error_callback, | 318 const ErrorCallback& error_callback, |
310 const BluetoothOutOfBandPairingData& data, | 319 const BluetoothOutOfBandPairingData& data, |
311 bool success) { | 320 bool success) { |
312 if (success) | 321 if (success) |
313 callback.Run(data); | 322 callback.Run(data); |
314 else | 323 else |
315 error_callback.Run(); | 324 error_callback.Run(); |
316 } | 325 } |
317 | 326 |
318 void BluetoothAdapter::AdapterPropertyChanged( | 327 void BluetoothAdapterChromeOs::AdapterPropertyChanged( |
319 const dbus::ObjectPath& adapter_path, | 328 const dbus::ObjectPath& adapter_path, |
320 const std::string& property_name) { | 329 const std::string& property_name) { |
321 if (adapter_path != object_path_) | 330 if (adapter_path != object_path_) |
322 return; | 331 return; |
323 | 332 |
324 BluetoothAdapterClient::Properties* properties = | 333 BluetoothAdapterClient::Properties* properties = |
325 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> | 334 DBusThreadManager::Get()->GetBluetoothAdapterClient()-> |
326 GetProperties(object_path_); | 335 GetProperties(object_path_); |
327 | 336 |
328 if (property_name == properties->powered.name()) { | 337 if (property_name == properties->powered.name()) { |
329 PoweredChanged(properties->powered.value()); | 338 PoweredChanged(properties->powered.value()); |
330 | 339 |
331 } else if (property_name == properties->discovering.name()) { | 340 } else if (property_name == properties->discovering.name()) { |
332 DiscoveringChanged(properties->discovering.value()); | 341 DiscoveringChanged(properties->discovering.value()); |
333 | 342 |
334 } else if (property_name == properties->devices.name()) { | 343 } else if (property_name == properties->devices.name()) { |
335 DevicesChanged(properties->devices.value()); | 344 DevicesChanged(properties->devices.value()); |
336 | 345 |
337 } else if (property_name == properties->address.name()) { | 346 } else if (property_name == properties->address.name()) { |
338 ChangeAdapter(object_path_); | 347 ChangeAdapter(object_path_); |
339 | 348 |
340 } else if (property_name == properties->name.name()) { | 349 } else if (property_name == properties->name.name()) { |
341 name_ = properties->name.value(); | 350 name_ = properties->name.value(); |
342 | 351 |
343 } | 352 } |
344 } | 353 } |
345 | 354 |
346 void BluetoothAdapter::DevicePropertyChanged( | 355 void BluetoothAdapterChromeOs::DevicePropertyChanged( |
347 const dbus::ObjectPath& device_path, | 356 const dbus::ObjectPath& device_path, |
348 const std::string& property_name) { | 357 const std::string& property_name) { |
349 UpdateDevice(device_path); | 358 UpdateDevice(device_path); |
350 } | 359 } |
351 | 360 |
352 void BluetoothAdapter::UpdateDevice(const dbus::ObjectPath& device_path) { | 361 void BluetoothAdapterChromeOs::UpdateDevice( |
362 const dbus::ObjectPath& device_path) { | |
353 BluetoothDeviceClient::Properties* properties = | 363 BluetoothDeviceClient::Properties* properties = |
354 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> | 364 DBusThreadManager::Get()->GetBluetoothDeviceClient()-> |
355 GetProperties(device_path); | 365 GetProperties(device_path); |
356 | 366 |
357 // When we first see a device, we may not know the address yet and need to | 367 // When we first see a device, we may not know the address yet and need to |
358 // wait for the DevicePropertyChanged signal before adding the device. | 368 // wait for the DevicePropertyChanged signal before adding the device. |
359 const std::string address = properties->address.value(); | 369 const std::string address = properties->address.value(); |
360 if (address.empty()) | 370 if (address.empty()) |
361 return; | 371 return; |
362 | 372 |
363 // The device may be already known to us, either because this is an update | 373 // The device may be already known to us, either because this is an update |
364 // to properties, or the device going from discovered to connected and | 374 // to properties, or the device going from discovered to connected and |
365 // pairing gaining an object path in the process. In any case, we want | 375 // pairing gaining an object path in the process. In any case, we want |
366 // to update the existing object, not create a new one. | 376 // to update the existing object, not create a new one. |
367 DevicesMap::iterator iter = devices_.find(address); | 377 BluetoothAdapterChromeOs::DevicesMap::iterator iter = devices_.find(address); |
bryeung
2012/09/18 19:18:39
Does this really need the class prefix?
youngki
2012/09/19 01:13:55
Removed.
| |
368 BluetoothDevice* device; | 378 BluetoothDeviceChromeOs* device; |
369 const bool update_device = (iter != devices_.end()); | 379 const bool update_device = (iter != devices_.end()); |
370 if (update_device) { | 380 if (update_device) { |
371 device = iter->second; | 381 device = iter->second; |
372 } else { | 382 } else { |
373 device = BluetoothDevice::Create(this); | 383 device = BluetoothDeviceChromeOs::Create(this); |
374 devices_[address] = device; | 384 devices_[address] = device; |
375 } | 385 } |
376 | 386 |
377 const bool was_paired = device->IsPaired(); | 387 const bool was_paired = device->IsPaired(); |
378 if (!was_paired) { | 388 if (!was_paired) { |
379 DVLOG(1) << "Assigned object path " << device_path.value() << " to device " | 389 DVLOG(1) << "Assigned object path " << device_path.value() << " to device " |
380 << address; | 390 << address; |
381 device->SetObjectPath(device_path); | 391 device->SetObjectPath(device_path); |
382 } | 392 } |
383 device->Update(properties, true); | 393 device->Update(properties, true); |
384 | 394 |
385 // Don't send a duplicate added event for supported devices that were | 395 // Don't send a duplicate added event for supported devices that were |
386 // previously visible or for already paired devices, send a changed | 396 // previously visible or for already paired devices, send a changed |
387 // event instead. We always send one event or the other since we always | 397 // event instead. We always send one event or the other since we always |
388 // inform observers about paired devices whether or not they're supported. | 398 // inform observers about paired devices whether or not they're supported. |
389 if (update_device && (device->IsSupported() || was_paired)) { | 399 if (update_device && (device->IsSupported() || was_paired)) { |
390 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 400 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
391 DeviceChanged(this, device)); | 401 DeviceChanged(this, device)); |
392 } else { | 402 } else { |
393 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 403 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
394 DeviceAdded(this, device)); | 404 DeviceAdded(this, device)); |
395 } | 405 } |
396 } | 406 } |
397 | 407 |
398 void BluetoothAdapter::ClearDevices() { | 408 void BluetoothAdapterChromeOs::ClearDevices() { |
399 DevicesMap replace; | 409 BluetoothAdapterChromeOs::DevicesMap replace; |
400 devices_.swap(replace); | 410 devices_.swap(replace); |
401 for (DevicesMap::iterator iter = replace.begin(); | 411 for (BluetoothAdapterChromeOs::DevicesMap::iterator iter = replace.begin(); |
402 iter != replace.end(); ++iter) { | 412 iter != replace.end(); ++iter) { |
403 BluetoothDevice* device = iter->second; | 413 BluetoothDeviceChromeOs* device = iter->second; |
404 if (device->IsSupported() || device->IsPaired()) | 414 if (device->IsSupported() || device->IsPaired()) |
405 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 415 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
406 DeviceRemoved(this, device)); | 416 DeviceRemoved(this, device)); |
407 | 417 |
408 delete device; | 418 delete device; |
409 } | 419 } |
410 } | 420 } |
411 | 421 |
412 void BluetoothAdapter::DeviceCreated(const dbus::ObjectPath& adapter_path, | 422 void BluetoothAdapterChromeOs::DeviceCreated( |
413 const dbus::ObjectPath& device_path) { | 423 const dbus::ObjectPath& adapter_path, |
424 const dbus::ObjectPath& device_path) { | |
414 if (adapter_path != object_path_) | 425 if (adapter_path != object_path_) |
415 return; | 426 return; |
416 | 427 |
417 UpdateDevice(device_path); | 428 UpdateDevice(device_path); |
418 } | 429 } |
419 | 430 |
420 void BluetoothAdapter::DeviceRemoved(const dbus::ObjectPath& adapter_path, | 431 void BluetoothAdapterChromeOs::DeviceRemoved( |
421 const dbus::ObjectPath& device_path) { | 432 const dbus::ObjectPath& adapter_path, |
433 const dbus::ObjectPath& device_path) { | |
422 if (adapter_path != object_path_) | 434 if (adapter_path != object_path_) |
423 return; | 435 return; |
424 | 436 |
425 DevicesMap::iterator iter = devices_.begin(); | 437 BluetoothAdapterChromeOs::DevicesMap::iterator iter = devices_.begin(); |
426 while (iter != devices_.end()) { | 438 while (iter != devices_.end()) { |
427 BluetoothDevice* device = iter->second; | 439 BluetoothDeviceChromeOs* device = iter->second; |
428 DevicesMap::iterator temp = iter; | 440 BluetoothAdapterChromeOs::DevicesMap::iterator temp = iter; |
429 ++iter; | 441 ++iter; |
430 | 442 |
431 if (device->object_path_ != device_path) | 443 if (device->object_path_ != device_path) |
432 continue; | 444 continue; |
433 | 445 |
434 // DeviceRemoved can also be called to indicate a device that is visible | 446 // DeviceRemoved can also be called to indicate a device that is visible |
435 // during discovery has disconnected, but it is still visible to the | 447 // during discovery has disconnected, but it is still visible to the |
436 // adapter, so don't remove in that case and only clear the object path. | 448 // adapter, so don't remove in that case and only clear the object path. |
437 if (!device->IsVisible()) { | 449 if (!device->IsVisible()) { |
438 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 450 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
(...skipping 13 matching lines...) Expand all Loading... | |
452 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 464 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
453 DeviceRemoved(this, device)); | 465 DeviceRemoved(this, device)); |
454 } else { | 466 } else { |
455 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 467 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
456 DeviceChanged(this, device)); | 468 DeviceChanged(this, device)); |
457 } | 469 } |
458 } | 470 } |
459 } | 471 } |
460 } | 472 } |
461 | 473 |
462 void BluetoothAdapter::DevicesChanged( | 474 void BluetoothAdapterChromeOs::DevicesChanged( |
463 const std::vector<dbus::ObjectPath>& devices) { | 475 const std::vector<dbus::ObjectPath>& devices) { |
464 for (std::vector<dbus::ObjectPath>::const_iterator iter = | 476 for (std::vector<dbus::ObjectPath>::const_iterator iter = |
465 devices.begin(); iter != devices.end(); ++iter) | 477 devices.begin(); iter != devices.end(); ++iter) |
466 UpdateDevice(*iter); | 478 UpdateDevice(*iter); |
467 } | 479 } |
468 | 480 |
469 void BluetoothAdapter::ClearDiscoveredDevices() { | 481 void BluetoothAdapterChromeOs::ClearDiscoveredDevices() { |
470 DevicesMap::iterator iter = devices_.begin(); | 482 BluetoothAdapterChromeOs::DevicesMap::iterator iter = devices_.begin(); |
471 while (iter != devices_.end()) { | 483 while (iter != devices_.end()) { |
472 BluetoothDevice* device = iter->second; | 484 BluetoothDeviceChromeOs* device = iter->second; |
473 DevicesMap::iterator temp = iter; | 485 BluetoothAdapterChromeOs::DevicesMap::iterator temp = iter; |
474 ++iter; | 486 ++iter; |
475 | 487 |
476 if (!device->IsPaired()) { | 488 if (!device->IsPaired()) { |
477 if (device->IsSupported()) | 489 if (device->IsSupported()) |
478 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 490 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
479 DeviceRemoved(this, device)); | 491 DeviceRemoved(this, device)); |
480 | 492 |
481 delete device; | 493 delete device; |
482 devices_.erase(temp); | 494 devices_.erase(temp); |
483 } | 495 } |
484 } | 496 } |
485 } | 497 } |
486 | 498 |
487 void BluetoothAdapter::DeviceFound( | 499 void BluetoothAdapterChromeOs::DeviceFound( |
488 const dbus::ObjectPath& adapter_path, const std::string& address, | 500 const dbus::ObjectPath& adapter_path, const std::string& address, |
489 const BluetoothDeviceClient::Properties& properties) { | 501 const BluetoothDeviceClient::Properties& properties) { |
490 if (adapter_path != object_path_) | 502 if (adapter_path != object_path_) |
491 return; | 503 return; |
492 | 504 |
493 // DeviceFound can also be called to indicate that a device we've | 505 // DeviceFound can also be called to indicate that a device we've |
494 // paired with is now visible to the adapter during discovery, in which | 506 // paired with is now visible to the adapter during discovery, in which |
495 // case we want to update the existing object, not create a new one. | 507 // case we want to update the existing object, not create a new one. |
496 BluetoothDevice* device; | 508 BluetoothDeviceChromeOs* device; |
497 DevicesMap::iterator iter = devices_.find(address); | 509 BluetoothAdapterChromeOs::DevicesMap::iterator iter = devices_.find(address); |
498 const bool update_device = (iter != devices_.end()); | 510 const bool update_device = (iter != devices_.end()); |
499 if (update_device) { | 511 if (update_device) { |
500 device = iter->second; | 512 device = iter->second; |
501 } else { | 513 } else { |
502 device = BluetoothDevice::Create(this); | 514 device = BluetoothDeviceChromeOs::Create(this); |
503 devices_[address] = device; | 515 devices_[address] = device; |
504 } | 516 } |
505 | 517 |
506 DVLOG(1) << "Device " << address << " is visible to the adapter"; | 518 DVLOG(1) << "Device " << address << " is visible to the adapter"; |
507 device->SetVisible(true); | 519 device->SetVisible(true); |
508 device->Update(&properties, false); | 520 device->Update(&properties, false); |
509 | 521 |
510 // Don't send a duplicated added event for duplicate signals for supported | 522 // Don't send a duplicated added event for duplicate signals for supported |
511 // devices that were previously visible (should never happen) or for already | 523 // devices that were previously visible (should never happen) or for already |
512 // paired devices, send a changed event instead. We do not inform observers | 524 // paired devices, send a changed event instead. We do not inform observers |
513 // if we find or update an unconnected and unsupported device. | 525 // if we find or update an unconnected and unsupported device. |
514 if (update_device && (device->IsSupported() || device->IsPaired())) { | 526 if (update_device && (device->IsSupported() || device->IsPaired())) { |
515 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 527 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
516 DeviceChanged(this, device)); | 528 DeviceChanged(this, device)); |
517 } else if (device->IsSupported()) { | 529 } else if (device->IsSupported()) { |
518 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 530 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
519 DeviceAdded(this, device)); | 531 DeviceAdded(this, device)); |
520 } | 532 } |
521 } | 533 } |
522 | 534 |
523 void BluetoothAdapter::DeviceDisappeared(const dbus::ObjectPath& adapter_path, | 535 void BluetoothAdapterChromeOs::DeviceDisappeared( |
524 const std::string& address) { | 536 const dbus::ObjectPath& adapter_path, const std::string& address) { |
bryeung
2012/09/18 19:18:39
arguments on separate lines please
youngki
2012/09/19 01:13:55
Done.
| |
525 if (adapter_path != object_path_) | 537 if (adapter_path != object_path_) |
526 return; | 538 return; |
527 | 539 |
528 DevicesMap::iterator iter = devices_.find(address); | 540 BluetoothAdapterChromeOs::DevicesMap::iterator iter = devices_.find(address); |
529 if (iter == devices_.end()) | 541 if (iter == devices_.end()) |
530 return; | 542 return; |
531 | 543 |
532 BluetoothDevice* device = iter->second; | 544 BluetoothDeviceChromeOs* device = iter->second; |
533 | 545 |
534 // DeviceDisappeared can also be called to indicate that a device we've | 546 // DeviceDisappeared can also be called to indicate that a device we've |
535 // paired with is no longer visible to the adapter, so don't remove | 547 // paired with is no longer visible to the adapter, so don't remove |
536 // in that case and only clear the visible flag. | 548 // in that case and only clear the visible flag. |
537 if (!device->IsPaired()) { | 549 if (!device->IsPaired()) { |
538 if (device->IsSupported()) | 550 if (device->IsSupported()) |
539 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 551 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
540 DeviceRemoved(this, device)); | 552 DeviceRemoved(this, device)); |
541 | 553 |
542 DVLOG(1) << "Discovered device " << device->address() | 554 DVLOG(1) << "Discovered device " << device->address() |
543 << " is no longer visible to the adapter"; | 555 << " is no longer visible to the adapter"; |
544 | 556 |
545 delete device; | 557 delete device; |
546 devices_.erase(iter); | 558 devices_.erase(iter); |
547 } else { | 559 } else { |
548 DVLOG(1) << "Paired device " << device->address() | 560 DVLOG(1) << "Paired device " << device->address() |
549 << " is no longer visible to the adapter"; | 561 << " is no longer visible to the adapter"; |
550 device->SetVisible(false); | 562 device->SetVisible(false); |
551 | 563 |
552 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, | 564 FOR_EACH_OBSERVER(BluetoothAdapter::Observer, observers_, |
553 DeviceChanged(this, device)); | 565 DeviceChanged(this, device)); |
554 } | 566 } |
555 } | 567 } |
556 | 568 |
557 | |
558 // static | |
559 scoped_refptr<BluetoothAdapter> BluetoothAdapter::DefaultAdapter() { | |
560 if (!default_adapter.Get().get()) { | |
561 BluetoothAdapter* new_adapter = new BluetoothAdapter; | |
562 default_adapter.Get() = new_adapter->weak_ptr_factory_.GetWeakPtr(); | |
563 default_adapter.Get()->TrackDefaultAdapter(); | |
564 } | |
565 | |
566 return scoped_refptr<BluetoothAdapter>(default_adapter.Get()); | |
567 } | |
568 | |
569 // static | |
570 BluetoothAdapter* BluetoothAdapter::Create(const std::string& address) { | |
571 BluetoothAdapter* adapter = new BluetoothAdapter; | |
572 adapter->FindAdapter(address); | |
573 return adapter; | |
574 } | |
575 | |
576 } // namespace chromeos | 569 } // namespace chromeos |
OLD | NEW |