| 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 #if defined(OS_CHROMEOS) | 7 #if defined(OS_CHROMEOS) |
| 8 #include <errno.h> | 8 #include <errno.h> |
| 9 #endif | 9 #endif |
| 10 | 10 |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/utf_string_conversions.h" | 13 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" |
| 14 #include "chrome/browser/extensions/extension_service.h" | 14 #include "chrome/browser/extensions/extension_service.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/common/extensions/api/experimental_bluetooth.h" | 16 #include "chrome/common/extensions/api/experimental_bluetooth.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 | 18 |
| 19 #if defined(OS_CHROMEOS) | 19 #if defined(OS_CHROMEOS) |
| 20 #include "base/memory/ref_counted.h" | 20 #include "base/memory/ref_counted.h" |
| 21 #include "base/safe_strerror_posix.h" | 21 #include "base/safe_strerror_posix.h" |
| 22 #include "base/synchronization/lock.h" | |
| 23 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" | 22 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" |
| 24 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" | 23 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" |
| 25 #include "chrome/browser/chromeos/bluetooth/bluetooth_socket.h" | 24 #include "chrome/browser/chromeos/bluetooth/bluetooth_socket.h" |
| 26 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h" | 25 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h" |
| 27 #include "chromeos/dbus/bluetooth_out_of_band_client.h" | 26 #include "chromeos/dbus/bluetooth_out_of_band_client.h" |
| 28 | 27 |
| 29 namespace { | 28 namespace { |
| 30 | 29 |
| 31 chromeos::ExtensionBluetoothEventRouter* GetEventRouter(Profile* profile) { | 30 chromeos::ExtensionBluetoothEventRouter* GetEventRouter(Profile* profile) { |
| 32 return profile->GetExtensionService()->bluetooth_event_router(); | 31 return profile->GetExtensionService()->bluetooth_event_router(); |
| 33 } | 32 } |
| 34 | 33 |
| 35 const chromeos::BluetoothAdapter* GetAdapter(Profile* profile) { | 34 const chromeos::BluetoothAdapter* GetAdapter(Profile* profile) { |
| 36 return GetEventRouter(profile)->adapter(); | 35 return GetEventRouter(profile)->adapter(); |
| 37 } | 36 } |
| 38 | 37 |
| 39 chromeos::BluetoothAdapter* GetMutableAdapter(Profile* profile) { | 38 chromeos::BluetoothAdapter* GetMutableAdapter(Profile* profile) { |
| 40 return GetEventRouter(profile)->GetMutableAdapter(); | 39 return GetEventRouter(profile)->GetMutableAdapter(); |
| 41 } | 40 } |
| 42 | 41 |
| 43 // Fill in a Device object from a chromeos::BluetoothDevice. | |
| 44 void PopulateApiDevice(const chromeos::BluetoothDevice& device, | |
| 45 extensions::api::experimental_bluetooth::Device* out) { | |
| 46 out->name = UTF16ToUTF8(device.GetName()); | |
| 47 out->address = device.address(); | |
| 48 out->paired = device.IsPaired(); | |
| 49 out->bonded = device.IsBonded(); | |
| 50 out->connected = device.IsConnected(); | |
| 51 } | |
| 52 | |
| 53 // The caller takes ownership of the returned pointer. | |
| 54 base::Value* BluetoothDeviceToValue(const chromeos::BluetoothDevice& device) { | |
| 55 extensions::api::experimental_bluetooth::Device api_device; | |
| 56 PopulateApiDevice(device, &api_device); | |
| 57 return api_device.ToValue().release(); | |
| 58 } | |
| 59 | |
| 60 } // namespace | 42 } // namespace |
| 61 #endif | 43 #endif |
| 62 | 44 |
| 63 namespace { | 45 namespace { |
| 64 | 46 |
| 65 const char kCouldNotGetLocalOutOfBandPairingData[] = | 47 const char kCouldNotGetLocalOutOfBandPairingData[] = |
| 66 "Could not get local Out Of Band Pairing Data"; | 48 "Could not get local Out Of Band Pairing Data"; |
| 67 const char kCouldNotSetOutOfBandPairingData[] = | 49 const char kCouldNotSetOutOfBandPairingData[] = |
| 68 "Could not set Out Of Band Pairing Data"; | 50 "Could not set Out Of Band Pairing Data"; |
| 69 const char kFailedToConnect[] = "Connection failed"; | 51 const char kFailedToConnect[] = "Connection failed"; |
| 70 const char kInvalidDevice[] = "Invalid device"; | 52 const char kInvalidDevice[] = "Invalid device"; |
| 71 const char kSocketNotFoundError[] = "Socket not found: invalid socket id"; | 53 const char kSocketNotFoundError[] = "Socket not found: invalid socket id"; |
| 54 const char kStartDiscoveryFailed[] = |
| 55 "Starting discovery failed, or already discovering"; |
| 56 const char kStopDiscoveryFailed[] = "Failed to stop discovery"; |
| 72 | 57 |
| 73 } // namespace | 58 } // namespace |
| 74 | 59 |
| 75 namespace Connect = extensions::api::experimental_bluetooth::Connect; | 60 namespace Connect = extensions::api::experimental_bluetooth::Connect; |
| 76 namespace Disconnect = extensions::api::experimental_bluetooth::Disconnect; | 61 namespace Disconnect = extensions::api::experimental_bluetooth::Disconnect; |
| 77 namespace GetDevices = extensions::api::experimental_bluetooth::GetDevices; | 62 namespace GetDevices = extensions::api::experimental_bluetooth::GetDevices; |
| 78 namespace Read = extensions::api::experimental_bluetooth::Read; | 63 namespace Read = extensions::api::experimental_bluetooth::Read; |
| 79 namespace SetOutOfBandPairingData = | 64 namespace SetOutOfBandPairingData = |
| 80 extensions::api::experimental_bluetooth::SetOutOfBandPairingData; | 65 extensions::api::experimental_bluetooth::SetOutOfBandPairingData; |
| 81 namespace Write = extensions::api::experimental_bluetooth::Write; | 66 namespace Write = extensions::api::experimental_bluetooth::Write; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 103 BluetoothGetDevicesFunction::BluetoothGetDevicesFunction() | 88 BluetoothGetDevicesFunction::BluetoothGetDevicesFunction() |
| 104 : callbacks_pending_(0) {} | 89 : callbacks_pending_(0) {} |
| 105 | 90 |
| 106 void BluetoothGetDevicesFunction::AddDeviceIfTrue( | 91 void BluetoothGetDevicesFunction::AddDeviceIfTrue( |
| 107 ListValue* list, | 92 ListValue* list, |
| 108 const chromeos::BluetoothDevice* device, | 93 const chromeos::BluetoothDevice* device, |
| 109 bool result) { | 94 bool result) { |
| 110 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 95 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 111 | 96 |
| 112 if (result) | 97 if (result) |
| 113 list->Append(BluetoothDeviceToValue(*device)); | 98 list->Append(experimental_bluetooth::BluetoothDeviceToValue(*device)); |
| 114 | 99 |
| 115 callbacks_pending_--; | 100 callbacks_pending_--; |
| 116 if (callbacks_pending_ == -1) { | 101 if (callbacks_pending_ == -1) { |
| 117 SendResponse(true); | 102 SendResponse(true); |
| 118 Release(); // Added in RunImpl | 103 Release(); // Added in RunImpl |
| 119 } | 104 } |
| 120 } | 105 } |
| 121 | 106 |
| 122 bool BluetoothGetDevicesFunction::RunImpl() { | 107 bool BluetoothGetDevicesFunction::RunImpl() { |
| 123 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 108 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 140 GetMutableAdapter(profile())->GetDevices(); | 125 GetMutableAdapter(profile())->GetDevices(); |
| 141 for (chromeos::BluetoothAdapter::DeviceList::iterator i = devices.begin(); | 126 for (chromeos::BluetoothAdapter::DeviceList::iterator i = devices.begin(); |
| 142 i != devices.end(); ++i) { | 127 i != devices.end(); ++i) { |
| 143 chromeos::BluetoothDevice* device = *i; | 128 chromeos::BluetoothDevice* device = *i; |
| 144 | 129 |
| 145 if (options.uuid.get() != NULL && | 130 if (options.uuid.get() != NULL && |
| 146 !(device->ProvidesServiceWithUUID(*(options.uuid)))) | 131 !(device->ProvidesServiceWithUUID(*(options.uuid)))) |
| 147 continue; | 132 continue; |
| 148 | 133 |
| 149 if (options.name.get() == NULL) { | 134 if (options.name.get() == NULL) { |
| 150 matches->Append(BluetoothDeviceToValue(*device)); | 135 matches->Append(experimental_bluetooth::BluetoothDeviceToValue(*device)); |
| 151 continue; | 136 continue; |
| 152 } | 137 } |
| 153 | 138 |
| 154 callbacks_pending_++; | 139 callbacks_pending_++; |
| 155 device->ProvidesServiceWithName( | 140 device->ProvidesServiceWithName( |
| 156 *(options.name), | 141 *(options.name), |
| 157 base::Bind(&BluetoothGetDevicesFunction::AddDeviceIfTrue, | 142 base::Bind(&BluetoothGetDevicesFunction::AddDeviceIfTrue, |
| 158 this, | 143 this, |
| 159 matches, | 144 matches, |
| 160 device)); | 145 device)); |
| 161 } | 146 } |
| 162 callbacks_pending_--; | 147 callbacks_pending_--; |
| 163 | 148 |
| 164 if (callbacks_pending_ == -1) { | 149 if (callbacks_pending_ == -1) { |
| 165 SendResponse(true); | 150 SendResponse(true); |
| 166 Release(); | 151 Release(); |
| 167 } | 152 } |
| 168 | 153 |
| 169 return true; | 154 return true; |
| 170 } | 155 } |
| 171 | 156 |
| 172 void BluetoothConnectFunction::ConnectToServiceCallback( | 157 void BluetoothConnectFunction::ConnectToServiceCallback( |
| 173 const chromeos::BluetoothDevice* device, | 158 const chromeos::BluetoothDevice* device, |
| 174 const std::string& service_uuid, | 159 const std::string& service_uuid, |
| 175 scoped_refptr<chromeos::BluetoothSocket> socket) { | 160 scoped_refptr<chromeos::BluetoothSocket> socket) { |
| 176 if (socket.get()) { | 161 if (socket.get()) { |
| 177 int socket_id = GetEventRouter(profile())->RegisterSocket(socket); | 162 int socket_id = GetEventRouter(profile())->RegisterSocket(socket); |
| 178 | 163 |
| 179 experimental_bluetooth::Socket result_socket; | 164 experimental_bluetooth::Socket result_socket; |
| 180 PopulateApiDevice(*device, &result_socket.device); | 165 experimental_bluetooth::BluetoothDeviceToApiDevice( |
| 166 *device, &result_socket.device); |
| 181 result_socket.service_uuid = service_uuid; | 167 result_socket.service_uuid = service_uuid; |
| 182 result_socket.id = socket_id; | 168 result_socket.id = socket_id; |
| 183 result_.reset(result_socket.ToValue().release()); | 169 result_.reset(result_socket.ToValue().release()); |
| 184 SendResponse(true); | 170 SendResponse(true); |
| 185 } else { | 171 } else { |
| 186 SetError(kFailedToConnect); | 172 SetError(kFailedToConnect); |
| 187 SendResponse(false); | 173 SendResponse(false); |
| 188 } | 174 } |
| 189 | 175 |
| 190 Release(); // Added in RunImpl | 176 Release(); // Added in RunImpl |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 bool BluetoothGetLocalOutOfBandPairingDataFunction::RunImpl() { | 405 bool BluetoothGetLocalOutOfBandPairingDataFunction::RunImpl() { |
| 420 AddRef(); // Released in one of the callbacks below | 406 AddRef(); // Released in one of the callbacks below |
| 421 GetMutableAdapter(profile())->ReadLocalOutOfBandPairingData( | 407 GetMutableAdapter(profile())->ReadLocalOutOfBandPairingData( |
| 422 base::Bind(&BluetoothGetLocalOutOfBandPairingDataFunction::ReadCallback, | 408 base::Bind(&BluetoothGetLocalOutOfBandPairingDataFunction::ReadCallback, |
| 423 this), | 409 this), |
| 424 base::Bind(&BluetoothGetLocalOutOfBandPairingDataFunction::ErrorCallback, | 410 base::Bind(&BluetoothGetLocalOutOfBandPairingDataFunction::ErrorCallback, |
| 425 this)); | 411 this)); |
| 426 return true; | 412 return true; |
| 427 } | 413 } |
| 428 | 414 |
| 415 void BluetoothStartDiscoveryFunction::OnSuccessCallback() { |
| 416 SendResponse(true); |
| 417 Release(); // Added in RunImpl |
| 418 } |
| 419 |
| 420 void BluetoothStartDiscoveryFunction::OnErrorCallback() { |
| 421 SetError(kStartDiscoveryFailed); |
| 422 SendResponse(false); |
| 423 Release(); // Added in RunImpl |
| 424 } |
| 425 |
| 426 bool BluetoothStartDiscoveryFunction::RunImpl() { |
| 427 GetEventRouter(profile())->SetSendDiscoveryEvents(true); |
| 428 |
| 429 // BluetoothAdapter will throw an error if we SetDiscovering(true) when |
| 430 // discovery is already in progress |
| 431 if (GetMutableAdapter(profile())->IsDiscovering()) { |
| 432 SendResponse(true); |
| 433 return true; |
| 434 } |
| 435 |
| 436 AddRef(); // Removed in whichever callback is called. |
| 437 GetMutableAdapter(profile())->SetDiscovering(true, |
| 438 base::Bind(&BluetoothStartDiscoveryFunction::OnSuccessCallback, this), |
| 439 base::Bind(&BluetoothStartDiscoveryFunction::OnErrorCallback, this)); |
| 440 return true; |
| 441 } |
| 442 |
| 443 void BluetoothStopDiscoveryFunction::OnSuccessCallback() { |
| 444 SendResponse(true); |
| 445 Release(); // Added in RunImpl |
| 446 } |
| 447 |
| 448 void BluetoothStopDiscoveryFunction::OnErrorCallback() { |
| 449 SetError(kStopDiscoveryFailed); |
| 450 SendResponse(false); |
| 451 Release(); // Added in RunImpl |
| 452 } |
| 453 |
| 454 bool BluetoothStopDiscoveryFunction::RunImpl() { |
| 455 GetEventRouter(profile())->SetSendDiscoveryEvents(false); |
| 456 AddRef(); // Removed in whichever callback is called. |
| 457 GetMutableAdapter(profile())->SetDiscovering(false, |
| 458 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), |
| 459 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); |
| 460 return true; |
| 461 } |
| 462 |
| 429 #else | 463 #else |
| 430 | 464 |
| 431 // ----------------------------------------------------------------------------- | 465 // ----------------------------------------------------------------------------- |
| 432 // NIY stubs | 466 // NIY stubs |
| 433 // ----------------------------------------------------------------------------- | 467 // ----------------------------------------------------------------------------- |
| 434 bool BluetoothIsAvailableFunction::RunImpl() { | 468 bool BluetoothIsAvailableFunction::RunImpl() { |
| 435 NOTREACHED() << "Not implemented yet"; | 469 NOTREACHED() << "Not implemented yet"; |
| 436 return false; | 470 return false; |
| 437 } | 471 } |
| 438 | 472 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 } | 517 } |
| 484 | 518 |
| 485 void BluetoothWriteFunction::Work() { | 519 void BluetoothWriteFunction::Work() { |
| 486 } | 520 } |
| 487 | 521 |
| 488 bool BluetoothWriteFunction::Respond() { | 522 bool BluetoothWriteFunction::Respond() { |
| 489 NOTREACHED() << "Not implemented yet"; | 523 NOTREACHED() << "Not implemented yet"; |
| 490 return false; | 524 return false; |
| 491 } | 525 } |
| 492 | 526 |
| 527 bool BluetoothStartDiscoveryFunction::RunImpl() { |
| 528 NOTREACHED() << "Not implemented yet"; |
| 529 return false; |
| 530 } |
| 531 |
| 532 bool BluetoothStopDiscoveryFunction::RunImpl() { |
| 533 NOTREACHED() << "Not implemented yet"; |
| 534 return false; |
| 535 } |
| 536 |
| 493 bool BluetoothSetOutOfBandPairingDataFunction::RunImpl() { | 537 bool BluetoothSetOutOfBandPairingDataFunction::RunImpl() { |
| 494 NOTREACHED() << "Not implemented yet"; | 538 NOTREACHED() << "Not implemented yet"; |
| 495 return false; | 539 return false; |
| 496 } | 540 } |
| 497 | 541 |
| 498 bool BluetoothGetLocalOutOfBandPairingDataFunction::RunImpl() { | 542 bool BluetoothGetLocalOutOfBandPairingDataFunction::RunImpl() { |
| 499 NOTREACHED() << "Not implemented yet"; | 543 NOTREACHED() << "Not implemented yet"; |
| 500 return false; | 544 return false; |
| 501 } | 545 } |
| 502 | 546 |
| 503 #endif | 547 #endif |
| 504 | 548 |
| 505 BluetoothReadFunction::BluetoothReadFunction() {} | 549 BluetoothReadFunction::BluetoothReadFunction() {} |
| 506 BluetoothReadFunction::~BluetoothReadFunction() {} | 550 BluetoothReadFunction::~BluetoothReadFunction() {} |
| 507 | 551 |
| 508 BluetoothWriteFunction::BluetoothWriteFunction() {} | 552 BluetoothWriteFunction::BluetoothWriteFunction() {} |
| 509 BluetoothWriteFunction::~BluetoothWriteFunction() {} | 553 BluetoothWriteFunction::~BluetoothWriteFunction() {} |
| 510 | 554 |
| 511 } // namespace api | 555 } // namespace api |
| 512 } // namespace extensions | 556 } // namespace extensions |
| OLD | NEW |