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