| 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/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
| 14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 15 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" | 15 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" |
| 16 #include "chrome/browser/extensions/event_names.h" | 16 #include "chrome/browser/extensions/event_names.h" |
| 17 #include "chrome/browser/extensions/event_router.h" | 17 #include "chrome/browser/extensions/event_router.h" |
| 18 #include "chrome/browser/extensions/extension_system.h" | 18 #include "chrome/browser/extensions/extension_system.h" |
| 19 #include "chrome/common/extensions/api/bluetooth.h" | 19 #include "chrome/common/extensions/api/bluetooth.h" |
| 20 #include "device/bluetooth/bluetooth_adapter.h" | 20 #include "device/bluetooth/bluetooth_adapter.h" |
| 21 #include "device/bluetooth/bluetooth_adapter_factory.h" | 21 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 22 #include "device/bluetooth/bluetooth_device.h" | 22 #include "device/bluetooth/bluetooth_device.h" |
| 23 #include "device/bluetooth/bluetooth_profile.h" |
| 23 #include "device/bluetooth/bluetooth_socket.h" | 24 #include "device/bluetooth/bluetooth_socket.h" |
| 24 | 25 |
| 25 namespace extensions { | 26 namespace extensions { |
| 26 | 27 |
| 27 ExtensionBluetoothEventRouter::ExtensionBluetoothEventRouter(Profile* profile) | 28 ExtensionBluetoothEventRouter::ExtensionBluetoothEventRouter(Profile* profile) |
| 28 : send_discovery_events_(false), | 29 : send_discovery_events_(false), |
| 29 responsible_for_discovery_(false), | 30 responsible_for_discovery_(false), |
| 30 profile_(profile), | 31 profile_(profile), |
| 31 adapter_(NULL), | 32 adapter_(NULL), |
| 32 num_event_listeners_(0), | 33 num_event_listeners_(0), |
| 33 next_socket_id_(1), | 34 next_socket_id_(1), |
| 34 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | 35 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
| 35 DCHECK(profile_); | 36 DCHECK(profile_); |
| 36 } | 37 } |
| 37 | 38 |
| 38 ExtensionBluetoothEventRouter::~ExtensionBluetoothEventRouter() { | 39 ExtensionBluetoothEventRouter::~ExtensionBluetoothEventRouter() { |
| 39 if (adapter_) { | 40 if (adapter_) { |
| 40 adapter_->RemoveObserver(this); | 41 adapter_->RemoveObserver(this); |
| 41 adapter_ = NULL; | 42 adapter_ = NULL; |
| 42 } | 43 } |
| 43 DLOG_IF(WARNING, socket_map_.size() != 0) | 44 DLOG_IF(WARNING, socket_map_.size() != 0) |
| 44 << "Bluetooth sockets are still open."; | 45 << "Bluetooth sockets are still open."; |
| 45 socket_map_.clear(); | 46 socket_map_.clear(); |
| 47 |
| 48 for (BluetoothProfileMap::iterator iter = bluetooth_profile_map_.begin(); |
| 49 iter != bluetooth_profile_map_.end(); |
| 50 ++iter) { |
| 51 iter->second->Unregister(); |
| 52 } |
| 46 } | 53 } |
| 47 | 54 |
| 48 bool ExtensionBluetoothEventRouter::IsBluetoothSupported() const { | 55 bool ExtensionBluetoothEventRouter::IsBluetoothSupported() const { |
| 49 return adapter_ || | 56 return adapter_ || |
| 50 device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable(); | 57 device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable(); |
| 51 } | 58 } |
| 52 | 59 |
| 53 void ExtensionBluetoothEventRouter::GetAdapter( | 60 void ExtensionBluetoothEventRouter::GetAdapter( |
| 54 const device::BluetoothAdapterFactory::AdapterCallback& callback) { | 61 const device::BluetoothAdapterFactory::AdapterCallback& callback) { |
| 55 if (adapter_) { | 62 if (adapter_) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 85 } | 92 } |
| 86 | 93 |
| 87 bool ExtensionBluetoothEventRouter::ReleaseSocket(int id) { | 94 bool ExtensionBluetoothEventRouter::ReleaseSocket(int id) { |
| 88 SocketMap::iterator socket_entry = socket_map_.find(id); | 95 SocketMap::iterator socket_entry = socket_map_.find(id); |
| 89 if (socket_entry == socket_map_.end()) | 96 if (socket_entry == socket_map_.end()) |
| 90 return false; | 97 return false; |
| 91 socket_map_.erase(socket_entry); | 98 socket_map_.erase(socket_entry); |
| 92 return true; | 99 return true; |
| 93 } | 100 } |
| 94 | 101 |
| 102 void ExtensionBluetoothEventRouter::AddProfile( |
| 103 const std::string& uuid, |
| 104 device::BluetoothProfile* bluetooth_profile) { |
| 105 DCHECK(!HasProfile(uuid)); |
| 106 bluetooth_profile_map_[uuid] = bluetooth_profile; |
| 107 } |
| 108 |
| 109 void ExtensionBluetoothEventRouter::RemoveProfile(const std::string& uuid) { |
| 110 BluetoothProfileMap::iterator iter = bluetooth_profile_map_.find(uuid); |
| 111 if (iter != bluetooth_profile_map_.end()) { |
| 112 device::BluetoothProfile* bluetooth_profile = iter->second; |
| 113 bluetooth_profile_map_.erase(iter); |
| 114 bluetooth_profile->Unregister(); |
| 115 } |
| 116 } |
| 117 |
| 118 bool ExtensionBluetoothEventRouter::HasProfile(const std::string& uuid) const { |
| 119 return bluetooth_profile_map_.find(uuid) != bluetooth_profile_map_.end(); |
| 120 } |
| 121 |
| 95 scoped_refptr<device::BluetoothSocket> | 122 scoped_refptr<device::BluetoothSocket> |
| 96 ExtensionBluetoothEventRouter::GetSocket(int id) { | 123 ExtensionBluetoothEventRouter::GetSocket(int id) { |
| 97 SocketMap::iterator socket_entry = socket_map_.find(id); | 124 SocketMap::iterator socket_entry = socket_map_.find(id); |
| 98 if (socket_entry == socket_map_.end()) | 125 if (socket_entry == socket_map_.end()) |
| 99 return NULL; | 126 return NULL; |
| 100 return socket_entry->second; | 127 return socket_entry->second; |
| 101 } | 128 } |
| 102 | 129 |
| 103 void ExtensionBluetoothEventRouter::SetResponsibleForDiscovery( | 130 void ExtensionBluetoothEventRouter::SetResponsibleForDiscovery( |
| 104 bool responsible) { | 131 bool responsible) { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 124 } | 151 } |
| 125 | 152 |
| 126 void ExtensionBluetoothEventRouter::DispatchDeviceEvent( | 153 void ExtensionBluetoothEventRouter::DispatchDeviceEvent( |
| 127 const char* event_name, const extensions::api::bluetooth::Device& device) { | 154 const char* event_name, const extensions::api::bluetooth::Device& device) { |
| 128 scoped_ptr<ListValue> args(new ListValue()); | 155 scoped_ptr<ListValue> args(new ListValue()); |
| 129 args->Append(device.ToValue().release()); | 156 args->Append(device.ToValue().release()); |
| 130 scoped_ptr<Event> event(new Event(event_name, args.Pass())); | 157 scoped_ptr<Event> event(new Event(event_name, args.Pass())); |
| 131 ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent(event.Pass()); | 158 ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent(event.Pass()); |
| 132 } | 159 } |
| 133 | 160 |
| 161 void ExtensionBluetoothEventRouter::DispatchConnectionEvent( |
| 162 const std::string& extension_id, |
| 163 const std::string& uuid, |
| 164 const device::BluetoothDevice* device, |
| 165 scoped_refptr<device::BluetoothSocket> socket) { |
| 166 if (!HasProfile(uuid)) |
| 167 return; |
| 168 |
| 169 int socket_id = RegisterSocket(socket); |
| 170 api::bluetooth::Socket result_socket; |
| 171 api::bluetooth::BluetoothDeviceToApiDevice(*device, &result_socket.device); |
| 172 result_socket.profile.uuid = uuid; |
| 173 result_socket.id = socket_id; |
| 174 |
| 175 scoped_ptr<ListValue> args(new ListValue()); |
| 176 args->Append(result_socket.ToValue().release()); |
| 177 scoped_ptr<Event> event(new Event( |
| 178 extensions::event_names::kBluetoothOnConnection, args.Pass())); |
| 179 ExtensionSystem::Get(profile_)->event_router()->DispatchEventToExtension( |
| 180 extension_id, event.Pass()); |
| 181 } |
| 182 |
| 134 void ExtensionBluetoothEventRouter::AdapterPresentChanged( | 183 void ExtensionBluetoothEventRouter::AdapterPresentChanged( |
| 135 device::BluetoothAdapter* adapter, bool present) { | 184 device::BluetoothAdapter* adapter, bool present) { |
| 136 if (adapter != adapter_) { | 185 if (adapter != adapter_) { |
| 137 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); | 186 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); |
| 138 return; | 187 return; |
| 139 } | 188 } |
| 140 DispatchAdapterStateEvent(); | 189 DispatchAdapterStateEvent(); |
| 141 } | 190 } |
| 142 | 191 |
| 143 void ExtensionBluetoothEventRouter::AdapterPoweredChanged( | 192 void ExtensionBluetoothEventRouter::AdapterPoweredChanged( |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 | 263 |
| 215 scoped_ptr<ListValue> args(new ListValue()); | 264 scoped_ptr<ListValue> args(new ListValue()); |
| 216 args->Append(state.ToValue().release()); | 265 args->Append(state.ToValue().release()); |
| 217 scoped_ptr<Event> event(new Event( | 266 scoped_ptr<Event> event(new Event( |
| 218 extensions::event_names::kBluetoothOnAdapterStateChanged, | 267 extensions::event_names::kBluetoothOnAdapterStateChanged, |
| 219 args.Pass())); | 268 args.Pass())); |
| 220 ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent(event.Pass()); | 269 ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent(event.Pass()); |
| 221 } | 270 } |
| 222 | 271 |
| 223 } // namespace extensions | 272 } // namespace extensions |
| OLD | NEW |