| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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 package org.chromium.content.browser; |
| 6 |
| 7 import android.app.Activity; |
| 8 import android.util.SparseArray; |
| 9 |
| 10 import org.chromium.base.Callback; |
| 11 import org.chromium.base.annotations.CalledByNative; |
| 12 import org.chromium.content_public.browser.WebContents; |
| 13 import org.chromium.content_public.browser.WebContentsObserver; |
| 14 import org.chromium.ui.base.WindowAndroid; |
| 15 |
| 16 /** Tracks the Activiy for a given WebContents on behalf of a NFC instance that
cannot talk |
| 17 * directly to WebContents. |
| 18 */ |
| 19 class NfcHost extends WebContentsObserver implements WindowAndroidChangedObserve
r { |
| 20 private static final SparseArray<NfcHost> sContextHostsMap = new SparseArray
<NfcHost>(); |
| 21 |
| 22 // The WebContents with which this host is associated. |
| 23 private final WebContents mWebContents; |
| 24 private final ContentViewCore mContentViewCore; |
| 25 |
| 26 // The context ID with which this host is associated. |
| 27 private final int mContextId; |
| 28 |
| 29 // The callback that the NFC instance has registered for being notified when
the Activity |
| 30 // changes. |
| 31 private Callback<Activity> mCallback; |
| 32 |
| 33 /** |
| 34 * Provides access to NfcHost via context ID. |
| 35 */ |
| 36 public static NfcHost fromContextId(int contextId) { |
| 37 return sContextHostsMap.get(contextId); |
| 38 } |
| 39 |
| 40 @CalledByNative |
| 41 private static NfcHost create(WebContents webContents, int contextId) { |
| 42 return new NfcHost(webContents, contextId); |
| 43 } |
| 44 |
| 45 NfcHost(WebContents webContents, int contextId) { |
| 46 super(webContents); |
| 47 |
| 48 mWebContents = webContents; |
| 49 mContentViewCore = ContentViewCore.fromWebContents(mWebContents); |
| 50 |
| 51 // NFC will not work if there is no CVC associated with the WebContents,
and it will only |
| 52 // be requested in contexts where there is a CVC associated with the Web
Contents as far as |
| 53 // we are aware. If the latter ever proves false, then we will need to s
imply drop any NFC |
| 54 // request that comes in if there is no CVC available. |
| 55 assert mContentViewCore != null; |
| 56 mContextId = contextId; |
| 57 sContextHostsMap.put(mContextId, this); |
| 58 } |
| 59 |
| 60 /** Called by the NFC implementation (via ContentNfcDelegate) to allow that
implementation to |
| 61 * track changes to the Activity associated with its context ID (i.e., the a
ctivity associated |
| 62 * with |mWebContents|). |
| 63 */ |
| 64 |
| 65 public void trackActivityChanges(Callback<Activity> callback) { |
| 66 // Only the main frame is allowed to access NFC |
| 67 // (https://w3c.github.io/web-nfc/#security-policies). The renderer enfo
rces this by |
| 68 // dropping connection requests from nested frames. Therefore, this cla
ss should never see |
| 69 // a request to track activity changes while there is already such a req
uest. |
| 70 assert mCallback == null : "Unexpected request to track activity changes
"; |
| 71 mCallback = callback; |
| 72 Activity activity = null; |
| 73 |
| 74 mContentViewCore.addWindowAndroidChangedObserver(this); |
| 75 if (mContentViewCore.getWindowAndroid() != null) { |
| 76 activity = mContentViewCore.getWindowAndroid().getActivity().get(); |
| 77 } |
| 78 |
| 79 mCallback.onResult(activity); |
| 80 } |
| 81 |
| 82 /** |
| 83 * @see org.chromium.device.nfc.NfcDelegate#stopTrackingActivityForHost(int) |
| 84 */ |
| 85 public void stopTrackingActivityChanges() { |
| 86 mCallback = null; |
| 87 mContentViewCore.removeWindowAndroidChangedObserver(this); |
| 88 } |
| 89 |
| 90 /** |
| 91 * Tears down current and future Activity tracking. |
| 92 */ |
| 93 @Override |
| 94 public void destroy() { |
| 95 stopTrackingActivityChanges(); |
| 96 sContextHostsMap.remove(mContextId); |
| 97 super.destroy(); |
| 98 } |
| 99 |
| 100 /** |
| 101 * Updates the Activity associated with this instance. |
| 102 */ |
| 103 @Override |
| 104 public void onWindowAndroidChanged(WindowAndroid newWindowAndroid) { |
| 105 Activity activity = null; |
| 106 if (newWindowAndroid != null) { |
| 107 activity = newWindowAndroid.getActivity().get(); |
| 108 } |
| 109 assert mCallback != null : "should have callback"; |
| 110 mCallback.onResult(activity); |
| 111 } |
| 112 } |
| OLD | NEW |