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 { ...@@ -22,6 +22,7 @@ namespace blink {
namespace { namespace {
const char kContextGone[] = "Script context has shut down.";
const char kFeaturePolicyBlocked[] = const char kFeaturePolicyBlocked[] =
"Access to the feature \"hid\" is disallowed by feature policy."; "Access to the feature \"hid\" is disallowed by feature policy.";
const char kNoDeviceSelected[] = "No device selected."; const char kNoDeviceSelected[] = "No device selected.";
...@@ -101,20 +102,19 @@ void HID::AddedEventListener(const AtomicString& event_type, ...@@ -101,20 +102,19 @@ void HID::AddedEventListener(const AtomicString& event_type,
// and disconnect events. // and disconnect events.
} }
ScriptPromise HID::getDevices(ScriptState* script_state) { ScriptPromise HID::getDevices(ScriptState* script_state,
ExceptionState& exception_state) {
auto* context = GetExecutionContext(); auto* context = GetExecutionContext();
if (!context) { if (!context) {
return ScriptPromise::RejectWithDOMException( exception_state.ThrowDOMException(DOMExceptionCode::kNotSupportedError,
script_state, MakeGarbageCollected<DOMException>( kContextGone);
DOMExceptionCode::kNotSupportedError)); return ScriptPromise();
} }
if (!context->GetSecurityContext().IsFeatureEnabled( if (!context->GetSecurityContext().IsFeatureEnabled(
mojom::FeaturePolicyFeature::kHid, ReportOptions::kReportOnFailure)) { mojom::FeaturePolicyFeature::kHid, ReportOptions::kReportOnFailure)) {
return ScriptPromise::RejectWithDOMException( exception_state.ThrowSecurityError(kFeaturePolicyBlocked);
script_state, return ScriptPromise();
MakeGarbageCollected<DOMException>(DOMExceptionCode::kSecurityError,
kFeaturePolicyBlocked));
} }
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state); auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state);
...@@ -127,28 +127,25 @@ ScriptPromise HID::getDevices(ScriptState* script_state) { ...@@ -127,28 +127,25 @@ ScriptPromise HID::getDevices(ScriptState* script_state) {
} }
ScriptPromise HID::requestDevice(ScriptState* script_state, ScriptPromise HID::requestDevice(ScriptState* script_state,
const HIDDeviceRequestOptions* options) { const HIDDeviceRequestOptions* options,
ExceptionState& exception_state) {
auto* frame = GetFrame(); auto* frame = GetFrame();
if (!frame || !frame->GetDocument()) { if (!frame || !frame->GetDocument()) {
return ScriptPromise::RejectWithDOMException( exception_state.ThrowDOMException(DOMExceptionCode::kNotSupportedError,
script_state, MakeGarbageCollected<DOMException>( kContextGone);
DOMExceptionCode::kNotSupportedError)); return ScriptPromise();
} }
if (!frame->GetDocument()->IsFeatureEnabled( if (!frame->GetDocument()->IsFeatureEnabled(
mojom::FeaturePolicyFeature::kHid, ReportOptions::kReportOnFailure)) { mojom::FeaturePolicyFeature::kHid, ReportOptions::kReportOnFailure)) {
return ScriptPromise::RejectWithDOMException( exception_state.ThrowSecurityError(kFeaturePolicyBlocked);
script_state, return ScriptPromise();
MakeGarbageCollected<DOMException>(DOMExceptionCode::kSecurityError,
kFeaturePolicyBlocked));
} }
if (!LocalFrame::HasTransientUserActivation(frame)) { if (!LocalFrame::HasTransientUserActivation(frame)) {
return ScriptPromise::RejectWithDOMException( exception_state.ThrowSecurityError(
script_state, "Must be handling a user gesture to show a permission request.");
MakeGarbageCollected<DOMException>( return ScriptPromise();
DOMExceptionCode::kSecurityError,
"Must be handling a user gesture to show a permission request."));
} }
auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state); auto* resolver = MakeGarbageCollected<ScriptPromiseResolver>(script_state);
......
...@@ -37,8 +37,10 @@ class HID : public EventTargetWithInlineData, public ContextLifecycleObserver { ...@@ -37,8 +37,10 @@ class HID : public EventTargetWithInlineData, public ContextLifecycleObserver {
// Web-exposed interfaces: // Web-exposed interfaces:
DEFINE_ATTRIBUTE_EVENT_LISTENER(connect, kConnect) DEFINE_ATTRIBUTE_EVENT_LISTENER(connect, kConnect)
DEFINE_ATTRIBUTE_EVENT_LISTENER(disconnect, kDisconnect) DEFINE_ATTRIBUTE_EVENT_LISTENER(disconnect, kDisconnect)
ScriptPromise getDevices(ScriptState*); ScriptPromise getDevices(ScriptState*, ExceptionState&);
ScriptPromise requestDevice(ScriptState*, const HIDDeviceRequestOptions*); ScriptPromise requestDevice(ScriptState*,
const HIDDeviceRequestOptions*,
ExceptionState&);
void Connect(const String& device_guid, void Connect(const String& device_guid,
mojo::PendingRemote<device::mojom::blink::HidConnectionClient> mojo::PendingRemote<device::mojom::blink::HidConnectionClient>
......
...@@ -16,12 +16,14 @@ ...@@ -16,12 +16,14 @@
// Retrieves information about all available HID subsystem devices. // Retrieves information about all available HID subsystem devices.
[ [
CallWith=ScriptState, CallWith=ScriptState,
RaisesException,
MeasureAs=HidGetDevices MeasureAs=HidGetDevices
] Promise<sequence<HIDDevice>> getDevices(); ] Promise<sequence<HIDDevice>> getDevices();
// Requests permission from the user to use a device. // Requests permission from the user to use a device.
[ [
CallWith=ScriptState, CallWith=ScriptState,
RaisesException,
MeasureAs=HidRequestDevice MeasureAs=HidRequestDevice
] Promise<sequence<HIDDevice>> requestDevice( ] Promise<sequence<HIDDevice>> requestDevice(
HIDDeviceRequestOptions options); 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