1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef EXTENSIONS_BROWSER_API_BLUETOOTH_BLUETOOTH_API_H_
#define EXTENSIONS_BROWSER_API_BLUETOOTH_BLUETOOTH_API_H_
#include <memory>
#include <string>
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "device/bluetooth/bluetooth_device.h"
#include "extensions/browser/api/bluetooth/bluetooth_extension_function.h"
#include "extensions/browser/browser_context_keyed_api_factory.h"
#include "extensions/browser/event_router.h"
#include "extensions/browser/extension_function.h"
#include "extensions/common/api/bluetooth.h"
namespace content {
class BrowserContext;
}
namespace device {
class BluetoothAdapter;
}
namespace extensions {
class BluetoothEventRouter;
// The profile-keyed service that manages the bluetooth extension API.
// All methods of this class must be called on the UI thread.
// TODO(rpaquay): Rename this and move to separate file.
class BluetoothAPI : public BrowserContextKeyedAPI,
public EventRouter::Observer {
public:
// Convenience method to get the BluetoothAPI for a browser context.
static BluetoothAPI* Get(content::BrowserContext* context);
static BrowserContextKeyedAPIFactory<BluetoothAPI>* GetFactoryInstance();
explicit BluetoothAPI(content::BrowserContext* context);
~BluetoothAPI() override;
BluetoothEventRouter* event_router();
// KeyedService implementation.
void Shutdown() override;
// EventRouter::Observer implementation.
void OnListenerAdded(const EventListenerInfo& details) override;
void OnListenerRemoved(const EventListenerInfo& details) override;
private:
// BrowserContextKeyedAPI implementation.
friend class BrowserContextKeyedAPIFactory<BluetoothAPI>;
static const char* service_name() { return "BluetoothAPI"; }
static const bool kServiceRedirectedInIncognito = true;
static const bool kServiceIsNULLWhileTesting = true;
raw_ptr<content::BrowserContext> browser_context_;
// Created lazily on first access.
std::unique_ptr<BluetoothEventRouter> event_router_;
};
template <>
void BrowserContextKeyedAPIFactory<BluetoothAPI>::DeclareFactoryDependencies();
namespace api {
class BluetoothGetAdapterStateFunction : public BluetoothExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("bluetooth.getAdapterState",
BLUETOOTH_GETADAPTERSTATE)
protected:
~BluetoothGetAdapterStateFunction() override;
// BluetoothExtensionFunction:
void DoWork(scoped_refptr<device::BluetoothAdapter> adapter) override;
};
class BluetoothGetDevicesFunction : public BluetoothExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("bluetooth.getDevices", BLUETOOTH_GETDEVICES)
BluetoothGetDevicesFunction();
BluetoothGetDevicesFunction(const BluetoothGetDevicesFunction&) = delete;
BluetoothGetDevicesFunction& operator=(const BluetoothGetDevicesFunction&) =
delete;
protected:
~BluetoothGetDevicesFunction() override;
// BluetoothExtensionFunction:
bool CreateParams() override;
void DoWork(scoped_refptr<device::BluetoothAdapter> adapter) override;
private:
std::optional<bluetooth::GetDevices::Params> params_;
};
class BluetoothGetDeviceFunction : public BluetoothExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("bluetooth.getDevice", BLUETOOTH_GETDEVICE)
BluetoothGetDeviceFunction();
BluetoothGetDeviceFunction(const BluetoothGetDeviceFunction&) = delete;
BluetoothGetDeviceFunction& operator=(const BluetoothGetDeviceFunction&) =
delete;
// BluetoothExtensionFunction:
bool CreateParams() override;
void DoWork(scoped_refptr<device::BluetoothAdapter> adapter) override;
protected:
~BluetoothGetDeviceFunction() override;
private:
std::optional<extensions::api::bluetooth::GetDevice::Params> params_;
};
class BluetoothStartDiscoveryFunction : public BluetoothExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("bluetooth.startDiscovery",
BLUETOOTH_STARTDISCOVERY)
protected:
~BluetoothStartDiscoveryFunction() override {}
// BluetoothExtensionFunction:
void DoWork(scoped_refptr<device::BluetoothAdapter> adapter) override;
private:
void OnSuccessCallback();
void OnErrorCallback();
};
class BluetoothStopDiscoveryFunction : public BluetoothExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("bluetooth.stopDiscovery", BLUETOOTH_STOPDISCOVERY)
protected:
~BluetoothStopDiscoveryFunction() override {}
// BluetoothExtensionFunction:
void DoWork(scoped_refptr<device::BluetoothAdapter> adapter) override;
private:
void OnSuccessCallback();
void OnErrorCallback();
};
} // namespace api
} // namespace extensions
#endif // EXTENSIONS_BROWSER_API_BLUETOOTH_BLUETOOTH_API_H_
|