Chromium Code Reviews| 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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 229 | 229 |
| 230 // Subsequent steps are handled in the browser process. | 230 // Subsequent steps are handled in the browser process. |
| 231 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 231 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
| 232 ScriptPromise promise = resolver->promise(); | 232 ScriptPromise promise = resolver->promise(); |
| 233 | 233 |
| 234 service()->RequestDevice( | 234 service()->RequestDevice( |
| 235 std::move(deviceOptions), | 235 std::move(deviceOptions), |
| 236 convertToBaseCallback(WTF::bind(&Bluetooth::RequestDeviceCallback, | 236 convertToBaseCallback(WTF::bind(&Bluetooth::RequestDeviceCallback, |
| 237 wrapPersistent(this), | 237 wrapPersistent(this), |
| 238 wrapPersistent(resolver)))); | 238 wrapPersistent(resolver)))); |
| 239 | |
|
scheib
2017/01/07 01:25:53
nit: avoid whitespace changes
juncai
2017/01/09 20:25:50
Done.
| |
| 239 return promise; | 240 return promise; |
| 240 } | 241 } |
| 241 | 242 |
| 242 void Bluetooth::addDevice(const String& deviceId, BluetoothDevice* device) { | 243 void Bluetooth::addDevice(const String& deviceId, BluetoothDevice* device) { |
| 243 m_connectedDevices.add(deviceId, device); | 244 m_connectedDevices.add(deviceId, device); |
| 244 } | 245 } |
| 245 | 246 |
| 246 void Bluetooth::removeDevice(const String& deviceId) { | 247 void Bluetooth::removeDevice(const String& deviceId) { |
| 247 m_connectedDevices.remove(deviceId); | 248 m_connectedDevices.remove(deviceId); |
| 248 } | 249 } |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 268 | 269 |
| 269 void Bluetooth::RemoteCharacteristicValueChanged( | 270 void Bluetooth::RemoteCharacteristicValueChanged( |
| 270 const WTF::String& characteristicInstanceId, | 271 const WTF::String& characteristicInstanceId, |
| 271 const WTF::Vector<uint8_t>& value) { | 272 const WTF::Vector<uint8_t>& value) { |
| 272 BluetoothRemoteGATTCharacteristic* characteristic = | 273 BluetoothRemoteGATTCharacteristic* characteristic = |
| 273 m_activeCharacteristics.get(characteristicInstanceId); | 274 m_activeCharacteristics.get(characteristicInstanceId); |
| 274 if (characteristic) | 275 if (characteristic) |
| 275 characteristic->dispatchCharacteristicValueChanged(value); | 276 characteristic->dispatchCharacteristicValueChanged(value); |
| 276 } | 277 } |
| 277 | 278 |
| 278 void Bluetooth::GattServerDisconnected( | 279 void Bluetooth::GattServerDisconnected(const WTF::String& deviceId) { |
| 279 mojom::blink::WebBluetoothDeviceIdPtr deviceId) { | 280 BluetoothDevice* device = m_connectedDevices.get(deviceId); |
| 280 BluetoothDevice* device = m_connectedDevices.get(deviceId->device_id); | |
| 281 if (device) { | 281 if (device) { |
| 282 // Remove device from the map before calling dispatchGattServerDisconnected | 282 // Remove device from the map before calling dispatchGattServerDisconnected |
| 283 // to avoid removing a device the gattserverdisconnected event handler might | 283 // to avoid removing a device the gattserverdisconnected event handler might |
| 284 // have re-connected. | 284 // have re-connected. |
| 285 m_connectedDevices.remove(deviceId->device_id); | 285 m_connectedDevices.remove(deviceId); |
| 286 device->dispatchGattServerDisconnected(); | 286 device->dispatchGattServerDisconnected(); |
| 287 } | 287 } |
| 288 } | 288 } |
| 289 | 289 |
| 290 BluetoothDevice* Bluetooth::getBluetoothDeviceRepresentingDevice( | 290 BluetoothDevice* Bluetooth::getBluetoothDeviceRepresentingDevice( |
| 291 const String& id, | 291 mojom::blink::WebBluetoothDevicePtr device, |
|
scheib
2017/01/07 01:25:53
I recommend renaming the parameter 'device_ptr' an
juncai
2017/01/09 20:25:50
Done.
| |
| 292 const String& name, | |
| 293 ScriptPromiseResolver* resolver) { | 292 ScriptPromiseResolver* resolver) { |
| 294 BluetoothDevice* device = m_deviceInstanceMap.get(id); | 293 WTF::String id = device->id; |
| 295 if (!device) { | 294 BluetoothDevice* bluetoothDevice = m_deviceInstanceMap.get(id); |
| 296 device = BluetoothDevice::take(resolver, id, name, this); | 295 if (!bluetoothDevice) { |
| 297 auto result = m_deviceInstanceMap.add(id, device); | 296 bluetoothDevice = BluetoothDevice::take(resolver, std::move(device), this); |
| 297 auto result = m_deviceInstanceMap.add(id, bluetoothDevice); | |
| 298 DCHECK(result.isNewEntry); | 298 DCHECK(result.isNewEntry); |
| 299 } | 299 } |
| 300 return device; | 300 return bluetoothDevice; |
| 301 } | 301 } |
| 302 | 302 |
| 303 } // namespace blink | 303 } // namespace blink |
| OLD | NEW |