| 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 "extensions/browser/api/bluetooth/bluetooth_event_router.h" | 5 #include "extensions/browser/api/bluetooth/bluetooth_event_router.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 #include "extensions/common/api/bluetooth.h" | 30 #include "extensions/common/api/bluetooth.h" |
| 31 #include "extensions/common/api/bluetooth_private.h" | 31 #include "extensions/common/api/bluetooth_private.h" |
| 32 | 32 |
| 33 namespace extensions { | 33 namespace extensions { |
| 34 | 34 |
| 35 namespace bluetooth = api::bluetooth; | 35 namespace bluetooth = api::bluetooth; |
| 36 namespace bt_private = api::bluetooth_private; | 36 namespace bt_private = api::bluetooth_private; |
| 37 | 37 |
| 38 BluetoothEventRouter::BluetoothEventRouter(content::BrowserContext* context) | 38 BluetoothEventRouter::BluetoothEventRouter(content::BrowserContext* context) |
| 39 : browser_context_(context), | 39 : browser_context_(context), |
| 40 adapter_(NULL), | 40 adapter_(nullptr), |
| 41 num_event_listeners_(0), | 41 num_event_listeners_(0), |
| 42 extension_registry_observer_(this), | 42 extension_registry_observer_(this), |
| 43 weak_ptr_factory_(this) { | 43 weak_ptr_factory_(this) { |
| 44 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 44 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 45 DCHECK(browser_context_); | 45 DCHECK(browser_context_); |
| 46 registrar_.Add(this, | 46 registrar_.Add(this, |
| 47 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 47 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 48 content::Source<content::BrowserContext>(browser_context_)); | 48 content::Source<content::BrowserContext>(browser_context_)); |
| 49 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); | 49 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); |
| 50 } | 50 } |
| 51 | 51 |
| 52 BluetoothEventRouter::~BluetoothEventRouter() { | 52 BluetoothEventRouter::~BluetoothEventRouter() { |
| 53 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 53 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 54 if (adapter_.get()) { | 54 if (adapter_.get()) { |
| 55 adapter_->RemoveObserver(this); | 55 adapter_->RemoveObserver(this); |
| 56 adapter_ = NULL; | 56 adapter_ = nullptr; |
| 57 } | 57 } |
| 58 CleanUpAllExtensions(); | 58 CleanUpAllExtensions(); |
| 59 } | 59 } |
| 60 | 60 |
| 61 bool BluetoothEventRouter::IsBluetoothSupported() const { | 61 bool BluetoothEventRouter::IsBluetoothSupported() const { |
| 62 return adapter_.get() || | 62 return adapter_.get() || |
| 63 device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable(); | 63 device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable(); |
| 64 } | 64 } |
| 65 | 65 |
| 66 void BluetoothEventRouter::GetAdapter( | 66 void BluetoothEventRouter::GetAdapter( |
| 67 const device::BluetoothAdapterFactory::AdapterCallback& callback) { | 67 const device::BluetoothAdapterFactory::AdapterCallback& callback) { |
| 68 if (adapter_.get()) { | 68 if (adapter_.get()) { |
| 69 callback.Run(scoped_refptr<device::BluetoothAdapter>(adapter_)); | 69 callback.Run(scoped_refptr<device::BluetoothAdapter>(adapter_)); |
| 70 return; | 70 return; |
| 71 } | 71 } |
| 72 | 72 |
| 73 device::BluetoothAdapterFactory::GetAdapter(callback); | 73 device::BluetoothAdapterFactory::GetAdapter(callback); |
| 74 } | 74 } |
| 75 | 75 |
| 76 void BluetoothEventRouter::StartDiscoverySession( | 76 void BluetoothEventRouter::StartDiscoverySession( |
| 77 device::BluetoothAdapter* adapter, | 77 device::BluetoothAdapter* adapter, |
| 78 const std::string& extension_id, | 78 const std::string& extension_id, |
| 79 const base::Closure& callback, | 79 const base::Closure& callback, |
| 80 const base::Closure& error_callback) { | 80 const base::Closure& error_callback) { |
| 81 if (adapter != adapter_.get()) { | 81 if (!adapter_.get() && IsBluetoothSupported()) { |
| 82 // If |adapter_| isn't set yet, call GetAdapter() which will synchronously |
| 83 // invoke the callback (StartDiscoverySessionImpl). |
| 84 GetAdapter(base::Bind( |
| 85 &BluetoothEventRouter::OnAdapterInitialized, |
| 86 weak_ptr_factory_.GetWeakPtr(), |
| 87 base::Bind(&BluetoothEventRouter::StartDiscoverySessionImpl, |
| 88 weak_ptr_factory_.GetWeakPtr(), make_scoped_refptr(adapter), |
| 89 extension_id, callback, error_callback))); |
| 90 return; |
| 91 } |
| 92 StartDiscoverySessionImpl(adapter, extension_id, callback, error_callback); |
| 93 } |
| 94 |
| 95 void BluetoothEventRouter::StartDiscoverySessionImpl( |
| 96 device::BluetoothAdapter* adapter, |
| 97 const std::string& extension_id, |
| 98 const base::Closure& callback, |
| 99 const base::Closure& error_callback) { |
| 100 if (!adapter_.get()) { |
| 101 LOG(ERROR) << "Unable to get Bluetooth adapter."; |
| 82 error_callback.Run(); | 102 error_callback.Run(); |
| 83 return; | 103 return; |
| 84 } | 104 } |
| 105 if (adapter != adapter_.get()) { |
| 106 LOG(ERROR) << "Bluetooth adapter mismatch."; |
| 107 error_callback.Run(); |
| 108 return; |
| 109 } |
| 85 DiscoverySessionMap::iterator iter = | 110 DiscoverySessionMap::iterator iter = |
| 86 discovery_session_map_.find(extension_id); | 111 discovery_session_map_.find(extension_id); |
| 87 if (iter != discovery_session_map_.end() && iter->second->IsActive()) { | 112 if (iter != discovery_session_map_.end() && iter->second->IsActive()) { |
| 88 DVLOG(1) << "An active discovery session exists for extension."; | 113 DVLOG(1) << "An active discovery session exists for extension."; |
| 89 error_callback.Run(); | 114 error_callback.Run(); |
| 90 return; | 115 return; |
| 91 } | 116 } |
| 92 | 117 |
| 93 // Check whether user pre set discovery filter by calling SetDiscoveryFilter | 118 // Check whether user pre set discovery filter by calling SetDiscoveryFilter |
| 94 // before. If the user has set a discovery filter then start a filtered | 119 // before. If the user has set a discovery filter then start a filtered |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 | 179 |
| 155 // extension is already running discovery, update it's discovery filter | 180 // extension is already running discovery, update it's discovery filter |
| 156 iter->second->SetDiscoveryFilter(discovery_filter.Pass(), callback, | 181 iter->second->SetDiscoveryFilter(discovery_filter.Pass(), callback, |
| 157 error_callback); | 182 error_callback); |
| 158 } | 183 } |
| 159 | 184 |
| 160 BluetoothApiPairingDelegate* BluetoothEventRouter::GetPairingDelegate( | 185 BluetoothApiPairingDelegate* BluetoothEventRouter::GetPairingDelegate( |
| 161 const std::string& extension_id) { | 186 const std::string& extension_id) { |
| 162 return ContainsKey(pairing_delegate_map_, extension_id) | 187 return ContainsKey(pairing_delegate_map_, extension_id) |
| 163 ? pairing_delegate_map_[extension_id] | 188 ? pairing_delegate_map_[extension_id] |
| 164 : NULL; | 189 : nullptr; |
| 165 } | 190 } |
| 166 | 191 |
| 167 void BluetoothEventRouter::OnAdapterInitialized( | 192 void BluetoothEventRouter::OnAdapterInitialized( |
| 168 const base::Closure& callback, | 193 const base::Closure& callback, |
| 169 scoped_refptr<device::BluetoothAdapter> adapter) { | 194 scoped_refptr<device::BluetoothAdapter> adapter) { |
| 170 if (!adapter_.get()) { | 195 if (!adapter_.get()) { |
| 171 adapter_ = adapter; | 196 adapter_ = adapter; |
| 172 adapter_->AddObserver(this); | 197 adapter_->AddObserver(this); |
| 173 } | 198 } |
| 174 | 199 |
| 175 callback.Run(); | 200 callback.Run(); |
| 176 } | 201 } |
| 177 | 202 |
| 178 void BluetoothEventRouter::MaybeReleaseAdapter() { | 203 void BluetoothEventRouter::MaybeReleaseAdapter() { |
| 179 if (adapter_.get() && num_event_listeners_ == 0 && | 204 if (adapter_.get() && num_event_listeners_ == 0 && |
| 180 pairing_delegate_map_.empty()) { | 205 pairing_delegate_map_.empty()) { |
| 206 VLOG(1) << "Releasing Adapter."; |
| 181 adapter_->RemoveObserver(this); | 207 adapter_->RemoveObserver(this); |
| 182 adapter_ = NULL; | 208 adapter_ = nullptr; |
| 183 } | 209 } |
| 184 } | 210 } |
| 185 | 211 |
| 186 void BluetoothEventRouter::AddPairingDelegate(const std::string& extension_id) { | 212 void BluetoothEventRouter::AddPairingDelegate(const std::string& extension_id) { |
| 187 if (!adapter_.get()) { | 213 if (!adapter_.get() && IsBluetoothSupported()) { |
| 188 base::Closure self_callback = | 214 GetAdapter( |
| 189 base::Bind(&BluetoothEventRouter::AddPairingDelegate, | 215 base::Bind(&BluetoothEventRouter::OnAdapterInitialized, |
| 190 weak_ptr_factory_.GetWeakPtr(), | 216 weak_ptr_factory_.GetWeakPtr(), |
| 191 extension_id); | 217 base::Bind(&BluetoothEventRouter::AddPairingDelegateImpl, |
| 192 GetAdapter(base::Bind(&BluetoothEventRouter::OnAdapterInitialized, | 218 weak_ptr_factory_.GetWeakPtr(), extension_id))); |
| 193 weak_ptr_factory_.GetWeakPtr(), | |
| 194 self_callback)); | |
| 195 return; | 219 return; |
| 196 } | 220 } |
| 221 AddPairingDelegateImpl(extension_id); |
| 222 } |
| 197 | 223 |
| 224 void BluetoothEventRouter::AddPairingDelegateImpl( |
| 225 const std::string& extension_id) { |
| 226 if (!adapter_.get()) { |
| 227 LOG(ERROR) << "Unable to get adatper."; |
| 228 return; |
| 229 } |
| 198 if (!ContainsKey(pairing_delegate_map_, extension_id)) { | 230 if (!ContainsKey(pairing_delegate_map_, extension_id)) { |
| 199 BluetoothApiPairingDelegate* delegate = | 231 BluetoothApiPairingDelegate* delegate = |
| 200 new BluetoothApiPairingDelegate(extension_id, browser_context_); | 232 new BluetoothApiPairingDelegate(extension_id, browser_context_); |
| 201 DCHECK(adapter_.get()); | 233 DCHECK(adapter_.get()); |
| 202 adapter_->AddPairingDelegate( | 234 adapter_->AddPairingDelegate( |
| 203 delegate, device::BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH); | 235 delegate, device::BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH); |
| 204 pairing_delegate_map_[extension_id] = delegate; | 236 pairing_delegate_map_[extension_id] = delegate; |
| 205 } else { | 237 } else { |
| 206 LOG(ERROR) << "Pairing delegate already exists for extension. " | 238 LOG(ERROR) << "Pairing delegate already exists for extension. " |
| 207 << "There should be at most one onPairing listener."; | 239 << "There should be at most one onPairing listener."; |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 iter != discovery_session_map_.end(); | 291 iter != discovery_session_map_.end(); |
| 260 ++iter) { | 292 ++iter) { |
| 261 device::BluetoothDiscoverySession* session = iter->second; | 293 device::BluetoothDiscoverySession* session = iter->second; |
| 262 if (session->IsActive()) { | 294 if (session->IsActive()) { |
| 263 active_session_map[iter->first] = session; | 295 active_session_map[iter->first] = session; |
| 264 continue; | 296 continue; |
| 265 } | 297 } |
| 266 delete session; | 298 delete session; |
| 267 } | 299 } |
| 268 discovery_session_map_.swap(active_session_map); | 300 discovery_session_map_.swap(active_session_map); |
| 269 MaybeReleaseAdapter(); | |
| 270 } | 301 } |
| 271 | 302 |
| 272 DispatchAdapterStateEvent(); | 303 DispatchAdapterStateEvent(); |
| 304 |
| 305 // Release the adapter after dispatching the event. |
| 306 if (!discovering) |
| 307 MaybeReleaseAdapter(); |
| 273 } | 308 } |
| 274 | 309 |
| 275 void BluetoothEventRouter::DeviceAdded(device::BluetoothAdapter* adapter, | 310 void BluetoothEventRouter::DeviceAdded(device::BluetoothAdapter* adapter, |
| 276 device::BluetoothDevice* device) { | 311 device::BluetoothDevice* device) { |
| 277 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 312 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 278 if (adapter != adapter_.get()) { | 313 if (adapter != adapter_.get()) { |
| 279 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); | 314 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); |
| 280 return; | 315 return; |
| 281 } | 316 } |
| 282 | 317 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 320 | 355 |
| 321 void BluetoothEventRouter::OnListenerRemoved() { | 356 void BluetoothEventRouter::OnListenerRemoved() { |
| 322 if (num_event_listeners_ > 0) | 357 if (num_event_listeners_ > 0) |
| 323 num_event_listeners_--; | 358 num_event_listeners_--; |
| 324 MaybeReleaseAdapter(); | 359 MaybeReleaseAdapter(); |
| 325 } | 360 } |
| 326 | 361 |
| 327 void BluetoothEventRouter::DispatchAdapterStateEvent() { | 362 void BluetoothEventRouter::DispatchAdapterStateEvent() { |
| 328 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 363 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 329 api::bluetooth::AdapterState state; | 364 api::bluetooth::AdapterState state; |
| 365 CHECK(adapter_.get()); |
| 330 PopulateAdapterState(*adapter_.get(), &state); | 366 PopulateAdapterState(*adapter_.get(), &state); |
| 331 | 367 |
| 332 scoped_ptr<base::ListValue> args = | 368 scoped_ptr<base::ListValue> args = |
| 333 bluetooth::OnAdapterStateChanged::Create(state); | 369 bluetooth::OnAdapterStateChanged::Create(state); |
| 334 scoped_ptr<Event> event( | 370 scoped_ptr<Event> event( |
| 335 new Event(events::BLUETOOTH_ON_ADAPTER_STATE_CHANGED, | 371 new Event(events::BLUETOOTH_ON_ADAPTER_STATE_CHANGED, |
| 336 bluetooth::OnAdapterStateChanged::kEventName, args.Pass())); | 372 bluetooth::OnAdapterStateChanged::kEventName, args.Pass())); |
| 337 EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); | 373 EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); |
| 338 } | 374 } |
| 339 | 375 |
| 340 void BluetoothEventRouter::DispatchDeviceEvent( | 376 void BluetoothEventRouter::DispatchDeviceEvent( |
| 341 events::HistogramValue histogram_value, | 377 events::HistogramValue histogram_value, |
| 342 const std::string& event_name, | 378 const std::string& event_name, |
| 343 device::BluetoothDevice* device) { | 379 device::BluetoothDevice* device) { |
| 344 bluetooth::Device extension_device; | 380 bluetooth::Device extension_device; |
| 381 CHECK(device); |
| 345 bluetooth::BluetoothDeviceToApiDevice(*device, &extension_device); | 382 bluetooth::BluetoothDeviceToApiDevice(*device, &extension_device); |
| 346 | 383 |
| 347 scoped_ptr<base::ListValue> args = | 384 scoped_ptr<base::ListValue> args = |
| 348 bluetooth::OnDeviceAdded::Create(extension_device); | 385 bluetooth::OnDeviceAdded::Create(extension_device); |
| 349 scoped_ptr<Event> event(new Event(histogram_value, event_name, args.Pass())); | 386 scoped_ptr<Event> event(new Event(histogram_value, event_name, args.Pass())); |
| 350 EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); | 387 EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); |
| 351 } | 388 } |
| 352 | 389 |
| 353 void BluetoothEventRouter::CleanUpForExtension( | 390 void BluetoothEventRouter::CleanUpForExtension( |
| 354 const std::string& extension_id) { | 391 const std::string& extension_id) { |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 417 } | 454 } |
| 418 | 455 |
| 419 void BluetoothEventRouter::OnExtensionUnloaded( | 456 void BluetoothEventRouter::OnExtensionUnloaded( |
| 420 content::BrowserContext* browser_context, | 457 content::BrowserContext* browser_context, |
| 421 const Extension* extension, | 458 const Extension* extension, |
| 422 UnloadedExtensionInfo::Reason reason) { | 459 UnloadedExtensionInfo::Reason reason) { |
| 423 CleanUpForExtension(extension->id()); | 460 CleanUpForExtension(extension->id()); |
| 424 } | 461 } |
| 425 | 462 |
| 426 } // namespace extensions | 463 } // namespace extensions |
| OLD | NEW |