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

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

Issue 1083163002: Expose SetDiscoveryFilter from BluetoothEventRouter (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Another windows build fix Created 5 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 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
94 // Check whether user pre set discovery filter by calling SetDiscoveryFilter
95 // before. If the user has set a discovery filter then start a filtered
96 // discovery session, otherwise start a regular session
97 PreSetFilterMap::iterator pre_set_iter =
98 pre_set_filter_map_.find(extension_id);
99 if (pre_set_iter != pre_set_filter_map_.end()) {
100 adapter->StartDiscoverySessionWithFilter(
101 scoped_ptr<device::BluetoothDiscoveryFilter>(pre_set_iter->second),
102 base::Bind(&BluetoothEventRouter::OnStartDiscoverySession,
103 weak_ptr_factory_.GetWeakPtr(), extension_id, callback),
104 error_callback);
105 pre_set_filter_map_.erase(pre_set_iter);
106 return;
107 }
93 adapter->StartDiscoverySession( 108 adapter->StartDiscoverySession(
94 base::Bind(&BluetoothEventRouter::OnStartDiscoverySession, 109 base::Bind(&BluetoothEventRouter::OnStartDiscoverySession,
95 weak_ptr_factory_.GetWeakPtr(), 110 weak_ptr_factory_.GetWeakPtr(), extension_id, callback),
96 extension_id,
97 callback),
98 error_callback); 111 error_callback);
99 } 112 }
100 113
101 void BluetoothEventRouter::StopDiscoverySession( 114 void BluetoothEventRouter::StopDiscoverySession(
102 device::BluetoothAdapter* adapter, 115 device::BluetoothAdapter* adapter,
103 const std::string& extension_id, 116 const std::string& extension_id,
104 const base::Closure& callback, 117 const base::Closure& callback,
105 const base::Closure& error_callback) { 118 const base::Closure& error_callback) {
106 if (adapter != adapter_.get()) { 119 if (adapter != adapter_.get()) {
107 error_callback.Run(); 120 error_callback.Run();
108 return; 121 return;
109 } 122 }
110 DiscoverySessionMap::iterator iter = 123 DiscoverySessionMap::iterator iter =
111 discovery_session_map_.find(extension_id); 124 discovery_session_map_.find(extension_id);
112 if (iter == discovery_session_map_.end() || !iter->second->IsActive()) { 125 if (iter == discovery_session_map_.end() || !iter->second->IsActive()) {
113 DVLOG(1) << "No active discovery session exists for extension."; 126 DVLOG(1) << "No active discovery session exists for extension.";
114 error_callback.Run(); 127 error_callback.Run();
115 return; 128 return;
116 } 129 }
117 device::BluetoothDiscoverySession* session = iter->second; 130 device::BluetoothDiscoverySession* session = iter->second;
118 session->Stop(callback, error_callback); 131 session->Stop(callback, error_callback);
119 } 132 }
120 133
134 void BluetoothEventRouter::SetDiscoveryFilter(
135 scoped_ptr<device::BluetoothDiscoveryFilter> discovery_filter,
136 device::BluetoothAdapter* adapter,
137 const std::string& extension_id,
138 const base::Closure& callback,
139 const base::Closure& error_callback) {
140 DVLOG(1) << "SetDiscoveryFilter";
141 if (adapter != adapter_.get()) {
142 error_callback.Run();
143 return;
144 }
145
146 DiscoverySessionMap::iterator iter =
147 discovery_session_map_.find(extension_id);
148 if (iter == discovery_session_map_.end() || !iter->second->IsActive()) {
149 DVLOG(1) << "No active discovery session exists for extension, so caching "
150 "filter for later use.";
151 pre_set_filter_map_[extension_id] = discovery_filter.release();
152 callback.Run();
153 return;
154 }
155
156 // extension is already running discovery, update it's discovery filter
157 iter->second->SetDiscoveryFilter(discovery_filter.Pass(), callback,
158 error_callback);
159 }
160
121 BluetoothApiPairingDelegate* BluetoothEventRouter::GetPairingDelegate( 161 BluetoothApiPairingDelegate* BluetoothEventRouter::GetPairingDelegate(
122 const std::string& extension_id) { 162 const std::string& extension_id) {
123 return ContainsKey(pairing_delegate_map_, extension_id) 163 return ContainsKey(pairing_delegate_map_, extension_id)
124 ? pairing_delegate_map_[extension_id] 164 ? pairing_delegate_map_[extension_id]
125 : NULL; 165 : NULL;
126 } 166 }
127 167
128 void BluetoothEventRouter::OnAdapterInitialized( 168 void BluetoothEventRouter::OnAdapterInitialized(
129 const base::Closure& callback, 169 const base::Closure& callback,
130 scoped_refptr<device::BluetoothAdapter> adapter) { 170 scoped_refptr<device::BluetoothAdapter> adapter) {
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
305 bluetooth::OnDeviceAdded::Create(extension_device); 345 bluetooth::OnDeviceAdded::Create(extension_device);
306 scoped_ptr<Event> event(new Event(event_name, args.Pass())); 346 scoped_ptr<Event> event(new Event(event_name, args.Pass()));
307 EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass()); 347 EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass());
308 } 348 }
309 349
310 void BluetoothEventRouter::CleanUpForExtension( 350 void BluetoothEventRouter::CleanUpForExtension(
311 const std::string& extension_id) { 351 const std::string& extension_id) {
312 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 352 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
313 RemovePairingDelegate(extension_id); 353 RemovePairingDelegate(extension_id);
314 354
355 PreSetFilterMap::iterator pre_set_iter =
356 pre_set_filter_map_.find(extension_id);
357 if (pre_set_iter != pre_set_filter_map_.end()) {
358 delete pre_set_iter->second;
359 pre_set_filter_map_.erase(pre_set_iter);
360 }
361
315 // Remove any discovery session initiated by the extension. 362 // Remove any discovery session initiated by the extension.
316 DiscoverySessionMap::iterator session_iter = 363 DiscoverySessionMap::iterator session_iter =
317 discovery_session_map_.find(extension_id); 364 discovery_session_map_.find(extension_id);
318 if (session_iter == discovery_session_map_.end()) 365 if (session_iter == discovery_session_map_.end())
319 return; 366 return;
320 delete session_iter->second; 367 delete session_iter->second;
321 discovery_session_map_.erase(session_iter); 368 discovery_session_map_.erase(session_iter);
322 } 369 }
323 370
324 void BluetoothEventRouter::CleanUpAllExtensions() { 371 void BluetoothEventRouter::CleanUpAllExtensions() {
325 for (DiscoverySessionMap::iterator it = discovery_session_map_.begin(); 372 for (auto& it : pre_set_filter_map_)
326 it != discovery_session_map_.end(); 373 delete it.second;
327 ++it) { 374
328 delete it->second; 375 pre_set_filter_map_.clear();
329 } 376
377 for (auto& it : discovery_session_map_)
378 delete it.second;
379
330 discovery_session_map_.clear(); 380 discovery_session_map_.clear();
331 381
332 PairingDelegateMap::iterator pairing_iter = pairing_delegate_map_.begin(); 382 PairingDelegateMap::iterator pairing_iter = pairing_delegate_map_.begin();
333 while (pairing_iter != pairing_delegate_map_.end()) 383 while (pairing_iter != pairing_delegate_map_.end())
334 RemovePairingDelegate(pairing_iter++->first); 384 RemovePairingDelegate(pairing_iter++->first);
335 } 385 }
336 386
337 void BluetoothEventRouter::OnStartDiscoverySession( 387 void BluetoothEventRouter::OnStartDiscoverySession(
338 const std::string& extension_id, 388 const std::string& extension_id,
339 const base::Closure& callback, 389 const base::Closure& callback,
340 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { 390 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) {
341 // Clean up any existing session instance for the extension. 391 // Clean up any existing session instance for the extension.
342 DiscoverySessionMap::iterator iter = 392 DiscoverySessionMap::iterator iter =
343 discovery_session_map_.find(extension_id); 393 discovery_session_map_.find(extension_id);
344 if (iter != discovery_session_map_.end()) 394 if (iter != discovery_session_map_.end())
345 delete iter->second; 395 delete iter->second;
346 discovery_session_map_[extension_id] = discovery_session.release(); 396 discovery_session_map_[extension_id] = discovery_session.release();
347 callback.Run(); 397 callback.Run();
348 } 398 }
349 399
400 void BluetoothEventRouter::OnSetDiscoveryFilter(const std::string& extension_id,
401 const base::Closure& callback) {
402 DVLOG(1) << "Successfully set DiscoveryFilter.";
403 callback.Run();
404 }
405
350 void BluetoothEventRouter::Observe( 406 void BluetoothEventRouter::Observe(
351 int type, 407 int type,
352 const content::NotificationSource& source, 408 const content::NotificationSource& source,
353 const content::NotificationDetails& details) { 409 const content::NotificationDetails& details) {
354 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 410 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
355 DCHECK_EQ(extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, type); 411 DCHECK_EQ(extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, type);
356 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); 412 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
357 CleanUpForExtension(host->extension_id()); 413 CleanUpForExtension(host->extension_id());
358 } 414 }
359 415
360 void BluetoothEventRouter::OnExtensionUnloaded( 416 void BluetoothEventRouter::OnExtensionUnloaded(
361 content::BrowserContext* browser_context, 417 content::BrowserContext* browser_context,
362 const Extension* extension, 418 const Extension* extension,
363 UnloadedExtensionInfo::Reason reason) { 419 UnloadedExtensionInfo::Reason reason) {
364 CleanUpForExtension(extension->id()); 420 CleanUpForExtension(extension->id());
365 } 421 }
366 422
367 } // namespace extensions 423 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698