| 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/extensions/api/bluetooth/bluetooth_event_router.h" | 5 #include "chrome/browser/extensions/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" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_vector.h" | 15 #include "base/memory/scoped_vector.h" |
| 16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 18 #include "chrome/browser/chrome_notification_types.h" | 18 #include "chrome/browser/chrome_notification_types.h" |
| 19 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_pairing_delegate
.h" | 19 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_pairing_delegate
.h" |
| 20 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" | 20 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" |
| 21 #include "chrome/browser/extensions/api/bluetooth/bluetooth_private_api.h" | 21 #include "chrome/browser/extensions/api/bluetooth/bluetooth_private_api.h" |
| 22 #include "chrome/common/extensions/api/bluetooth.h" | 22 #include "chrome/common/extensions/api/bluetooth.h" |
| 23 #include "chrome/common/extensions/api/bluetooth_private.h" | 23 #include "chrome/common/extensions/api/bluetooth_private.h" |
| 24 #include "content/public/browser/notification_details.h" | 24 #include "content/public/browser/notification_details.h" |
| 25 #include "content/public/browser/notification_source.h" | 25 #include "content/public/browser/notification_source.h" |
| 26 #include "device/bluetooth/bluetooth_adapter.h" | 26 #include "device/bluetooth/bluetooth_adapter.h" |
| 27 #include "device/bluetooth/bluetooth_adapter_factory.h" | 27 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 28 #include "device/bluetooth/bluetooth_device.h" | 28 #include "device/bluetooth/bluetooth_device.h" |
| 29 #include "device/bluetooth/bluetooth_discovery_session.h" | 29 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 30 #include "device/bluetooth/bluetooth_profile.h" | |
| 31 #include "device/bluetooth/bluetooth_socket.h" | |
| 32 #include "extensions/browser/event_router.h" | 30 #include "extensions/browser/event_router.h" |
| 33 #include "extensions/browser/extension_host.h" | 31 #include "extensions/browser/extension_host.h" |
| 34 | 32 |
| 35 namespace extensions { | 33 namespace extensions { |
| 36 | 34 |
| 37 namespace bluetooth = api::bluetooth; | 35 namespace bluetooth = api::bluetooth; |
| 38 namespace bt_private = api::bluetooth_private; | 36 namespace bt_private = api::bluetooth_private; |
| 39 | 37 |
| 40 // A struct storing a Bluetooth profile and the extension that added it. | |
| 41 struct BluetoothEventRouter::ExtensionBluetoothProfileRecord { | |
| 42 std::string extension_id; | |
| 43 device::BluetoothProfile* profile; | |
| 44 }; | |
| 45 | |
| 46 BluetoothEventRouter::BluetoothEventRouter(content::BrowserContext* context) | 38 BluetoothEventRouter::BluetoothEventRouter(content::BrowserContext* context) |
| 47 : browser_context_(context), | 39 : browser_context_(context), |
| 48 adapter_(NULL), | 40 adapter_(NULL), |
| 49 num_event_listeners_(0), | 41 num_event_listeners_(0), |
| 50 weak_ptr_factory_(this) { | 42 weak_ptr_factory_(this) { |
| 51 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 43 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 52 DCHECK(browser_context_); | 44 DCHECK(browser_context_); |
| 53 registrar_.Add(this, | 45 registrar_.Add(this, |
| 54 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | 46 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, |
| 55 content::Source<content::BrowserContext>(browser_context_)); | 47 content::Source<content::BrowserContext>(browser_context_)); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 75 void BluetoothEventRouter::GetAdapter( | 67 void BluetoothEventRouter::GetAdapter( |
| 76 const device::BluetoothAdapterFactory::AdapterCallback& callback) { | 68 const device::BluetoothAdapterFactory::AdapterCallback& callback) { |
| 77 if (adapter_.get()) { | 69 if (adapter_.get()) { |
| 78 callback.Run(scoped_refptr<device::BluetoothAdapter>(adapter_)); | 70 callback.Run(scoped_refptr<device::BluetoothAdapter>(adapter_)); |
| 79 return; | 71 return; |
| 80 } | 72 } |
| 81 | 73 |
| 82 device::BluetoothAdapterFactory::GetAdapter(callback); | 74 device::BluetoothAdapterFactory::GetAdapter(callback); |
| 83 } | 75 } |
| 84 | 76 |
| 85 void BluetoothEventRouter::AddProfile( | |
| 86 const device::BluetoothUUID& uuid, | |
| 87 const std::string& extension_id, | |
| 88 device::BluetoothProfile* bluetooth_profile) { | |
| 89 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 90 DCHECK(!HasProfile(uuid)); | |
| 91 ExtensionBluetoothProfileRecord record = { extension_id, bluetooth_profile }; | |
| 92 bluetooth_profile_map_[uuid] = record; | |
| 93 } | |
| 94 | |
| 95 void BluetoothEventRouter::RemoveProfile(const device::BluetoothUUID& uuid) { | |
| 96 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 97 BluetoothProfileMap::iterator iter = bluetooth_profile_map_.find(uuid); | |
| 98 if (iter != bluetooth_profile_map_.end()) { | |
| 99 device::BluetoothProfile* bluetooth_profile = iter->second.profile; | |
| 100 bluetooth_profile_map_.erase(iter); | |
| 101 bluetooth_profile->Unregister(); | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 bool BluetoothEventRouter::HasProfile(const device::BluetoothUUID& uuid) const { | |
| 106 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 107 return bluetooth_profile_map_.find(uuid) != bluetooth_profile_map_.end(); | |
| 108 } | |
| 109 | |
| 110 void BluetoothEventRouter::StartDiscoverySession( | 77 void BluetoothEventRouter::StartDiscoverySession( |
| 111 device::BluetoothAdapter* adapter, | 78 device::BluetoothAdapter* adapter, |
| 112 const std::string& extension_id, | 79 const std::string& extension_id, |
| 113 const base::Closure& callback, | 80 const base::Closure& callback, |
| 114 const base::Closure& error_callback) { | 81 const base::Closure& error_callback) { |
| 115 if (adapter != adapter_.get()) { | 82 if (adapter != adapter_.get()) { |
| 116 error_callback.Run(); | 83 error_callback.Run(); |
| 117 return; | 84 return; |
| 118 } | 85 } |
| 119 DiscoverySessionMap::iterator iter = | 86 DiscoverySessionMap::iterator iter = |
| (...skipping 24 matching lines...) Expand all Loading... |
| 144 discovery_session_map_.find(extension_id); | 111 discovery_session_map_.find(extension_id); |
| 145 if (iter == discovery_session_map_.end() || !iter->second->IsActive()) { | 112 if (iter == discovery_session_map_.end() || !iter->second->IsActive()) { |
| 146 DVLOG(1) << "No active discovery session exists for extension."; | 113 DVLOG(1) << "No active discovery session exists for extension."; |
| 147 error_callback.Run(); | 114 error_callback.Run(); |
| 148 return; | 115 return; |
| 149 } | 116 } |
| 150 device::BluetoothDiscoverySession* session = iter->second; | 117 device::BluetoothDiscoverySession* session = iter->second; |
| 151 session->Stop(callback, error_callback); | 118 session->Stop(callback, error_callback); |
| 152 } | 119 } |
| 153 | 120 |
| 154 device::BluetoothProfile* BluetoothEventRouter::GetProfile( | |
| 155 const device::BluetoothUUID& uuid) const { | |
| 156 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 157 BluetoothProfileMap::const_iterator iter = bluetooth_profile_map_.find(uuid); | |
| 158 if (iter != bluetooth_profile_map_.end()) | |
| 159 return iter->second.profile; | |
| 160 | |
| 161 return NULL; | |
| 162 } | |
| 163 | |
| 164 BluetoothApiPairingDelegate* BluetoothEventRouter::GetPairingDelegate( | 121 BluetoothApiPairingDelegate* BluetoothEventRouter::GetPairingDelegate( |
| 165 const std::string& extension_id) { | 122 const std::string& extension_id) { |
| 166 return ContainsKey(pairing_delegate_map_, extension_id) | 123 return ContainsKey(pairing_delegate_map_, extension_id) |
| 167 ? pairing_delegate_map_[extension_id] | 124 ? pairing_delegate_map_[extension_id] |
| 168 : NULL; | 125 : NULL; |
| 169 } | 126 } |
| 170 | 127 |
| 171 void BluetoothEventRouter::OnAdapterInitialized( | 128 void BluetoothEventRouter::OnAdapterInitialized( |
| 172 const base::Closure& callback, | 129 const base::Closure& callback, |
| 173 scoped_refptr<device::BluetoothAdapter> adapter) { | 130 scoped_refptr<device::BluetoothAdapter> adapter) { |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 bluetooth::OnDeviceAdded::Create(extension_device); | 305 bluetooth::OnDeviceAdded::Create(extension_device); |
| 349 scoped_ptr<Event> event(new Event(event_name, args.Pass())); | 306 scoped_ptr<Event> event(new Event(event_name, args.Pass())); |
| 350 EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); | 307 EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); |
| 351 } | 308 } |
| 352 | 309 |
| 353 void BluetoothEventRouter::CleanUpForExtension( | 310 void BluetoothEventRouter::CleanUpForExtension( |
| 354 const std::string& extension_id) { | 311 const std::string& extension_id) { |
| 355 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 312 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 356 RemovePairingDelegate(extension_id); | 313 RemovePairingDelegate(extension_id); |
| 357 | 314 |
| 358 // Remove all profiles added by the extension. | |
| 359 BluetoothProfileMap::iterator profile_iter = bluetooth_profile_map_.begin(); | |
| 360 while (profile_iter != bluetooth_profile_map_.end()) { | |
| 361 ExtensionBluetoothProfileRecord record = profile_iter->second; | |
| 362 if (record.extension_id == extension_id) { | |
| 363 bluetooth_profile_map_.erase(profile_iter++); | |
| 364 record.profile->Unregister(); | |
| 365 } else { | |
| 366 profile_iter++; | |
| 367 } | |
| 368 } | |
| 369 | |
| 370 // Remove any discovery session initiated by the extension. | 315 // Remove any discovery session initiated by the extension. |
| 371 DiscoverySessionMap::iterator session_iter = | 316 DiscoverySessionMap::iterator session_iter = |
| 372 discovery_session_map_.find(extension_id); | 317 discovery_session_map_.find(extension_id); |
| 373 if (session_iter == discovery_session_map_.end()) | 318 if (session_iter == discovery_session_map_.end()) |
| 374 return; | 319 return; |
| 375 delete session_iter->second; | 320 delete session_iter->second; |
| 376 discovery_session_map_.erase(session_iter); | 321 discovery_session_map_.erase(session_iter); |
| 377 } | 322 } |
| 378 | 323 |
| 379 void BluetoothEventRouter::CleanUpAllExtensions() { | 324 void BluetoothEventRouter::CleanUpAllExtensions() { |
| 380 for (BluetoothProfileMap::iterator it = bluetooth_profile_map_.begin(); | |
| 381 it != bluetooth_profile_map_.end(); | |
| 382 ++it) { | |
| 383 it->second.profile->Unregister(); | |
| 384 } | |
| 385 bluetooth_profile_map_.clear(); | |
| 386 | |
| 387 for (DiscoverySessionMap::iterator it = discovery_session_map_.begin(); | 325 for (DiscoverySessionMap::iterator it = discovery_session_map_.begin(); |
| 388 it != discovery_session_map_.end(); | 326 it != discovery_session_map_.end(); |
| 389 ++it) { | 327 ++it) { |
| 390 delete it->second; | 328 delete it->second; |
| 391 } | 329 } |
| 392 discovery_session_map_.clear(); | 330 discovery_session_map_.clear(); |
| 393 | 331 |
| 394 PairingDelegateMap::iterator pairing_iter = pairing_delegate_map_.begin(); | 332 PairingDelegateMap::iterator pairing_iter = pairing_delegate_map_.begin(); |
| 395 while (pairing_iter != pairing_delegate_map_.end()) | 333 while (pairing_iter != pairing_delegate_map_.end()) |
| 396 RemovePairingDelegate(pairing_iter++->first); | 334 RemovePairingDelegate(pairing_iter++->first); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 423 } | 361 } |
| 424 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { | 362 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { |
| 425 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); | 363 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); |
| 426 CleanUpForExtension(host->extension_id()); | 364 CleanUpForExtension(host->extension_id()); |
| 427 break; | 365 break; |
| 428 } | 366 } |
| 429 } | 367 } |
| 430 } | 368 } |
| 431 | 369 |
| 432 } // namespace extensions | 370 } // namespace extensions |
| OLD | NEW |