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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/modules/nfc/NFC.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/nfc/NFC.h" 5 #include "modules/nfc/NFC.h"
6 6
7 #include "bindings/core/v8/ScriptPromiseResolver.h" 7 #include "bindings/core/v8/ScriptPromiseResolver.h"
8 #include "bindings/core/v8/V8ArrayBuffer.h" 8 #include "bindings/core/v8/V8ArrayBuffer.h"
9 #include "bindings/core/v8/V8StringResource.h" 9 #include "bindings/core/v8/V8StringResource.h"
10 #include "core/dom/DOMArrayBuffer.h" 10 #include "core/dom/DOMArrayBuffer.h"
(...skipping 561 matching lines...) Expand 10 before | Expand all | Expand 10 after
572 } 572 }
573 return message_size; 573 return message_size;
574 } 574 }
575 575
576 } // namespace 576 } // namespace
577 577
578 NFC::NFC(LocalFrame* frame) 578 NFC::NFC(LocalFrame* frame)
579 : PageVisibilityObserver(frame->GetPage()), 579 : PageVisibilityObserver(frame->GetPage()),
580 ContextLifecycleObserver(frame->GetDocument()), 580 ContextLifecycleObserver(frame->GetDocument()),
581 client_(this) { 581 client_(this) {
582 String error_message;
583
584 // Only connect to NFC if we are in a context that supports it.
585 if (!IsSupportedInContext(GetExecutionContext(), error_message))
586 return;
587
582 frame->GetInterfaceProvider()->GetInterface(mojo::MakeRequest(&nfc_)); 588 frame->GetInterfaceProvider()->GetInterface(mojo::MakeRequest(&nfc_));
583 nfc_.set_connection_error_handler(ConvertToBaseCallback( 589 nfc_.set_connection_error_handler(ConvertToBaseCallback(
584 WTF::Bind(&NFC::OnConnectionError, WrapWeakPersistent(this)))); 590 WTF::Bind(&NFC::OnConnectionError, WrapWeakPersistent(this))));
585 nfc_->SetClient(client_.CreateInterfacePtrAndBind()); 591 nfc_->SetClient(client_.CreateInterfacePtrAndBind());
586 } 592 }
587 593
588 NFC* NFC::Create(LocalFrame* frame) { 594 NFC* NFC::Create(LocalFrame* frame) {
589 NFC* nfc = new NFC(frame); 595 NFC* nfc = new NFC(frame);
590 return nfc; 596 return nfc;
591 } 597 }
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
783 MessageCallback* callback = it->value; 789 MessageCallback* callback = it->value;
784 ScriptState* script_state = callback->GetScriptState(); 790 ScriptState* script_state = callback->GetScriptState();
785 DCHECK(script_state); 791 DCHECK(script_state);
786 ScriptState::Scope scope(script_state); 792 ScriptState::Scope scope(script_state);
787 NFCMessage nfc_message = ToNFCMessage(script_state, message); 793 NFCMessage nfc_message = ToNFCMessage(script_state, message);
788 callback->handleMessage(nfc_message); 794 callback->handleMessage(nfc_message);
789 } 795 }
790 } 796 }
791 } 797 }
792 798
793 ScriptPromise NFC::RejectIfNotSupported(ScriptState* script_state) { 799 bool NFC::IsSupportedInContext(ExecutionContext* context,
794 String error_message; 800 String& error_message) {
795 ExecutionContext* context = ExecutionContext::From(script_state);
796 if (!context->IsSecureContext(error_message)) { 801 if (!context->IsSecureContext(error_message)) {
797 return ScriptPromise::RejectWithDOMException( 802 return false;
798 script_state, DOMException::Create(kSecurityError, error_message));
799 } 803 }
800 804
801 // https://w3c.github.io/web-nfc/#security-policies 805 // https://w3c.github.io/web-nfc/#security-policies
802 // WebNFC API must be only accessible from top level browsing context. 806 // WebNFC API must be only accessible from top level browsing context.
803 if (!ToDocument(context)->domWindow()->GetFrame() || 807 if (!ToDocument(context)->domWindow()->GetFrame() ||
804 !ToDocument(context)->GetFrame()->IsMainFrame()) { 808 !ToDocument(context)->GetFrame()->IsMainFrame()) {
809 error_message = "Must be in a top-level browsing context";
810 return false;
811 }
812
813 return true;
814 }
815
816 ScriptPromise NFC::RejectIfNotSupported(ScriptState* script_state) {
817 String error_message;
818 if (!IsSupportedInContext(ExecutionContext::From(script_state),
819 error_message)) {
805 return ScriptPromise::RejectWithDOMException( 820 return ScriptPromise::RejectWithDOMException(
806 script_state, 821 script_state, DOMException::Create(kSecurityError, error_message));
807 DOMException::Create(kSecurityError,
808 "Must be in a top-level browsing context."));
809 } 822 }
810 823
811 if (!nfc_) { 824 if (!nfc_) {
812 return ScriptPromise::RejectWithDOMException( 825 return ScriptPromise::RejectWithDOMException(
813 script_state, DOMException::Create(kNotSupportedError)); 826 script_state, DOMException::Create(kNotSupportedError));
814 } 827 }
815 828
816 return ScriptPromise(); 829 return ScriptPromise();
817 } 830 }
818 831
(...skipping 22 matching lines...) Expand all
841 } 854 }
842 855
843 DEFINE_TRACE(NFC) { 856 DEFINE_TRACE(NFC) {
844 PageVisibilityObserver::Trace(visitor); 857 PageVisibilityObserver::Trace(visitor);
845 ContextLifecycleObserver::Trace(visitor); 858 ContextLifecycleObserver::Trace(visitor);
846 visitor->Trace(requests_); 859 visitor->Trace(requests_);
847 visitor->Trace(callbacks_); 860 visitor->Trace(callbacks_);
848 } 861 }
849 862
850 } // namespace blink 863 } // namespace blink
OLDNEW
« 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