| 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" |
| 11 #include "core/dom/ExceptionCode.h" | 11 #include "core/dom/ExceptionCode.h" |
| 12 #include "core/inspector/ConsoleMessage.h" |
| 13 #include "core/origin_trials/OriginTrialContext.h" |
| 14 #include "core/origin_trials/OriginTrials.h" |
| 12 #include "modules/bluetooth/BluetoothDevice.h" | 15 #include "modules/bluetooth/BluetoothDevice.h" |
| 13 #include "modules/bluetooth/BluetoothError.h" | 16 #include "modules/bluetooth/BluetoothError.h" |
| 14 #include "modules/bluetooth/BluetoothSupplement.h" | 17 #include "modules/bluetooth/BluetoothSupplement.h" |
| 15 #include "modules/bluetooth/BluetoothUUID.h" | 18 #include "modules/bluetooth/BluetoothUUID.h" |
| 16 #include "modules/bluetooth/RequestDeviceOptions.h" | 19 #include "modules/bluetooth/RequestDeviceOptions.h" |
| 17 #include "platform/RuntimeEnabledFeatures.h" | 20 #include "platform/RuntimeEnabledFeatures.h" |
| 18 #include "platform/UserGestureIndicator.h" | 21 #include "platform/UserGestureIndicator.h" |
| 19 #include "public/platform/modules/bluetooth/WebBluetooth.h" | 22 #include "public/platform/modules/bluetooth/WebBluetooth.h" |
| 20 #include "public/platform/modules/bluetooth/WebRequestDeviceOptions.h" | 23 #include "public/platform/modules/bluetooth/WebRequestDeviceOptions.h" |
| 21 | 24 |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 return; | 126 return; |
| 124 optionalServices.append(validatedOptionalService); | 127 optionalServices.append(validatedOptionalService); |
| 125 } | 128 } |
| 126 result.optionalServices.assign(optionalServices); | 129 result.optionalServices.assign(optionalServices); |
| 127 } | 130 } |
| 128 } | 131 } |
| 129 | 132 |
| 130 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetooth-requestdevi
ce | 133 // https://webbluetoothchrome.github.io/web-bluetooth/#dom-bluetooth-requestdevi
ce |
| 131 ScriptPromise Bluetooth::requestDevice(ScriptState* scriptState, const RequestDe
viceOptions& options, ExceptionState& exceptionState) | 134 ScriptPromise Bluetooth::requestDevice(ScriptState* scriptState, const RequestDe
viceOptions& options, ExceptionState& exceptionState) |
| 132 { | 135 { |
| 133 | |
| 134 // By adding the "OriginTrialEnabled" extended binding, we enable the | 136 // By adding the "OriginTrialEnabled" extended binding, we enable the |
| 135 // requestDevice function on all platforms for websites that contain an | 137 // requestDevice function on all platforms for websites that contain an |
| 136 // origin trial token. Since we only support Chrome OS, Android and MacOS | 138 // origin trial token. Since we only support Chrome OS, Android and MacOS |
| 137 // for this experiment we reject any promises from other platforms unless | 139 // for this experiment we reject any promises from other platforms unless |
| 138 // they have the enable-web-bluetooth flag on. | 140 // they have the enable-web-bluetooth flag on. |
| 139 #if !OS(CHROMEOS) && !OS(ANDROID) && !OS(MACOSX) | 141 #if !OS(CHROMEOS) && !OS(ANDROID) && !OS(MACOSX) |
| 140 if (!RuntimeEnabledFeatures::webBluetoothEnabled()) { | 142 if (!RuntimeEnabledFeatures::webBluetoothEnabled()) { |
| 141 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError, "Web Bluetooth is not enabled on this platform. To fin
d out how to enable it and the current implementation status visit https://goo.g
l/HKa2If")); | 143 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError, "Web Bluetooth is not enabled on this platform. To fin
d out how to enable it and the current implementation status visit https://goo.g
l/HKa2If")); |
| 142 } | 144 } |
| 143 #endif | 145 #endif |
| 144 | 146 |
| 147 // Promote use of Origin Trials |
| 148 // * When not being run on an origin trial. |
| 149 // * Only once for the lifetime of this Bluetooth object, to avoid being |
| 150 // a nuisance and too verbose in the console. Reloading a page will reset |
| 151 // and the message can be shown again. |
| 152 ExecutionContext* context = scriptState->getExecutionContext(); |
| 153 OriginTrialContext* originTrials = OriginTrialContext::from(context, OriginT
rialContext::DontCreateIfNotExists); |
| 154 bool originTrialActiveForThisPage = originTrials && originTrials->isFeatureE
nabled("WebBluetooth"); |
| 155 |
| 156 if (!originTrialActiveForThisPage && !promotedOriginTrial) { |
| 157 promotedOriginTrial = true; |
| 158 context->addConsoleMessage(ConsoleMessage::create(JSMessageSource, InfoM
essageLevel, |
| 159 "Web Bluetooth is available as an Origin Trial: https://bit.ly/WebBl
uetoothOriginTrial")); |
| 160 } |
| 161 |
| 145 // 1. If the incumbent settings object is not a secure context, reject promi
se with a SecurityError and abort these steps. | 162 // 1. If the incumbent settings object is not a secure context, reject promi
se with a SecurityError and abort these steps. |
| 146 String errorMessage; | 163 String errorMessage; |
| 147 if (!scriptState->getExecutionContext()->isSecureContext(errorMessage)) { | 164 if (!context->isSecureContext(errorMessage)) { |
| 148 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SecurityError, errorMessage)); | 165 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SecurityError, errorMessage)); |
| 149 } | 166 } |
| 150 | 167 |
| 151 // 2. If the algorithm is not allowed to show a popup, reject promise with a
SecurityError and abort these steps. | 168 // 2. If the algorithm is not allowed to show a popup, reject promise with a
SecurityError and abort these steps. |
| 152 if (!UserGestureIndicator::consumeUserGesture()) { | 169 if (!UserGestureIndicator::consumeUserGesture()) { |
| 153 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SecurityError, "Must be handling a user gesture to show a permission requ
est.")); | 170 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(SecurityError, "Must be handling a user gesture to show a permission requ
est.")); |
| 154 } | 171 } |
| 155 | 172 |
| 156 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); | 173 WebBluetooth* webbluetooth = BluetoothSupplement::fromScriptState(scriptStat
e); |
| 157 if (!webbluetooth) | 174 if (!webbluetooth) |
| 158 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); | 175 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::
create(NotSupportedError)); |
| 159 | 176 |
| 160 // 3. In order to convert the arguments from service names and aliases to ju
st UUIDs, do the following substeps: | 177 // 3. In order to convert the arguments from service names and aliases to ju
st UUIDs, do the following substeps: |
| 161 WebRequestDeviceOptions webOptions; | 178 WebRequestDeviceOptions webOptions; |
| 162 convertRequestDeviceOptions(options, webOptions, exceptionState); | 179 convertRequestDeviceOptions(options, webOptions, exceptionState); |
| 163 if (exceptionState.hadException()) | 180 if (exceptionState.hadException()) |
| 164 return exceptionState.reject(scriptState); | 181 return exceptionState.reject(scriptState); |
| 165 | 182 |
| 166 // Subsequent steps are handled in the browser process. | 183 // Subsequent steps are handled in the browser process. |
| 167 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; | 184 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState)
; |
| 168 ScriptPromise promise = resolver->promise(); | 185 ScriptPromise promise = resolver->promise(); |
| 169 webbluetooth->requestDevice(webOptions, new CallbackPromiseAdapter<Bluetooth
Device, BluetoothError>(resolver)); | 186 webbluetooth->requestDevice(webOptions, new CallbackPromiseAdapter<Bluetooth
Device, BluetoothError>(resolver)); |
| 170 return promise; | 187 return promise; |
| 171 | 188 |
| 172 } | 189 } |
| 173 | 190 |
| 174 } // namespace blink | 191 } // namespace blink |
| OLD | NEW |