Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1244)

Side by Side Diff: extensions/browser/api/bluetooth/bluetooth_event_router.cc

Issue 2801403002: MD Settings: Bluetooth: Fix adapter state and discovery (Closed)
Patch Set: Use CHECK instead of early exit Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include <utility> 9 #include <utility>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/bind_helpers.h" 12 #include "base/bind_helpers.h"
13 #include "base/lazy_instance.h" 13 #include "base/lazy_instance.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.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 "build/build_config.h" 18 #include "build/build_config.h"
19 #include "components/device_event_log/device_event_log.h"
19 #include "content/public/browser/notification_details.h" 20 #include "content/public/browser/notification_details.h"
20 #include "content/public/browser/notification_source.h" 21 #include "content/public/browser/notification_source.h"
21 #include "device/bluetooth/bluetooth_adapter.h" 22 #include "device/bluetooth/bluetooth_adapter.h"
22 #include "device/bluetooth/bluetooth_adapter_factory.h" 23 #include "device/bluetooth/bluetooth_adapter_factory.h"
23 #include "device/bluetooth/bluetooth_device.h" 24 #include "device/bluetooth/bluetooth_device.h"
24 #include "device/bluetooth/bluetooth_discovery_session.h" 25 #include "device/bluetooth/bluetooth_discovery_session.h"
25 #include "extensions/browser/api/bluetooth/bluetooth_api_pairing_delegate.h" 26 #include "extensions/browser/api/bluetooth/bluetooth_api_pairing_delegate.h"
26 #include "extensions/browser/api/bluetooth/bluetooth_api_utils.h" 27 #include "extensions/browser/api/bluetooth/bluetooth_api_utils.h"
27 #include "extensions/browser/api/bluetooth/bluetooth_private_api.h" 28 #include "extensions/browser/api/bluetooth/bluetooth_private_api.h"
28 #include "extensions/browser/event_router.h" 29 #include "extensions/browser/event_router.h"
29 #include "extensions/browser/extension_host.h" 30 #include "extensions/browser/extension_host.h"
30 #include "extensions/browser/extension_registry.h" 31 #include "extensions/browser/extension_registry.h"
31 #include "extensions/browser/notification_types.h" 32 #include "extensions/browser/notification_types.h"
32 #include "extensions/common/api/bluetooth.h" 33 #include "extensions/common/api/bluetooth.h"
33 #include "extensions/common/api/bluetooth_private.h" 34 #include "extensions/common/api/bluetooth_private.h"
34 35
35 namespace extensions { 36 namespace extensions {
36 37
37 namespace { 38 namespace {
38 39
39 void IgnoreAdapterResult(scoped_refptr<device::BluetoothAdapter> adapter) {} 40 void IgnoreAdapterResult(scoped_refptr<device::BluetoothAdapter> adapter) {}
40 41
41 void IgnoreAdapterResultAndThen( 42 void IgnoreAdapterResultAndThen(
42 const base::Closure& callback, 43 const base::Closure& callback,
43 scoped_refptr<device::BluetoothAdapter> adapter) { 44 scoped_refptr<device::BluetoothAdapter> adapter) {
44 callback.Run(); 45 callback.Run();
45 } 46 }
46 47
48 std::string GetListenerId(const extensions::EventListenerInfo& details) {
49 return !details.extension_id.empty() ? details.extension_id
50 : details.listener_url.host();
51 }
52
47 } // namespace 53 } // namespace
48 54
49 namespace bluetooth = api::bluetooth; 55 namespace bluetooth = api::bluetooth;
50 namespace bt_private = api::bluetooth_private; 56 namespace bt_private = api::bluetooth_private;
51 57
52 BluetoothEventRouter::BluetoothEventRouter(content::BrowserContext* context) 58 BluetoothEventRouter::BluetoothEventRouter(content::BrowserContext* context)
53 : browser_context_(context), 59 : browser_context_(context),
54 adapter_(nullptr), 60 adapter_(nullptr),
55 num_event_listeners_(0),
56 extension_registry_observer_(this), 61 extension_registry_observer_(this),
57 weak_ptr_factory_(this) { 62 weak_ptr_factory_(this) {
58 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 63 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
64 BLUETOOTH_LOG(USER) << "BluetoothEventRouter()";
59 DCHECK(browser_context_); 65 DCHECK(browser_context_);
60 registrar_.Add(this, 66 registrar_.Add(this, extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
61 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
62 content::Source<content::BrowserContext>(browser_context_)); 67 content::Source<content::BrowserContext>(browser_context_));
63 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_)); 68 extension_registry_observer_.Add(ExtensionRegistry::Get(browser_context_));
64 } 69 }
65 70
66 BluetoothEventRouter::~BluetoothEventRouter() { 71 BluetoothEventRouter::~BluetoothEventRouter() {
67 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 72 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
73 BLUETOOTH_LOG(USER) << "~BluetoothEventRouter()";
68 if (adapter_.get()) { 74 if (adapter_.get()) {
69 adapter_->RemoveObserver(this); 75 adapter_->RemoveObserver(this);
70 adapter_ = nullptr; 76 adapter_ = nullptr;
71 } 77 }
72 CleanUpAllExtensions(); 78 CleanUpAllExtensions();
73 } 79 }
74 80
75 bool BluetoothEventRouter::IsBluetoothSupported() const { 81 bool BluetoothEventRouter::IsBluetoothSupported() const {
76 return adapter_.get() || 82 return adapter_.get() ||
77 device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable(); 83 device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable();
(...skipping 28 matching lines...) Expand all
106 } 112 }
107 StartDiscoverySessionImpl(adapter, extension_id, callback, error_callback); 113 StartDiscoverySessionImpl(adapter, extension_id, callback, error_callback);
108 } 114 }
109 115
110 void BluetoothEventRouter::StartDiscoverySessionImpl( 116 void BluetoothEventRouter::StartDiscoverySessionImpl(
111 device::BluetoothAdapter* adapter, 117 device::BluetoothAdapter* adapter,
112 const std::string& extension_id, 118 const std::string& extension_id,
113 const base::Closure& callback, 119 const base::Closure& callback,
114 const base::Closure& error_callback) { 120 const base::Closure& error_callback) {
115 if (!adapter_.get()) { 121 if (!adapter_.get()) {
116 LOG(ERROR) << "Unable to get Bluetooth adapter."; 122 BLUETOOTH_LOG(ERROR) << "Unable to get Bluetooth adapter.";
117 error_callback.Run(); 123 error_callback.Run();
118 return; 124 return;
119 } 125 }
120 if (adapter != adapter_.get()) { 126 if (adapter != adapter_.get()) {
121 LOG(ERROR) << "Bluetooth adapter mismatch."; 127 BLUETOOTH_LOG(ERROR) << "Bluetooth adapter mismatch.";
122 error_callback.Run(); 128 error_callback.Run();
123 return; 129 return;
124 } 130 }
125 DiscoverySessionMap::iterator iter = 131 DiscoverySessionMap::iterator iter =
126 discovery_session_map_.find(extension_id); 132 discovery_session_map_.find(extension_id);
127 if (iter != discovery_session_map_.end() && iter->second->IsActive()) { 133 if (iter != discovery_session_map_.end() && iter->second->IsActive()) {
128 DVLOG(1) << "An active discovery session exists for extension."; 134 BLUETOOTH_LOG(DEBUG) << "An active discovery session exists for extension: "
135 << extension_id;
129 error_callback.Run(); 136 error_callback.Run();
130 return; 137 return;
131 } 138 }
132 139
140 BLUETOOTH_LOG(USER) << "StartDiscoverySession: " << extension_id;
141
133 // Check whether user pre set discovery filter by calling SetDiscoveryFilter 142 // Check whether user pre set discovery filter by calling SetDiscoveryFilter
134 // before. If the user has set a discovery filter then start a filtered 143 // before. If the user has set a discovery filter then start a filtered
135 // discovery session, otherwise start a regular session 144 // discovery session, otherwise start a regular session
136 PreSetFilterMap::iterator pre_set_iter = 145 PreSetFilterMap::iterator pre_set_iter =
137 pre_set_filter_map_.find(extension_id); 146 pre_set_filter_map_.find(extension_id);
138 if (pre_set_iter != pre_set_filter_map_.end()) { 147 if (pre_set_iter != pre_set_filter_map_.end()) {
139 adapter->StartDiscoverySessionWithFilter( 148 adapter->StartDiscoverySessionWithFilter(
140 std::unique_ptr<device::BluetoothDiscoveryFilter>(pre_set_iter->second), 149 std::unique_ptr<device::BluetoothDiscoveryFilter>(pre_set_iter->second),
141 base::Bind(&BluetoothEventRouter::OnStartDiscoverySession, 150 base::Bind(&BluetoothEventRouter::OnStartDiscoverySession,
142 weak_ptr_factory_.GetWeakPtr(), extension_id, callback), 151 weak_ptr_factory_.GetWeakPtr(), extension_id, callback),
(...skipping 12 matching lines...) Expand all
155 const std::string& extension_id, 164 const std::string& extension_id,
156 const base::Closure& callback, 165 const base::Closure& callback,
157 const base::Closure& error_callback) { 166 const base::Closure& error_callback) {
158 if (adapter != adapter_.get()) { 167 if (adapter != adapter_.get()) {
159 error_callback.Run(); 168 error_callback.Run();
160 return; 169 return;
161 } 170 }
162 DiscoverySessionMap::iterator iter = 171 DiscoverySessionMap::iterator iter =
163 discovery_session_map_.find(extension_id); 172 discovery_session_map_.find(extension_id);
164 if (iter == discovery_session_map_.end() || !iter->second->IsActive()) { 173 if (iter == discovery_session_map_.end() || !iter->second->IsActive()) {
165 DVLOG(1) << "No active discovery session exists for extension."; 174 BLUETOOTH_LOG(DEBUG) << "No active discovery session exists for extension.";
166 error_callback.Run(); 175 error_callback.Run();
167 return; 176 return;
168 } 177 }
178 BLUETOOTH_LOG(USER) << "StopDiscoverySession: " << extension_id;
169 device::BluetoothDiscoverySession* session = iter->second; 179 device::BluetoothDiscoverySession* session = iter->second;
170 session->Stop(callback, error_callback); 180 session->Stop(callback, error_callback);
171 } 181 }
172 182
173 void BluetoothEventRouter::SetDiscoveryFilter( 183 void BluetoothEventRouter::SetDiscoveryFilter(
174 std::unique_ptr<device::BluetoothDiscoveryFilter> discovery_filter, 184 std::unique_ptr<device::BluetoothDiscoveryFilter> discovery_filter,
175 device::BluetoothAdapter* adapter, 185 device::BluetoothAdapter* adapter,
176 const std::string& extension_id, 186 const std::string& extension_id,
177 const base::Closure& callback, 187 const base::Closure& callback,
178 const base::Closure& error_callback) { 188 const base::Closure& error_callback) {
179 DVLOG(1) << "SetDiscoveryFilter"; 189 BLUETOOTH_LOG(USER) << "SetDiscoveryFilter";
180 if (adapter != adapter_.get()) { 190 if (adapter != adapter_.get()) {
181 error_callback.Run(); 191 error_callback.Run();
182 return; 192 return;
183 } 193 }
184 194
185 DiscoverySessionMap::iterator iter = 195 DiscoverySessionMap::iterator iter =
186 discovery_session_map_.find(extension_id); 196 discovery_session_map_.find(extension_id);
187 if (iter == discovery_session_map_.end() || !iter->second->IsActive()) { 197 if (iter == discovery_session_map_.end() || !iter->second->IsActive()) {
188 DVLOG(1) << "No active discovery session exists for extension, so caching " 198 BLUETOOTH_LOG(DEBUG) << "No active discovery session exists for extension, "
189 "filter for later use."; 199 << "so caching filter for later use.";
190 pre_set_filter_map_[extension_id] = discovery_filter.release(); 200 pre_set_filter_map_[extension_id] = discovery_filter.release();
191 callback.Run(); 201 callback.Run();
192 return; 202 return;
193 } 203 }
194 204
195 // extension is already running discovery, update it's discovery filter 205 // extension is already running discovery, update it's discovery filter
196 iter->second->SetDiscoveryFilter(std::move(discovery_filter), callback, 206 iter->second->SetDiscoveryFilter(std::move(discovery_filter), callback,
197 error_callback); 207 error_callback);
198 } 208 }
199 209
200 BluetoothApiPairingDelegate* BluetoothEventRouter::GetPairingDelegate( 210 BluetoothApiPairingDelegate* BluetoothEventRouter::GetPairingDelegate(
201 const std::string& extension_id) { 211 const std::string& extension_id) {
202 return base::ContainsKey(pairing_delegate_map_, extension_id) 212 return base::ContainsKey(pairing_delegate_map_, extension_id)
203 ? pairing_delegate_map_[extension_id] 213 ? pairing_delegate_map_[extension_id]
204 : nullptr; 214 : nullptr;
205 } 215 }
206 216
207 void BluetoothEventRouter::OnAdapterInitialized( 217 void BluetoothEventRouter::OnAdapterInitialized(
208 const device::BluetoothAdapterFactory::AdapterCallback& callback, 218 const device::BluetoothAdapterFactory::AdapterCallback& callback,
209 scoped_refptr<device::BluetoothAdapter> adapter) { 219 scoped_refptr<device::BluetoothAdapter> adapter) {
210 if (!adapter_.get()) { 220 if (!adapter_.get()) {
211 adapter_ = adapter; 221 adapter_ = adapter;
212 adapter_->AddObserver(this); 222 adapter_->AddObserver(this);
213 } 223 }
214 224
215 callback.Run(adapter); 225 callback.Run(adapter);
216 } 226 }
217 227
218 void BluetoothEventRouter::MaybeReleaseAdapter() { 228 void BluetoothEventRouter::MaybeReleaseAdapter() {
219 if (adapter_.get() && num_event_listeners_ == 0 && 229 if (adapter_.get() && event_listener_count_.empty() &&
220 pairing_delegate_map_.empty()) { 230 pairing_delegate_map_.empty()) {
221 VLOG(1) << "Releasing Adapter."; 231 BLUETOOTH_LOG(EVENT) << "Releasing Adapter.";
222 adapter_->RemoveObserver(this); 232 adapter_->RemoveObserver(this);
223 adapter_ = nullptr; 233 adapter_ = nullptr;
224 } 234 }
225 } 235 }
226 236
227 void BluetoothEventRouter::AddPairingDelegate(const std::string& extension_id) { 237 void BluetoothEventRouter::AddPairingDelegate(const std::string& extension_id) {
228 if (!adapter_.get() && IsBluetoothSupported()) { 238 if (!adapter_.get() && IsBluetoothSupported()) {
229 GetAdapter( 239 GetAdapter(
230 base::Bind(&IgnoreAdapterResultAndThen, 240 base::Bind(&IgnoreAdapterResultAndThen,
231 base::Bind(&BluetoothEventRouter::AddPairingDelegateImpl, 241 base::Bind(&BluetoothEventRouter::AddPairingDelegateImpl,
232 weak_ptr_factory_.GetWeakPtr(), extension_id))); 242 weak_ptr_factory_.GetWeakPtr(), extension_id)));
233 return; 243 return;
234 } 244 }
235 AddPairingDelegateImpl(extension_id); 245 AddPairingDelegateImpl(extension_id);
236 } 246 }
237 247
238 void BluetoothEventRouter::AddPairingDelegateImpl( 248 void BluetoothEventRouter::AddPairingDelegateImpl(
239 const std::string& extension_id) { 249 const std::string& extension_id) {
240 if (!adapter_.get()) { 250 if (!adapter_.get()) {
241 LOG(ERROR) << "Unable to get adapter for extension_id: " << extension_id; 251 LOG(ERROR) << "Unable to get adapter for extension_id: " << extension_id;
242 return; 252 return;
243 } 253 }
244 if (base::ContainsKey(pairing_delegate_map_, extension_id)) { 254 if (base::ContainsKey(pairing_delegate_map_, extension_id)) {
245 // For WebUI there may be more than one page open to the same url 255 // For WebUI there may be more than one page open to the same url
246 // (e.g. chrome://settings). These will share the same pairing delegate. 256 // (e.g. chrome://settings). These will share the same pairing delegate.
247 VLOG(1) << "Pairing delegate already exists for extension_id: " 257 BLUETOOTH_LOG(EVENT) << "Pairing delegate already exists for extension_id: "
248 << extension_id; 258 << extension_id;
249 return; 259 return;
250 } 260 }
251 BluetoothApiPairingDelegate* delegate = 261 BluetoothApiPairingDelegate* delegate =
252 new BluetoothApiPairingDelegate(browser_context_); 262 new BluetoothApiPairingDelegate(browser_context_);
253 DCHECK(adapter_.get()); 263 DCHECK(adapter_.get());
254 adapter_->AddPairingDelegate( 264 adapter_->AddPairingDelegate(
255 delegate, device::BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH); 265 delegate, device::BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
256 pairing_delegate_map_[extension_id] = delegate; 266 pairing_delegate_map_[extension_id] = delegate;
257 } 267 }
258 268
259 void BluetoothEventRouter::RemovePairingDelegate( 269 void BluetoothEventRouter::RemovePairingDelegate(
260 const std::string& extension_id) { 270 const std::string& extension_id) {
261 if (base::ContainsKey(pairing_delegate_map_, extension_id)) { 271 if (base::ContainsKey(pairing_delegate_map_, extension_id)) {
262 BluetoothApiPairingDelegate* delegate = pairing_delegate_map_[extension_id]; 272 BluetoothApiPairingDelegate* delegate = pairing_delegate_map_[extension_id];
263 if (adapter_.get()) 273 if (adapter_.get())
264 adapter_->RemovePairingDelegate(delegate); 274 adapter_->RemovePairingDelegate(delegate);
265 pairing_delegate_map_.erase(extension_id); 275 pairing_delegate_map_.erase(extension_id);
266 delete delegate; 276 delete delegate;
267 MaybeReleaseAdapter(); 277 MaybeReleaseAdapter();
268 } 278 }
269 } 279 }
270 280
271 void BluetoothEventRouter::AdapterPresentChanged( 281 void BluetoothEventRouter::AdapterPresentChanged(
272 device::BluetoothAdapter* adapter, 282 device::BluetoothAdapter* adapter,
273 bool present) { 283 bool present) {
274 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 284 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
275 if (adapter != adapter_.get()) { 285 if (adapter != adapter_.get()) {
276 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); 286 BLUETOOTH_LOG(DEBUG) << "Ignoring event for adapter "
287 << adapter->GetAddress();
277 return; 288 return;
278 } 289 }
279 DispatchAdapterStateEvent(); 290 DispatchAdapterStateEvent();
280 } 291 }
281 292
282 void BluetoothEventRouter::AdapterPoweredChanged( 293 void BluetoothEventRouter::AdapterPoweredChanged(
283 device::BluetoothAdapter* adapter, 294 device::BluetoothAdapter* adapter,
284 bool has_power) { 295 bool has_power) {
285 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 296 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
286 if (adapter != adapter_.get()) { 297 if (adapter != adapter_.get()) {
287 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); 298 BLUETOOTH_LOG(DEBUG) << "Ignoring event for adapter "
299 << adapter->GetAddress();
288 return; 300 return;
289 } 301 }
290 DispatchAdapterStateEvent(); 302 DispatchAdapterStateEvent();
291 } 303 }
292 304
293 void BluetoothEventRouter::AdapterDiscoveringChanged( 305 void BluetoothEventRouter::AdapterDiscoveringChanged(
294 device::BluetoothAdapter* adapter, 306 device::BluetoothAdapter* adapter,
295 bool discovering) { 307 bool discovering) {
296 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 308 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
297 if (adapter != adapter_.get()) { 309 if (adapter != adapter_.get()) {
298 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); 310 BLUETOOTH_LOG(DEBUG) << "Ignoring event for adapter "
311 << adapter->GetAddress();
299 return; 312 return;
300 } 313 }
301 314
302 if (!discovering) { 315 if (!discovering) {
303 // If any discovery sessions are inactive, clean them up. 316 // If any discovery sessions are inactive, clean them up.
304 DiscoverySessionMap active_session_map; 317 DiscoverySessionMap active_session_map;
305 for (DiscoverySessionMap::iterator iter = discovery_session_map_.begin(); 318 for (DiscoverySessionMap::iterator iter = discovery_session_map_.begin();
306 iter != discovery_session_map_.end(); 319 iter != discovery_session_map_.end(); ++iter) {
307 ++iter) {
308 device::BluetoothDiscoverySession* session = iter->second; 320 device::BluetoothDiscoverySession* session = iter->second;
309 if (session->IsActive()) { 321 if (session->IsActive()) {
310 active_session_map[iter->first] = session; 322 active_session_map[iter->first] = session;
311 continue; 323 continue;
312 } 324 }
313 delete session; 325 delete session;
314 } 326 }
315 discovery_session_map_.swap(active_session_map); 327 discovery_session_map_.swap(active_session_map);
316 } 328 }
317 329
318 DispatchAdapterStateEvent(); 330 DispatchAdapterStateEvent();
319 331
320 // Release the adapter after dispatching the event. 332 // Release the adapter after dispatching the event.
321 if (!discovering) 333 if (!discovering)
322 MaybeReleaseAdapter(); 334 MaybeReleaseAdapter();
323 } 335 }
324 336
325 void BluetoothEventRouter::DeviceAdded(device::BluetoothAdapter* adapter, 337 void BluetoothEventRouter::DeviceAdded(device::BluetoothAdapter* adapter,
326 device::BluetoothDevice* device) { 338 device::BluetoothDevice* device) {
327 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 339 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
328 if (adapter != adapter_.get()) { 340 if (adapter != adapter_.get()) {
329 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); 341 BLUETOOTH_LOG(DEBUG) << "Ignoring event for adapter "
342 << adapter->GetAddress();
330 return; 343 return;
331 } 344 }
332 345
333 DispatchDeviceEvent(events::BLUETOOTH_ON_DEVICE_ADDED, 346 DispatchDeviceEvent(events::BLUETOOTH_ON_DEVICE_ADDED,
334 bluetooth::OnDeviceAdded::kEventName, device); 347 bluetooth::OnDeviceAdded::kEventName, device);
335 } 348 }
336 349
337 void BluetoothEventRouter::DeviceChanged(device::BluetoothAdapter* adapter, 350 void BluetoothEventRouter::DeviceChanged(device::BluetoothAdapter* adapter,
338 device::BluetoothDevice* device) { 351 device::BluetoothDevice* device) {
339 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 352 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
340 if (adapter != adapter_.get()) { 353 if (adapter != adapter_.get()) {
341 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); 354 BLUETOOTH_LOG(DEBUG) << "Ignoring event for adapter "
355 << adapter->GetAddress();
342 return; 356 return;
343 } 357 }
344 358
345 DispatchDeviceEvent(events::BLUETOOTH_ON_DEVICE_CHANGED, 359 DispatchDeviceEvent(events::BLUETOOTH_ON_DEVICE_CHANGED,
346 bluetooth::OnDeviceChanged::kEventName, device); 360 bluetooth::OnDeviceChanged::kEventName, device);
347 } 361 }
348 362
349 void BluetoothEventRouter::DeviceRemoved(device::BluetoothAdapter* adapter, 363 void BluetoothEventRouter::DeviceRemoved(device::BluetoothAdapter* adapter,
350 device::BluetoothDevice* device) { 364 device::BluetoothDevice* device) {
351 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 365 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
352 if (adapter != adapter_.get()) { 366 if (adapter != adapter_.get()) {
353 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); 367 BLUETOOTH_LOG(DEBUG) << "Ignoring event for adapter "
368 << adapter->GetAddress();
354 return; 369 return;
355 } 370 }
356 371
357 DispatchDeviceEvent(events::BLUETOOTH_ON_DEVICE_REMOVED, 372 DispatchDeviceEvent(events::BLUETOOTH_ON_DEVICE_REMOVED,
358 bluetooth::OnDeviceRemoved::kEventName, device); 373 bluetooth::OnDeviceRemoved::kEventName, device);
359 } 374 }
360 375
361 void BluetoothEventRouter::OnListenerAdded() { 376 void BluetoothEventRouter::OnListenerAdded(const EventListenerInfo& details) {
362 num_event_listeners_++;
363 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 377 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
378 std::string id = GetListenerId(details);
379 int count = ++event_listener_count_[id];
380 BLUETOOTH_LOG(EVENT) << "Event Listener Added: " << id << " Count: " << count;
364 if (!adapter_.get()) 381 if (!adapter_.get())
365 GetAdapter(base::Bind(&IgnoreAdapterResult)); 382 GetAdapter(base::Bind(&IgnoreAdapterResult));
366 } 383 }
367 384
368 void BluetoothEventRouter::OnListenerRemoved() { 385 void BluetoothEventRouter::OnListenerRemoved(const EventListenerInfo& details) {
369 if (num_event_listeners_ > 0) 386 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
370 num_event_listeners_--; 387 std::string id = GetListenerId(details);
388 auto iter = event_listener_count_.find(id);
389 CHECK(iter != event_listener_count_.end());
390 int count = --(iter->second);
391 BLUETOOTH_LOG(EVENT) << "Event Listener Removed: " << id
392 << " Count: " << count;
393 if (count == 0) {
394 event_listener_count_.erase(iter);
395 // When all listeners for a listener id have been removed, remove any
396 // pairing delegate or discovery session and filters.
397 CleanUpForExtension(id);
398 }
371 MaybeReleaseAdapter(); 399 MaybeReleaseAdapter();
372 } 400 }
373 401
374 void BluetoothEventRouter::DispatchAdapterStateEvent() { 402 void BluetoothEventRouter::DispatchAdapterStateEvent() {
375 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 403 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
376 api::bluetooth::AdapterState state; 404 api::bluetooth::AdapterState state;
377 CHECK(adapter_.get()); 405 CHECK(adapter_.get());
378 PopulateAdapterState(*adapter_, &state); 406 PopulateAdapterState(*adapter_, &state);
379 407
380 std::unique_ptr<base::ListValue> args = 408 std::unique_ptr<base::ListValue> args =
(...skipping 14 matching lines...) Expand all
395 423
396 std::unique_ptr<base::ListValue> args = 424 std::unique_ptr<base::ListValue> args =
397 bluetooth::OnDeviceAdded::Create(extension_device); 425 bluetooth::OnDeviceAdded::Create(extension_device);
398 std::unique_ptr<Event> event( 426 std::unique_ptr<Event> event(
399 new Event(histogram_value, event_name, std::move(args))); 427 new Event(histogram_value, event_name, std::move(args)));
400 EventRouter::Get(browser_context_)->BroadcastEvent(std::move(event)); 428 EventRouter::Get(browser_context_)->BroadcastEvent(std::move(event));
401 } 429 }
402 430
403 void BluetoothEventRouter::CleanUpForExtension( 431 void BluetoothEventRouter::CleanUpForExtension(
404 const std::string& extension_id) { 432 const std::string& extension_id) {
433 BLUETOOTH_LOG(DEBUG) << "CleanUpForExtension: " << extension_id;
405 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 434 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
406 RemovePairingDelegate(extension_id); 435 RemovePairingDelegate(extension_id);
407 436
408 PreSetFilterMap::iterator pre_set_iter = 437 PreSetFilterMap::iterator pre_set_iter =
409 pre_set_filter_map_.find(extension_id); 438 pre_set_filter_map_.find(extension_id);
410 if (pre_set_iter != pre_set_filter_map_.end()) { 439 if (pre_set_iter != pre_set_filter_map_.end()) {
411 delete pre_set_iter->second; 440 delete pre_set_iter->second;
412 pre_set_filter_map_.erase(pre_set_iter); 441 pre_set_filter_map_.erase(pre_set_iter);
413 } 442 }
414 443
415 // Remove any discovery session initiated by the extension. 444 // Remove any discovery session initiated by the extension.
416 DiscoverySessionMap::iterator session_iter = 445 DiscoverySessionMap::iterator session_iter =
417 discovery_session_map_.find(extension_id); 446 discovery_session_map_.find(extension_id);
418 if (session_iter == discovery_session_map_.end()) 447 if (session_iter == discovery_session_map_.end())
419 return; 448 return;
420 delete session_iter->second; 449 delete session_iter->second;
421 discovery_session_map_.erase(session_iter); 450 discovery_session_map_.erase(session_iter);
422 } 451 }
423 452
424 void BluetoothEventRouter::CleanUpAllExtensions() { 453 void BluetoothEventRouter::CleanUpAllExtensions() {
454 BLUETOOTH_LOG(DEBUG) << "CleanUpAllExtensions";
455
425 for (auto& it : pre_set_filter_map_) 456 for (auto& it : pre_set_filter_map_)
426 delete it.second; 457 delete it.second;
427
428 pre_set_filter_map_.clear(); 458 pre_set_filter_map_.clear();
429 459
430 for (auto& it : discovery_session_map_) 460 for (auto& it : discovery_session_map_) {
461 BLUETOOTH_LOG(DEBUG) << "Clean up Discovery Session: " << it.first;
431 delete it.second; 462 delete it.second;
432 463 }
433 discovery_session_map_.clear(); 464 discovery_session_map_.clear();
434 465
435 PairingDelegateMap::iterator pairing_iter = pairing_delegate_map_.begin(); 466 PairingDelegateMap::iterator pairing_iter = pairing_delegate_map_.begin();
436 while (pairing_iter != pairing_delegate_map_.end()) 467 while (pairing_iter != pairing_delegate_map_.end())
437 RemovePairingDelegate(pairing_iter++->first); 468 RemovePairingDelegate(pairing_iter++->first);
438 } 469 }
439 470
440 void BluetoothEventRouter::OnStartDiscoverySession( 471 void BluetoothEventRouter::OnStartDiscoverySession(
441 const std::string& extension_id, 472 const std::string& extension_id,
442 const base::Closure& callback, 473 const base::Closure& callback,
443 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session) { 474 std::unique_ptr<device::BluetoothDiscoverySession> discovery_session) {
475 BLUETOOTH_LOG(EVENT) << "OnStartDiscoverySession: " << extension_id;
444 // Clean up any existing session instance for the extension. 476 // Clean up any existing session instance for the extension.
445 DiscoverySessionMap::iterator iter = 477 DiscoverySessionMap::iterator iter =
446 discovery_session_map_.find(extension_id); 478 discovery_session_map_.find(extension_id);
447 if (iter != discovery_session_map_.end()) 479 if (iter != discovery_session_map_.end())
448 delete iter->second; 480 delete iter->second;
449 discovery_session_map_[extension_id] = discovery_session.release(); 481 discovery_session_map_[extension_id] = discovery_session.release();
450 callback.Run(); 482 callback.Run();
451 } 483 }
452 484
453 void BluetoothEventRouter::OnSetDiscoveryFilter(const std::string& extension_id, 485 void BluetoothEventRouter::OnSetDiscoveryFilter(const std::string& extension_id,
454 const base::Closure& callback) { 486 const base::Closure& callback) {
455 DVLOG(1) << "Successfully set DiscoveryFilter."; 487 BLUETOOTH_LOG(DEBUG) << "Successfully set DiscoveryFilter.";
456 callback.Run(); 488 callback.Run();
457 } 489 }
458 490
459 void BluetoothEventRouter::Observe( 491 void BluetoothEventRouter::Observe(
460 int type, 492 int type,
461 const content::NotificationSource& source, 493 const content::NotificationSource& source,
462 const content::NotificationDetails& details) { 494 const content::NotificationDetails& details) {
463 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 495 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
464 DCHECK_EQ(extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, type); 496 DCHECK_EQ(extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, type);
465 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); 497 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
498 BLUETOOTH_LOG(DEBUG) << "Host Destroyed: " << host->extension_id();
466 CleanUpForExtension(host->extension_id()); 499 CleanUpForExtension(host->extension_id());
467 } 500 }
468 501
469 void BluetoothEventRouter::OnExtensionUnloaded( 502 void BluetoothEventRouter::OnExtensionUnloaded(
470 content::BrowserContext* browser_context, 503 content::BrowserContext* browser_context,
471 const Extension* extension, 504 const Extension* extension,
472 UnloadedExtensionInfo::Reason reason) { 505 UnloadedExtensionInfo::Reason reason) {
473 CleanUpForExtension(extension->id()); 506 CleanUpForExtension(extension->id());
474 } 507 }
475 508
476 } // namespace extensions 509 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698