| 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_api.h" | 5 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" | 11 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" |
| 12 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" | 12 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" |
| 13 #include "chrome/browser/extensions/api/bluetooth/bluetooth_socket_event_dispatc
her.h" |
| 13 #include "chrome/browser/extensions/event_names.h" | 14 #include "chrome/browser/extensions/event_names.h" |
| 15 #include "chrome/browser/profiles/profile_manager.h" |
| 14 #include "chrome/common/extensions/api/bluetooth.h" | 16 #include "chrome/common/extensions/api/bluetooth.h" |
| 15 #include "chrome/common/extensions/api/bluetooth/bluetooth_manifest_data.h" | 17 #include "chrome/common/extensions/api/bluetooth/bluetooth_manifest_data.h" |
| 16 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 17 #include "device/bluetooth/bluetooth_adapter.h" | 19 #include "device/bluetooth/bluetooth_adapter.h" |
| 18 #include "device/bluetooth/bluetooth_device.h" | 20 #include "device/bluetooth/bluetooth_device.h" |
| 19 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" | 21 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" |
| 20 #include "device/bluetooth/bluetooth_profile.h" | 22 #include "device/bluetooth/bluetooth_profile.h" |
| 21 #include "device/bluetooth/bluetooth_service_record.h" | 23 #include "device/bluetooth/bluetooth_service_record.h" |
| 22 #include "device/bluetooth/bluetooth_socket.h" | 24 #include "device/bluetooth/bluetooth_socket.h" |
| 23 #include "device/bluetooth/bluetooth_utils.h" | 25 #include "device/bluetooth/bluetooth_utils.h" |
| 24 #include "extensions/browser/event_router.h" | 26 #include "extensions/browser/event_router.h" |
| 25 #include "extensions/browser/extension_system.h" | 27 #include "extensions/browser/extension_system.h" |
| 26 #include "extensions/common/permissions/permissions_data.h" | 28 #include "extensions/common/permissions/permissions_data.h" |
| 27 #include "net/base/io_buffer.h" | 29 #include "net/base/io_buffer.h" |
| 28 | 30 |
| 29 using content::BrowserContext; | 31 using content::BrowserContext; |
| 32 using content::BrowserThread; |
| 33 |
| 30 using device::BluetoothAdapter; | 34 using device::BluetoothAdapter; |
| 31 using device::BluetoothDevice; | 35 using device::BluetoothDevice; |
| 32 using device::BluetoothProfile; | 36 using device::BluetoothProfile; |
| 33 using device::BluetoothServiceRecord; | 37 using device::BluetoothServiceRecord; |
| 34 using device::BluetoothSocket; | 38 using device::BluetoothSocket; |
| 35 | 39 |
| 36 namespace { | 40 using extensions::BluetoothApiSocket; |
| 37 | 41 |
| 38 extensions::ExtensionBluetoothEventRouter* GetEventRouter( | 42 namespace AddProfile = extensions::api::bluetooth::AddProfile; |
| 39 BrowserContext* context) { | 43 namespace bluetooth = extensions::api::bluetooth; |
| 40 return extensions::BluetoothAPI::Get(context)->bluetooth_event_router(); | 44 namespace Connect = extensions::api::bluetooth::Connect; |
| 41 } | 45 namespace Disconnect = extensions::api::bluetooth::Disconnect; |
| 42 | 46 namespace GetDevices = extensions::api::bluetooth::GetDevices; |
| 43 } // namespace | 47 namespace GetProfiles = extensions::api::bluetooth::GetProfiles; |
| 48 namespace RemoveProfile = extensions::api::bluetooth::RemoveProfile; |
| 49 namespace SetOutOfBandPairingData = |
| 50 extensions::api::bluetooth::SetOutOfBandPairingData; |
| 51 namespace Send = extensions::api::bluetooth::Send; |
| 44 | 52 |
| 45 namespace { | 53 namespace { |
| 46 | 54 |
| 47 const char kCouldNotGetLocalOutOfBandPairingData[] = | 55 const char kCouldNotGetLocalOutOfBandPairingData[] = |
| 48 "Could not get local Out Of Band Pairing Data"; | 56 "Could not get local Out Of Band Pairing Data"; |
| 49 const char kCouldNotSetOutOfBandPairingData[] = | 57 const char kCouldNotSetOutOfBandPairingData[] = |
| 50 "Could not set Out Of Band Pairing Data"; | 58 "Could not set Out Of Band Pairing Data"; |
| 51 const char kFailedToConnect[] = "Connection failed"; | |
| 52 const char kInvalidDevice[] = "Invalid device"; | 59 const char kInvalidDevice[] = "Invalid device"; |
| 53 const char kInvalidUuid[] = "Invalid UUID"; | 60 const char kInvalidUuid[] = "Invalid UUID"; |
| 54 const char kPermissionDenied[] = "Permission to add profile denied."; | 61 const char kPermissionDenied[] = "Permission to add profile denied."; |
| 55 const char kProfileAlreadyRegistered[] = | 62 const char kProfileAlreadyRegistered[] = |
| 56 "This profile has already been registered"; | 63 "This profile has already been registered"; |
| 57 const char kProfileNotFound[] = "Profile not found: invalid uuid"; | 64 const char kProfileNotFound[] = "Profile not found: invalid uuid"; |
| 58 const char kProfileRegistrationFailed[] = "Profile registration failed"; | 65 const char kProfileRegistrationFailed[] = "Profile registration failed"; |
| 59 const char kServiceDiscoveryFailed[] = "Service discovery failed"; | |
| 60 const char kSocketNotFoundError[] = "Socket not found: invalid socket id"; | 66 const char kSocketNotFoundError[] = "Socket not found: invalid socket id"; |
| 61 const char kStartDiscoveryFailed[] = "Starting discovery failed"; | 67 const char kStartDiscoveryFailed[] = "Starting discovery failed"; |
| 62 const char kStopDiscoveryFailed[] = "Failed to stop discovery"; | 68 const char kStopDiscoveryFailed[] = "Failed to stop discovery"; |
| 63 | 69 |
| 70 extensions::ExtensionBluetoothEventRouter* GetEventRouter( |
| 71 BrowserContext* context) { |
| 72 // Note: |context| is valid on UI thread only. |
| 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 74 return extensions::BluetoothAPI::Get(context)->event_router(); |
| 75 } |
| 76 |
| 77 linked_ptr<bluetooth::Socket> CreateSocketInfo(int socket_id, |
| 78 BluetoothApiSocket* socket) { |
| 79 DCHECK(BrowserThread::CurrentlyOn(BluetoothApiSocket::kThreadId)); |
| 80 linked_ptr<bluetooth::Socket> socket_info(new bluetooth::Socket()); |
| 81 // This represents what we know about the socket, and does not call through |
| 82 // to the system. |
| 83 socket_info->id = socket_id; |
| 84 if (!socket->name().empty()) { |
| 85 socket_info->name.reset(new std::string(socket->name())); |
| 86 } |
| 87 socket_info->persistent = socket->persistent(); |
| 88 if (socket->buffer_size() > 0) { |
| 89 socket_info->buffer_size.reset(new int(socket->buffer_size())); |
| 90 } |
| 91 socket_info->paused = socket->paused(); |
| 92 socket_info->device.address = socket->device_address(); |
| 93 socket_info->uuid = socket->uuid(); |
| 94 |
| 95 return socket_info; |
| 96 } |
| 97 |
| 98 void SetSocketProperties(extensions::BluetoothApiSocket* socket, |
| 99 bluetooth::SocketProperties* properties) { |
| 100 if (properties->name.get()) { |
| 101 socket->set_name(*properties->name.get()); |
| 102 } |
| 103 if (properties->persistent.get()) { |
| 104 socket->set_persistent(*properties->persistent.get()); |
| 105 } |
| 106 if (properties->buffer_size.get()) { |
| 107 // buffer size is validated when issuing the actual Recv operation |
| 108 // on the socket. |
| 109 socket->set_buffer_size(*properties->buffer_size.get()); |
| 110 } |
| 111 } |
| 112 |
| 113 static void DispatchConnectionEventWorker( |
| 114 void* browser_context_id, |
| 115 const std::string& extension_id, |
| 116 const std::string& profile_uuid, |
| 117 const device::BluetoothDevice* device, |
| 118 scoped_refptr<device::BluetoothSocket> socket) { |
| 119 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 120 |
| 121 content::BrowserContext* context = |
| 122 reinterpret_cast<content::BrowserContext*>(browser_context_id); |
| 123 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context)) |
| 124 return; |
| 125 |
| 126 extensions::BluetoothAPI* bluetooth_api = |
| 127 extensions::BluetoothAPI::Get(context); |
| 128 if (!bluetooth_api) |
| 129 return; |
| 130 |
| 131 bluetooth_api->DispatchConnectionEvent( |
| 132 extension_id, profile_uuid, device, socket); |
| 133 } |
| 134 |
| 64 } // namespace | 135 } // namespace |
| 65 | 136 |
| 66 namespace AddProfile = extensions::api::bluetooth::AddProfile; | |
| 67 namespace bluetooth = extensions::api::bluetooth; | |
| 68 namespace Connect = extensions::api::bluetooth::Connect; | |
| 69 namespace Disconnect = extensions::api::bluetooth::Disconnect; | |
| 70 namespace GetDevices = extensions::api::bluetooth::GetDevices; | |
| 71 namespace GetProfiles = extensions::api::bluetooth::GetProfiles; | |
| 72 namespace GetServices = extensions::api::bluetooth::GetServices; | |
| 73 namespace Read = extensions::api::bluetooth::Read; | |
| 74 namespace RemoveProfile = extensions::api::bluetooth::RemoveProfile; | |
| 75 namespace SetOutOfBandPairingData = | |
| 76 extensions::api::bluetooth::SetOutOfBandPairingData; | |
| 77 namespace Write = extensions::api::bluetooth::Write; | |
| 78 | |
| 79 namespace extensions { | 137 namespace extensions { |
| 80 | 138 |
| 81 static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothAPI> > | 139 static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothAPI> > |
| 82 g_factory = LAZY_INSTANCE_INITIALIZER; | 140 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 83 | 141 |
| 84 // static | 142 // static |
| 85 BrowserContextKeyedAPIFactory<BluetoothAPI>* | 143 BrowserContextKeyedAPIFactory<BluetoothAPI>* |
| 86 BluetoothAPI::GetFactoryInstance() { | 144 BluetoothAPI::GetFactoryInstance() { |
| 87 return g_factory.Pointer(); | 145 return g_factory.Pointer(); |
| 88 } | 146 } |
| 89 | 147 |
| 90 // static | 148 // static |
| 91 BluetoothAPI* BluetoothAPI::Get(BrowserContext* context) { | 149 BluetoothAPI* BluetoothAPI::Get(BrowserContext* context) { |
| 150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 92 return GetFactoryInstance()->Get(context); | 151 return GetFactoryInstance()->Get(context); |
| 93 } | 152 } |
| 94 | 153 |
| 95 BluetoothAPI::BluetoothAPI(BrowserContext* context) | 154 BluetoothAPI::ConnectionParams::ConnectionParams() {} |
| 155 |
| 156 BluetoothAPI::ConnectionParams::~ConnectionParams() {} |
| 157 |
| 158 BluetoothAPI::BluetoothAPI(content::BrowserContext* context) |
| 96 : browser_context_(context) { | 159 : browser_context_(context) { |
| 160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 97 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver( | 161 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver( |
| 98 this, bluetooth::OnAdapterStateChanged::kEventName); | 162 this, bluetooth::OnAdapterStateChanged::kEventName); |
| 99 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver( | 163 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver( |
| 100 this, bluetooth::OnDeviceAdded::kEventName); | 164 this, bluetooth::OnDeviceAdded::kEventName); |
| 101 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver( | 165 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver( |
| 102 this, bluetooth::OnDeviceChanged::kEventName); | 166 this, bluetooth::OnDeviceChanged::kEventName); |
| 103 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver( | 167 ExtensionSystem::Get(browser_context_)->event_router()->RegisterObserver( |
| 104 this, bluetooth::OnDeviceRemoved::kEventName); | 168 this, bluetooth::OnDeviceRemoved::kEventName); |
| 105 } | 169 } |
| 106 | 170 |
| 107 BluetoothAPI::~BluetoothAPI() { | 171 BluetoothAPI::~BluetoothAPI() {} |
| 108 } | 172 |
| 109 | 173 ExtensionBluetoothEventRouter* BluetoothAPI::event_router() { |
| 110 ExtensionBluetoothEventRouter* BluetoothAPI::bluetooth_event_router() { | 174 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 111 if (!bluetooth_event_router_) | 175 if (!event_router_) { |
| 112 bluetooth_event_router_.reset( | 176 event_router_.reset(new ExtensionBluetoothEventRouter(browser_context_)); |
| 113 new ExtensionBluetoothEventRouter(browser_context_)); | 177 } |
| 114 | 178 return event_router_.get(); |
| 115 return bluetooth_event_router_.get(); | 179 } |
| 180 |
| 181 scoped_refptr<BluetoothAPI::SocketData> BluetoothAPI::socket_data() { |
| 182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 183 if (!socket_data_) { |
| 184 ApiResourceManager<BluetoothApiSocket>* socket_manager = |
| 185 ApiResourceManager<BluetoothApiSocket>::Get(browser_context_); |
| 186 DCHECK(socket_manager) |
| 187 << "There is no socket manager. " |
| 188 "If this assertion is failing during a test, then it is likely that " |
| 189 "TestExtensionSystem is failing to provide an instance of " |
| 190 "ApiResourceManager<BluetoothApiSocket>."; |
| 191 |
| 192 socket_data_ = socket_manager->data_; |
| 193 } |
| 194 return socket_data_; |
| 195 } |
| 196 |
| 197 scoped_refptr<api::BluetoothSocketEventDispatcher> |
| 198 BluetoothAPI::socket_event_dispatcher() { |
| 199 if (!socket_event_dispatcher_) { |
| 200 socket_event_dispatcher_ = new api::BluetoothSocketEventDispatcher( |
| 201 browser_context_, socket_data()); |
| 202 } |
| 203 return socket_event_dispatcher_; |
| 116 } | 204 } |
| 117 | 205 |
| 118 void BluetoothAPI::Shutdown() { | 206 void BluetoothAPI::Shutdown() { |
| 207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 119 ExtensionSystem::Get(browser_context_)->event_router()->UnregisterObserver( | 208 ExtensionSystem::Get(browser_context_)->event_router()->UnregisterObserver( |
| 120 this); | 209 this); |
| 121 } | 210 } |
| 122 | 211 |
| 123 void BluetoothAPI::OnListenerAdded(const EventListenerInfo& details) { | 212 void BluetoothAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 124 if (bluetooth_event_router()->IsBluetoothSupported()) | 213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 125 bluetooth_event_router()->OnListenerAdded(); | 214 if (event_router()->IsBluetoothSupported()) |
| 215 event_router()->OnListenerAdded(); |
| 126 } | 216 } |
| 127 | 217 |
| 128 void BluetoothAPI::OnListenerRemoved(const EventListenerInfo& details) { | 218 void BluetoothAPI::OnListenerRemoved(const EventListenerInfo& details) { |
| 129 if (bluetooth_event_router()->IsBluetoothSupported()) | 219 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 130 bluetooth_event_router()->OnListenerRemoved(); | 220 if (event_router()->IsBluetoothSupported()) |
| 221 event_router()->OnListenerRemoved(); |
| 222 } |
| 223 |
| 224 void BluetoothAPI::DispatchConnectionEvent( |
| 225 const std::string& extension_id, |
| 226 const std::string& uuid, |
| 227 const device::BluetoothDevice* device, |
| 228 scoped_refptr<device::BluetoothSocket> socket) { |
| 229 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 230 |
| 231 if (!event_router()->HasProfile(uuid)) |
| 232 return; |
| 233 |
| 234 extensions::BluetoothAPI::ConnectionParams params; |
| 235 params.browser_context_id = browser_context_; |
| 236 params.thread_id = BluetoothApiSocket::kThreadId; |
| 237 params.extension_id = extension_id; |
| 238 params.uuid = uuid; |
| 239 params.device_address = device->GetAddress(); |
| 240 params.socket = socket; |
| 241 params.socket_data = socket_data(); |
| 242 BrowserThread::PostTask( |
| 243 params.thread_id, FROM_HERE, base::Bind(&RegisterSocket, params)); |
| 244 } |
| 245 |
| 246 // static |
| 247 void BluetoothAPI::RegisterSocket( |
| 248 const BluetoothAPI::ConnectionParams& params) { |
| 249 DCHECK(BrowserThread::CurrentlyOn(params.thread_id)); |
| 250 |
| 251 BluetoothApiSocket* api_socket = new BluetoothApiSocket( |
| 252 params.extension_id, params.socket, params.device_address, params.uuid); |
| 253 int socket_id = params.socket_data->Add(api_socket); |
| 254 |
| 255 BrowserThread::PostTask(BrowserThread::UI, |
| 256 FROM_HERE, |
| 257 base::Bind(&RegisterSocketUI, params, socket_id)); |
| 258 } |
| 259 |
| 260 // static |
| 261 void BluetoothAPI::RegisterSocketUI(const ConnectionParams& params, |
| 262 int socket_id) { |
| 263 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 264 |
| 265 content::BrowserContext* context = |
| 266 reinterpret_cast<content::BrowserContext*>(params.browser_context_id); |
| 267 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context)) |
| 268 return; |
| 269 |
| 270 BluetoothAPI::Get(context)->event_router()->GetAdapter( |
| 271 base::Bind(&RegisterSocketWithAdapterUI, params, socket_id)); |
| 272 } |
| 273 |
| 274 void BluetoothAPI::RegisterSocketWithAdapterUI( |
| 275 const ConnectionParams& params, |
| 276 int socket_id, |
| 277 scoped_refptr<device::BluetoothAdapter> adapter) { |
| 278 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 279 |
| 280 content::BrowserContext* context = |
| 281 reinterpret_cast<content::BrowserContext*>(params.browser_context_id); |
| 282 if (!extensions::ExtensionsBrowserClient::Get()->IsValidContext(context)) |
| 283 return; |
| 284 |
| 285 BluetoothDevice* device = adapter->GetDevice(params.device_address); |
| 286 if (!device) |
| 287 return; |
| 288 |
| 289 api::bluetooth::Socket result_socket; |
| 290 bluetooth::BluetoothDeviceToApiDevice(*device, &result_socket.device); |
| 291 result_socket.uuid = params.uuid; |
| 292 result_socket.id = socket_id; |
| 293 |
| 294 scoped_ptr<base::ListValue> args = |
| 295 bluetooth::OnConnection::Create(result_socket); |
| 296 scoped_ptr<Event> event( |
| 297 new Event(bluetooth::OnConnection::kEventName, args.Pass())); |
| 298 |
| 299 EventRouter* router = ExtensionSystem::Get(context)->event_router(); |
| 300 if (router) |
| 301 router->DispatchEventToExtension(params.extension_id, event.Pass()); |
| 131 } | 302 } |
| 132 | 303 |
| 133 namespace api { | 304 namespace api { |
| 134 | 305 |
| 135 BluetoothAddProfileFunction::BluetoothAddProfileFunction() { | 306 BluetoothSocketApiFunction::BluetoothSocketApiFunction() {} |
| 136 } | 307 |
| 308 BluetoothSocketApiFunction::~BluetoothSocketApiFunction() {} |
| 309 |
| 310 bool BluetoothSocketApiFunction::RunImpl() { |
| 311 if (!PrePrepare() || !Prepare()) { |
| 312 return false; |
| 313 } |
| 314 AsyncWorkStart(); |
| 315 return true; |
| 316 } |
| 317 |
| 318 bool BluetoothSocketApiFunction::PrePrepare() { |
| 319 socket_data_ = BluetoothAPI::Get(browser_context())->socket_data(); |
| 320 socket_event_dispatcher_ = |
| 321 BluetoothAPI::Get(browser_context())->socket_event_dispatcher(); |
| 322 return socket_data_ && socket_event_dispatcher_; |
| 323 } |
| 324 |
| 325 void BluetoothSocketApiFunction::AsyncWorkStart() { |
| 326 Work(); |
| 327 AsyncWorkCompleted(); |
| 328 } |
| 329 |
| 330 void BluetoothSocketApiFunction::Work() {} |
| 331 |
| 332 void BluetoothSocketApiFunction::AsyncWorkCompleted() { |
| 333 SendResponse(Respond()); |
| 334 } |
| 335 |
| 336 bool BluetoothSocketApiFunction::Respond() { return error_.empty(); } |
| 337 |
| 338 BluetoothGetAdapterStateFunction::~BluetoothGetAdapterStateFunction() {} |
| 339 |
| 340 bool BluetoothGetAdapterStateFunction::DoWork( |
| 341 scoped_refptr<BluetoothAdapter> adapter) { |
| 342 bluetooth::AdapterState state; |
| 343 PopulateAdapterState(*adapter.get(), &state); |
| 344 results_ = bluetooth::GetAdapterState::Results::Create(state); |
| 345 SendResponse(true); |
| 346 return true; |
| 347 } |
| 348 |
| 349 BluetoothGetDevicesFunction::~BluetoothGetDevicesFunction() {} |
| 350 |
| 351 bool BluetoothGetDevicesFunction::DoWork( |
| 352 scoped_refptr<BluetoothAdapter> adapter) { |
| 353 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 354 |
| 355 base::ListValue* device_list = new base::ListValue; |
| 356 SetResult(device_list); |
| 357 |
| 358 BluetoothAdapter::DeviceList devices = adapter->GetDevices(); |
| 359 for (BluetoothAdapter::DeviceList::const_iterator iter = devices.begin(); |
| 360 iter != devices.end(); |
| 361 ++iter) { |
| 362 const BluetoothDevice* device = *iter; |
| 363 DCHECK(device); |
| 364 |
| 365 bluetooth::Device extension_device; |
| 366 bluetooth::BluetoothDeviceToApiDevice(*device, &extension_device); |
| 367 |
| 368 device_list->Append(extension_device.ToValue().release()); |
| 369 } |
| 370 |
| 371 SendResponse(true); |
| 372 |
| 373 return true; |
| 374 } |
| 375 |
| 376 BluetoothAddProfileFunction::BluetoothAddProfileFunction() {} |
| 377 |
| 378 BluetoothAddProfileFunction::~BluetoothAddProfileFunction() {} |
| 137 | 379 |
| 138 bool BluetoothAddProfileFunction::RunImpl() { | 380 bool BluetoothAddProfileFunction::RunImpl() { |
| 381 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 139 scoped_ptr<AddProfile::Params> params(AddProfile::Params::Create(*args_)); | 382 scoped_ptr<AddProfile::Params> params(AddProfile::Params::Create(*args_)); |
| 140 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 383 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
| 141 | 384 |
| 142 if (!BluetoothDevice::IsUUIDValid(params->profile.uuid)) { | 385 if (!BluetoothDevice::IsUUIDValid(params->profile.uuid)) { |
| 143 SetError(kInvalidUuid); | 386 SetError(kInvalidUuid); |
| 144 return false; | 387 return false; |
| 145 } | 388 } |
| 146 | 389 |
| 147 BluetoothPermissionRequest param(params->profile.uuid); | 390 BluetoothPermissionRequest param(params->profile.uuid); |
| 148 if (!BluetoothManifestData::CheckRequest(GetExtension(), param)) { | 391 if (!BluetoothManifestData::CheckRequest(GetExtension(), param)) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 RegisterProfile( | 425 RegisterProfile( |
| 183 options, | 426 options, |
| 184 base::Bind(&BluetoothAddProfileFunction::OnProfileRegistered, this)); | 427 base::Bind(&BluetoothAddProfileFunction::OnProfileRegistered, this)); |
| 185 | 428 |
| 186 return true; | 429 return true; |
| 187 } | 430 } |
| 188 | 431 |
| 189 void BluetoothAddProfileFunction::RegisterProfile( | 432 void BluetoothAddProfileFunction::RegisterProfile( |
| 190 const BluetoothProfile::Options& options, | 433 const BluetoothProfile::Options& options, |
| 191 const BluetoothProfile::ProfileCallback& callback) { | 434 const BluetoothProfile::ProfileCallback& callback) { |
| 435 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 192 BluetoothProfile::Register(uuid_, options, callback); | 436 BluetoothProfile::Register(uuid_, options, callback); |
| 193 } | 437 } |
| 194 | 438 |
| 195 void BluetoothAddProfileFunction::OnProfileRegistered( | 439 void BluetoothAddProfileFunction::OnProfileRegistered( |
| 196 BluetoothProfile* bluetooth_profile) { | 440 BluetoothProfile* bluetooth_profile) { |
| 441 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 197 if (!bluetooth_profile) { | 442 if (!bluetooth_profile) { |
| 198 SetError(kProfileRegistrationFailed); | 443 SetError(kProfileRegistrationFailed); |
| 199 SendResponse(false); | 444 SendResponse(false); |
| 200 return; | 445 return; |
| 201 } | 446 } |
| 202 | 447 |
| 203 if (GetEventRouter(browser_context())->HasProfile(uuid_)) { | 448 if (GetEventRouter(browser_context())->HasProfile(uuid_)) { |
| 204 bluetooth_profile->Unregister(); | 449 bluetooth_profile->Unregister(); |
| 205 SetError(kProfileAlreadyRegistered); | 450 SetError(kProfileAlreadyRegistered); |
| 206 SendResponse(false); | 451 SendResponse(false); |
| 207 return; | 452 return; |
| 208 } | 453 } |
| 209 | 454 |
| 210 bluetooth_profile->SetConnectionCallback( | 455 bluetooth_profile->SetConnectionCallback( |
| 211 base::Bind(&ExtensionBluetoothEventRouter::DispatchConnectionEvent, | 456 base::Bind(&DispatchConnectionEventWorker, |
| 212 base::Unretained(GetEventRouter(browser_context())), | 457 browser_context(), |
| 213 extension_id(), | 458 extension_id(), |
| 214 uuid_)); | 459 uuid_)); |
| 215 GetEventRouter(browser_context()) | 460 GetEventRouter(browser_context()) |
| 216 ->AddProfile(uuid_, extension_id(), bluetooth_profile); | 461 ->AddProfile(uuid_, extension_id(), bluetooth_profile); |
| 217 SendResponse(true); | 462 SendResponse(true); |
| 218 } | 463 } |
| 219 | 464 |
| 465 BluetoothRemoveProfileFunction::~BluetoothRemoveProfileFunction() {} |
| 466 |
| 220 bool BluetoothRemoveProfileFunction::RunImpl() { | 467 bool BluetoothRemoveProfileFunction::RunImpl() { |
| 468 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 221 scoped_ptr<RemoveProfile::Params> params( | 469 scoped_ptr<RemoveProfile::Params> params( |
| 222 RemoveProfile::Params::Create(*args_)); | 470 RemoveProfile::Params::Create(*args_)); |
| 223 | 471 |
| 224 if (!BluetoothDevice::IsUUIDValid(params->profile.uuid)) { | 472 if (!BluetoothDevice::IsUUIDValid(params->profile.uuid)) { |
| 225 SetError(kInvalidUuid); | 473 SetError(kInvalidUuid); |
| 226 return false; | 474 return false; |
| 227 } | 475 } |
| 228 | 476 |
| 229 std::string uuid = | 477 std::string uuid = |
| 230 device::bluetooth_utils::CanonicalUuid(params->profile.uuid); | 478 device::bluetooth_utils::CanonicalUuid(params->profile.uuid); |
| 231 | 479 |
| 232 if (!GetEventRouter(browser_context())->HasProfile(uuid)) { | 480 if (!GetEventRouter(browser_context())->HasProfile(uuid)) { |
| 233 SetError(kProfileNotFound); | 481 SetError(kProfileNotFound); |
| 234 return false; | 482 return false; |
| 235 } | 483 } |
| 236 | 484 |
| 237 GetEventRouter(browser_context())->RemoveProfile(uuid); | 485 GetEventRouter(browser_context())->RemoveProfile(uuid); |
| 238 return true; | 486 return true; |
| 239 } | 487 } |
| 240 | 488 |
| 241 // TODO(youngki): Implement. | 489 BluetoothGetProfilesFunction::~BluetoothGetProfilesFunction() {} |
| 490 |
| 242 bool BluetoothGetProfilesFunction::DoWork( | 491 bool BluetoothGetProfilesFunction::DoWork( |
| 243 scoped_refptr<device::BluetoothAdapter> adapter) { | 492 scoped_refptr<device::BluetoothAdapter> adapter) { |
| 493 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 244 scoped_ptr<GetProfiles::Params> params(GetProfiles::Params::Create(*args_)); | 494 scoped_ptr<GetProfiles::Params> params(GetProfiles::Params::Create(*args_)); |
| 245 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 495 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
| 246 const bluetooth::GetProfilesOptions& options = params->options; | 496 const bluetooth::GetProfilesOptions& options = params->options; |
| 247 | 497 |
| 248 BluetoothDevice* device = adapter->GetDevice(options.device.address); | 498 BluetoothDevice* device = adapter->GetDevice(options.device.address); |
| 249 if (!device) { | 499 if (!device) { |
| 250 SetError(kInvalidDevice); | 500 SetError(kInvalidDevice); |
| 251 SendResponse(false); | 501 SendResponse(false); |
| 252 return false; | 502 return false; |
| 253 } | 503 } |
| 254 | 504 |
| 255 BluetoothDevice::ServiceList service_list = device->GetServices(); | 505 BluetoothDevice::ServiceList service_list = device->GetServices(); |
| 256 | 506 |
| 257 base::ListValue* profiles = new base::ListValue; | 507 std::vector<linked_ptr<bluetooth::Profile> > profiles; |
| 258 for (BluetoothDevice::ServiceList::const_iterator iter = service_list.begin(); | 508 for (BluetoothDevice::ServiceList::const_iterator iter = service_list.begin(); |
| 259 iter != service_list.end(); | 509 iter != service_list.end(); |
| 260 ++iter) { | 510 ++iter) { |
| 261 bluetooth::Profile api_profile; | 511 linked_ptr<bluetooth::Profile> api_profile(new bluetooth::Profile()); |
| 262 api_profile.uuid = *iter; | 512 api_profile->uuid = *iter; |
| 263 profiles->Append(api_profile.ToValue().release()); | 513 // TODO(rpaquay): Add other fields. |
| 514 profiles.push_back(api_profile); |
| 264 } | 515 } |
| 265 | 516 |
| 266 SetResult(profiles); | 517 results_ = bluetooth::GetProfiles::Results::Create(profiles); |
| 267 SendResponse(true); | 518 SendResponse(true); |
| 268 | 519 |
| 269 return true; | 520 return true; |
| 270 } | 521 } |
| 271 | 522 |
| 272 bool BluetoothGetAdapterStateFunction::DoWork( | 523 BluetoothConnectFunction::~BluetoothConnectFunction() {} |
| 273 scoped_refptr<BluetoothAdapter> adapter) { | |
| 274 bluetooth::AdapterState state; | |
| 275 PopulateAdapterState(*adapter.get(), &state); | |
| 276 SetResult(state.ToValue().release()); | |
| 277 SendResponse(true); | |
| 278 return true; | |
| 279 } | |
| 280 | |
| 281 bool BluetoothGetDevicesFunction::DoWork( | |
| 282 scoped_refptr<BluetoothAdapter> adapter) { | |
| 283 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
| 284 | |
| 285 base::ListValue* device_list = new base::ListValue; | |
| 286 SetResult(device_list); | |
| 287 | |
| 288 BluetoothAdapter::DeviceList devices = adapter->GetDevices(); | |
| 289 for (BluetoothAdapter::DeviceList::const_iterator iter = devices.begin(); | |
| 290 iter != devices.end(); | |
| 291 ++iter) { | |
| 292 const BluetoothDevice* device = *iter; | |
| 293 DCHECK(device); | |
| 294 | |
| 295 bluetooth::Device extension_device; | |
| 296 bluetooth::BluetoothDeviceToApiDevice(*device, &extension_device); | |
| 297 | |
| 298 device_list->Append(extension_device.ToValue().release()); | |
| 299 } | |
| 300 | |
| 301 SendResponse(true); | |
| 302 | |
| 303 return true; | |
| 304 } | |
| 305 | |
| 306 void BluetoothGetServicesFunction::GetServiceRecordsCallback( | |
| 307 base::ListValue* services, | |
| 308 const BluetoothDevice::ServiceRecordList& records) { | |
| 309 for (BluetoothDevice::ServiceRecordList::const_iterator i = records.begin(); | |
| 310 i != records.end(); ++i) { | |
| 311 const BluetoothServiceRecord& record = **i; | |
| 312 bluetooth::ServiceRecord api_record; | |
| 313 api_record.name = record.name(); | |
| 314 if (!record.uuid().empty()) | |
| 315 api_record.uuid.reset(new std::string(record.uuid())); | |
| 316 services->Append(api_record.ToValue().release()); | |
| 317 } | |
| 318 | |
| 319 SendResponse(true); | |
| 320 } | |
| 321 | |
| 322 void BluetoothGetServicesFunction::OnErrorCallback() { | |
| 323 SetError(kServiceDiscoveryFailed); | |
| 324 SendResponse(false); | |
| 325 } | |
| 326 | |
| 327 bool BluetoothGetServicesFunction::DoWork( | |
| 328 scoped_refptr<BluetoothAdapter> adapter) { | |
| 329 scoped_ptr<GetServices::Params> params(GetServices::Params::Create(*args_)); | |
| 330 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | |
| 331 const bluetooth::GetServicesOptions& options = params->options; | |
| 332 | |
| 333 BluetoothDevice* device = adapter->GetDevice(options.device_address); | |
| 334 if (!device) { | |
| 335 SetError(kInvalidDevice); | |
| 336 SendResponse(false); | |
| 337 return false; | |
| 338 } | |
| 339 | |
| 340 base::ListValue* services = new base::ListValue; | |
| 341 SetResult(services); | |
| 342 | |
| 343 device->GetServiceRecords( | |
| 344 base::Bind(&BluetoothGetServicesFunction::GetServiceRecordsCallback, | |
| 345 this, | |
| 346 services), | |
| 347 base::Bind(&BluetoothGetServicesFunction::OnErrorCallback, | |
| 348 this)); | |
| 349 | |
| 350 return true; | |
| 351 } | |
| 352 | |
| 353 void BluetoothConnectFunction::OnSuccessCallback() { | |
| 354 SendResponse(true); | |
| 355 } | |
| 356 | |
| 357 void BluetoothConnectFunction::OnErrorCallback() { | |
| 358 SetError(kFailedToConnect); | |
| 359 SendResponse(false); | |
| 360 } | |
| 361 | 524 |
| 362 bool BluetoothConnectFunction::DoWork(scoped_refptr<BluetoothAdapter> adapter) { | 525 bool BluetoothConnectFunction::DoWork(scoped_refptr<BluetoothAdapter> adapter) { |
| 526 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 363 scoped_ptr<Connect::Params> params(Connect::Params::Create(*args_)); | 527 scoped_ptr<Connect::Params> params(Connect::Params::Create(*args_)); |
| 364 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 528 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
| 365 const bluetooth::ConnectOptions& options = params->options; | 529 const bluetooth::ConnectOptions& options = params->options; |
| 366 | 530 |
| 367 if (!BluetoothDevice::IsUUIDValid(options.profile.uuid)) { | 531 if (!BluetoothDevice::IsUUIDValid(options.profile.uuid)) { |
| 368 SetError(kInvalidUuid); | 532 SetError(kInvalidUuid); |
| 369 SendResponse(false); | 533 SendResponse(false); |
| 370 return false; | 534 return false; |
| 371 } | 535 } |
| 372 | 536 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 389 } | 553 } |
| 390 | 554 |
| 391 device->ConnectToProfile( | 555 device->ConnectToProfile( |
| 392 bluetooth_profile, | 556 bluetooth_profile, |
| 393 base::Bind(&BluetoothConnectFunction::OnSuccessCallback, this), | 557 base::Bind(&BluetoothConnectFunction::OnSuccessCallback, this), |
| 394 base::Bind(&BluetoothConnectFunction::OnErrorCallback, this)); | 558 base::Bind(&BluetoothConnectFunction::OnErrorCallback, this)); |
| 395 | 559 |
| 396 return true; | 560 return true; |
| 397 } | 561 } |
| 398 | 562 |
| 399 bool BluetoothDisconnectFunction::RunImpl() { | 563 void BluetoothConnectFunction::OnSuccessCallback() { |
| 400 scoped_ptr<Disconnect::Params> params(Disconnect::Params::Create(*args_)); | 564 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 401 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 565 SendResponse(true); |
| 402 const bluetooth::DisconnectOptions& options = params->options; | |
| 403 return GetEventRouter(browser_context())->ReleaseSocket(options.socket.id); | |
| 404 } | 566 } |
| 405 | 567 |
| 406 BluetoothReadFunction::BluetoothReadFunction() : success_(false) {} | 568 void BluetoothConnectFunction::OnErrorCallback(const std::string& error) { |
| 407 BluetoothReadFunction::~BluetoothReadFunction() {} | 569 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 570 SetError(error); |
| 571 SendResponse(false); |
| 572 } |
| 408 | 573 |
| 409 bool BluetoothReadFunction::Prepare() { | 574 BluetoothDisconnectFunction::BluetoothDisconnectFunction() {} |
| 410 scoped_ptr<Read::Params> params(Read::Params::Create(*args_)); | |
| 411 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | |
| 412 const bluetooth::ReadOptions& options = params->options; | |
| 413 | 575 |
| 414 socket_ = GetEventRouter(browser_context())->GetSocket(options.socket.id); | 576 BluetoothDisconnectFunction::~BluetoothDisconnectFunction() {} |
| 415 if (socket_.get() == NULL) { | |
| 416 SetError(kSocketNotFoundError); | |
| 417 return false; | |
| 418 } | |
| 419 | 577 |
| 420 success_ = false; | 578 bool BluetoothDisconnectFunction::Prepare() { |
| 579 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 580 params_ = Disconnect::Params::Create(*args_); |
| 581 EXTENSION_FUNCTION_VALIDATE(params_.get() != NULL); |
| 421 return true; | 582 return true; |
| 422 } | 583 } |
| 423 | 584 |
| 424 void BluetoothReadFunction::Work() { | 585 void BluetoothDisconnectFunction::AsyncWorkStart() { |
| 425 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 586 DCHECK(BrowserThread::CurrentlyOn(work_thread_id())); |
| 426 | 587 BluetoothApiSocket* socket = |
| 427 if (!socket_.get()) | 588 socket_data_->Get(extension_id(), params_->options.socket_id); |
| 589 if (!socket) { |
| 590 error_ = kSocketNotFoundError; |
| 428 return; | 591 return; |
| 429 | 592 } |
| 430 scoped_refptr<net::GrowableIOBuffer> buffer(new net::GrowableIOBuffer); | 593 socket->Disconnect(base::Bind(&BluetoothDisconnectFunction::OnSuccess, this)); |
| 431 success_ = socket_->Receive(buffer.get()); | |
| 432 if (success_) | |
| 433 SetResult(base::BinaryValue::CreateWithCopiedBuffer(buffer->StartOfBuffer(), | |
| 434 buffer->offset())); | |
| 435 else | |
| 436 SetError(socket_->GetLastErrorMessage()); | |
| 437 } | 594 } |
| 438 | 595 |
| 439 bool BluetoothReadFunction::Respond() { | 596 void BluetoothDisconnectFunction::OnSuccess() { |
| 440 return success_; | 597 DCHECK(BrowserThread::CurrentlyOn(work_thread_id())); |
| 598 socket_data_->Remove(extension_id(), params_->options.socket_id); |
| 599 results_ = bluetooth::Disconnect::Results::Create(); |
| 600 AsyncWorkCompleted(); |
| 441 } | 601 } |
| 442 | 602 |
| 443 BluetoothWriteFunction::BluetoothWriteFunction() | 603 BluetoothSendFunction::BluetoothSendFunction() : io_buffer_size_(0) {} |
| 444 : success_(false), | 604 |
| 445 data_to_write_(NULL) { | 605 BluetoothSendFunction::~BluetoothSendFunction() {} |
| 606 |
| 607 bool BluetoothSendFunction::Prepare() { |
| 608 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 609 params_ = Send::Params::Create(*args_); |
| 610 EXTENSION_FUNCTION_VALIDATE(params_.get() != NULL); |
| 611 io_buffer_size_ = params_->data.size(); |
| 612 io_buffer_ = new net::WrappedIOBuffer(params_->data.data()); |
| 613 return true; |
| 446 } | 614 } |
| 447 | 615 |
| 448 BluetoothWriteFunction::~BluetoothWriteFunction() {} | 616 void BluetoothSendFunction::AsyncWorkStart() { |
| 617 DCHECK(BrowserThread::CurrentlyOn(work_thread_id())); |
| 618 BluetoothApiSocket* socket = |
| 619 socket_data_->Get(extension_id(), params_->socket_id); |
| 620 if (!socket) { |
| 621 error_ = kSocketNotFoundError; |
| 622 return; |
| 623 } |
| 624 socket->Send(io_buffer_, |
| 625 io_buffer_size_, |
| 626 base::Bind(&BluetoothSendFunction::OnSendSuccess, this), |
| 627 base::Bind(&BluetoothSendFunction::OnSendError, this)); |
| 628 } |
| 449 | 629 |
| 450 bool BluetoothWriteFunction::Prepare() { | 630 void BluetoothSendFunction::OnSendSuccess(int bytes_sent) { |
| 451 // TODO(bryeung): update to new-style parameter passing when ArrayBuffer | 631 DCHECK(BrowserThread::CurrentlyOn(work_thread_id())); |
| 452 // support is added | 632 results_ = Send::Results::Create(bytes_sent); |
| 453 base::DictionaryValue* options; | 633 AsyncWorkCompleted(); |
| 454 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options)); | 634 } |
| 455 | 635 |
| 456 base::DictionaryValue* socket; | 636 void BluetoothSendFunction::OnSendError(const std::string& message) { |
| 457 EXTENSION_FUNCTION_VALIDATE(options->GetDictionary("socket", &socket)); | 637 DCHECK(BrowserThread::CurrentlyOn(work_thread_id())); |
| 638 error_ = message; |
| 639 AsyncWorkCompleted(); |
| 640 } |
| 458 | 641 |
| 459 int socket_id; | 642 BluetoothUpdateSocketFunction::BluetoothUpdateSocketFunction() {} |
| 460 EXTENSION_FUNCTION_VALIDATE(socket->GetInteger("id", &socket_id)); | |
| 461 | 643 |
| 462 socket_ = GetEventRouter(browser_context())->GetSocket(socket_id); | 644 BluetoothUpdateSocketFunction::~BluetoothUpdateSocketFunction() {} |
| 463 if (socket_.get() == NULL) { | 645 |
| 464 SetError(kSocketNotFoundError); | 646 bool BluetoothUpdateSocketFunction::Prepare() { |
| 465 return false; | 647 params_ = bluetooth::UpdateSocket::Params::Create(*args_); |
| 648 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 649 return true; |
| 650 } |
| 651 |
| 652 void BluetoothUpdateSocketFunction::Work() { |
| 653 BluetoothApiSocket* socket = |
| 654 socket_data_->Get(extension_id(), params_->socket_id); |
| 655 if (!socket) { |
| 656 error_ = kSocketNotFoundError; |
| 657 return; |
| 466 } | 658 } |
| 467 | 659 |
| 468 base::BinaryValue* tmp_data; | 660 SetSocketProperties(socket, ¶ms_.get()->properties); |
| 469 EXTENSION_FUNCTION_VALIDATE(options->GetBinary("data", &tmp_data)); | 661 results_ = bluetooth::UpdateSocket::Results::Create(); |
| 470 data_to_write_ = tmp_data; | |
| 471 | |
| 472 success_ = false; | |
| 473 return socket_.get() != NULL; | |
| 474 } | 662 } |
| 475 | 663 |
| 476 void BluetoothWriteFunction::Work() { | 664 BluetoothSetSocketPausedFunction::BluetoothSetSocketPausedFunction() {} |
| 477 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
| 478 | 665 |
| 479 if (socket_.get() == NULL) | 666 BluetoothSetSocketPausedFunction::~BluetoothSetSocketPausedFunction() {} |
| 480 return; | |
| 481 | 667 |
| 482 scoped_refptr<net::WrappedIOBuffer> wrapped_io_buffer( | 668 bool BluetoothSetSocketPausedFunction::Prepare() { |
| 483 new net::WrappedIOBuffer(data_to_write_->GetBuffer())); | 669 params_ = bluetooth::SetSocketPaused::Params::Create(*args_); |
| 484 scoped_refptr<net::DrainableIOBuffer> drainable_io_buffer( | 670 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 485 new net::DrainableIOBuffer(wrapped_io_buffer.get(), | 671 return true; |
| 486 data_to_write_->GetSize())); | |
| 487 success_ = socket_->Send(drainable_io_buffer.get()); | |
| 488 if (success_) { | |
| 489 if (drainable_io_buffer->BytesConsumed() > 0) | |
| 490 SetResult(new base::FundamentalValue( | |
| 491 drainable_io_buffer->BytesConsumed())); | |
| 492 else | |
| 493 results_.reset(); | |
| 494 } else { | |
| 495 SetError(socket_->GetLastErrorMessage()); | |
| 496 } | |
| 497 } | 672 } |
| 498 | 673 |
| 499 bool BluetoothWriteFunction::Respond() { | 674 void BluetoothSetSocketPausedFunction::Work() { |
| 500 return success_; | 675 BluetoothApiSocket* socket = |
| 676 socket_data_->Get(extension_id(), params_->socket_id); |
| 677 if (!socket) { |
| 678 error_ = kSocketNotFoundError; |
| 679 return; |
| 680 } |
| 681 |
| 682 if (socket->paused() != params_->paused) { |
| 683 socket->set_paused(params_->paused); |
| 684 if (!params_->paused) { |
| 685 socket_event_dispatcher_->OnSocketResume(extension_->id(), |
| 686 params_->socket_id); |
| 687 } |
| 688 } |
| 689 |
| 690 results_ = bluetooth::SetSocketPaused::Results::Create(); |
| 691 } |
| 692 |
| 693 BluetoothGetSocketFunction::BluetoothGetSocketFunction() {} |
| 694 |
| 695 BluetoothGetSocketFunction::~BluetoothGetSocketFunction() {} |
| 696 |
| 697 bool BluetoothGetSocketFunction::Prepare() { |
| 698 params_ = bluetooth::GetSocket::Params::Create(*args_); |
| 699 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 700 return true; |
| 701 } |
| 702 |
| 703 void BluetoothGetSocketFunction::Work() { |
| 704 BluetoothApiSocket* socket = |
| 705 socket_data_->Get(extension_id(), params_->socket_id); |
| 706 if (!socket) { |
| 707 error_ = kSocketNotFoundError; |
| 708 return; |
| 709 } |
| 710 |
| 711 linked_ptr<bluetooth::Socket> socket_info = |
| 712 CreateSocketInfo(params_->socket_id, socket); |
| 713 results_ = bluetooth::GetSocket::Results::Create(*socket_info); |
| 714 } |
| 715 |
| 716 BluetoothGetSocketsFunction::BluetoothGetSocketsFunction() {} |
| 717 |
| 718 BluetoothGetSocketsFunction::~BluetoothGetSocketsFunction() {} |
| 719 |
| 720 bool BluetoothGetSocketsFunction::Prepare() { return true; } |
| 721 |
| 722 void BluetoothGetSocketsFunction::Work() { |
| 723 std::vector<linked_ptr<bluetooth::Socket> > socket_infos; |
| 724 base::hash_set<int>* resource_ids = |
| 725 socket_data_->GetResourceIds(extension_id()); |
| 726 if (resource_ids != NULL) { |
| 727 for (base::hash_set<int>::iterator it = resource_ids->begin(); |
| 728 it != resource_ids->end(); |
| 729 ++it) { |
| 730 int socket_id = *it; |
| 731 BluetoothApiSocket* socket = socket_data_->Get(extension_id(), socket_id); |
| 732 if (socket) { |
| 733 socket_infos.push_back(CreateSocketInfo(socket_id, socket)); |
| 734 } |
| 735 } |
| 736 } |
| 737 results_ = bluetooth::GetSockets::Results::Create(socket_infos); |
| 501 } | 738 } |
| 502 | 739 |
| 503 void BluetoothSetOutOfBandPairingDataFunction::OnSuccessCallback() { | 740 void BluetoothSetOutOfBandPairingDataFunction::OnSuccessCallback() { |
| 504 SendResponse(true); | 741 SendResponse(true); |
| 505 } | 742 } |
| 506 | 743 |
| 507 void BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback() { | 744 void BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback() { |
| 508 SetError(kCouldNotSetOutOfBandPairingData); | 745 SetError(kCouldNotSetOutOfBandPairingData); |
| 509 SendResponse(false); | 746 SendResponse(false); |
| 510 } | 747 } |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 634 adapter, | 871 adapter, |
| 635 extension_id(), | 872 extension_id(), |
| 636 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), | 873 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), |
| 637 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); | 874 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); |
| 638 | 875 |
| 639 return true; | 876 return true; |
| 640 } | 877 } |
| 641 | 878 |
| 642 } // namespace api | 879 } // namespace api |
| 643 } // namespace extensions | 880 } // namespace extensions |
| OLD | NEW |