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

Side by Side 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: Address code review feedback. Created 6 years, 9 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 unified diff | Download patch
OLDNEW
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"
12 #include "chrome/browser/extensions/api/bluetooth/bluetooth_extension_function.h " 13 #include "chrome/browser/extensions/api/bluetooth/bluetooth_extension_function.h "
14 #include "chrome/common/extensions/api/bluetooth.h"
13 #include "device/bluetooth/bluetooth_device.h" 15 #include "device/bluetooth/bluetooth_device.h"
14 #include "device/bluetooth/bluetooth_profile.h" 16 #include "device/bluetooth/bluetooth_profile.h"
17 #include "device/bluetooth/bluetooth_socket.h"
18 #include "extensions/browser/api/api_resource_manager.h"
15 #include "extensions/browser/api/async_api_function.h" 19 #include "extensions/browser/api/async_api_function.h"
16 #include "extensions/browser/browser_context_keyed_api_factory.h" 20 #include "extensions/browser/browser_context_keyed_api_factory.h"
17 #include "extensions/browser/event_router.h" 21 #include "extensions/browser/event_router.h"
18 #include "extensions/browser/extension_function.h" 22 #include "extensions/browser/extension_function.h"
19 23
20 namespace content { 24 namespace content {
21 class BrowserContext; 25 class BrowserContext;
22 } 26 }
23 27
24 namespace device { 28 namespace device {
29 class BluetoothAdapter;
30 struct BluetoothOutOfBandPairingData;
31 }
25 32
26 class BluetoothAdapter; 33 namespace net {
27 class BluetoothDevice; 34 class IOBuffer;
28 class BluetoothSocket; 35 }
29 struct BluetoothOutOfBandPairingData;
30
31 } // namespace device
32 36
33 namespace extensions { 37 namespace extensions {
34 38
35 class ExtensionBluetoothEventRouter; 39 class ExtensionBluetoothEventRouter;
36 40
37 // The profile-keyed service that manages the bluetooth extension API. 41 // The profile-keyed service that manages the bluetooth extension API.
42 // All methods of this class must be called on the UI thread.
43 // TODO(rpaquay): Rename this and move to separate file.
38 class BluetoothAPI : public BrowserContextKeyedAPI, 44 class BluetoothAPI : public BrowserContextKeyedAPI,
39 public EventRouter::Observer { 45 public EventRouter::Observer {
40 public: 46 public:
41 // Convenience method to get the BluetoothAPI for a profile. 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 std::string profile_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.
42 static BluetoothAPI* Get(content::BrowserContext* context); 63 static BluetoothAPI* Get(content::BrowserContext* context);
43 64
44 static BrowserContextKeyedAPIFactory<BluetoothAPI>* GetFactoryInstance(); 65 static BrowserContextKeyedAPIFactory<BluetoothAPI>* GetFactoryInstance();
45 66
46 explicit BluetoothAPI(content::BrowserContext* context); 67 explicit BluetoothAPI(content::BrowserContext* context);
47 virtual ~BluetoothAPI(); 68 virtual ~BluetoothAPI();
48 69
49 ExtensionBluetoothEventRouter* bluetooth_event_router(); 70 ExtensionBluetoothEventRouter* event_router();
71 scoped_refptr<SocketData> socket_data();
72 scoped_refptr<api::BluetoothSocketEventDispatcher> socket_event_dispatcher();
50 73
51 // KeyedService implementation. 74 // KeyedService implementation.
52 virtual void Shutdown() OVERRIDE; 75 virtual void Shutdown() OVERRIDE;
53 76
54 // EventRouter::Observer implementation. 77 // EventRouter::Observer implementation.
55 virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE; 78 virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE;
56 virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE; 79 virtual void OnListenerRemoved(const EventListenerInfo& details) OVERRIDE;
57 80
81 // Dispatch an event that takes a connection socket as a parameter to the
82 // extension that registered the profile that the socket has connected to.
83 void DispatchConnectionEvent(const std::string& extension_id,
84 const std::string& profile_uuid,
85 const device::BluetoothDevice* device,
86 scoped_refptr<device::BluetoothSocket> socket);
87
58 private: 88 private:
59 friend class BrowserContextKeyedAPIFactory<BluetoothAPI>; 89 static void InitializeBluetoothAdapter(device::BluetoothAdapter* adapter);
90 static void RegisterSocket(const ConnectionParams& params);
91 static void RegisterSocketUI(const ConnectionParams& params, int socket_id);
92 static void RegisterSocketWithAdapterUI(
93 const ConnectionParams& params,
94 int socket_id,
95 scoped_refptr<device::BluetoothAdapter> adapter);
60 96
61 // BrowserContextKeyedAPI implementation. 97 // BrowserContextKeyedAPI implementation.
98 friend class BrowserContextKeyedAPIFactory<BluetoothAPI>;
62 static const char* service_name() { return "BluetoothAPI"; } 99 static const char* service_name() { return "BluetoothAPI"; }
63 static const bool kServiceRedirectedInIncognito = true; 100 static const bool kServiceRedirectedInIncognito = true;
64 static const bool kServiceIsNULLWhileTesting = true; 101 static const bool kServiceIsNULLWhileTesting = true;
65 102
66 content::BrowserContext* browser_context_; 103 content::BrowserContext* browser_context_;
67 104
68 // Created lazily on first access. 105 // Created lazily on first access.
69 scoped_ptr<ExtensionBluetoothEventRouter> bluetooth_event_router_; 106 scoped_ptr<ExtensionBluetoothEventRouter> event_router_;
107 scoped_refptr<SocketData> socket_data_;
108 scoped_refptr<api::BluetoothSocketEventDispatcher> socket_event_dispatcher_;
70 }; 109 };
71 110
72 namespace api { 111 namespace api {
73 112
113 class BluetoothSocketEventDispatcher;
114
115 class BluetoothSocketApiFunction : public AsyncApiFunction {
116 public:
117 BluetoothSocketApiFunction();
118
119 protected:
120 virtual ~BluetoothSocketApiFunction();
121
122 // AsyncApiFunction:
123 virtual bool PrePrepare() OVERRIDE;
124 virtual bool Respond() OVERRIDE;
125
126 scoped_refptr<BluetoothAPI::SocketData> socket_data_;
127 scoped_refptr<api::BluetoothSocketEventDispatcher> socket_event_dispatcher_;
128 };
129
130 class BluetoothGetAdapterStateFunction : public BluetoothExtensionFunction {
131 public:
132 DECLARE_EXTENSION_FUNCTION("bluetooth.getAdapterState",
133 BLUETOOTH_GETADAPTERSTATE)
134
135 protected:
136 virtual ~BluetoothGetAdapterStateFunction();
137
138 // BluetoothExtensionFunction:
139 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
140 };
141
142 class BluetoothGetDevicesFunction : public BluetoothExtensionFunction {
143 public:
144 DECLARE_EXTENSION_FUNCTION("bluetooth.getDevices", BLUETOOTH_GETDEVICES)
145
146 protected:
147 virtual ~BluetoothGetDevicesFunction();
148
149 // BluetoothExtensionFunction:
150 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
151 };
152
74 class BluetoothAddProfileFunction : public UIThreadExtensionFunction { 153 class BluetoothAddProfileFunction : public UIThreadExtensionFunction {
75 public: 154 public:
76 DECLARE_EXTENSION_FUNCTION("bluetooth.addProfile", BLUETOOTH_ADDPROFILE) 155 DECLARE_EXTENSION_FUNCTION("bluetooth.addProfile", BLUETOOTH_ADDPROFILE)
77 156
78 BluetoothAddProfileFunction(); 157 BluetoothAddProfileFunction();
79 158
80 protected: 159 protected:
81 virtual ~BluetoothAddProfileFunction() {} 160 virtual ~BluetoothAddProfileFunction();
82 virtual bool RunImpl() OVERRIDE; 161 virtual bool RunImpl() OVERRIDE;
83 162
84 virtual void RegisterProfile( 163 virtual void RegisterProfile(
85 const device::BluetoothProfile::Options& options, 164 const device::BluetoothProfile::Options& options,
86 const device::BluetoothProfile::ProfileCallback& callback); 165 const device::BluetoothProfile::ProfileCallback& callback);
87 166
88 private: 167 private:
89 void OnProfileRegistered(device::BluetoothProfile* bluetooth_profile); 168 void OnProfileRegistered(device::BluetoothProfile* bluetooth_profile);
90 169
91 std::string uuid_; 170 std::string uuid_;
92 }; 171 };
93 172
94 class BluetoothRemoveProfileFunction : public SyncExtensionFunction { 173 class BluetoothRemoveProfileFunction : public SyncExtensionFunction {
95 public: 174 public:
96 DECLARE_EXTENSION_FUNCTION("bluetooth.removeProfile", 175 DECLARE_EXTENSION_FUNCTION("bluetooth.removeProfile",
97 BLUETOOTH_REMOVEPROFILE) 176 BLUETOOTH_REMOVEPROFILE)
98 177
99 protected: 178 protected:
100 virtual ~BluetoothRemoveProfileFunction() {} 179 virtual ~BluetoothRemoveProfileFunction();
101 virtual bool RunImpl() OVERRIDE; 180 virtual bool RunImpl() OVERRIDE;
102 }; 181 };
103 182
104 class BluetoothGetProfilesFunction : public BluetoothExtensionFunction { 183 class BluetoothGetProfilesFunction : public BluetoothExtensionFunction {
105 public: 184 public:
106 DECLARE_EXTENSION_FUNCTION("bluetooth.getProfiles", BLUETOOTH_GETPROFILES) 185 DECLARE_EXTENSION_FUNCTION("bluetooth.getProfiles", BLUETOOTH_GETPROFILES)
107 186
108 protected: 187 protected:
109 virtual ~BluetoothGetProfilesFunction() {} 188 virtual ~BluetoothGetProfilesFunction();
110 189
111 // BluetoothExtensionFunction: 190 // BluetoothExtensionFunction:
112 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE; 191 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
113 }; 192 };
114 193
115 class BluetoothGetAdapterStateFunction : public BluetoothExtensionFunction {
116 public:
117 DECLARE_EXTENSION_FUNCTION("bluetooth.getAdapterState",
118 BLUETOOTH_GETADAPTERSTATE)
119
120 protected:
121 virtual ~BluetoothGetAdapterStateFunction() {}
122
123 // BluetoothExtensionFunction:
124 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
125 };
126
127 class BluetoothGetDevicesFunction : public BluetoothExtensionFunction {
128 public:
129 DECLARE_EXTENSION_FUNCTION("bluetooth.getDevices", BLUETOOTH_GETDEVICES)
130
131 protected:
132 virtual ~BluetoothGetDevicesFunction() {}
133
134 // BluetoothExtensionFunction:
135 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
136 };
137
138 class BluetoothGetServicesFunction : public BluetoothExtensionFunction {
139 public:
140 DECLARE_EXTENSION_FUNCTION("bluetooth.getServices", BLUETOOTH_GETSERVICES)
141
142 protected:
143 virtual ~BluetoothGetServicesFunction() {}
144
145 // BluetoothExtensionFunction:
146 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
147
148 private:
149 void GetServiceRecordsCallback(
150 base::ListValue* services,
151 const device::BluetoothDevice::ServiceRecordList& records);
152 void OnErrorCallback();
153 };
154
155 class BluetoothConnectFunction : public BluetoothExtensionFunction { 194 class BluetoothConnectFunction : public BluetoothExtensionFunction {
156 public: 195 public:
157 DECLARE_EXTENSION_FUNCTION("bluetooth.connect", BLUETOOTH_CONNECT) 196 DECLARE_EXTENSION_FUNCTION("bluetooth.connect", BLUETOOTH_CONNECT)
158 197
159 protected: 198 protected:
160 virtual ~BluetoothConnectFunction() {} 199 virtual ~BluetoothConnectFunction();
161 200
162 // BluetoothExtensionFunction: 201 // BluetoothExtensionFunction:
163 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE; 202 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
164 203
165 private: 204 private:
166 void OnSuccessCallback(); 205 void OnSuccessCallback();
167 void OnErrorCallback(); 206 void OnErrorCallback(const std::string& error);
168 }; 207 };
169 208
170 class BluetoothDisconnectFunction : public SyncExtensionFunction { 209 class BluetoothDisconnectFunction : public BluetoothSocketApiFunction {
171 public: 210 public:
172 DECLARE_EXTENSION_FUNCTION("bluetooth.disconnect", BLUETOOTH_DISCONNECT) 211 DECLARE_EXTENSION_FUNCTION("bluetooth.disconnect", BLUETOOTH_DISCONNECT)
212 BluetoothDisconnectFunction();
173 213
174 protected: 214 protected:
175 virtual ~BluetoothDisconnectFunction() {} 215 virtual ~BluetoothDisconnectFunction();
176
177 // ExtensionFunction:
178 virtual bool RunImpl() OVERRIDE;
179 };
180
181 class BluetoothReadFunction : public AsyncApiFunction {
182 public:
183 DECLARE_EXTENSION_FUNCTION("bluetooth.read", BLUETOOTH_READ)
184 BluetoothReadFunction();
185
186 protected:
187 virtual ~BluetoothReadFunction();
188 216
189 // AsyncApiFunction: 217 // AsyncApiFunction:
190 virtual bool Prepare() OVERRIDE; 218 virtual bool Prepare() OVERRIDE;
191 virtual bool Respond() OVERRIDE; 219 virtual void AsyncWorkStart() OVERRIDE;
220
221 private:
222 void OnSuccess();
223
224 scoped_ptr<bluetooth::Disconnect::Params> params_;
225 };
226
227 class BluetoothSendFunction : public BluetoothSocketApiFunction {
228 public:
229 DECLARE_EXTENSION_FUNCTION("bluetooth.send", BLUETOOTH_SEND)
230 BluetoothSendFunction();
231
232 protected:
233 virtual ~BluetoothSendFunction();
234
235 // AsyncApiFunction:
236 virtual bool Prepare() OVERRIDE;
237 virtual void AsyncWorkStart() OVERRIDE;
238
239 private:
240 void OnSendSuccess(int bytes_sent);
241 void OnSendError(const std::string& message);
242
243 scoped_ptr<bluetooth::Send::Params> params_;
244 scoped_refptr<net::IOBuffer> io_buffer_;
245 size_t io_buffer_size_;
246 };
247
248 class BluetoothUpdateSocketFunction : public BluetoothSocketApiFunction {
249 public:
250 DECLARE_EXTENSION_FUNCTION("bluetooth.updateSocket", BLUETOOTH_UPDATE_SOCKET)
251 BluetoothUpdateSocketFunction();
252
253 protected:
254 virtual ~BluetoothUpdateSocketFunction();
255
256 // AsyncApiFunction:
257 virtual bool Prepare() OVERRIDE;
192 virtual void Work() OVERRIDE; 258 virtual void Work() OVERRIDE;
193 259
194 private: 260 private:
195 bool success_; 261 scoped_ptr<bluetooth::UpdateSocket::Params> params_;
196 scoped_refptr<device::BluetoothSocket> socket_;
197 }; 262 };
198 263
199 class BluetoothWriteFunction : public AsyncApiFunction { 264 class BluetoothSetSocketPausedFunction : public BluetoothSocketApiFunction {
200 public: 265 public:
201 DECLARE_EXTENSION_FUNCTION("bluetooth.write", BLUETOOTH_WRITE) 266 DECLARE_EXTENSION_FUNCTION("bluetooth.setSocketPaused",
202 BluetoothWriteFunction(); 267 BLUETOOTH_SET_SOCKET_PAUSED)
268 BluetoothSetSocketPausedFunction();
203 269
204 protected: 270 protected:
205 virtual ~BluetoothWriteFunction(); 271 virtual ~BluetoothSetSocketPausedFunction();
206 272
207 // AsyncApiFunction: 273 // AsyncApiFunction:
208 virtual bool Prepare() OVERRIDE; 274 virtual bool Prepare() OVERRIDE;
209 virtual bool Respond() OVERRIDE;
210 virtual void Work() OVERRIDE; 275 virtual void Work() OVERRIDE;
211 276
212 private: 277 private:
213 bool success_; 278 scoped_ptr<bluetooth::SetSocketPaused::Params> params_;
214 const base::BinaryValue* data_to_write_; // memory is owned by args_ 279 };
215 scoped_refptr<device::BluetoothSocket> socket_; 280
281 class BluetoothGetSocketFunction : public BluetoothSocketApiFunction {
282 public:
283 DECLARE_EXTENSION_FUNCTION("bluetooth.getSocket", BLUETOOTH_GET_SOCKET)
284
285 BluetoothGetSocketFunction();
286
287 protected:
288 virtual ~BluetoothGetSocketFunction();
289
290 // AsyncApiFunction:
291 virtual bool Prepare() OVERRIDE;
292 virtual void Work() OVERRIDE;
293
294 private:
295 scoped_ptr<bluetooth::GetSocket::Params> params_;
296 };
297
298 class BluetoothGetSocketsFunction : public BluetoothSocketApiFunction {
299 public:
300 DECLARE_EXTENSION_FUNCTION("bluetooth.getSockets", BLUETOOTH_GET_SOCKETS)
301
302 BluetoothGetSocketsFunction();
303
304 protected:
305 virtual ~BluetoothGetSocketsFunction();
306
307 // AsyncApiFunction:
308 virtual bool Prepare() OVERRIDE;
309 virtual void Work() OVERRIDE;
310 };
311
312 class BluetoothGetLocalOutOfBandPairingDataFunction
313 : public BluetoothExtensionFunction {
314 public:
315 DECLARE_EXTENSION_FUNCTION("bluetooth.getLocalOutOfBandPairingData",
316 BLUETOOTH_GETLOCALOUTOFBANDPAIRINGDATA)
317
318 protected:
319 virtual ~BluetoothGetLocalOutOfBandPairingDataFunction() {}
320
321 void ReadCallback(const device::BluetoothOutOfBandPairingData& data);
322 void ErrorCallback();
323
324 // BluetoothExtensionFunction:
325 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
216 }; 326 };
217 327
218 class BluetoothSetOutOfBandPairingDataFunction 328 class BluetoothSetOutOfBandPairingDataFunction
219 : public BluetoothExtensionFunction { 329 : public BluetoothExtensionFunction {
220 public: 330 public:
221 DECLARE_EXTENSION_FUNCTION("bluetooth.setOutOfBandPairingData", 331 DECLARE_EXTENSION_FUNCTION("bluetooth.setOutOfBandPairingData",
222 BLUETOOTH_SETOUTOFBANDPAIRINGDATA) 332 BLUETOOTH_SETOUTOFBANDPAIRINGDATA)
223 333
224 protected: 334 protected:
225 virtual ~BluetoothSetOutOfBandPairingDataFunction() {} 335 virtual ~BluetoothSetOutOfBandPairingDataFunction() {}
226 336
227 void OnSuccessCallback(); 337 void OnSuccessCallback();
228 void OnErrorCallback(); 338 void OnErrorCallback(const std::string& error);
229 339
230 // BluetoothExtensionFunction: 340 // BluetoothExtensionFunction:
231 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE; 341 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
232 };
233
234 class BluetoothGetLocalOutOfBandPairingDataFunction
235 : public BluetoothExtensionFunction {
236 public:
237 DECLARE_EXTENSION_FUNCTION("bluetooth.getLocalOutOfBandPairingData",
238 BLUETOOTH_GETLOCALOUTOFBANDPAIRINGDATA)
239
240 protected:
241 virtual ~BluetoothGetLocalOutOfBandPairingDataFunction() {}
242
243 void ReadCallback(
244 const device::BluetoothOutOfBandPairingData& data);
245 void ErrorCallback();
246
247 // BluetoothExtensionFunction:
248 virtual bool DoWork(scoped_refptr<device::BluetoothAdapter> adapter) OVERRIDE;
249 }; 342 };
250 343
251 class BluetoothStartDiscoveryFunction : public BluetoothExtensionFunction { 344 class BluetoothStartDiscoveryFunction : public BluetoothExtensionFunction {
252 public: 345 public:
253 DECLARE_EXTENSION_FUNCTION("bluetooth.startDiscovery", 346 DECLARE_EXTENSION_FUNCTION("bluetooth.startDiscovery",
254 BLUETOOTH_STARTDISCOVERY) 347 BLUETOOTH_STARTDISCOVERY)
255 348
256 protected: 349 protected:
257 virtual ~BluetoothStartDiscoveryFunction() {} 350 virtual ~BluetoothStartDiscoveryFunction() {}
258 351
(...skipping 17 matching lines...) Expand all
276 369
277 private: 370 private:
278 void OnSuccessCallback(); 371 void OnSuccessCallback();
279 void OnErrorCallback(); 372 void OnErrorCallback();
280 }; 373 };
281 374
282 } // namespace api 375 } // namespace api
283 } // namespace extensions 376 } // namespace extensions
284 377
285 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_H_ 378 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698