| Index: chrome/browser/extensions/api/bluetooth/bluetooth_api.h
|
| diff --git a/chrome/browser/extensions/api/bluetooth/bluetooth_api.h b/chrome/browser/extensions/api/bluetooth/bluetooth_api.h
|
| index 53263ec3f5d4a3b7e64583cc2404d3ac624d27a3..9206848aa80f972632dc7a127400714965b0e95d 100644
|
| --- a/chrome/browser/extensions/api/bluetooth/bluetooth_api.h
|
| +++ b/chrome/browser/extensions/api/bluetooth/bluetooth_api.h
|
| @@ -9,9 +9,13 @@
|
|
|
| #include "base/memory/ref_counted.h"
|
| #include "base/memory/scoped_ptr.h"
|
| +#include "chrome/browser/extensions/api/bluetooth/bluetooth_api_socket.h"
|
| #include "chrome/browser/extensions/api/bluetooth/bluetooth_extension_function.h"
|
| +#include "chrome/common/extensions/api/bluetooth.h"
|
| #include "device/bluetooth/bluetooth_device.h"
|
| #include "device/bluetooth/bluetooth_profile.h"
|
| +#include "device/bluetooth/bluetooth_socket.h"
|
| +#include "extensions/browser/api/api_resource_manager.h"
|
| #include "extensions/browser/api/async_api_function.h"
|
| #include "extensions/browser/browser_context_keyed_api_factory.h"
|
| #include "extensions/browser/event_router.h"
|
| @@ -22,23 +26,40 @@ class BrowserContext;
|
| }
|
|
|
| namespace device {
|
| -
|
| class BluetoothAdapter;
|
| -class BluetoothDevice;
|
| -class BluetoothSocket;
|
| struct BluetoothOutOfBandPairingData;
|
| +}
|
|
|
| -} // namespace device
|
| +namespace net {
|
| +class IOBuffer;
|
| +}
|
|
|
| namespace extensions {
|
|
|
| class ExtensionBluetoothEventRouter;
|
|
|
| // 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 profile.
|
| + typedef ApiResourceManager<BluetoothApiSocket>::ApiResourceData SocketData;
|
| +
|
| + struct ConnectionParams {
|
| + ConnectionParams();
|
| + ~ConnectionParams();
|
| +
|
| + content::BrowserThread::ID thread_id;
|
| + void* browser_context_id;
|
| + std::string extension_id;
|
| + std::string device_address;
|
| + std::string profile_uuid;
|
| + scoped_refptr<device::BluetoothSocket> socket;
|
| + scoped_refptr<SocketData> socket_data;
|
| + };
|
| +
|
| + // Convenience method to get the BluetoothAPI for a browser context.
|
| static BluetoothAPI* Get(content::BrowserContext* context);
|
|
|
| static BrowserContextKeyedAPIFactory<BluetoothAPI>* GetFactoryInstance();
|
| @@ -46,7 +67,9 @@ class BluetoothAPI : public BrowserContextKeyedAPI,
|
| explicit BluetoothAPI(content::BrowserContext* context);
|
| virtual ~BluetoothAPI();
|
|
|
| - ExtensionBluetoothEventRouter* bluetooth_event_router();
|
| + ExtensionBluetoothEventRouter* event_router();
|
| + scoped_refptr<SocketData> socket_data();
|
| + scoped_refptr<api::BluetoothSocketEventDispatcher> socket_event_dispatcher();
|
|
|
| // BrowserContextKeyedService implementation.
|
| virtual void Shutdown() OVERRIDE;
|
| @@ -55,10 +78,23 @@ class BluetoothAPI : public BrowserContextKeyedAPI,
|
| virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
|
| virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE;
|
|
|
| + // Dispatch an event that takes a connection socket as a parameter to the
|
| + // extension that registered the profile that the socket has connected to.
|
| + void DispatchConnectionEvent(const std::string& extension_id,
|
| + const std::string& profile_uuid,
|
| + const device::BluetoothDevice* device,
|
| + scoped_refptr<device::BluetoothSocket> socket);
|
| +
|
| private:
|
| - friend class BrowserContextKeyedAPIFactory<BluetoothAPI>;
|
| + static void RegisterSocket(const ConnectionParams& params);
|
| + static void PostEvent(const ConnectionParams& params,
|
| + scoped_ptr<extensions::Event> event);
|
| + static void DispatchEvent(void* browser_context_id,
|
| + const std::string& extension_id,
|
| + scoped_ptr<extensions::Event> event);
|
|
|
| // BrowserContextKeyedAPI implementation.
|
| + friend class BrowserContextKeyedAPIFactory<BluetoothAPI>;
|
| static const char* service_name() { return "BluetoothAPI"; }
|
| static const bool kServiceRedirectedInIncognito = true;
|
| static const bool kServiceIsNULLWhileTesting = true;
|
| @@ -66,11 +102,53 @@ class BluetoothAPI : public BrowserContextKeyedAPI,
|
| content::BrowserContext* browser_context_;
|
|
|
| // Created lazily on first access.
|
| - scoped_ptr<ExtensionBluetoothEventRouter> bluetooth_event_router_;
|
| + scoped_ptr<ExtensionBluetoothEventRouter> event_router_;
|
| + scoped_refptr<SocketData> socket_data_;
|
| + scoped_refptr<api::BluetoothSocketEventDispatcher> socket_event_dispatcher_;
|
| };
|
|
|
| namespace api {
|
|
|
| +class BluetoothSocketEventDispatcher;
|
| +
|
| +class BluetoothSocketApiFunction : public AsyncApiFunction {
|
| + public:
|
| + BluetoothSocketApiFunction();
|
| +
|
| + protected:
|
| + virtual ~BluetoothSocketApiFunction();
|
| +
|
| + // AsyncApiFunction:
|
| + virtual bool PrePrepare() OVERRIDE;
|
| + virtual bool Respond() OVERRIDE;
|
| +
|
| + scoped_refptr<BluetoothAPI::SocketData> socket_data_;
|
| + scoped_refptr<api::BluetoothSocketEventDispatcher> socket_event_dispatcher_;
|
| +};
|
| +
|
| +class BluetoothGetAdapterStateFunction : public BluetoothExtensionFunction {
|
| + public:
|
| + DECLARE_EXTENSION_FUNCTION("bluetooth.getAdapterState",
|
| + BLUETOOTH_GETADAPTERSTATE)
|
| +
|
| + protected:
|
| + virtual ~BluetoothGetAdapterStateFunction();
|
| +
|
| + // BluetoothExtensionFunction:
|
| + virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
|
| +};
|
| +
|
| +class BluetoothGetDevicesFunction : public BluetoothExtensionFunction {
|
| + public:
|
| + DECLARE_EXTENSION_FUNCTION("bluetooth.getDevices", BLUETOOTH_GETDEVICES)
|
| +
|
| + protected:
|
| + virtual ~BluetoothGetDevicesFunction();
|
| +
|
| + // BluetoothExtensionFunction:
|
| + virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
|
| +};
|
| +
|
| class BluetoothAddProfileFunction : public UIThreadExtensionFunction {
|
| public:
|
| DECLARE_EXTENSION_FUNCTION("bluetooth.addProfile", BLUETOOTH_ADDPROFILE)
|
| @@ -78,7 +156,7 @@ class BluetoothAddProfileFunction : public UIThreadExtensionFunction {
|
| BluetoothAddProfileFunction();
|
|
|
| protected:
|
| - virtual ~BluetoothAddProfileFunction() {}
|
| + virtual ~BluetoothAddProfileFunction();
|
| virtual bool RunImpl() OVERRIDE;
|
|
|
| virtual void RegisterProfile(
|
| @@ -97,7 +175,7 @@ class BluetoothRemoveProfileFunction : public SyncExtensionFunction {
|
| BLUETOOTH_REMOVEPROFILE)
|
|
|
| protected:
|
| - virtual ~BluetoothRemoveProfileFunction() {}
|
| + virtual ~BluetoothRemoveProfileFunction();
|
| virtual bool RunImpl() OVERRIDE;
|
| };
|
|
|
| @@ -106,143 +184,157 @@ class BluetoothGetProfilesFunction : public BluetoothExtensionFunction {
|
| DECLARE_EXTENSION_FUNCTION("bluetooth.getProfiles", BLUETOOTH_GETPROFILES)
|
|
|
| protected:
|
| - virtual ~BluetoothGetProfilesFunction() {}
|
| + virtual ~BluetoothGetProfilesFunction();
|
|
|
| // BluetoothExtensionFunction:
|
| virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
|
| };
|
|
|
| -class BluetoothGetAdapterStateFunction : public BluetoothExtensionFunction {
|
| +class BluetoothConnectFunction : public BluetoothExtensionFunction {
|
| public:
|
| - DECLARE_EXTENSION_FUNCTION("bluetooth.getAdapterState",
|
| - BLUETOOTH_GETADAPTERSTATE)
|
| + DECLARE_EXTENSION_FUNCTION("bluetooth.connect", BLUETOOTH_CONNECT)
|
|
|
| protected:
|
| - virtual ~BluetoothGetAdapterStateFunction() {}
|
| + virtual ~BluetoothConnectFunction();
|
|
|
| // BluetoothExtensionFunction:
|
| virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
|
| +
|
| + private:
|
| + void OnSuccessCallback();
|
| + void OnErrorCallback(const std::string& error);
|
| };
|
|
|
| -class BluetoothGetDevicesFunction : public BluetoothExtensionFunction {
|
| +class BluetoothDisconnectFunction : public BluetoothSocketApiFunction {
|
| public:
|
| - DECLARE_EXTENSION_FUNCTION("bluetooth.getDevices", BLUETOOTH_GETDEVICES)
|
| + DECLARE_EXTENSION_FUNCTION("bluetooth.disconnect", BLUETOOTH_DISCONNECT)
|
| + BluetoothDisconnectFunction();
|
|
|
| protected:
|
| - virtual ~BluetoothGetDevicesFunction() {}
|
| + virtual ~BluetoothDisconnectFunction();
|
|
|
| - // BluetoothExtensionFunction:
|
| - virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
|
| + // AsyncApiFunction:
|
| + virtual bool Prepare() OVERRIDE;
|
| + virtual void AsyncWorkStart() OVERRIDE;
|
| +
|
| + private:
|
| + void OnSuccess();
|
| +
|
| + scoped_ptr<bluetooth::Disconnect::Params> params_;
|
| };
|
|
|
| -class BluetoothGetServicesFunction : public BluetoothExtensionFunction {
|
| +class BluetoothSendFunction : public BluetoothSocketApiFunction {
|
| public:
|
| - DECLARE_EXTENSION_FUNCTION("bluetooth.getServices", BLUETOOTH_GETSERVICES)
|
| + DECLARE_EXTENSION_FUNCTION("bluetooth.send", BLUETOOTH_SEND)
|
| + BluetoothSendFunction();
|
|
|
| protected:
|
| - virtual ~BluetoothGetServicesFunction() {}
|
| + virtual ~BluetoothSendFunction();
|
|
|
| - // BluetoothExtensionFunction:
|
| - virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
|
| + // AsyncApiFunction:
|
| + virtual bool Prepare() OVERRIDE;
|
| + virtual void AsyncWorkStart() OVERRIDE;
|
|
|
| private:
|
| - void GetServiceRecordsCallback(
|
| - base::ListValue* services,
|
| - const device::BluetoothDevice::ServiceRecordList& records);
|
| - void OnErrorCallback();
|
| + void OnSendSuccess(int bytes_sent);
|
| + void OnSendError(const std::string& message);
|
| +
|
| + scoped_ptr<bluetooth::Send::Params> params_;
|
| + scoped_refptr<net::IOBuffer> io_buffer_;
|
| + size_t io_buffer_size_;
|
| };
|
|
|
| -class BluetoothConnectFunction : public BluetoothExtensionFunction {
|
| +class BluetoothUpdateSocketFunction : public BluetoothSocketApiFunction {
|
| public:
|
| - DECLARE_EXTENSION_FUNCTION("bluetooth.connect", BLUETOOTH_CONNECT)
|
| + DECLARE_EXTENSION_FUNCTION("bluetooth.updateSocket", BLUETOOTH_UPDATE_SOCKET)
|
| + BluetoothUpdateSocketFunction();
|
|
|
| protected:
|
| - virtual ~BluetoothConnectFunction() {}
|
| + virtual ~BluetoothUpdateSocketFunction();
|
|
|
| - // BluetoothExtensionFunction:
|
| - virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
|
| + // AsyncApiFunction:
|
| + virtual bool Prepare() OVERRIDE;
|
| + virtual void Work() OVERRIDE;
|
|
|
| private:
|
| - void OnSuccessCallback();
|
| - void OnErrorCallback();
|
| + scoped_ptr<bluetooth::UpdateSocket::Params> params_;
|
| };
|
|
|
| -class BluetoothDisconnectFunction : public SyncExtensionFunction {
|
| +class BluetoothSetSocketPausedFunction : public BluetoothSocketApiFunction {
|
| public:
|
| - DECLARE_EXTENSION_FUNCTION("bluetooth.disconnect", BLUETOOTH_DISCONNECT)
|
| + DECLARE_EXTENSION_FUNCTION("bluetooth.setSocketPaused",
|
| + BLUETOOTH_SET_SOCKET_PAUSED)
|
| + BluetoothSetSocketPausedFunction();
|
|
|
| protected:
|
| - virtual ~BluetoothDisconnectFunction() {}
|
| + virtual ~BluetoothSetSocketPausedFunction();
|
|
|
| - // ExtensionFunction:
|
| - virtual bool RunImpl() OVERRIDE;
|
| + // AsyncApiFunction:
|
| + virtual bool Prepare() OVERRIDE;
|
| + virtual void Work() OVERRIDE;
|
| +
|
| + private:
|
| + scoped_ptr<bluetooth::SetSocketPaused::Params> params_;
|
| };
|
|
|
| -class BluetoothReadFunction : public AsyncApiFunction {
|
| +class BluetoothGetSocketFunction : public BluetoothSocketApiFunction {
|
| public:
|
| - DECLARE_EXTENSION_FUNCTION("bluetooth.read", BLUETOOTH_READ)
|
| - BluetoothReadFunction();
|
| + DECLARE_EXTENSION_FUNCTION("bluetooth.getSocket", BLUETOOTH_GET_SOCKET)
|
| +
|
| + BluetoothGetSocketFunction();
|
|
|
| protected:
|
| - virtual ~BluetoothReadFunction();
|
| + virtual ~BluetoothGetSocketFunction();
|
|
|
| // AsyncApiFunction:
|
| virtual bool Prepare() OVERRIDE;
|
| - virtual bool Respond() OVERRIDE;
|
| virtual void Work() OVERRIDE;
|
|
|
| private:
|
| - bool success_;
|
| - scoped_refptr<device::BluetoothSocket> socket_;
|
| + scoped_ptr<bluetooth::GetSocket::Params> params_;
|
| };
|
|
|
| -class BluetoothWriteFunction : public AsyncApiFunction {
|
| +class BluetoothGetSocketsFunction : public BluetoothSocketApiFunction {
|
| public:
|
| - DECLARE_EXTENSION_FUNCTION("bluetooth.write", BLUETOOTH_WRITE)
|
| - BluetoothWriteFunction();
|
| + DECLARE_EXTENSION_FUNCTION("bluetooth.getSockets", BLUETOOTH_GET_SOCKETS)
|
| +
|
| + BluetoothGetSocketsFunction();
|
|
|
| protected:
|
| - virtual ~BluetoothWriteFunction();
|
| + virtual ~BluetoothGetSocketsFunction();
|
|
|
| // AsyncApiFunction:
|
| virtual bool Prepare() OVERRIDE;
|
| - virtual bool Respond() OVERRIDE;
|
| virtual void Work() OVERRIDE;
|
| -
|
| - private:
|
| - bool success_;
|
| - const base::BinaryValue* data_to_write_; // memory is owned by args_
|
| - scoped_refptr<device::BluetoothSocket> socket_;
|
| };
|
|
|
| -class BluetoothSetOutOfBandPairingDataFunction
|
| +class BluetoothGetLocalOutOfBandPairingDataFunction
|
| : public BluetoothExtensionFunction {
|
| public:
|
| - DECLARE_EXTENSION_FUNCTION("bluetooth.setOutOfBandPairingData",
|
| - BLUETOOTH_SETOUTOFBANDPAIRINGDATA)
|
| + DECLARE_EXTENSION_FUNCTION("bluetooth.getLocalOutOfBandPairingData",
|
| + BLUETOOTH_GETLOCALOUTOFBANDPAIRINGDATA)
|
|
|
| protected:
|
| - virtual ~BluetoothSetOutOfBandPairingDataFunction() {}
|
| + virtual ~BluetoothGetLocalOutOfBandPairingDataFunction() {}
|
|
|
| - void OnSuccessCallback();
|
| - void OnErrorCallback();
|
| + void ReadCallback(const device::BluetoothOutOfBandPairingData& data);
|
| + void ErrorCallback();
|
|
|
| // BluetoothExtensionFunction:
|
| virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
|
| };
|
|
|
| -class BluetoothGetLocalOutOfBandPairingDataFunction
|
| +class BluetoothSetOutOfBandPairingDataFunction
|
| : public BluetoothExtensionFunction {
|
| public:
|
| - DECLARE_EXTENSION_FUNCTION("bluetooth.getLocalOutOfBandPairingData",
|
| - BLUETOOTH_GETLOCALOUTOFBANDPAIRINGDATA)
|
| + DECLARE_EXTENSION_FUNCTION("bluetooth.setOutOfBandPairingData",
|
| + BLUETOOTH_SETOUTOFBANDPAIRINGDATA)
|
|
|
| protected:
|
| - virtual ~BluetoothGetLocalOutOfBandPairingDataFunction() {}
|
| + virtual ~BluetoothSetOutOfBandPairingDataFunction() {}
|
|
|
| - void ReadCallback(
|
| - const device::BluetoothOutOfBandPairingData& data);
|
| - void ErrorCallback();
|
| + void OnSuccessCallback();
|
| + void OnErrorCallback(const std::string& error);
|
|
|
| // BluetoothExtensionFunction:
|
| virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
|
|
|