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

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

Issue 189463002: Migrate chrome.bluetooth API backend to use device::BluetoothDiscoverySession. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 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 | Annotate | Revision Log
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 "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/json/json_writer.h" 11 #include "base/json/json_writer.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_vector.h" 14 #include "base/memory/scoped_vector.h"
15 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
16 #include "chrome/browser/chrome_notification_types.h" 16 #include "chrome/browser/chrome_notification_types.h"
17 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" 17 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h"
18 #include "chrome/browser/extensions/event_names.h" 18 #include "chrome/browser/extensions/event_names.h"
19 #include "chrome/common/extensions/api/bluetooth.h" 19 #include "chrome/common/extensions/api/bluetooth.h"
20 #include "content/public/browser/notification_details.h" 20 #include "content/public/browser/notification_details.h"
21 #include "content/public/browser/notification_source.h" 21 #include "content/public/browser/notification_source.h"
22 #include "device/bluetooth/bluetooth_adapter.h" 22 #include "device/bluetooth/bluetooth_adapter.h"
23 #include "device/bluetooth/bluetooth_adapter_factory.h" 23 #include "device/bluetooth/bluetooth_adapter_factory.h"
24 #include "device/bluetooth/bluetooth_device.h" 24 #include "device/bluetooth/bluetooth_device.h"
25 #include "device/bluetooth/bluetooth_discovery_session.h"
25 #include "device/bluetooth/bluetooth_profile.h" 26 #include "device/bluetooth/bluetooth_profile.h"
26 #include "device/bluetooth/bluetooth_socket.h" 27 #include "device/bluetooth/bluetooth_socket.h"
27 #include "extensions/browser/event_router.h" 28 #include "extensions/browser/event_router.h"
28 #include "extensions/browser/extension_system.h" 29 #include "extensions/browser/extension_system.h"
29 30
30 namespace extensions { 31 namespace extensions {
31 32
32 namespace bluetooth = api::bluetooth; 33 namespace bluetooth = api::bluetooth;
33 34
34 // A struct storing a Bluetooth socket and the extension that added it. 35 // A struct storing a Bluetooth socket and the extension that added it.
35 struct ExtensionBluetoothSocketRecord { 36 struct ExtensionBluetoothSocketRecord {
36 std::string extension_id; 37 std::string extension_id;
37 scoped_refptr<device::BluetoothSocket> socket; 38 scoped_refptr<device::BluetoothSocket> socket;
38 }; 39 };
39 40
40 // A struct storing a Bluetooth profile and the extension that added it. 41 // A struct storing a Bluetooth profile and the extension that added it.
41 struct ExtensionBluetoothProfileRecord { 42 struct ExtensionBluetoothProfileRecord {
42 std::string extension_id; 43 std::string extension_id;
43 device::BluetoothProfile* profile; 44 device::BluetoothProfile* profile;
44 }; 45 };
45 46
46 ExtensionBluetoothEventRouter::ExtensionBluetoothEventRouter( 47 ExtensionBluetoothEventRouter::ExtensionBluetoothEventRouter(
47 content::BrowserContext* context) 48 content::BrowserContext* context)
48 : send_discovery_events_(false), 49 : browser_context_(context),
49 responsible_for_discovery_(false),
50 browser_context_(context),
51 adapter_(NULL), 50 adapter_(NULL),
52 num_event_listeners_(0), 51 num_event_listeners_(0),
53 next_socket_id_(1), 52 next_socket_id_(1),
54 weak_ptr_factory_(this) { 53 weak_ptr_factory_(this) {
55 DCHECK(browser_context_); 54 DCHECK(browser_context_);
56 registrar_.Add(this, 55 registrar_.Add(this,
57 chrome::NOTIFICATION_EXTENSION_UNLOADED, 56 chrome::NOTIFICATION_EXTENSION_UNLOADED,
58 content::Source<content::BrowserContext>(browser_context_)); 57 content::Source<content::BrowserContext>(browser_context_));
59 } 58 }
60 59
61 ExtensionBluetoothEventRouter::~ExtensionBluetoothEventRouter() { 60 ExtensionBluetoothEventRouter::~ExtensionBluetoothEventRouter() {
62 if (adapter_.get()) { 61 if (adapter_.get()) {
63 adapter_->RemoveObserver(this); 62 adapter_->RemoveObserver(this);
64 adapter_ = NULL; 63 adapter_ = NULL;
65 } 64 }
66 DLOG_IF(WARNING, socket_map_.size() != 0) 65 DLOG_IF(WARNING, socket_map_.size() != 0)
67 << "Bluetooth sockets are still open."; 66 << "Bluetooth sockets are still open.";
68 socket_map_.clear(); 67 socket_map_.clear();
69 68
70 for (BluetoothProfileMap::iterator iter = bluetooth_profile_map_.begin(); 69 for (BluetoothProfileMap::iterator iter = bluetooth_profile_map_.begin();
71 iter != bluetooth_profile_map_.end(); 70 iter != bluetooth_profile_map_.end();
72 ++iter) { 71 ++iter) {
73 iter->second.profile->Unregister(); 72 iter->second.profile->Unregister();
74 } 73 }
74 for (DiscoverySessionMap::iterator iter = discovery_session_map_.begin();
75 iter != discovery_session_map_.end();
76 ++iter) {
77 delete iter->second;
78 }
75 } 79 }
76 80
77 bool ExtensionBluetoothEventRouter::IsBluetoothSupported() const { 81 bool ExtensionBluetoothEventRouter::IsBluetoothSupported() const {
78 return adapter_.get() || 82 return adapter_.get() ||
79 device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable(); 83 device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable();
80 } 84 }
81 85
82 void ExtensionBluetoothEventRouter::GetAdapter( 86 void ExtensionBluetoothEventRouter::GetAdapter(
83 const device::BluetoothAdapterFactory::AdapterCallback& callback) { 87 const device::BluetoothAdapterFactory::AdapterCallback& callback) {
84 if (adapter_.get()) { 88 if (adapter_.get()) {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 device::BluetoothProfile* bluetooth_profile = iter->second.profile; 142 device::BluetoothProfile* bluetooth_profile = iter->second.profile;
139 bluetooth_profile_map_.erase(iter); 143 bluetooth_profile_map_.erase(iter);
140 bluetooth_profile->Unregister(); 144 bluetooth_profile->Unregister();
141 } 145 }
142 } 146 }
143 147
144 bool ExtensionBluetoothEventRouter::HasProfile(const std::string& uuid) const { 148 bool ExtensionBluetoothEventRouter::HasProfile(const std::string& uuid) const {
145 return bluetooth_profile_map_.find(uuid) != bluetooth_profile_map_.end(); 149 return bluetooth_profile_map_.find(uuid) != bluetooth_profile_map_.end();
146 } 150 }
147 151
152 void ExtensionBluetoothEventRouter::StartDiscoverySession(
153 device::BluetoothAdapter* adapter,
154 const std::string& extension_id,
155 const base::Closure& callback,
156 const base::Closure& error_callback) {
157 if (adapter != adapter_.get()) {
158 error_callback.Run();
159 return;
160 }
161 DiscoverySessionMap::iterator iter =
162 discovery_session_map_.find(extension_id);
163 if (iter != discovery_session_map_.end() && iter->second->IsActive()) {
164 DVLOG(1) << "An active discovery session exists for extension.";
165 error_callback.Run();
166 return;
167 }
168 adapter->StartDiscoverySession(
169 base::Bind(&ExtensionBluetoothEventRouter::OnStartDiscoverySession,
170 weak_ptr_factory_.GetWeakPtr(),
171 extension_id,
172 callback),
173 error_callback);
174 }
175
176 void ExtensionBluetoothEventRouter::StopDiscoverySession(
177 device::BluetoothAdapter* adapter,
178 const std::string& extension_id,
179 const base::Closure& callback,
180 const base::Closure& error_callback) {
181 if (adapter != adapter_.get()) {
182 error_callback.Run();
183 return;
184 }
185 DiscoverySessionMap::iterator iter =
186 discovery_session_map_.find(extension_id);
187 if (iter == discovery_session_map_.end() || !iter->second->IsActive()) {
188 DVLOG(1) << "No active discovery session exists for extension.";
189 error_callback.Run();
190 return;
191 }
192 device::BluetoothDiscoverySession* session = iter->second;
193 session->Stop(callback, error_callback);
194 }
195
148 device::BluetoothProfile* ExtensionBluetoothEventRouter::GetProfile( 196 device::BluetoothProfile* ExtensionBluetoothEventRouter::GetProfile(
149 const std::string& uuid) const { 197 const std::string& uuid) const {
150 BluetoothProfileMap::const_iterator iter = bluetooth_profile_map_.find(uuid); 198 BluetoothProfileMap::const_iterator iter = bluetooth_profile_map_.find(uuid);
151 if (iter != bluetooth_profile_map_.end()) 199 if (iter != bluetooth_profile_map_.end())
152 return iter->second.profile; 200 return iter->second.profile;
153 201
154 return NULL; 202 return NULL;
155 } 203 }
156 204
157 scoped_refptr<device::BluetoothSocket> 205 scoped_refptr<device::BluetoothSocket>
158 ExtensionBluetoothEventRouter::GetSocket(int id) { 206 ExtensionBluetoothEventRouter::GetSocket(int id) {
159 SocketMap::iterator socket_entry = socket_map_.find(id); 207 SocketMap::iterator socket_entry = socket_map_.find(id);
160 if (socket_entry == socket_map_.end()) 208 if (socket_entry == socket_map_.end())
161 return NULL; 209 return NULL;
162 return socket_entry->second.socket; 210 return socket_entry->second.socket;
163 } 211 }
164 212
165 void ExtensionBluetoothEventRouter::SetResponsibleForDiscovery(
166 bool responsible) {
167 responsible_for_discovery_ = responsible;
168 }
169
170 bool ExtensionBluetoothEventRouter::IsResponsibleForDiscovery() const {
171 return responsible_for_discovery_;
172 }
173
174 void ExtensionBluetoothEventRouter::SetSendDiscoveryEvents(bool should_send) {
175 // At the transition into sending devices, also send past devices that
176 // were discovered as they will not be discovered again.
177 if (should_send && !send_discovery_events_) {
178 for (DeviceList::const_iterator i = discovered_devices_.begin();
179 i != discovered_devices_.end(); ++i) {
180 DispatchDeviceEvent(extensions::event_names::kBluetoothOnDeviceDiscovered,
181 **i);
182 }
183 }
184
185 send_discovery_events_ = should_send;
186 }
187
188 void ExtensionBluetoothEventRouter::DispatchDeviceEvent( 213 void ExtensionBluetoothEventRouter::DispatchDeviceEvent(
189 const std::string& event_name, const bluetooth::Device& device) { 214 const std::string& event_name, const bluetooth::Device& device) {
190 scoped_ptr<base::ListValue> args(new base::ListValue()); 215 scoped_ptr<base::ListValue> args(new base::ListValue());
191 args->Append(device.ToValue().release()); 216 args->Append(device.ToValue().release());
192 scoped_ptr<Event> event(new Event(event_name, args.Pass())); 217 scoped_ptr<Event> event(new Event(event_name, args.Pass()));
193 ExtensionSystem::Get(browser_context_)->event_router()->BroadcastEvent( 218 ExtensionSystem::Get(browser_context_)->event_router()->BroadcastEvent(
194 event.Pass()); 219 event.Pass());
195 } 220 }
196 221
197 void ExtensionBluetoothEventRouter::DispatchConnectionEvent( 222 void ExtensionBluetoothEventRouter::DispatchConnectionEvent(
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 } 261 }
237 262
238 void ExtensionBluetoothEventRouter::AdapterDiscoveringChanged( 263 void ExtensionBluetoothEventRouter::AdapterDiscoveringChanged(
239 device::BluetoothAdapter* adapter, bool discovering) { 264 device::BluetoothAdapter* adapter, bool discovering) {
240 if (adapter != adapter_.get()) { 265 if (adapter != adapter_.get()) {
241 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); 266 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress();
242 return; 267 return;
243 } 268 }
244 269
245 if (!discovering) { 270 if (!discovering) {
246 send_discovery_events_ = false; 271 // If any discovery sessions are inactive, clean them up.
247 responsible_for_discovery_ = false; 272 DiscoverySessionMap active_session_map;
248 discovered_devices_.clear(); 273 for (DiscoverySessionMap::iterator iter = discovery_session_map_.begin();
274 iter != discovery_session_map_.end();
275 ++iter) {
276 device::BluetoothDiscoverySession* session = iter->second;
277 if (session->IsActive()) {
278 active_session_map[iter->first] = session;
279 continue;
280 }
281 delete session;
282 }
283 discovery_session_map_.swap(active_session_map);
249 } 284 }
250 285
251 DispatchAdapterStateEvent(); 286 DispatchAdapterStateEvent();
252 } 287 }
253 288
254 void ExtensionBluetoothEventRouter::DeviceAdded( 289 void ExtensionBluetoothEventRouter::DeviceAdded(
255 device::BluetoothAdapter* adapter, 290 device::BluetoothAdapter* adapter,
256 device::BluetoothDevice* device) { 291 device::BluetoothDevice* device) {
257 if (adapter != adapter_.get()) { 292 if (adapter != adapter_.get()) {
258 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); 293 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress();
259 return; 294 return;
260 } 295 }
261 296
262 bluetooth::Device* extension_device = 297 bluetooth::Device extension_device;
263 new bluetooth::Device(); 298 bluetooth::BluetoothDeviceToApiDevice(*device, &extension_device);
264 bluetooth::BluetoothDeviceToApiDevice(
265 *device, extension_device);
266 discovered_devices_.push_back(extension_device);
267
268 if (!send_discovery_events_)
269 return;
270 299
271 DispatchDeviceEvent(extensions::event_names::kBluetoothOnDeviceDiscovered, 300 DispatchDeviceEvent(extensions::event_names::kBluetoothOnDeviceDiscovered,
272 *extension_device); 301 extension_device);
273 } 302 }
274 303
275 void ExtensionBluetoothEventRouter::InitializeAdapterIfNeeded() { 304 void ExtensionBluetoothEventRouter::InitializeAdapterIfNeeded() {
276 if (!adapter_.get()) { 305 if (!adapter_.get()) {
277 GetAdapter(base::Bind(&ExtensionBluetoothEventRouter::InitializeAdapter, 306 GetAdapter(base::Bind(&ExtensionBluetoothEventRouter::InitializeAdapter,
278 weak_ptr_factory_.GetWeakPtr())); 307 weak_ptr_factory_.GetWeakPtr()));
279 } 308 }
280 } 309 }
281 310
282 void ExtensionBluetoothEventRouter::InitializeAdapter( 311 void ExtensionBluetoothEventRouter::InitializeAdapter(
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 // Remove all sockets opened by the extension. 353 // Remove all sockets opened by the extension.
325 SocketMap::iterator socket_iter = socket_map_.begin(); 354 SocketMap::iterator socket_iter = socket_map_.begin();
326 while (socket_iter != socket_map_.end()) { 355 while (socket_iter != socket_map_.end()) {
327 int socket_id = socket_iter->first; 356 int socket_id = socket_iter->first;
328 ExtensionBluetoothSocketRecord record = socket_iter->second; 357 ExtensionBluetoothSocketRecord record = socket_iter->second;
329 socket_iter++; 358 socket_iter++;
330 if (record.extension_id == extension_id) { 359 if (record.extension_id == extension_id) {
331 ReleaseSocket(socket_id); 360 ReleaseSocket(socket_id);
332 } 361 }
333 } 362 }
363
364 // Remove any discovery session initiated by the extension.
365 DiscoverySessionMap::iterator session_iter =
366 discovery_session_map_.find(extension_id);
367 if (session_iter == discovery_session_map_.end())
368 return;
369 delete session_iter->second;
370 discovery_session_map_.erase(session_iter);
371 }
372
373 void ExtensionBluetoothEventRouter::OnStartDiscoverySession(
374 const std::string& extension_id,
375 const base::Closure& callback,
376 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) {
377 // Clean up any existing session instance for the extension.
378 DiscoverySessionMap::iterator iter =
379 discovery_session_map_.find(extension_id);
380 if (iter != discovery_session_map_.end())
381 delete iter->second;
382 discovery_session_map_[extension_id] = discovery_session.release();
383 callback.Run();
334 } 384 }
335 385
336 void ExtensionBluetoothEventRouter::Observe( 386 void ExtensionBluetoothEventRouter::Observe(
337 int type, 387 int type,
338 const content::NotificationSource& source, 388 const content::NotificationSource& source,
339 const content::NotificationDetails& details) { 389 const content::NotificationDetails& details) {
340 switch (type) { 390 switch (type) {
341 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { 391 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
342 extensions::UnloadedExtensionInfo* info = 392 extensions::UnloadedExtensionInfo* info =
343 content::Details<extensions::UnloadedExtensionInfo>(details).ptr(); 393 content::Details<extensions::UnloadedExtensionInfo>(details).ptr();
344 CleanUpForExtension(info->extension->id()); 394 CleanUpForExtension(info->extension->id());
345 break; 395 break;
346 } 396 }
347 } 397 }
348 } 398 }
349 399
350 } // namespace extensions 400 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698