1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "content/browser/android/nfc_host.h"
6 
7 #include <utility>
8 
9 #include "base/atomic_sequence_num.h"
10 #include "content/browser/permissions/permission_controller_impl.h"
11 #include "content/public/android/content_jni_headers/NfcHost_jni.h"
12 #include "content/public/browser/device_service.h"
13 #include "content/public/browser/navigation_handle.h"
14 #include "content/public/browser/web_contents.h"
15 #include "services/device/public/mojom/nfc.mojom.h"
16 #include "third_party/blink/public/mojom/permissions/permission_status.mojom.h"
17 
18 namespace content {
19 
20 namespace {
21 base::AtomicSequenceNumber g_unique_id;
22 }  // namespace
23 
NFCHost(WebContents * web_contents)24 NFCHost::NFCHost(WebContents* web_contents)
25     : WebContentsObserver(web_contents) {
26   DCHECK(web_contents);
27 
28   permission_controller_ = PermissionControllerImpl::FromBrowserContext(
29       web_contents->GetBrowserContext());
30 }
31 
~NFCHost()32 NFCHost::~NFCHost() {
33   Close();
34 }
35 
GetNFC(RenderFrameHost * render_frame_host,mojo::PendingReceiver<device::mojom::NFC> receiver)36 void NFCHost::GetNFC(RenderFrameHost* render_frame_host,
37                      mojo::PendingReceiver<device::mojom::NFC> receiver) {
38   // https://w3c.github.io/web-nfc/#security-policies
39   // WebNFC API must be only accessible from top level browsing context.
40   if (render_frame_host->GetParent()) {
41     mojo::ReportBadMessage(
42         "WebNFC is only allowed in a top-level browsing context.");
43     return;
44   }
45 
46   GURL origin_url = render_frame_host->GetLastCommittedOrigin().GetURL();
47   if (permission_controller_->GetPermissionStatusForFrame(
48           PermissionType::NFC, render_frame_host, origin_url) !=
49       blink::mojom::PermissionStatus::GRANTED) {
50     return;
51   }
52 
53   if (subscription_id_ == PermissionController::kNoPendingOperation) {
54     // base::Unretained() is safe here because the subscription is canceled when
55     // this object is destroyed.
56     subscription_id_ = permission_controller_->SubscribePermissionStatusChange(
57         PermissionType::NFC, render_frame_host, origin_url,
58         base::BindRepeating(&NFCHost::OnPermissionStatusChange,
59                             base::Unretained(this)));
60   }
61 
62   if (!nfc_provider_) {
63     content::GetDeviceService().BindNFCProvider(
64         nfc_provider_.BindNewPipeAndPassReceiver());
65   }
66 
67   JNIEnv* env = base::android::AttachCurrentThread();
68 
69   // The created instance's reference is kept inside a map in Java world.
70   int id = g_unique_id.GetNext();
71   Java_NfcHost_create(env, web_contents()->GetJavaWebContents(), id);
72 
73   // Connect to an NFC object, associating it with |id_|.
74   nfc_provider_->GetNFCForHost(id, std::move(receiver));
75 }
76 
RenderFrameHostChanged(RenderFrameHost * old_host,RenderFrameHost * new_host)77 void NFCHost::RenderFrameHostChanged(RenderFrameHost* old_host,
78                                      RenderFrameHost* new_host) {
79   // If the main frame has been replaced then close an old NFC connection.
80   if (!new_host->GetParent())
81     Close();
82 }
83 
OnVisibilityChanged(Visibility visibility)84 void NFCHost::OnVisibilityChanged(Visibility visibility) {
85   // For cases NFC not initialized, such as the permission has been revoked.
86   if (!nfc_provider_)
87     return;
88 
89   // NFC operations should be suspended.
90   // https://w3c.github.io/web-nfc/#nfc-suspended
91   if (visibility == Visibility::VISIBLE)
92     nfc_provider_->ResumeNFCOperations();
93   else
94     nfc_provider_->SuspendNFCOperations();
95 }
96 
OnPermissionStatusChange(blink::mojom::PermissionStatus status)97 void NFCHost::OnPermissionStatusChange(blink::mojom::PermissionStatus status) {
98   if (status != blink::mojom::PermissionStatus::GRANTED)
99     Close();
100 }
101 
Close()102 void NFCHost::Close() {
103   nfc_provider_.reset();
104   if (subscription_id_ != PermissionController::kNoPendingOperation) {
105     permission_controller_->UnsubscribePermissionStatusChange(subscription_id_);
106     subscription_id_ = PermissionController::kNoPendingOperation;
107   }
108 }
109 
110 }  // namespace content
111