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 "base/optional.h" | 12 #include "base/optional.h" |
13 #include "content/child/mojo/type_converters.h" | 13 #include "content/child/mojo/type_converters.h" |
14 #include "content/child/thread_safe_sender.h" | 14 #include "content/child/thread_safe_sender.h" |
15 #include "content/common/bluetooth/web_bluetooth_device_id.h" | |
16 #include "content/renderer/bluetooth/bluetooth_type_converters.h" | 15 #include "content/renderer/bluetooth/bluetooth_type_converters.h" |
17 #include "ipc/ipc_message.h" | 16 #include "ipc/ipc_message.h" |
18 #include "mojo/public/cpp/bindings/array.h" | 17 #include "mojo/public/cpp/bindings/array.h" |
19 #include "services/shell/public/cpp/interface_provider.h" | 18 #include "services/shell/public/cpp/interface_provider.h" |
20 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic
e.h" | 19 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic
e.h" |
21 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic
eInit.h" | 20 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic
eInit.h" |
22 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot
eGATTCharacteristic.h" | 21 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot
eGATTCharacteristic.h" |
23 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot
eGATTCharacteristicInit.h" | 22 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot
eGATTCharacteristicInit.h" |
24 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot
eGATTService.h" | 23 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot
eGATTService.h" |
25 #include "third_party/WebKit/public/platform/modules/bluetooth/WebRequestDeviceO
ptions.h" | 24 #include "third_party/WebKit/public/platform/modules/bluetooth/WebRequestDeviceO
ptions.h" |
(...skipping 25 matching lines...) Expand all Loading... |
51 base::Unretained(this), | 50 base::Unretained(this), |
52 base::Passed(base::WrapUnique(callbacks)))); | 51 base::Passed(base::WrapUnique(callbacks)))); |
53 } | 52 } |
54 | 53 |
55 void WebBluetoothImpl::connect( | 54 void WebBluetoothImpl::connect( |
56 const blink::WebString& device_id, | 55 const blink::WebString& device_id, |
57 blink::WebBluetoothDevice* device, | 56 blink::WebBluetoothDevice* device, |
58 blink::WebBluetoothRemoteGATTServerConnectCallbacks* callbacks) { | 57 blink::WebBluetoothRemoteGATTServerConnectCallbacks* callbacks) { |
59 // TODO(crbug.com/495270): After the Bluetooth Tree is implemented, there will | 58 // TODO(crbug.com/495270): After the Bluetooth Tree is implemented, there will |
60 // only be one object per device. But for now we replace the previous object. | 59 // only be one object per device. But for now we replace the previous object. |
61 WebBluetoothDeviceId device_id_obj = WebBluetoothDeviceId(device_id.utf8()); | 60 connected_devices_[device_id.utf8()] = device; |
62 connected_devices_[device_id_obj] = device; | |
63 | 61 |
64 GetWebBluetoothService().RemoteServerConnect( | 62 GetWebBluetoothService().RemoteServerConnect( |
65 std::move(device_id_obj), | 63 mojo::String::From(device_id), |
66 base::Bind(&WebBluetoothImpl::OnConnectComplete, base::Unretained(this), | 64 base::Bind(&WebBluetoothImpl::OnConnectComplete, base::Unretained(this), |
67 base::Passed(base::WrapUnique(callbacks)))); | 65 base::Passed(base::WrapUnique(callbacks)))); |
68 } | 66 } |
69 | 67 |
70 void WebBluetoothImpl::disconnect(const blink::WebString& device_id) { | 68 void WebBluetoothImpl::disconnect(const blink::WebString& device_id) { |
71 WebBluetoothDeviceId device_id_obj = WebBluetoothDeviceId(device_id.utf8()); | 69 connected_devices_.erase(device_id.utf8()); |
72 connected_devices_.erase(device_id_obj); | |
73 | 70 |
74 GetWebBluetoothService().RemoteServerDisconnect(std::move(device_id_obj)); | 71 GetWebBluetoothService().RemoteServerDisconnect( |
| 72 mojo::String::From(device_id)); |
75 } | 73 } |
76 | 74 |
77 void WebBluetoothImpl::getPrimaryServices( | 75 void WebBluetoothImpl::getPrimaryServices( |
78 const blink::WebString& device_id, | 76 const blink::WebString& device_id, |
79 int32_t quantity, | 77 int32_t quantity, |
80 const blink::WebString& services_uuid, | 78 const blink::WebString& services_uuid, |
81 blink::WebBluetoothGetPrimaryServicesCallbacks* callbacks) { | 79 blink::WebBluetoothGetPrimaryServicesCallbacks* callbacks) { |
82 DCHECK(blink::mojom::IsKnownEnumValue( | 80 DCHECK(blink::mojom::IsKnownEnumValue( |
83 static_cast<blink::mojom::WebBluetoothGATTQueryQuantity>(quantity))); | 81 static_cast<blink::mojom::WebBluetoothGATTQueryQuantity>(quantity))); |
84 GetWebBluetoothService().RemoteServerGetPrimaryServices( | 82 GetWebBluetoothService().RemoteServerGetPrimaryServices( |
85 WebBluetoothDeviceId(device_id.utf8()), | 83 mojo::String::From(device_id), |
86 static_cast<blink::mojom::WebBluetoothGATTQueryQuantity>(quantity), | 84 static_cast<blink::mojom::WebBluetoothGATTQueryQuantity>(quantity), |
87 services_uuid.isEmpty() | 85 services_uuid.isEmpty() |
88 ? base::nullopt | 86 ? base::nullopt |
89 : base::make_optional(device::BluetoothUUID(services_uuid.utf8())), | 87 : base::make_optional(device::BluetoothUUID(services_uuid.utf8())), |
90 base::Bind(&WebBluetoothImpl::OnGetPrimaryServicesComplete, | 88 base::Bind(&WebBluetoothImpl::OnGetPrimaryServicesComplete, |
91 base::Unretained(this), device_id, | 89 base::Unretained(this), device_id, |
92 base::Passed(base::WrapUnique(callbacks)))); | 90 base::Passed(base::WrapUnique(callbacks)))); |
93 } | 91 } |
94 | 92 |
95 void WebBluetoothImpl::getCharacteristics( | 93 void WebBluetoothImpl::getCharacteristics( |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 void WebBluetoothImpl::OnRequestDeviceComplete( | 181 void WebBluetoothImpl::OnRequestDeviceComplete( |
184 std::unique_ptr<blink::WebBluetoothRequestDeviceCallbacks> callbacks, | 182 std::unique_ptr<blink::WebBluetoothRequestDeviceCallbacks> callbacks, |
185 const blink::mojom::WebBluetoothError error, | 183 const blink::mojom::WebBluetoothError error, |
186 blink::mojom::WebBluetoothDevicePtr device) { | 184 blink::mojom::WebBluetoothDevicePtr device) { |
187 if (error == blink::mojom::WebBluetoothError::SUCCESS) { | 185 if (error == blink::mojom::WebBluetoothError::SUCCESS) { |
188 blink::WebVector<blink::WebString> uuids(device->uuids.size()); | 186 blink::WebVector<blink::WebString> uuids(device->uuids.size()); |
189 for (size_t i = 0; i < device->uuids.size(); ++i) | 187 for (size_t i = 0; i < device->uuids.size(); ++i) |
190 uuids[i] = blink::WebString::fromUTF8(device->uuids[i]); | 188 uuids[i] = blink::WebString::fromUTF8(device->uuids[i]); |
191 | 189 |
192 callbacks->onSuccess(base::WrapUnique(new blink::WebBluetoothDeviceInit( | 190 callbacks->onSuccess(base::WrapUnique(new blink::WebBluetoothDeviceInit( |
193 blink::WebString::fromUTF8(device->id.str()), | 191 blink::WebString::fromUTF8(device->id), |
194 blink::WebString::fromUTF8(device->name), uuids))); | 192 blink::WebString::fromUTF8(device->name), uuids))); |
195 } else { | 193 } else { |
196 callbacks->onError(ToInt32(error)); | 194 callbacks->onError(ToInt32(error)); |
197 } | 195 } |
198 } | 196 } |
199 | 197 |
200 void WebBluetoothImpl::GattServerDisconnected( | 198 void WebBluetoothImpl::GattServerDisconnected(const mojo::String& device_id) { |
201 const WebBluetoothDeviceId& device_id) { | |
202 auto device_iter = connected_devices_.find(device_id); | 199 auto device_iter = connected_devices_.find(device_id); |
203 if (device_iter != connected_devices_.end()) { | 200 if (device_iter != connected_devices_.end()) { |
204 // Remove device from the map before calling dispatchGattServerDisconnected | 201 // Remove device from the map before calling dispatchGattServerDisconnected |
205 // to avoid removing a device the gattserverdisconnected event handler might | 202 // to avoid removing a device the gattserverdisconnected event handler might |
206 // have re-connected. | 203 // have re-connected. |
207 blink::WebBluetoothDevice* device = device_iter->second; | 204 blink::WebBluetoothDevice* device = device_iter->second; |
208 connected_devices_.erase(device_iter); | 205 connected_devices_.erase(device_iter); |
209 device->dispatchGattServerDisconnected(); | 206 device->dispatchGattServerDisconnected(); |
210 } | 207 } |
211 } | 208 } |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 // Create an associated interface ptr and pass it to the WebBluetoothService | 317 // Create an associated interface ptr and pass it to the WebBluetoothService |
321 // so that it can send us events without us prompting. | 318 // so that it can send us events without us prompting. |
322 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo ptr_info; | 319 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo ptr_info; |
323 binding_.Bind(&ptr_info, web_bluetooth_service_.associated_group()); | 320 binding_.Bind(&ptr_info, web_bluetooth_service_.associated_group()); |
324 web_bluetooth_service_->SetClient(std::move(ptr_info)); | 321 web_bluetooth_service_->SetClient(std::move(ptr_info)); |
325 } | 322 } |
326 return *web_bluetooth_service_; | 323 return *web_bluetooth_service_; |
327 } | 324 } |
328 | 325 |
329 } // namespace content | 326 } // namespace content |
OLD | NEW |