Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1283)

Unified Diff: chrome/browser/extensions/api/bluetooth/bluetooth_api.h

Issue 180163009: chrome.bluetooth API improvements. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix ChromeOS Full build. Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/browser/extensions/api/bluetooth/bluetooth_api.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 21e164c1e7a9e9174dfa3ff1d5d108ce454ea8be..38dce60863c8304fd532fbcd198a70aeeff9ed9c 100644
--- a/chrome/browser/extensions/api/bluetooth/bluetooth_api.h
+++ b/chrome/browser/extensions/api/bluetooth/bluetooth_api.h
@@ -9,10 +9,14 @@
#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 "device/bluetooth/bluetooth_uuid.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"
@@ -23,23 +27,40 @@ class BrowserContext;
}
namespace device {
-
class BluetoothAdapter;
-class BluetoothDevice;
-class BluetoothSocket;
struct BluetoothOutOfBandPairingData;
+}
-} // namespace device
+namespace net {
+class IOBuffer;
+}
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 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;
+ device::BluetoothUUID 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();
@@ -47,7 +68,9 @@ class BluetoothAPI : public BrowserContextKeyedAPI,
explicit BluetoothAPI(content::BrowserContext* context);
virtual ~BluetoothAPI();
- BluetoothEventRouter* bluetooth_event_router();
+ BluetoothEventRouter* event_router();
+ scoped_refptr<SocketData> socket_data();
+ scoped_refptr<api::BluetoothSocketEventDispatcher> socket_event_dispatcher();
// KeyedService implementation.
virtual void Shutdown() OVERRIDE;
@@ -56,10 +79,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 device::BluetoothUUID& profile_uuid,
+ const device::BluetoothDevice* device,
+ scoped_refptr<device::BluetoothSocket> socket);
+
private:
- friend class BrowserContextKeyedAPIFactory<BluetoothAPI>;
+ static void RegisterSocket(const ConnectionParams& params);
+ static void RegisterSocketUI(const ConnectionParams& params, int socket_id);
+ static void RegisterSocketWithAdapterUI(
+ const ConnectionParams& params,
+ int socket_id,
+ scoped_refptr<device::BluetoothAdapter> adapter);
// BrowserContextKeyedAPI implementation.
+ friend class BrowserContextKeyedAPIFactory<BluetoothAPI>;
static const char* service_name() { return "BluetoothAPI"; }
static const bool kServiceRedirectedInIncognito = true;
static const bool kServiceIsNULLWhileTesting = true;
@@ -67,11 +103,77 @@ class BluetoothAPI : public BrowserContextKeyedAPI,
content::BrowserContext* browser_context_;
// Created lazily on first access.
- scoped_ptr<BluetoothEventRouter> bluetooth_event_router_;
+ scoped_ptr<BluetoothEventRouter> event_router_;
+ scoped_refptr<SocketData> socket_data_;
+ scoped_refptr<api::BluetoothSocketEventDispatcher> socket_event_dispatcher_;
};
namespace api {
+class BluetoothSocketEventDispatcher;
+
+// Base class for methods dealing with BluetoothSocketApi and
+// ApiResourceManager<BluetoothSocketApi>.
+class BluetoothSocketApiFunction : public UIThreadExtensionFunction {
+ public:
+ BluetoothSocketApiFunction();
+
+ protected:
+ virtual ~BluetoothSocketApiFunction();
+
+ // ExtensionFunction::RunImpl()
+ virtual bool RunImpl() OVERRIDE;
+
+ bool PrePrepare();
+ bool Respond();
+ void AsyncWorkCompleted();
+
+ virtual bool Prepare() = 0;
+ virtual void Work();
+ virtual void AsyncWorkStart();
+
+ content::BrowserThread::ID work_thread_id() const {
+ return BluetoothApiSocket::kThreadId;
+ }
+
+ 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 BluetoothGetDeviceFunction : public BluetoothExtensionFunction {
+ public:
+ DECLARE_EXTENSION_FUNCTION("bluetooth.getDevice", BLUETOOTH_GETDEVICE)
+
+ // BluetoothExtensionFunction:
+ virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
+
+ protected:
+ virtual ~BluetoothGetDeviceFunction();
+};
+
class BluetoothAddProfileFunction : public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("bluetooth.addProfile", BLUETOOTH_ADDPROFILE)
@@ -79,7 +181,7 @@ class BluetoothAddProfileFunction : public UIThreadExtensionFunction {
BluetoothAddProfileFunction();
protected:
- virtual ~BluetoothAddProfileFunction() {}
+ virtual ~BluetoothAddProfileFunction();
virtual bool RunImpl() OVERRIDE;
virtual void RegisterProfile(
@@ -98,121 +200,137 @@ class BluetoothRemoveProfileFunction : public SyncExtensionFunction {
BLUETOOTH_REMOVEPROFILE)
protected:
- virtual ~BluetoothRemoveProfileFunction() {}
+ virtual ~BluetoothRemoveProfileFunction();
virtual bool RunImpl() OVERRIDE;
};
-class BluetoothGetAdapterStateFunction : public BluetoothExtensionFunction {
+class BluetoothGetProfilesFunction : public BluetoothExtensionFunction {
public:
- DECLARE_EXTENSION_FUNCTION("bluetooth.getAdapterState",
- BLUETOOTH_GETADAPTERSTATE)
+ DECLARE_EXTENSION_FUNCTION("bluetooth.getProfiles", BLUETOOTH_GETPROFILES)
protected:
- virtual ~BluetoothGetAdapterStateFunction() {}
+ virtual ~BluetoothGetProfilesFunction() {}
// BluetoothExtensionFunction:
virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
};
-class BluetoothGetDevicesFunction : public BluetoothExtensionFunction {
+class BluetoothConnectFunction : public BluetoothExtensionFunction {
public:
- DECLARE_EXTENSION_FUNCTION("bluetooth.getDevices", BLUETOOTH_GETDEVICES)
+ DECLARE_EXTENSION_FUNCTION("bluetooth.connect", BLUETOOTH_CONNECT)
protected:
- virtual ~BluetoothGetDevicesFunction() {}
+ virtual ~BluetoothConnectFunction();
// BluetoothExtensionFunction:
virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
+
+ private:
+ void OnSuccessCallback();
+ void OnErrorCallback(const std::string& error);
};
-class BluetoothGetDeviceFunction : public BluetoothExtensionFunction {
+class BluetoothDisconnectFunction : public BluetoothSocketApiFunction {
public:
- DECLARE_EXTENSION_FUNCTION("bluetooth.getDevice", BLUETOOTH_GETDEVICE)
+ DECLARE_EXTENSION_FUNCTION("bluetooth.disconnect", BLUETOOTH_DISCONNECT)
+ BluetoothDisconnectFunction();
protected:
- virtual ~BluetoothGetDeviceFunction() {}
+ 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 BluetoothConnectFunction : public BluetoothExtensionFunction {
+class BluetoothSendFunction : public BluetoothSocketApiFunction {
public:
- DECLARE_EXTENSION_FUNCTION("bluetooth.connect", BLUETOOTH_CONNECT)
+ DECLARE_EXTENSION_FUNCTION("bluetooth.send", BLUETOOTH_WRITE)
+ BluetoothSendFunction();
protected:
- virtual ~BluetoothConnectFunction() {}
+ virtual ~BluetoothSendFunction();
- // BluetoothExtensionFunction:
- virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
+ // AsyncApiFunction:
+ virtual bool Prepare() OVERRIDE;
+ virtual void AsyncWorkStart() OVERRIDE;
private:
- void OnSuccessCallback();
- 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 BluetoothDisconnectFunction : public SyncExtensionFunction {
+class BluetoothUpdateSocketFunction : public BluetoothSocketApiFunction {
public:
- DECLARE_EXTENSION_FUNCTION("bluetooth.disconnect", BLUETOOTH_DISCONNECT)
+ DECLARE_EXTENSION_FUNCTION("bluetooth.updateSocket", BLUETOOTH_UPDATE_SOCKET)
+ BluetoothUpdateSocketFunction();
protected:
- virtual ~BluetoothDisconnectFunction() {}
+ virtual ~BluetoothUpdateSocketFunction();
- // ExtensionFunction:
- virtual bool RunImpl() OVERRIDE;
+ // AsyncApiFunction:
+ virtual bool Prepare() OVERRIDE;
+ virtual void Work() OVERRIDE;
+
+ private:
+ scoped_ptr<bluetooth::UpdateSocket::Params> params_;
};
-class BluetoothReadFunction : public AsyncApiFunction {
+class BluetoothSetSocketPausedFunction : public BluetoothSocketApiFunction {
public:
- DECLARE_EXTENSION_FUNCTION("bluetooth.read", BLUETOOTH_READ)
- BluetoothReadFunction();
+ DECLARE_EXTENSION_FUNCTION("bluetooth.setSocketPaused",
+ BLUETOOTH_SET_SOCKET_PAUSED)
+ BluetoothSetSocketPausedFunction();
protected:
- virtual ~BluetoothReadFunction();
+ virtual ~BluetoothSetSocketPausedFunction();
// AsyncApiFunction:
virtual bool Prepare() OVERRIDE;
- virtual bool Respond() OVERRIDE;
virtual void Work() OVERRIDE;
private:
- bool success_;
- scoped_refptr<device::BluetoothSocket> socket_;
+ scoped_ptr<bluetooth::SetSocketPaused::Params> params_;
};
-class BluetoothWriteFunction : public AsyncApiFunction {
+class BluetoothGetSocketFunction : public BluetoothSocketApiFunction {
public:
- DECLARE_EXTENSION_FUNCTION("bluetooth.write", BLUETOOTH_WRITE)
- BluetoothWriteFunction();
+ DECLARE_EXTENSION_FUNCTION("bluetooth.getSocket", BLUETOOTH_GET_SOCKET)
+
+ BluetoothGetSocketFunction();
protected:
- virtual ~BluetoothWriteFunction();
+ virtual ~BluetoothGetSocketFunction();
// 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_;
+ scoped_ptr<bluetooth::GetSocket::Params> params_;
};
-class BluetoothSetOutOfBandPairingDataFunction
- : public BluetoothExtensionFunction {
+class BluetoothGetSocketsFunction : public BluetoothSocketApiFunction {
public:
- DECLARE_EXTENSION_FUNCTION("bluetooth.setOutOfBandPairingData",
- BLUETOOTH_SETOUTOFBANDPAIRINGDATA)
+ DECLARE_EXTENSION_FUNCTION("bluetooth.getSockets", BLUETOOTH_GET_SOCKETS)
- protected:
- virtual ~BluetoothSetOutOfBandPairingDataFunction() {}
+ BluetoothGetSocketsFunction();
- void OnSuccessCallback();
- void OnErrorCallback();
+ protected:
+ virtual ~BluetoothGetSocketsFunction();
- // BluetoothExtensionFunction:
- virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
+ // AsyncApiFunction:
+ virtual bool Prepare() OVERRIDE;
+ virtual void Work() OVERRIDE;
};
class BluetoothGetLocalOutOfBandPairingDataFunction
@@ -224,14 +342,29 @@ class BluetoothGetLocalOutOfBandPairingDataFunction
protected:
virtual ~BluetoothGetLocalOutOfBandPairingDataFunction() {}
- void ReadCallback(
- const device::BluetoothOutOfBandPairingData& data);
+ void ReadCallback(const device::BluetoothOutOfBandPairingData& data);
void ErrorCallback();
// BluetoothExtensionFunction:
virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
};
+class BluetoothSetOutOfBandPairingDataFunction
+ : public BluetoothExtensionFunction {
+ public:
+ DECLARE_EXTENSION_FUNCTION("bluetooth.setOutOfBandPairingData",
+ BLUETOOTH_SETOUTOFBANDPAIRINGDATA)
+
+ protected:
+ virtual ~BluetoothSetOutOfBandPairingDataFunction() {}
+
+ void OnSuccessCallback();
+ void OnErrorCallback();
+
+ // BluetoothExtensionFunction:
+ virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
+};
+
class BluetoothStartDiscoveryFunction : public BluetoothExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("bluetooth.startDiscovery",
« no previous file with comments | « no previous file | chrome/browser/extensions/api/bluetooth/bluetooth_api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698