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

Side by Side Diff: content/renderer/bluetooth/web_bluetooth_impl.cc

Issue 2019853002: bluetooth: Use WebBluetoothDeviceId instead of string (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bluetooth-uuid-typemap
Patch Set: Fix build Created 4 years, 5 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/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/renderer/bluetooth/bluetooth_type_converters.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 "services/shell/public/cpp/interface_provider.h" 18 #include "services/shell/public/cpp/interface_provider.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/WebBluetoothDevic eInit.h" 20 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic eInit.h"
21 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot eGATTCharacteristic.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" 22 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot eGATTCharacteristicInit.h"
23 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot eGATTService.h" 23 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot eGATTService.h"
24 #include "third_party/WebKit/public/platform/modules/bluetooth/WebRequestDeviceO ptions.h" 24 #include "third_party/WebKit/public/platform/modules/bluetooth/WebRequestDeviceO ptions.h"
25 25
26 using web_bluetooth::WebBluetoothDeviceId;
27
26 namespace content { 28 namespace content {
27 29
28 WebBluetoothImpl::WebBluetoothImpl(shell::InterfaceProvider* remote_interfaces) 30 WebBluetoothImpl::WebBluetoothImpl(shell::InterfaceProvider* remote_interfaces)
29 : remote_interfaces_(remote_interfaces), binding_(this) {} 31 : remote_interfaces_(remote_interfaces), binding_(this) {}
30 32
31 WebBluetoothImpl::~WebBluetoothImpl() { 33 WebBluetoothImpl::~WebBluetoothImpl() {
32 } 34 }
33 35
34 void WebBluetoothImpl::requestDevice( 36 void WebBluetoothImpl::requestDevice(
35 const blink::WebRequestDeviceOptions& options, 37 const blink::WebRequestDeviceOptions& options,
36 blink::WebBluetoothRequestDeviceCallbacks* callbacks) { 38 blink::WebBluetoothRequestDeviceCallbacks* callbacks) {
37 GetWebBluetoothService().RequestDevice( 39 GetWebBluetoothService().RequestDevice(
38 blink::mojom::WebBluetoothRequestDeviceOptions::From(options), 40 blink::mojom::WebBluetoothRequestDeviceOptions::From(options),
39 base::Bind(&WebBluetoothImpl::OnRequestDeviceComplete, 41 base::Bind(&WebBluetoothImpl::OnRequestDeviceComplete,
40 base::Unretained(this), 42 base::Unretained(this),
41 base::Passed(base::WrapUnique(callbacks)))); 43 base::Passed(base::WrapUnique(callbacks))));
42 } 44 }
43 45
44 void WebBluetoothImpl::connect( 46 void WebBluetoothImpl::connect(
45 const blink::WebString& device_id, 47 const blink::WebString& device_id,
46 blink::WebBluetoothDevice* device, 48 blink::WebBluetoothDevice* device,
47 blink::WebBluetoothRemoteGATTServerConnectCallbacks* callbacks) { 49 blink::WebBluetoothRemoteGATTServerConnectCallbacks* callbacks) {
48 // TODO(crbug.com/495270): After the Bluetooth Tree is implemented, there will 50 // TODO(crbug.com/495270): After the Bluetooth Tree is implemented, there will
49 // only be one object per device. But for now we replace the previous object. 51 // only be one object per device. But for now we replace the previous object.
50 connected_devices_[device_id.utf8()] = device; 52 WebBluetoothDeviceId device_id_obj = WebBluetoothDeviceId(device_id.utf8());
53 connected_devices_[device_id_obj] = device;
51 54
52 GetWebBluetoothService().RemoteServerConnect( 55 GetWebBluetoothService().RemoteServerConnect(
53 mojo::String::From(device_id), 56 std::move(device_id_obj),
54 base::Bind(&WebBluetoothImpl::OnConnectComplete, base::Unretained(this), 57 base::Bind(&WebBluetoothImpl::OnConnectComplete, base::Unretained(this),
55 base::Passed(base::WrapUnique(callbacks)))); 58 base::Passed(base::WrapUnique(callbacks))));
56 } 59 }
57 60
58 void WebBluetoothImpl::disconnect(const blink::WebString& device_id) { 61 void WebBluetoothImpl::disconnect(const blink::WebString& device_id) {
59 connected_devices_.erase(device_id.utf8()); 62 WebBluetoothDeviceId device_id_obj = WebBluetoothDeviceId(device_id.utf8());
63 connected_devices_.erase(device_id_obj);
60 64
61 GetWebBluetoothService().RemoteServerDisconnect( 65 GetWebBluetoothService().RemoteServerDisconnect(std::move(device_id_obj));
62 mojo::String::From(device_id));
63 } 66 }
64 67
65 void WebBluetoothImpl::getPrimaryServices( 68 void WebBluetoothImpl::getPrimaryServices(
66 const blink::WebString& device_id, 69 const blink::WebString& device_id,
67
68 blink::mojom::WebBluetoothGATTQueryQuantity quantity, 70 blink::mojom::WebBluetoothGATTQueryQuantity quantity,
69 const blink::WebString& services_uuid, 71 const blink::WebString& services_uuid,
70 blink::WebBluetoothGetPrimaryServicesCallbacks* callbacks) { 72 blink::WebBluetoothGetPrimaryServicesCallbacks* callbacks) {
71 GetWebBluetoothService().RemoteServerGetPrimaryServices( 73 GetWebBluetoothService().RemoteServerGetPrimaryServices(
72 mojo::String::From(device_id), quantity, 74 WebBluetoothDeviceId(device_id.utf8()), quantity,
73 services_uuid.isEmpty() 75 services_uuid.isEmpty()
74 ? base::nullopt 76 ? base::nullopt
75 : base::make_optional(device::BluetoothUUID(services_uuid.utf8())), 77 : base::make_optional(device::BluetoothUUID(services_uuid.utf8())),
76 base::Bind(&WebBluetoothImpl::OnGetPrimaryServicesComplete, 78 base::Bind(&WebBluetoothImpl::OnGetPrimaryServicesComplete,
77 base::Unretained(this), device_id, 79 base::Unretained(this), device_id,
78 base::Passed(base::WrapUnique(callbacks)))); 80 base::Passed(base::WrapUnique(callbacks))));
79 } 81 }
80 82
81 void WebBluetoothImpl::getCharacteristics( 83 void WebBluetoothImpl::getCharacteristics(
82 const blink::WebString& service_instance_id, 84 const blink::WebString& service_instance_id,
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 void WebBluetoothImpl::OnRequestDeviceComplete( 168 void WebBluetoothImpl::OnRequestDeviceComplete(
167 std::unique_ptr<blink::WebBluetoothRequestDeviceCallbacks> callbacks, 169 std::unique_ptr<blink::WebBluetoothRequestDeviceCallbacks> callbacks,
168 const blink::mojom::WebBluetoothError error, 170 const blink::mojom::WebBluetoothError error,
169 blink::mojom::WebBluetoothDevicePtr device) { 171 blink::mojom::WebBluetoothDevicePtr device) {
170 if (error == blink::mojom::WebBluetoothError::SUCCESS) { 172 if (error == blink::mojom::WebBluetoothError::SUCCESS) {
171 blink::WebVector<blink::WebString> uuids(device->uuids.size()); 173 blink::WebVector<blink::WebString> uuids(device->uuids.size());
172 for (size_t i = 0; i < device->uuids.size(); ++i) 174 for (size_t i = 0; i < device->uuids.size(); ++i)
173 uuids[i] = blink::WebString::fromUTF8(device->uuids[i]); 175 uuids[i] = blink::WebString::fromUTF8(device->uuids[i]);
174 176
175 callbacks->onSuccess(base::WrapUnique(new blink::WebBluetoothDeviceInit( 177 callbacks->onSuccess(base::WrapUnique(new blink::WebBluetoothDeviceInit(
176 blink::WebString::fromUTF8(device->id), 178 blink::WebString::fromUTF8(device->id.str()),
177 blink::WebString::fromUTF8(device->name), uuids))); 179 blink::WebString::fromUTF8(device->name), uuids)));
178 } else { 180 } else {
179 callbacks->onError(error); 181 callbacks->onError(error);
180 } 182 }
181 } 183 }
182 184
183 void WebBluetoothImpl::GattServerDisconnected(const mojo::String& device_id) { 185 void WebBluetoothImpl::GattServerDisconnected(
186 const WebBluetoothDeviceId& device_id) {
184 auto device_iter = connected_devices_.find(device_id); 187 auto device_iter = connected_devices_.find(device_id);
185 if (device_iter != connected_devices_.end()) { 188 if (device_iter != connected_devices_.end()) {
186 // Remove device from the map before calling dispatchGattServerDisconnected 189 // Remove device from the map before calling dispatchGattServerDisconnected
187 // to avoid removing a device the gattserverdisconnected event handler might 190 // to avoid removing a device the gattserverdisconnected event handler might
188 // have re-connected. 191 // have re-connected.
189 blink::WebBluetoothDevice* device = device_iter->second; 192 blink::WebBluetoothDevice* device = device_iter->second;
190 connected_devices_.erase(device_iter); 193 connected_devices_.erase(device_iter);
191 device->dispatchGattServerDisconnected(); 194 device->dispatchGattServerDisconnected();
192 } 195 }
193 } 196 }
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 // Create an associated interface ptr and pass it to the WebBluetoothService 305 // Create an associated interface ptr and pass it to the WebBluetoothService
303 // so that it can send us events without us prompting. 306 // so that it can send us events without us prompting.
304 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo ptr_info; 307 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo ptr_info;
305 binding_.Bind(&ptr_info, web_bluetooth_service_.associated_group()); 308 binding_.Bind(&ptr_info, web_bluetooth_service_.associated_group());
306 web_bluetooth_service_->SetClient(std::move(ptr_info)); 309 web_bluetooth_service_->SetClient(std::move(ptr_info));
307 } 310 }
308 return *web_bluetooth_service_; 311 return *web_bluetooth_service_;
309 } 312 }
310 313
311 } // namespace content 314 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698