Upstreaming browser/ui/uikit_ui_util from iOS.
[chromium-blink-merge.git] / content / renderer / bluetooth / bluetooth_dispatcher.cc
blobd9391041bf7289146542cd6b1af24d6e0e885809
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.
5 #include "content/renderer/bluetooth/bluetooth_dispatcher.h"
7 #include "base/lazy_instance.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "content/child/thread_safe_sender.h"
12 #include "content/common/bluetooth/bluetooth_messages.h"
13 #include "device/bluetooth/bluetooth_uuid.h"
14 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevice.h"
15 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothError.h"
16 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothGATTCharacteristic.h"
17 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothGATTRemoteServer.h"
18 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothGATTService.h"
19 #include "third_party/WebKit/public/platform/modules/bluetooth/WebRequestDeviceOptions.h"
21 using blink::WebBluetoothConnectGATTCallbacks;
22 using blink::WebBluetoothDevice;
23 using blink::WebBluetoothError;
24 using blink::WebBluetoothGATTCharacteristic;
25 using blink::WebBluetoothGATTRemoteServer;
26 using blink::WebBluetoothGATTService;
27 using blink::WebBluetoothReadValueCallbacks;
28 using blink::WebBluetoothRequestDeviceCallbacks;
29 using blink::WebBluetoothScanFilter;
30 using blink::WebRequestDeviceOptions;
31 using blink::WebString;
32 using blink::WebVector;
34 struct BluetoothPrimaryServiceRequest {
35 BluetoothPrimaryServiceRequest(
36 blink::WebString device_instance_id,
37 blink::WebString service_uuid,
38 blink::WebBluetoothGetPrimaryServiceCallbacks* callbacks)
39 : device_instance_id(device_instance_id),
40 service_uuid(service_uuid),
41 callbacks(callbacks) {}
42 ~BluetoothPrimaryServiceRequest() {}
44 blink::WebString device_instance_id;
45 blink::WebString service_uuid;
46 scoped_ptr<blink::WebBluetoothGetPrimaryServiceCallbacks> callbacks;
49 struct BluetoothCharacteristicRequest {
50 BluetoothCharacteristicRequest(
51 blink::WebString service_instance_id,
52 blink::WebString characteristic_uuid,
53 blink::WebBluetoothGetCharacteristicCallbacks* callbacks)
54 : service_instance_id(service_instance_id),
55 characteristic_uuid(characteristic_uuid),
56 callbacks(callbacks) {}
57 ~BluetoothCharacteristicRequest() {}
59 blink::WebString service_instance_id;
60 blink::WebString characteristic_uuid;
61 scoped_ptr<blink::WebBluetoothGetCharacteristicCallbacks> callbacks;
64 namespace content {
66 namespace {
68 base::LazyInstance<base::ThreadLocalPointer<BluetoothDispatcher>>::Leaky
69 g_dispatcher_tls = LAZY_INSTANCE_INITIALIZER;
71 BluetoothDispatcher* const kHasBeenDeleted =
72 reinterpret_cast<BluetoothDispatcher*>(0x1);
74 int CurrentWorkerId() {
75 return WorkerTaskRunner::Instance()->CurrentWorkerId();
78 WebBluetoothDevice::VendorIDSource GetWebVendorIdSource(
79 device::BluetoothDevice::VendorIDSource vendor_id_source) {
80 switch (vendor_id_source) {
81 case device::BluetoothDevice::VENDOR_ID_UNKNOWN:
82 return WebBluetoothDevice::VendorIDSource::Unknown;
83 case device::BluetoothDevice::VENDOR_ID_BLUETOOTH:
84 return WebBluetoothDevice::VendorIDSource::Bluetooth;
85 case device::BluetoothDevice::VENDOR_ID_USB:
86 return WebBluetoothDevice::VendorIDSource::USB;
88 NOTREACHED();
89 return WebBluetoothDevice::VendorIDSource::Unknown;
92 } // namespace
94 BluetoothDispatcher::BluetoothDispatcher(ThreadSafeSender* sender)
95 : thread_safe_sender_(sender) {
96 g_dispatcher_tls.Pointer()->Set(this);
99 BluetoothDispatcher::~BluetoothDispatcher() {
100 g_dispatcher_tls.Pointer()->Set(kHasBeenDeleted);
103 BluetoothDispatcher* BluetoothDispatcher::GetOrCreateThreadSpecificInstance(
104 ThreadSafeSender* thread_safe_sender) {
105 if (g_dispatcher_tls.Pointer()->Get() == kHasBeenDeleted) {
106 NOTREACHED() << "Re-instantiating TLS BluetoothDispatcher.";
107 g_dispatcher_tls.Pointer()->Set(NULL);
109 if (g_dispatcher_tls.Pointer()->Get())
110 return g_dispatcher_tls.Pointer()->Get();
112 BluetoothDispatcher* dispatcher = new BluetoothDispatcher(thread_safe_sender);
113 if (CurrentWorkerId())
114 WorkerTaskRunner::Instance()->AddStopObserver(dispatcher);
115 return dispatcher;
118 bool BluetoothDispatcher::Send(IPC::Message* msg) {
119 return thread_safe_sender_->Send(msg);
122 void BluetoothDispatcher::OnMessageReceived(const IPC::Message& msg) {
123 bool handled = true;
124 IPC_BEGIN_MESSAGE_MAP(BluetoothDispatcher, msg)
125 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceSuccess,
126 OnRequestDeviceSuccess);
127 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceError, OnRequestDeviceError);
128 IPC_MESSAGE_HANDLER(BluetoothMsg_ConnectGATTSuccess, OnConnectGATTSuccess);
129 IPC_MESSAGE_HANDLER(BluetoothMsg_ConnectGATTError, OnConnectGATTError);
130 IPC_MESSAGE_HANDLER(BluetoothMsg_GetPrimaryServiceSuccess,
131 OnGetPrimaryServiceSuccess);
132 IPC_MESSAGE_HANDLER(BluetoothMsg_GetPrimaryServiceError,
133 OnGetPrimaryServiceError);
134 IPC_MESSAGE_HANDLER(BluetoothMsg_GetCharacteristicSuccess,
135 OnGetCharacteristicSuccess);
136 IPC_MESSAGE_HANDLER(BluetoothMsg_GetCharacteristicError,
137 OnGetCharacteristicError);
138 IPC_MESSAGE_HANDLER(BluetoothMsg_ReadCharacteristicValueSuccess,
139 OnReadValueSuccess);
140 IPC_MESSAGE_HANDLER(BluetoothMsg_ReadCharacteristicValueError,
141 OnReadValueError);
142 IPC_MESSAGE_HANDLER(BluetoothMsg_WriteCharacteristicValueSuccess,
143 OnWriteValueSuccess);
144 IPC_MESSAGE_HANDLER(BluetoothMsg_WriteCharacteristicValueError,
145 OnWriteValueError);
146 IPC_MESSAGE_UNHANDLED(handled = false)
147 IPC_END_MESSAGE_MAP()
148 DCHECK(handled) << "Unhandled message:" << msg.type();
151 void BluetoothDispatcher::requestDevice(
152 int frame_routing_id,
153 const WebRequestDeviceOptions& options,
154 blink::WebBluetoothRequestDeviceCallbacks* callbacks) {
155 int request_id = pending_requests_.Add(callbacks);
157 // Convert |options| to its IPC form.
158 std::vector<content::BluetoothScanFilter> filters(options.filters.size());
159 for (size_t i = 0; i < options.filters.size(); ++i) {
160 const WebBluetoothScanFilter& web_filter = options.filters[i];
161 BluetoothScanFilter& filter = filters[i];
162 filter.services.reserve(web_filter.services.size());
163 for (const WebString& service : web_filter.services) {
164 filter.services.push_back(device::BluetoothUUID(service.utf8()));
167 std::vector<device::BluetoothUUID> optional_services;
168 optional_services.reserve(options.optionalServices.size());
169 for (const WebString& optional_service : options.optionalServices) {
170 optional_services.push_back(device::BluetoothUUID(optional_service.utf8()));
173 Send(new BluetoothHostMsg_RequestDevice(CurrentWorkerId(), request_id,
174 frame_routing_id, filters,
175 optional_services));
178 void BluetoothDispatcher::connectGATT(
179 const blink::WebString& device_instance_id,
180 blink::WebBluetoothConnectGATTCallbacks* callbacks) {
181 int request_id = pending_connect_requests_.Add(callbacks);
182 Send(new BluetoothHostMsg_ConnectGATT(CurrentWorkerId(), request_id,
183 device_instance_id.utf8()));
186 void BluetoothDispatcher::getPrimaryService(
187 const blink::WebString& device_instance_id,
188 const blink::WebString& service_uuid,
189 blink::WebBluetoothGetPrimaryServiceCallbacks* callbacks) {
190 int request_id =
191 pending_primary_service_requests_.Add(new BluetoothPrimaryServiceRequest(
192 device_instance_id, service_uuid, callbacks));
193 Send(new BluetoothHostMsg_GetPrimaryService(CurrentWorkerId(), request_id,
194 device_instance_id.utf8(),
195 service_uuid.utf8()));
198 void BluetoothDispatcher::getCharacteristic(
199 const blink::WebString& service_instance_id,
200 const blink::WebString& characteristic_uuid,
201 blink::WebBluetoothGetCharacteristicCallbacks* callbacks) {
202 int request_id =
203 pending_characteristic_requests_.Add(new BluetoothCharacteristicRequest(
204 service_instance_id, characteristic_uuid, callbacks));
205 Send(new BluetoothHostMsg_GetCharacteristic(CurrentWorkerId(), request_id,
206 service_instance_id.utf8(),
207 characteristic_uuid.utf8()));
210 void BluetoothDispatcher::readValue(
211 const blink::WebString& characteristic_instance_id,
212 blink::WebBluetoothReadValueCallbacks* callbacks) {
213 int request_id = pending_read_value_requests_.Add(callbacks);
214 Send(new BluetoothHostMsg_ReadValue(CurrentWorkerId(), request_id,
215 characteristic_instance_id.utf8()));
218 void BluetoothDispatcher::writeValue(
219 const blink::WebString& characteristic_instance_id,
220 const std::vector<uint8_t>& value,
221 blink::WebBluetoothWriteValueCallbacks* callbacks) {
222 int request_id = pending_write_value_requests_.Add(callbacks);
224 Send(new BluetoothHostMsg_WriteValue(
225 CurrentWorkerId(), request_id, characteristic_instance_id.utf8(), value));
228 void BluetoothDispatcher::OnWorkerRunLoopStopped() {
229 delete this;
232 void BluetoothDispatcher::OnRequestDeviceSuccess(
233 int thread_id,
234 int request_id,
235 const BluetoothDevice& device) {
236 DCHECK(pending_requests_.Lookup(request_id)) << request_id;
238 WebVector<WebString> uuids(device.uuids.size());
239 for (size_t i = 0; i < device.uuids.size(); ++i)
240 uuids[i] = WebString::fromUTF8(device.uuids[i].c_str());
242 pending_requests_.Lookup(request_id)
243 ->onSuccess(new WebBluetoothDevice(
244 WebString::fromUTF8(device.instance_id), WebString(device.name),
245 device.device_class, GetWebVendorIdSource(device.vendor_id_source),
246 device.vendor_id, device.product_id, device.product_version,
247 device.paired, uuids));
248 pending_requests_.Remove(request_id);
251 void BluetoothDispatcher::OnRequestDeviceError(int thread_id,
252 int request_id,
253 WebBluetoothError error) {
254 DCHECK(pending_requests_.Lookup(request_id)) << request_id;
255 pending_requests_.Lookup(request_id)->onError(new WebBluetoothError(error));
256 pending_requests_.Remove(request_id);
259 void BluetoothDispatcher::OnConnectGATTSuccess(
260 int thread_id,
261 int request_id,
262 const std::string& device_instance_id) {
263 DCHECK(pending_connect_requests_.Lookup(request_id)) << request_id;
264 pending_connect_requests_.Lookup(request_id)
265 ->onSuccess(new WebBluetoothGATTRemoteServer(
266 WebString::fromUTF8(device_instance_id), true /* connected */));
267 pending_connect_requests_.Remove(request_id);
270 void BluetoothDispatcher::OnConnectGATTError(int thread_id,
271 int request_id,
272 WebBluetoothError error) {
273 DCHECK(pending_connect_requests_.Lookup(request_id)) << request_id;
274 pending_connect_requests_.Lookup(request_id)
275 ->onError(new WebBluetoothError(error));
276 pending_connect_requests_.Remove(request_id);
279 void BluetoothDispatcher::OnGetPrimaryServiceSuccess(
280 int thread_id,
281 int request_id,
282 const std::string& service_instance_id) {
283 DCHECK(pending_primary_service_requests_.Lookup(request_id)) << request_id;
284 BluetoothPrimaryServiceRequest* request =
285 pending_primary_service_requests_.Lookup(request_id);
286 request->callbacks->onSuccess(new WebBluetoothGATTService(
287 WebString::fromUTF8(service_instance_id), request->service_uuid,
288 true /* isPrimary */, request->device_instance_id));
289 pending_primary_service_requests_.Remove(request_id);
292 void BluetoothDispatcher::OnGetPrimaryServiceError(int thread_id,
293 int request_id,
294 WebBluetoothError error) {
295 DCHECK(pending_primary_service_requests_.Lookup(request_id)) << request_id;
297 // Since we couldn't find the service return null. See Step 3 of
298 // getPrimaryService algorithm:
299 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetoothgattremoteserver-getprimaryservice
300 if (error == WebBluetoothError::ServiceNotFound) {
301 pending_primary_service_requests_.Lookup(request_id)
302 ->callbacks->onSuccess(nullptr);
303 pending_primary_service_requests_.Remove(request_id);
304 return;
307 pending_primary_service_requests_.Lookup(request_id)
308 ->callbacks->onError(new WebBluetoothError(error));
309 pending_primary_service_requests_.Remove(request_id);
312 void BluetoothDispatcher::OnGetCharacteristicSuccess(
313 int thread_id,
314 int request_id,
315 const std::string& characteristic_instance_id) {
316 DCHECK(pending_characteristic_requests_.Lookup(request_id)) << request_id;
318 BluetoothCharacteristicRequest* request =
319 pending_characteristic_requests_.Lookup(request_id);
320 request->callbacks->onSuccess(new WebBluetoothGATTCharacteristic(
321 WebString::fromUTF8(characteristic_instance_id),
322 request->service_instance_id, request->characteristic_uuid));
324 pending_characteristic_requests_.Remove(request_id);
327 void BluetoothDispatcher::OnGetCharacteristicError(int thread_id,
328 int request_id,
329 WebBluetoothError error) {
330 DCHECK(pending_characteristic_requests_.Lookup(request_id)) << request_id;
332 // Since we couldn't find the characteristic return null. See Step 3 of
333 // getCharacteristic algorithm:
334 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetoothgattservice-getcharacteristic
335 if (error == WebBluetoothError::CharacteristicNotFound) {
336 pending_characteristic_requests_.Lookup(request_id)
337 ->callbacks->onSuccess(nullptr);
338 } else {
339 pending_characteristic_requests_.Lookup(request_id)
340 ->callbacks->onError(new WebBluetoothError(error));
342 pending_characteristic_requests_.Remove(request_id);
345 void BluetoothDispatcher::OnReadValueSuccess(
346 int thread_id,
347 int request_id,
348 const std::vector<uint8_t>& value) {
349 DCHECK(pending_read_value_requests_.Lookup(request_id)) << request_id;
351 // WebArrayBuffer is not accessible from Source/modules so we pass a
352 // WebVector instead.
353 pending_read_value_requests_.Lookup(request_id)
354 ->onSuccess(new WebVector<uint8_t>(value));
356 pending_read_value_requests_.Remove(request_id);
359 void BluetoothDispatcher::OnReadValueError(int thread_id,
360 int request_id,
361 WebBluetoothError error) {
362 DCHECK(pending_read_value_requests_.Lookup(request_id)) << request_id;
364 pending_read_value_requests_.Lookup(request_id)
365 ->onError(new WebBluetoothError(error));
367 pending_read_value_requests_.Remove(request_id);
370 void BluetoothDispatcher::OnWriteValueSuccess(int thread_id, int request_id) {
371 DCHECK(pending_write_value_requests_.Lookup(request_id)) << request_id;
373 pending_write_value_requests_.Lookup(request_id)->onSuccess();
375 pending_write_value_requests_.Remove(request_id);
378 void BluetoothDispatcher::OnWriteValueError(int thread_id,
379 int request_id,
380 WebBluetoothError error) {
381 DCHECK(pending_write_value_requests_.Lookup(request_id)) << request_id;
383 pending_write_value_requests_.Lookup(request_id)
384 ->onError(new WebBluetoothError(error));
386 pending_write_value_requests_.Remove(request_id);
389 } // namespace content