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