summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions
diff options
context:
space:
mode:
authorbryeung@chromium.org <bryeung@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-06-18 03:55:54 +0000
committerbryeung@chromium.org <bryeung@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-06-18 03:55:54 +0000
commita19410b911dc01c89330f7d2e9183c6710edaf97 (patch)
treee47e02688924ff6a6c319166167604a8dd296c67 /chrome/browser/extensions
parent4d247c5ec7442f5a85e56aae400a91d5b50feb11 (diff)
downloadchromium_src-a19410b911dc01c89330f7d2e9183c6710edaf97.zip
chromium_src-a19410b911dc01c89330f7d2e9183c6710edaf97.tar.gz
chromium_src-a19410b911dc01c89330f7d2e9183c6710edaf97.tar.bz2
Bluetooth Extension API: Add a discovery API
BUG=132597 TEST=simple test added (events testing coming in a future CL: crbug.com/132616) Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=142623 Review URL: https://chromiumcodereview.appspot.com/10536159 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@142666 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions')
-rw-r--r--chrome/browser/extensions/api/bluetooth/bluetooth_api.cc88
-rw-r--r--chrome/browser/extensions/api/bluetooth/bluetooth_api.h34
-rw-r--r--chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.cc40
-rw-r--r--chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h33
-rw-r--r--chrome/browser/extensions/api/bluetooth/bluetooth_apitest_chromeos.cc56
-rw-r--r--chrome/browser/extensions/extension_event_names.cc2
-rw-r--r--chrome/browser/extensions/extension_event_names.h1
7 files changed, 225 insertions, 29 deletions
diff --git a/chrome/browser/extensions/api/bluetooth/bluetooth_api.cc b/chrome/browser/extensions/api/bluetooth/bluetooth_api.cc
index 9e7eebd..76d2233 100644
--- a/chrome/browser/extensions/api/bluetooth/bluetooth_api.cc
+++ b/chrome/browser/extensions/api/bluetooth/bluetooth_api.cc
@@ -10,7 +10,7 @@
#include <string>
-#include "base/utf_string_conversions.h"
+#include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/api/experimental_bluetooth.h"
@@ -19,7 +19,6 @@
#if defined(OS_CHROMEOS)
#include "base/memory/ref_counted.h"
#include "base/safe_strerror_posix.h"
-#include "base/synchronization/lock.h"
#include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h"
#include "chrome/browser/chromeos/bluetooth/bluetooth_device.h"
#include "chrome/browser/chromeos/bluetooth/bluetooth_socket.h"
@@ -40,23 +39,6 @@ chromeos::BluetoothAdapter* GetMutableAdapter(Profile* profile) {
return GetEventRouter(profile)->GetMutableAdapter();
}
-// Fill in a Device object from a chromeos::BluetoothDevice.
-void PopulateApiDevice(const chromeos::BluetoothDevice& device,
- extensions::api::experimental_bluetooth::Device* out) {
- out->name = UTF16ToUTF8(device.GetName());
- out->address = device.address();
- out->paired = device.IsPaired();
- out->bonded = device.IsBonded();
- out->connected = device.IsConnected();
-}
-
-// The caller takes ownership of the returned pointer.
-base::Value* BluetoothDeviceToValue(const chromeos::BluetoothDevice& device) {
- extensions::api::experimental_bluetooth::Device api_device;
- PopulateApiDevice(device, &api_device);
- return api_device.ToValue().release();
-}
-
} // namespace
#endif
@@ -69,6 +51,9 @@ const char kCouldNotSetOutOfBandPairingData[] =
const char kFailedToConnect[] = "Connection failed";
const char kInvalidDevice[] = "Invalid device";
const char kSocketNotFoundError[] = "Socket not found: invalid socket id";
+const char kStartDiscoveryFailed[] =
+ "Starting discovery failed, or already discovering";
+const char kStopDiscoveryFailed[] = "Failed to stop discovery";
} // namespace
@@ -110,7 +95,7 @@ void BluetoothGetDevicesFunction::AddDeviceIfTrueCallback(
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
if (shouldAdd)
- list->Append(BluetoothDeviceToValue(*device));
+ list->Append(experimental_bluetooth::BluetoothDeviceToValue(*device));
callbacks_pending_--;
if (callbacks_pending_ == -1) {
@@ -147,7 +132,7 @@ bool BluetoothGetDevicesFunction::RunImpl() {
continue;
if (options.name.get() == NULL) {
- matches->Append(BluetoothDeviceToValue(*device));
+ matches->Append(experimental_bluetooth::BluetoothDeviceToValue(*device));
continue;
}
@@ -177,7 +162,8 @@ void BluetoothConnectFunction::ConnectToServiceCallback(
int socket_id = GetEventRouter(profile())->RegisterSocket(socket);
experimental_bluetooth::Socket result_socket;
- PopulateApiDevice(*device, &result_socket.device);
+ experimental_bluetooth::BluetoothDeviceToApiDevice(
+ *device, &result_socket.device);
result_socket.service_uuid = service_uuid;
result_socket.id = socket_id;
result_.reset(result_socket.ToValue().release());
@@ -424,6 +410,54 @@ bool BluetoothGetLocalOutOfBandPairingDataFunction::RunImpl() {
return true;
}
+void BluetoothStartDiscoveryFunction::OnSuccessCallback() {
+ SendResponse(true);
+ Release(); // Added in RunImpl
+}
+
+void BluetoothStartDiscoveryFunction::OnErrorCallback() {
+ SetError(kStartDiscoveryFailed);
+ SendResponse(false);
+ Release(); // Added in RunImpl
+}
+
+bool BluetoothStartDiscoveryFunction::RunImpl() {
+ GetEventRouter(profile())->SetSendDiscoveryEvents(true);
+
+ // BluetoothAdapter will throw an error if we SetDiscovering(true) when
+ // discovery is already in progress
+ if (GetMutableAdapter(profile())->IsDiscovering()) {
+ SendResponse(true);
+ return true;
+ }
+
+ AddRef(); // Removed in whichever callback is called.
+ GetMutableAdapter(profile())->SetDiscovering(true,
+ base::Bind(&BluetoothStartDiscoveryFunction::OnSuccessCallback, this),
+ base::Bind(&BluetoothStartDiscoveryFunction::OnErrorCallback, this));
+ return true;
+}
+
+void BluetoothStopDiscoveryFunction::OnSuccessCallback() {
+ SendResponse(true);
+ Release(); // Added in RunImpl
+}
+
+void BluetoothStopDiscoveryFunction::OnErrorCallback() {
+ SetError(kStopDiscoveryFailed);
+ SendResponse(false);
+ Release(); // Added in RunImpl
+}
+
+bool BluetoothStopDiscoveryFunction::RunImpl() {
+ GetEventRouter(profile())->SetSendDiscoveryEvents(false);
+ AddRef(); // Removed in whichever callback is called.
+ GetMutableAdapter(profile())->SetDiscovering(false,
+ base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this),
+ base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this));
+ return true;
+}
+
#else
// -----------------------------------------------------------------------------
@@ -483,6 +517,16 @@ bool BluetoothWriteFunction::Respond() {
return false;
}
+bool BluetoothStartDiscoveryFunction::RunImpl() {
+ NOTREACHED() << "Not implemented yet";
+ return false;
+}
+
+bool BluetoothStopDiscoveryFunction::RunImpl() {
+ NOTREACHED() << "Not implemented yet";
+ return false;
+}
+
bool BluetoothSetOutOfBandPairingDataFunction::RunImpl() {
NOTREACHED() << "Not implemented yet";
return false;
diff --git a/chrome/browser/extensions/api/bluetooth/bluetooth_api.h b/chrome/browser/extensions/api/bluetooth/bluetooth_api.h
index 669873a..4abe159 100644
--- a/chrome/browser/extensions/api/bluetooth/bluetooth_api.h
+++ b/chrome/browser/extensions/api/bluetooth/bluetooth_api.h
@@ -190,6 +190,40 @@ class BluetoothGetLocalOutOfBandPairingDataFunction
virtual bool RunImpl() OVERRIDE;
};
+class BluetoothStartDiscoveryFunction : public AsyncExtensionFunction {
+ public:
+ DECLARE_EXTENSION_FUNCTION_NAME("experimental.bluetooth.startDiscovery")
+
+ protected:
+ virtual ~BluetoothStartDiscoveryFunction() {}
+
+ // ExtensionFunction:
+ virtual bool RunImpl() OVERRIDE;
+
+#if defined(OS_CHROMEOS)
+ private:
+ void OnSuccessCallback();
+ void OnErrorCallback();
+#endif
+};
+
+class BluetoothStopDiscoveryFunction : public AsyncExtensionFunction {
+ public:
+ DECLARE_EXTENSION_FUNCTION_NAME("experimental.bluetooth.stopDiscovery")
+
+ protected:
+ virtual ~BluetoothStopDiscoveryFunction() {}
+
+ // ExtensionFunction:
+ virtual bool RunImpl() OVERRIDE;
+
+#if defined(OS_CHROMEOS)
+ private:
+ void OnSuccessCallback();
+ void OnErrorCallback();
+#endif
+};
+
} // namespace api
} // namespace extensions
diff --git a/chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.cc b/chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.cc
new file mode 100644
index 0000000..5503434
--- /dev/null
+++ b/chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.cc
@@ -0,0 +1,40 @@
+// Copyright (c) 2012 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.
+
+#include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h"
+
+#if defined(OS_CHROMEOS)
+
+#include "base/utf_string_conversions.h"
+#include "base/values.h"
+#include "chrome/browser/chromeos/bluetooth/bluetooth_device.h"
+#include "chrome/common/extensions/api/experimental_bluetooth.h"
+
+namespace extensions {
+namespace api {
+namespace experimental_bluetooth {
+
+// Fill in a Device object from a chromeos::BluetoothDevice.
+void BluetoothDeviceToApiDevice(
+ const chromeos::BluetoothDevice& device,
+ Device* out) {
+ out->name = UTF16ToUTF8(device.GetName());
+ out->address = device.address();
+ out->paired = device.IsPaired();
+ out->bonded = device.IsBonded();
+ out->connected = device.IsConnected();
+}
+
+// The caller takes ownership of the returned pointer.
+base::Value* BluetoothDeviceToValue(const chromeos::BluetoothDevice& device) {
+ extensions::api::experimental_bluetooth::Device api_device;
+ BluetoothDeviceToApiDevice(device, &api_device);
+ return api_device.ToValue().release();
+}
+
+} // namespace experimental_bluetooth
+} // namespace api
+} // namespace extensions
+
+#endif
diff --git a/chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h b/chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h
new file mode 100644
index 0000000..bc1ba9e
--- /dev/null
+++ b/chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h
@@ -0,0 +1,33 @@
+// Copyright (c) 2012 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.
+
+#ifndef CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_UTILS_H_
+#define CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_UTILS_H_
+#pragma once
+
+#if defined(OS_CHROMEOS)
+
+#include "base/values.h"
+#include "chrome/browser/chromeos/bluetooth/bluetooth_device.h"
+#include "chrome/common/extensions/api/experimental_bluetooth.h"
+
+namespace extensions {
+namespace api {
+namespace experimental_bluetooth {
+
+// Fill in a Device object from a chromeos::BluetoothDevice.
+void BluetoothDeviceToApiDevice(
+ const chromeos::BluetoothDevice& device,
+ Device* out);
+
+// The caller takes ownership of the returned pointer.
+base::Value* BluetoothDeviceToValue(const chromeos::BluetoothDevice& device);
+
+} // namespace experimental_bluetooth
+} // namespace api
+} // namespace extensions
+
+#endif
+
+#endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_UTILS_H_
diff --git a/chrome/browser/extensions/api/bluetooth/bluetooth_apitest_chromeos.cc b/chrome/browser/extensions/api/bluetooth/bluetooth_apitest_chromeos.cc
index e445920..30b8ba6 100644
--- a/chrome/browser/extensions/api/bluetooth/bluetooth_apitest_chromeos.cc
+++ b/chrome/browser/extensions/api/bluetooth/bluetooth_apitest_chromeos.cc
@@ -22,13 +22,6 @@ using extensions::Extension;
namespace utils = extension_function_test_utils;
namespace api = extensions::api;
-namespace chromeos {
-
-class BluetoothAdapater;
-
-} // namespace chromeos
-
-
namespace {
class BluetoothApiTest : public PlatformAppApiTest {
@@ -282,3 +275,52 @@ IN_PROC_BROWSER_TEST_F(BluetoothApiTest, SetOutOfBandPairingData) {
// ArrayBuffers in the arguments to the RunFunctionAnd* methods.
// crbug.com/132796
}
+
+IN_PROC_BROWSER_TEST_F(BluetoothApiTest, Discovery) {
+ // TODO(bryeung): test that no events are sent now (crbug.com/132616)
+
+ // Try with a failure to start
+ EXPECT_CALL(*mock_adapter_,
+ SetDiscovering(true,
+ testing::_,
+ testing::Truly(CallClosure)));
+ scoped_refptr<api::BluetoothStartDiscoveryFunction> start_function;
+ start_function = setupFunction(new api::BluetoothStartDiscoveryFunction);
+ std::string error(
+ utils::RunFunctionAndReturnError(start_function, "[]", browser()));
+ ASSERT_TRUE(!error.empty());
+
+ // Reset for a successful start
+ testing::Mock::VerifyAndClearExpectations(mock_adapter_);
+ EXPECT_CALL(*mock_adapter_,
+ SetDiscovering(true,
+ testing::Truly(CallClosure),
+ testing::_));
+
+ start_function = setupFunction(new api::BluetoothStartDiscoveryFunction);
+ (void)utils::RunFunctionAndReturnError(start_function, "[]", browser());
+
+ // TODO(bryeung): test that events are sent now (crbug.com/132616)
+
+ // Reset to try stopping
+ testing::Mock::VerifyAndClearExpectations(mock_adapter_);
+ EXPECT_CALL(*mock_adapter_,
+ SetDiscovering(false,
+ testing::Truly(CallClosure),
+ testing::_));
+ scoped_refptr<api::BluetoothStopDiscoveryFunction> stop_function;
+ stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction);
+ (void)utils::RunFunctionAndReturnResult(stop_function, "[]", browser());
+
+ // TODO(bryeung): test that no events are sent now (crbug.com/132616)
+
+ // Reset to try stopping with an error
+ testing::Mock::VerifyAndClearExpectations(mock_adapter_);
+ EXPECT_CALL(*mock_adapter_,
+ SetDiscovering(false,
+ testing::_,
+ testing::Truly(CallClosure)));
+ stop_function = setupFunction(new api::BluetoothStopDiscoveryFunction);
+ error = utils::RunFunctionAndReturnError(stop_function, "[]", browser());
+ ASSERT_TRUE(!error.empty());
+}
diff --git a/chrome/browser/extensions/extension_event_names.cc b/chrome/browser/extensions/extension_event_names.cc
index 5a8391f..9d76081 100644
--- a/chrome/browser/extensions/extension_event_names.cc
+++ b/chrome/browser/extensions/extension_event_names.cc
@@ -58,6 +58,8 @@ const char kOnOffscreenTabUpdated[] = "experimental.offscreenTabs.onUpdated";
#if defined(OS_CHROMEOS)
const char kBluetoothOnAvailabilityChanged[] =
"experimental.bluetooth.onAvailabilityChanged";
+const char kBluetoothOnDeviceDiscovered[] =
+ "experimental.bluetooth.onDeviceDiscovered";
const char kBluetoothOnPowerChanged[] =
"experimental.bluetooth.onPowerChanged";
#endif
diff --git a/chrome/browser/extensions/extension_event_names.h b/chrome/browser/extensions/extension_event_names.h
index b65fbafe..ca01863 100644
--- a/chrome/browser/extensions/extension_event_names.h
+++ b/chrome/browser/extensions/extension_event_names.h
@@ -67,6 +67,7 @@ extern const char kOnOffscreenTabUpdated[];
#if defined(OS_CHROMEOS)
// Bluetooth.
extern const char kBluetoothOnAvailabilityChanged[];
+extern const char kBluetoothOnDeviceDiscovered[];
extern const char kBluetoothOnPowerChanged[];
#endif