1 // Copyright 2014 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 #ifndef CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_API_H_ 6 #define CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_API_H_ 7 8 #include <string> 9 10 #include "base/containers/hash_tables.h" 11 #include "base/memory/ref_counted.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "chrome/browser/extensions/api/bluetooth_socket/bluetooth_api_socket.h" 14 #include "chrome/common/extensions/api/bluetooth_socket.h" 15 #include "device/bluetooth/bluetooth_adapter.h" 16 #include "extensions/browser/api/api_resource_manager.h" 17 #include "extensions/browser/api/async_api_function.h" 18 #include "extensions/browser/extension_function.h" 19 #include "extensions/browser/extension_function_histogram_value.h" 20 21 namespace device { 22 class BluetoothSocket; 23 } 24 25 namespace net { 26 class IOBuffer; 27 } 28 29 namespace extensions { 30 31 namespace api { 32 33 class BluetoothSocketEventDispatcher; 34 35 // Asynchronous API function that performs its work on the BluetoothApiSocket 36 // thread while providing methods to manage resources of that class. This 37 // follows the pattern of AsyncApiFunction, but does not derive from it, 38 // because BluetoothApiSocket methods must be called on the UI Thread. 39 class BluetoothSocketAsyncApiFunction : public AsyncExtensionFunction { 40 public: 41 BluetoothSocketAsyncApiFunction(); 42 43 protected: 44 virtual ~BluetoothSocketAsyncApiFunction(); 45 46 // AsyncExtensionFunction: 47 virtual bool RunAsync() OVERRIDE; 48 49 bool PrePrepare(); 50 bool Respond(); 51 void AsyncWorkCompleted(); 52 53 virtual bool Prepare() = 0; 54 virtual void Work(); 55 virtual void AsyncWorkStart(); 56 57 content::BrowserThread::ID work_thread_id() const; 58 59 int AddSocket(BluetoothApiSocket* socket); 60 BluetoothApiSocket* GetSocket(int api_resource_id); 61 void RemoveSocket(int api_resource_id); 62 base::hash_set<int>* GetSocketIds(); 63 64 private: 65 ApiResourceManager<BluetoothApiSocket>* manager_; 66 }; 67 68 class BluetoothSocketCreateFunction : public BluetoothSocketAsyncApiFunction { 69 public: 70 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.create", BLUETOOTHSOCKET_CREATE); 71 72 BluetoothSocketCreateFunction(); 73 74 protected: 75 virtual ~BluetoothSocketCreateFunction(); 76 77 // BluetoothSocketAsyncApiFunction: 78 virtual bool Prepare() OVERRIDE; 79 virtual void Work() OVERRIDE; 80 81 private: 82 scoped_ptr<bluetooth_socket::Create::Params> params_; 83 }; 84 85 class BluetoothSocketUpdateFunction : public BluetoothSocketAsyncApiFunction { 86 public: 87 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.update", BLUETOOTHSOCKET_UPDATE); 88 89 BluetoothSocketUpdateFunction(); 90 91 protected: 92 virtual ~BluetoothSocketUpdateFunction(); 93 94 // BluetoothSocketAsyncApiFunction: 95 virtual bool Prepare() OVERRIDE; 96 virtual void Work() OVERRIDE; 97 98 private: 99 scoped_ptr<bluetooth_socket::Update::Params> params_; 100 }; 101 102 class BluetoothSocketSetPausedFunction 103 : public BluetoothSocketAsyncApiFunction { 104 public: 105 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.setPaused", 106 BLUETOOTHSOCKET_SETPAUSED); 107 108 BluetoothSocketSetPausedFunction(); 109 110 protected: 111 virtual ~BluetoothSocketSetPausedFunction(); 112 113 // BluetoothSocketAsyncApiFunction: 114 virtual bool Prepare() OVERRIDE; 115 virtual void Work() OVERRIDE; 116 117 private: 118 scoped_ptr<bluetooth_socket::SetPaused::Params> params_; 119 BluetoothSocketEventDispatcher* socket_event_dispatcher_; 120 }; 121 122 class BluetoothSocketListenFunction : public BluetoothSocketAsyncApiFunction { 123 public: 124 BluetoothSocketListenFunction(); 125 126 virtual bool CreateParams() = 0; 127 virtual void CreateService( 128 scoped_refptr<device::BluetoothAdapter> adapter, 129 const device::BluetoothUUID& uuid, 130 const device::BluetoothAdapter::CreateServiceCallback& callback, 131 const device::BluetoothAdapter::CreateServiceErrorCallback& 132 error_callback) = 0; 133 virtual void CreateResults() = 0; 134 135 virtual int socket_id() const = 0; 136 virtual const std::string& uuid() const = 0; 137 138 // BluetoothSocketAsyncApiFunction: 139 virtual bool Prepare() OVERRIDE; 140 virtual void AsyncWorkStart() OVERRIDE; 141 142 protected: 143 virtual ~BluetoothSocketListenFunction(); 144 145 virtual void OnGetAdapter(scoped_refptr<device::BluetoothAdapter> adapter); 146 virtual void OnCreateService(scoped_refptr<device::BluetoothSocket> socket); 147 virtual void OnCreateServiceError(const std::string& message); 148 149 BluetoothSocketEventDispatcher* socket_event_dispatcher_; 150 }; 151 152 class BluetoothSocketListenUsingRfcommFunction 153 : public BluetoothSocketListenFunction { 154 public: 155 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.listenUsingRfcomm", 156 BLUETOOTHSOCKET_LISTENUSINGRFCOMM); 157 158 BluetoothSocketListenUsingRfcommFunction(); 159 160 // BluetoothSocketListenFunction: 161 virtual int socket_id() const OVERRIDE; 162 virtual const std::string& uuid() const OVERRIDE; 163 164 virtual bool CreateParams() OVERRIDE; 165 virtual void CreateService( 166 scoped_refptr<device::BluetoothAdapter> adapter, 167 const device::BluetoothUUID& uuid, 168 const device::BluetoothAdapter::CreateServiceCallback& callback, 169 const device::BluetoothAdapter::CreateServiceErrorCallback& 170 error_callback) OVERRIDE; 171 virtual void CreateResults() OVERRIDE; 172 173 protected: 174 virtual ~BluetoothSocketListenUsingRfcommFunction(); 175 176 private: 177 scoped_ptr<bluetooth_socket::ListenUsingRfcomm::Params> params_; 178 }; 179 180 class BluetoothSocketListenUsingL2capFunction 181 : public BluetoothSocketListenFunction { 182 public: 183 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.listenUsingL2cap", 184 BLUETOOTHSOCKET_LISTENUSINGL2CAP); 185 186 BluetoothSocketListenUsingL2capFunction(); 187 188 // BluetoothSocketListenFunction: 189 virtual int socket_id() const OVERRIDE; 190 virtual const std::string& uuid() const OVERRIDE; 191 192 virtual bool CreateParams() OVERRIDE; 193 virtual void CreateService( 194 scoped_refptr<device::BluetoothAdapter> adapter, 195 const device::BluetoothUUID& uuid, 196 const device::BluetoothAdapter::CreateServiceCallback& callback, 197 const device::BluetoothAdapter::CreateServiceErrorCallback& 198 error_callback) OVERRIDE; 199 virtual void CreateResults() OVERRIDE; 200 201 protected: 202 virtual ~BluetoothSocketListenUsingL2capFunction(); 203 204 private: 205 scoped_ptr<bluetooth_socket::ListenUsingL2cap::Params> params_; 206 }; 207 208 class BluetoothSocketConnectFunction : public BluetoothSocketAsyncApiFunction { 209 public: 210 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.connect", 211 BLUETOOTHSOCKET_CONNECT); 212 213 BluetoothSocketConnectFunction(); 214 215 protected: 216 virtual ~BluetoothSocketConnectFunction(); 217 218 // BluetoothSocketAsyncApiFunction: 219 virtual bool Prepare() OVERRIDE; 220 virtual void AsyncWorkStart() OVERRIDE; 221 222 private: 223 virtual void OnGetAdapter(scoped_refptr<device::BluetoothAdapter> adapter); 224 virtual void OnConnect(scoped_refptr<device::BluetoothSocket> socket); 225 virtual void OnConnectError(const std::string& message); 226 227 scoped_ptr<bluetooth_socket::Connect::Params> params_; 228 BluetoothSocketEventDispatcher* socket_event_dispatcher_; 229 }; 230 231 class BluetoothSocketDisconnectFunction 232 : public BluetoothSocketAsyncApiFunction { 233 public: 234 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.disconnect", 235 BLUETOOTHSOCKET_DISCONNECT); 236 237 BluetoothSocketDisconnectFunction(); 238 239 protected: 240 virtual ~BluetoothSocketDisconnectFunction(); 241 242 // BluetoothSocketAsyncApiFunction: 243 virtual bool Prepare() OVERRIDE; 244 virtual void AsyncWorkStart() OVERRIDE; 245 246 private: 247 virtual void OnSuccess(); 248 249 scoped_ptr<bluetooth_socket::Disconnect::Params> params_; 250 }; 251 252 class BluetoothSocketCloseFunction : public BluetoothSocketAsyncApiFunction { 253 public: 254 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.close", BLUETOOTHSOCKET_CLOSE); 255 256 BluetoothSocketCloseFunction(); 257 258 protected: 259 virtual ~BluetoothSocketCloseFunction(); 260 261 // BluetoothSocketAsyncApiFunction: 262 virtual bool Prepare() OVERRIDE; 263 virtual void Work() OVERRIDE; 264 265 private: 266 scoped_ptr<bluetooth_socket::Close::Params> params_; 267 }; 268 269 class BluetoothSocketSendFunction : public BluetoothSocketAsyncApiFunction { 270 public: 271 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.send", BLUETOOTHSOCKET_SEND); 272 273 BluetoothSocketSendFunction(); 274 275 protected: 276 virtual ~BluetoothSocketSendFunction(); 277 278 // BluetoothSocketAsyncApiFunction: 279 virtual bool Prepare() OVERRIDE; 280 virtual void AsyncWorkStart() OVERRIDE; 281 282 private: 283 void OnSuccess(int bytes_sent); 284 void OnError(BluetoothApiSocket::ErrorReason reason, 285 const std::string& message); 286 287 scoped_ptr<bluetooth_socket::Send::Params> params_; 288 scoped_refptr<net::IOBuffer> io_buffer_; 289 size_t io_buffer_size_; 290 }; 291 292 class BluetoothSocketGetInfoFunction : public BluetoothSocketAsyncApiFunction { 293 public: 294 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.getInfo", 295 BLUETOOTHSOCKET_GETINFO); 296 297 BluetoothSocketGetInfoFunction(); 298 299 protected: 300 virtual ~BluetoothSocketGetInfoFunction(); 301 302 // BluetoothSocketAsyncApiFunction: 303 virtual bool Prepare() OVERRIDE; 304 virtual void Work() OVERRIDE; 305 306 private: 307 scoped_ptr<bluetooth_socket::GetInfo::Params> params_; 308 }; 309 310 class BluetoothSocketGetSocketsFunction 311 : public BluetoothSocketAsyncApiFunction { 312 public: 313 DECLARE_EXTENSION_FUNCTION("bluetoothSocket.getSockets", 314 BLUETOOTHSOCKET_GETSOCKETS); 315 316 BluetoothSocketGetSocketsFunction(); 317 318 protected: 319 virtual ~BluetoothSocketGetSocketsFunction(); 320 321 // BluetoothSocketAsyncApiFunction: 322 virtual bool Prepare() OVERRIDE; 323 virtual void Work() OVERRIDE; 324 }; 325 326 } // namespace api 327 } // namespace extensions 328 329 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_SOCKET_BLUETOOTH_SOCKET_API_H_ 330