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

Side by Side Diff: content/child/bluetooth/bluetooth_dispatcher.cc

Issue 1156573005: bluetooth: Browser-side implementation of getCharacteristic (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bluetooth-get-primary-service-initial
Patch Set: Fix histograms.xml and bad_messages.h merge conflict Created 5 years, 6 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 2014 The Chromium Authors. All rights reserved. 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 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 #include "content/child/bluetooth/bluetooth_dispatcher.h" 5 #include "content/child/bluetooth/bluetooth_dispatcher.h"
6 6
7 #include "base/lazy_instance.h" 7 #include "base/lazy_instance.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "base/thread_task_runner_handle.h" 10 #include "base/thread_task_runner_handle.h"
11 #include "content/child/thread_safe_sender.h" 11 #include "content/child/thread_safe_sender.h"
12 #include "content/common/bluetooth/bluetooth_messages.h" 12 #include "content/common/bluetooth/bluetooth_messages.h"
13 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic e.h" 13 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic e.h"
14 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothError .h" 14 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothError .h"
15 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothGATTC haracteristic.h"
15 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothGATTR emoteServer.h" 16 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothGATTR emoteServer.h"
16 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothGATTS ervice.h" 17 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothGATTS ervice.h"
17 18
18 using blink::WebBluetoothConnectGATTCallbacks; 19 using blink::WebBluetoothConnectGATTCallbacks;
19 using blink::WebBluetoothDevice; 20 using blink::WebBluetoothDevice;
20 using blink::WebBluetoothError; 21 using blink::WebBluetoothError;
22 using blink::WebBluetoothGATTCharacteristic;
21 using blink::WebBluetoothGATTRemoteServer; 23 using blink::WebBluetoothGATTRemoteServer;
22 using blink::WebBluetoothGATTService; 24 using blink::WebBluetoothGATTService;
23 using blink::WebBluetoothRequestDeviceCallbacks; 25 using blink::WebBluetoothRequestDeviceCallbacks;
24 using blink::WebString; 26 using blink::WebString;
25 using blink::WebVector; 27 using blink::WebVector;
26 28
27 struct BluetoothPrimaryServiceRequest { 29 struct BluetoothPrimaryServiceRequest {
28 BluetoothPrimaryServiceRequest( 30 BluetoothPrimaryServiceRequest(
29 blink::WebString device_instance_id, 31 blink::WebString device_instance_id,
30 blink::WebString service_uuid, 32 blink::WebString service_uuid,
31 blink::WebBluetoothGetPrimaryServiceCallbacks* callbacks) 33 blink::WebBluetoothGetPrimaryServiceCallbacks* callbacks)
32 : device_instance_id(device_instance_id), 34 : device_instance_id(device_instance_id),
33 service_uuid(service_uuid), 35 service_uuid(service_uuid),
34 callbacks(callbacks) {} 36 callbacks(callbacks) {}
35 ~BluetoothPrimaryServiceRequest() {} 37 ~BluetoothPrimaryServiceRequest() {}
36 38
37 blink::WebString device_instance_id; 39 blink::WebString device_instance_id;
38 blink::WebString service_uuid; 40 blink::WebString service_uuid;
39 scoped_ptr<blink::WebBluetoothGetPrimaryServiceCallbacks> callbacks; 41 scoped_ptr<blink::WebBluetoothGetPrimaryServiceCallbacks> callbacks;
40 }; 42 };
41 43
44 struct BluetoothCharacteristicRequest {
45 BluetoothCharacteristicRequest(
46 blink::WebString service_instance_id,
47 blink::WebString characteristic_uuid,
48 blink::WebBluetoothGetCharacteristicCallbacks* callbacks)
49 : service_instance_id(service_instance_id),
50 characteristic_uuid(characteristic_uuid),
51 callbacks(callbacks) {}
52 ~BluetoothCharacteristicRequest() {}
53
54 blink::WebString service_instance_id;
55 blink::WebString characteristic_uuid;
56 scoped_ptr<blink::WebBluetoothGetCharacteristicCallbacks> callbacks;
57 };
58
42 namespace content { 59 namespace content {
43 60
44 namespace { 61 namespace {
45 62
46 base::LazyInstance<base::ThreadLocalPointer<BluetoothDispatcher>>::Leaky 63 base::LazyInstance<base::ThreadLocalPointer<BluetoothDispatcher>>::Leaky
47 g_dispatcher_tls = LAZY_INSTANCE_INITIALIZER; 64 g_dispatcher_tls = LAZY_INSTANCE_INITIALIZER;
48 65
49 BluetoothDispatcher* const kHasBeenDeleted = 66 BluetoothDispatcher* const kHasBeenDeleted =
50 reinterpret_cast<BluetoothDispatcher*>(0x1); 67 reinterpret_cast<BluetoothDispatcher*>(0x1);
51 68
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcher, msg) 133 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcher, msg)
117 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceSuccess, 134 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceSuccess,
118 OnRequestDeviceSuccess); 135 OnRequestDeviceSuccess);
119 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceError, OnRequestDeviceError); 136 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceError, OnRequestDeviceError);
120 IPC_MESSAGE_HANDLER(BluetoothMsg_ConnectGATTSuccess, OnConnectGATTSuccess); 137 IPC_MESSAGE_HANDLER(BluetoothMsg_ConnectGATTSuccess, OnConnectGATTSuccess);
121 IPC_MESSAGE_HANDLER(BluetoothMsg_ConnectGATTError, OnConnectGATTError); 138 IPC_MESSAGE_HANDLER(BluetoothMsg_ConnectGATTError, OnConnectGATTError);
122 IPC_MESSAGE_HANDLER(BluetoothMsg_GetPrimaryServiceSuccess, 139 IPC_MESSAGE_HANDLER(BluetoothMsg_GetPrimaryServiceSuccess,
123 OnGetPrimaryServiceSuccess); 140 OnGetPrimaryServiceSuccess);
124 IPC_MESSAGE_HANDLER(BluetoothMsg_GetPrimaryServiceError, 141 IPC_MESSAGE_HANDLER(BluetoothMsg_GetPrimaryServiceError,
125 OnGetPrimaryServiceError); 142 OnGetPrimaryServiceError);
143 IPC_MESSAGE_HANDLER(BluetoothMsg_GetCharacteristicSuccess,
144 OnGetCharacteristicSuccess);
145 IPC_MESSAGE_HANDLER(BluetoothMsg_GetCharacteristicError,
146 OnGetCharacteristicError);
126 IPC_MESSAGE_UNHANDLED(handled = false) 147 IPC_MESSAGE_UNHANDLED(handled = false)
127 IPC_END_MESSAGE_MAP() 148 IPC_END_MESSAGE_MAP()
128 DCHECK(handled) << "Unhandled message:" << msg.type(); 149 DCHECK(handled) << "Unhandled message:" << msg.type();
129 } 150 }
130 151
131 void BluetoothDispatcher::requestDevice( 152 void BluetoothDispatcher::requestDevice(
132 blink::WebBluetoothRequestDeviceCallbacks* callbacks) { 153 blink::WebBluetoothRequestDeviceCallbacks* callbacks) {
133 int request_id = pending_requests_.Add(callbacks); 154 int request_id = pending_requests_.Add(callbacks);
134 Send(new BluetoothHostMsg_RequestDevice(CurrentWorkerId(), request_id)); 155 Send(new BluetoothHostMsg_RequestDevice(CurrentWorkerId(), request_id));
135 } 156 }
(...skipping 11 matching lines...) Expand all
147 const blink::WebString& service_uuid, 168 const blink::WebString& service_uuid,
148 blink::WebBluetoothGetPrimaryServiceCallbacks* callbacks) { 169 blink::WebBluetoothGetPrimaryServiceCallbacks* callbacks) {
149 int request_id = 170 int request_id =
150 pending_primary_service_requests_.Add(new BluetoothPrimaryServiceRequest( 171 pending_primary_service_requests_.Add(new BluetoothPrimaryServiceRequest(
151 device_instance_id, service_uuid, callbacks)); 172 device_instance_id, service_uuid, callbacks));
152 Send(new BluetoothHostMsg_GetPrimaryService(CurrentWorkerId(), request_id, 173 Send(new BluetoothHostMsg_GetPrimaryService(CurrentWorkerId(), request_id,
153 device_instance_id.utf8(), 174 device_instance_id.utf8(),
154 service_uuid.utf8())); 175 service_uuid.utf8()));
155 } 176 }
156 177
178 void BluetoothDispatcher::getCharacteristic(
179 const blink::WebString& service_instance_id,
180 const blink::WebString& characteristic_uuid,
181 blink::WebBluetoothGetCharacteristicCallbacks* callbacks) {
182 int request_id =
183 pending_characteristic_requests_.Add(new BluetoothCharacteristicRequest(
184 service_instance_id, characteristic_uuid, callbacks));
185 Send(new BluetoothHostMsg_GetCharacteristic(CurrentWorkerId(), request_id,
186 service_instance_id.utf8(),
187 characteristic_uuid.utf8()));
188 }
189
157 void BluetoothDispatcher::OnWorkerRunLoopStopped() { 190 void BluetoothDispatcher::OnWorkerRunLoopStopped() {
158 delete this; 191 delete this;
159 } 192 }
160 193
161 void BluetoothDispatcher::OnRequestDeviceSuccess( 194 void BluetoothDispatcher::OnRequestDeviceSuccess(
162 int thread_id, 195 int thread_id,
163 int request_id, 196 int request_id,
164 const BluetoothDevice& device) { 197 const BluetoothDevice& device) {
165 DCHECK(pending_requests_.Lookup(request_id)) << request_id; 198 DCHECK(pending_requests_.Lookup(request_id)) << request_id;
166 199
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 } 274 }
242 275
243 pending_primary_service_requests_.Lookup(request_id) 276 pending_primary_service_requests_.Lookup(request_id)
244 ->callbacks->onError(new WebBluetoothError( 277 ->callbacks->onError(new WebBluetoothError(
245 // TODO(ortuno): Return more descriptive error messages. 278 // TODO(ortuno): Return more descriptive error messages.
246 // http://crbug.com/490419 279 // http://crbug.com/490419
247 WebBluetoothErrorFromBluetoothError(error_type), "")); 280 WebBluetoothErrorFromBluetoothError(error_type), ""));
248 pending_primary_service_requests_.Remove(request_id); 281 pending_primary_service_requests_.Remove(request_id);
249 } 282 }
250 283
284 void BluetoothDispatcher::OnGetCharacteristicSuccess(
285 int thread_id,
286 int request_id,
287 const std::string& characteristic_instance_id) {
288 DCHECK(pending_characteristic_requests_.Lookup(request_id)) << request_id;
289
290 BluetoothCharacteristicRequest* request =
291 pending_characteristic_requests_.Lookup(request_id);
292 request->callbacks->onSuccess(new WebBluetoothGATTCharacteristic(
293 WebString::fromUTF8(characteristic_instance_id),
294 request->service_instance_id, request->characteristic_uuid));
295
296 pending_characteristic_requests_.Remove(request_id);
297 }
298
299 void BluetoothDispatcher::OnGetCharacteristicError(int thread_id,
300 int request_id,
301 BluetoothError error_type) {
302 DCHECK(pending_characteristic_requests_.Lookup(request_id)) << request_id;
303
304 // Since we couldn't find the characteristic return null. See Step 3 of
305 // getCharacteristic algorithm:
306 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetoothgattservice-ge tcharacteristic
307 if (error_type == BluetoothError::NOT_FOUND) {
308 pending_characteristic_requests_.Lookup(request_id)
309 ->callbacks->onSuccess(nullptr);
310 } else {
311 pending_characteristic_requests_.Lookup(request_id)
312 ->callbacks->onError(new WebBluetoothError(
313 // TODO(ortuno): Return more descriptive error messages.
314 // http://crbug.com/490419
315 WebBluetoothErrorFromBluetoothError(error_type), ""));
316 }
317 pending_characteristic_requests_.Remove(request_id);
318 }
319
251 } // namespace content 320 } // namespace content
OLDNEW
« no previous file with comments | « content/child/bluetooth/bluetooth_dispatcher.h ('k') | content/child/bluetooth/web_bluetooth_impl.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698