Chromium Code Reviews| Index: content/public/android/java/src/org/chromium/content/browser/NfcHost.java |
| diff --git a/content/public/android/java/src/org/chromium/content/browser/NfcHost.java b/content/public/android/java/src/org/chromium/content/browser/NfcHost.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..fd6096bb90f4508b783b503f74659d5147fc7f7c |
| --- /dev/null |
| +++ b/content/public/android/java/src/org/chromium/content/browser/NfcHost.java |
| @@ -0,0 +1,104 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.content.browser; |
| + |
| +import android.app.Activity; |
| +import android.util.SparseArray; |
| + |
| +import org.chromium.base.Callback; |
| +import org.chromium.base.annotations.CalledByNative; |
| +import org.chromium.content_public.browser.WebContents; |
| +import org.chromium.content_public.browser.WebContentsObserver; |
| +import org.chromium.ui.base.WindowAndroid; |
| + |
| +/** |
| + * Tracks the Activiy for a given WebContents on behalf of a NFC instance that |
| + * cannot talk directly to WebContents. |
| + */ |
| +class NfcHost extends WebContentsObserver implements WindowAndroidChangedObserver { |
| + // The WebContents with which this host is associated. |
| + private WebContents mWebContents; |
| + private ContentViewCore mContentViewCore; |
| + |
| + // The callback that the NFC instance has registered for being notified |
| + // when the Activity changes. |
| + private Callback<Activity> mCallback; |
| + |
| + // The context ID with which this host is associated. |
| + private int mContextId; |
| + |
| + private static final SparseArray<NfcHost> mContextHostsMap = new SparseArray<NfcHost>(); |
|
Ted C
2017/05/10 14:37:41
non-constant static in java start with a lowercase
blundell
2017/05/11 15:13:52
Done.
|
| + |
| + /** |
| + * Provides access to NfcHost via context ID. |
| + */ |
| + public static NfcHost fromContextId(int contextId) { |
| + return mContextHostsMap.get(contextId); |
| + } |
| + |
| + @CalledByNative |
| + private static NfcHost create(WebContents webContents, int contextId) { |
| + return new NfcHost(webContents, contextId); |
| + } |
| + |
| + NfcHost(WebContents webContents, int contextId) { |
|
Ted C
2017/05/10 14:37:41
you'll need to do:
super(webContents)
Otherwise,
blundell
2017/05/11 15:13:52
Done.
|
| + mCallback = null; |
|
Ted C
2017/05/10 14:37:41
no need for this, it will default to null in java
blundell
2017/05/11 15:13:52
Done.
|
| + mWebContents = webContents; |
|
Ted C
2017/05/10 14:37:41
looks like mWebContents, mContentViewCore, and mCo
blundell
2017/05/11 15:13:52
Done.
|
| + mContentViewCore = ContentViewCore.fromWebContents(mWebContents); |
| + mContextId = contextId; |
| + mContextHostsMap.put(mContextId, this); |
| + } |
| + |
| + /** Called by the NFC implementation (via ContentNfcDelegate) to allow |
|
Ted C
2017/05/10 14:37:41
If you have a /** comment header, it should be on
blundell
2017/05/11 15:13:52
Done.
|
| + * that implementation to track changes to the Activity associated with |
| + * its context ID (i.e., the activity associated with |mWebContents|). |
| + */ |
| + public void trackActivityChanges(Callback<Activity> callback) { |
| + // Only the main frame is allowed to access NFC |
| + // (https://w3c.github.io/web-nfc/#security-policies). The renderer |
| + // enforces this by dropping connection requests from nested frames. |
| + // Therefore, this class should never see a request to track activity |
| + // changes while there is already such a request. |
| + assert mCallback == null : "Unexpected request to track activity changes"; |
| + mCallback = callback; |
| + Activity activity = null; |
| + |
| + if (mContentViewCore != null) { |
|
Ted C
2017/05/10 14:37:41
What is the timeline of the NfcHost? I'm more won
blundell
2017/05/11 15:13:52
NfcHost is constructed in response to a request to
Ted C
2017/05/11 16:36:49
CVC has historically been the glue between the web
blundell
2017/05/12 11:01:14
Makes sense to me. I added a comment explaining th
|
| + mContentViewCore.addWindowAndroidChangedObserver(this); |
| + if (mContentViewCore.getWindowAndroid() != null) { |
| + activity = mContentViewCore.getWindowAndroid().getActivity().get(); |
| + } |
| + } |
| + |
| + mCallback.onResult(activity); |
| + } |
| + |
| + public void stopTrackingActivityChanges() { |
|
Ted C
2017/05/10 14:37:41
javadoc non-private methods
Although here, I susp
blundell
2017/05/11 15:13:52
Done.
|
| + mCallback = null; |
| + if (mContentViewCore != null) { |
| + mContentViewCore.removeWindowAndroidChangedObserver(this); |
| + } |
| + } |
| + |
| + @Override |
| + public void destroy() { |
| + mContextHostsMap.remove(mContextId); |
| + mCallback = null; |
|
Ted C
2017/05/10 14:37:41
maybe replace these three following lines with sto
blundell
2017/05/11 15:13:52
Done.
|
| + if (mContentViewCore != null) { |
| + mContentViewCore.removeWindowAndroidChangedObserver(this); |
| + } |
| + super.destroy(); |
| + } |
| + |
| + @Override |
| + public void onWindowAndroidChanged(WindowAndroid newWindowAndroid) { |
| + Activity activity = null; |
| + if (newWindowAndroid != null) { |
| + activity = newWindowAndroid.getActivity().get(); |
| + } |
| + assert mCallback != null : "should have callback"; |
| + mCallback.onResult(activity); |
| + } |
| +} |