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 "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 WebBluetoothScanFilter webFilter; | |
| 36 webFilter.services.assign(services); | |
|
ortuno
2015/06/15 19:13:37
why do you use assign instead of the constructor?
Jeffrey Yasskin
2015/06/15 19:19:18
I hadn't added the constructor when I wrote this c
| |
| 37 filters.append(webFilter); | |
| 38 } | |
| 39 result.filters.assign(filters); | |
| 40 } | |
| 41 if (options.hasOptionalServices()) { | |
| 42 Vector<WebString> optionalServices; | |
| 43 for (const String& optionalService : options.optionalServices()) { | |
| 44 // TODO(jyasskin): https://crbug.com/500630: Pass the services | |
| 45 // through BluetoothUUID.getService() per | |
| 46 // https://webbluetoothcg.github.io/web-bluetooth/#dom-bluetooth-req uestdevice | |
| 47 optionalServices.append(optionalService); | |
| 48 } | |
| 49 result.optionalServices.assign(optionalServices); | |
| 50 } | |
| 51 return 0; | |
| 52 } | |
| 53 | |
| 54 ScriptPromise Bluetooth::requestDevice(ScriptState* scriptState, const RequestDe viceOptions& options) | |
| 21 { | 55 { |
| 22 WebBluetooth* webbluetooth = Platform::current()->bluetooth(); | 56 WebBluetooth* webbluetooth = Platform::current()->bluetooth(); |
| 23 if (!webbluetooth) | 57 if (!webbluetooth) |
| 24 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(NotSupportedError)); | 58 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(NotSupportedError)); |
| 25 | 59 |
| 60 WebRequestDeviceOptions webOptions; | |
| 61 DOMException* exception = convertRequestDeviceOptions(options, webOptions); | |
| 62 if (exception) | |
| 63 return ScriptPromise::rejectWithDOMException(scriptState, exception); | |
| 64 | |
| 26 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState); | 65 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState); |
| 27 ScriptPromise promise = resolver->promise(); | 66 ScriptPromise promise = resolver->promise(); |
| 28 webbluetooth->requestDevice(new CallbackPromiseAdapter<BluetoothDevice, Blue toothError>(resolver)); | 67 webbluetooth->requestDevice(webOptions, new CallbackPromiseAdapter<Bluetooth Device, BluetoothError>(resolver)); |
| 29 return promise; | 68 return promise; |
| 30 | 69 |
| 31 } | 70 } |
| 32 | 71 |
| 33 }; // blink | 72 }; // blink |
| OLD | NEW |