| 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 "config.h" | 5 #include "config.h" |
| 6 #include "modules/bluetooth/Bluetooth.h" | 6 #include "modules/bluetooth/Bluetooth.h" |
| 7 | 7 |
| 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" | 8 #include "bindings/core/v8/CallbackPromiseAdapter.h" |
| 9 #include "bindings/core/v8/ScriptPromise.h" | 9 #include "bindings/core/v8/ScriptPromise.h" |
| 10 #include "bindings/core/v8/ScriptPromiseResolver.h" | 10 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 11 #include "core/dom/DOMException.h" | 11 #include "core/dom/DOMException.h" |
| 12 #include "core/dom/ExceptionCode.h" | 12 #include "core/dom/ExceptionCode.h" |
| 13 #include "modules/bluetooth/BluetoothDevice.h" | 13 #include "modules/bluetooth/BluetoothDevice.h" |
| 14 #include "modules/bluetooth/BluetoothError.h" | 14 #include "modules/bluetooth/BluetoothError.h" |
| 15 #include "modules/bluetooth/RequestDeviceOptions.h" |
| 15 #include "public/platform/Platform.h" | 16 #include "public/platform/Platform.h" |
| 16 #include "public/platform/modules/bluetooth/WebBluetooth.h" | 17 #include "public/platform/modules/bluetooth/WebBluetooth.h" |
| 18 #include "public/platform/modules/bluetooth/WebRequestDeviceOptions.h" |
| 17 | 19 |
| 18 namespace blink { | 20 namespace blink { |
| 19 | 21 |
| 20 ScriptPromise Bluetooth::requestDevice(ScriptState* scriptState) | 22 // Returns a DOMException if the conversion fails, or null if it succeeds. |
| 23 static DOMException* convertRequestDeviceOptions(const RequestDeviceOptions& opt
ions, WebRequestDeviceOptions& result) |
| 24 { |
| 25 if (options.hasFilters()) { |
| 26 Vector<WebBluetoothScanFilter> filters; |
| 27 for (const BluetoothScanFilter& filter : options.filters()) { |
| 28 Vector<WebString> services; |
| 29 for (const String& service : filter.services()) { |
| 30 // TODO(jyasskin): https://crbug.com/500630: Pass the services |
| 31 // through BluetoothUUID.getService() per |
| 32 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth
-requestdevice |
| 33 services.append(service); |
| 34 } |
| 35 filters.append(WebBluetoothScanFilter(services)); |
| 36 } |
| 37 result.filters.assign(filters); |
| 38 } |
| 39 if (options.hasOptionalServices()) { |
| 40 Vector<WebString> optionalServices; |
| 41 for (const String& optionalService : options.optionalServices()) { |
| 42 // TODO(jyasskin): https://crbug.com/500630: Pass the services |
| 43 // through BluetoothUUID.getService() per |
| 44 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-req
uestdevice |
| 45 optionalServices.append(optionalService); |
| 46 } |
| 47 result.optionalServices.assign(optionalServices); |
| 48 } |
| 49 return 0; |
| 50 } |
| 51 |
| 52 ScriptPromise Bluetooth::requestDevice(ScriptState* scriptState, const RequestDe
viceOptions& options) |
| 21 { | 53 { |
| 22 WebBluetooth* webbluetooth = Platform::current()->bluetooth(); | 54 WebBluetooth* webbluetooth = Platform::current()->bluetooth(); |
| 23 if (!webbluetooth) | 55 if (!webbluetooth) |
| 24 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); | 56 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); |
| 25 | 57 |
| 58 WebRequestDeviceOptions webOptions; |
| 59 DOMException* exception = convertRequestDeviceOptions(options, webOptions); |
| 60 if (exception) |
| 61 return ScriptPromise::rejectWithDOMException(scriptState, exception); |
| 62 |
| 26 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); | 63 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::
create(scriptState); |
| 27 ScriptPromise promise = resolver->promise(); | 64 ScriptPromise promise = resolver->promise(); |
| 28 webbluetooth->requestDevice(new CallbackPromiseAdapter<BluetoothDevice, Blue
toothError>(resolver)); | 65 webbluetooth->requestDevice(webOptions, new CallbackPromiseAdapter<Bluetooth
Device, BluetoothError>(resolver)); |
| 29 return promise; | 66 return promise; |
| 30 | 67 |
| 31 } | 68 } |
| 32 | 69 |
| 33 }; // blink | 70 }; // blink |
| OLD | NEW |