| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "modules/bluetooth/Bluetooth.h" | 5 #include "modules/bluetooth/Bluetooth.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 7 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
| 8 #include "bindings/core/v8/ScriptPromise.h" | 8 #include "bindings/core/v8/ScriptPromise.h" |
| 9 #include "bindings/core/v8/ScriptPromiseResolver.h" | 9 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 10 #include "core/dom/DOMException.h" | 10 #include "core/dom/DOMException.h" |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 | 153 |
| 154 void Bluetooth::RequestDeviceCallback( | 154 void Bluetooth::RequestDeviceCallback( |
| 155 ScriptPromiseResolver* resolver, | 155 ScriptPromiseResolver* resolver, |
| 156 mojom::blink::WebBluetoothResult result, | 156 mojom::blink::WebBluetoothResult result, |
| 157 mojom::blink::WebBluetoothDevicePtr device) { | 157 mojom::blink::WebBluetoothDevicePtr device) { |
| 158 if (!resolver->getExecutionContext() || | 158 if (!resolver->getExecutionContext() || |
| 159 resolver->getExecutionContext()->isContextDestroyed()) | 159 resolver->getExecutionContext()->isContextDestroyed()) |
| 160 return; | 160 return; |
| 161 | 161 |
| 162 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { | 162 if (result == mojom::blink::WebBluetoothResult::SUCCESS) { |
| 163 BluetoothDevice* bluetoothDevice = getBluetoothDeviceRepresentingDevice( | 163 BluetoothDevice* bluetoothDevice = |
| 164 device->id->device_id, device->name, resolver); | 164 getBluetoothDeviceRepresentingDevice(std::move(device), resolver); |
| 165 resolver->resolve(bluetoothDevice); | 165 resolver->resolve(bluetoothDevice); |
| 166 } else { | 166 } else { |
| 167 resolver->reject(BluetoothError::take(resolver, result)); | 167 resolver->reject(BluetoothError::take(resolver, result)); |
| 168 } | 168 } |
| 169 } | 169 } |
| 170 | 170 |
| 171 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-requestdevice | 171 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-requestdevice |
| 172 ScriptPromise Bluetooth::requestDevice(ScriptState* scriptState, | 172 ScriptPromise Bluetooth::requestDevice(ScriptState* scriptState, |
| 173 const RequestDeviceOptions& options, | 173 const RequestDeviceOptions& options, |
| 174 ExceptionState& exceptionState) { | 174 ExceptionState& exceptionState) { |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 268 | 268 |
| 269 void Bluetooth::RemoteCharacteristicValueChanged( | 269 void Bluetooth::RemoteCharacteristicValueChanged( |
| 270 const WTF::String& characteristicInstanceId, | 270 const WTF::String& characteristicInstanceId, |
| 271 const WTF::Vector<uint8_t>& value) { | 271 const WTF::Vector<uint8_t>& value) { |
| 272 BluetoothRemoteGATTCharacteristic* characteristic = | 272 BluetoothRemoteGATTCharacteristic* characteristic = |
| 273 m_activeCharacteristics.get(characteristicInstanceId); | 273 m_activeCharacteristics.get(characteristicInstanceId); |
| 274 if (characteristic) | 274 if (characteristic) |
| 275 characteristic->dispatchCharacteristicValueChanged(value); | 275 characteristic->dispatchCharacteristicValueChanged(value); |
| 276 } | 276 } |
| 277 | 277 |
| 278 void Bluetooth::GattServerDisconnected( | 278 void Bluetooth::GattServerDisconnected(const WTF::String& deviceId) { |
| 279 mojom::blink::WebBluetoothDeviceIdPtr deviceId) { | 279 BluetoothDevice* device = m_connectedDevices.get(deviceId); |
| 280 BluetoothDevice* device = m_connectedDevices.get(deviceId->device_id); | |
| 281 if (device) { | 280 if (device) { |
| 282 // Remove device from the map before calling dispatchGattServerDisconnected | 281 // Remove device from the map before calling dispatchGattServerDisconnected |
| 283 // to avoid removing a device the gattserverdisconnected event handler might | 282 // to avoid removing a device the gattserverdisconnected event handler might |
| 284 // have re-connected. | 283 // have re-connected. |
| 285 m_connectedDevices.remove(deviceId->device_id); | 284 m_connectedDevices.remove(deviceId); |
| 286 device->dispatchGattServerDisconnected(); | 285 device->dispatchGattServerDisconnected(); |
| 287 } | 286 } |
| 288 } | 287 } |
| 289 | 288 |
| 290 BluetoothDevice* Bluetooth::getBluetoothDeviceRepresentingDevice( | 289 BluetoothDevice* Bluetooth::getBluetoothDeviceRepresentingDevice( |
| 291 const String& id, | 290 mojom::blink::WebBluetoothDevicePtr devicePtr, |
| 292 const String& name, | |
| 293 ScriptPromiseResolver* resolver) { | 291 ScriptPromiseResolver* resolver) { |
| 292 WTF::String id = devicePtr->id; |
| 294 BluetoothDevice* device = m_deviceInstanceMap.get(id); | 293 BluetoothDevice* device = m_deviceInstanceMap.get(id); |
| 295 if (!device) { | 294 if (!device) { |
| 296 device = BluetoothDevice::take(resolver, id, name, this); | 295 device = BluetoothDevice::take(resolver, std::move(devicePtr), this); |
| 297 auto result = m_deviceInstanceMap.add(id, device); | 296 auto result = m_deviceInstanceMap.add(id, device); |
| 298 DCHECK(result.isNewEntry); | 297 DCHECK(result.isNewEntry); |
| 299 } | 298 } |
| 300 return device; | 299 return device; |
| 301 } | 300 } |
| 302 | 301 |
| 303 } // namespace blink | 302 } // namespace blink |
| OLD | NEW |