Commit 8f81345f authored by Peter E Conn's avatar Peter E Conn Committed by Commit Bot

📎 Rename Verifier and VerifierDelegate.

This CL renames:
- Verifier to CurrentPageVerifier.
- VerifierDelegate to Verifier.
- TwaVerifierDelegate to TwaVerifier.
- corresponding test classes.

Bug: 1017114
Change-Id: I8c9e9688ba1eca415bc3c4b4951e2839db458c89
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1899987Reviewed-by: default avatarPeter Kotwicz <pkotwicz@chromium.org>
Commit-Queue: Peter Conn <peconn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#713158}
parent e817b5ef
......@@ -173,9 +173,9 @@ chrome_java_sources = [
"java/src/org/chromium/chrome/browser/browserservices/trustedwebactivityui/controller/TrustedWebActivityDisclosureController.java",
"java/src/org/chromium/chrome/browser/browserservices/trustedwebactivityui/controller/TrustedWebActivityOpenTimeRecorder.java",
"java/src/org/chromium/chrome/browser/browserservices/trustedwebactivityui/controller/TwaRegistrar.java",
"java/src/org/chromium/chrome/browser/browserservices/trustedwebactivityui/controller/TwaVerifierDelegate.java",
"java/src/org/chromium/chrome/browser/browserservices/trustedwebactivityui/controller/TwaVerifier.java",
"java/src/org/chromium/chrome/browser/browserservices/trustedwebactivityui/controller/CurrentPageVerifier.java",
"java/src/org/chromium/chrome/browser/browserservices/trustedwebactivityui/controller/Verifier.java",
"java/src/org/chromium/chrome/browser/browserservices/trustedwebactivityui/controller/VerifierDelegate.java",
"java/src/org/chromium/chrome/browser/browserservices/trustedwebactivityui/sharing/TwaSharingController.java",
"java/src/org/chromium/chrome/browser/browserservices/trustedwebactivityui/splashscreen/SplashImageHolder.java",
"java/src/org/chromium/chrome/browser/browserservices/trustedwebactivityui/splashscreen/TwaSplashController.java",
......
......@@ -30,11 +30,11 @@ chrome_junit_test_java_sources = [
"junit/src/org/chromium/chrome/browser/browserservices/permissiondelegation/NotificationChannelPreserverTest.java",
"junit/src/org/chromium/chrome/browser/browserservices/permissiondelegation/NotificationPermissionUpdaterTest.java",
"junit/src/org/chromium/chrome/browser/browserservices/trustedwebactivityui/controller/ClientAppDataRecorderTest.java",
"junit/src/org/chromium/chrome/browser/browserservices/trustedwebactivityui/controller/TestVerifierDelegate.java",
"junit/src/org/chromium/chrome/browser/browserservices/trustedwebactivityui/controller/TestVerifier.java",
"junit/src/org/chromium/chrome/browser/browserservices/trustedwebactivityui/controller/TrustedWebActivityDisclosureControllerTest.java",
"junit/src/org/chromium/chrome/browser/browserservices/trustedwebactivityui/controller/TrustedWebActivityOpenTimeRecorderTest.java",
"junit/src/org/chromium/chrome/browser/browserservices/trustedwebactivityui/controller/TwaVerifierDelegateTest.java",
"junit/src/org/chromium/chrome/browser/browserservices/trustedwebactivityui/controller/VerifierTest.java",
"junit/src/org/chromium/chrome/browser/browserservices/trustedwebactivityui/controller/TwaVerifierTest.java",
"junit/src/org/chromium/chrome/browser/browserservices/trustedwebactivityui/controller/CurrentPageVerifierTest.java",
"junit/src/org/chromium/chrome/browser/compositor/CompositorSurfaceManagerImplTest.java",
"junit/src/org/chromium/chrome/browser/compositor/animation/CompositorAnimationHandlerTest.java",
"junit/src/org/chromium/chrome/browser/compositor/animation/CompositorAnimatorTest.java",
......
......@@ -9,13 +9,13 @@ import org.chromium.chrome.browser.browserservices.BrowserServicesIntentDataProv
import org.chromium.chrome.browser.browserservices.Origin;
import org.chromium.chrome.browser.browserservices.TrustedWebActivityUmaRecorder;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.ClientPackageNameProvider;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.CurrentPageVerifier;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.TrustedWebActivityBrowserControlsVisibilityManager;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.TrustedWebActivityDisclosureController;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.TrustedWebActivityOpenTimeRecorder;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.TwaRegistrar;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.CurrentPageVerifier.VerificationStatus;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.Verifier;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.Verifier.VerificationStatus;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.VerifierDelegate;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.splashscreen.TwaSplashController;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.view.TrustedWebActivityDisclosureView;
import org.chromium.chrome.browser.customtabs.CustomTabIntentDataProvider;
......@@ -39,7 +39,7 @@ import dagger.Lazy;
@ActivityScope
public class TrustedWebActivityCoordinator implements InflationObserver {
private final Verifier mVerifier;
private final CurrentPageVerifier mCurrentPageVerifier;
private TrustedWebActivityBrowserControlsVisibilityManager mBrowserControlsVisibilityManager;
private final CustomTabStatusBarColorProvider mStatusBarColorProvider;
private final Lazy<ImmersiveModeController> mImmersiveModeController;
......@@ -53,8 +53,8 @@ public class TrustedWebActivityCoordinator implements InflationObserver {
TrustedWebActivityDisclosureController disclosureController,
TrustedWebActivityDisclosureView disclosureView,
TrustedWebActivityOpenTimeRecorder openTimeRecorder,
CurrentPageVerifier currentPageVerifier,
Verifier verifier,
VerifierDelegate verifierDelegate,
CustomTabActivityNavigationController navigationController,
Lazy<TwaSplashController> splashController,
CustomTabIntentDataProvider intentDataProvider,
......@@ -68,7 +68,7 @@ public class TrustedWebActivityCoordinator implements InflationObserver {
CustomTabsConnection customTabsConnection) {
// We don't need to do anything with most of the classes above, we just need to resolve them
// so they start working.
mVerifier = verifier;
mCurrentPageVerifier = currentPageVerifier;
mBrowserControlsVisibilityManager = browserControlsVisibilityManager;
mStatusBarColorProvider = statusBarColorProvider;
mImmersiveModeController = immersiveModeController;
......@@ -76,10 +76,10 @@ public class TrustedWebActivityCoordinator implements InflationObserver {
mClientPackageNameProvider = clientPackageNameProvider;
navigationController.setLandingPageOnCloseCriterion(
verifierDelegate::wasPreviouslyVerified);
verifier::wasPreviouslyVerified);
initSplashScreen(splashController, intentDataProvider, umaRecorder);
verifier.addVerificationObserver(this::onVerificationUpdate);
currentPageVerifier.addVerificationObserver(this::onVerificationUpdate);
lifecycleDispatcher.register(this);
lifecycleDispatcher.register(
new PostMessageDisabler(customTabsConnection, intentDataProvider));
......@@ -87,7 +87,7 @@ public class TrustedWebActivityCoordinator implements InflationObserver {
@Override
public void onPreInflationStartup() {
if (mVerifier.getState() == null) {
if (mCurrentPageVerifier.getState() == null) {
updateImmersiveMode(true); // Set immersive mode ASAP, before layout inflation.
}
}
......@@ -96,7 +96,7 @@ public class TrustedWebActivityCoordinator implements InflationObserver {
public void onPostInflationStartup() {
// Before the verification completes, we optimistically expect it to be successful and apply
// the trusted web activity mode to UI.
if (mVerifier.getState() == null) {
if (mCurrentPageVerifier.getState() == null) {
updateUi(true);
}
}
......@@ -115,7 +115,7 @@ public class TrustedWebActivityCoordinator implements InflationObserver {
}
private void onVerificationUpdate() {
Verifier.VerificationState state = mVerifier.getState();
CurrentPageVerifier.VerificationState state = mCurrentPageVerifier.getState();
// The state will start off as null and progress to PENDING then SUCCESS/FAILURE. We only
// want to register the clients once the state reaches SUCCESS, however we are happy to
......
......@@ -31,7 +31,7 @@ import javax.inject.Named;
* - Transforming the origin into a domain (requires native).
*
* Lifecycle: There should be a 1-1 relationship between this class and
* {@link Verifier}. Having more instances won't effect correctness, but will
* {@link CurrentPageVerifier}. Having more instances won't effect correctness, but will
* limit the performance benefits of the cache.
* Thread safety: All methods on this class should be called from the same thread.
*/
......
// Copyright 2018 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.chrome.browser.browserservices.trustedwebactivityui.controller;
import org.chromium.base.ObserverList;
import org.chromium.base.Promise;
import org.chromium.chrome.browser.browserservices.BrowserServicesIntentDataProvider;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.TrustedWebActivityModel;
import org.chromium.chrome.browser.customtabs.CustomTabIntentDataProvider;
import org.chromium.chrome.browser.customtabs.content.CustomTabActivityTabProvider;
import org.chromium.chrome.browser.dependency_injection.ActivityScope;
import org.chromium.chrome.browser.lifecycle.ActivityLifecycleDispatcher;
import org.chromium.chrome.browser.lifecycle.NativeInitObserver;
import org.chromium.chrome.browser.tab.EmptyTabObserver;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tab.TabObserver;
import org.chromium.chrome.browser.tab.TabObserverRegistrar;
import org.chromium.content_public.browser.NavigationHandle;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.inject.Inject;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
/**
* Checks whether the currently seen web page belongs to a verified origin and updates the
* {@link TrustedWebActivityModel} accordingly.
*/
@ActivityScope
public class CurrentPageVerifier implements NativeInitObserver {
private final CustomTabActivityTabProvider mTabProvider;
private final BrowserServicesIntentDataProvider mIntentDataProvider;
private final Verifier mDelegate;
@Nullable private VerificationState mState;
private final ObserverList<Runnable> mObservers = new ObserverList<>();
@IntDef({VerificationStatus.PENDING, VerificationStatus.SUCCESS, VerificationStatus.FAILURE})
@Retention(RetentionPolicy.SOURCE)
public @interface VerificationStatus {
int PENDING = 0;
int SUCCESS = 1;
int FAILURE = 2;
}
/** Represents the verification state of currently viewed web page. */
public static class VerificationState {
public final String scope;
@VerificationStatus
public final int status;
public VerificationState(String scope, @VerificationStatus int status) {
this.scope = scope;
this.status = status;
}
}
/** A {@link TabObserver} that checks whether we are on a verified page on navigation. */
private final TabObserver mVerifyOnPageLoadObserver = new EmptyTabObserver() {
@Override
public void onDidFinishNavigation(Tab tab, NavigationHandle navigation) {
if (!navigation.hasCommitted() || !navigation.isInMainFrame()) return;
verify(navigation.getUrl());
}
};
private final CustomTabActivityTabProvider.Observer mVerifyOnTabSwitchObserver =
new CustomTabActivityTabProvider.Observer() {
@Override
public void onTabSwapped(@NonNull Tab tab) {
// When a link with target="_blank" is followed and the user navigates back, we
// don't get the onDidFinishNavigation event (because the original page wasn't
// navigated away from, it was only ever hidden). https://crbug.com/942088
verify(tab.getUrl());
}
};
@Inject
public CurrentPageVerifier(
ActivityLifecycleDispatcher lifecycleDispatcher,
TabObserverRegistrar tabObserverRegistrar,
CustomTabActivityTabProvider tabProvider,
CustomTabIntentDataProvider intentDataProvider,
Verifier delegate) {
// TODO(peconn): Change the CustomTabIntentDataProvider to a BrowserServices... once
// https://chromium-review.googlesource.com/c/chromium/src/+/1877600 has landed.
mTabProvider = tabProvider;
mIntentDataProvider = intentDataProvider;
mDelegate = delegate;
tabObserverRegistrar.registerTabObserver(mVerifyOnPageLoadObserver);
tabProvider.addObserver(mVerifyOnTabSwitchObserver);
lifecycleDispatcher.register(this);
}
/**
* @return the {@link VerificationState} of the page we are currently on.
* Since verification may require native, may return null before native is loaded.
*/
@Nullable
public VerificationState getState() {
return mState;
}
public void addVerificationObserver(Runnable observer) {
mObservers.addObserver(observer);
}
public void removeVerificationObserver(Runnable observer) {
mObservers.removeObserver(observer);
}
@Override
public void onFinishNativeInitialization() {
verify(mIntentDataProvider.getUrlToLoad());
}
/**
* Perform verification for the given page.
*/
private void verify(String url) {
Promise<Boolean> result = mDelegate.verify(url);
String scope = mDelegate.getVerifiedScope(url);
if (scope == null) return;
if (result.isFulfilled()) {
updateState(scope, statusFromBoolean(result.getResult()));
} else {
updateState(scope, VerificationStatus.PENDING);
result.then(verified -> { onVerificationResult(scope, verified); });
}
}
/**
* Is called as a result of a verification request to OriginVerifier. Is not called if the
* client called |validateRelationship| before launching the TWA and we found that verification
* in the cache.
*/
private void onVerificationResult(String scope, boolean verified) {
Tab tab = mTabProvider.getTab();
boolean resultStillApplies = tab != null
&& scope.equals(mDelegate.getVerifiedScope(tab.getUrl()));
if (resultStillApplies) {
updateState(scope, verified ? VerificationStatus.SUCCESS : VerificationStatus.FAILURE);
}
}
private void updateState(String scope, @VerificationStatus int status) {
mState = new VerificationState(scope, status);
for (Runnable observer : mObservers) {
observer.run();
}
}
private static @VerificationStatus int statusFromBoolean(boolean success) {
return success ? VerificationStatus.SUCCESS : VerificationStatus.FAILURE;
}
}
......@@ -12,8 +12,8 @@ import static org.chromium.chrome.browser.browserservices.trustedwebactivityui.T
import org.chromium.chrome.browser.browserservices.TrustedWebActivityUmaRecorder;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.TrustedWebActivityModel;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.Verifier.VerificationState;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.Verifier.VerificationStatus;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.CurrentPageVerifier.VerificationState;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.CurrentPageVerifier.VerificationStatus;
import org.chromium.chrome.browser.lifecycle.ActivityLifecycleDispatcher;
import org.chromium.chrome.browser.lifecycle.NativeInitObserver;
import org.chromium.chrome.browser.preferences.ChromePreferenceManager;
......@@ -28,7 +28,7 @@ public class TrustedWebActivityDisclosureController implements NativeInitObserve
TrustedWebActivityModel.DisclosureEventsCallback {
private final ChromePreferenceManager mPreferenceManager;
private final TrustedWebActivityModel mModel;
private final Verifier mVerifier;
private final CurrentPageVerifier mCurrentPageVerifier;
private final TrustedWebActivityUmaRecorder mRecorder;
private final ClientPackageNameProvider mClientPackageNameProvider;
......@@ -37,16 +37,16 @@ public class TrustedWebActivityDisclosureController implements NativeInitObserve
ChromePreferenceManager preferenceManager,
TrustedWebActivityModel model,
ActivityLifecycleDispatcher lifecycleDispatcher,
Verifier verifier,
CurrentPageVerifier currentPageVerifier,
TrustedWebActivityUmaRecorder recorder,
ClientPackageNameProvider clientPackageNameProvider) {
mVerifier = verifier;
mCurrentPageVerifier = currentPageVerifier;
mPreferenceManager = preferenceManager;
mModel = model;
mRecorder = recorder;
mClientPackageNameProvider = clientPackageNameProvider;
model.set(DISCLOSURE_EVENTS_CALLBACK, this);
verifier.addVerificationObserver(this::onVerificationStatusChanged);
currentPageVerifier.addVerificationObserver(this::onVerificationStatusChanged);
lifecycleDispatcher.register(this);
}
......@@ -95,7 +95,7 @@ public class TrustedWebActivityDisclosureController implements NativeInitObserve
}
private boolean shouldShowInCurrentState() {
VerificationState state = mVerifier.getState();
VerificationState state = mCurrentPageVerifier.getState();
return state != null && state.status != VerificationStatus.FAILURE;
}
......
......@@ -8,8 +8,8 @@ import android.os.SystemClock;
import org.chromium.chrome.browser.ActivityTabProvider;
import org.chromium.chrome.browser.browserservices.TrustedWebActivityUmaRecorder;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.Verifier.VerificationState;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.Verifier.VerificationStatus;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.CurrentPageVerifier.VerificationState;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.CurrentPageVerifier.VerificationStatus;
import org.chromium.chrome.browser.dependency_injection.ActivityScope;
import org.chromium.chrome.browser.lifecycle.ActivityLifecycleDispatcher;
import org.chromium.chrome.browser.lifecycle.PauseResumeWithNativeObserver;
......@@ -21,7 +21,7 @@ import javax.inject.Inject;
*/
@ActivityScope
public class TrustedWebActivityOpenTimeRecorder implements PauseResumeWithNativeObserver {
private final Verifier mVerifier;
private final CurrentPageVerifier mCurrentPageVerifier;
private final TrustedWebActivityUmaRecorder mRecorder;
private final ActivityTabProvider mTabProvider;
......@@ -34,14 +34,14 @@ public class TrustedWebActivityOpenTimeRecorder implements PauseResumeWithNative
@Inject
TrustedWebActivityOpenTimeRecorder(
ActivityLifecycleDispatcher lifecycleDispatcher,
Verifier verifier,
CurrentPageVerifier currentPageVerifier,
TrustedWebActivityUmaRecorder recorder,
ActivityTabProvider provider) {
mVerifier = verifier;
mCurrentPageVerifier = currentPageVerifier;
mRecorder = recorder;
mTabProvider = provider;
lifecycleDispatcher.register(this);
verifier.addVerificationObserver(this::onVerificationStateChanged);
currentPageVerifier.addVerificationObserver(this::onVerificationStateChanged);
}
@Override
......@@ -58,7 +58,7 @@ public class TrustedWebActivityOpenTimeRecorder implements PauseResumeWithNative
}
private void onVerificationStateChanged() {
VerificationState state = mVerifier.getState();
VerificationState state = mCurrentPageVerifier.getState();
if (state == null || state.status == VerificationStatus.PENDING) {
return;
}
......
......@@ -23,10 +23,10 @@ import javax.inject.Inject;
import androidx.browser.customtabs.CustomTabsService;
/**
* Provides Trusted Web Activity specific behaviour for the {@link Verifier}.
* Provides Trusted Web Activity specific behaviour for the {@link CurrentPageVerifier}.
*/
@ActivityScope
public class TwaVerifierDelegate implements VerifierDelegate, Destroyable {
public class TwaVerifier implements Verifier, Destroyable {
/** The Digital Asset Link relationship used for Trusted Web Activities. */
private static final int RELATIONSHIP = CustomTabsService.RELATION_HANDLE_ALL_URLS;
......@@ -37,7 +37,7 @@ public class TwaVerifierDelegate implements VerifierDelegate, Destroyable {
private Set<Origin> mOriginsPendingVerification;
@Inject
public TwaVerifierDelegate(
public TwaVerifier(
ActivityLifecycleDispatcher lifecycleDispatcher,
CustomTabIntentDataProvider intentDataProvider,
OriginVerifier.Factory originVerifierFactory,
......
// Copyright 2018 The Chromium Authors. All rights reserved.
// Copyright 2019 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.chrome.browser.browserservices.trustedwebactivityui.controller;
import org.chromium.base.ObserverList;
import org.chromium.base.Promise;
import org.chromium.chrome.browser.browserservices.BrowserServicesIntentDataProvider;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.TrustedWebActivityModel;
import org.chromium.chrome.browser.customtabs.CustomTabIntentDataProvider;
import org.chromium.chrome.browser.customtabs.content.CustomTabActivityTabProvider;
import org.chromium.chrome.browser.dependency_injection.ActivityScope;
import org.chromium.chrome.browser.lifecycle.ActivityLifecycleDispatcher;
import org.chromium.chrome.browser.lifecycle.NativeInitObserver;
import org.chromium.chrome.browser.tab.EmptyTabObserver;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tab.TabObserver;
import org.chromium.chrome.browser.tab.TabObserverRegistrar;
import org.chromium.content_public.browser.NavigationHandle;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.inject.Inject;
import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
/**
* Checks whether the currently seen web page belongs to a verified origin and updates the
* {@link TrustedWebActivityModel} accordingly.
* A Delegate for the {@link CurrentPageVerifier} that provides implementation specific to
* Trusted Web Activities, WebAPKs or A2HS as appropriate.
*/
@ActivityScope
public class Verifier implements NativeInitObserver {
private final CustomTabActivityTabProvider mTabProvider;
private final BrowserServicesIntentDataProvider mIntentDataProvider;
private final VerifierDelegate mDelegate;
@Nullable private VerificationState mState;
private final ObserverList<Runnable> mObservers = new ObserverList<>();
@IntDef({VerificationStatus.PENDING, VerificationStatus.SUCCESS, VerificationStatus.FAILURE})
@Retention(RetentionPolicy.SOURCE)
public @interface VerificationStatus {
int PENDING = 0;
int SUCCESS = 1;
int FAILURE = 2;
}
/** Represents the verification state of currently viewed web page. */
public static class VerificationState {
public final String scope;
@VerificationStatus
public final int status;
public VerificationState(String scope, @VerificationStatus int status) {
this.scope = scope;
this.status = status;
}
}
/** A {@link TabObserver} that checks whether we are on a verified page on navigation. */
private final TabObserver mVerifyOnPageLoadObserver = new EmptyTabObserver() {
@Override
public void onDidFinishNavigation(Tab tab, NavigationHandle navigation) {
if (!navigation.hasCommitted() || !navigation.isInMainFrame()) return;
verify(navigation.getUrl());
}
};
private final CustomTabActivityTabProvider.Observer mVerifyOnTabSwitchObserver =
new CustomTabActivityTabProvider.Observer() {
@Override
public void onTabSwapped(@NonNull Tab tab) {
// When a link with target="_blank" is followed and the user navigates back, we
// don't get the onDidFinishNavigation event (because the original page wasn't
// navigated away from, it was only ever hidden). https://crbug.com/942088
verify(tab.getUrl());
}
};
@Inject
public Verifier(
ActivityLifecycleDispatcher lifecycleDispatcher,
TabObserverRegistrar tabObserverRegistrar,
CustomTabActivityTabProvider tabProvider,
CustomTabIntentDataProvider intentDataProvider,
VerifierDelegate delegate) {
// TODO(peconn): Change the CustomTabIntentDataProvider to a BrowserServices... once
// https://chromium-review.googlesource.com/c/chromium/src/+/1877600 has landed.
mTabProvider = tabProvider;
mIntentDataProvider = intentDataProvider;
mDelegate = delegate;
tabObserverRegistrar.registerTabObserver(mVerifyOnPageLoadObserver);
tabProvider.addObserver(mVerifyOnTabSwitchObserver);
lifecycleDispatcher.register(this);
}
public interface Verifier {
/**
* @return the {@link VerificationState} of the page we are currently on.
* Since verification may require native, may return null before native is loaded.
* Checks whether the given URL is verified.
*
* The returned Promise may be immediately fulfilled (eg if we know that the given url is one
* we shouldn't even attempt to verify or if we have a cached result). It is worth explicitly
* checking for this to deal with the result synchronously and not incur the delay of
* {@link Promise#then}.
*/
@Nullable
public VerificationState getState() {
return mState;
}
public void addVerificationObserver(Runnable observer) {
mObservers.addObserver(observer);
}
public void removeVerificationObserver(Runnable observer) {
mObservers.removeObserver(observer);
}
@Override
public void onFinishNativeInitialization() {
verify(mIntentDataProvider.getUrlToLoad());
}
Promise<Boolean> verify(String url);
/**
* Perform verification for the given page.
* A synchronous version of verify that returns true iff verification has previously been
* completed successfully for the given url.
*/
private void verify(String url) {
Promise<Boolean> result = mDelegate.verify(url);
String scope = mDelegate.getVerifiedScope(url);
if (scope == null) return;
if (result.isFulfilled()) {
updateState(scope, statusFromBoolean(result.getResult()));
} else {
updateState(scope, VerificationStatus.PENDING);
result.then(verified -> { onVerificationResult(scope, verified); });
}
}
boolean wasPreviouslyVerified(String url);
/**
* Is called as a result of a verification request to OriginVerifier. Is not called if the
* client called |validateRelationship| before launching the TWA and we found that verification
* in the cache.
* Returns the widest scope for which verification is relevant. This can be used to determine
* whether two different urls are the same for the purposes of verification. Returns
* {@link null} if the given url cannot be parsed.
*
* The purpose of this method is to determine whether two different pages can share verification
* state. Eg, if we've already verified a TWA for https://www.example.com/webapp/page1.html we
* don't need to perform verification again for https://www.example.com/webapp/folder/page2.html
* (but we do for https://developers.google.com/web/updates).
*
* eg, for a TWA where verification is on a per origin basis, this method would map to origins:
* https://www.example.com/webapp/page1.html -> https://www.example.com
* https://www.example.com/webapp/folder/page2.html -> https://www.example.com
* https://developers.google.com/web/updates -> https://developers.google.com
*
* eg, say we have a WebAPK with the verified scope being https://www.example.com/webapp/, then
* this method would map:
*
* https://www.example.com/webapp/page1.html -> https://www.example.com/webapp/
* https://www.example.com/webapp/folder/page2.html -> https://www.example.com/webapp/
* https://www.example.com/somewhere_else/page3.html -> https://www.example.com/somewhere_else/
*
* The last result can really be anything other than https://www.example.com/webapp/ - just
* something to signify we aren't on the verified scope.
*/
private void onVerificationResult(String scope, boolean verified) {
Tab tab = mTabProvider.getTab();
boolean resultStillApplies = tab != null
&& scope.equals(mDelegate.getVerifiedScope(tab.getUrl()));
if (resultStillApplies) {
updateState(scope, verified ? VerificationStatus.SUCCESS : VerificationStatus.FAILURE);
}
}
private void updateState(String scope, @VerificationStatus int status) {
mState = new VerificationState(scope, status);
for (Runnable observer : mObservers) {
observer.run();
}
}
private static @VerificationStatus int statusFromBoolean(boolean success) {
return success ? VerificationStatus.SUCCESS : VerificationStatus.FAILURE;
}
@Nullable
String getVerifiedScope(String url);
}
// Copyright 2019 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.chrome.browser.browserservices.trustedwebactivityui.controller;
import org.chromium.base.Promise;
import androidx.annotation.Nullable;
/**
* A Delegate for the {@link Verifier} that provides implementation specific to
* Trusted Web Activities, WebAPKs or A2HS as appropriate.
*/
public interface VerifierDelegate {
/**
* Checks whether the given URL is verified.
*
* The returned Promise may be immediately fulfilled (eg if we know that the given url is one
* we shouldn't even attempt to verify or if we have a cached result). It is worth explicitly
* checking for this to deal with the result synchronously and not incur the delay of
* {@link Promise#then}.
*/
Promise<Boolean> verify(String url);
/**
* A synchronous version of verify that returns true iff verification has previously been
* completed successfully for the given url.
*/
boolean wasPreviouslyVerified(String url);
/**
* Returns the widest scope for which verification is relevant. This can be used to determine
* whether two different urls are the same for the purposes of verification. Returns
* {@link null} if the given url cannot be parsed.
*
* The purpose of this method is to determine whether two different pages can share verification
* state. Eg, if we've already verified a TWA for https://www.example.com/webapp/page1.html we
* don't need to perform verification again for https://www.example.com/webapp/folder/page2.html
* (but we do for https://developers.google.com/web/updates).
*
* eg, for a TWA where verification is on a per origin basis, this method would map to origins:
* https://www.example.com/webapp/page1.html -> https://www.example.com
* https://www.example.com/webapp/folder/page2.html -> https://www.example.com
* https://developers.google.com/web/updates -> https://developers.google.com
*
* eg, say we have a WebAPK with the verified scope being https://www.example.com/webapp/, then
* this method would map:
*
* https://www.example.com/webapp/page1.html -> https://www.example.com/webapp/
* https://www.example.com/webapp/folder/page2.html -> https://www.example.com/webapp/
* https://www.example.com/somewhere_else/page3.html -> https://www.example.com/somewhere_else/
*
* The last result can really be anything other than https://www.example.com/webapp/ - just
* something to signify we aren't on the verified scope.
*/
@Nullable
String getVerifiedScope(String url);
}
......@@ -11,7 +11,7 @@ import android.util.Pair;
import org.chromium.base.Promise;
import org.chromium.chrome.browser.browserservices.TrustedWebActivityUmaRecorder;
import org.chromium.chrome.browser.browserservices.TrustedWebActivityUmaRecorder.ShareRequestMethod;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.TwaVerifierDelegate;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.TwaVerifier;
import org.chromium.chrome.browser.customtabs.CustomTabIntentDataProvider;
import org.chromium.chrome.browser.customtabs.content.CustomTabActivityNavigationController;
import org.chromium.chrome.browser.customtabs.content.CustomTabActivityTabProvider;
......@@ -36,14 +36,14 @@ public class TwaSharingController {
private final CustomTabActivityTabProvider mTabProvider;
private final CustomTabActivityNavigationController mNavigationController;
private final WebApkPostShareTargetNavigator mPostNavigator;
private final TwaVerifierDelegate mVerifierDelegate;
private final TwaVerifier mVerifierDelegate;
private final TrustedWebActivityUmaRecorder mUmaRecorder;
@Inject
public TwaSharingController(CustomTabActivityTabProvider tabProvider,
CustomTabActivityNavigationController navigationController,
WebApkPostShareTargetNavigator postNavigator,
TwaVerifierDelegate verifierDelegate,
TwaVerifier verifierDelegate,
TrustedWebActivityUmaRecorder umaRecorder) {
mTabProvider = tabProvider;
mNavigationController = navigationController;
......
......@@ -8,8 +8,8 @@ import org.chromium.chrome.browser.browserservices.BrowserServicesActivityTabCon
import org.chromium.chrome.browser.browserservices.BrowserServicesIntentDataProvider;
import org.chromium.chrome.browser.browserservices.ClientAppDataRegister;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.TwaIntentHandlingStrategy;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.TwaVerifierDelegate;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.VerifierDelegate;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.TwaVerifier;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.Verifier;
import org.chromium.chrome.browser.customtabs.CustomTabIntentDataProvider;
import org.chromium.chrome.browser.customtabs.CustomTabNightModeStateController;
import org.chromium.chrome.browser.customtabs.content.CustomTabActivityTabController;
......@@ -78,7 +78,7 @@ public class CustomTabActivityModule {
}
@Provides
public VerifierDelegate provideVerifierDelegate(Lazy<TwaVerifierDelegate> twaVerifierDelegate) {
public Verifier provideVerifierDelegate(Lazy<TwaVerifier> twaVerifierDelegate) {
// TODO(peconn): Add handing of WebAPK/A2HS delegate.
return twaVerifierDelegate.get();
}
......
......@@ -23,7 +23,7 @@ import org.robolectric.annotation.Config;
import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.chrome.browser.ChromeFeatureList;
import org.chromium.chrome.browser.browserservices.Origin;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.Verifier.VerificationStatus;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.CurrentPageVerifier.VerificationStatus;
import org.chromium.chrome.browser.customtabs.CustomTabIntentDataProvider;
import org.chromium.chrome.browser.customtabs.content.CustomTabActivityTabProvider;
import org.chromium.chrome.browser.lifecycle.ActivityLifecycleDispatcher;
......@@ -38,13 +38,13 @@ import org.chromium.content_public.browser.NavigationHandle;
import java.util.Collections;
/**
* Tests for {@link Verifier}.
* Tests for {@link CurrentPageVerifier}.
*/
@RunWith(BaseRobolectricTestRunner.class)
@Config(manifest = Config.NONE)
@EnableFeatures(ChromeFeatureList.TRUSTED_WEB_ACTIVITY)
@DisableFeatures(ChromeFeatureList.TRUSTED_WEB_ACTIVITY_POST_MESSAGE)
public class VerifierTest {
public class CurrentPageVerifierTest {
private static final Origin TRUSTED_ORIGIN = Origin.create("https://www.origin1.com/");
private static final Origin OTHER_TRUSTED_ORIGIN = Origin.create("https://www.origin2.com/");
private static final String TRUSTED_ORIGIN_PAGE1 = TRUSTED_ORIGIN + "/page1";
......@@ -65,9 +65,9 @@ public class VerifierTest {
ClientPackageNameProvider mClientPackageNameProvider;
@Captor ArgumentCaptor<TabObserver> mTabObserverCaptor;
TestVerifierDelegate mVerifierDelegate = new TestVerifierDelegate();
TestVerifier mVerifierDelegate = new TestVerifier();
private Verifier mVerifier;
private CurrentPageVerifier mCurrentPageVerifier;
@Before
public void setUp() {
......@@ -77,7 +77,7 @@ public class VerifierTest {
doNothing().when(mTabObserverRegistrar).registerTabObserver(mTabObserverCaptor.capture());
when(mIntentDataProvider.getTrustedWebActivityAdditionalOrigins())
.thenReturn(Collections.singletonList("https://www.origin2.com/"));
mVerifier = new Verifier(mLifecycleDispatcher, mTabObserverRegistrar,
mCurrentPageVerifier = new CurrentPageVerifier(mLifecycleDispatcher, mTabObserverRegistrar,
mTabProvider, mIntentDataProvider, mVerifierDelegate);
// TODO(peconn): Add check on permission updated being updated.
}
......@@ -85,21 +85,21 @@ public class VerifierTest {
@Test
public void verifiesOriginOfInitialPage() {
setInitialUrl(TRUSTED_ORIGIN_PAGE1);
mVerifier.onFinishNativeInitialization();
mCurrentPageVerifier.onFinishNativeInitialization();
verifyStartsVerification(TRUSTED_ORIGIN_PAGE1);
}
@Test
public void statusIsPending_UntilVerificationFinished() {
setInitialUrl(TRUSTED_ORIGIN_PAGE1);
mVerifier.onFinishNativeInitialization();
mCurrentPageVerifier.onFinishNativeInitialization();
assertStatus(VerificationStatus.PENDING);
}
@Test
public void statusIsSuccess_WhenVerificationSucceeds() {
setInitialUrl(TRUSTED_ORIGIN_PAGE1);
mVerifier.onFinishNativeInitialization();
mCurrentPageVerifier.onFinishNativeInitialization();
mVerifierDelegate.passVerification(Origin.create(TRUSTED_ORIGIN_PAGE1));
assertStatus(VerificationStatus.SUCCESS);
}
......@@ -107,7 +107,7 @@ public class VerifierTest {
@Test
public void statusIsFail_WhenVerificationFails() {
setInitialUrl(UNTRUSTED_PAGE);
mVerifier.onFinishNativeInitialization();
mCurrentPageVerifier.onFinishNativeInitialization();
mVerifierDelegate.failVerification(Origin.create(UNTRUSTED_PAGE));
assertStatus(VerificationStatus.FAILURE);
}
......@@ -115,7 +115,7 @@ public class VerifierTest {
@Test
public void verifies_WhenNavigatingToOtherTrustedOrigin() {
setInitialUrl(TRUSTED_ORIGIN_PAGE1);
mVerifier.onFinishNativeInitialization();
mCurrentPageVerifier.onFinishNativeInitialization();
mVerifierDelegate.passVerification(Origin.create(TRUSTED_ORIGIN_PAGE1));
navigateToUrl(OTHER_TRUSTED_ORIGIN_PAGE1);
......@@ -125,7 +125,7 @@ public class VerifierTest {
@Test
public void doesntUpdateState_IfVerificationFinishedAfterLeavingOrigin() {
setInitialUrl(TRUSTED_ORIGIN_PAGE1);
mVerifier.onFinishNativeInitialization();
mCurrentPageVerifier.onFinishNativeInitialization();
navigateToUrl(UNTRUSTED_PAGE);
mVerifierDelegate.failVerification(Origin.create(UNTRUSTED_PAGE));
......@@ -135,7 +135,7 @@ public class VerifierTest {
@Test
public void reverifiesOrigin_WhenReturningToIt_IfFirstVerificationDidntFinishInTime() {
setInitialUrl(TRUSTED_ORIGIN_PAGE1);
mVerifier.onFinishNativeInitialization();
mCurrentPageVerifier.onFinishNativeInitialization();
navigateToUrl(OTHER_TRUSTED_ORIGIN_PAGE1);
mVerifierDelegate.passVerification(Origin.create(OTHER_TRUSTED_ORIGIN_PAGE1));
navigateToUrl(TRUSTED_ORIGIN_PAGE1);
......@@ -143,8 +143,8 @@ public class VerifierTest {
assertStatus(VerificationStatus.SUCCESS);
}
private void assertStatus(@Verifier.VerificationStatus int status) {
assertEquals(status, mVerifier.getState().status);
private void assertStatus(@CurrentPageVerifier.VerificationStatus int status) {
assertEquals(status, mCurrentPageVerifier.getState().status);
}
private void verifyStartsVerification(String url) {
......
......@@ -13,9 +13,9 @@ import java.util.Map;
import java.util.Set;
/**
* A {@link VerifierDelegate} for testing.
* A {@link Verifier} for testing.
*/
class TestVerifierDelegate implements VerifierDelegate {
class TestVerifier implements Verifier {
private final Set<Origin> mPreviouslyVerifiedOrigins = new HashSet<>();
private final Map<Origin, Promise<Boolean>> mPendingVerifications = new HashMap<>();
......
......@@ -28,8 +28,8 @@ import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.base.test.util.Feature;
import org.chromium.chrome.browser.browserservices.TrustedWebActivityUmaRecorder;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.TrustedWebActivityModel;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.Verifier.VerificationState;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.Verifier.VerificationStatus;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.CurrentPageVerifier.VerificationState;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.CurrentPageVerifier.VerificationStatus;
import org.chromium.chrome.browser.lifecycle.ActivityLifecycleDispatcher;
import org.chromium.chrome.browser.preferences.ChromePreferenceManager;
......@@ -44,7 +44,7 @@ public class TrustedWebActivityDisclosureControllerTest {
@Mock public ChromePreferenceManager mPreferences;
@Mock public ActivityLifecycleDispatcher mLifecycleDispatcher;
@Mock public Verifier mVerifier;
@Mock public CurrentPageVerifier mCurrentPageVerifier;
@Mock public TrustedWebActivityUmaRecorder mRecorder;
@Mock public ClientPackageNameProvider mClientPackageNameProvider;
......@@ -57,11 +57,12 @@ public class TrustedWebActivityDisclosureControllerTest {
MockitoAnnotations.initMocks(this);
doReturn(CLIENT_PACKAGE).when(mClientPackageNameProvider).get();
doNothing().when(mVerifier).addVerificationObserver(mVerificationObserverCaptor.capture());
doNothing().when(mCurrentPageVerifier)
.addVerificationObserver(mVerificationObserverCaptor.capture());
doReturn(false).when(mPreferences).hasUserAcceptedTwaDisclosureForPackage(anyString());
new TrustedWebActivityDisclosureController(mPreferences, mModel, mLifecycleDispatcher,
mVerifier, mRecorder, mClientPackageNameProvider);
mCurrentPageVerifier, mRecorder, mClientPackageNameProvider);
}
@Test
......@@ -113,7 +114,7 @@ public class TrustedWebActivityDisclosureControllerTest {
}
private void setVerificationState(VerificationState state) {
doReturn(state).when(mVerifier).getState();
doReturn(state).when(mCurrentPageVerifier).getState();
for (Runnable observer : mVerificationObserverCaptor.getAllValues()) {
observer.run();
}
......
......@@ -25,8 +25,8 @@ import org.robolectric.annotation.Config;
import org.chromium.base.test.BaseRobolectricTestRunner;
import org.chromium.chrome.browser.ActivityTabProvider;
import org.chromium.chrome.browser.browserservices.TrustedWebActivityUmaRecorder;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.Verifier.VerificationState;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.Verifier.VerificationStatus;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.CurrentPageVerifier.VerificationState;
import org.chromium.chrome.browser.browserservices.trustedwebactivityui.controller.CurrentPageVerifier.VerificationStatus;
import org.chromium.chrome.browser.lifecycle.ActivityLifecycleDispatcher;
import java.util.concurrent.TimeUnit;
......@@ -39,7 +39,8 @@ import java.util.concurrent.TimeUnit;
public class TrustedWebActivityOpenTimeRecorderTest {
@Mock ActivityLifecycleDispatcher mLifecycleDispatcher;
@Mock Verifier mVerifier;
@Mock
CurrentPageVerifier mCurrentPageVerifier;
@Mock TrustedWebActivityUmaRecorder mUmaRecorder;
@Mock ActivityTabProvider mTabProvider;
@Captor ArgumentCaptor<Runnable> mVerificationObserverCaptor;
......@@ -49,9 +50,10 @@ public class TrustedWebActivityOpenTimeRecorderTest {
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
doNothing().when(mVerifier).addVerificationObserver(mVerificationObserverCaptor.capture());
doNothing().when(mCurrentPageVerifier)
.addVerificationObserver(mVerificationObserverCaptor.capture());
mRecorder = new TrustedWebActivityOpenTimeRecorder(
mLifecycleDispatcher, mVerifier, mUmaRecorder, mTabProvider);
mLifecycleDispatcher, mCurrentPageVerifier, mUmaRecorder, mTabProvider);
}
@Test
......@@ -202,7 +204,7 @@ public class TrustedWebActivityOpenTimeRecorderTest {
private void setVerificationStatus(@VerificationStatus int status) {
VerificationState newState = new VerificationState("www.example.com", status);
when(mVerifier.getState()).thenReturn(newState);
when(mCurrentPageVerifier.getState()).thenReturn(newState);
for (Runnable observer : mVerificationObserverCaptor.getAllValues()) {
observer.run();
}
......
......@@ -42,12 +42,12 @@ import org.chromium.chrome.test.util.browser.Features.DisableFeatures;
import java.util.Collections;
/**
* Tests for {@link TwaVerifierDelegate}.
* Tests for {@link TwaVerifier}.
*/
@RunWith(BaseRobolectricTestRunner.class)
@Config(manifest = Config.NONE)
@DisableFeatures(ChromeFeatureList.TRUSTED_WEB_ACTIVITY_POST_MESSAGE)
public class TwaVerifierDelegateTest {
public class TwaVerifierTest {
private static final String INITIAL_URL = "https://www.initialurl.com/page.html";
private static final String ADDITIONAL_ORIGIN = "https://www.otherverifiedorigin.com";
private static final String OTHER_URL = "https://www.notverifiedurl.com/page2.html";
......@@ -62,7 +62,7 @@ public class TwaVerifierDelegateTest {
@Mock CustomTabActivityTabProvider mActivityTabProvider;
@Mock ClientPackageNameProvider mClientPackageNameProvider;
private TwaVerifierDelegate mDelegate;
private TwaVerifier mDelegate;
@Before
public void setUp() {
......@@ -77,7 +77,7 @@ public class TwaVerifierDelegateTest {
when(mClientPackageNameProvider.get()).thenReturn("some.package.name");
mDelegate = new TwaVerifierDelegate(mLifecycleDispatcher, mIntentDataProvider,
mDelegate = new TwaVerifier(mLifecycleDispatcher, mIntentDataProvider,
mOriginVerifierFactory, mActivityTabProvider, mClientPackageNameProvider);
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment