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

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

Issue 2466223002: Implement WebBluetooth getDescriptor[s] (Closed)
Patch Set: Ensure that we throw a kGattServerNotConnected error if getDescriptor[s] is called while not connec… Created 4 years, 1 month 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
166 // only be one object per characteristic. But for now we replace 208 // only be one object per characteristic. But for now we replace
167 // the previous object. 209 // the previous object.
168 // https://crbug.com/495270 210 // https://crbug.com/495270
169 active_characteristics_[characteristic_instance_id.utf8()] = characteristic; 211 active_characteristics_[characteristic_instance_id.utf8()] = characteristic;
170 } 212 }
171 213
172 void WebBluetoothImpl::RemoteCharacteristicValueChanged( 214 void WebBluetoothImpl::RemoteCharacteristicValueChanged(
173 const mojo::String& characteristic_instance_id, 215 const mojo::String& characteristic_instance_id,
174 mojo::Array<uint8_t> value) { 216 mojo::Array<uint8_t> value) {
175 // We post a task so that the event is fired after any pending promises have 217 // We post a task so that the event is fired after any pending promises have
176 // resolved. 218 // resolved.
177 base::ThreadTaskRunnerHandle::Get()->PostTask( 219 base::ThreadTaskRunnerHandle::Get()->PostTask(
178 FROM_HERE, 220 FROM_HERE,
179 base::Bind(&WebBluetoothImpl::DispatchCharacteristicValueChanged, 221 base::Bind(&WebBluetoothImpl::DispatchCharacteristicValueChanged,
180 base::Unretained(this), characteristic_instance_id, 222 base::Unretained(this), characteristic_instance_id,
181 value.PassStorage())); 223 value.PassStorage()));
182 } 224 }
183 225
226 void WebBluetoothImpl::RemoteDescriptorValueChanged(
227 const mojo::String& descriptor_instance_id,
228 mojo::Array<uint8_t> value) {
229 // We post a task so that the event is fired after any pending promises have
230 // resolved.
231 base::ThreadTaskRunnerHandle::Get()->PostTask(
232 FROM_HERE, base::Bind(&WebBluetoothImpl::DispatchDescriptorValueChanged,
233 base::Unretained(this), descriptor_instance_id,
234 value.PassStorage()));
235 }
236
237 void WebBluetoothImpl::descriptorObjectRemoved(
238 const blink::WebString& descriptor_instance_id,
239 blink::WebBluetoothRemoteGATTDescriptor* descriptor) {
240 active_descriptors_.erase(descriptor_instance_id.utf8());
241 }
242
243 void WebBluetoothImpl::registerDescriptorObject(
244 const blink::WebString& descriptor_instance_id,
245 blink::WebBluetoothRemoteGATTDescriptor* descriptor) {
246 // TODO(dft): After the Bluetooth Tree is implemented, there will
247 // only be one object per descriptor. But for now we replace
248 // the previous object.
249 // https://crbug.com/xxx
250 active_descriptors_[descriptor_instance_id.utf8()] = descriptor;
251 }
252
184 void WebBluetoothImpl::OnRequestDeviceComplete( 253 void WebBluetoothImpl::OnRequestDeviceComplete(
185 std::unique_ptr<blink::WebBluetoothRequestDeviceCallbacks> callbacks, 254 std::unique_ptr<blink::WebBluetoothRequestDeviceCallbacks> callbacks,
186 const blink::mojom::WebBluetoothResult result, 255 const blink::mojom::WebBluetoothResult result,
187 blink::mojom::WebBluetoothDevicePtr device) { 256 blink::mojom::WebBluetoothDevicePtr device) {
188 if (result == blink::mojom::WebBluetoothResult::SUCCESS) { 257 if (result == blink::mojom::WebBluetoothResult::SUCCESS) {
189 callbacks->onSuccess(base::MakeUnique<blink::WebBluetoothDeviceInit>( 258 callbacks->onSuccess(base::MakeUnique<blink::WebBluetoothDeviceInit>(
190 blink::WebString::fromUTF8(device->id.str()), 259 blink::WebString::fromUTF8(device->id.str()),
191 device->name.is_null() ? blink::WebString() 260 device->name.is_null() ? blink::WebString()
192 : blink::WebString::fromUTF8(device->name))); 261 : blink::WebString::fromUTF8(device->name)));
193 } else { 262 } else {
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 blink::WebString::fromUTF8(characteristics[i]->instance_id), 328 blink::WebString::fromUTF8(characteristics[i]->instance_id),
260 blink::WebString::fromUTF8(characteristics[i]->uuid), 329 blink::WebString::fromUTF8(characteristics[i]->uuid),
261 characteristics[i]->properties); 330 characteristics[i]->properties);
262 } 331 }
263 callbacks->onSuccess(promise_characteristics); 332 callbacks->onSuccess(promise_characteristics);
264 } else { 333 } else {
265 callbacks->onError(ToInt32(result)); 334 callbacks->onError(ToInt32(result));
266 } 335 }
267 } 336 }
268 337
338 void WebBluetoothImpl::OnGetDescriptorsComplete(
339 const blink::WebString& characteristic_instance_id,
340 std::unique_ptr<blink::WebBluetoothGetDescriptorsCallbacks> callbacks,
341 blink::mojom::WebBluetoothResult result,
342 mojo::Array<blink::mojom::WebBluetoothRemoteGATTDescriptorPtr>
343 descriptors) {
344 if (result == blink::mojom::WebBluetoothResult::SUCCESS) {
345 // TODO(dcheng): This WebVector should use smart pointers.
346 blink::WebVector<blink::WebBluetoothRemoteGATTDescriptorInit*>
347 promise_descriptors(descriptors.size());
348
349 for (size_t i = 0; i < descriptors.size(); i++) {
350 promise_descriptors[i] = new blink::WebBluetoothRemoteGATTDescriptorInit(
351 characteristic_instance_id,
352 blink::WebString::fromUTF8(descriptors[i]->instance_id),
353 blink::WebString::fromUTF8(descriptors[i]->uuid));
354 }
355 callbacks->onSuccess(promise_descriptors);
356 } else {
357 callbacks->onError(ToInt32(result));
358 }
359 }
360
269 void WebBluetoothImpl::OnReadValueComplete( 361 void WebBluetoothImpl::OnReadValueComplete(
270 std::unique_ptr<blink::WebBluetoothReadValueCallbacks> callbacks, 362 std::unique_ptr<blink::WebBluetoothReadValueCallbacks> callbacks,
271 blink::mojom::WebBluetoothResult result, 363 blink::mojom::WebBluetoothResult result,
272 mojo::Array<uint8_t> value) { 364 mojo::Array<uint8_t> value) {
273 if (result == blink::mojom::WebBluetoothResult::SUCCESS) { 365 if (result == blink::mojom::WebBluetoothResult::SUCCESS) {
274 callbacks->onSuccess(value.PassStorage()); 366 callbacks->onSuccess(value.PassStorage());
275 } else { 367 } else {
276 callbacks->onError(ToInt32(result)); 368 callbacks->onError(ToInt32(result));
277 } 369 }
278 } 370 }
(...skipping 26 matching lines...) Expand all
305 397
306 void WebBluetoothImpl::DispatchCharacteristicValueChanged( 398 void WebBluetoothImpl::DispatchCharacteristicValueChanged(
307 const std::string& characteristic_instance_id, 399 const std::string& characteristic_instance_id,
308 const std::vector<uint8_t>& value) { 400 const std::vector<uint8_t>& value) {
309 auto active_iter = active_characteristics_.find(characteristic_instance_id); 401 auto active_iter = active_characteristics_.find(characteristic_instance_id);
310 if (active_iter != active_characteristics_.end()) { 402 if (active_iter != active_characteristics_.end()) {
311 active_iter->second->dispatchCharacteristicValueChanged(value); 403 active_iter->second->dispatchCharacteristicValueChanged(value);
312 } 404 }
313 } 405 }
314 406
407 void WebBluetoothImpl::DispatchDescriptorValueChanged(
408 const std::string& descriptor_instance_id,
409 const std::vector<uint8_t>& value) {
410 auto active_iter = active_descriptors_.find(descriptor_instance_id);
411 if (active_iter != active_descriptors_.end()) {
412 active_iter->second->dispatchDescriptorValueChanged(value);
413 }
414 }
415
315 blink::mojom::WebBluetoothService& WebBluetoothImpl::GetWebBluetoothService() { 416 blink::mojom::WebBluetoothService& WebBluetoothImpl::GetWebBluetoothService() {
316 if (!web_bluetooth_service_) { 417 if (!web_bluetooth_service_) {
317 remote_interfaces_->GetInterface(mojo::GetProxy(&web_bluetooth_service_)); 418 remote_interfaces_->GetInterface(mojo::GetProxy(&web_bluetooth_service_));
318 // Create an associated interface ptr and pass it to the WebBluetoothService 419 // Create an associated interface ptr and pass it to the WebBluetoothService
319 // so that it can send us events without us prompting. 420 // so that it can send us events without us prompting.
320 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo ptr_info; 421 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo ptr_info;
321 binding_.Bind(&ptr_info, web_bluetooth_service_.associated_group()); 422 binding_.Bind(&ptr_info, web_bluetooth_service_.associated_group());
322 web_bluetooth_service_->SetClient(std::move(ptr_info)); 423 web_bluetooth_service_->SetClient(std::move(ptr_info));
323 } 424 }
324 return *web_bluetooth_service_; 425 return *web_bluetooth_service_;
325 } 426 }
326 427
327 } // namespace content 428 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698