Chromium Code Reviews| 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/event_names.h" | 18 #include "chrome/browser/extensions/event_names.h" |
| 19 #include "chrome/browser/profiles/profile.h" | |
| 20 #include "chrome/common/extensions/api/bluetooth.h" | 19 #include "chrome/common/extensions/api/bluetooth.h" |
| 21 #include "content/public/browser/notification_details.h" | 20 #include "content/public/browser/notification_details.h" |
| 22 #include "content/public/browser/notification_source.h" | 21 #include "content/public/browser/notification_source.h" |
| 23 #include "device/bluetooth/bluetooth_adapter.h" | 22 #include "device/bluetooth/bluetooth_adapter.h" |
| 24 #include "device/bluetooth/bluetooth_adapter_factory.h" | 23 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 25 #include "device/bluetooth/bluetooth_device.h" | 24 #include "device/bluetooth/bluetooth_device.h" |
| 26 #include "device/bluetooth/bluetooth_profile.h" | 25 #include "device/bluetooth/bluetooth_profile.h" |
| 27 #include "device/bluetooth/bluetooth_socket.h" | 26 #include "device/bluetooth/bluetooth_socket.h" |
| 28 #include "extensions/browser/event_router.h" | 27 #include "extensions/browser/event_router.h" |
| 29 #include "extensions/browser/extension_system.h" | 28 #include "extensions/browser/extension_system.h" |
| 30 | 29 |
| 31 namespace extensions { | 30 namespace extensions { |
| 32 | 31 |
| 33 namespace bluetooth = api::bluetooth; | 32 namespace bluetooth = api::bluetooth; |
| 34 | 33 |
| 35 // A struct storing a Bluetooth socket and the extension that added it. | 34 // A struct storing a Bluetooth socket and the extension that added it. |
| 36 struct ExtensionBluetoothSocketRecord { | 35 struct ExtensionBluetoothSocketRecord { |
| 37 std::string extension_id; | 36 std::string extension_id; |
| 38 scoped_refptr<device::BluetoothSocket> socket; | 37 scoped_refptr<device::BluetoothSocket> socket; |
| 39 }; | 38 }; |
| 40 | 39 |
| 41 // A struct storing a Bluetooth profile and the extension that added it. | 40 // A struct storing a Bluetooth profile and the extension that added it. |
| 42 struct ExtensionBluetoothProfileRecord { | 41 struct ExtensionBluetoothProfileRecord { |
| 43 std::string extension_id; | 42 std::string extension_id; |
| 44 device::BluetoothProfile* profile; | 43 device::BluetoothProfile* profile; |
| 45 }; | 44 }; |
| 46 | 45 |
| 47 ExtensionBluetoothEventRouter::ExtensionBluetoothEventRouter(Profile* profile) | 46 ExtensionBluetoothEventRouter::ExtensionBluetoothEventRouter( |
|
James Cook
2014/02/26 17:57:21
These conversions seem awfully familiar, and simil
Ken Rockot(use gerrit already)
2014/02/26 18:20:44
Adding yoz to take a look. I see some conflict, bu
Yoyo Zhou
2014/02/27 00:08:52
https://codereview.chromium.org/168253002/ does co
| |
| 47 content::BrowserContext* context) | |
| 48 : send_discovery_events_(false), | 48 : send_discovery_events_(false), |
| 49 responsible_for_discovery_(false), | 49 responsible_for_discovery_(false), |
| 50 profile_(profile), | 50 browser_context_(context), |
| 51 adapter_(NULL), | 51 adapter_(NULL), |
| 52 num_event_listeners_(0), | 52 num_event_listeners_(0), |
| 53 next_socket_id_(1), | 53 next_socket_id_(1), |
| 54 weak_ptr_factory_(this) { | 54 weak_ptr_factory_(this) { |
| 55 DCHECK(profile_); | 55 DCHECK(browser_context_); |
| 56 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 56 registrar_.Add(this, |
| 57 content::Source<Profile>(profile_)); | 57 chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 58 content::Source<content::BrowserContext>(browser_context_)); | |
| 58 } | 59 } |
| 59 | 60 |
| 60 ExtensionBluetoothEventRouter::~ExtensionBluetoothEventRouter() { | 61 ExtensionBluetoothEventRouter::~ExtensionBluetoothEventRouter() { |
| 61 if (adapter_.get()) { | 62 if (adapter_.get()) { |
| 62 adapter_->RemoveObserver(this); | 63 adapter_->RemoveObserver(this); |
| 63 adapter_ = NULL; | 64 adapter_ = NULL; |
| 64 } | 65 } |
| 65 DLOG_IF(WARNING, socket_map_.size() != 0) | 66 DLOG_IF(WARNING, socket_map_.size() != 0) |
| 66 << "Bluetooth sockets are still open."; | 67 << "Bluetooth sockets are still open."; |
| 67 socket_map_.clear(); | 68 socket_map_.clear(); |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 } | 183 } |
| 183 | 184 |
| 184 send_discovery_events_ = should_send; | 185 send_discovery_events_ = should_send; |
| 185 } | 186 } |
| 186 | 187 |
| 187 void ExtensionBluetoothEventRouter::DispatchDeviceEvent( | 188 void ExtensionBluetoothEventRouter::DispatchDeviceEvent( |
| 188 const std::string& event_name, const bluetooth::Device& device) { | 189 const std::string& event_name, const bluetooth::Device& device) { |
| 189 scoped_ptr<base::ListValue> args(new base::ListValue()); | 190 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 190 args->Append(device.ToValue().release()); | 191 args->Append(device.ToValue().release()); |
| 191 scoped_ptr<Event> event(new Event(event_name, args.Pass())); | 192 scoped_ptr<Event> event(new Event(event_name, args.Pass())); |
| 192 ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent(event.Pass()); | 193 ExtensionSystem::Get(browser_context_)->event_router()->BroadcastEvent( |
| 194 event.Pass()); | |
| 193 } | 195 } |
| 194 | 196 |
| 195 void ExtensionBluetoothEventRouter::DispatchConnectionEvent( | 197 void ExtensionBluetoothEventRouter::DispatchConnectionEvent( |
| 196 const std::string& extension_id, | 198 const std::string& extension_id, |
| 197 const std::string& uuid, | 199 const std::string& uuid, |
| 198 const device::BluetoothDevice* device, | 200 const device::BluetoothDevice* device, |
| 199 scoped_refptr<device::BluetoothSocket> socket) { | 201 scoped_refptr<device::BluetoothSocket> socket) { |
| 200 if (!HasProfile(uuid)) | 202 if (!HasProfile(uuid)) |
| 201 return; | 203 return; |
| 202 | 204 |
| 203 int socket_id = RegisterSocket(extension_id, socket); | 205 int socket_id = RegisterSocket(extension_id, socket); |
| 204 api::bluetooth::Socket result_socket; | 206 api::bluetooth::Socket result_socket; |
| 205 api::bluetooth::BluetoothDeviceToApiDevice(*device, &result_socket.device); | 207 api::bluetooth::BluetoothDeviceToApiDevice(*device, &result_socket.device); |
| 206 result_socket.profile.uuid = uuid; | 208 result_socket.profile.uuid = uuid; |
| 207 result_socket.id = socket_id; | 209 result_socket.id = socket_id; |
| 208 | 210 |
| 209 scoped_ptr<base::ListValue> args(new base::ListValue()); | 211 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 210 args->Append(result_socket.ToValue().release()); | 212 args->Append(result_socket.ToValue().release()); |
| 211 scoped_ptr<Event> event(new Event( | 213 scoped_ptr<Event> event(new Event( |
| 212 bluetooth::OnConnection::kEventName, args.Pass())); | 214 bluetooth::OnConnection::kEventName, args.Pass())); |
| 213 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( | 215 ExtensionSystem::Get(browser_context_) |
| 214 extension_id, event.Pass()); | 216 ->event_router() |
| 217 ->DispatchEventToExtension(extension_id, event.Pass()); | |
| 215 } | 218 } |
| 216 | 219 |
| 217 void ExtensionBluetoothEventRouter::AdapterPresentChanged( | 220 void ExtensionBluetoothEventRouter::AdapterPresentChanged( |
| 218 device::BluetoothAdapter* adapter, bool present) { | 221 device::BluetoothAdapter* adapter, bool present) { |
| 219 if (adapter != adapter_.get()) { | 222 if (adapter != adapter_.get()) { |
| 220 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); | 223 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); |
| 221 return; | 224 return; |
| 222 } | 225 } |
| 223 DispatchAdapterStateEvent(); | 226 DispatchAdapterStateEvent(); |
| 224 } | 227 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 293 | 296 |
| 294 void ExtensionBluetoothEventRouter::DispatchAdapterStateEvent() { | 297 void ExtensionBluetoothEventRouter::DispatchAdapterStateEvent() { |
| 295 api::bluetooth::AdapterState state; | 298 api::bluetooth::AdapterState state; |
| 296 PopulateAdapterState(*adapter_.get(), &state); | 299 PopulateAdapterState(*adapter_.get(), &state); |
| 297 | 300 |
| 298 scoped_ptr<base::ListValue> args(new base::ListValue()); | 301 scoped_ptr<base::ListValue> args(new base::ListValue()); |
| 299 args->Append(state.ToValue().release()); | 302 args->Append(state.ToValue().release()); |
| 300 scoped_ptr<Event> event(new Event( | 303 scoped_ptr<Event> event(new Event( |
| 301 bluetooth::OnAdapterStateChanged::kEventName, | 304 bluetooth::OnAdapterStateChanged::kEventName, |
| 302 args.Pass())); | 305 args.Pass())); |
| 303 ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent(event.Pass()); | 306 ExtensionSystem::Get(browser_context_)->event_router()->BroadcastEvent( |
| 307 event.Pass()); | |
| 304 } | 308 } |
| 305 | 309 |
| 306 void ExtensionBluetoothEventRouter::CleanUpForExtension( | 310 void ExtensionBluetoothEventRouter::CleanUpForExtension( |
| 307 const std::string& extension_id) { | 311 const std::string& extension_id) { |
| 308 // Remove all profiles added by the extension. | 312 // Remove all profiles added by the extension. |
| 309 BluetoothProfileMap::iterator profile_iter = bluetooth_profile_map_.begin(); | 313 BluetoothProfileMap::iterator profile_iter = bluetooth_profile_map_.begin(); |
| 310 while (profile_iter != bluetooth_profile_map_.end()) { | 314 while (profile_iter != bluetooth_profile_map_.end()) { |
| 311 ExtensionBluetoothProfileRecord record = profile_iter->second; | 315 ExtensionBluetoothProfileRecord record = profile_iter->second; |
| 312 if (record.extension_id == extension_id) { | 316 if (record.extension_id == extension_id) { |
| 313 bluetooth_profile_map_.erase(profile_iter++); | 317 bluetooth_profile_map_.erase(profile_iter++); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 337 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { | 341 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { |
| 338 extensions::UnloadedExtensionInfo* info = | 342 extensions::UnloadedExtensionInfo* info = |
| 339 content::Details<extensions::UnloadedExtensionInfo>(details).ptr(); | 343 content::Details<extensions::UnloadedExtensionInfo>(details).ptr(); |
| 340 CleanUpForExtension(info->extension->id()); | 344 CleanUpForExtension(info->extension->id()); |
| 341 break; | 345 break; |
| 342 } | 346 } |
| 343 } | 347 } |
| 344 } | 348 } |
| 345 | 349 |
| 346 } // namespace extensions | 350 } // namespace extensions |
| OLD | NEW |