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.h" |
23 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" | 23 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" |
24 #include "chrome/browser/chromeos/bluetooth/bluetooth_socket.h" | 24 #include "chrome/browser/chromeos/bluetooth/bluetooth_socket.h" |
| 25 #include "chrome/browser/chromeos/bluetooth/bluetooth_utils.h" |
25 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h" | 26 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h" |
26 #include "chromeos/dbus/bluetooth_out_of_band_client.h" | 27 #include "chromeos/dbus/bluetooth_out_of_band_client.h" |
27 | 28 |
28 namespace { | 29 namespace { |
29 | 30 |
30 chromeos::ExtensionBluetoothEventRouter* GetEventRouter(Profile* profile) { | 31 chromeos::ExtensionBluetoothEventRouter* GetEventRouter(Profile* profile) { |
31 return profile->GetExtensionService()->bluetooth_event_router(); | 32 return profile->GetExtensionService()->bluetooth_event_router(); |
32 } | 33 } |
33 | 34 |
34 const chromeos::BluetoothAdapter* GetAdapter(Profile* profile) { | 35 const chromeos::BluetoothAdapter* GetAdapter(Profile* profile) { |
35 return GetEventRouter(profile)->adapter(); | 36 return GetEventRouter(profile)->adapter(); |
36 } | 37 } |
37 | 38 |
38 chromeos::BluetoothAdapter* GetMutableAdapter(Profile* profile) { | 39 chromeos::BluetoothAdapter* GetMutableAdapter(Profile* profile) { |
39 return GetEventRouter(profile)->GetMutableAdapter(); | 40 return GetEventRouter(profile)->GetMutableAdapter(); |
40 } | 41 } |
41 | 42 |
42 } // namespace | 43 } // namespace |
43 #endif | 44 #endif |
44 | 45 |
45 namespace { | 46 namespace { |
46 | 47 |
47 const char kCouldNotGetLocalOutOfBandPairingData[] = | 48 const char kCouldNotGetLocalOutOfBandPairingData[] = |
48 "Could not get local Out Of Band Pairing Data"; | 49 "Could not get local Out Of Band Pairing Data"; |
49 const char kCouldNotSetOutOfBandPairingData[] = | 50 const char kCouldNotSetOutOfBandPairingData[] = |
50 "Could not set Out Of Band Pairing Data"; | 51 "Could not set Out Of Band Pairing Data"; |
51 const char kFailedToConnect[] = "Connection failed"; | 52 const char kFailedToConnect[] = "Connection failed"; |
52 const char kInvalidDevice[] = "Invalid device"; | 53 const char kInvalidDevice[] = "Invalid device"; |
| 54 const char kInvalidUuid[] = "Invalid UUID"; |
53 const char kServiceDiscoveryFailed[] = "Service discovery failed"; | 55 const char kServiceDiscoveryFailed[] = "Service discovery failed"; |
54 const char kSocketNotFoundError[] = "Socket not found: invalid socket id"; | 56 const char kSocketNotFoundError[] = "Socket not found: invalid socket id"; |
55 const char kStartDiscoveryFailed[] = | 57 const char kStartDiscoveryFailed[] = |
56 "Starting discovery failed, or already discovering"; | 58 "Starting discovery failed, or already discovering"; |
57 const char kStopDiscoveryFailed[] = "Failed to stop discovery"; | 59 const char kStopDiscoveryFailed[] = "Failed to stop discovery"; |
58 | 60 |
59 } // namespace | 61 } // namespace |
60 | 62 |
61 namespace Connect = extensions::api::experimental_bluetooth::Connect; | 63 namespace Connect = extensions::api::experimental_bluetooth::Connect; |
62 namespace Disconnect = extensions::api::experimental_bluetooth::Disconnect; | 64 namespace Disconnect = extensions::api::experimental_bluetooth::Disconnect; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 SendResponse(true); | 106 SendResponse(true); |
105 } | 107 } |
106 | 108 |
107 bool BluetoothGetDevicesFunction::RunImpl() { | 109 bool BluetoothGetDevicesFunction::RunImpl() { |
108 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 110 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
109 | 111 |
110 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_)); | 112 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_)); |
111 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 113 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
112 const experimental_bluetooth::GetDevicesOptions& options = params->options; | 114 const experimental_bluetooth::GetDevicesOptions& options = params->options; |
113 | 115 |
| 116 std::string uuid; |
| 117 if (options.uuid.get() != NULL) { |
| 118 uuid = chromeos::bluetooth_utils::CanonicalUuid(*options.uuid.get()); |
| 119 if (uuid.empty()) { |
| 120 SetError(kInvalidUuid); |
| 121 return false; |
| 122 } |
| 123 } |
| 124 |
114 ListValue* matches = new ListValue; | 125 ListValue* matches = new ListValue; |
115 SetResult(matches); | 126 SetResult(matches); |
116 | 127 |
117 CHECK_EQ(0, callbacks_pending_); | 128 CHECK_EQ(0, callbacks_pending_); |
118 | 129 |
119 chromeos::BluetoothAdapter::DeviceList devices = | 130 chromeos::BluetoothAdapter::DeviceList devices = |
120 GetMutableAdapter(profile())->GetDevices(); | 131 GetMutableAdapter(profile())->GetDevices(); |
121 for (chromeos::BluetoothAdapter::DeviceList::iterator i = devices.begin(); | 132 for (chromeos::BluetoothAdapter::DeviceList::iterator i = devices.begin(); |
122 i != devices.end(); ++i) { | 133 i != devices.end(); ++i) { |
123 chromeos::BluetoothDevice* device = *i; | 134 chromeos::BluetoothDevice* device = *i; |
124 | 135 |
125 if (options.uuid.get() != NULL && | 136 if (!uuid.empty() && !(device->ProvidesServiceWithUUID(uuid))) |
126 !(device->ProvidesServiceWithUUID(*(options.uuid)))) | |
127 continue; | 137 continue; |
128 | 138 |
129 if (options.name.get() == NULL) { | 139 if (options.name.get() == NULL) { |
130 matches->Append(experimental_bluetooth::BluetoothDeviceToValue(*device)); | 140 matches->Append(experimental_bluetooth::BluetoothDeviceToValue(*device)); |
131 continue; | 141 continue; |
132 } | 142 } |
133 | 143 |
134 callbacks_pending_++; | 144 callbacks_pending_++; |
135 device->ProvidesServiceWithName( | 145 device->ProvidesServiceWithName( |
136 *(options.name), | 146 *(options.name), |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 } | 182 } |
173 | 183 |
174 bool BluetoothGetServicesFunction::RunImpl() { | 184 bool BluetoothGetServicesFunction::RunImpl() { |
175 scoped_ptr<GetServices::Params> params(GetServices::Params::Create(*args_)); | 185 scoped_ptr<GetServices::Params> params(GetServices::Params::Create(*args_)); |
176 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 186 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
177 const experimental_bluetooth::GetServicesOptions& options = params->options; | 187 const experimental_bluetooth::GetServicesOptions& options = params->options; |
178 | 188 |
179 chromeos::BluetoothDevice* device = | 189 chromeos::BluetoothDevice* device = |
180 GetMutableAdapter(profile())->GetDevice(options.device_address); | 190 GetMutableAdapter(profile())->GetDevice(options.device_address); |
181 if (!device) { | 191 if (!device) { |
182 SendResponse(false); | |
183 SetError(kInvalidDevice); | 192 SetError(kInvalidDevice); |
184 return false; | 193 return false; |
185 } | 194 } |
186 | 195 |
187 ListValue* services = new ListValue; | 196 ListValue* services = new ListValue; |
188 SetResult(services); | 197 SetResult(services); |
189 | 198 |
190 device->GetServiceRecords( | 199 device->GetServiceRecords( |
191 base::Bind(&BluetoothGetServicesFunction::GetServiceRecordsCallback, | 200 base::Bind(&BluetoothGetServicesFunction::GetServiceRecordsCallback, |
192 this, | 201 this, |
(...skipping 22 matching lines...) Expand all Loading... |
215 SetError(kFailedToConnect); | 224 SetError(kFailedToConnect); |
216 SendResponse(false); | 225 SendResponse(false); |
217 } | 226 } |
218 } | 227 } |
219 | 228 |
220 bool BluetoothConnectFunction::RunImpl() { | 229 bool BluetoothConnectFunction::RunImpl() { |
221 scoped_ptr<Connect::Params> params(Connect::Params::Create(*args_)); | 230 scoped_ptr<Connect::Params> params(Connect::Params::Create(*args_)); |
222 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 231 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
223 const experimental_bluetooth::ConnectOptions& options = params->options; | 232 const experimental_bluetooth::ConnectOptions& options = params->options; |
224 | 233 |
| 234 std::string uuid = chromeos::bluetooth_utils::CanonicalUuid( |
| 235 options.service_uuid); |
| 236 if (uuid.empty()) { |
| 237 SetError(kInvalidUuid); |
| 238 return false; |
| 239 } |
| 240 |
225 chromeos::BluetoothDevice* device = | 241 chromeos::BluetoothDevice* device = |
226 GetMutableAdapter(profile())->GetDevice(options.device_address); | 242 GetMutableAdapter(profile())->GetDevice(options.device_address); |
227 if (!device) { | 243 if (!device) { |
228 SendResponse(false); | |
229 SetError(kInvalidDevice); | 244 SetError(kInvalidDevice); |
230 return false; | 245 return false; |
231 } | 246 } |
232 | 247 |
233 device->ConnectToService(options.service_uuid, | 248 device->ConnectToService(uuid, |
234 base::Bind(&BluetoothConnectFunction::ConnectToServiceCallback, | 249 base::Bind(&BluetoothConnectFunction::ConnectToServiceCallback, |
235 this, | 250 this, |
236 device, | 251 device, |
237 options.service_uuid)); | 252 uuid)); |
238 return true; | 253 return true; |
239 } | 254 } |
240 | 255 |
241 bool BluetoothDisconnectFunction::RunImpl() { | 256 bool BluetoothDisconnectFunction::RunImpl() { |
242 scoped_ptr<Disconnect::Params> params(Disconnect::Params::Create(*args_)); | 257 scoped_ptr<Disconnect::Params> params(Disconnect::Params::Create(*args_)); |
243 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 258 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
244 const experimental_bluetooth::DisconnectOptions& options = params->options; | 259 const experimental_bluetooth::DisconnectOptions& options = params->options; |
245 return GetEventRouter(profile())->ReleaseSocket(options.socket_id); | 260 return GetEventRouter(profile())->ReleaseSocket(options.socket_id); |
246 } | 261 } |
247 | 262 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
360 // TODO(bryeung): update to new-style parameter passing when ArrayBuffer | 375 // TODO(bryeung): update to new-style parameter passing when ArrayBuffer |
361 // support is added | 376 // support is added |
362 DictionaryValue* options; | 377 DictionaryValue* options; |
363 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options)); | 378 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options)); |
364 std::string address; | 379 std::string address; |
365 EXTENSION_FUNCTION_VALIDATE(options->GetString("deviceAddress", &address)); | 380 EXTENSION_FUNCTION_VALIDATE(options->GetString("deviceAddress", &address)); |
366 | 381 |
367 chromeos::BluetoothDevice* device = | 382 chromeos::BluetoothDevice* device = |
368 GetMutableAdapter(profile())->GetDevice(address); | 383 GetMutableAdapter(profile())->GetDevice(address); |
369 if (!device) { | 384 if (!device) { |
370 SendResponse(false); | |
371 SetError(kInvalidDevice); | 385 SetError(kInvalidDevice); |
372 return false; | 386 return false; |
373 } | 387 } |
374 | 388 |
375 if (options->HasKey("data")) { | 389 if (options->HasKey("data")) { |
376 DictionaryValue* data_in; | 390 DictionaryValue* data_in; |
377 EXTENSION_FUNCTION_VALIDATE(options->GetDictionary("data", &data_in)); | 391 EXTENSION_FUNCTION_VALIDATE(options->GetDictionary("data", &data_in)); |
378 | 392 |
379 chromeos::BluetoothOutOfBandPairingData data_out; | 393 chromeos::BluetoothOutOfBandPairingData data_out; |
380 | 394 |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
573 #endif | 587 #endif |
574 | 588 |
575 BluetoothReadFunction::BluetoothReadFunction() {} | 589 BluetoothReadFunction::BluetoothReadFunction() {} |
576 BluetoothReadFunction::~BluetoothReadFunction() {} | 590 BluetoothReadFunction::~BluetoothReadFunction() {} |
577 | 591 |
578 BluetoothWriteFunction::BluetoothWriteFunction() {} | 592 BluetoothWriteFunction::BluetoothWriteFunction() {} |
579 BluetoothWriteFunction::~BluetoothWriteFunction() {} | 593 BluetoothWriteFunction::~BluetoothWriteFunction() {} |
580 | 594 |
581 } // namespace api | 595 } // namespace api |
582 } // namespace extensions | 596 } // namespace extensions |
OLD | NEW |