| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_socket.h" | |
| 13 #include "chrome/browser/extensions/api/bluetooth/bluetooth_extension_function.h
" | 12 #include "chrome/browser/extensions/api/bluetooth/bluetooth_extension_function.h
" |
| 14 #include "chrome/common/extensions/api/bluetooth.h" | 13 #include "chrome/common/extensions/api/bluetooth.h" |
| 15 #include "device/bluetooth/bluetooth_device.h" | 14 #include "device/bluetooth/bluetooth_device.h" |
| 16 #include "device/bluetooth/bluetooth_profile.h" | |
| 17 #include "device/bluetooth/bluetooth_socket.h" | |
| 18 #include "device/bluetooth/bluetooth_uuid.h" | |
| 19 #include "extensions/browser/api/api_resource_manager.h" | |
| 20 #include "extensions/browser/api/async_api_function.h" | |
| 21 #include "extensions/browser/browser_context_keyed_api_factory.h" | 15 #include "extensions/browser/browser_context_keyed_api_factory.h" |
| 22 #include "extensions/browser/event_router.h" | 16 #include "extensions/browser/event_router.h" |
| 23 #include "extensions/browser/extension_function.h" | 17 #include "extensions/browser/extension_function.h" |
| 24 | 18 |
| 25 namespace content { | 19 namespace content { |
| 26 class BrowserContext; | 20 class BrowserContext; |
| 27 } | 21 } |
| 28 | 22 |
| 29 namespace device { | 23 namespace device { |
| 30 class BluetoothAdapter; | 24 class BluetoothAdapter; |
| 31 } | 25 } |
| 32 | 26 |
| 33 namespace net { | |
| 34 class IOBuffer; | |
| 35 } | |
| 36 | |
| 37 namespace extensions { | 27 namespace extensions { |
| 38 | 28 |
| 39 class BluetoothEventRouter; | 29 class BluetoothEventRouter; |
| 40 | 30 |
| 41 // The profile-keyed service that manages the bluetooth extension API. | 31 // The profile-keyed service that manages the bluetooth extension API. |
| 42 // All methods of this class must be called on the UI thread. | 32 // All methods of this class must be called on the UI thread. |
| 43 // TODO(rpaquay): Rename this and move to separate file. | 33 // TODO(rpaquay): Rename this and move to separate file. |
| 44 class BluetoothAPI : public BrowserContextKeyedAPI, | 34 class BluetoothAPI : public BrowserContextKeyedAPI, |
| 45 public EventRouter::Observer { | 35 public EventRouter::Observer { |
| 46 public: | 36 public: |
| 47 typedef ApiResourceManager<BluetoothApiSocket>::ApiResourceData SocketData; | |
| 48 | |
| 49 struct ConnectionParams { | |
| 50 ConnectionParams(); | |
| 51 ~ConnectionParams(); | |
| 52 | |
| 53 content::BrowserThread::ID thread_id; | |
| 54 void* browser_context_id; | |
| 55 std::string extension_id; | |
| 56 std::string device_address; | |
| 57 device::BluetoothUUID uuid; | |
| 58 scoped_refptr<device::BluetoothSocket> socket; | |
| 59 scoped_refptr<SocketData> socket_data; | |
| 60 }; | |
| 61 | |
| 62 // Convenience method to get the BluetoothAPI for a browser context. | 37 // Convenience method to get the BluetoothAPI for a browser context. |
| 63 static BluetoothAPI* Get(content::BrowserContext* context); | 38 static BluetoothAPI* Get(content::BrowserContext* context); |
| 64 | 39 |
| 65 static BrowserContextKeyedAPIFactory<BluetoothAPI>* GetFactoryInstance(); | 40 static BrowserContextKeyedAPIFactory<BluetoothAPI>* GetFactoryInstance(); |
| 66 | 41 |
| 67 explicit BluetoothAPI(content::BrowserContext* context); | 42 explicit BluetoothAPI(content::BrowserContext* context); |
| 68 virtual ~BluetoothAPI(); | 43 virtual ~BluetoothAPI(); |
| 69 | 44 |
| 70 BluetoothEventRouter* event_router(); | 45 BluetoothEventRouter* event_router(); |
| 71 scoped_refptr<SocketData> socket_data(); | |
| 72 | 46 |
| 73 // KeyedService implementation. | 47 // KeyedService implementation. |
| 74 virtual void Shutdown() OVERRIDE; | 48 virtual void Shutdown() OVERRIDE; |
| 75 | 49 |
| 76 // EventRouter::Observer implementation. | 50 // EventRouter::Observer implementation. |
| 77 virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE; | 51 virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE; |
| 78 virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE; | 52 virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE; |
| 79 | 53 |
| 80 // Dispatch an event that takes a connection socket as a parameter to the | |
| 81 // extension that registered the profile that the socket has connected to. | |
| 82 void DispatchConnectionEvent(const std::string& extension_id, | |
| 83 const device::BluetoothUUID& profile_uuid, | |
| 84 const device::BluetoothDevice* device, | |
| 85 scoped_refptr<device::BluetoothSocket> socket); | |
| 86 | |
| 87 private: | 54 private: |
| 88 static void RegisterSocket(const ConnectionParams& params); | |
| 89 static void RegisterSocketUI(const ConnectionParams& params, int socket_id); | |
| 90 static void RegisterSocketWithAdapterUI( | |
| 91 const ConnectionParams& params, | |
| 92 int socket_id, | |
| 93 scoped_refptr<device::BluetoothAdapter> adapter); | |
| 94 | |
| 95 // BrowserContextKeyedAPI implementation. | 55 // BrowserContextKeyedAPI implementation. |
| 96 friend class BrowserContextKeyedAPIFactory<BluetoothAPI>; | 56 friend class BrowserContextKeyedAPIFactory<BluetoothAPI>; |
| 97 static const char* service_name() { return "BluetoothAPI"; } | 57 static const char* service_name() { return "BluetoothAPI"; } |
| 98 static const bool kServiceRedirectedInIncognito = true; | 58 static const bool kServiceRedirectedInIncognito = true; |
| 99 static const bool kServiceIsNULLWhileTesting = true; | 59 static const bool kServiceIsNULLWhileTesting = true; |
| 100 | 60 |
| 101 content::BrowserContext* browser_context_; | 61 content::BrowserContext* browser_context_; |
| 102 | 62 |
| 103 // Created lazily on first access. | 63 // Created lazily on first access. |
| 104 scoped_ptr<BluetoothEventRouter> event_router_; | 64 scoped_ptr<BluetoothEventRouter> event_router_; |
| 105 scoped_refptr<SocketData> socket_data_; | |
| 106 }; | 65 }; |
| 107 | 66 |
| 108 namespace api { | 67 namespace api { |
| 109 | 68 |
| 110 class BluetoothGetAdapterStateFunction : public BluetoothExtensionFunction { | 69 class BluetoothGetAdapterStateFunction : public BluetoothExtensionFunction { |
| 111 public: | 70 public: |
| 112 DECLARE_EXTENSION_FUNCTION("bluetooth.getAdapterState", | 71 DECLARE_EXTENSION_FUNCTION("bluetooth.getAdapterState", |
| 113 BLUETOOTH_GETADAPTERSTATE) | 72 BLUETOOTH_GETADAPTERSTATE) |
| 114 | 73 |
| 115 protected: | 74 protected: |
| (...skipping 18 matching lines...) Expand all Loading... |
| 134 public: | 93 public: |
| 135 DECLARE_EXTENSION_FUNCTION("bluetooth.getDevice", BLUETOOTH_GETDEVICE) | 94 DECLARE_EXTENSION_FUNCTION("bluetooth.getDevice", BLUETOOTH_GETDEVICE) |
| 136 | 95 |
| 137 // BluetoothExtensionFunction: | 96 // BluetoothExtensionFunction: |
| 138 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE; | 97 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE; |
| 139 | 98 |
| 140 protected: | 99 protected: |
| 141 virtual ~BluetoothGetDeviceFunction(); | 100 virtual ~BluetoothGetDeviceFunction(); |
| 142 }; | 101 }; |
| 143 | 102 |
| 144 class BluetoothAddProfileFunction : public AsyncExtensionFunction { | |
| 145 public: | |
| 146 DECLARE_EXTENSION_FUNCTION("bluetooth.addProfile", BLUETOOTH_ADDPROFILE) | |
| 147 | |
| 148 BluetoothAddProfileFunction(); | |
| 149 | |
| 150 protected: | |
| 151 virtual ~BluetoothAddProfileFunction(); | |
| 152 virtual bool RunAsync() OVERRIDE; | |
| 153 | |
| 154 virtual void RegisterProfile( | |
| 155 const device::BluetoothProfile::Options& options, | |
| 156 const device::BluetoothProfile::ProfileCallback& callback); | |
| 157 | |
| 158 private: | |
| 159 void OnProfileRegistered(device::BluetoothProfile* bluetooth_profile); | |
| 160 | |
| 161 device::BluetoothUUID uuid_; | |
| 162 }; | |
| 163 | |
| 164 class BluetoothRemoveProfileFunction : public SyncExtensionFunction { | |
| 165 public: | |
| 166 DECLARE_EXTENSION_FUNCTION("bluetooth.removeProfile", | |
| 167 BLUETOOTH_REMOVEPROFILE) | |
| 168 | |
| 169 protected: | |
| 170 virtual ~BluetoothRemoveProfileFunction(); | |
| 171 virtual bool RunSync() OVERRIDE; | |
| 172 }; | |
| 173 | |
| 174 class BluetoothConnectFunction : public BluetoothExtensionFunction { | |
| 175 public: | |
| 176 DECLARE_EXTENSION_FUNCTION("bluetooth.connect", BLUETOOTH_CONNECT) | |
| 177 | |
| 178 protected: | |
| 179 virtual ~BluetoothConnectFunction(); | |
| 180 | |
| 181 // BluetoothExtensionFunction: | |
| 182 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE; | |
| 183 | |
| 184 private: | |
| 185 void OnConnectedCallback(scoped_refptr<device::BluetoothAdapter> adapter, | |
| 186 const std::string& device_address); | |
| 187 void OnMonitorStartedCallback(); | |
| 188 void OnErrorCallback(const std::string& error); | |
| 189 }; | |
| 190 | |
| 191 class BluetoothDisconnectFunction : public AsyncExtensionFunction { | |
| 192 public: | |
| 193 DECLARE_EXTENSION_FUNCTION("bluetooth.disconnect", BLUETOOTH_DISCONNECT) | |
| 194 | |
| 195 protected: | |
| 196 virtual ~BluetoothDisconnectFunction() {} | |
| 197 | |
| 198 // AsyncExtensionFunction: | |
| 199 virtual bool RunAsync() OVERRIDE; | |
| 200 }; | |
| 201 | |
| 202 class BluetoothSendFunction : public AsyncExtensionFunction { | |
| 203 public: | |
| 204 DECLARE_EXTENSION_FUNCTION("bluetooth.send", BLUETOOTH_WRITE) | |
| 205 | |
| 206 protected: | |
| 207 virtual ~BluetoothSendFunction() {} | |
| 208 | |
| 209 // AsyncExtensionFunction: | |
| 210 virtual bool RunAsync() OVERRIDE; | |
| 211 }; | |
| 212 | |
| 213 class BluetoothUpdateSocketFunction : public AsyncExtensionFunction { | |
| 214 public: | |
| 215 DECLARE_EXTENSION_FUNCTION("bluetooth.updateSocket", BLUETOOTH_UPDATE_SOCKET) | |
| 216 | |
| 217 protected: | |
| 218 virtual ~BluetoothUpdateSocketFunction() {} | |
| 219 | |
| 220 // AsyncExtensionFunction: | |
| 221 virtual bool RunAsync() OVERRIDE; | |
| 222 }; | |
| 223 | |
| 224 class BluetoothSetSocketPausedFunction : public AsyncExtensionFunction { | |
| 225 public: | |
| 226 DECLARE_EXTENSION_FUNCTION("bluetooth.setSocketPaused", | |
| 227 BLUETOOTH_SET_SOCKET_PAUSED) | |
| 228 | |
| 229 protected: | |
| 230 virtual ~BluetoothSetSocketPausedFunction() {} | |
| 231 | |
| 232 // AsyncExtensionFunction: | |
| 233 virtual bool RunAsync() OVERRIDE; | |
| 234 }; | |
| 235 | |
| 236 class BluetoothGetSocketFunction : public AsyncExtensionFunction { | |
| 237 public: | |
| 238 DECLARE_EXTENSION_FUNCTION("bluetooth.getSocket", BLUETOOTH_GET_SOCKET) | |
| 239 | |
| 240 protected: | |
| 241 virtual ~BluetoothGetSocketFunction() {} | |
| 242 | |
| 243 // AsyncExtensionFunction: | |
| 244 virtual bool RunAsync() OVERRIDE; | |
| 245 }; | |
| 246 | |
| 247 class BluetoothGetSocketsFunction : public AsyncExtensionFunction { | |
| 248 public: | |
| 249 DECLARE_EXTENSION_FUNCTION("bluetooth.getSockets", BLUETOOTH_GET_SOCKETS) | |
| 250 | |
| 251 protected: | |
| 252 virtual ~BluetoothGetSocketsFunction() {} | |
| 253 | |
| 254 // AsyncExtensionFunction: | |
| 255 virtual bool RunAsync() OVERRIDE; | |
| 256 }; | |
| 257 | |
| 258 class BluetoothStartDiscoveryFunction : public BluetoothExtensionFunction { | 103 class BluetoothStartDiscoveryFunction : public BluetoothExtensionFunction { |
| 259 public: | 104 public: |
| 260 DECLARE_EXTENSION_FUNCTION("bluetooth.startDiscovery", | 105 DECLARE_EXTENSION_FUNCTION("bluetooth.startDiscovery", |
| 261 BLUETOOTH_STARTDISCOVERY) | 106 BLUETOOTH_STARTDISCOVERY) |
| 262 | 107 |
| 263 protected: | 108 protected: |
| 264 virtual ~BluetoothStartDiscoveryFunction() {} | 109 virtual ~BluetoothStartDiscoveryFunction() {} |
| 265 | 110 |
| 266 // BluetoothExtensionFunction: | 111 // BluetoothExtensionFunction: |
| 267 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE; | 112 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE; |
| (...skipping 15 matching lines...) Expand all Loading... |
| 283 | 128 |
| 284 private: | 129 private: |
| 285 void OnSuccessCallback(); | 130 void OnSuccessCallback(); |
| 286 void OnErrorCallback(); | 131 void OnErrorCallback(); |
| 287 }; | 132 }; |
| 288 | 133 |
| 289 } // namespace api | 134 } // namespace api |
| 290 } // namespace extensions | 135 } // namespace extensions |
| 291 | 136 |
| 292 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_H_ | 137 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_H_ |
| OLD | NEW |