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 |
(...skipping 25 matching lines...) Expand all Loading... | |
36 using device::BluetoothDevice; | 36 using device::BluetoothDevice; |
37 using device::BluetoothServiceRecord; | 37 using device::BluetoothServiceRecord; |
38 using device::BluetoothSocket; | 38 using device::BluetoothSocket; |
39 | 39 |
40 namespace { | 40 namespace { |
41 | 41 |
42 extensions::ExtensionBluetoothEventRouter* GetEventRouter(Profile* profile) { | 42 extensions::ExtensionBluetoothEventRouter* GetEventRouter(Profile* profile) { |
43 return extensions::BluetoothAPI::Get(profile)->bluetooth_event_router(); | 43 return extensions::BluetoothAPI::Get(profile)->bluetooth_event_router(); |
44 } | 44 } |
45 | 45 |
46 const BluetoothAdapter* GetAdapter(Profile* profile) { | |
47 return GetEventRouter(profile)->adapter(); | |
48 } | |
49 | |
50 BluetoothAdapter* GetMutableAdapter(Profile* profile) { | |
51 return GetEventRouter(profile)->GetMutableAdapter(); | |
52 } | |
53 | |
54 bool IsBluetoothSupported(Profile* profile) { | |
55 return GetAdapter(profile) != NULL; | |
56 } | |
57 | |
58 } // namespace | 46 } // namespace |
59 | 47 |
60 namespace { | 48 namespace { |
61 | 49 |
62 const char kCouldNotGetLocalOutOfBandPairingData[] = | 50 const char kCouldNotGetLocalOutOfBandPairingData[] = |
63 "Could not get local Out Of Band Pairing Data"; | 51 "Could not get local Out Of Band Pairing Data"; |
64 const char kCouldNotSetOutOfBandPairingData[] = | 52 const char kCouldNotSetOutOfBandPairingData[] = |
65 "Could not set Out Of Band Pairing Data"; | 53 "Could not set Out Of Band Pairing Data"; |
66 const char kDevicePermissionDenied[] = "Permission to access device denied"; | 54 const char kDevicePermissionDenied[] = "Permission to access device denied"; |
67 const char kFailedToConnect[] = "Connection failed"; | 55 const char kFailedToConnect[] = "Connection failed"; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
114 void BluetoothAPI::OnListenerAdded(const EventListenerInfo& details) { | 102 void BluetoothAPI::OnListenerAdded(const EventListenerInfo& details) { |
115 bluetooth_event_router()->OnListenerAdded(); | 103 bluetooth_event_router()->OnListenerAdded(); |
116 } | 104 } |
117 | 105 |
118 void BluetoothAPI::OnListenerRemoved(const EventListenerInfo& details) { | 106 void BluetoothAPI::OnListenerRemoved(const EventListenerInfo& details) { |
119 bluetooth_event_router()->OnListenerRemoved(); | 107 bluetooth_event_router()->OnListenerRemoved(); |
120 } | 108 } |
121 | 109 |
122 namespace api { | 110 namespace api { |
123 | 111 |
124 bool BluetoothGetAdapterStateFunction::RunImpl() { | 112 void BluetoothGetAdapterStateFunction::DoWork( |
125 if (!IsBluetoothSupported(profile())) { | 113 scoped_refptr<BluetoothAdapter> adapter) { |
126 SetError(kPlatformNotSupported); | |
127 return false; | |
128 } | |
129 | |
130 bluetooth::AdapterState state; | 114 bluetooth::AdapterState state; |
131 PopulateAdapterState(*GetAdapter(profile()), &state); | 115 PopulateAdapterState(*adapter, &state); |
132 SetResult(state.ToValue().release()); | 116 SetResult(state.ToValue().release()); |
133 return true; | 117 SendResponse(true); |
134 } | 118 } |
135 | 119 |
136 BluetoothGetDevicesFunction::BluetoothGetDevicesFunction() | 120 BluetoothGetDevicesFunction::BluetoothGetDevicesFunction() |
137 : callbacks_pending_(0), | 121 : callbacks_pending_(0), |
138 device_events_sent_(0) {} | 122 device_events_sent_(0) {} |
139 | 123 |
140 void BluetoothGetDevicesFunction::DispatchDeviceSearchResult( | 124 void BluetoothGetDevicesFunction::DispatchDeviceSearchResult( |
141 const BluetoothDevice& device) { | 125 const BluetoothDevice& device) { |
142 bluetooth::Device extension_device; | 126 bluetooth::Device extension_device; |
143 bluetooth::BluetoothDeviceToApiDevice(device, &extension_device); | 127 bluetooth::BluetoothDeviceToApiDevice(device, &extension_device); |
(...skipping 24 matching lines...) Expand all Loading... | |
168 args->Append(info.release()); | 152 args->Append(info.release()); |
169 | 153 |
170 scoped_ptr<extensions::Event> event(new extensions::Event( | 154 scoped_ptr<extensions::Event> event(new extensions::Event( |
171 extensions::event_names::kBluetoothOnDeviceSearchFinished, args.Pass())); | 155 extensions::event_names::kBluetoothOnDeviceSearchFinished, args.Pass())); |
172 extensions::ExtensionSystem::Get(profile())->event_router()-> | 156 extensions::ExtensionSystem::Get(profile())->event_router()-> |
173 BroadcastEvent(event.Pass()); | 157 BroadcastEvent(event.Pass()); |
174 | 158 |
175 SendResponse(true); | 159 SendResponse(true); |
176 } | 160 } |
177 | 161 |
178 bool BluetoothGetDevicesFunction::RunImpl() { | 162 void BluetoothGetDevicesFunction::DoWork( |
179 if (!IsBluetoothSupported(profile())) { | 163 scoped_refptr<BluetoothAdapter> adapter) { |
180 SetError(kPlatformNotSupported); | |
181 return false; | |
182 } | |
183 | |
184 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 164 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
185 | 165 |
186 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_)); | 166 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_)); |
187 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 167 if (!params) { |
168 bad_message_ = true; | |
169 SendResponse(false); | |
170 return; | |
171 } | |
asargent_no_longer_on_chrome
2013/01/04 21:54:22
Instead of manually putting in the code to essenti
youngki
2013/01/04 23:44:44
Done.
| |
188 const bluetooth::GetDevicesOptions& options = params->options; | 172 const bluetooth::GetDevicesOptions& options = params->options; |
189 | 173 |
190 std::string uuid; | 174 std::string uuid; |
191 if (options.uuid.get() != NULL) { | 175 if (options.uuid.get() != NULL) { |
192 uuid = device::bluetooth_utils::CanonicalUuid(*options.uuid.get()); | 176 uuid = device::bluetooth_utils::CanonicalUuid(*options.uuid.get()); |
193 if (uuid.empty()) { | 177 if (uuid.empty()) { |
194 SetError(kInvalidUuid); | 178 SetError(kInvalidUuid); |
195 return false; | 179 SendResponse(false); |
180 return; | |
196 } | 181 } |
197 } | 182 } |
198 | 183 |
199 CHECK_EQ(0, callbacks_pending_); | 184 CHECK_EQ(0, callbacks_pending_); |
200 | 185 |
201 BluetoothAdapter::DeviceList devices = | 186 BluetoothAdapter::DeviceList devices = adapter->GetDevices(); |
202 GetMutableAdapter(profile())->GetDevices(); | |
203 for (BluetoothAdapter::DeviceList::iterator i = devices.begin(); | 187 for (BluetoothAdapter::DeviceList::iterator i = devices.begin(); |
204 i != devices.end(); ++i) { | 188 i != devices.end(); ++i) { |
205 BluetoothDevice* device = *i; | 189 BluetoothDevice* device = *i; |
206 CHECK(device); | 190 CHECK(device); |
207 | 191 |
208 if (!uuid.empty() && !(device->ProvidesServiceWithUUID(uuid))) | 192 if (!uuid.empty() && !(device->ProvidesServiceWithUUID(uuid))) |
209 continue; | 193 continue; |
210 | 194 |
211 if (options.name.get() == NULL) { | 195 if (options.name.get() == NULL) { |
212 DispatchDeviceSearchResult(*device); | 196 DispatchDeviceSearchResult(*device); |
213 continue; | 197 continue; |
214 } | 198 } |
215 | 199 |
216 callbacks_pending_++; | 200 callbacks_pending_++; |
217 device->ProvidesServiceWithName( | 201 device->ProvidesServiceWithName( |
218 *(options.name), | 202 *(options.name), |
219 base::Bind(&BluetoothGetDevicesFunction::ProvidesServiceCallback, | 203 base::Bind(&BluetoothGetDevicesFunction::ProvidesServiceCallback, |
220 this, | 204 this, |
221 device)); | 205 device)); |
222 } | 206 } |
223 callbacks_pending_--; | 207 callbacks_pending_--; |
224 | 208 |
225 // The count is checked for -1 because of the extra decrement after the | 209 // The count is checked for -1 because of the extra decrement after the |
226 // for-loop, which ensures that all requests have been made before | 210 // for-loop, which ensures that all requests have been made before |
227 // SendResponse happens. | 211 // SendResponse happens. |
228 if (callbacks_pending_ == -1) | 212 if (callbacks_pending_ == -1) |
229 FinishDeviceSearch(); | 213 FinishDeviceSearch(); |
230 | |
231 return true; | |
232 } | 214 } |
233 | 215 |
234 void BluetoothGetServicesFunction::GetServiceRecordsCallback( | 216 void BluetoothGetServicesFunction::GetServiceRecordsCallback( |
235 base::ListValue* services, | 217 base::ListValue* services, |
236 const BluetoothDevice::ServiceRecordList& records) { | 218 const BluetoothDevice::ServiceRecordList& records) { |
237 for (BluetoothDevice::ServiceRecordList::const_iterator i = records.begin(); | 219 for (BluetoothDevice::ServiceRecordList::const_iterator i = records.begin(); |
238 i != records.end(); ++i) { | 220 i != records.end(); ++i) { |
239 const BluetoothServiceRecord& record = **i; | 221 const BluetoothServiceRecord& record = **i; |
240 bluetooth::ServiceRecord api_record; | 222 bluetooth::ServiceRecord api_record; |
241 api_record.name = record.name(); | 223 api_record.name = record.name(); |
242 if (!record.uuid().empty()) | 224 if (!record.uuid().empty()) |
243 api_record.uuid.reset(new std::string(record.uuid())); | 225 api_record.uuid.reset(new std::string(record.uuid())); |
244 services->Append(api_record.ToValue().release()); | 226 services->Append(api_record.ToValue().release()); |
245 } | 227 } |
246 | 228 |
247 SendResponse(true); | 229 SendResponse(true); |
248 } | 230 } |
249 | 231 |
250 void BluetoothGetServicesFunction::OnErrorCallback() { | 232 void BluetoothGetServicesFunction::OnErrorCallback() { |
251 SetError(kServiceDiscoveryFailed); | 233 SetError(kServiceDiscoveryFailed); |
252 SendResponse(false); | 234 SendResponse(false); |
253 } | 235 } |
254 | 236 |
255 bool BluetoothGetServicesFunction::RunImpl() { | 237 void BluetoothGetServicesFunction::DoWork( |
256 if (!IsBluetoothSupported(profile())) { | 238 scoped_refptr<BluetoothAdapter> adapter) { |
257 SetError(kPlatformNotSupported); | 239 scoped_ptr<GetServices::Params> params(GetServices::Params::Create(*args_)); |
258 return false; | 240 if (!params) { |
241 bad_message_ = true; | |
242 SendResponse(false); | |
243 return; | |
259 } | 244 } |
260 | |
261 scoped_ptr<GetServices::Params> params(GetServices::Params::Create(*args_)); | |
262 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | |
263 const bluetooth::GetServicesOptions& options = params->options; | 245 const bluetooth::GetServicesOptions& options = params->options; |
264 | 246 |
265 BluetoothDevice* device = | 247 BluetoothDevice* device = adapter->GetDevice(options.device_address); |
266 GetMutableAdapter(profile())->GetDevice(options.device_address); | |
267 if (!device) { | 248 if (!device) { |
268 SetError(kInvalidDevice); | 249 SetError(kInvalidDevice); |
269 return false; | 250 SendResponse(false); |
251 return; | |
270 } | 252 } |
271 | 253 |
272 ListValue* services = new ListValue; | 254 ListValue* services = new ListValue; |
273 SetResult(services); | 255 SetResult(services); |
274 | 256 |
275 device->GetServiceRecords( | 257 device->GetServiceRecords( |
276 base::Bind(&BluetoothGetServicesFunction::GetServiceRecordsCallback, | 258 base::Bind(&BluetoothGetServicesFunction::GetServiceRecordsCallback, |
277 this, | 259 this, |
278 services), | 260 services), |
279 base::Bind(&BluetoothGetServicesFunction::OnErrorCallback, | 261 base::Bind(&BluetoothGetServicesFunction::OnErrorCallback, |
280 this)); | 262 this)); |
281 | |
282 return true; | |
283 } | 263 } |
284 | 264 |
285 void BluetoothConnectFunction::ConnectToServiceCallback( | 265 void BluetoothConnectFunction::ConnectToServiceCallback( |
286 const BluetoothDevice* device, | 266 const BluetoothDevice* device, |
287 const std::string& service_uuid, | 267 const std::string& service_uuid, |
288 scoped_refptr<BluetoothSocket> socket) { | 268 scoped_refptr<BluetoothSocket> socket) { |
289 if (socket.get()) { | 269 if (socket.get()) { |
290 int socket_id = GetEventRouter(profile())->RegisterSocket(socket); | 270 int socket_id = GetEventRouter(profile())->RegisterSocket(socket); |
291 | 271 |
292 bluetooth::Socket result_socket; | 272 bluetooth::Socket result_socket; |
293 bluetooth::BluetoothDeviceToApiDevice(*device, &result_socket.device); | 273 bluetooth::BluetoothDeviceToApiDevice(*device, &result_socket.device); |
294 result_socket.service_uuid = service_uuid; | 274 result_socket.service_uuid = service_uuid; |
295 result_socket.id = socket_id; | 275 result_socket.id = socket_id; |
296 SetResult(result_socket.ToValue().release()); | 276 SetResult(result_socket.ToValue().release()); |
297 SendResponse(true); | 277 SendResponse(true); |
298 } else { | 278 } else { |
299 SetError(kFailedToConnect); | 279 SetError(kFailedToConnect); |
300 SendResponse(false); | 280 SendResponse(false); |
301 } | 281 } |
302 } | 282 } |
303 | 283 |
304 bool BluetoothConnectFunction::RunImpl() { | 284 void BluetoothConnectFunction::DoWork(scoped_refptr<BluetoothAdapter> adapter) { |
305 if (!IsBluetoothSupported(profile())) { | 285 scoped_ptr<Connect::Params> params(Connect::Params::Create(*args_)); |
306 SetError(kPlatformNotSupported); | 286 if (!params) { |
307 return false; | 287 bad_message_ = true; |
288 SendResponse(false); | |
289 return; | |
308 } | 290 } |
309 | |
310 scoped_ptr<Connect::Params> params(Connect::Params::Create(*args_)); | |
311 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | |
312 const bluetooth::ConnectOptions& options = params->options; | 291 const bluetooth::ConnectOptions& options = params->options; |
313 | 292 |
314 BluetoothDevicePermission::CheckParam param(options.device_address); | 293 BluetoothDevicePermission::CheckParam param(options.device_address); |
315 if (!GetExtension()->CheckAPIPermissionWithParam( | 294 if (!GetExtension()->CheckAPIPermissionWithParam( |
316 APIPermission::kBluetoothDevice, ¶m)) { | 295 APIPermission::kBluetoothDevice, ¶m)) { |
317 SetError(kDevicePermissionDenied); | 296 SetError(kDevicePermissionDenied); |
318 return false; | 297 SendResponse(false); |
298 return; | |
319 } | 299 } |
320 | 300 |
321 std::string uuid = device::bluetooth_utils::CanonicalUuid( | 301 std::string uuid = device::bluetooth_utils::CanonicalUuid( |
322 options.service_uuid); | 302 options.service_uuid); |
323 if (uuid.empty()) { | 303 if (uuid.empty()) { |
324 SetError(kInvalidUuid); | 304 SetError(kInvalidUuid); |
325 return false; | 305 SendResponse(false); |
306 return; | |
326 } | 307 } |
327 | 308 |
328 BluetoothDevice* device = | 309 BluetoothDevice* device = adapter->GetDevice(options.device_address); |
329 GetMutableAdapter(profile())->GetDevice(options.device_address); | |
330 if (!device) { | 310 if (!device) { |
331 SetError(kInvalidDevice); | 311 SetError(kInvalidDevice); |
332 return false; | 312 SendResponse(false); |
313 return; | |
333 } | 314 } |
334 | 315 |
335 device->ConnectToService(uuid, | 316 device->ConnectToService(uuid, |
336 base::Bind(&BluetoothConnectFunction::ConnectToServiceCallback, | 317 base::Bind(&BluetoothConnectFunction::ConnectToServiceCallback, |
337 this, | 318 this, |
338 device, | 319 device, |
339 uuid)); | 320 uuid)); |
340 return true; | |
341 } | 321 } |
342 | 322 |
343 bool BluetoothDisconnectFunction::RunImpl() { | 323 bool BluetoothDisconnectFunction::RunImpl() { |
344 scoped_ptr<Disconnect::Params> params(Disconnect::Params::Create(*args_)); | 324 scoped_ptr<Disconnect::Params> params(Disconnect::Params::Create(*args_)); |
345 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 325 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
346 const bluetooth::DisconnectOptions& options = params->options; | 326 const bluetooth::DisconnectOptions& options = params->options; |
347 return GetEventRouter(profile())->ReleaseSocket(options.socket_id); | 327 return GetEventRouter(profile())->ReleaseSocket(options.socket_id); |
348 } | 328 } |
349 | 329 |
350 BluetoothReadFunction::BluetoothReadFunction() : success_(false) {} | 330 BluetoothReadFunction::BluetoothReadFunction() : success_(false) {} |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
467 | 447 |
468 void BluetoothSetOutOfBandPairingDataFunction::OnSuccessCallback() { | 448 void BluetoothSetOutOfBandPairingDataFunction::OnSuccessCallback() { |
469 SendResponse(true); | 449 SendResponse(true); |
470 } | 450 } |
471 | 451 |
472 void BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback() { | 452 void BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback() { |
473 SetError(kCouldNotSetOutOfBandPairingData); | 453 SetError(kCouldNotSetOutOfBandPairingData); |
474 SendResponse(false); | 454 SendResponse(false); |
475 } | 455 } |
476 | 456 |
477 bool BluetoothSetOutOfBandPairingDataFunction::RunImpl() { | 457 void BluetoothSetOutOfBandPairingDataFunction::DoWork( |
478 if (!IsBluetoothSupported(profile())) { | 458 scoped_refptr<BluetoothAdapter> adapter) { |
479 SetError(kPlatformNotSupported); | |
480 return false; | |
481 } | |
482 | |
483 // TODO(bryeung): update to new-style parameter passing when ArrayBuffer | 459 // TODO(bryeung): update to new-style parameter passing when ArrayBuffer |
484 // support is added | 460 // support is added |
485 DictionaryValue* options; | 461 DictionaryValue* options; |
486 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options)); | |
487 std::string address; | 462 std::string address; |
488 EXTENSION_FUNCTION_VALIDATE(options->GetString("deviceAddress", &address)); | 463 if (!args_->GetDictionary(0, &options) || |
464 !options->GetString("deviceAddress", &address)) { | |
465 bad_message_ = true; | |
466 SendResponse(false); | |
467 return; | |
468 } | |
489 | 469 |
490 BluetoothDevice* device = GetMutableAdapter(profile())->GetDevice(address); | 470 BluetoothDevice* device = adapter->GetDevice(address); |
491 if (!device) { | 471 if (!device) { |
492 SetError(kInvalidDevice); | 472 SetError(kInvalidDevice); |
493 return false; | 473 SendResponse(false); |
474 return; | |
494 } | 475 } |
495 | 476 |
496 if (options->HasKey("data")) { | 477 if (options->HasKey("data")) { |
497 DictionaryValue* data_in; | 478 DictionaryValue* data_in; |
498 EXTENSION_FUNCTION_VALIDATE(options->GetDictionary("data", &data_in)); | 479 device::BluetoothOutOfBandPairingData data_out; |
480 base::BinaryValue* tmp_data; | |
499 | 481 |
500 device::BluetoothOutOfBandPairingData data_out; | 482 if (!options->GetDictionary("data", &data_in) || |
483 !data_in->GetBinary("hash", &tmp_data) || | |
484 tmp_data->GetSize() != device::kBluetoothOutOfBandPairingDataSize) { | |
485 bad_message_ = true; | |
486 SendResponse(false); | |
487 return; | |
488 } | |
501 | 489 |
502 base::BinaryValue* tmp_data; | |
503 EXTENSION_FUNCTION_VALIDATE(data_in->GetBinary("hash", &tmp_data)); | |
504 EXTENSION_FUNCTION_VALIDATE( | |
505 tmp_data->GetSize() == device::kBluetoothOutOfBandPairingDataSize); | |
506 memcpy(data_out.hash, | 490 memcpy(data_out.hash, |
507 reinterpret_cast<uint8_t*>(tmp_data->GetBuffer()), | 491 reinterpret_cast<uint8_t*>(tmp_data->GetBuffer()), |
508 device::kBluetoothOutOfBandPairingDataSize); | 492 device::kBluetoothOutOfBandPairingDataSize); |
509 | 493 |
510 EXTENSION_FUNCTION_VALIDATE(data_in->GetBinary("randomizer", &tmp_data)); | 494 if (!data_in->GetBinary("randomizer", &tmp_data) || |
511 EXTENSION_FUNCTION_VALIDATE( | 495 tmp_data->GetSize() != device::kBluetoothOutOfBandPairingDataSize) { |
512 tmp_data->GetSize() == device::kBluetoothOutOfBandPairingDataSize); | 496 bad_message_ = true; |
497 SendResponse(false); | |
498 return; | |
499 } | |
513 memcpy(data_out.randomizer, | 500 memcpy(data_out.randomizer, |
514 reinterpret_cast<uint8_t*>(tmp_data->GetBuffer()), | 501 reinterpret_cast<uint8_t*>(tmp_data->GetBuffer()), |
515 device::kBluetoothOutOfBandPairingDataSize); | 502 device::kBluetoothOutOfBandPairingDataSize); |
516 | 503 |
517 device->SetOutOfBandPairingData( | 504 device->SetOutOfBandPairingData( |
518 data_out, | 505 data_out, |
519 base::Bind(&BluetoothSetOutOfBandPairingDataFunction::OnSuccessCallback, | 506 base::Bind(&BluetoothSetOutOfBandPairingDataFunction::OnSuccessCallback, |
520 this), | 507 this), |
521 base::Bind(&BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback, | 508 base::Bind(&BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback, |
522 this)); | 509 this)); |
523 } else { | 510 } else { |
524 device->ClearOutOfBandPairingData( | 511 device->ClearOutOfBandPairingData( |
525 base::Bind(&BluetoothSetOutOfBandPairingDataFunction::OnSuccessCallback, | 512 base::Bind(&BluetoothSetOutOfBandPairingDataFunction::OnSuccessCallback, |
526 this), | 513 this), |
527 base::Bind(&BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback, | 514 base::Bind(&BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback, |
528 this)); | 515 this)); |
529 } | 516 } |
530 | |
531 return true; | |
532 } | 517 } |
533 | 518 |
534 void BluetoothGetLocalOutOfBandPairingDataFunction::ReadCallback( | 519 void BluetoothGetLocalOutOfBandPairingDataFunction::ReadCallback( |
535 const device::BluetoothOutOfBandPairingData& data) { | 520 const device::BluetoothOutOfBandPairingData& data) { |
536 base::BinaryValue* hash = base::BinaryValue::CreateWithCopiedBuffer( | 521 base::BinaryValue* hash = base::BinaryValue::CreateWithCopiedBuffer( |
537 reinterpret_cast<const char*>(data.hash), | 522 reinterpret_cast<const char*>(data.hash), |
538 device::kBluetoothOutOfBandPairingDataSize); | 523 device::kBluetoothOutOfBandPairingDataSize); |
539 base::BinaryValue* randomizer = base::BinaryValue::CreateWithCopiedBuffer( | 524 base::BinaryValue* randomizer = base::BinaryValue::CreateWithCopiedBuffer( |
540 reinterpret_cast<const char*>(data.randomizer), | 525 reinterpret_cast<const char*>(data.randomizer), |
541 device::kBluetoothOutOfBandPairingDataSize); | 526 device::kBluetoothOutOfBandPairingDataSize); |
542 | 527 |
543 // TODO(bryeung): convert to bluetooth::OutOfBandPairingData | 528 // TODO(bryeung): convert to bluetooth::OutOfBandPairingData |
544 // when ArrayBuffer support within objects is completed. | 529 // when ArrayBuffer support within objects is completed. |
545 DictionaryValue* result = new DictionaryValue(); | 530 DictionaryValue* result = new DictionaryValue(); |
546 result->Set("hash", hash); | 531 result->Set("hash", hash); |
547 result->Set("randomizer", randomizer); | 532 result->Set("randomizer", randomizer); |
548 | 533 |
549 SetResult(result); | 534 SetResult(result); |
550 | 535 |
551 SendResponse(true); | 536 SendResponse(true); |
552 } | 537 } |
553 | 538 |
554 void BluetoothGetLocalOutOfBandPairingDataFunction::ErrorCallback() { | 539 void BluetoothGetLocalOutOfBandPairingDataFunction::ErrorCallback() { |
555 SetError(kCouldNotGetLocalOutOfBandPairingData); | 540 SetError(kCouldNotGetLocalOutOfBandPairingData); |
556 SendResponse(false); | 541 SendResponse(false); |
557 } | 542 } |
558 | 543 |
559 bool BluetoothGetLocalOutOfBandPairingDataFunction::RunImpl() { | 544 void BluetoothGetLocalOutOfBandPairingDataFunction::DoWork( |
560 if (!IsBluetoothSupported(profile())) { | 545 scoped_refptr<BluetoothAdapter> adapter) { |
561 SetError(kPlatformNotSupported); | 546 adapter->ReadLocalOutOfBandPairingData( |
562 return false; | |
563 } | |
564 | |
565 GetMutableAdapter(profile())->ReadLocalOutOfBandPairingData( | |
566 base::Bind(&BluetoothGetLocalOutOfBandPairingDataFunction::ReadCallback, | 547 base::Bind(&BluetoothGetLocalOutOfBandPairingDataFunction::ReadCallback, |
567 this), | 548 this), |
568 base::Bind(&BluetoothGetLocalOutOfBandPairingDataFunction::ErrorCallback, | 549 base::Bind(&BluetoothGetLocalOutOfBandPairingDataFunction::ErrorCallback, |
569 this)); | 550 this)); |
570 return true; | |
571 } | 551 } |
572 | 552 |
573 void BluetoothStartDiscoveryFunction::OnSuccessCallback() { | 553 void BluetoothStartDiscoveryFunction::OnSuccessCallback() { |
574 GetEventRouter(profile())->SetResponsibleForDiscovery(true); | 554 GetEventRouter(profile())->SetResponsibleForDiscovery(true); |
575 SendResponse(true); | 555 SendResponse(true); |
576 } | 556 } |
577 | 557 |
578 void BluetoothStartDiscoveryFunction::OnErrorCallback() { | 558 void BluetoothStartDiscoveryFunction::OnErrorCallback() { |
579 SetError(kStartDiscoveryFailed); | 559 SetError(kStartDiscoveryFailed); |
580 SendResponse(false); | 560 SendResponse(false); |
581 } | 561 } |
582 | 562 |
583 bool BluetoothStartDiscoveryFunction::RunImpl() { | 563 void BluetoothStartDiscoveryFunction::DoWork( |
584 if (!IsBluetoothSupported(profile())) { | 564 scoped_refptr<BluetoothAdapter> adapter) { |
585 SetError(kPlatformNotSupported); | |
586 return false; | |
587 } | |
588 | |
589 GetEventRouter(profile())->SetSendDiscoveryEvents(true); | 565 GetEventRouter(profile())->SetSendDiscoveryEvents(true); |
590 | 566 |
591 // If the adapter is already discovering, there is nothing else to do. | 567 // If the adapter is already discovering, there is nothing else to do. |
592 if (GetAdapter(profile())->IsDiscovering()) { | 568 if (adapter->IsDiscovering()) { |
593 SendResponse(true); | 569 SendResponse(true); |
594 return true; | 570 return; |
595 } | 571 } |
596 | 572 |
597 GetMutableAdapter(profile())->SetDiscovering(true, | 573 adapter->SetDiscovering(true, |
598 base::Bind(&BluetoothStartDiscoveryFunction::OnSuccessCallback, this), | 574 base::Bind(&BluetoothStartDiscoveryFunction::OnSuccessCallback, this), |
599 base::Bind(&BluetoothStartDiscoveryFunction::OnErrorCallback, this)); | 575 base::Bind(&BluetoothStartDiscoveryFunction::OnErrorCallback, this)); |
600 return true; | |
601 } | 576 } |
602 | 577 |
603 void BluetoothStopDiscoveryFunction::OnSuccessCallback() { | 578 void BluetoothStopDiscoveryFunction::OnSuccessCallback() { |
604 SendResponse(true); | 579 SendResponse(true); |
605 } | 580 } |
606 | 581 |
607 void BluetoothStopDiscoveryFunction::OnErrorCallback() { | 582 void BluetoothStopDiscoveryFunction::OnErrorCallback() { |
608 SetError(kStopDiscoveryFailed); | 583 SetError(kStopDiscoveryFailed); |
609 SendResponse(false); | 584 SendResponse(false); |
610 } | 585 } |
611 | 586 |
612 bool BluetoothStopDiscoveryFunction::RunImpl() { | 587 void BluetoothStopDiscoveryFunction::DoWork( |
613 if (!IsBluetoothSupported(profile())) { | 588 scoped_refptr<BluetoothAdapter> adapter) { |
614 SetError(kPlatformNotSupported); | |
615 return false; | |
616 } | |
617 | |
618 GetEventRouter(profile())->SetSendDiscoveryEvents(false); | 589 GetEventRouter(profile())->SetSendDiscoveryEvents(false); |
619 if (GetEventRouter(profile())->IsResponsibleForDiscovery()) { | 590 if (GetEventRouter(profile())->IsResponsibleForDiscovery()) { |
620 GetMutableAdapter(profile())->SetDiscovering(false, | 591 adapter->SetDiscovering(false, |
621 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), | 592 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), |
622 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); | 593 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); |
623 } | 594 } |
624 return true; | |
625 } | 595 } |
626 | 596 |
627 } // namespace api | 597 } // namespace api |
628 } // namespace extensions | 598 } // namespace extensions |
OLD | NEW |