Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(211)

Unified Diff: third_party/WebKit/Source/modules/nfc/NFC.cpp

Issue 2875283002: [NFC] Only connect to NFC impl if ExecutionContext supports it (Closed)
Patch Set: Rebase Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/WebKit/Source/modules/nfc/NFC.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/modules/nfc/NFC.cpp
diff --git a/third_party/WebKit/Source/modules/nfc/NFC.cpp b/third_party/WebKit/Source/modules/nfc/NFC.cpp
index 10d06b95fcb43947508ae76becdbc83d585e9e22..8d7c566764c85270a8ee87910824904ae43abbf0 100644
--- a/third_party/WebKit/Source/modules/nfc/NFC.cpp
+++ b/third_party/WebKit/Source/modules/nfc/NFC.cpp
@@ -579,6 +579,12 @@ NFC::NFC(LocalFrame* frame)
: PageVisibilityObserver(frame->GetPage()),
ContextLifecycleObserver(frame->GetDocument()),
client_(this) {
+ String error_message;
+
+ // Only connect to NFC if we are in a context that supports it.
+ if (!IsSupportedInContext(GetExecutionContext(), error_message))
+ return;
+
frame->GetInterfaceProvider()->GetInterface(mojo::MakeRequest(&nfc_));
nfc_.set_connection_error_handler(ConvertToBaseCallback(
WTF::Bind(&NFC::OnConnectionError, WrapWeakPersistent(this))));
@@ -790,22 +796,29 @@ void NFC::OnWatch(const WTF::Vector<uint32_t>& ids,
}
}
-ScriptPromise NFC::RejectIfNotSupported(ScriptState* script_state) {
- String error_message;
- ExecutionContext* context = ExecutionContext::From(script_state);
+bool NFC::IsSupportedInContext(ExecutionContext* context,
+ String& error_message) {
if (!context->IsSecureContext(error_message)) {
- return ScriptPromise::RejectWithDOMException(
- script_state, DOMException::Create(kSecurityError, error_message));
+ return false;
}
// https://w3c.github.io/web-nfc/#security-policies
// WebNFC API must be only accessible from top level browsing context.
if (!ToDocument(context)->domWindow()->GetFrame() ||
!ToDocument(context)->GetFrame()->IsMainFrame()) {
+ error_message = "Must be in a top-level browsing context";
+ return false;
+ }
+
+ return true;
+}
+
+ScriptPromise NFC::RejectIfNotSupported(ScriptState* script_state) {
+ String error_message;
+ if (!IsSupportedInContext(ExecutionContext::From(script_state),
+ error_message)) {
return ScriptPromise::RejectWithDOMException(
- script_state,
- DOMException::Create(kSecurityError,
- "Must be in a top-level browsing context."));
+ script_state, DOMException::Create(kSecurityError, error_message));
}
if (!nfc_) {
« no previous file with comments | « third_party/WebKit/Source/modules/nfc/NFC.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698