Commit c04518b4 authored by Francois Beaufort's avatar Francois Beaufort Committed by Chromium LUCI CQ

[WebNFC] Raise InvalidStateError in non top-level browsing context

This CL replaces NotAllowedError with InvalidStateError DOMException
when Web NFC interfaces are used in a non top-level browsing context.

Spec: https://github.com/w3c/web-nfc/pull/610
Bug: 520391
Change-Id: I1aba5440d34664f65b321f8425609cc7e74488f4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2560267
Commit-Queue: François Beaufort <beaufort.francois@gmail.com>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarRijubrata Bhaumik <rijubrata.bhaumik@intel.com>
Cr-Commit-Position: refs/heads/master@{#832288}
parent 95c1668b
...@@ -38,7 +38,10 @@ using mojom::blink::PermissionStatus; ...@@ -38,7 +38,10 @@ using mojom::blink::PermissionStatus;
namespace { namespace {
constexpr char kNotSupportedOrPermissionDenied[] = constexpr char kNotSupportedOrPermissionDenied[] =
"WebNFC feature is unavailable or permission denied."; "Web NFC is unavailable or permission denied.";
constexpr char kChildFrameErrorMessage[] =
"Web NFC can only be accessed in a top-level browsing context.";
constexpr char kInvalidStateErrorMessage[] = "A scan() operation is ongoing."; constexpr char kInvalidStateErrorMessage[] = "A scan() operation is ongoing.";
} // namespace } // namespace
...@@ -77,15 +80,11 @@ bool NDEFReader::HasPendingActivity() const { ...@@ -77,15 +80,11 @@ bool NDEFReader::HasPendingActivity() const {
ScriptPromise NDEFReader::scan(ScriptState* script_state, ScriptPromise NDEFReader::scan(ScriptState* script_state,
const NDEFScanOptions* options, const NDEFScanOptions* options,
ExceptionState& exception_state) { ExceptionState& exception_state) {
LocalFrame* frame = script_state->ContextIsValid()
? LocalDOMWindow::From(script_state)->GetFrame()
: nullptr;
// https://w3c.github.io/web-nfc/#security-policies // https://w3c.github.io/web-nfc/#security-policies
// WebNFC API must be only accessible from top level browsing context. // WebNFC API must be only accessible from top level browsing context.
if (!frame || !frame->IsMainFrame()) { if (!DomWindow() || !DomWindow()->GetFrame()->IsMainFrame()) {
exception_state.ThrowDOMException(DOMExceptionCode::kNotAllowedError, exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"NFC interfaces are only available " kChildFrameErrorMessage);
"in a top-level browsing context");
return ScriptPromise(); return ScriptPromise();
} }
...@@ -122,7 +121,7 @@ ScriptPromise NDEFReader::scan(ScriptState* script_state, ...@@ -122,7 +121,7 @@ ScriptPromise NDEFReader::scan(ScriptState* script_state,
GetPermissionService()->RequestPermission( GetPermissionService()->RequestPermission(
CreatePermissionDescriptor(PermissionName::NFC), CreatePermissionDescriptor(PermissionName::NFC),
LocalFrame::HasTransientUserActivation(frame), LocalFrame::HasTransientUserActivation(DomWindow()->GetFrame()),
WTF::Bind(&NDEFReader::ReadOnRequestPermission, WrapPersistent(this), WTF::Bind(&NDEFReader::ReadOnRequestPermission, WrapPersistent(this),
WrapPersistent(options))); WrapPersistent(options)));
return scan_resolver_->Promise(); return scan_resolver_->Promise();
...@@ -211,15 +210,11 @@ ScriptPromise NDEFReader::write(ScriptState* script_state, ...@@ -211,15 +210,11 @@ ScriptPromise NDEFReader::write(ScriptState* script_state,
const NDEFMessageSource& write_message, const NDEFMessageSource& write_message,
const NDEFWriteOptions* options, const NDEFWriteOptions* options,
ExceptionState& exception_state) { ExceptionState& exception_state) {
LocalDOMWindow* window = script_state->ContextIsValid()
? LocalDOMWindow::From(script_state)
: nullptr;
// https://w3c.github.io/web-nfc/#security-policies // https://w3c.github.io/web-nfc/#security-policies
// WebNFC API must be only accessible from top level browsing context. // WebNFC API must be only accessible from top level browsing context.
if (!window || !window->GetFrame()->IsMainFrame()) { if (!DomWindow() || !DomWindow()->GetFrame()->IsMainFrame()) {
exception_state.ThrowDOMException(DOMExceptionCode::kNotAllowedError, exception_state.ThrowDOMException(DOMExceptionCode::kInvalidStateError,
"NFC interfaces are only available " kChildFrameErrorMessage);
"in a top-level browsing context");
return ScriptPromise(); return ScriptPromise();
} }
...@@ -234,7 +229,7 @@ ScriptPromise NDEFReader::write(ScriptState* script_state, ...@@ -234,7 +229,7 @@ ScriptPromise NDEFReader::write(ScriptState* script_state,
// Step 11.2: Run "create NDEF message", if this throws an exception, // Step 11.2: Run "create NDEF message", if this throws an exception,
// reject p with that exception and abort these steps. // reject p with that exception and abort these steps.
NDEFMessage* ndef_message = NDEFMessage* ndef_message =
NDEFMessage::Create(window, write_message, exception_state); NDEFMessage::Create(DomWindow(), write_message, exception_state);
if (exception_state.HadException()) { if (exception_state.HadException()) {
return ScriptPromise(); return ScriptPromise();
} }
...@@ -251,7 +246,7 @@ ScriptPromise NDEFReader::write(ScriptState* script_state, ...@@ -251,7 +246,7 @@ ScriptPromise NDEFReader::write(ScriptState* script_state,
GetPermissionService()->RequestPermission( GetPermissionService()->RequestPermission(
CreatePermissionDescriptor(PermissionName::NFC), CreatePermissionDescriptor(PermissionName::NFC),
LocalFrame::HasTransientUserActivation(window->GetFrame()), LocalFrame::HasTransientUserActivation(DomWindow()->GetFrame()),
WTF::Bind(&NDEFReader::WriteOnRequestPermission, WrapPersistent(this), WTF::Bind(&NDEFReader::WriteOnRequestPermission, WrapPersistent(this),
WrapPersistent(resolver), WrapPersistent(options), WrapPersistent(resolver), WrapPersistent(options),
std::move(message))); std::move(message)));
...@@ -318,8 +313,8 @@ void NDEFReader::WriteAbort(ScriptPromiseResolver* resolver) { ...@@ -318,8 +313,8 @@ void NDEFReader::WriteAbort(ScriptPromiseResolver* resolver) {
} }
NFCProxy* NDEFReader::GetNfcProxy() const { NFCProxy* NDEFReader::GetNfcProxy() const {
DCHECK(GetExecutionContext()); DCHECK(DomWindow());
return NFCProxy::From(*To<LocalDOMWindow>(GetExecutionContext())); return NFCProxy::From(*DomWindow());
} }
void NDEFReader::Trace(Visitor* visitor) const { void NDEFReader::Trace(Visitor* visitor) const {
......
...@@ -72,7 +72,7 @@ nfc_test(async () => { ...@@ -72,7 +72,7 @@ nfc_test(async () => {
await ndef.scan(); await ndef.scan();
parent.postMessage("Failure", "*"); parent.postMessage("Failure", "*");
} catch (error) { } catch (error) {
if (error.name == "NotAllowedError") { if (error.name == "InvalidStateError") {
parent.postMessage("Success", "*"); parent.postMessage("Success", "*");
} else { } else {
parent.postMessage("Failure", "*"); parent.postMessage("Failure", "*");
......
...@@ -236,7 +236,7 @@ nfc_test(async () => { ...@@ -236,7 +236,7 @@ nfc_test(async () => {
await ndef.write("Test"); await ndef.write("Test");
parent.postMessage("Failure", "*"); parent.postMessage("Failure", "*");
} catch (error) { } catch (error) {
if (error.name == "NotAllowedError") { if (error.name == "InvalidStateError") {
parent.postMessage("Success", "*"); parent.postMessage("Success", "*");
} else { } else {
parent.postMessage("Failure", "*"); parent.postMessage("Failure", "*");
......
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