| 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/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 namespace content { | 26 namespace content { |
| 27 | 27 |
| 28 namespace { |
| 29 |
| 30 // Blink can't use non-blink mojo enums like blink::mojom::WebBluetoothError, so |
| 31 // we pass it as an int32 across the boundary. |
| 32 int32_t ToInt32(blink::mojom::WebBluetoothError error) { |
| 33 return static_cast<int32_t>(error); |
| 34 } |
| 35 |
| 36 } // namespace |
| 37 |
| 28 WebBluetoothImpl::WebBluetoothImpl(shell::InterfaceProvider* remote_interfaces) | 38 WebBluetoothImpl::WebBluetoothImpl(shell::InterfaceProvider* remote_interfaces) |
| 29 : remote_interfaces_(remote_interfaces), binding_(this) {} | 39 : remote_interfaces_(remote_interfaces), binding_(this) {} |
| 30 | 40 |
| 31 WebBluetoothImpl::~WebBluetoothImpl() { | 41 WebBluetoothImpl::~WebBluetoothImpl() { |
| 32 } | 42 } |
| 33 | 43 |
| 34 void WebBluetoothImpl::requestDevice( | 44 void WebBluetoothImpl::requestDevice( |
| 35 const blink::WebRequestDeviceOptions& options, | 45 const blink::WebRequestDeviceOptions& options, |
| 36 blink::WebBluetoothRequestDeviceCallbacks* callbacks) { | 46 blink::WebBluetoothRequestDeviceCallbacks* callbacks) { |
| 37 GetWebBluetoothService().RequestDevice( | 47 GetWebBluetoothService().RequestDevice( |
| (...skipping 19 matching lines...) Expand all Loading... |
| 57 | 67 |
| 58 void WebBluetoothImpl::disconnect(const blink::WebString& device_id) { | 68 void WebBluetoothImpl::disconnect(const blink::WebString& device_id) { |
| 59 connected_devices_.erase(device_id.utf8()); | 69 connected_devices_.erase(device_id.utf8()); |
| 60 | 70 |
| 61 GetWebBluetoothService().RemoteServerDisconnect( | 71 GetWebBluetoothService().RemoteServerDisconnect( |
| 62 mojo::String::From(device_id)); | 72 mojo::String::From(device_id)); |
| 63 } | 73 } |
| 64 | 74 |
| 65 void WebBluetoothImpl::getPrimaryServices( | 75 void WebBluetoothImpl::getPrimaryServices( |
| 66 const blink::WebString& device_id, | 76 const blink::WebString& device_id, |
| 67 | 77 int32_t quantity, |
| 68 blink::mojom::WebBluetoothGATTQueryQuantity quantity, | |
| 69 const blink::WebString& services_uuid, | 78 const blink::WebString& services_uuid, |
| 70 blink::WebBluetoothGetPrimaryServicesCallbacks* callbacks) { | 79 blink::WebBluetoothGetPrimaryServicesCallbacks* callbacks) { |
| 80 DCHECK(blink::mojom::IsKnownEnumValue( |
| 81 static_cast<blink::mojom::WebBluetoothGATTQueryQuantity>(quantity))); |
| 71 GetWebBluetoothService().RemoteServerGetPrimaryServices( | 82 GetWebBluetoothService().RemoteServerGetPrimaryServices( |
| 72 mojo::String::From(device_id), quantity, | 83 mojo::String::From(device_id), |
| 84 static_cast<blink::mojom::WebBluetoothGATTQueryQuantity>(quantity), |
| 73 services_uuid.isEmpty() | 85 services_uuid.isEmpty() |
| 74 ? base::nullopt | 86 ? base::nullopt |
| 75 : base::make_optional(device::BluetoothUUID(services_uuid.utf8())), | 87 : base::make_optional(device::BluetoothUUID(services_uuid.utf8())), |
| 76 base::Bind(&WebBluetoothImpl::OnGetPrimaryServicesComplete, | 88 base::Bind(&WebBluetoothImpl::OnGetPrimaryServicesComplete, |
| 77 base::Unretained(this), device_id, | 89 base::Unretained(this), device_id, |
| 78 base::Passed(base::WrapUnique(callbacks)))); | 90 base::Passed(base::WrapUnique(callbacks)))); |
| 79 } | 91 } |
| 80 | 92 |
| 81 void WebBluetoothImpl::getCharacteristics( | 93 void WebBluetoothImpl::getCharacteristics( |
| 82 const blink::WebString& service_instance_id, | 94 const blink::WebString& service_instance_id, |
| 83 blink::mojom::WebBluetoothGATTQueryQuantity quantity, | 95 int32_t quantity, |
| 84 const blink::WebString& characteristics_uuid, | 96 const blink::WebString& characteristics_uuid, |
| 85 blink::WebBluetoothGetCharacteristicsCallbacks* callbacks) { | 97 blink::WebBluetoothGetCharacteristicsCallbacks* callbacks) { |
| 98 DCHECK(blink::mojom::IsKnownEnumValue( |
| 99 static_cast<blink::mojom::WebBluetoothGATTQueryQuantity>(quantity))); |
| 86 GetWebBluetoothService().RemoteServiceGetCharacteristics( | 100 GetWebBluetoothService().RemoteServiceGetCharacteristics( |
| 87 mojo::String::From(service_instance_id), quantity, | 101 mojo::String::From(service_instance_id), |
| 102 static_cast<blink::mojom::WebBluetoothGATTQueryQuantity>(quantity), |
| 88 characteristics_uuid.isEmpty() | 103 characteristics_uuid.isEmpty() |
| 89 ? base::nullopt | 104 ? base::nullopt |
| 90 : base::make_optional( | 105 : base::make_optional( |
| 91 device::BluetoothUUID(characteristics_uuid.utf8())), | 106 device::BluetoothUUID(characteristics_uuid.utf8())), |
| 92 base::Bind(&WebBluetoothImpl::OnGetCharacteristicsComplete, | 107 base::Bind(&WebBluetoothImpl::OnGetCharacteristicsComplete, |
| 93 base::Unretained(this), service_instance_id, | 108 base::Unretained(this), service_instance_id, |
| 94 base::Passed(base::WrapUnique(callbacks)))); | 109 base::Passed(base::WrapUnique(callbacks)))); |
| 95 } | 110 } |
| 96 | 111 |
| 97 void WebBluetoothImpl::readValue( | 112 void WebBluetoothImpl::readValue( |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 blink::mojom::WebBluetoothDevicePtr device) { | 184 blink::mojom::WebBluetoothDevicePtr device) { |
| 170 if (error == blink::mojom::WebBluetoothError::SUCCESS) { | 185 if (error == blink::mojom::WebBluetoothError::SUCCESS) { |
| 171 blink::WebVector<blink::WebString> uuids(device->uuids.size()); | 186 blink::WebVector<blink::WebString> uuids(device->uuids.size()); |
| 172 for (size_t i = 0; i < device->uuids.size(); ++i) | 187 for (size_t i = 0; i < device->uuids.size(); ++i) |
| 173 uuids[i] = blink::WebString::fromUTF8(device->uuids[i]); | 188 uuids[i] = blink::WebString::fromUTF8(device->uuids[i]); |
| 174 | 189 |
| 175 callbacks->onSuccess(base::WrapUnique(new blink::WebBluetoothDeviceInit( | 190 callbacks->onSuccess(base::WrapUnique(new blink::WebBluetoothDeviceInit( |
| 176 blink::WebString::fromUTF8(device->id), | 191 blink::WebString::fromUTF8(device->id), |
| 177 blink::WebString::fromUTF8(device->name), uuids))); | 192 blink::WebString::fromUTF8(device->name), uuids))); |
| 178 } else { | 193 } else { |
| 179 callbacks->onError(error); | 194 callbacks->onError(ToInt32(error)); |
| 180 } | 195 } |
| 181 } | 196 } |
| 182 | 197 |
| 183 void WebBluetoothImpl::GattServerDisconnected(const mojo::String& device_id) { | 198 void WebBluetoothImpl::GattServerDisconnected(const mojo::String& device_id) { |
| 184 auto device_iter = connected_devices_.find(device_id); | 199 auto device_iter = connected_devices_.find(device_id); |
| 185 if (device_iter != connected_devices_.end()) { | 200 if (device_iter != connected_devices_.end()) { |
| 186 // Remove device from the map before calling dispatchGattServerDisconnected | 201 // Remove device from the map before calling dispatchGattServerDisconnected |
| 187 // to avoid removing a device the gattserverdisconnected event handler might | 202 // to avoid removing a device the gattserverdisconnected event handler might |
| 188 // have re-connected. | 203 // have re-connected. |
| 189 blink::WebBluetoothDevice* device = device_iter->second; | 204 blink::WebBluetoothDevice* device = device_iter->second; |
| 190 connected_devices_.erase(device_iter); | 205 connected_devices_.erase(device_iter); |
| 191 device->dispatchGattServerDisconnected(); | 206 device->dispatchGattServerDisconnected(); |
| 192 } | 207 } |
| 193 } | 208 } |
| 194 | 209 |
| 195 void WebBluetoothImpl::OnConnectComplete( | 210 void WebBluetoothImpl::OnConnectComplete( |
| 196 std::unique_ptr<blink::WebBluetoothRemoteGATTServerConnectCallbacks> | 211 std::unique_ptr<blink::WebBluetoothRemoteGATTServerConnectCallbacks> |
| 197 callbacks, | 212 callbacks, |
| 198 blink::mojom::WebBluetoothError error) { | 213 blink::mojom::WebBluetoothError error) { |
| 199 if (error == blink::mojom::WebBluetoothError::SUCCESS) { | 214 if (error == blink::mojom::WebBluetoothError::SUCCESS) { |
| 200 callbacks->onSuccess(); | 215 callbacks->onSuccess(); |
| 201 } else { | 216 } else { |
| 202 callbacks->onError(error); | 217 callbacks->onError(ToInt32(error)); |
| 203 } | 218 } |
| 204 } | 219 } |
| 205 | 220 |
| 206 void WebBluetoothImpl::OnGetPrimaryServicesComplete( | 221 void WebBluetoothImpl::OnGetPrimaryServicesComplete( |
| 207 const blink::WebString& device_id, | 222 const blink::WebString& device_id, |
| 208 std::unique_ptr<blink::WebBluetoothGetPrimaryServicesCallbacks> callbacks, | 223 std::unique_ptr<blink::WebBluetoothGetPrimaryServicesCallbacks> callbacks, |
| 209 blink::mojom::WebBluetoothError error, | 224 blink::mojom::WebBluetoothError error, |
| 210 mojo::Array<blink::mojom::WebBluetoothRemoteGATTServicePtr> services) { | 225 mojo::Array<blink::mojom::WebBluetoothRemoteGATTServicePtr> services) { |
| 211 if (error == blink::mojom::WebBluetoothError::SUCCESS) { | 226 if (error == blink::mojom::WebBluetoothError::SUCCESS) { |
| 212 // TODO(dcheng): This WebVector should use smart pointers. | 227 // TODO(dcheng): This WebVector should use smart pointers. |
| 213 blink::WebVector<blink::WebBluetoothRemoteGATTService*> promise_services( | 228 blink::WebVector<blink::WebBluetoothRemoteGATTService*> promise_services( |
| 214 services.size()); | 229 services.size()); |
| 215 | 230 |
| 216 for (size_t i = 0; i < services.size(); i++) { | 231 for (size_t i = 0; i < services.size(); i++) { |
| 217 promise_services[i] = new blink::WebBluetoothRemoteGATTService( | 232 promise_services[i] = new blink::WebBluetoothRemoteGATTService( |
| 218 blink::WebString::fromUTF8(services[i]->instance_id), | 233 blink::WebString::fromUTF8(services[i]->instance_id), |
| 219 blink::WebString::fromUTF8(services[i]->uuid), true /* isPrimary */, | 234 blink::WebString::fromUTF8(services[i]->uuid), true /* isPrimary */, |
| 220 device_id); | 235 device_id); |
| 221 } | 236 } |
| 222 callbacks->onSuccess(promise_services); | 237 callbacks->onSuccess(promise_services); |
| 223 } else { | 238 } else { |
| 224 callbacks->onError(error); | 239 callbacks->onError(ToInt32(error)); |
| 225 } | 240 } |
| 226 } | 241 } |
| 227 | 242 |
| 228 void WebBluetoothImpl::OnGetCharacteristicsComplete( | 243 void WebBluetoothImpl::OnGetCharacteristicsComplete( |
| 229 const blink::WebString& service_instance_id, | 244 const blink::WebString& service_instance_id, |
| 230 std::unique_ptr<blink::WebBluetoothGetCharacteristicsCallbacks> callbacks, | 245 std::unique_ptr<blink::WebBluetoothGetCharacteristicsCallbacks> callbacks, |
| 231 blink::mojom::WebBluetoothError error, | 246 blink::mojom::WebBluetoothError error, |
| 232 mojo::Array<blink::mojom::WebBluetoothRemoteGATTCharacteristicPtr> | 247 mojo::Array<blink::mojom::WebBluetoothRemoteGATTCharacteristicPtr> |
| 233 characteristics) { | 248 characteristics) { |
| 234 if (error == blink::mojom::WebBluetoothError::SUCCESS) { | 249 if (error == blink::mojom::WebBluetoothError::SUCCESS) { |
| 235 // TODO(dcheng): This WebVector should use smart pointers. | 250 // TODO(dcheng): This WebVector should use smart pointers. |
| 236 blink::WebVector<blink::WebBluetoothRemoteGATTCharacteristicInit*> | 251 blink::WebVector<blink::WebBluetoothRemoteGATTCharacteristicInit*> |
| 237 promise_characteristics(characteristics.size()); | 252 promise_characteristics(characteristics.size()); |
| 238 | 253 |
| 239 for (size_t i = 0; i < characteristics.size(); i++) { | 254 for (size_t i = 0; i < characteristics.size(); i++) { |
| 240 promise_characteristics[i] = | 255 promise_characteristics[i] = |
| 241 new blink::WebBluetoothRemoteGATTCharacteristicInit( | 256 new blink::WebBluetoothRemoteGATTCharacteristicInit( |
| 242 service_instance_id, | 257 service_instance_id, |
| 243 blink::WebString::fromUTF8(characteristics[i]->instance_id), | 258 blink::WebString::fromUTF8(characteristics[i]->instance_id), |
| 244 blink::WebString::fromUTF8(characteristics[i]->uuid), | 259 blink::WebString::fromUTF8(characteristics[i]->uuid), |
| 245 characteristics[i]->properties); | 260 characteristics[i]->properties); |
| 246 } | 261 } |
| 247 callbacks->onSuccess(promise_characteristics); | 262 callbacks->onSuccess(promise_characteristics); |
| 248 } else { | 263 } else { |
| 249 callbacks->onError(error); | 264 callbacks->onError(ToInt32(error)); |
| 250 } | 265 } |
| 251 } | 266 } |
| 252 | 267 |
| 253 void WebBluetoothImpl::OnReadValueComplete( | 268 void WebBluetoothImpl::OnReadValueComplete( |
| 254 std::unique_ptr<blink::WebBluetoothReadValueCallbacks> callbacks, | 269 std::unique_ptr<blink::WebBluetoothReadValueCallbacks> callbacks, |
| 255 blink::mojom::WebBluetoothError error, | 270 blink::mojom::WebBluetoothError error, |
| 256 mojo::Array<uint8_t> value) { | 271 mojo::Array<uint8_t> value) { |
| 257 if (error == blink::mojom::WebBluetoothError::SUCCESS) { | 272 if (error == blink::mojom::WebBluetoothError::SUCCESS) { |
| 258 callbacks->onSuccess(value.PassStorage()); | 273 callbacks->onSuccess(value.PassStorage()); |
| 259 } else { | 274 } else { |
| 260 callbacks->onError(error); | 275 callbacks->onError(ToInt32(error)); |
| 261 } | 276 } |
| 262 } | 277 } |
| 263 | 278 |
| 264 void WebBluetoothImpl::OnWriteValueComplete( | 279 void WebBluetoothImpl::OnWriteValueComplete( |
| 265 const blink::WebVector<uint8_t>& value, | 280 const blink::WebVector<uint8_t>& value, |
| 266 std::unique_ptr<blink::WebBluetoothWriteValueCallbacks> callbacks, | 281 std::unique_ptr<blink::WebBluetoothWriteValueCallbacks> callbacks, |
| 267 blink::mojom::WebBluetoothError error) { | 282 blink::mojom::WebBluetoothError error) { |
| 268 if (error == blink::mojom::WebBluetoothError::SUCCESS) { | 283 if (error == blink::mojom::WebBluetoothError::SUCCESS) { |
| 269 callbacks->onSuccess(value); | 284 callbacks->onSuccess(value); |
| 270 } else { | 285 } else { |
| 271 callbacks->onError(error); | 286 callbacks->onError(ToInt32(error)); |
| 272 } | 287 } |
| 273 } | 288 } |
| 274 | 289 |
| 275 void WebBluetoothImpl::OnStartNotificationsComplete( | 290 void WebBluetoothImpl::OnStartNotificationsComplete( |
| 276 std::unique_ptr<blink::WebBluetoothNotificationsCallbacks> callbacks, | 291 std::unique_ptr<blink::WebBluetoothNotificationsCallbacks> callbacks, |
| 277 blink::mojom::WebBluetoothError error) { | 292 blink::mojom::WebBluetoothError error) { |
| 278 if (error == blink::mojom::WebBluetoothError::SUCCESS) { | 293 if (error == blink::mojom::WebBluetoothError::SUCCESS) { |
| 279 callbacks->onSuccess(); | 294 callbacks->onSuccess(); |
| 280 } else { | 295 } else { |
| 281 callbacks->onError(error); | 296 callbacks->onError(ToInt32(error)); |
| 282 } | 297 } |
| 283 } | 298 } |
| 284 | 299 |
| 285 void WebBluetoothImpl::OnStopNotificationsComplete( | 300 void WebBluetoothImpl::OnStopNotificationsComplete( |
| 286 std::unique_ptr<blink::WebBluetoothNotificationsCallbacks> callbacks) { | 301 std::unique_ptr<blink::WebBluetoothNotificationsCallbacks> callbacks) { |
| 287 callbacks->onSuccess(); | 302 callbacks->onSuccess(); |
| 288 } | 303 } |
| 289 | 304 |
| 290 void WebBluetoothImpl::DispatchCharacteristicValueChanged( | 305 void WebBluetoothImpl::DispatchCharacteristicValueChanged( |
| 291 const std::string& characteristic_instance_id, | 306 const std::string& characteristic_instance_id, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 302 // Create an associated interface ptr and pass it to the WebBluetoothService | 317 // Create an associated interface ptr and pass it to the WebBluetoothService |
| 303 // so that it can send us events without us prompting. | 318 // so that it can send us events without us prompting. |
| 304 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo ptr_info; | 319 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo ptr_info; |
| 305 binding_.Bind(&ptr_info, web_bluetooth_service_.associated_group()); | 320 binding_.Bind(&ptr_info, web_bluetooth_service_.associated_group()); |
| 306 web_bluetooth_service_->SetClient(std::move(ptr_info)); | 321 web_bluetooth_service_->SetClient(std::move(ptr_info)); |
| 307 } | 322 } |
| 308 return *web_bluetooth_service_; | 323 return *web_bluetooth_service_; |
| 309 } | 324 } |
| 310 | 325 |
| 311 } // namespace content | 326 } // namespace content |
| OLD | NEW |