Commit 7e123ff2 authored by Jeremy Roman's avatar Jeremy Roman Committed by Commit Bot

Use [RaisesException] for immediate promise rejections in /third_party/blink/renderer/modules/hid.

The bindings layer implicitly converts thrown exceptions in promise-returning
functions to promise rejections, and using ExceptionState makes this more
similar to ordinarily throwing exceptions, notably by including the auto-
generated exception context that makes it easier to see what IDL operation
caused an exception to be thrown.

This CL was uploaded by git cl split.

R=reillyg@chromium.org

Bug: 1001114
Change-Id: I6126a5183b44f492dd91a2f74a07b063e87ca0d1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1787351
Commit-Queue: Jeremy Roman <jbroman@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Auto-Submit: Jeremy Roman <jbroman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#694302}
parent 13663e59
......@@ -22,6 +22,7 @@ namespace blink {
namespace {
const char kContextGone[] = "Script context has shut down.";
const char kFeaturePolicyBlocked[] =
"Access to the feature \"hid\" is disallowed by feature policy.";
const char kNoDeviceSelected[] = "No device selected.";
......@@ -101,20 +102,19 @@ void HID::AddedEventListener(const AtomicString& event_type,
// and disconnect events.
}
ScriptPromise HID::getDevices(ScriptState* script_state) {
ScriptPromise HID::getDevices(ScriptState* script_state,
ExceptionState& exception_state) {
auto* context = GetExecutionContext();
if (!context) {
return ScriptPromise::RejectWithDOMException(
script_state, MakeGarbageCollected<DOMException>(
DOMExceptionCode::kNotSupportedError));
exception_state.ThrowDOMException(DOMExceptionCode::kNotSupportedError,
kContextGone);
return ScriptPromise();
}
if (!context->GetSecurityContext().IsFeatureEnabled(
mojom::FeaturePolicyFeature::kHid, ReportOptions::kReportOnFailure)) {
return ScriptPromise::RejectWithDOMException(
script_state,
MakeGarbageCollected<DOMException>(DOMExceptionCode::kSecurityError,
kFeaturePolicyBlocked));
exception_state.ThrowSecurityError(kFeaturePolicyBlocked);
return ScriptPromise();
}
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state);
......@@ -127,28 +127,25 @@ ScriptPromise HID::getDevices(ScriptState* script_state) {
}
ScriptPromise HID::requestDevice(ScriptState* script_state,
const HIDDeviceRequestOptions* options) {
const HIDDeviceRequestOptions* options,
ExceptionState& exception_state) {
auto* frame = GetFrame();
if (!frame || !frame->GetDocument()) {
return ScriptPromise::RejectWithDOMException(
script_state, MakeGarbageCollected<DOMException>(
DOMExceptionCode::kNotSupportedError));
exception_state.ThrowDOMException(DOMExceptionCode::kNotSupportedError,
kContextGone);
return ScriptPromise();
}
if (!frame->GetDocument()->IsFeatureEnabled(
mojom::FeaturePolicyFeature::kHid, ReportOptions::kReportOnFailure)) {
return ScriptPromise::RejectWithDOMException(
script_state,
MakeGarbageCollected<DOMException>(DOMExceptionCode::kSecurityError,
kFeaturePolicyBlocked));
exception_state.ThrowSecurityError(kFeaturePolicyBlocked);
return ScriptPromise();
}
if (!LocalFrame::HasTransientUserActivation(frame)) {
return ScriptPromise::RejectWithDOMException(
script_state,
MakeGarbageCollected<DOMException>(
DOMExceptionCode::kSecurityError,
"Must be handling a user gesture to show a permission request."));
exception_state.ThrowSecurityError(
"Must be handling a user gesture to show a permission request.");
return ScriptPromise();
}
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state);
......
......@@ -37,8 +37,10 @@ class HID : public EventTargetWithInlineData, public ContextLifecycleObserver {
// Web-exposed interfaces:
DEFINE_ATTRIBUTE_EVENT_LISTENER(connect, kConnect)
DEFINE_ATTRIBUTE_EVENT_LISTENER(disconnect, kDisconnect)
ScriptPromise getDevices(ScriptState*);
ScriptPromise requestDevice(ScriptState*, const HIDDeviceRequestOptions*);
ScriptPromise getDevices(ScriptState*, ExceptionState&);
ScriptPromise requestDevice(ScriptState*,
const HIDDeviceRequestOptions*,
ExceptionState&);
void Connect(const String& device_guid,
mojo::PendingRemote<device::mojom::blink::HidConnectionClient>
......
......@@ -16,12 +16,14 @@
// Retrieves information about all available HID subsystem devices.
[
CallWith=ScriptState,
RaisesException,
MeasureAs=HidGetDevices
] Promise<sequence<HIDDevice>> getDevices();
// Requests permission from the user to use a device.
[
CallWith=ScriptState,
RaisesException,
MeasureAs=HidRequestDevice
] Promise<sequence<HIDDevice>> requestDevice(
HIDDeviceRequestOptions options);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment