| 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 20 matching lines...) Expand all Loading... |
| 31 #include "extensions/common/api/bluetooth.h" | 31 #include "extensions/common/api/bluetooth.h" |
| 32 #include "extensions/common/api/bluetooth_private.h" | 32 #include "extensions/common/api/bluetooth_private.h" |
| 33 | 33 |
| 34 namespace extensions { | 34 namespace extensions { |
| 35 | 35 |
| 36 namespace bluetooth = core_api::bluetooth; | 36 namespace bluetooth = core_api::bluetooth; |
| 37 namespace bt_private = core_api::bluetooth_private; | 37 namespace bt_private = core_api::bluetooth_private; |
| 38 | 38 |
| 39 BluetoothEventRouter::BluetoothEventRouter(content::BrowserContext* context) | 39 BluetoothEventRouter::BluetoothEventRouter(content::BrowserContext* context) |
| 40 : browser_context_(context), | 40 : browser_context_(context), |
| 41 adapter_(NULL), | 41 adapter_(nullptr), |
| 42 num_event_listeners_(0), | 42 num_event_listeners_(0), |
| 43 extension_registry_observer_(this), | 43 extension_registry_observer_(this), |
| 44 weak_ptr_factory_(this) { | 44 weak_ptr_factory_(this) { |
| 45 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 45 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 46 DCHECK(browser_context_); | 46 DCHECK(browser_context_); |
| 47 registrar_.Add(this, | 47 registrar_.Add(this, |
| 48 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 48 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 49 content::Source<content::BrowserContext>(browser_context_)); | 49 content::Source<content::BrowserContext>(browser_context_)); |
| 50 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); | 50 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); |
| 51 } | 51 } |
| 52 | 52 |
| 53 BluetoothEventRouter::~BluetoothEventRouter() { | 53 BluetoothEventRouter::~BluetoothEventRouter() { |
| 54 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 54 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 55 if (adapter_.get()) { | 55 if (adapter_.get()) { |
| 56 adapter_->RemoveObserver(this); | 56 adapter_->RemoveObserver(this); |
| 57 adapter_ = NULL; | 57 adapter_ = nullptr; |
| 58 } | 58 } |
| 59 CleanUpAllExtensions(); | 59 CleanUpAllExtensions(); |
| 60 } | 60 } |
| 61 | 61 |
| 62 bool BluetoothEventRouter::IsBluetoothSupported() const { | 62 bool BluetoothEventRouter::IsBluetoothSupported() const { |
| 63 return adapter_.get() || | 63 return adapter_.get() || |
| 64 device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable(); | 64 device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable(); |
| 65 } | 65 } |
| 66 | 66 |
| 67 void BluetoothEventRouter::GetAdapter( | 67 void BluetoothEventRouter::GetAdapter( |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 return; | 115 return; |
| 116 } | 116 } |
| 117 device::BluetoothDiscoverySession* session = iter->second; | 117 device::BluetoothDiscoverySession* session = iter->second; |
| 118 session->Stop(callback, error_callback); | 118 session->Stop(callback, error_callback); |
| 119 } | 119 } |
| 120 | 120 |
| 121 BluetoothApiPairingDelegate* BluetoothEventRouter::GetPairingDelegate( | 121 BluetoothApiPairingDelegate* BluetoothEventRouter::GetPairingDelegate( |
| 122 const std::string& extension_id) { | 122 const std::string& extension_id) { |
| 123 return ContainsKey(pairing_delegate_map_, extension_id) | 123 return ContainsKey(pairing_delegate_map_, extension_id) |
| 124 ? pairing_delegate_map_[extension_id] | 124 ? pairing_delegate_map_[extension_id] |
| 125 : NULL; | 125 : nullptr; |
| 126 } | 126 } |
| 127 | 127 |
| 128 void BluetoothEventRouter::OnAdapterInitialized( | 128 void BluetoothEventRouter::OnAdapterInitialized( |
| 129 const base::Closure& callback, | 129 const base::Closure& callback, |
| 130 scoped_refptr<device::BluetoothAdapter> adapter) { | 130 scoped_refptr<device::BluetoothAdapter> adapter) { |
| 131 if (!adapter_.get()) { | 131 if (!adapter_.get()) { |
| 132 adapter_ = adapter; | 132 adapter_ = adapter; |
| 133 adapter_->AddObserver(this); | 133 adapter_->AddObserver(this); |
| 134 } | 134 } |
| 135 | 135 |
| 136 callback.Run(); | 136 callback.Run(); |
| 137 } | 137 } |
| 138 | 138 |
| 139 void BluetoothEventRouter::MaybeReleaseAdapter() { | 139 void BluetoothEventRouter::MaybeReleaseAdapter() { |
| 140 if (adapter_.get() && num_event_listeners_ == 0 && | 140 if (adapter_.get() && num_event_listeners_ == 0 && |
| 141 pairing_delegate_map_.empty()) { | 141 pairing_delegate_map_.empty()) { |
| 142 adapter_->RemoveObserver(this); | 142 adapter_->RemoveObserver(this); |
| 143 adapter_ = NULL; | 143 adapter_ = nullptr; |
| 144 } | 144 } |
| 145 } | 145 } |
| 146 | 146 |
| 147 void BluetoothEventRouter::AddPairingDelegate(const std::string& extension_id) { | 147 void BluetoothEventRouter::AddPairingDelegate(const std::string& extension_id) { |
| 148 if (!adapter_.get()) { | 148 if (!adapter_.get()) { |
| 149 base::Closure self_callback = | 149 base::Closure self_callback = |
| 150 base::Bind(&BluetoothEventRouter::AddPairingDelegate, | 150 base::Bind(&BluetoothEventRouter::AddPairingDelegate, |
| 151 weak_ptr_factory_.GetWeakPtr(), | 151 weak_ptr_factory_.GetWeakPtr(), |
| 152 extension_id); | 152 extension_id); |
| 153 GetAdapter(base::Bind(&BluetoothEventRouter::OnAdapterInitialized, | 153 GetAdapter(base::Bind(&BluetoothEventRouter::OnAdapterInitialized, |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 } | 358 } |
| 359 | 359 |
| 360 void BluetoothEventRouter::OnExtensionUnloaded( | 360 void BluetoothEventRouter::OnExtensionUnloaded( |
| 361 content::BrowserContext* browser_context, | 361 content::BrowserContext* browser_context, |
| 362 const Extension* extension, | 362 const Extension* extension, |
| 363 UnloadedExtensionInfo::Reason reason) { | 363 UnloadedExtensionInfo::Reason reason) { |
| 364 CleanUpForExtension(extension->id()); | 364 CleanUpForExtension(extension->id()); |
| 365 } | 365 } |
| 366 | 366 |
| 367 } // namespace extensions | 367 } // namespace extensions |
| OLD | NEW |