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

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

Issue 2466223002: Implement WebBluetooth getDescriptor[s] (Closed)
Patch Set: Addressing code review comments from ortuno (still working on test changes) Created 4 years 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/common/bluetooth/web_bluetooth_device_id.h" 15 #include "content/common/bluetooth/web_bluetooth_device_id.h"
16 #include "content/renderer/bluetooth/bluetooth_type_converters.h" 16 #include "content/renderer/bluetooth/bluetooth_type_converters.h"
17 #include "ipc/ipc_message.h" 17 #include "ipc/ipc_message.h"
18 #include "mojo/public/cpp/bindings/array.h" 18 #include "mojo/public/cpp/bindings/array.h"
19 #include "services/service_manager/public/cpp/interface_provider.h" 19 #include "services/service_manager/public/cpp/interface_provider.h"
20 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic e.h" 20 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic e.h"
21 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic eInit.h" 21 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothDevic eInit.h"
22 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot eGATTCharacteristic.h" 22 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot eGATTCharacteristic.h"
23 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot eGATTCharacteristicInit.h" 23 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot eGATTCharacteristicInit.h"
24 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot eGATTDescriptor.h"
25 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot eGATTDescriptorInit.h"
24 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot eGATTService.h" 26 #include "third_party/WebKit/public/platform/modules/bluetooth/WebBluetoothRemot eGATTService.h"
25 #include "third_party/WebKit/public/platform/modules/bluetooth/WebRequestDeviceO ptions.h" 27 #include "third_party/WebKit/public/platform/modules/bluetooth/WebRequestDeviceO ptions.h"
26 28
27 namespace content { 29 namespace content {
28 30
29 namespace { 31 namespace {
30 32
31 // Blink can't use non-blink mojo enums like blink::mojom::WebBluetoothResult, 33 // Blink can't use non-blink mojo enums like blink::mojom::WebBluetoothResult,
32 // so we pass it as an int32 across the boundary. 34 // so we pass it as an int32 across the boundary.
33 int32_t ToInt32(blink::mojom::WebBluetoothResult result) { 35 int32_t ToInt32(blink::mojom::WebBluetoothResult result) {
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 static_cast<blink::mojom::WebBluetoothGATTQueryQuantity>(quantity), 107 static_cast<blink::mojom::WebBluetoothGATTQueryQuantity>(quantity),
106 characteristics_uuid.isEmpty() 108 characteristics_uuid.isEmpty()
107 ? base::nullopt 109 ? base::nullopt
108 : base::make_optional( 110 : base::make_optional(
109 device::BluetoothUUID(characteristics_uuid.utf8())), 111 device::BluetoothUUID(characteristics_uuid.utf8())),
110 base::Bind(&WebBluetoothImpl::OnGetCharacteristicsComplete, 112 base::Bind(&WebBluetoothImpl::OnGetCharacteristicsComplete,
111 base::Unretained(this), service_instance_id, 113 base::Unretained(this), service_instance_id,
112 base::Passed(base::WrapUnique(callbacks)))); 114 base::Passed(base::WrapUnique(callbacks))));
113 } 115 }
114 116
115 void WebBluetoothImpl::readValue( 117 void WebBluetoothImpl::characteristicReadValue(
116 const blink::WebString& characteristic_instance_id, 118 const blink::WebString& characteristic_instance_id,
117 blink::WebBluetoothReadValueCallbacks* callbacks) { 119 blink::WebBluetoothReadValueCallbacks* callbacks) {
118 GetWebBluetoothService().RemoteCharacteristicReadValue( 120 GetWebBluetoothService().RemoteCharacteristicReadValue(
119 mojo::String::From(characteristic_instance_id), 121 mojo::String::From(characteristic_instance_id),
120 base::Bind(&WebBluetoothImpl::OnReadValueComplete, base::Unretained(this), 122 base::Bind(&WebBluetoothImpl::OnReadValueComplete, base::Unretained(this),
121 base::Passed(base::WrapUnique(callbacks)))); 123 base::Passed(base::WrapUnique(callbacks))));
122 } 124 }
123 125
124 void WebBluetoothImpl::writeValue( 126 void WebBluetoothImpl::characteristicWriteValue(
125 const blink::WebString& characteristic_instance_id, 127 const blink::WebString& characteristic_instance_id,
126 const blink::WebVector<uint8_t>& value, 128 const blink::WebVector<uint8_t>& value,
127 blink::WebBluetoothWriteValueCallbacks* callbacks) { 129 blink::WebBluetoothWriteValueCallbacks* callbacks) {
128 GetWebBluetoothService().RemoteCharacteristicWriteValue( 130 GetWebBluetoothService().RemoteCharacteristicWriteValue(
129 mojo::String::From(characteristic_instance_id), 131 mojo::String::From(characteristic_instance_id),
130 mojo::Array<uint8_t>::From(value), 132 mojo::Array<uint8_t>::From(value),
131 base::Bind(&WebBluetoothImpl::OnWriteValueComplete, 133 base::Bind(&WebBluetoothImpl::OnWriteValueComplete,
132 base::Unretained(this), value, 134 base::Unretained(this), value,
133 base::Passed(base::WrapUnique(callbacks)))); 135 base::Passed(base::WrapUnique(callbacks))));
134 } 136 }
(...skipping 11 matching lines...) Expand all
146 void WebBluetoothImpl::stopNotifications( 148 void WebBluetoothImpl::stopNotifications(
147 const blink::WebString& characteristic_instance_id, 149 const blink::WebString& characteristic_instance_id,
148 blink::WebBluetoothNotificationsCallbacks* callbacks) { 150 blink::WebBluetoothNotificationsCallbacks* callbacks) {
149 GetWebBluetoothService().RemoteCharacteristicStopNotifications( 151 GetWebBluetoothService().RemoteCharacteristicStopNotifications(
150 mojo::String::From(characteristic_instance_id), 152 mojo::String::From(characteristic_instance_id),
151 base::Bind(&WebBluetoothImpl::OnStopNotificationsComplete, 153 base::Bind(&WebBluetoothImpl::OnStopNotificationsComplete,
152 base::Unretained(this), 154 base::Unretained(this),
153 base::Passed(base::WrapUnique(callbacks)))); 155 base::Passed(base::WrapUnique(callbacks))));
154 } 156 }
155 157
158 void WebBluetoothImpl::getDescriptors(
159 const blink::WebString& characteristic_instance_id,
160 int32_t quantity,
161 const blink::WebString& descriptors_uuid,
162 blink::WebBluetoothGetDescriptorsCallbacks* callbacks) {
163 DCHECK(blink::mojom::IsKnownEnumValue(
164 static_cast<blink::mojom::WebBluetoothGATTQueryQuantity>(quantity)));
165
166 GetWebBluetoothService().RemoteCharacteristicGetDescriptors(
167 mojo::String::From(characteristic_instance_id),
168 static_cast<blink::mojom::WebBluetoothGATTQueryQuantity>(quantity),
169 descriptors_uuid.isEmpty()
170 ? base::nullopt
171 : base::make_optional(device::BluetoothUUID(descriptors_uuid.utf8())),
172 base::Bind(&WebBluetoothImpl::OnGetDescriptorsComplete,
173 base::Unretained(this), characteristic_instance_id,
174 base::Passed(base::WrapUnique(callbacks))));
175 }
176
177 void WebBluetoothImpl::descriptorReadValue(
178 const blink::WebString& descriptor_instance_id,
179 blink::WebBluetoothReadValueCallbacks* callbacks) {
180 GetWebBluetoothService().RemoteDescriptorReadValue(
181 mojo::String::From(descriptor_instance_id),
182 base::Bind(&WebBluetoothImpl::OnReadValueComplete, base::Unretained(this),
183 base::Passed(base::WrapUnique(callbacks))));
184 }
185
186 void WebBluetoothImpl::descriptorWriteValue(
187 const blink::WebString& descriptor_instance_id,
188 const blink::WebVector<uint8_t>& value,
189 blink::WebBluetoothWriteValueCallbacks* callbacks) {
190 GetWebBluetoothService().RemoteDescriptorWriteValue(
191 mojo::String::From(descriptor_instance_id),
192 mojo::Array<uint8_t>::From(value),
193 base::Bind(&WebBluetoothImpl::OnWriteValueComplete,
194 base::Unretained(this), value,
195 base::Passed(base::WrapUnique(callbacks))));
196 }
197
156 void WebBluetoothImpl::characteristicObjectRemoved( 198 void WebBluetoothImpl::characteristicObjectRemoved(
157 const blink::WebString& characteristic_instance_id, 199 const blink::WebString& characteristic_instance_id,
158 blink::WebBluetoothRemoteGATTCharacteristic* characteristic) { 200 blink::WebBluetoothRemoteGATTCharacteristic* characteristic) {
159 active_characteristics_.erase(characteristic_instance_id.utf8()); 201 active_characteristics_.erase(characteristic_instance_id.utf8());
160 } 202 }
161 203
162 void WebBluetoothImpl::registerCharacteristicObject( 204 void WebBluetoothImpl::registerCharacteristicObject(
163 const blink::WebString& characteristic_instance_id, 205 const blink::WebString& characteristic_instance_id,
164 blink::WebBluetoothRemoteGATTCharacteristic* characteristic) { 206 blink::WebBluetoothRemoteGATTCharacteristic* characteristic) {
165 // TODO(ortuno): After the Bluetooth Tree is implemented, there will 207 // TODO(ortuno): After the Bluetooth Tree is implemented, there will
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 blink::WebString::fromUTF8(characteristics[i]->instance_id), 301 blink::WebString::fromUTF8(characteristics[i]->instance_id),
260 blink::WebString::fromUTF8(characteristics[i]->uuid), 302 blink::WebString::fromUTF8(characteristics[i]->uuid),
261 characteristics[i]->properties); 303 characteristics[i]->properties);
262 } 304 }
263 callbacks->onSuccess(promise_characteristics); 305 callbacks->onSuccess(promise_characteristics);
264 } else { 306 } else {
265 callbacks->onError(ToInt32(result)); 307 callbacks->onError(ToInt32(result));
266 } 308 }
267 } 309 }
268 310
311 void WebBluetoothImpl::OnGetDescriptorsComplete(
312 const blink::WebString& characteristic_instance_id,
313 std::unique_ptr<blink::WebBluetoothGetDescriptorsCallbacks> callbacks,
314 blink::mojom::WebBluetoothResult result,
315 mojo::Array<blink::mojom::WebBluetoothRemoteGATTDescriptorPtr>
316 descriptors) {
317 if (result == blink::mojom::WebBluetoothResult::SUCCESS) {
318 // TODO(dcheng): This WebVector should use smart pointers.
319 blink::WebVector<blink::WebBluetoothRemoteGATTDescriptorInit*>
320 promise_descriptors(descriptors.size());
321
322 for (size_t i = 0; i < descriptors.size(); i++) {
323 promise_descriptors[i] = new blink::WebBluetoothRemoteGATTDescriptorInit(
324 characteristic_instance_id,
325 blink::WebString::fromUTF8(descriptors[i]->instance_id),
326 blink::WebString::fromUTF8(descriptors[i]->uuid));
327 }
328 callbacks->onSuccess(promise_descriptors);
329 } else {
330 callbacks->onError(ToInt32(result));
331 }
332 }
333
269 void WebBluetoothImpl::OnReadValueComplete( 334 void WebBluetoothImpl::OnReadValueComplete(
270 std::unique_ptr<blink::WebBluetoothReadValueCallbacks> callbacks, 335 std::unique_ptr<blink::WebBluetoothReadValueCallbacks> callbacks,
271 blink::mojom::WebBluetoothResult result, 336 blink::mojom::WebBluetoothResult result,
272 mojo::Array<uint8_t> value) { 337 mojo::Array<uint8_t> value) {
273 if (result == blink::mojom::WebBluetoothResult::SUCCESS) { 338 if (result == blink::mojom::WebBluetoothResult::SUCCESS) {
274 callbacks->onSuccess(value.PassStorage()); 339 callbacks->onSuccess(value.PassStorage());
275 } else { 340 } else {
276 callbacks->onError(ToInt32(result)); 341 callbacks->onError(ToInt32(result));
277 } 342 }
278 } 343 }
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 // Create an associated interface ptr and pass it to the WebBluetoothService 383 // Create an associated interface ptr and pass it to the WebBluetoothService
319 // so that it can send us events without us prompting. 384 // so that it can send us events without us prompting.
320 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo ptr_info; 385 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo ptr_info;
321 binding_.Bind(&ptr_info, web_bluetooth_service_.associated_group()); 386 binding_.Bind(&ptr_info, web_bluetooth_service_.associated_group());
322 web_bluetooth_service_->SetClient(std::move(ptr_info)); 387 web_bluetooth_service_->SetClient(std::move(ptr_info));
323 } 388 }
324 return *web_bluetooth_service_; 389 return *web_bluetooth_service_;
325 } 390 }
326 391
327 } // namespace content 392 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698