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/web_bluetooth_impl.h" | 5 #include "content/renderer/bluetooth/web_bluetooth_impl.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
12 #include "content/child/mojo/type_converters.h" | 12 #include "content/child/mojo/type_converters.h" |
13 #include "content/child/thread_safe_sender.h" | 13 #include "content/child/thread_safe_sender.h" |
14 #include "content/public/common/service_registry.h" | 14 #include "content/public/common/service_registry.h" |
15 #include "content/renderer/bluetooth/bluetooth_dispatcher.h" | 15 #include "content/renderer/bluetooth/bluetooth_type_converters.h" |
16 #include "ipc/ipc_message.h" | 16 #include "ipc/ipc_message.h" |
17 #include "mojo/public/cpp/bindings/array.h" | 17 #include "mojo/public/cpp/bindings/array.h" |
18 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic
e.h" | 18 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic
e.h" |
| 19 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic
eInit.h" |
19 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot
eGATTCharacteristic.h" | 20 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot
eGATTCharacteristic.h" |
20 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot
eGATTCharacteristicInit.h" | 21 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot
eGATTCharacteristicInit.h" |
21 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot
eGATTService.h" | 22 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot
eGATTService.h" |
| 23 #include "third_party/WebKit/public/platform/modules/bluetooth/WebRequestDeviceO
ptions.h" |
22 | 24 |
23 namespace content { | 25 namespace content { |
24 | 26 |
25 WebBluetoothImpl::WebBluetoothImpl(ServiceRegistry* service_registry, | 27 WebBluetoothImpl::WebBluetoothImpl(ServiceRegistry* service_registry) |
26 ThreadSafeSender* thread_safe_sender, | 28 : service_registry_(service_registry), binding_(this) {} |
27 int frame_routing_id) | |
28 : service_registry_(service_registry), | |
29 binding_(this), | |
30 thread_safe_sender_(thread_safe_sender), | |
31 frame_routing_id_(frame_routing_id) {} | |
32 | 29 |
33 WebBluetoothImpl::~WebBluetoothImpl() { | 30 WebBluetoothImpl::~WebBluetoothImpl() { |
34 } | 31 } |
35 | 32 |
36 void WebBluetoothImpl::requestDevice( | 33 void WebBluetoothImpl::requestDevice( |
37 const blink::WebRequestDeviceOptions& options, | 34 const blink::WebRequestDeviceOptions& options, |
38 blink::WebBluetoothRequestDeviceCallbacks* callbacks) { | 35 blink::WebBluetoothRequestDeviceCallbacks* callbacks) { |
39 GetDispatcher()->requestDevice(frame_routing_id_, options, callbacks); | 36 GetWebBluetoothService().RequestDevice( |
| 37 blink::mojom::WebBluetoothRequestDeviceOptions::From(options), |
| 38 base::Bind(&WebBluetoothImpl::OnRequestDeviceComplete, |
| 39 base::Unretained(this), |
| 40 base::Passed(base::WrapUnique(callbacks)))); |
40 } | 41 } |
41 | 42 |
42 void WebBluetoothImpl::connect( | 43 void WebBluetoothImpl::connect( |
43 const blink::WebString& device_id, | 44 const blink::WebString& device_id, |
44 blink::WebBluetoothDevice* device, | 45 blink::WebBluetoothDevice* device, |
45 blink::WebBluetoothRemoteGATTServerConnectCallbacks* callbacks) { | 46 blink::WebBluetoothRemoteGATTServerConnectCallbacks* callbacks) { |
46 // TODO(crbug.com/495270): After the Bluetooth Tree is implemented, there will | 47 // TODO(crbug.com/495270): After the Bluetooth Tree is implemented, there will |
47 // only be one object per device. But for now we replace the previous object. | 48 // only be one object per device. But for now we replace the previous object. |
48 connected_devices_[device_id.utf8()] = device; | 49 connected_devices_[device_id.utf8()] = device; |
49 | 50 |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 mojo::Array<uint8_t> value) { | 148 mojo::Array<uint8_t> value) { |
148 // We post a task so that the event is fired after any pending promises have | 149 // We post a task so that the event is fired after any pending promises have |
149 // resolved. | 150 // resolved. |
150 base::ThreadTaskRunnerHandle::Get()->PostTask( | 151 base::ThreadTaskRunnerHandle::Get()->PostTask( |
151 FROM_HERE, | 152 FROM_HERE, |
152 base::Bind(&WebBluetoothImpl::DispatchCharacteristicValueChanged, | 153 base::Bind(&WebBluetoothImpl::DispatchCharacteristicValueChanged, |
153 base::Unretained(this), characteristic_instance_id, | 154 base::Unretained(this), characteristic_instance_id, |
154 value.PassStorage())); | 155 value.PassStorage())); |
155 } | 156 } |
156 | 157 |
| 158 void WebBluetoothImpl::OnRequestDeviceComplete( |
| 159 std::unique_ptr<blink::WebBluetoothRequestDeviceCallbacks> callbacks, |
| 160 const blink::mojom::WebBluetoothError error, |
| 161 blink::mojom::WebBluetoothDevicePtr device) { |
| 162 if (error == blink::mojom::WebBluetoothError::SUCCESS) { |
| 163 blink::WebVector<blink::WebString> uuids(device->uuids.size()); |
| 164 for (size_t i = 0; i < device->uuids.size(); ++i) |
| 165 uuids[i] = blink::WebString::fromUTF8(device->uuids[i]); |
| 166 |
| 167 callbacks->onSuccess(base::WrapUnique(new blink::WebBluetoothDeviceInit( |
| 168 blink::WebString::fromUTF8(device->id), |
| 169 blink::WebString::fromUTF8(device->name), uuids))); |
| 170 } else { |
| 171 callbacks->onError(error); |
| 172 } |
| 173 } |
| 174 |
157 void WebBluetoothImpl::GattServerDisconnected(const mojo::String& device_id) { | 175 void WebBluetoothImpl::GattServerDisconnected(const mojo::String& device_id) { |
158 auto device_iter = connected_devices_.find(device_id); | 176 auto device_iter = connected_devices_.find(device_id); |
159 if (device_iter != connected_devices_.end()) { | 177 if (device_iter != connected_devices_.end()) { |
160 device_iter->second->dispatchGattServerDisconnected(); | 178 device_iter->second->dispatchGattServerDisconnected(); |
161 connected_devices_.erase(device_iter); | 179 connected_devices_.erase(device_iter); |
162 } | 180 } |
163 } | 181 } |
164 | 182 |
165 void WebBluetoothImpl::OnConnectComplete( | 183 void WebBluetoothImpl::OnConnectComplete( |
166 std::unique_ptr<blink::WebBluetoothRemoteGATTServerConnectCallbacks> | 184 std::unique_ptr<blink::WebBluetoothRemoteGATTServerConnectCallbacks> |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 | 271 |
254 void WebBluetoothImpl::DispatchCharacteristicValueChanged( | 272 void WebBluetoothImpl::DispatchCharacteristicValueChanged( |
255 const std::string& characteristic_instance_id, | 273 const std::string& characteristic_instance_id, |
256 const std::vector<uint8_t>& value) { | 274 const std::vector<uint8_t>& value) { |
257 auto active_iter = active_characteristics_.find(characteristic_instance_id); | 275 auto active_iter = active_characteristics_.find(characteristic_instance_id); |
258 if (active_iter != active_characteristics_.end()) { | 276 if (active_iter != active_characteristics_.end()) { |
259 active_iter->second->dispatchCharacteristicValueChanged(value); | 277 active_iter->second->dispatchCharacteristicValueChanged(value); |
260 } | 278 } |
261 } | 279 } |
262 | 280 |
263 BluetoothDispatcher* WebBluetoothImpl::GetDispatcher() { | |
264 return BluetoothDispatcher::GetOrCreateThreadSpecificInstance( | |
265 thread_safe_sender_.get()); | |
266 } | |
267 | |
268 blink::mojom::WebBluetoothService& WebBluetoothImpl::GetWebBluetoothService() { | 281 blink::mojom::WebBluetoothService& WebBluetoothImpl::GetWebBluetoothService() { |
269 if (!web_bluetooth_service_) { | 282 if (!web_bluetooth_service_) { |
270 service_registry_->ConnectToRemoteService( | 283 service_registry_->ConnectToRemoteService( |
271 mojo::GetProxy(&web_bluetooth_service_)); | 284 mojo::GetProxy(&web_bluetooth_service_)); |
272 // Create an associated interface ptr and pass it to the WebBluetoothService | 285 // Create an associated interface ptr and pass it to the WebBluetoothService |
273 // so that it can send us events without us prompting. | 286 // so that it can send us events without us prompting. |
274 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo ptr_info; | 287 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo ptr_info; |
275 binding_.Bind(&ptr_info, web_bluetooth_service_.associated_group()); | 288 binding_.Bind(&ptr_info, web_bluetooth_service_.associated_group()); |
276 web_bluetooth_service_->SetClient(std::move(ptr_info)); | 289 web_bluetooth_service_->SetClient(std::move(ptr_info)); |
277 } | 290 } |
278 return *web_bluetooth_service_; | 291 return *web_bluetooth_service_; |
279 } | 292 } |
280 | 293 |
281 } // namespace content | 294 } // namespace content |
OLD | NEW |