Add testing/scripts/OWNERS
[chromium-blink-merge.git] / extensions / browser / api / bluetooth / bluetooth_extension_function.cc
blob7a5eefe501f07174fc45f7d6895c07b24f74464f
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
3 // found in the LICENSE file.
5 #include "extensions/browser/api/bluetooth/bluetooth_extension_function.h"
7 #include "base/memory/ref_counted.h"
8 #include "content/public/browser/browser_thread.h"
9 #include "device/bluetooth/bluetooth_adapter.h"
10 #include "device/bluetooth/bluetooth_adapter_factory.h"
11 #include "extensions/browser/api/bluetooth/bluetooth_api.h"
12 #include "extensions/browser/api/bluetooth/bluetooth_event_router.h"
14 using content::BrowserThread;
16 namespace {
18 const char kPlatformNotSupported[] =
19 "This operation is not supported on your platform";
21 extensions::BluetoothEventRouter* GetEventRouter(
22 content::BrowserContext* context) {
23 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
24 return extensions::BluetoothAPI::Get(context)->event_router();
27 bool IsBluetoothSupported(content::BrowserContext* context) {
28 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
29 return GetEventRouter(context)->IsBluetoothSupported();
32 void GetAdapter(const device::BluetoothAdapterFactory::AdapterCallback callback,
33 content::BrowserContext* context) {
34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
35 GetEventRouter(context)->GetAdapter(callback);
38 } // namespace
40 namespace extensions {
41 namespace core_api {
43 BluetoothExtensionFunction::BluetoothExtensionFunction() {
46 BluetoothExtensionFunction::~BluetoothExtensionFunction() {
49 bool BluetoothExtensionFunction::RunAsync() {
50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
52 if (!IsBluetoothSupported(browser_context())) {
53 SetError(kPlatformNotSupported);
54 return false;
56 GetAdapter(base::Bind(&BluetoothExtensionFunction::RunOnAdapterReady, this),
57 browser_context());
59 return true;
62 void BluetoothExtensionFunction::RunOnAdapterReady(
63 scoped_refptr<device::BluetoothAdapter> adapter) {
64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
65 DoWork(adapter);
68 } // namespace core_api
69 } // namespace extensions