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 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
83 error_callback.Run(); | 83 error_callback.Run(); |
84 return; | 84 return; |
85 } | 85 } |
86 DiscoverySessionMap::iterator iter = | 86 DiscoverySessionMap::iterator iter = |
87 discovery_session_map_.find(extension_id); | 87 discovery_session_map_.find(extension_id); |
88 if (iter != discovery_session_map_.end() && iter->second->IsActive()) { | 88 if (iter != discovery_session_map_.end() && iter->second->IsActive()) { |
89 DVLOG(1) << "An active discovery session exists for extension."; | 89 DVLOG(1) << "An active discovery session exists for extension."; |
90 error_callback.Run(); | 90 error_callback.Run(); |
91 return; | 91 return; |
92 } | 92 } |
93 adapter->StartDiscoverySession( | 93 |
armansito
2015/04/16 19:13:54
Add comments explaining what's going on here.
jpawlowski1
2015/04/16 19:36:27
Done.
| |
94 base::Bind(&BluetoothEventRouter::OnStartDiscoverySession, | 94 PreSetFilterMap::iterator pre_set_iter = |
95 weak_ptr_factory_.GetWeakPtr(), | 95 pre_set_filter_map_.find(extension_id); |
96 extension_id, | 96 if (pre_set_iter != pre_set_filter_map_.end()) { |
97 callback), | 97 adapter->StartDiscoverySessionWithFilter( |
98 error_callback); | 98 scoped_ptr<device::BluetoothDiscoveryFilter>(pre_set_iter->second), |
99 base::Bind(&BluetoothEventRouter::OnStartDiscoverySession, | |
100 weak_ptr_factory_.GetWeakPtr(), extension_id, callback), | |
101 error_callback); | |
102 pre_set_filter_map_.erase(pre_set_iter); | |
armansito
2015/04/16 19:13:55
Do an early return and drop 'else'.
jpawlowski1
2015/04/16 19:36:27
Done.
| |
103 } else { | |
104 adapter->StartDiscoverySession( | |
105 base::Bind(&BluetoothEventRouter::OnStartDiscoverySession, | |
106 weak_ptr_factory_.GetWeakPtr(), extension_id, callback), | |
107 error_callback); | |
108 } | |
99 } | 109 } |
100 | 110 |
101 void BluetoothEventRouter::StopDiscoverySession( | 111 void BluetoothEventRouter::StopDiscoverySession( |
102 device::BluetoothAdapter* adapter, | 112 device::BluetoothAdapter* adapter, |
103 const std::string& extension_id, | 113 const std::string& extension_id, |
104 const base::Closure& callback, | 114 const base::Closure& callback, |
105 const base::Closure& error_callback) { | 115 const base::Closure& error_callback) { |
106 if (adapter != adapter_.get()) { | 116 if (adapter != adapter_.get()) { |
107 error_callback.Run(); | 117 error_callback.Run(); |
108 return; | 118 return; |
109 } | 119 } |
110 DiscoverySessionMap::iterator iter = | 120 DiscoverySessionMap::iterator iter = |
111 discovery_session_map_.find(extension_id); | 121 discovery_session_map_.find(extension_id); |
112 if (iter == discovery_session_map_.end() || !iter->second->IsActive()) { | 122 if (iter == discovery_session_map_.end() || !iter->second->IsActive()) { |
113 DVLOG(1) << "No active discovery session exists for extension."; | 123 DVLOG(1) << "No active discovery session exists for extension."; |
114 error_callback.Run(); | 124 error_callback.Run(); |
115 return; | 125 return; |
116 } | 126 } |
117 device::BluetoothDiscoverySession* session = iter->second; | 127 device::BluetoothDiscoverySession* session = iter->second; |
118 session->Stop(callback, error_callback); | 128 session->Stop(callback, error_callback); |
119 } | 129 } |
120 | 130 |
131 void BluetoothEventRouter::SetDiscoveryFilter( | |
132 scoped_ptr<device::BluetoothDiscoveryFilter> discovery_filter, | |
133 device::BluetoothAdapter* adapter, | |
134 const std::string& extension_id, | |
135 const base::Closure& callback, | |
136 const base::Closure& error_callback) { | |
137 VLOG(1) << __func__; | |
armansito
2015/04/16 19:13:54
Prefer DVLOG
jpawlowski1
2015/04/16 19:36:27
Done.
| |
138 if (adapter != adapter_.get()) { | |
139 error_callback.Run(); | |
140 return; | |
141 } | |
142 | |
143 DiscoverySessionMap::iterator iter = | |
144 discovery_session_map_.find(extension_id); | |
145 if (iter == discovery_session_map_.end() || !iter->second->IsActive()) { | |
146 VLOG(1) << "No active discovery session exists for extension."; | |
armansito
2015/04/16 19:13:55
This may be interpreted incorrectly as an error ca
armansito
2015/04/16 19:13:55
DVLOG
jpawlowski1
2015/04/16 19:36:27
Done.
jpawlowski1
2015/04/16 19:36:27
Done.
| |
147 pre_set_filter_map_[extension_id] = discovery_filter.release(); | |
148 callback.Run(); | |
149 return; | |
150 } | |
151 | |
152 // extension is already running discovery, update it's discovery filter | |
153 iter->second->SetDiscoveryFilter(discovery_filter.Pass(), callback, | |
154 error_callback); | |
155 } | |
156 | |
121 BluetoothApiPairingDelegate* BluetoothEventRouter::GetPairingDelegate( | 157 BluetoothApiPairingDelegate* BluetoothEventRouter::GetPairingDelegate( |
122 const std::string& extension_id) { | 158 const std::string& extension_id) { |
123 return ContainsKey(pairing_delegate_map_, extension_id) | 159 return ContainsKey(pairing_delegate_map_, extension_id) |
124 ? pairing_delegate_map_[extension_id] | 160 ? pairing_delegate_map_[extension_id] |
125 : NULL; | 161 : NULL; |
126 } | 162 } |
127 | 163 |
128 void BluetoothEventRouter::OnAdapterInitialized( | 164 void BluetoothEventRouter::OnAdapterInitialized( |
129 const base::Closure& callback, | 165 const base::Closure& callback, |
130 scoped_refptr<device::BluetoothAdapter> adapter) { | 166 scoped_refptr<device::BluetoothAdapter> adapter) { |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
305 bluetooth::OnDeviceAdded::Create(extension_device); | 341 bluetooth::OnDeviceAdded::Create(extension_device); |
306 scoped_ptr<Event> event(new Event(event_name, args.Pass())); | 342 scoped_ptr<Event> event(new Event(event_name, args.Pass())); |
307 EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); | 343 EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); |
308 } | 344 } |
309 | 345 |
310 void BluetoothEventRouter::CleanUpForExtension( | 346 void BluetoothEventRouter::CleanUpForExtension( |
311 const std::string& extension_id) { | 347 const std::string& extension_id) { |
312 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 348 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
313 RemovePairingDelegate(extension_id); | 349 RemovePairingDelegate(extension_id); |
314 | 350 |
351 PreSetFilterMap::iterator pre_set_iter = | |
352 pre_set_filter_map_.find(extension_id); | |
353 if (pre_set_iter != pre_set_filter_map_.end()) { | |
354 delete pre_set_iter->second; | |
355 pre_set_filter_map_.erase(pre_set_iter); | |
356 } | |
357 | |
315 // Remove any discovery session initiated by the extension. | 358 // Remove any discovery session initiated by the extension. |
316 DiscoverySessionMap::iterator session_iter = | 359 DiscoverySessionMap::iterator session_iter = |
317 discovery_session_map_.find(extension_id); | 360 discovery_session_map_.find(extension_id); |
318 if (session_iter == discovery_session_map_.end()) | 361 if (session_iter == discovery_session_map_.end()) |
319 return; | 362 return; |
320 delete session_iter->second; | 363 delete session_iter->second; |
321 discovery_session_map_.erase(session_iter); | 364 discovery_session_map_.erase(session_iter); |
322 } | 365 } |
323 | 366 |
324 void BluetoothEventRouter::CleanUpAllExtensions() { | 367 void BluetoothEventRouter::CleanUpAllExtensions() { |
368 for (PreSetFilterMap::iterator it = pre_set_filter_map_.begin(); | |
369 it != pre_set_filter_map_.end(); it++) { | |
armansito
2015/04/16 19:13:54
for (auto& it : pre_set_filter_map_)
jpawlowski1
2015/04/16 19:36:27
Done.
| |
370 delete it->second; | |
371 } | |
372 pre_set_filter_map_.clear(); | |
373 | |
325 for (DiscoverySessionMap::iterator it = discovery_session_map_.begin(); | 374 for (DiscoverySessionMap::iterator it = discovery_session_map_.begin(); |
326 it != discovery_session_map_.end(); | 375 it != discovery_session_map_.end(); |
327 ++it) { | 376 ++it) { |
328 delete it->second; | 377 delete it->second; |
329 } | 378 } |
330 discovery_session_map_.clear(); | 379 discovery_session_map_.clear(); |
331 | 380 |
332 PairingDelegateMap::iterator pairing_iter = pairing_delegate_map_.begin(); | 381 PairingDelegateMap::iterator pairing_iter = pairing_delegate_map_.begin(); |
333 while (pairing_iter != pairing_delegate_map_.end()) | 382 while (pairing_iter != pairing_delegate_map_.end()) |
334 RemovePairingDelegate(pairing_iter++->first); | 383 RemovePairingDelegate(pairing_iter++->first); |
335 } | 384 } |
336 | 385 |
337 void BluetoothEventRouter::OnStartDiscoverySession( | 386 void BluetoothEventRouter::OnStartDiscoverySession( |
338 const std::string& extension_id, | 387 const std::string& extension_id, |
339 const base::Closure& callback, | 388 const base::Closure& callback, |
340 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { | 389 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { |
341 // Clean up any existing session instance for the extension. | 390 // Clean up any existing session instance for the extension. |
342 DiscoverySessionMap::iterator iter = | 391 DiscoverySessionMap::iterator iter = |
343 discovery_session_map_.find(extension_id); | 392 discovery_session_map_.find(extension_id); |
344 if (iter != discovery_session_map_.end()) | 393 if (iter != discovery_session_map_.end()) |
345 delete iter->second; | 394 delete iter->second; |
346 discovery_session_map_[extension_id] = discovery_session.release(); | 395 discovery_session_map_[extension_id] = discovery_session.release(); |
347 callback.Run(); | 396 callback.Run(); |
348 } | 397 } |
349 | 398 |
399 void BluetoothEventRouter::OnSetDiscoveryFilter(const std::string& extension_id, | |
400 const base::Closure& callback) { | |
armansito
2015/04/16 19:13:55
DVLOG success?
jpawlowski1
2015/04/16 19:36:27
Done.
| |
401 callback.Run(); | |
402 } | |
403 | |
350 void BluetoothEventRouter::Observe( | 404 void BluetoothEventRouter::Observe( |
351 int type, | 405 int type, |
352 const content::NotificationSource& source, | 406 const content::NotificationSource& source, |
353 const content::NotificationDetails& details) { | 407 const content::NotificationDetails& details) { |
354 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 408 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
355 DCHECK_EQ(extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, type); | 409 DCHECK_EQ(extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, type); |
356 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); | 410 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); |
357 CleanUpForExtension(host->extension_id()); | 411 CleanUpForExtension(host->extension_id()); |
358 } | 412 } |
359 | 413 |
360 void BluetoothEventRouter::OnExtensionUnloaded( | 414 void BluetoothEventRouter::OnExtensionUnloaded( |
361 content::BrowserContext* browser_context, | 415 content::BrowserContext* browser_context, |
362 const Extension* extension, | 416 const Extension* extension, |
363 UnloadedExtensionInfo::Reason reason) { | 417 UnloadedExtensionInfo::Reason reason) { |
364 CleanUpForExtension(extension->id()); | 418 CleanUpForExtension(extension->id()); |
365 } | 419 } |
366 | 420 |
367 } // namespace extensions | 421 } // namespace extensions |
OLD | NEW |