diff options
author | keybuk@chromium.org <keybuk@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-17 19:24:16 +0000 |
---|---|---|
committer | keybuk@chromium.org <keybuk@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-17 19:24:16 +0000 |
commit | 16f765c6a6c3e147a47193afcc757dfd58a1b4a6 (patch) | |
tree | 2bf4714baadae7b91bc4012fa9e66c31a42de2b8 | |
parent | adb300506181ab79b94c27c575bcc20b40373fea (diff) | |
download | chromium_src-16f765c6a6c3e147a47193afcc757dfd58a1b4a6.zip chromium_src-16f765c6a6c3e147a47193afcc757dfd58a1b4a6.tar.gz chromium_src-16f765c6a6c3e147a47193afcc757dfd58a1b4a6.tar.bz2 |
Bluetooth: add chrome.bluetooth.getDevice
Add a function for obtaining a Device object for a single device by
address, instead of having to iterate getDevices to look for it.
BUG=352852
TEST=BluetoothApiTest.GetDevice
R=armansito@chromium.org, miket@chromium.org, rpaquay@chromium.org, xiyuan@chromium.org
Review URL: https://codereview.chromium.org/201063002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@257477 0039d316-1c4b-4281-b951-d872f2087c98
7 files changed, 119 insertions, 9 deletions
diff --git a/chrome/browser/extensions/api/bluetooth/bluetooth_api.cc b/chrome/browser/extensions/api/bluetooth/bluetooth_api.cc index 7a24969..02a3653 100644 --- a/chrome/browser/extensions/api/bluetooth/bluetooth_api.cc +++ b/chrome/browser/extensions/api/bluetooth/bluetooth_api.cc @@ -67,6 +67,7 @@ namespace AddProfile = extensions::api::bluetooth::AddProfile; namespace bluetooth = extensions::api::bluetooth; namespace Connect = extensions::api::bluetooth::Connect; namespace Disconnect = extensions::api::bluetooth::Disconnect; +namespace GetDevice = extensions::api::bluetooth::GetDevice; namespace GetDevices = extensions::api::bluetooth::GetDevices; namespace GetProfiles = extensions::api::bluetooth::GetProfiles; namespace GetServices = extensions::api::bluetooth::GetServices; @@ -303,6 +304,28 @@ bool BluetoothGetDevicesFunction::DoWork( return true; } +bool BluetoothGetDeviceFunction::DoWork( + scoped_refptr<BluetoothAdapter> adapter) { + DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); + + scoped_ptr<GetDevice::Params> params(GetDevice::Params::Create(*args_)); + EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); + const std::string& device_address = params->device_address; + + BluetoothDevice* device = adapter->GetDevice(device_address); + if (device) { + bluetooth::Device extension_device; + bluetooth::BluetoothDeviceToApiDevice(*device, &extension_device); + SetResult(extension_device.ToValue().release()); + SendResponse(true); + } else { + SetError(kInvalidDevice); + SendResponse(false); + } + + return false; +} + void BluetoothGetServicesFunction::GetServiceRecordsCallback( base::ListValue* services, const BluetoothDevice::ServiceRecordList& records) { diff --git a/chrome/browser/extensions/api/bluetooth/bluetooth_api.h b/chrome/browser/extensions/api/bluetooth/bluetooth_api.h index 6b842ea..87b9ce1 100644 --- a/chrome/browser/extensions/api/bluetooth/bluetooth_api.h +++ b/chrome/browser/extensions/api/bluetooth/bluetooth_api.h @@ -135,6 +135,17 @@ class BluetoothGetDevicesFunction : public BluetoothExtensionFunction { virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE; }; +class BluetoothGetDeviceFunction : public BluetoothExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION("bluetooth.getDevice", BLUETOOTH_GETDEVICE) + + protected: + virtual ~BluetoothGetDeviceFunction() {} + + // BluetoothExtensionFunction: + virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE; +}; + class BluetoothGetServicesFunction : public BluetoothExtensionFunction { public: DECLARE_EXTENSION_FUNCTION("bluetooth.getServices", BLUETOOTH_GETSERVICES) diff --git a/chrome/browser/extensions/api/bluetooth/bluetooth_apitest.cc b/chrome/browser/extensions/api/bluetooth/bluetooth_apitest.cc index cbd4043..015aae8 100644 --- a/chrome/browser/extensions/api/bluetooth/bluetooth_apitest.cc +++ b/chrome/browser/extensions/api/bluetooth/bluetooth_apitest.cc @@ -615,6 +615,27 @@ IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevices) { EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); } +IN_PROC_BROWSER_TEST_F(BluetoothApiTest, GetDevice) { + ResultCatcher catcher; + catcher.RestrictToProfile(browser()->profile()); + + EXPECT_CALL(*mock_adapter_, GetDevice(device1_->GetAddress())) + .WillOnce(testing::Return(device1_.get())); + EXPECT_CALL(*mock_adapter_, GetDevice(device2_->GetAddress())) + .Times(1) + .WillRepeatedly(testing::Return(static_cast<BluetoothDevice*>(NULL))); + + // Load and wait for setup + ExtensionTestMessageListener listener("ready", true); + ASSERT_TRUE( + LoadExtension(test_data_dir_.AppendASCII("bluetooth/get_device"))); + EXPECT_TRUE(listener.WaitUntilSatisfied()); + + listener.Reply("go"); + + EXPECT_TRUE(catcher.GetNextResult()) << catcher.message(); +} + IN_PROC_BROWSER_TEST_F(BluetoothApiTest, DeviceInfo) { ResultCatcher catcher; catcher.RestrictToProfile(browser()->profile()); diff --git a/chrome/common/extensions/api/bluetooth.idl b/chrome/common/extensions/api/bluetooth.idl index 5e59dc4..747df71 100644 --- a/chrome/common/extensions/api/bluetooth.idl +++ b/chrome/common/extensions/api/bluetooth.idl @@ -125,17 +125,18 @@ namespace bluetooth { }; callback AdapterStateCallback = void(AdapterState result); - callback AddressCallback = void (DOMString result); - callback BooleanCallback = void (boolean result); - callback DataCallback = void (optional ArrayBuffer result); - callback DevicesCallback = void (Device[] result); - callback NameCallback = void (DOMString result); - callback OutOfBandPairingDataCallback = void (OutOfBandPairingData data); + callback AddressCallback = void(DOMString result); + callback BooleanCallback = void(boolean result); + callback DataCallback = void(optional ArrayBuffer result); + callback DeviceCallback = void(Device result); + callback DevicesCallback = void(Device[] result); + callback NameCallback = void(DOMString result); + callback OutOfBandPairingDataCallback = void(OutOfBandPairingData data); callback ProfilesCallback = void(Profile[] result); - callback ResultCallback = void (); + callback ResultCallback = void(); callback ServicesCallback = void(ServiceRecord[] result); - callback SizeCallback = void (long result); - callback SocketCallback = void (Socket result); + callback SizeCallback = void(long result); + callback SocketCallback = void(Socket result); // Options for the getProfiles function. dictionary GetProfilesOptions { @@ -212,6 +213,11 @@ namespace bluetooth { // |callback| : Called when the search is completed. static void getDevices(DevicesCallback callback); + // Get information about a Bluetooth device known to the system. + // |deviceAddress| : Address of device to get. + // |callback| : Called with the Device object describing the device. + static void getDevice(DOMString deviceAddress, DeviceCallback callback); + // Returns the set of exported profiles for the device specified in options. // This function will not initiate a connection to the remote device. static void getProfiles(GetProfilesOptions options, diff --git a/chrome/test/data/extensions/api_test/bluetooth/get_device/manifest.json b/chrome/test/data/extensions/api_test/bluetooth/get_device/manifest.json new file mode 100644 index 0000000..e047a9c9 --- /dev/null +++ b/chrome/test/data/extensions/api_test/bluetooth/get_device/manifest.json @@ -0,0 +1,11 @@ +{ + "manifest_version": 2, + "name": "Test the Bluetooth getDevice API", + "version": "1.0", + "app": { + "background": { + "scripts": ["runtest.js"] + } + }, + "bluetooth": {} +} diff --git a/chrome/test/data/extensions/api_test/bluetooth/get_device/runtest.js b/chrome/test/data/extensions/api_test/bluetooth/get_device/runtest.js new file mode 100644 index 0000000..8d73624 --- /dev/null +++ b/chrome/test/data/extensions/api_test/bluetooth/get_device/runtest.js @@ -0,0 +1,37 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +function testGetDevice() { + chrome.test.assertEq(1, devices.length); + chrome.test.assertEq('d1', devices[0].name); + + chrome.test.succeed(); +} + +var devices = []; + +function failOnError() { + if (chrome.runtime.lastError) { + chrome.test.fail(chrome.runtime.lastError.message); + } +} + +chrome.bluetooth.getDevice('11:12:13:14:15:16', + function(device) { + failOnError(); + devices.push(device); + + chrome.bluetooth.getDevice('21:22:23:24:25:26', + function(device) { + // device should not exists + if (device || !chrome.runtime.lastError) { + chrome.test.fail('Unexpected device or missing error'); + } + + chrome.test.sendMessage('ready', + function(message) { + chrome.test.runTests([testGetDevice]); + }); + }); + }); diff --git a/extensions/browser/extension_function_histogram_value.h b/extensions/browser/extension_function_histogram_value.h index c3d60fe..9841213 100644 --- a/extensions/browser/extension_function_histogram_value.h +++ b/extensions/browser/extension_function_histogram_value.h @@ -759,6 +759,7 @@ enum HistogramValue { WEBVIEW_CONTEXTMENUSREMOVEALL, AUTOMATIONINTERNAL_ENABLECURRENTTAB, APP_CURRENTWINDOWINTERNAL_SETSIZECONSTRAINTS, + BLUETOOTH_GETDEVICE, // Last entry: Add new entries above and ensure to update // tools/metrics/histograms/histograms/histograms.xml. ENUM_BOUNDARY |