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/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_factory.h" | 10 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_factory.h" |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 | 222 |
223 BluetoothDevice* device = adapter->GetDevice(options.device.address); | 223 BluetoothDevice* device = adapter->GetDevice(options.device.address); |
224 if (!device) { | 224 if (!device) { |
225 SetError(kInvalidDevice); | 225 SetError(kInvalidDevice); |
226 SendResponse(false); | 226 SendResponse(false); |
227 return false; | 227 return false; |
228 } | 228 } |
229 | 229 |
230 BluetoothDevice::ServiceList service_list = device->GetServices(); | 230 BluetoothDevice::ServiceList service_list = device->GetServices(); |
231 | 231 |
232 ListValue* profiles = new ListValue; | 232 base::ListValue* profiles = new base::ListValue; |
233 for (BluetoothDevice::ServiceList::const_iterator iter = service_list.begin(); | 233 for (BluetoothDevice::ServiceList::const_iterator iter = service_list.begin(); |
234 iter != service_list.end(); | 234 iter != service_list.end(); |
235 ++iter) { | 235 ++iter) { |
236 bluetooth::Profile api_profile; | 236 bluetooth::Profile api_profile; |
237 api_profile.uuid = *iter; | 237 api_profile.uuid = *iter; |
238 profiles->Append(api_profile.ToValue().release()); | 238 profiles->Append(api_profile.ToValue().release()); |
239 } | 239 } |
240 | 240 |
241 SetResult(profiles); | 241 SetResult(profiles); |
242 SendResponse(true); | 242 SendResponse(true); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
341 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 341 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
342 const bluetooth::GetServicesOptions& options = params->options; | 342 const bluetooth::GetServicesOptions& options = params->options; |
343 | 343 |
344 BluetoothDevice* device = adapter->GetDevice(options.device_address); | 344 BluetoothDevice* device = adapter->GetDevice(options.device_address); |
345 if (!device) { | 345 if (!device) { |
346 SetError(kInvalidDevice); | 346 SetError(kInvalidDevice); |
347 SendResponse(false); | 347 SendResponse(false); |
348 return false; | 348 return false; |
349 } | 349 } |
350 | 350 |
351 ListValue* services = new ListValue; | 351 base::ListValue* services = new base::ListValue; |
352 SetResult(services); | 352 SetResult(services); |
353 | 353 |
354 device->GetServiceRecords( | 354 device->GetServiceRecords( |
355 base::Bind(&BluetoothGetServicesFunction::GetServiceRecordsCallback, | 355 base::Bind(&BluetoothGetServicesFunction::GetServiceRecordsCallback, |
356 this, | 356 this, |
357 services), | 357 services), |
358 base::Bind(&BluetoothGetServicesFunction::OnErrorCallback, | 358 base::Bind(&BluetoothGetServicesFunction::OnErrorCallback, |
359 this)); | 359 this)); |
360 | 360 |
361 return true; | 361 return true; |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 BluetoothWriteFunction::BluetoothWriteFunction() | 462 BluetoothWriteFunction::BluetoothWriteFunction() |
463 : success_(false), | 463 : success_(false), |
464 data_to_write_(NULL) { | 464 data_to_write_(NULL) { |
465 } | 465 } |
466 | 466 |
467 BluetoothWriteFunction::~BluetoothWriteFunction() {} | 467 BluetoothWriteFunction::~BluetoothWriteFunction() {} |
468 | 468 |
469 bool BluetoothWriteFunction::Prepare() { | 469 bool BluetoothWriteFunction::Prepare() { |
470 // TODO(bryeung): update to new-style parameter passing when ArrayBuffer | 470 // TODO(bryeung): update to new-style parameter passing when ArrayBuffer |
471 // support is added | 471 // support is added |
472 DictionaryValue* options; | 472 base::DictionaryValue* options; |
473 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options)); | 473 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options)); |
474 | 474 |
475 DictionaryValue* socket; | 475 base::DictionaryValue* socket; |
476 EXTENSION_FUNCTION_VALIDATE(options->GetDictionary("socket", &socket)); | 476 EXTENSION_FUNCTION_VALIDATE(options->GetDictionary("socket", &socket)); |
477 | 477 |
478 int socket_id; | 478 int socket_id; |
479 EXTENSION_FUNCTION_VALIDATE(socket->GetInteger("id", &socket_id)); | 479 EXTENSION_FUNCTION_VALIDATE(socket->GetInteger("id", &socket_id)); |
480 | 480 |
481 socket_ = GetEventRouter(profile())->GetSocket(socket_id); | 481 socket_ = GetEventRouter(profile())->GetSocket(socket_id); |
482 if (socket_.get() == NULL) { | 482 if (socket_.get() == NULL) { |
483 SetError(kSocketNotFoundError); | 483 SetError(kSocketNotFoundError); |
484 return false; | 484 return false; |
485 } | 485 } |
(...skipping 13 matching lines...) Expand all Loading... |
499 return; | 499 return; |
500 | 500 |
501 scoped_refptr<net::WrappedIOBuffer> wrapped_io_buffer( | 501 scoped_refptr<net::WrappedIOBuffer> wrapped_io_buffer( |
502 new net::WrappedIOBuffer(data_to_write_->GetBuffer())); | 502 new net::WrappedIOBuffer(data_to_write_->GetBuffer())); |
503 scoped_refptr<net::DrainableIOBuffer> drainable_io_buffer( | 503 scoped_refptr<net::DrainableIOBuffer> drainable_io_buffer( |
504 new net::DrainableIOBuffer(wrapped_io_buffer.get(), | 504 new net::DrainableIOBuffer(wrapped_io_buffer.get(), |
505 data_to_write_->GetSize())); | 505 data_to_write_->GetSize())); |
506 success_ = socket_->Send(drainable_io_buffer.get()); | 506 success_ = socket_->Send(drainable_io_buffer.get()); |
507 if (success_) { | 507 if (success_) { |
508 if (drainable_io_buffer->BytesConsumed() > 0) | 508 if (drainable_io_buffer->BytesConsumed() > 0) |
509 SetResult( | 509 SetResult(base::Value::CreateIntegerValue( |
510 Value::CreateIntegerValue(drainable_io_buffer->BytesConsumed())); | 510 drainable_io_buffer->BytesConsumed())); |
511 else | 511 else |
512 results_.reset(); | 512 results_.reset(); |
513 } else { | 513 } else { |
514 SetError(socket_->GetLastErrorMessage()); | 514 SetError(socket_->GetLastErrorMessage()); |
515 } | 515 } |
516 } | 516 } |
517 | 517 |
518 bool BluetoothWriteFunction::Respond() { | 518 bool BluetoothWriteFunction::Respond() { |
519 return success_; | 519 return success_; |
520 } | 520 } |
521 | 521 |
522 void BluetoothSetOutOfBandPairingDataFunction::OnSuccessCallback() { | 522 void BluetoothSetOutOfBandPairingDataFunction::OnSuccessCallback() { |
523 SendResponse(true); | 523 SendResponse(true); |
524 } | 524 } |
525 | 525 |
526 void BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback() { | 526 void BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback() { |
527 SetError(kCouldNotSetOutOfBandPairingData); | 527 SetError(kCouldNotSetOutOfBandPairingData); |
528 SendResponse(false); | 528 SendResponse(false); |
529 } | 529 } |
530 | 530 |
531 bool BluetoothSetOutOfBandPairingDataFunction::DoWork( | 531 bool BluetoothSetOutOfBandPairingDataFunction::DoWork( |
532 scoped_refptr<BluetoothAdapter> adapter) { | 532 scoped_refptr<BluetoothAdapter> adapter) { |
533 // TODO(bryeung): update to new-style parameter passing when ArrayBuffer | 533 // TODO(bryeung): update to new-style parameter passing when ArrayBuffer |
534 // support is added | 534 // support is added |
535 DictionaryValue* options; | 535 base::DictionaryValue* options; |
536 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options)); | 536 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options)); |
537 std::string address; | 537 std::string address; |
538 EXTENSION_FUNCTION_VALIDATE(options->GetString("deviceAddress", &address)); | 538 EXTENSION_FUNCTION_VALIDATE(options->GetString("deviceAddress", &address)); |
539 | 539 |
540 BluetoothDevice* device = adapter->GetDevice(address); | 540 BluetoothDevice* device = adapter->GetDevice(address); |
541 if (!device) { | 541 if (!device) { |
542 SetError(kInvalidDevice); | 542 SetError(kInvalidDevice); |
543 SendResponse(false); | 543 SendResponse(false); |
544 return false; | 544 return false; |
545 } | 545 } |
546 | 546 |
547 if (options->HasKey("data")) { | 547 if (options->HasKey("data")) { |
548 DictionaryValue* data_in; | 548 base::DictionaryValue* data_in; |
549 EXTENSION_FUNCTION_VALIDATE(options->GetDictionary("data", &data_in)); | 549 EXTENSION_FUNCTION_VALIDATE(options->GetDictionary("data", &data_in)); |
550 | 550 |
551 device::BluetoothOutOfBandPairingData data_out; | 551 device::BluetoothOutOfBandPairingData data_out; |
552 | 552 |
553 base::BinaryValue* tmp_data; | 553 base::BinaryValue* tmp_data; |
554 EXTENSION_FUNCTION_VALIDATE(data_in->GetBinary("hash", &tmp_data)); | 554 EXTENSION_FUNCTION_VALIDATE(data_in->GetBinary("hash", &tmp_data)); |
555 EXTENSION_FUNCTION_VALIDATE( | 555 EXTENSION_FUNCTION_VALIDATE( |
556 tmp_data->GetSize() == device::kBluetoothOutOfBandPairingDataSize); | 556 tmp_data->GetSize() == device::kBluetoothOutOfBandPairingDataSize); |
557 memcpy(data_out.hash, | 557 memcpy(data_out.hash, |
558 reinterpret_cast<uint8_t*>(tmp_data->GetBuffer()), | 558 reinterpret_cast<uint8_t*>(tmp_data->GetBuffer()), |
(...skipping 27 matching lines...) Expand all Loading... |
586 const device::BluetoothOutOfBandPairingData& data) { | 586 const device::BluetoothOutOfBandPairingData& data) { |
587 base::BinaryValue* hash = base::BinaryValue::CreateWithCopiedBuffer( | 587 base::BinaryValue* hash = base::BinaryValue::CreateWithCopiedBuffer( |
588 reinterpret_cast<const char*>(data.hash), | 588 reinterpret_cast<const char*>(data.hash), |
589 device::kBluetoothOutOfBandPairingDataSize); | 589 device::kBluetoothOutOfBandPairingDataSize); |
590 base::BinaryValue* randomizer = base::BinaryValue::CreateWithCopiedBuffer( | 590 base::BinaryValue* randomizer = base::BinaryValue::CreateWithCopiedBuffer( |
591 reinterpret_cast<const char*>(data.randomizer), | 591 reinterpret_cast<const char*>(data.randomizer), |
592 device::kBluetoothOutOfBandPairingDataSize); | 592 device::kBluetoothOutOfBandPairingDataSize); |
593 | 593 |
594 // TODO(bryeung): convert to bluetooth::OutOfBandPairingData | 594 // TODO(bryeung): convert to bluetooth::OutOfBandPairingData |
595 // when ArrayBuffer support within objects is completed. | 595 // when ArrayBuffer support within objects is completed. |
596 DictionaryValue* result = new DictionaryValue(); | 596 base::DictionaryValue* result = new base::DictionaryValue(); |
597 result->Set("hash", hash); | 597 result->Set("hash", hash); |
598 result->Set("randomizer", randomizer); | 598 result->Set("randomizer", randomizer); |
599 | 599 |
600 SetResult(result); | 600 SetResult(result); |
601 | 601 |
602 SendResponse(true); | 602 SendResponse(true); |
603 } | 603 } |
604 | 604 |
605 void BluetoothGetLocalOutOfBandPairingDataFunction::ErrorCallback() { | 605 void BluetoothGetLocalOutOfBandPairingDataFunction::ErrorCallback() { |
606 SetError(kCouldNotGetLocalOutOfBandPairingData); | 606 SetError(kCouldNotGetLocalOutOfBandPairingData); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
665 adapter->StopDiscovering( | 665 adapter->StopDiscovering( |
666 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), | 666 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), |
667 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); | 667 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); |
668 } | 668 } |
669 | 669 |
670 return true; | 670 return true; |
671 } | 671 } |
672 | 672 |
673 } // namespace api | 673 } // namespace api |
674 } // namespace extensions | 674 } // namespace extensions |
OLD | NEW |