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.
4
5#include "extensions/browser/api/bluetooth/bluetooth_extension_function.h"
6
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"
13
14using content::BrowserThread;
15
16namespace {
17
18const char kPlatformNotSupported[] =
19    "This operation is not supported on your platform";
20
21extensions::BluetoothEventRouter* GetEventRouter(
22    content::BrowserContext* context) {
23  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
24  return extensions::BluetoothAPI::Get(context)->event_router();
25}
26
27bool IsBluetoothSupported(content::BrowserContext* context) {
28  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
29  return GetEventRouter(context)->IsBluetoothSupported();
30}
31
32void GetAdapter(const device::BluetoothAdapterFactory::AdapterCallback callback,
33                content::BrowserContext* context) {
34  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
35  GetEventRouter(context)->GetAdapter(callback);
36}
37
38}  // namespace
39
40namespace extensions {
41namespace core_api {
42
43BluetoothExtensionFunction::BluetoothExtensionFunction() {
44}
45
46BluetoothExtensionFunction::~BluetoothExtensionFunction() {
47}
48
49bool BluetoothExtensionFunction::RunAsync() {
50  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
51
52  if (!IsBluetoothSupported(browser_context())) {
53    SetError(kPlatformNotSupported);
54    return false;
55  }
56  GetAdapter(base::Bind(&BluetoothExtensionFunction::RunOnAdapterReady, this),
57             browser_context());
58
59  return true;
60}
61
62void BluetoothExtensionFunction::RunOnAdapterReady(
63    scoped_refptr<device::BluetoothAdapter> adapter) {
64  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
65  DoWork(adapter);
66}
67
68}  // namespace core_api
69}  // namespace extensions
70