| 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>();
|
| +
|
| + /**
|
| + * 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) {
|
| + mCallback = null;
|
| + mWebContents = webContents;
|
| + mContentViewCore = ContentViewCore.fromWebContents(mWebContents);
|
| + mContextId = contextId;
|
| + mContextHostsMap.put(mContextId, this);
|
| + }
|
| +
|
| + /** Called by the NFC implementation (via ContentNfcDelegate) to allow
|
| + * 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) {
|
| + mContentViewCore.addWindowAndroidChangedObserver(this);
|
| + if (mContentViewCore.getWindowAndroid() != null) {
|
| + activity = mContentViewCore.getWindowAndroid().getActivity().get();
|
| + }
|
| + }
|
| +
|
| + mCallback.onResult(activity);
|
| + }
|
| +
|
| + public void stopTrackingActivityChanges() {
|
| + mCallback = null;
|
| + if (mContentViewCore != null) {
|
| + mContentViewCore.removeWindowAndroidChangedObserver(this);
|
| + }
|
| + }
|
| +
|
| + @Override
|
| + public void destroy() {
|
| + mContextHostsMap.remove(mContextId);
|
| + mCallback = null;
|
| + 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);
|
| + }
|
| +}
|
|
|