| 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/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
| 10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" | 12 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" |
| 13 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" | 13 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" |
| 14 #include "chrome/browser/profiles/profile_manager.h" | 14 #include "chrome/browser/profiles/profile_manager.h" |
| 15 #include "chrome/common/extensions/api/bluetooth.h" | 15 #include "chrome/common/extensions/api/bluetooth.h" |
| 16 #include "chrome/common/extensions/api/bluetooth/bluetooth_manifest_data.h" | 16 #include "chrome/common/extensions/api/bluetooth/bluetooth_manifest_data.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "device/bluetooth/bluetooth_adapter.h" | 18 #include "device/bluetooth/bluetooth_adapter.h" |
| 19 #include "device/bluetooth/bluetooth_device.h" | 19 #include "device/bluetooth/bluetooth_device.h" |
| 20 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" | |
| 21 #include "device/bluetooth/bluetooth_profile.h" | 20 #include "device/bluetooth/bluetooth_profile.h" |
| 22 #include "device/bluetooth/bluetooth_service_record.h" | 21 #include "device/bluetooth/bluetooth_service_record.h" |
| 23 #include "device/bluetooth/bluetooth_socket.h" | 22 #include "device/bluetooth/bluetooth_socket.h" |
| 24 #include "extensions/browser/event_router.h" | 23 #include "extensions/browser/event_router.h" |
| 25 #include "extensions/common/permissions/permissions_data.h" | 24 #include "extensions/common/permissions/permissions_data.h" |
| 26 #include "net/base/io_buffer.h" | 25 #include "net/base/io_buffer.h" |
| 27 | 26 |
| 28 using content::BrowserContext; | 27 using content::BrowserContext; |
| 29 using content::BrowserThread; | 28 using content::BrowserThread; |
| 30 | 29 |
| 31 using device::BluetoothAdapter; | 30 using device::BluetoothAdapter; |
| 32 using device::BluetoothDevice; | 31 using device::BluetoothDevice; |
| 33 using device::BluetoothProfile; | 32 using device::BluetoothProfile; |
| 34 using device::BluetoothServiceRecord; | 33 using device::BluetoothServiceRecord; |
| 35 using device::BluetoothSocket; | 34 using device::BluetoothSocket; |
| 36 | 35 |
| 37 using extensions::BluetoothApiSocket; | 36 using extensions::BluetoothApiSocket; |
| 38 | 37 |
| 39 namespace AddProfile = extensions::api::bluetooth::AddProfile; | 38 namespace AddProfile = extensions::api::bluetooth::AddProfile; |
| 40 namespace bluetooth = extensions::api::bluetooth; | 39 namespace bluetooth = extensions::api::bluetooth; |
| 41 namespace Connect = extensions::api::bluetooth::Connect; | 40 namespace Connect = extensions::api::bluetooth::Connect; |
| 42 namespace Disconnect = extensions::api::bluetooth::Disconnect; | 41 namespace Disconnect = extensions::api::bluetooth::Disconnect; |
| 43 namespace GetDevice = extensions::api::bluetooth::GetDevice; | 42 namespace GetDevice = extensions::api::bluetooth::GetDevice; |
| 44 namespace GetDevices = extensions::api::bluetooth::GetDevices; | 43 namespace GetDevices = extensions::api::bluetooth::GetDevices; |
| 45 namespace RemoveProfile = extensions::api::bluetooth::RemoveProfile; | 44 namespace RemoveProfile = extensions::api::bluetooth::RemoveProfile; |
| 46 namespace SetOutOfBandPairingData = | |
| 47 extensions::api::bluetooth::SetOutOfBandPairingData; | |
| 48 namespace Send = extensions::api::bluetooth::Send; | 45 namespace Send = extensions::api::bluetooth::Send; |
| 49 | 46 |
| 50 namespace { | 47 namespace { |
| 51 | 48 |
| 52 const char kCouldNotGetLocalOutOfBandPairingData[] = | |
| 53 "Could not get local Out Of Band Pairing Data"; | |
| 54 const char kCouldNotSetOutOfBandPairingData[] = | |
| 55 "Could not set Out Of Band Pairing Data"; | |
| 56 const char kInvalidDevice[] = "Invalid device"; | 49 const char kInvalidDevice[] = "Invalid device"; |
| 57 const char kInvalidUuid[] = "Invalid UUID"; | 50 const char kInvalidUuid[] = "Invalid UUID"; |
| 58 const char kPermissionDenied[] = "Permission to add profile denied."; | 51 const char kPermissionDenied[] = "Permission to add profile denied."; |
| 59 const char kProfileAlreadyRegistered[] = | 52 const char kProfileAlreadyRegistered[] = |
| 60 "This profile has already been registered"; | 53 "This profile has already been registered"; |
| 61 const char kProfileNotFound[] = "Profile not found: invalid uuid"; | 54 const char kProfileNotFound[] = "Profile not found: invalid uuid"; |
| 62 const char kProfileRegistrationFailed[] = "Profile registration failed"; | 55 const char kProfileRegistrationFailed[] = "Profile registration failed"; |
| 63 const char kStartDiscoveryFailed[] = "Starting discovery failed"; | 56 const char kStartDiscoveryFailed[] = "Starting discovery failed"; |
| 64 const char kStopDiscoveryFailed[] = "Failed to stop discovery"; | 57 const char kStopDiscoveryFailed[] = "Failed to stop discovery"; |
| 65 | 58 |
| (...skipping 460 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 526 SetError("Removed. Use chrome.bluetoothSocket.getInfo() instead."); | 519 SetError("Removed. Use chrome.bluetoothSocket.getInfo() instead."); |
| 527 return false; | 520 return false; |
| 528 } | 521 } |
| 529 | 522 |
| 530 bool BluetoothGetSocketsFunction::RunAsync() { | 523 bool BluetoothGetSocketsFunction::RunAsync() { |
| 531 // TODO(keybuk): Remove. | 524 // TODO(keybuk): Remove. |
| 532 SetError("Removed. Use chrome.bluetoothSocket.getSockets() instead."); | 525 SetError("Removed. Use chrome.bluetoothSocket.getSockets() instead."); |
| 533 return false; | 526 return false; |
| 534 } | 527 } |
| 535 | 528 |
| 536 void BluetoothSetOutOfBandPairingDataFunction::OnSuccessCallback() { | |
| 537 SendResponse(true); | |
| 538 } | |
| 539 | |
| 540 void BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback() { | |
| 541 SetError(kCouldNotSetOutOfBandPairingData); | |
| 542 SendResponse(false); | |
| 543 } | |
| 544 | |
| 545 bool BluetoothSetOutOfBandPairingDataFunction::DoWork( | |
| 546 scoped_refptr<BluetoothAdapter> adapter) { | |
| 547 // TODO(bryeung): update to new-style parameter passing when ArrayBuffer | |
| 548 // support is added | |
| 549 base::DictionaryValue* options; | |
| 550 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options)); | |
| 551 std::string address; | |
| 552 EXTENSION_FUNCTION_VALIDATE(options->GetString("deviceAddress", &address)); | |
| 553 | |
| 554 BluetoothDevice* device = adapter->GetDevice(address); | |
| 555 if (!device) { | |
| 556 SetError(kInvalidDevice); | |
| 557 SendResponse(false); | |
| 558 return false; | |
| 559 } | |
| 560 | |
| 561 if (options->HasKey("data")) { | |
| 562 base::DictionaryValue* data_in; | |
| 563 EXTENSION_FUNCTION_VALIDATE(options->GetDictionary("data", &data_in)); | |
| 564 | |
| 565 device::BluetoothOutOfBandPairingData data_out; | |
| 566 | |
| 567 base::BinaryValue* tmp_data; | |
| 568 EXTENSION_FUNCTION_VALIDATE(data_in->GetBinary("hash", &tmp_data)); | |
| 569 EXTENSION_FUNCTION_VALIDATE( | |
| 570 tmp_data->GetSize() == device::kBluetoothOutOfBandPairingDataSize); | |
| 571 memcpy(data_out.hash, | |
| 572 reinterpret_cast<uint8_t*>(tmp_data->GetBuffer()), | |
| 573 device::kBluetoothOutOfBandPairingDataSize); | |
| 574 | |
| 575 EXTENSION_FUNCTION_VALIDATE(data_in->GetBinary("randomizer", &tmp_data)); | |
| 576 EXTENSION_FUNCTION_VALIDATE( | |
| 577 tmp_data->GetSize() == device::kBluetoothOutOfBandPairingDataSize); | |
| 578 memcpy(data_out.randomizer, | |
| 579 reinterpret_cast<uint8_t*>(tmp_data->GetBuffer()), | |
| 580 device::kBluetoothOutOfBandPairingDataSize); | |
| 581 | |
| 582 device->SetOutOfBandPairingData( | |
| 583 data_out, | |
| 584 base::Bind(&BluetoothSetOutOfBandPairingDataFunction::OnSuccessCallback, | |
| 585 this), | |
| 586 base::Bind(&BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback, | |
| 587 this)); | |
| 588 } else { | |
| 589 device->ClearOutOfBandPairingData( | |
| 590 base::Bind(&BluetoothSetOutOfBandPairingDataFunction::OnSuccessCallback, | |
| 591 this), | |
| 592 base::Bind(&BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback, | |
| 593 this)); | |
| 594 } | |
| 595 | |
| 596 return true; | |
| 597 } | |
| 598 | |
| 599 void BluetoothGetLocalOutOfBandPairingDataFunction::ReadCallback( | |
| 600 const device::BluetoothOutOfBandPairingData& data) { | |
| 601 base::BinaryValue* hash = base::BinaryValue::CreateWithCopiedBuffer( | |
| 602 reinterpret_cast<const char*>(data.hash), | |
| 603 device::kBluetoothOutOfBandPairingDataSize); | |
| 604 base::BinaryValue* randomizer = base::BinaryValue::CreateWithCopiedBuffer( | |
| 605 reinterpret_cast<const char*>(data.randomizer), | |
| 606 device::kBluetoothOutOfBandPairingDataSize); | |
| 607 | |
| 608 // TODO(bryeung): convert to bluetooth::OutOfBandPairingData | |
| 609 // when ArrayBuffer support within objects is completed. | |
| 610 base::DictionaryValue* result = new base::DictionaryValue(); | |
| 611 result->Set("hash", hash); | |
| 612 result->Set("randomizer", randomizer); | |
| 613 | |
| 614 SetResult(result); | |
| 615 | |
| 616 SendResponse(true); | |
| 617 } | |
| 618 | |
| 619 void BluetoothGetLocalOutOfBandPairingDataFunction::ErrorCallback() { | |
| 620 SetError(kCouldNotGetLocalOutOfBandPairingData); | |
| 621 SendResponse(false); | |
| 622 } | |
| 623 | |
| 624 bool BluetoothGetLocalOutOfBandPairingDataFunction::DoWork( | |
| 625 scoped_refptr<BluetoothAdapter> adapter) { | |
| 626 adapter->ReadLocalOutOfBandPairingData( | |
| 627 base::Bind(&BluetoothGetLocalOutOfBandPairingDataFunction::ReadCallback, | |
| 628 this), | |
| 629 base::Bind(&BluetoothGetLocalOutOfBandPairingDataFunction::ErrorCallback, | |
| 630 this)); | |
| 631 | |
| 632 return true; | |
| 633 } | |
| 634 | |
| 635 void BluetoothStartDiscoveryFunction::OnSuccessCallback() { | 529 void BluetoothStartDiscoveryFunction::OnSuccessCallback() { |
| 636 SendResponse(true); | 530 SendResponse(true); |
| 637 } | 531 } |
| 638 | 532 |
| 639 void BluetoothStartDiscoveryFunction::OnErrorCallback() { | 533 void BluetoothStartDiscoveryFunction::OnErrorCallback() { |
| 640 SetError(kStartDiscoveryFailed); | 534 SetError(kStartDiscoveryFailed); |
| 641 SendResponse(false); | 535 SendResponse(false); |
| 642 } | 536 } |
| 643 | 537 |
| 644 bool BluetoothStartDiscoveryFunction::DoWork( | 538 bool BluetoothStartDiscoveryFunction::DoWork( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 667 adapter, | 561 adapter, |
| 668 extension_id(), | 562 extension_id(), |
| 669 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), | 563 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), |
| 670 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); | 564 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); |
| 671 | 565 |
| 672 return true; | 566 return true; |
| 673 } | 567 } |
| 674 | 568 |
| 675 } // namespace api | 569 } // namespace api |
| 676 } // namespace extensions | 570 } // namespace extensions |
| OLD | NEW |