| 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 "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.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 "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" | 22 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter_interface.h" |
| 23 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" | 23 #include "chrome/browser/chromeos/bluetooth/bluetooth_device_interface.h" |
| 24 #include "chrome/browser/chromeos/bluetooth/bluetooth_socket.h" | 24 #include "chrome/browser/chromeos/bluetooth/bluetooth_service_record_interface.h
" |
| 25 #include "chrome/browser/chromeos/bluetooth/bluetooth_socket_posix.h" |
| 25 #include "chrome/browser/chromeos/bluetooth/bluetooth_utils.h" | 26 #include "chrome/browser/chromeos/bluetooth/bluetooth_utils.h" |
| 26 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h" | 27 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h" |
| 27 #include "chromeos/dbus/bluetooth_out_of_band_client.h" | 28 #include "chromeos/dbus/bluetooth_out_of_band_client.h" |
| 29 #include "chromeos/dbus/bluetooth_out_of_band_pairing_data.h" |
| 28 | 30 |
| 29 namespace { | 31 namespace { |
| 30 | 32 |
| 31 chromeos::ExtensionBluetoothEventRouter* GetEventRouter(Profile* profile) { | 33 chromeos::ExtensionBluetoothEventRouter* GetEventRouter(Profile* profile) { |
| 32 return profile->GetExtensionService()->bluetooth_event_router(); | 34 return profile->GetExtensionService()->bluetooth_event_router(); |
| 33 } | 35 } |
| 34 | 36 |
| 35 const chromeos::BluetoothAdapter& GetAdapter(Profile* profile) { | 37 const chromeos::BluetoothAdapterInterface& GetAdapter(Profile* profile) { |
| 36 return GetEventRouter(profile)->adapter(); | 38 return GetEventRouter(profile)->adapter(); |
| 37 } | 39 } |
| 38 | 40 |
| 39 chromeos::BluetoothAdapter* GetMutableAdapter(Profile* profile) { | 41 chromeos::BluetoothAdapterInterface* GetMutableAdapter(Profile* profile) { |
| 40 chromeos::BluetoothAdapter* adapter = | 42 chromeos::BluetoothAdapterInterface* adapter = |
| 41 GetEventRouter(profile)->GetMutableAdapter(); | 43 GetEventRouter(profile)->GetMutableAdapter(); |
| 42 CHECK(adapter); | 44 CHECK(adapter); |
| 43 return adapter; | 45 return adapter; |
| 44 } | 46 } |
| 45 | 47 |
| 46 } // namespace | 48 } // namespace |
| 47 #endif | 49 #endif |
| 48 | 50 |
| 49 namespace { | 51 namespace { |
| 50 | 52 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 bool BluetoothGetNameFunction::RunImpl() { | 96 bool BluetoothGetNameFunction::RunImpl() { |
| 95 SetResult(Value::CreateStringValue(GetAdapter(profile()).name())); | 97 SetResult(Value::CreateStringValue(GetAdapter(profile()).name())); |
| 96 return true; | 98 return true; |
| 97 } | 99 } |
| 98 | 100 |
| 99 BluetoothGetDevicesFunction::BluetoothGetDevicesFunction() | 101 BluetoothGetDevicesFunction::BluetoothGetDevicesFunction() |
| 100 : callbacks_pending_(0) {} | 102 : callbacks_pending_(0) {} |
| 101 | 103 |
| 102 void BluetoothGetDevicesFunction::AddDeviceIfTrueCallback( | 104 void BluetoothGetDevicesFunction::AddDeviceIfTrueCallback( |
| 103 ListValue* list, | 105 ListValue* list, |
| 104 const chromeos::BluetoothDevice* device, | 106 const chromeos::BluetoothDeviceInterface* device, |
| 105 bool shouldAdd) { | 107 bool shouldAdd) { |
| 106 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 108 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 107 | 109 |
| 108 if (shouldAdd) | 110 if (shouldAdd) |
| 109 list->Append(experimental_bluetooth::BluetoothDeviceToValue(*device)); | 111 list->Append(experimental_bluetooth::BluetoothDeviceToValue(*device)); |
| 110 | 112 |
| 111 callbacks_pending_--; | 113 callbacks_pending_--; |
| 112 if (callbacks_pending_ == -1) | 114 if (callbacks_pending_ == -1) |
| 113 SendResponse(true); | 115 SendResponse(true); |
| 114 } | 116 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 127 SetError(kInvalidUuid); | 129 SetError(kInvalidUuid); |
| 128 return false; | 130 return false; |
| 129 } | 131 } |
| 130 } | 132 } |
| 131 | 133 |
| 132 ListValue* matches = new ListValue; | 134 ListValue* matches = new ListValue; |
| 133 SetResult(matches); | 135 SetResult(matches); |
| 134 | 136 |
| 135 CHECK_EQ(0, callbacks_pending_); | 137 CHECK_EQ(0, callbacks_pending_); |
| 136 | 138 |
| 137 chromeos::BluetoothAdapter::DeviceList devices = | 139 chromeos::BluetoothAdapterInterface::DeviceList devices = |
| 138 GetMutableAdapter(profile())->GetDevices(); | 140 GetMutableAdapter(profile())->GetDevices(); |
| 139 for (chromeos::BluetoothAdapter::DeviceList::iterator i = devices.begin(); | 141 for (chromeos::BluetoothAdapterInterface::DeviceList::iterator i = |
| 140 i != devices.end(); ++i) { | 142 devices.begin(); |
| 141 chromeos::BluetoothDevice* device = *i; | 143 i != devices.end(); ++i) { |
| 144 chromeos::BluetoothDeviceInterface* device = *i; |
| 142 | 145 |
| 143 if (!uuid.empty() && !(device->ProvidesServiceWithUUID(uuid))) | 146 if (!uuid.empty() && !(device->ProvidesServiceWithUUID(uuid))) |
| 144 continue; | 147 continue; |
| 145 | 148 |
| 146 if (options.name.get() == NULL) { | 149 if (options.name.get() == NULL) { |
| 147 matches->Append(experimental_bluetooth::BluetoothDeviceToValue(*device)); | 150 matches->Append(experimental_bluetooth::BluetoothDeviceToValue(*device)); |
| 148 continue; | 151 continue; |
| 149 } | 152 } |
| 150 | 153 |
| 151 callbacks_pending_++; | 154 callbacks_pending_++; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 162 // for-loop, which ensures that all requests have been made before | 165 // for-loop, which ensures that all requests have been made before |
| 163 // SendResponse happens. | 166 // SendResponse happens. |
| 164 if (callbacks_pending_ == -1) | 167 if (callbacks_pending_ == -1) |
| 165 SendResponse(true); | 168 SendResponse(true); |
| 166 | 169 |
| 167 return true; | 170 return true; |
| 168 } | 171 } |
| 169 | 172 |
| 170 void BluetoothGetServicesFunction::GetServiceRecordsCallback( | 173 void BluetoothGetServicesFunction::GetServiceRecordsCallback( |
| 171 base::ListValue* services, | 174 base::ListValue* services, |
| 172 const chromeos::BluetoothDevice::ServiceRecordList& records) { | 175 const chromeos::BluetoothDeviceInterface::ServiceRecordList& records) { |
| 173 for (chromeos::BluetoothDevice::ServiceRecordList::const_iterator i = | 176 for (chromeos::BluetoothDeviceInterface::ServiceRecordList::const_iterator i = |
| 174 records.begin(); i != records.end(); ++i) { | 177 records.begin(); i != records.end(); ++i) { |
| 175 const chromeos::BluetoothServiceRecord& record = **i; | 178 const chromeos::BluetoothServiceRecordInterface& record = **i; |
| 176 experimental_bluetooth::ServiceRecord api_record; | 179 experimental_bluetooth::ServiceRecord api_record; |
| 177 api_record.name = record.name(); | 180 api_record.name = record.name(); |
| 178 if (!record.uuid().empty()) | 181 if (!record.uuid().empty()) |
| 179 api_record.uuid.reset(new std::string(record.uuid())); | 182 api_record.uuid.reset(new std::string(record.uuid())); |
| 180 services->Append(api_record.ToValue().release()); | 183 services->Append(api_record.ToValue().release()); |
| 181 } | 184 } |
| 182 | 185 |
| 183 SendResponse(true); | 186 SendResponse(true); |
| 184 } | 187 } |
| 185 | 188 |
| 186 void BluetoothGetServicesFunction::OnErrorCallback() { | 189 void BluetoothGetServicesFunction::OnErrorCallback() { |
| 187 SetError(kServiceDiscoveryFailed); | 190 SetError(kServiceDiscoveryFailed); |
| 188 SendResponse(false); | 191 SendResponse(false); |
| 189 } | 192 } |
| 190 | 193 |
| 191 bool BluetoothGetServicesFunction::RunImpl() { | 194 bool BluetoothGetServicesFunction::RunImpl() { |
| 192 scoped_ptr<GetServices::Params> params(GetServices::Params::Create(*args_)); | 195 scoped_ptr<GetServices::Params> params(GetServices::Params::Create(*args_)); |
| 193 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 196 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
| 194 const experimental_bluetooth::GetServicesOptions& options = params->options; | 197 const experimental_bluetooth::GetServicesOptions& options = params->options; |
| 195 | 198 |
| 196 chromeos::BluetoothDevice* device = | 199 chromeos::BluetoothDeviceInterface* device = |
| 197 GetMutableAdapter(profile())->GetDevice(options.device_address); | 200 GetMutableAdapter(profile())->GetDevice(options.device_address); |
| 198 if (!device) { | 201 if (!device) { |
| 199 SetError(kInvalidDevice); | 202 SetError(kInvalidDevice); |
| 200 return false; | 203 return false; |
| 201 } | 204 } |
| 202 | 205 |
| 203 ListValue* services = new ListValue; | 206 ListValue* services = new ListValue; |
| 204 SetResult(services); | 207 SetResult(services); |
| 205 | 208 |
| 206 device->GetServiceRecords( | 209 device->GetServiceRecords( |
| 207 base::Bind(&BluetoothGetServicesFunction::GetServiceRecordsCallback, | 210 base::Bind(&BluetoothGetServicesFunction::GetServiceRecordsCallback, |
| 208 this, | 211 this, |
| 209 services), | 212 services), |
| 210 base::Bind(&BluetoothGetServicesFunction::OnErrorCallback, | 213 base::Bind(&BluetoothGetServicesFunction::OnErrorCallback, |
| 211 this)); | 214 this)); |
| 212 | 215 |
| 213 return true; | 216 return true; |
| 214 } | 217 } |
| 215 | 218 |
| 216 void BluetoothConnectFunction::ConnectToServiceCallback( | 219 void BluetoothConnectFunction::ConnectToServiceCallback( |
| 217 const chromeos::BluetoothDevice* device, | 220 const chromeos::BluetoothDeviceInterface* device, |
| 218 const std::string& service_uuid, | 221 const std::string& service_uuid, |
| 219 scoped_refptr<chromeos::BluetoothSocket> socket) { | 222 scoped_refptr<chromeos::BluetoothSocketPosix> socket) { |
| 220 if (socket.get()) { | 223 if (socket.get()) { |
| 221 int socket_id = GetEventRouter(profile())->RegisterSocket(socket); | 224 int socket_id = GetEventRouter(profile())->RegisterSocket(socket); |
| 222 | 225 |
| 223 experimental_bluetooth::Socket result_socket; | 226 experimental_bluetooth::Socket result_socket; |
| 224 experimental_bluetooth::BluetoothDeviceToApiDevice( | 227 experimental_bluetooth::BluetoothDeviceToApiDevice( |
| 225 *device, &result_socket.device); | 228 *device, &result_socket.device); |
| 226 result_socket.service_uuid = service_uuid; | 229 result_socket.service_uuid = service_uuid; |
| 227 result_socket.id = socket_id; | 230 result_socket.id = socket_id; |
| 228 SetResult(result_socket.ToValue().release()); | 231 SetResult(result_socket.ToValue().release()); |
| 229 SendResponse(true); | 232 SendResponse(true); |
| 230 } else { | 233 } else { |
| 231 SetError(kFailedToConnect); | 234 SetError(kFailedToConnect); |
| 232 SendResponse(false); | 235 SendResponse(false); |
| 233 } | 236 } |
| 234 } | 237 } |
| 235 | 238 |
| 236 bool BluetoothConnectFunction::RunImpl() { | 239 bool BluetoothConnectFunction::RunImpl() { |
| 237 scoped_ptr<Connect::Params> params(Connect::Params::Create(*args_)); | 240 scoped_ptr<Connect::Params> params(Connect::Params::Create(*args_)); |
| 238 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 241 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
| 239 const experimental_bluetooth::ConnectOptions& options = params->options; | 242 const experimental_bluetooth::ConnectOptions& options = params->options; |
| 240 | 243 |
| 241 std::string uuid = chromeos::bluetooth_utils::CanonicalUuid( | 244 std::string uuid = chromeos::bluetooth_utils::CanonicalUuid( |
| 242 options.service_uuid); | 245 options.service_uuid); |
| 243 if (uuid.empty()) { | 246 if (uuid.empty()) { |
| 244 SetError(kInvalidUuid); | 247 SetError(kInvalidUuid); |
| 245 return false; | 248 return false; |
| 246 } | 249 } |
| 247 | 250 |
| 248 chromeos::BluetoothDevice* device = | 251 chromeos::BluetoothDeviceInterface* device = |
| 249 GetMutableAdapter(profile())->GetDevice(options.device_address); | 252 GetMutableAdapter(profile())->GetDevice(options.device_address); |
| 250 if (!device) { | 253 if (!device) { |
| 251 SetError(kInvalidDevice); | 254 SetError(kInvalidDevice); |
| 252 return false; | 255 return false; |
| 253 } | 256 } |
| 254 | 257 |
| 255 device->ConnectToService(uuid, | 258 device->ConnectToService(uuid, |
| 256 base::Bind(&BluetoothConnectFunction::ConnectToServiceCallback, | 259 base::Bind(&BluetoothConnectFunction::ConnectToServiceCallback, |
| 257 this, | 260 this, |
| 258 device, | 261 device, |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 } | 382 } |
| 380 | 383 |
| 381 bool BluetoothSetOutOfBandPairingDataFunction::RunImpl() { | 384 bool BluetoothSetOutOfBandPairingDataFunction::RunImpl() { |
| 382 // TODO(bryeung): update to new-style parameter passing when ArrayBuffer | 385 // TODO(bryeung): update to new-style parameter passing when ArrayBuffer |
| 383 // support is added | 386 // support is added |
| 384 DictionaryValue* options; | 387 DictionaryValue* options; |
| 385 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options)); | 388 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options)); |
| 386 std::string address; | 389 std::string address; |
| 387 EXTENSION_FUNCTION_VALIDATE(options->GetString("deviceAddress", &address)); | 390 EXTENSION_FUNCTION_VALIDATE(options->GetString("deviceAddress", &address)); |
| 388 | 391 |
| 389 chromeos::BluetoothDevice* device = | 392 chromeos::BluetoothDeviceInterface* device = |
| 390 GetMutableAdapter(profile())->GetDevice(address); | 393 GetMutableAdapter(profile())->GetDevice(address); |
| 391 if (!device) { | 394 if (!device) { |
| 392 SetError(kInvalidDevice); | 395 SetError(kInvalidDevice); |
| 393 return false; | 396 return false; |
| 394 } | 397 } |
| 395 | 398 |
| 396 if (options->HasKey("data")) { | 399 if (options->HasKey("data")) { |
| 397 DictionaryValue* data_in; | 400 DictionaryValue* data_in; |
| 398 EXTENSION_FUNCTION_VALIDATE(options->GetDictionary("data", &data_in)); | 401 EXTENSION_FUNCTION_VALIDATE(options->GetDictionary("data", &data_in)); |
| 399 | 402 |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 601 #endif | 604 #endif |
| 602 | 605 |
| 603 BluetoothReadFunction::BluetoothReadFunction() {} | 606 BluetoothReadFunction::BluetoothReadFunction() {} |
| 604 BluetoothReadFunction::~BluetoothReadFunction() {} | 607 BluetoothReadFunction::~BluetoothReadFunction() {} |
| 605 | 608 |
| 606 BluetoothWriteFunction::BluetoothWriteFunction() {} | 609 BluetoothWriteFunction::BluetoothWriteFunction() {} |
| 607 BluetoothWriteFunction::~BluetoothWriteFunction() {} | 610 BluetoothWriteFunction::~BluetoothWriteFunction() {} |
| 608 | 611 |
| 609 } // namespace api | 612 } // namespace api |
| 610 } // namespace extensions | 613 } // namespace extensions |
| OLD | NEW |