Chromium Code Reviews| 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 shouldn't use non-blink mojo bindings so we can't return a non-blink | |
|
Jeffrey Yasskin
2016/07/13 20:34:58
Maybe "Blink can't use non-blink mojo enums like b
ortuno
2016/07/13 22:47:28
Done.
| |
| 31 // enum. For that reason we cast to int32_t. | |
| 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) { |
| 71 GetWebBluetoothService().RemoteServerGetPrimaryServices( | 80 GetWebBluetoothService().RemoteServerGetPrimaryServices( |
| 72 mojo::String::From(device_id), quantity, | 81 mojo::String::From(device_id), |
| 82 static_cast<blink::mojom::WebBluetoothGATTQueryQuantity>(quantity), | |
| 73 services_uuid.isEmpty() | 83 services_uuid.isEmpty() |
| 74 ? base::nullopt | 84 ? base::nullopt |
| 75 : base::make_optional(device::BluetoothUUID(services_uuid.utf8())), | 85 : base::make_optional(device::BluetoothUUID(services_uuid.utf8())), |
| 76 base::Bind(&WebBluetoothImpl::OnGetPrimaryServicesComplete, | 86 base::Bind(&WebBluetoothImpl::OnGetPrimaryServicesComplete, |
| 77 base::Unretained(this), device_id, | 87 base::Unretained(this), device_id, |
| 78 base::Passed(base::WrapUnique(callbacks)))); | 88 base::Passed(base::WrapUnique(callbacks)))); |
| 79 } | 89 } |
| 80 | 90 |
| 81 void WebBluetoothImpl::getCharacteristics( | 91 void WebBluetoothImpl::getCharacteristics( |
| 82 const blink::WebString& service_instance_id, | 92 const blink::WebString& service_instance_id, |
| 83 blink::mojom::WebBluetoothGATTQueryQuantity quantity, | 93 int32_t quantity, |
| 84 const blink::WebString& characteristics_uuid, | 94 const blink::WebString& characteristics_uuid, |
| 85 blink::WebBluetoothGetCharacteristicsCallbacks* callbacks) { | 95 blink::WebBluetoothGetCharacteristicsCallbacks* callbacks) { |
| 86 GetWebBluetoothService().RemoteServiceGetCharacteristics( | 96 GetWebBluetoothService().RemoteServiceGetCharacteristics( |
| 87 mojo::String::From(service_instance_id), quantity, | 97 mojo::String::From(service_instance_id), |
| 98 static_cast<blink::mojom::WebBluetoothGATTQueryQuantity>(quantity), | |
|
Jeffrey Yasskin
2016/07/13 20:34:58
Could you CHECK(IsKnownEnumValue(static_cast<blink
ortuno
2016/07/13 22:47:28
I could but I'm not sure what would be the benefit
Jeffrey Yasskin
2016/07/13 22:56:14
It makes sure that we match enums across the bound
ortuno
2016/07/13 23:12:39
Done.
| |
| 88 characteristics_uuid.isEmpty() | 99 characteristics_uuid.isEmpty() |
| 89 ? base::nullopt | 100 ? base::nullopt |
| 90 : base::make_optional( | 101 : base::make_optional( |
| 91 device::BluetoothUUID(characteristics_uuid.utf8())), | 102 device::BluetoothUUID(characteristics_uuid.utf8())), |
| 92 base::Bind(&WebBluetoothImpl::OnGetCharacteristicsComplete, | 103 base::Bind(&WebBluetoothImpl::OnGetCharacteristicsComplete, |
| 93 base::Unretained(this), service_instance_id, | 104 base::Unretained(this), service_instance_id, |
| 94 base::Passed(base::WrapUnique(callbacks)))); | 105 base::Passed(base::WrapUnique(callbacks)))); |
| 95 } | 106 } |
| 96 | 107 |
| 97 void WebBluetoothImpl::readValue( | 108 void WebBluetoothImpl::readValue( |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 169 blink::mojom::WebBluetoothDevicePtr device) { | 180 blink::mojom::WebBluetoothDevicePtr device) { |
| 170 if (error == blink::mojom::WebBluetoothError::SUCCESS) { | 181 if (error == blink::mojom::WebBluetoothError::SUCCESS) { |
| 171 blink::WebVector<blink::WebString> uuids(device->uuids.size()); | 182 blink::WebVector<blink::WebString> uuids(device->uuids.size()); |
| 172 for (size_t i = 0; i < device->uuids.size(); ++i) | 183 for (size_t i = 0; i < device->uuids.size(); ++i) |
| 173 uuids[i] = blink::WebString::fromUTF8(device->uuids[i]); | 184 uuids[i] = blink::WebString::fromUTF8(device->uuids[i]); |
| 174 | 185 |
| 175 callbacks->onSuccess(base::WrapUnique(new blink::WebBluetoothDeviceInit( | 186 callbacks->onSuccess(base::WrapUnique(new blink::WebBluetoothDeviceInit( |
| 176 blink::WebString::fromUTF8(device->id), | 187 blink::WebString::fromUTF8(device->id), |
| 177 blink::WebString::fromUTF8(device->name), uuids))); | 188 blink::WebString::fromUTF8(device->name), uuids))); |
| 178 } else { | 189 } else { |
| 179 callbacks->onError(error); | 190 callbacks->onError(ToInt32(error)); |
| 180 } | 191 } |
| 181 } | 192 } |
| 182 | 193 |
| 183 void WebBluetoothImpl::GattServerDisconnected(const mojo::String& device_id) { | 194 void WebBluetoothImpl::GattServerDisconnected(const mojo::String& device_id) { |
| 184 auto device_iter = connected_devices_.find(device_id); | 195 auto device_iter = connected_devices_.find(device_id); |
| 185 if (device_iter != connected_devices_.end()) { | 196 if (device_iter != connected_devices_.end()) { |
| 186 // Remove device from the map before calling dispatchGattServerDisconnected | 197 // Remove device from the map before calling dispatchGattServerDisconnected |
| 187 // to avoid removing a device the gattserverdisconnected event handler might | 198 // to avoid removing a device the gattserverdisconnected event handler might |
| 188 // have re-connected. | 199 // have re-connected. |
| 189 blink::WebBluetoothDevice* device = device_iter->second; | 200 blink::WebBluetoothDevice* device = device_iter->second; |
| 190 connected_devices_.erase(device_iter); | 201 connected_devices_.erase(device_iter); |
| 191 device->dispatchGattServerDisconnected(); | 202 device->dispatchGattServerDisconnected(); |
| 192 } | 203 } |
| 193 } | 204 } |
| 194 | 205 |
| 195 void WebBluetoothImpl::OnConnectComplete( | 206 void WebBluetoothImpl::OnConnectComplete( |
| 196 std::unique_ptr<blink::WebBluetoothRemoteGATTServerConnectCallbacks> | 207 std::unique_ptr<blink::WebBluetoothRemoteGATTServerConnectCallbacks> |
| 197 callbacks, | 208 callbacks, |
| 198 blink::mojom::WebBluetoothError error) { | 209 blink::mojom::WebBluetoothError error) { |
| 199 if (error == blink::mojom::WebBluetoothError::SUCCESS) { | 210 if (error == blink::mojom::WebBluetoothError::SUCCESS) { |
| 200 callbacks->onSuccess(); | 211 callbacks->onSuccess(); |
| 201 } else { | 212 } else { |
| 202 callbacks->onError(error); | 213 callbacks->onError(ToInt32(error)); |
| 203 } | 214 } |
| 204 } | 215 } |
| 205 | 216 |
| 206 void WebBluetoothImpl::OnGetPrimaryServicesComplete( | 217 void WebBluetoothImpl::OnGetPrimaryServicesComplete( |
| 207 const blink::WebString& device_id, | 218 const blink::WebString& device_id, |
| 208 std::unique_ptr<blink::WebBluetoothGetPrimaryServicesCallbacks> callbacks, | 219 std::unique_ptr<blink::WebBluetoothGetPrimaryServicesCallbacks> callbacks, |
| 209 blink::mojom::WebBluetoothError error, | 220 blink::mojom::WebBluetoothError error, |
| 210 mojo::Array<blink::mojom::WebBluetoothRemoteGATTServicePtr> services) { | 221 mojo::Array<blink::mojom::WebBluetoothRemoteGATTServicePtr> services) { |
| 211 if (error == blink::mojom::WebBluetoothError::SUCCESS) { | 222 if (error == blink::mojom::WebBluetoothError::SUCCESS) { |
| 212 // TODO(dcheng): This WebVector should use smart pointers. | 223 // TODO(dcheng): This WebVector should use smart pointers. |
| 213 blink::WebVector<blink::WebBluetoothRemoteGATTService*> promise_services( | 224 blink::WebVector<blink::WebBluetoothRemoteGATTService*> promise_services( |
| 214 services.size()); | 225 services.size()); |
| 215 | 226 |
| 216 for (size_t i = 0; i < services.size(); i++) { | 227 for (size_t i = 0; i < services.size(); i++) { |
| 217 promise_services[i] = new blink::WebBluetoothRemoteGATTService( | 228 promise_services[i] = new blink::WebBluetoothRemoteGATTService( |
| 218 blink::WebString::fromUTF8(services[i]->instance_id), | 229 blink::WebString::fromUTF8(services[i]->instance_id), |
| 219 blink::WebString::fromUTF8(services[i]->uuid), true /* isPrimary */, | 230 blink::WebString::fromUTF8(services[i]->uuid), true /* isPrimary */, |
| 220 device_id); | 231 device_id); |
| 221 } | 232 } |
| 222 callbacks->onSuccess(promise_services); | 233 callbacks->onSuccess(promise_services); |
| 223 } else { | 234 } else { |
| 224 callbacks->onError(error); | 235 callbacks->onError(ToInt32(error)); |
| 225 } | 236 } |
| 226 } | 237 } |
| 227 | 238 |
| 228 void WebBluetoothImpl::OnGetCharacteristicsComplete( | 239 void WebBluetoothImpl::OnGetCharacteristicsComplete( |
| 229 const blink::WebString& service_instance_id, | 240 const blink::WebString& service_instance_id, |
| 230 std::unique_ptr<blink::WebBluetoothGetCharacteristicsCallbacks> callbacks, | 241 std::unique_ptr<blink::WebBluetoothGetCharacteristicsCallbacks> callbacks, |
| 231 blink::mojom::WebBluetoothError error, | 242 blink::mojom::WebBluetoothError error, |
| 232 mojo::Array<blink::mojom::WebBluetoothRemoteGATTCharacteristicPtr> | 243 mojo::Array<blink::mojom::WebBluetoothRemoteGATTCharacteristicPtr> |
| 233 characteristics) { | 244 characteristics) { |
| 234 if (error == blink::mojom::WebBluetoothError::SUCCESS) { | 245 if (error == blink::mojom::WebBluetoothError::SUCCESS) { |
| 235 // TODO(dcheng): This WebVector should use smart pointers. | 246 // TODO(dcheng): This WebVector should use smart pointers. |
| 236 blink::WebVector<blink::WebBluetoothRemoteGATTCharacteristicInit*> | 247 blink::WebVector<blink::WebBluetoothRemoteGATTCharacteristicInit*> |
| 237 promise_characteristics(characteristics.size()); | 248 promise_characteristics(characteristics.size()); |
| 238 | 249 |
| 239 for (size_t i = 0; i < characteristics.size(); i++) { | 250 for (size_t i = 0; i < characteristics.size(); i++) { |
| 240 promise_characteristics[i] = | 251 promise_characteristics[i] = |
| 241 new blink::WebBluetoothRemoteGATTCharacteristicInit( | 252 new blink::WebBluetoothRemoteGATTCharacteristicInit( |
| 242 service_instance_id, | 253 service_instance_id, |
| 243 blink::WebString::fromUTF8(characteristics[i]->instance_id), | 254 blink::WebString::fromUTF8(characteristics[i]->instance_id), |
| 244 blink::WebString::fromUTF8(characteristics[i]->uuid), | 255 blink::WebString::fromUTF8(characteristics[i]->uuid), |
| 245 characteristics[i]->properties); | 256 characteristics[i]->properties); |
| 246 } | 257 } |
| 247 callbacks->onSuccess(promise_characteristics); | 258 callbacks->onSuccess(promise_characteristics); |
| 248 } else { | 259 } else { |
| 249 callbacks->onError(error); | 260 callbacks->onError(ToInt32(error)); |
| 250 } | 261 } |
| 251 } | 262 } |
| 252 | 263 |
| 253 void WebBluetoothImpl::OnReadValueComplete( | 264 void WebBluetoothImpl::OnReadValueComplete( |
| 254 std::unique_ptr<blink::WebBluetoothReadValueCallbacks> callbacks, | 265 std::unique_ptr<blink::WebBluetoothReadValueCallbacks> callbacks, |
| 255 blink::mojom::WebBluetoothError error, | 266 blink::mojom::WebBluetoothError error, |
| 256 mojo::Array<uint8_t> value) { | 267 mojo::Array<uint8_t> value) { |
| 257 if (error == blink::mojom::WebBluetoothError::SUCCESS) { | 268 if (error == blink::mojom::WebBluetoothError::SUCCESS) { |
| 258 callbacks->onSuccess(value.PassStorage()); | 269 callbacks->onSuccess(value.PassStorage()); |
| 259 } else { | 270 } else { |
| 260 callbacks->onError(error); | 271 callbacks->onError(ToInt32(error)); |
| 261 } | 272 } |
| 262 } | 273 } |
| 263 | 274 |
| 264 void WebBluetoothImpl::OnWriteValueComplete( | 275 void WebBluetoothImpl::OnWriteValueComplete( |
| 265 const blink::WebVector<uint8_t>& value, | 276 const blink::WebVector<uint8_t>& value, |
| 266 std::unique_ptr<blink::WebBluetoothWriteValueCallbacks> callbacks, | 277 std::unique_ptr<blink::WebBluetoothWriteValueCallbacks> callbacks, |
| 267 blink::mojom::WebBluetoothError error) { | 278 blink::mojom::WebBluetoothError error) { |
| 268 if (error == blink::mojom::WebBluetoothError::SUCCESS) { | 279 if (error == blink::mojom::WebBluetoothError::SUCCESS) { |
| 269 callbacks->onSuccess(value); | 280 callbacks->onSuccess(value); |
| 270 } else { | 281 } else { |
| 271 callbacks->onError(error); | 282 callbacks->onError(ToInt32(error)); |
| 272 } | 283 } |
| 273 } | 284 } |
| 274 | 285 |
| 275 void WebBluetoothImpl::OnStartNotificationsComplete( | 286 void WebBluetoothImpl::OnStartNotificationsComplete( |
| 276 std::unique_ptr<blink::WebBluetoothNotificationsCallbacks> callbacks, | 287 std::unique_ptr<blink::WebBluetoothNotificationsCallbacks> callbacks, |
| 277 blink::mojom::WebBluetoothError error) { | 288 blink::mojom::WebBluetoothError error) { |
| 278 if (error == blink::mojom::WebBluetoothError::SUCCESS) { | 289 if (error == blink::mojom::WebBluetoothError::SUCCESS) { |
| 279 callbacks->onSuccess(); | 290 callbacks->onSuccess(); |
| 280 } else { | 291 } else { |
| 281 callbacks->onError(error); | 292 callbacks->onError(ToInt32(error)); |
| 282 } | 293 } |
| 283 } | 294 } |
| 284 | 295 |
| 285 void WebBluetoothImpl::OnStopNotificationsComplete( | 296 void WebBluetoothImpl::OnStopNotificationsComplete( |
| 286 std::unique_ptr<blink::WebBluetoothNotificationsCallbacks> callbacks) { | 297 std::unique_ptr<blink::WebBluetoothNotificationsCallbacks> callbacks) { |
| 287 callbacks->onSuccess(); | 298 callbacks->onSuccess(); |
| 288 } | 299 } |
| 289 | 300 |
| 290 void WebBluetoothImpl::DispatchCharacteristicValueChanged( | 301 void WebBluetoothImpl::DispatchCharacteristicValueChanged( |
| 291 const std::string& characteristic_instance_id, | 302 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 | 313 // Create an associated interface ptr and pass it to the WebBluetoothService |
| 303 // so that it can send us events without us prompting. | 314 // so that it can send us events without us prompting. |
| 304 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo ptr_info; | 315 blink::mojom::WebBluetoothServiceClientAssociatedPtrInfo ptr_info; |
| 305 binding_.Bind(&ptr_info, web_bluetooth_service_.associated_group()); | 316 binding_.Bind(&ptr_info, web_bluetooth_service_.associated_group()); |
| 306 web_bluetooth_service_->SetClient(std::move(ptr_info)); | 317 web_bluetooth_service_->SetClient(std::move(ptr_info)); |
| 307 } | 318 } |
| 308 return *web_bluetooth_service_; | 319 return *web_bluetooth_service_; |
| 309 } | 320 } |
| 310 | 321 |
| 311 } // namespace content | 322 } // namespace content |
| OLD | NEW |