| OLD | NEW |
| 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/renderer/bluetooth/bluetooth_dispatcher.h" | 5 #include "content/renderer/bluetooth/bluetooth_dispatcher.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/thread_task_runner_handle.h" | 15 #include "base/thread_task_runner_handle.h" |
| 16 #include "content/child/thread_safe_sender.h" | 16 #include "content/child/thread_safe_sender.h" |
| 17 #include "content/common/bluetooth/bluetooth_messages.h" | 17 #include "content/common/bluetooth/bluetooth_messages.h" |
| 18 #include "device/bluetooth/bluetooth_uuid.h" | 18 #include "device/bluetooth/bluetooth_uuid.h" |
| 19 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic
e.h" | 19 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic
e.h" |
| 20 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothError
.h" | 20 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothError
.h" |
| 21 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot
eGATTCharacteristic.h" | |
| 22 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot
eGATTCharacteristicInit.h" | |
| 23 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot
eGATTService.h" | 21 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot
eGATTService.h" |
| 24 #include "third_party/WebKit/public/platform/modules/bluetooth/WebRequestDeviceO
ptions.h" | 22 #include "third_party/WebKit/public/platform/modules/bluetooth/WebRequestDeviceO
ptions.h" |
| 25 | 23 |
| 26 using blink::WebBluetoothDevice; | 24 using blink::WebBluetoothDevice; |
| 27 using blink::WebBluetoothError; | 25 using blink::WebBluetoothError; |
| 28 using blink::WebBluetoothRemoteGATTCharacteristicInit; | |
| 29 using blink::WebBluetoothRemoteGATTServerConnectCallbacks; | 26 using blink::WebBluetoothRemoteGATTServerConnectCallbacks; |
| 30 using blink::WebBluetoothRemoteGATTService; | 27 using blink::WebBluetoothRemoteGATTService; |
| 31 using blink::WebBluetoothReadValueCallbacks; | 28 using blink::WebBluetoothReadValueCallbacks; |
| 32 using blink::WebBluetoothRequestDeviceCallbacks; | 29 using blink::WebBluetoothRequestDeviceCallbacks; |
| 33 using blink::WebBluetoothScanFilter; | 30 using blink::WebBluetoothScanFilter; |
| 34 using blink::WebRequestDeviceOptions; | 31 using blink::WebRequestDeviceOptions; |
| 35 using blink::WebString; | 32 using blink::WebString; |
| 36 using blink::WebVector; | 33 using blink::WebVector; |
| 37 | 34 |
| 38 struct BluetoothPrimaryServiceRequest { | 35 struct BluetoothPrimaryServiceRequest { |
| 39 BluetoothPrimaryServiceRequest( | 36 BluetoothPrimaryServiceRequest( |
| 40 blink::WebString device_id, | 37 blink::WebString device_id, |
| 41 blink::WebString service_uuid, | 38 blink::WebString service_uuid, |
| 42 blink::WebBluetoothGetPrimaryServiceCallbacks* callbacks) | 39 blink::WebBluetoothGetPrimaryServiceCallbacks* callbacks) |
| 43 : device_id(device_id), | 40 : device_id(device_id), |
| 44 service_uuid(service_uuid), | 41 service_uuid(service_uuid), |
| 45 callbacks(callbacks) {} | 42 callbacks(callbacks) {} |
| 46 ~BluetoothPrimaryServiceRequest() {} | 43 ~BluetoothPrimaryServiceRequest() {} |
| 47 | 44 |
| 48 blink::WebString device_id; | 45 blink::WebString device_id; |
| 49 blink::WebString service_uuid; | 46 blink::WebString service_uuid; |
| 50 std::unique_ptr<blink::WebBluetoothGetPrimaryServiceCallbacks> callbacks; | 47 std::unique_ptr<blink::WebBluetoothGetPrimaryServiceCallbacks> callbacks; |
| 51 }; | 48 }; |
| 52 | 49 |
| 53 struct BluetoothCharacteristicRequest { | |
| 54 BluetoothCharacteristicRequest( | |
| 55 blink::WebString service_instance_id, | |
| 56 blink::WebString characteristic_uuid, | |
| 57 blink::WebBluetoothGetCharacteristicCallbacks* callbacks) | |
| 58 : service_instance_id(service_instance_id), | |
| 59 characteristic_uuid(characteristic_uuid), | |
| 60 callbacks(callbacks) {} | |
| 61 ~BluetoothCharacteristicRequest() {} | |
| 62 | |
| 63 blink::WebString service_instance_id; | |
| 64 blink::WebString characteristic_uuid; | |
| 65 std::unique_ptr<blink::WebBluetoothGetCharacteristicCallbacks> callbacks; | |
| 66 }; | |
| 67 | |
| 68 struct BluetoothCharacteristicsRequest { | |
| 69 BluetoothCharacteristicsRequest( | |
| 70 blink::WebString service_instance_id, | |
| 71 blink::WebBluetoothGetCharacteristicsCallbacks* callbacks) | |
| 72 : service_instance_id(service_instance_id), callbacks(callbacks) {} | |
| 73 ~BluetoothCharacteristicsRequest() {} | |
| 74 | |
| 75 blink::WebString service_instance_id; | |
| 76 std::unique_ptr<blink::WebBluetoothGetCharacteristicsCallbacks> callbacks; | |
| 77 }; | |
| 78 | |
| 79 namespace content { | 50 namespace content { |
| 80 | 51 |
| 81 namespace { | 52 namespace { |
| 82 | 53 |
| 83 base::LazyInstance<base::ThreadLocalPointer<void>>::Leaky g_dispatcher_tls = | 54 base::LazyInstance<base::ThreadLocalPointer<void>>::Leaky g_dispatcher_tls = |
| 84 LAZY_INSTANCE_INITIALIZER; | 55 LAZY_INSTANCE_INITIALIZER; |
| 85 | 56 |
| 86 void* const kHasBeenDeleted = reinterpret_cast<void*>(0x1); | 57 void* const kHasBeenDeleted = reinterpret_cast<void*>(0x1); |
| 87 | 58 |
| 88 int CurrentWorkerId() { | 59 int CurrentWorkerId() { |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 OnRequestDeviceSuccess); | 111 OnRequestDeviceSuccess); |
| 141 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceError, OnRequestDeviceError); | 112 IPC_MESSAGE_HANDLER(BluetoothMsg_RequestDeviceError, OnRequestDeviceError); |
| 142 IPC_MESSAGE_HANDLER(BluetoothMsg_GATTServerConnectSuccess, | 113 IPC_MESSAGE_HANDLER(BluetoothMsg_GATTServerConnectSuccess, |
| 143 OnGATTServerConnectSuccess); | 114 OnGATTServerConnectSuccess); |
| 144 IPC_MESSAGE_HANDLER(BluetoothMsg_GATTServerConnectError, | 115 IPC_MESSAGE_HANDLER(BluetoothMsg_GATTServerConnectError, |
| 145 OnGATTServerConnectError); | 116 OnGATTServerConnectError); |
| 146 IPC_MESSAGE_HANDLER(BluetoothMsg_GetPrimaryServiceSuccess, | 117 IPC_MESSAGE_HANDLER(BluetoothMsg_GetPrimaryServiceSuccess, |
| 147 OnGetPrimaryServiceSuccess); | 118 OnGetPrimaryServiceSuccess); |
| 148 IPC_MESSAGE_HANDLER(BluetoothMsg_GetPrimaryServiceError, | 119 IPC_MESSAGE_HANDLER(BluetoothMsg_GetPrimaryServiceError, |
| 149 OnGetPrimaryServiceError); | 120 OnGetPrimaryServiceError); |
| 150 IPC_MESSAGE_HANDLER(BluetoothMsg_GetCharacteristicSuccess, | |
| 151 OnGetCharacteristicSuccess); | |
| 152 IPC_MESSAGE_HANDLER(BluetoothMsg_GetCharacteristicError, | |
| 153 OnGetCharacteristicError); | |
| 154 IPC_MESSAGE_HANDLER(BluetoothMsg_GetCharacteristicsSuccess, | |
| 155 OnGetCharacteristicsSuccess); | |
| 156 IPC_MESSAGE_HANDLER(BluetoothMsg_GetCharacteristicsError, | |
| 157 OnGetCharacteristicsError); | |
| 158 IPC_MESSAGE_UNHANDLED(handled = false) | 121 IPC_MESSAGE_UNHANDLED(handled = false) |
| 159 IPC_END_MESSAGE_MAP() | 122 IPC_END_MESSAGE_MAP() |
| 160 DCHECK(handled) << "Unhandled message:" << msg.type(); | 123 DCHECK(handled) << "Unhandled message:" << msg.type(); |
| 161 } | 124 } |
| 162 | 125 |
| 163 void BluetoothDispatcher::requestDevice( | 126 void BluetoothDispatcher::requestDevice( |
| 164 int frame_routing_id, | 127 int frame_routing_id, |
| 165 const WebRequestDeviceOptions& options, | 128 const WebRequestDeviceOptions& options, |
| 166 blink::WebBluetoothRequestDeviceCallbacks* callbacks) { | 129 blink::WebBluetoothRequestDeviceCallbacks* callbacks) { |
| 167 int request_id = pending_requests_.Add(callbacks); | 130 int request_id = pending_requests_.Add(callbacks); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 const blink::WebString& device_id, | 172 const blink::WebString& device_id, |
| 210 const blink::WebString& service_uuid, | 173 const blink::WebString& service_uuid, |
| 211 blink::WebBluetoothGetPrimaryServiceCallbacks* callbacks) { | 174 blink::WebBluetoothGetPrimaryServiceCallbacks* callbacks) { |
| 212 int request_id = pending_primary_service_requests_.Add( | 175 int request_id = pending_primary_service_requests_.Add( |
| 213 new BluetoothPrimaryServiceRequest(device_id, service_uuid, callbacks)); | 176 new BluetoothPrimaryServiceRequest(device_id, service_uuid, callbacks)); |
| 214 Send(new BluetoothHostMsg_GetPrimaryService( | 177 Send(new BluetoothHostMsg_GetPrimaryService( |
| 215 CurrentWorkerId(), request_id, frame_routing_id, device_id.utf8(), | 178 CurrentWorkerId(), request_id, frame_routing_id, device_id.utf8(), |
| 216 service_uuid.utf8())); | 179 service_uuid.utf8())); |
| 217 } | 180 } |
| 218 | 181 |
| 219 void BluetoothDispatcher::getCharacteristic( | |
| 220 int frame_routing_id, | |
| 221 const blink::WebString& service_instance_id, | |
| 222 const blink::WebString& characteristic_uuid, | |
| 223 blink::WebBluetoothGetCharacteristicCallbacks* callbacks) { | |
| 224 int request_id = | |
| 225 pending_characteristic_requests_.Add(new BluetoothCharacteristicRequest( | |
| 226 service_instance_id, characteristic_uuid, callbacks)); | |
| 227 Send(new BluetoothHostMsg_GetCharacteristic( | |
| 228 CurrentWorkerId(), request_id, frame_routing_id, | |
| 229 service_instance_id.utf8(), characteristic_uuid.utf8())); | |
| 230 } | |
| 231 | |
| 232 void BluetoothDispatcher::getCharacteristics( | |
| 233 int frame_routing_id, | |
| 234 const blink::WebString& service_instance_id, | |
| 235 const blink::WebString& characteristics_uuid, | |
| 236 blink::WebBluetoothGetCharacteristicsCallbacks* callbacks) { | |
| 237 int request_id = pending_characteristics_requests_.Add( | |
| 238 new BluetoothCharacteristicsRequest(service_instance_id, callbacks)); | |
| 239 Send(new BluetoothHostMsg_GetCharacteristics( | |
| 240 CurrentWorkerId(), request_id, frame_routing_id, | |
| 241 service_instance_id.utf8(), characteristics_uuid.utf8())); | |
| 242 } | |
| 243 | |
| 244 void BluetoothDispatcher::WillStopCurrentWorkerThread() { | 182 void BluetoothDispatcher::WillStopCurrentWorkerThread() { |
| 245 delete this; | 183 delete this; |
| 246 } | 184 } |
| 247 | 185 |
| 248 void BluetoothDispatcher::OnRequestDeviceSuccess( | 186 void BluetoothDispatcher::OnRequestDeviceSuccess( |
| 249 int thread_id, | 187 int thread_id, |
| 250 int request_id, | 188 int request_id, |
| 251 const BluetoothDevice& device) { | 189 const BluetoothDevice& device) { |
| 252 DCHECK(pending_requests_.Lookup(request_id)) << request_id; | 190 DCHECK(pending_requests_.Lookup(request_id)) << request_id; |
| 253 | 191 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 305 void BluetoothDispatcher::OnGetPrimaryServiceError(int thread_id, | 243 void BluetoothDispatcher::OnGetPrimaryServiceError(int thread_id, |
| 306 int request_id, | 244 int request_id, |
| 307 WebBluetoothError error) { | 245 WebBluetoothError error) { |
| 308 DCHECK(pending_primary_service_requests_.Lookup(request_id)) << request_id; | 246 DCHECK(pending_primary_service_requests_.Lookup(request_id)) << request_id; |
| 309 | 247 |
| 310 pending_primary_service_requests_.Lookup(request_id) | 248 pending_primary_service_requests_.Lookup(request_id) |
| 311 ->callbacks->onError(WebBluetoothError(error)); | 249 ->callbacks->onError(WebBluetoothError(error)); |
| 312 pending_primary_service_requests_.Remove(request_id); | 250 pending_primary_service_requests_.Remove(request_id); |
| 313 } | 251 } |
| 314 | 252 |
| 315 void BluetoothDispatcher::OnGetCharacteristicSuccess( | |
| 316 int thread_id, | |
| 317 int request_id, | |
| 318 const std::string& characteristic_instance_id, | |
| 319 uint32_t characteristic_properties) { | |
| 320 DCHECK(pending_characteristic_requests_.Lookup(request_id)) << request_id; | |
| 321 | |
| 322 BluetoothCharacteristicRequest* request = | |
| 323 pending_characteristic_requests_.Lookup(request_id); | |
| 324 request->callbacks->onSuccess( | |
| 325 base::WrapUnique(new WebBluetoothRemoteGATTCharacteristicInit( | |
| 326 request->service_instance_id, | |
| 327 WebString::fromUTF8(characteristic_instance_id), | |
| 328 request->characteristic_uuid, characteristic_properties))); | |
| 329 | |
| 330 pending_characteristic_requests_.Remove(request_id); | |
| 331 } | |
| 332 | |
| 333 void BluetoothDispatcher::OnGetCharacteristicError(int thread_id, | |
| 334 int request_id, | |
| 335 WebBluetoothError error) { | |
| 336 DCHECK(pending_characteristic_requests_.Lookup(request_id)) << request_id; | |
| 337 | |
| 338 pending_characteristic_requests_.Lookup(request_id) | |
| 339 ->callbacks->onError(WebBluetoothError(error)); | |
| 340 | |
| 341 pending_characteristic_requests_.Remove(request_id); | |
| 342 } | |
| 343 | |
| 344 void BluetoothDispatcher::OnGetCharacteristicsSuccess( | |
| 345 int thread_id, | |
| 346 int request_id, | |
| 347 const std::vector<std::string>& characteristics_instance_ids, | |
| 348 const std::vector<std::string>& characteristics_uuids, | |
| 349 const std::vector<uint32_t>& characteristics_properties) { | |
| 350 DCHECK(pending_characteristics_requests_.Lookup(request_id)) << request_id; | |
| 351 | |
| 352 BluetoothCharacteristicsRequest* request = | |
| 353 pending_characteristics_requests_.Lookup(request_id); | |
| 354 | |
| 355 // TODO(dcheng): This WebVector should use smart pointers. | |
| 356 std::unique_ptr<WebVector<blink::WebBluetoothRemoteGATTCharacteristicInit*>> | |
| 357 characteristics(new WebVector<WebBluetoothRemoteGATTCharacteristicInit*>( | |
| 358 characteristics_instance_ids.size())); | |
| 359 | |
| 360 for (size_t i = 0; i < characteristics_instance_ids.size(); i++) { | |
| 361 (*characteristics)[i] = new WebBluetoothRemoteGATTCharacteristicInit( | |
| 362 request->service_instance_id, | |
| 363 WebString::fromUTF8(characteristics_instance_ids[i]), | |
| 364 WebString::fromUTF8(characteristics_uuids[i]), | |
| 365 characteristics_properties[i]); | |
| 366 } | |
| 367 | |
| 368 request->callbacks->onSuccess(std::move(characteristics)); | |
| 369 | |
| 370 pending_characteristics_requests_.Remove(request_id); | |
| 371 } | |
| 372 | |
| 373 void BluetoothDispatcher::OnGetCharacteristicsError(int thread_id, | |
| 374 int request_id, | |
| 375 WebBluetoothError error) { | |
| 376 DCHECK(pending_characteristics_requests_.Lookup(request_id)) << request_id; | |
| 377 | |
| 378 pending_characteristics_requests_.Lookup(request_id) | |
| 379 ->callbacks->onError(WebBluetoothError(error)); | |
| 380 | |
| 381 pending_characteristics_requests_.Remove(request_id); | |
| 382 } | |
| 383 | |
| 384 } // namespace content | 253 } // namespace content |
| OLD | NEW |