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 "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" |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include "chrome/common/extensions/api/bluetooth_private.h" | 23 #include "chrome/common/extensions/api/bluetooth_private.h" |
24 #include "content/public/browser/notification_details.h" | 24 #include "content/public/browser/notification_details.h" |
25 #include "content/public/browser/notification_source.h" | 25 #include "content/public/browser/notification_source.h" |
26 #include "device/bluetooth/bluetooth_adapter.h" | 26 #include "device/bluetooth/bluetooth_adapter.h" |
27 #include "device/bluetooth/bluetooth_adapter_factory.h" | 27 #include "device/bluetooth/bluetooth_adapter_factory.h" |
28 #include "device/bluetooth/bluetooth_device.h" | 28 #include "device/bluetooth/bluetooth_device.h" |
29 #include "device/bluetooth/bluetooth_discovery_session.h" | 29 #include "device/bluetooth/bluetooth_discovery_session.h" |
30 #include "device/bluetooth/bluetooth_profile.h" | 30 #include "device/bluetooth/bluetooth_profile.h" |
31 #include "device/bluetooth/bluetooth_socket.h" | 31 #include "device/bluetooth/bluetooth_socket.h" |
32 #include "extensions/browser/event_router.h" | 32 #include "extensions/browser/event_router.h" |
| 33 #include "extensions/browser/extension_host.h" |
33 #include "extensions/browser/extension_system.h" | 34 #include "extensions/browser/extension_system.h" |
34 | 35 |
35 namespace extensions { | 36 namespace extensions { |
36 | 37 |
37 namespace bluetooth = api::bluetooth; | 38 namespace bluetooth = api::bluetooth; |
38 namespace bt_private = api::bluetooth_private; | 39 namespace bt_private = api::bluetooth_private; |
39 | 40 |
40 // A struct storing a Bluetooth socket and the extension that added it. | |
41 struct BluetoothEventRouter::ExtensionBluetoothSocketRecord { | |
42 std::string extension_id; | |
43 scoped_refptr<device::BluetoothSocket> socket; | |
44 }; | |
45 | |
46 // 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. |
47 struct BluetoothEventRouter::ExtensionBluetoothProfileRecord { | 42 struct BluetoothEventRouter::ExtensionBluetoothProfileRecord { |
48 std::string extension_id; | 43 std::string extension_id; |
49 device::BluetoothProfile* profile; | 44 device::BluetoothProfile* profile; |
50 }; | 45 }; |
51 | 46 |
52 BluetoothEventRouter::BluetoothEventRouter(content::BrowserContext* context) | 47 BluetoothEventRouter::BluetoothEventRouter(content::BrowserContext* context) |
53 : browser_context_(context), | 48 : browser_context_(context), |
54 adapter_(NULL), | 49 adapter_(NULL), |
55 num_event_listeners_(0), | 50 num_event_listeners_(0), |
56 next_socket_id_(1), | |
57 weak_ptr_factory_(this) { | 51 weak_ptr_factory_(this) { |
| 52 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
58 DCHECK(browser_context_); | 53 DCHECK(browser_context_); |
59 registrar_.Add(this, | 54 registrar_.Add(this, |
60 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | 55 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, |
61 content::Source<content::BrowserContext>(browser_context_)); | 56 content::Source<content::BrowserContext>(browser_context_)); |
| 57 registrar_.Add(this, |
| 58 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 59 content::Source<content::BrowserContext>(browser_context_)); |
62 } | 60 } |
63 | 61 |
64 BluetoothEventRouter::~BluetoothEventRouter() { | 62 BluetoothEventRouter::~BluetoothEventRouter() { |
| 63 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
65 if (adapter_.get()) { | 64 if (adapter_.get()) { |
66 adapter_->RemoveObserver(this); | 65 adapter_->RemoveObserver(this); |
67 adapter_ = NULL; | 66 adapter_ = NULL; |
68 } | 67 } |
69 DLOG_IF(WARNING, socket_map_.size() != 0) | |
70 << "Bluetooth sockets are still open."; | |
71 CleanUpAllExtensions(); | 68 CleanUpAllExtensions(); |
72 } | 69 } |
73 | 70 |
74 bool BluetoothEventRouter::IsBluetoothSupported() const { | 71 bool BluetoothEventRouter::IsBluetoothSupported() const { |
75 return adapter_.get() || | 72 return adapter_.get() || |
76 device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable(); | 73 device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable(); |
77 } | 74 } |
78 | 75 |
79 void BluetoothEventRouter::GetAdapter( | 76 void BluetoothEventRouter::GetAdapter( |
80 const device::BluetoothAdapterFactory::AdapterCallback& callback) { | 77 const device::BluetoothAdapterFactory::AdapterCallback& callback) { |
81 if (adapter_.get()) { | 78 if (adapter_.get()) { |
82 callback.Run(scoped_refptr<device::BluetoothAdapter>(adapter_)); | 79 callback.Run(scoped_refptr<device::BluetoothAdapter>(adapter_)); |
83 return; | 80 return; |
84 } | 81 } |
85 | 82 |
86 device::BluetoothAdapterFactory::GetAdapter(callback); | 83 device::BluetoothAdapterFactory::GetAdapter(callback); |
87 } | 84 } |
88 | 85 |
89 int BluetoothEventRouter::RegisterSocket( | |
90 const std::string& extension_id, | |
91 scoped_refptr<device::BluetoothSocket> socket) { | |
92 // If there is a socket registered with the same fd, just return it's id | |
93 for (SocketMap::const_iterator i = socket_map_.begin(); | |
94 i != socket_map_.end(); ++i) { | |
95 if (i->second.socket.get() == socket.get()) | |
96 return i->first; | |
97 } | |
98 int return_id = next_socket_id_++; | |
99 ExtensionBluetoothSocketRecord record = { extension_id, socket }; | |
100 socket_map_[return_id] = record; | |
101 return return_id; | |
102 } | |
103 | |
104 bool BluetoothEventRouter::ReleaseSocket(int id) { | |
105 SocketMap::iterator socket_entry = socket_map_.find(id); | |
106 if (socket_entry == socket_map_.end()) | |
107 return false; | |
108 socket_map_.erase(socket_entry); | |
109 return true; | |
110 } | |
111 | |
112 void BluetoothEventRouter::AddProfile( | 86 void BluetoothEventRouter::AddProfile( |
113 const device::BluetoothUUID& uuid, | 87 const device::BluetoothUUID& uuid, |
114 const std::string& extension_id, | 88 const std::string& extension_id, |
115 device::BluetoothProfile* bluetooth_profile) { | 89 device::BluetoothProfile* bluetooth_profile) { |
| 90 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
116 DCHECK(!HasProfile(uuid)); | 91 DCHECK(!HasProfile(uuid)); |
117 ExtensionBluetoothProfileRecord record = { extension_id, bluetooth_profile }; | 92 ExtensionBluetoothProfileRecord record = { extension_id, bluetooth_profile }; |
118 bluetooth_profile_map_[uuid] = record; | 93 bluetooth_profile_map_[uuid] = record; |
119 } | 94 } |
120 | 95 |
121 void BluetoothEventRouter::RemoveProfile(const device::BluetoothUUID& uuid) { | 96 void BluetoothEventRouter::RemoveProfile(const device::BluetoothUUID& uuid) { |
| 97 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
122 BluetoothProfileMap::iterator iter = bluetooth_profile_map_.find(uuid); | 98 BluetoothProfileMap::iterator iter = bluetooth_profile_map_.find(uuid); |
123 if (iter != bluetooth_profile_map_.end()) { | 99 if (iter != bluetooth_profile_map_.end()) { |
124 device::BluetoothProfile* bluetooth_profile = iter->second.profile; | 100 device::BluetoothProfile* bluetooth_profile = iter->second.profile; |
125 bluetooth_profile_map_.erase(iter); | 101 bluetooth_profile_map_.erase(iter); |
126 bluetooth_profile->Unregister(); | 102 bluetooth_profile->Unregister(); |
127 } | 103 } |
128 } | 104 } |
129 | 105 |
130 bool BluetoothEventRouter::HasProfile(const device::BluetoothUUID& uuid) const { | 106 bool BluetoothEventRouter::HasProfile(const device::BluetoothUUID& uuid) const { |
| 107 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
131 return bluetooth_profile_map_.find(uuid) != bluetooth_profile_map_.end(); | 108 return bluetooth_profile_map_.find(uuid) != bluetooth_profile_map_.end(); |
132 } | 109 } |
133 | 110 |
134 void BluetoothEventRouter::StartDiscoverySession( | 111 void BluetoothEventRouter::StartDiscoverySession( |
135 device::BluetoothAdapter* adapter, | 112 device::BluetoothAdapter* adapter, |
136 const std::string& extension_id, | 113 const std::string& extension_id, |
137 const base::Closure& callback, | 114 const base::Closure& callback, |
138 const base::Closure& error_callback) { | 115 const base::Closure& error_callback) { |
139 if (adapter != adapter_.get()) { | 116 if (adapter != adapter_.get()) { |
140 error_callback.Run(); | 117 error_callback.Run(); |
(...skipping 29 matching lines...) Expand all Loading... |
170 DVLOG(1) << "No active discovery session exists for extension."; | 147 DVLOG(1) << "No active discovery session exists for extension."; |
171 error_callback.Run(); | 148 error_callback.Run(); |
172 return; | 149 return; |
173 } | 150 } |
174 device::BluetoothDiscoverySession* session = iter->second; | 151 device::BluetoothDiscoverySession* session = iter->second; |
175 session->Stop(callback, error_callback); | 152 session->Stop(callback, error_callback); |
176 } | 153 } |
177 | 154 |
178 device::BluetoothProfile* BluetoothEventRouter::GetProfile( | 155 device::BluetoothProfile* BluetoothEventRouter::GetProfile( |
179 const device::BluetoothUUID& uuid) const { | 156 const device::BluetoothUUID& uuid) const { |
| 157 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
180 BluetoothProfileMap::const_iterator iter = bluetooth_profile_map_.find(uuid); | 158 BluetoothProfileMap::const_iterator iter = bluetooth_profile_map_.find(uuid); |
181 if (iter != bluetooth_profile_map_.end()) | 159 if (iter != bluetooth_profile_map_.end()) |
182 return iter->second.profile; | 160 return iter->second.profile; |
183 | 161 |
184 return NULL; | 162 return NULL; |
185 } | 163 } |
186 | 164 |
187 scoped_refptr<device::BluetoothSocket> BluetoothEventRouter::GetSocket(int id) { | |
188 SocketMap::iterator socket_entry = socket_map_.find(id); | |
189 if (socket_entry == socket_map_.end()) | |
190 return NULL; | |
191 return socket_entry->second.socket; | |
192 } | |
193 | |
194 void BluetoothEventRouter::DispatchConnectionEvent( | |
195 const std::string& extension_id, | |
196 const device::BluetoothUUID& uuid, | |
197 const device::BluetoothDevice* device, | |
198 scoped_refptr<device::BluetoothSocket> socket) { | |
199 if (!HasProfile(uuid)) | |
200 return; | |
201 | |
202 int socket_id = RegisterSocket(extension_id, socket); | |
203 bluetooth::Socket result_socket; | |
204 bluetooth::BluetoothDeviceToApiDevice(*device, &result_socket.device); | |
205 result_socket.profile.uuid = uuid.canonical_value(); | |
206 result_socket.id = socket_id; | |
207 | |
208 scoped_ptr<base::ListValue> args = | |
209 bluetooth::OnConnection::Create(result_socket); | |
210 scoped_ptr<Event> event(new Event( | |
211 bluetooth::OnConnection::kEventName, args.Pass())); | |
212 ExtensionSystem::Get(browser_context_) | |
213 ->event_router() | |
214 ->DispatchEventToExtension(extension_id, event.Pass()); | |
215 } | |
216 | |
217 BluetoothApiPairingDelegate* BluetoothEventRouter::GetPairingDelegate( | 165 BluetoothApiPairingDelegate* BluetoothEventRouter::GetPairingDelegate( |
218 const std::string& extension_id) { | 166 const std::string& extension_id) { |
219 return ContainsKey(pairing_delegate_map_, extension_id) | 167 return ContainsKey(pairing_delegate_map_, extension_id) |
220 ? pairing_delegate_map_[extension_id] | 168 ? pairing_delegate_map_[extension_id] |
221 : NULL; | 169 : NULL; |
222 } | 170 } |
223 | 171 |
224 void BluetoothEventRouter::OnAdapterInitialized( | 172 void BluetoothEventRouter::OnAdapterInitialized( |
225 const base::Closure& callback, | 173 const base::Closure& callback, |
226 scoped_refptr<device::BluetoothAdapter> adapter) { | 174 scoped_refptr<device::BluetoothAdapter> adapter) { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 adapter_->RemovePairingDelegate(delegate); | 222 adapter_->RemovePairingDelegate(delegate); |
275 pairing_delegate_map_.erase(extension_id); | 223 pairing_delegate_map_.erase(extension_id); |
276 delete delegate; | 224 delete delegate; |
277 MaybeReleaseAdapter(); | 225 MaybeReleaseAdapter(); |
278 } | 226 } |
279 } | 227 } |
280 | 228 |
281 void BluetoothEventRouter::AdapterPresentChanged( | 229 void BluetoothEventRouter::AdapterPresentChanged( |
282 device::BluetoothAdapter* adapter, | 230 device::BluetoothAdapter* adapter, |
283 bool present) { | 231 bool present) { |
| 232 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
284 if (adapter != adapter_.get()) { | 233 if (adapter != adapter_.get()) { |
285 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); | 234 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); |
286 return; | 235 return; |
287 } | 236 } |
288 DispatchAdapterStateEvent(); | 237 DispatchAdapterStateEvent(); |
289 } | 238 } |
290 | 239 |
291 void BluetoothEventRouter::AdapterPoweredChanged( | 240 void BluetoothEventRouter::AdapterPoweredChanged( |
292 device::BluetoothAdapter* adapter, | 241 device::BluetoothAdapter* adapter, |
293 bool has_power) { | 242 bool has_power) { |
| 243 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
294 if (adapter != adapter_.get()) { | 244 if (adapter != adapter_.get()) { |
295 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); | 245 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); |
296 return; | 246 return; |
297 } | 247 } |
298 DispatchAdapterStateEvent(); | 248 DispatchAdapterStateEvent(); |
299 } | 249 } |
300 | 250 |
301 void BluetoothEventRouter::AdapterDiscoveringChanged( | 251 void BluetoothEventRouter::AdapterDiscoveringChanged( |
302 device::BluetoothAdapter* adapter, | 252 device::BluetoothAdapter* adapter, |
303 bool discovering) { | 253 bool discovering) { |
| 254 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
304 if (adapter != adapter_.get()) { | 255 if (adapter != adapter_.get()) { |
305 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); | 256 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); |
306 return; | 257 return; |
307 } | 258 } |
308 | 259 |
309 if (!discovering) { | 260 if (!discovering) { |
310 // If any discovery sessions are inactive, clean them up. | 261 // If any discovery sessions are inactive, clean them up. |
311 DiscoverySessionMap active_session_map; | 262 DiscoverySessionMap active_session_map; |
312 for (DiscoverySessionMap::iterator iter = discovery_session_map_.begin(); | 263 for (DiscoverySessionMap::iterator iter = discovery_session_map_.begin(); |
313 iter != discovery_session_map_.end(); | 264 iter != discovery_session_map_.end(); |
314 ++iter) { | 265 ++iter) { |
315 device::BluetoothDiscoverySession* session = iter->second; | 266 device::BluetoothDiscoverySession* session = iter->second; |
316 if (session->IsActive()) { | 267 if (session->IsActive()) { |
317 active_session_map[iter->first] = session; | 268 active_session_map[iter->first] = session; |
318 continue; | 269 continue; |
319 } | 270 } |
320 delete session; | 271 delete session; |
321 } | 272 } |
322 discovery_session_map_.swap(active_session_map); | 273 discovery_session_map_.swap(active_session_map); |
323 MaybeReleaseAdapter(); | 274 MaybeReleaseAdapter(); |
324 } | 275 } |
325 | 276 |
326 DispatchAdapterStateEvent(); | 277 DispatchAdapterStateEvent(); |
327 } | 278 } |
328 | 279 |
329 void BluetoothEventRouter::DeviceAdded(device::BluetoothAdapter* adapter, | 280 void BluetoothEventRouter::DeviceAdded(device::BluetoothAdapter* adapter, |
330 device::BluetoothDevice* device) { | 281 device::BluetoothDevice* device) { |
| 282 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
331 if (adapter != adapter_.get()) { | 283 if (adapter != adapter_.get()) { |
332 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); | 284 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); |
333 return; | 285 return; |
334 } | 286 } |
335 | 287 |
336 DispatchDeviceEvent(bluetooth::OnDeviceAdded::kEventName, device); | 288 DispatchDeviceEvent(bluetooth::OnDeviceAdded::kEventName, device); |
337 } | 289 } |
338 | 290 |
339 void BluetoothEventRouter::DeviceChanged(device::BluetoothAdapter* adapter, | 291 void BluetoothEventRouter::DeviceChanged(device::BluetoothAdapter* adapter, |
340 device::BluetoothDevice* device) { | 292 device::BluetoothDevice* device) { |
| 293 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
341 if (adapter != adapter_.get()) { | 294 if (adapter != adapter_.get()) { |
342 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); | 295 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); |
343 return; | 296 return; |
344 } | 297 } |
345 | 298 |
346 DispatchDeviceEvent(bluetooth::OnDeviceChanged::kEventName, device); | 299 DispatchDeviceEvent(bluetooth::OnDeviceChanged::kEventName, device); |
347 } | 300 } |
348 | 301 |
349 void BluetoothEventRouter::DeviceRemoved(device::BluetoothAdapter* adapter, | 302 void BluetoothEventRouter::DeviceRemoved(device::BluetoothAdapter* adapter, |
350 device::BluetoothDevice* device) { | 303 device::BluetoothDevice* device) { |
| 304 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
351 if (adapter != adapter_.get()) { | 305 if (adapter != adapter_.get()) { |
352 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); | 306 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); |
353 return; | 307 return; |
354 } | 308 } |
355 | 309 |
356 DispatchDeviceEvent(bluetooth::OnDeviceRemoved::kEventName, device); | 310 DispatchDeviceEvent(bluetooth::OnDeviceRemoved::kEventName, device); |
357 } | 311 } |
358 | 312 |
359 void BluetoothEventRouter::OnListenerAdded() { | 313 void BluetoothEventRouter::OnListenerAdded() { |
360 num_event_listeners_++; | 314 num_event_listeners_++; |
| 315 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
361 if (!adapter_.get()) { | 316 if (!adapter_.get()) { |
362 GetAdapter(base::Bind(&BluetoothEventRouter::OnAdapterInitialized, | 317 GetAdapter(base::Bind(&BluetoothEventRouter::OnAdapterInitialized, |
363 weak_ptr_factory_.GetWeakPtr(), | 318 weak_ptr_factory_.GetWeakPtr(), |
364 base::Bind(&base::DoNothing))); | 319 base::Bind(&base::DoNothing))); |
365 } | 320 } |
366 } | 321 } |
367 | 322 |
368 void BluetoothEventRouter::OnListenerRemoved() { | 323 void BluetoothEventRouter::OnListenerRemoved() { |
369 if (num_event_listeners_ > 0) | 324 if (num_event_listeners_ > 0) |
370 num_event_listeners_--; | 325 num_event_listeners_--; |
371 MaybeReleaseAdapter(); | 326 MaybeReleaseAdapter(); |
372 } | 327 } |
373 | 328 |
374 void BluetoothEventRouter::DispatchAdapterStateEvent() { | 329 void BluetoothEventRouter::DispatchAdapterStateEvent() { |
375 bluetooth::AdapterState state; | 330 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 331 api::bluetooth::AdapterState state; |
376 PopulateAdapterState(*adapter_.get(), &state); | 332 PopulateAdapterState(*adapter_.get(), &state); |
377 | 333 |
378 scoped_ptr<base::ListValue> args = | 334 scoped_ptr<base::ListValue> args = |
379 bluetooth::OnAdapterStateChanged::Create(state); | 335 bluetooth::OnAdapterStateChanged::Create(state); |
380 scoped_ptr<Event> event(new Event( | 336 scoped_ptr<Event> event(new Event( |
381 bluetooth::OnAdapterStateChanged::kEventName, | 337 bluetooth::OnAdapterStateChanged::kEventName, |
382 args.Pass())); | 338 args.Pass())); |
383 ExtensionSystem::Get(browser_context_)->event_router()->BroadcastEvent( | 339 ExtensionSystem::Get(browser_context_)->event_router()->BroadcastEvent( |
384 event.Pass()); | 340 event.Pass()); |
385 } | 341 } |
386 | 342 |
387 void BluetoothEventRouter::DispatchDeviceEvent( | 343 void BluetoothEventRouter::DispatchDeviceEvent( |
388 const std::string& event_name, | 344 const std::string& event_name, |
389 device::BluetoothDevice* device) { | 345 device::BluetoothDevice* device) { |
390 bluetooth::Device extension_device; | 346 bluetooth::Device extension_device; |
391 bluetooth::BluetoothDeviceToApiDevice(*device, &extension_device); | 347 bluetooth::BluetoothDeviceToApiDevice(*device, &extension_device); |
392 | 348 |
393 scoped_ptr<base::ListValue> args = | 349 scoped_ptr<base::ListValue> args = |
394 bluetooth::OnDeviceAdded::Create(extension_device); | 350 bluetooth::OnDeviceAdded::Create(extension_device); |
395 scoped_ptr<Event> event(new Event(event_name, args.Pass())); | 351 scoped_ptr<Event> event(new Event(event_name, args.Pass())); |
396 ExtensionSystem::Get(browser_context_)->event_router()->BroadcastEvent( | 352 ExtensionSystem::Get(browser_context_)->event_router()->BroadcastEvent( |
397 event.Pass()); | 353 event.Pass()); |
398 } | 354 } |
399 | 355 |
400 void BluetoothEventRouter::CleanUpForExtension( | 356 void BluetoothEventRouter::CleanUpForExtension( |
401 const std::string& extension_id) { | 357 const std::string& extension_id) { |
| 358 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
402 RemovePairingDelegate(extension_id); | 359 RemovePairingDelegate(extension_id); |
403 | 360 |
404 // Remove all profiles added by the extension. | 361 // Remove all profiles added by the extension. |
405 BluetoothProfileMap::iterator profile_iter = bluetooth_profile_map_.begin(); | 362 BluetoothProfileMap::iterator profile_iter = bluetooth_profile_map_.begin(); |
406 while (profile_iter != bluetooth_profile_map_.end()) { | 363 while (profile_iter != bluetooth_profile_map_.end()) { |
407 ExtensionBluetoothProfileRecord record = profile_iter->second; | 364 ExtensionBluetoothProfileRecord record = profile_iter->second; |
408 if (record.extension_id == extension_id) { | 365 if (record.extension_id == extension_id) { |
409 bluetooth_profile_map_.erase(profile_iter++); | 366 bluetooth_profile_map_.erase(profile_iter++); |
410 record.profile->Unregister(); | 367 record.profile->Unregister(); |
411 } else { | 368 } else { |
412 profile_iter++; | 369 profile_iter++; |
413 } | 370 } |
414 } | 371 } |
415 | 372 |
416 // Remove all sockets opened by the extension. | |
417 SocketMap::iterator socket_iter = socket_map_.begin(); | |
418 while (socket_iter != socket_map_.end()) { | |
419 int socket_id = socket_iter->first; | |
420 ExtensionBluetoothSocketRecord record = socket_iter->second; | |
421 socket_iter++; | |
422 if (record.extension_id == extension_id) { | |
423 ReleaseSocket(socket_id); | |
424 } | |
425 } | |
426 | |
427 // Remove any discovery session initiated by the extension. | 373 // Remove any discovery session initiated by the extension. |
428 DiscoverySessionMap::iterator session_iter = | 374 DiscoverySessionMap::iterator session_iter = |
429 discovery_session_map_.find(extension_id); | 375 discovery_session_map_.find(extension_id); |
430 if (session_iter == discovery_session_map_.end()) | 376 if (session_iter == discovery_session_map_.end()) |
431 return; | 377 return; |
432 delete session_iter->second; | 378 delete session_iter->second; |
433 discovery_session_map_.erase(session_iter); | 379 discovery_session_map_.erase(session_iter); |
434 } | 380 } |
435 | 381 |
436 void BluetoothEventRouter::CleanUpAllExtensions() { | 382 void BluetoothEventRouter::CleanUpAllExtensions() { |
437 for (BluetoothProfileMap::iterator it = bluetooth_profile_map_.begin(); | 383 for (BluetoothProfileMap::iterator it = bluetooth_profile_map_.begin(); |
438 it != bluetooth_profile_map_.end(); | 384 it != bluetooth_profile_map_.end(); |
439 ++it) { | 385 ++it) { |
440 it->second.profile->Unregister(); | 386 it->second.profile->Unregister(); |
441 } | 387 } |
442 bluetooth_profile_map_.clear(); | 388 bluetooth_profile_map_.clear(); |
443 | 389 |
444 for (DiscoverySessionMap::iterator it = discovery_session_map_.begin(); | 390 for (DiscoverySessionMap::iterator it = discovery_session_map_.begin(); |
445 it != discovery_session_map_.end(); | 391 it != discovery_session_map_.end(); |
446 ++it) { | 392 ++it) { |
447 delete it->second; | 393 delete it->second; |
448 } | 394 } |
449 discovery_session_map_.clear(); | 395 discovery_session_map_.clear(); |
450 | 396 |
451 SocketMap::iterator socket_iter = socket_map_.begin(); | |
452 while (socket_iter != socket_map_.end()) | |
453 ReleaseSocket(socket_iter++->first); | |
454 | |
455 PairingDelegateMap::iterator pairing_iter = pairing_delegate_map_.begin(); | 397 PairingDelegateMap::iterator pairing_iter = pairing_delegate_map_.begin(); |
456 while (pairing_iter != pairing_delegate_map_.end()) | 398 while (pairing_iter != pairing_delegate_map_.end()) |
457 RemovePairingDelegate(pairing_iter++->first); | 399 RemovePairingDelegate(pairing_iter++->first); |
458 } | 400 } |
459 | 401 |
460 void BluetoothEventRouter::OnStartDiscoverySession( | 402 void BluetoothEventRouter::OnStartDiscoverySession( |
461 const std::string& extension_id, | 403 const std::string& extension_id, |
462 const base::Closure& callback, | 404 const base::Closure& callback, |
463 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { | 405 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { |
464 // Clean up any existing session instance for the extension. | 406 // Clean up any existing session instance for the extension. |
465 DiscoverySessionMap::iterator iter = | 407 DiscoverySessionMap::iterator iter = |
466 discovery_session_map_.find(extension_id); | 408 discovery_session_map_.find(extension_id); |
467 if (iter != discovery_session_map_.end()) | 409 if (iter != discovery_session_map_.end()) |
468 delete iter->second; | 410 delete iter->second; |
469 discovery_session_map_[extension_id] = discovery_session.release(); | 411 discovery_session_map_[extension_id] = discovery_session.release(); |
470 callback.Run(); | 412 callback.Run(); |
471 } | 413 } |
472 | 414 |
473 void BluetoothEventRouter::Observe( | 415 void BluetoothEventRouter::Observe( |
474 int type, | 416 int type, |
475 const content::NotificationSource& source, | 417 const content::NotificationSource& source, |
476 const content::NotificationDetails& details) { | 418 const content::NotificationDetails& details) { |
| 419 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
477 switch (type) { | 420 switch (type) { |
478 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { | 421 case chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED: { |
479 extensions::UnloadedExtensionInfo* info = | 422 extensions::UnloadedExtensionInfo* info = |
480 content::Details<extensions::UnloadedExtensionInfo>(details).ptr(); | 423 content::Details<extensions::UnloadedExtensionInfo>(details).ptr(); |
481 CleanUpForExtension(info->extension->id()); | 424 CleanUpForExtension(info->extension->id()); |
482 break; | 425 break; |
483 } | 426 } |
| 427 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { |
| 428 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); |
| 429 CleanUpForExtension(host->extension_id()); |
| 430 break; |
| 431 } |
484 } | 432 } |
485 } | 433 } |
486 | 434 |
487 } // namespace extensions | 435 } // namespace extensions |
OLD | NEW |