| Index: chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappInterceptNavigationDelegate.java
|
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappInterceptNavigationDelegate.java b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappInterceptNavigationDelegate.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..9e16699cf98ae00845736c86bb90b01384ff2465
|
| --- /dev/null
|
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappInterceptNavigationDelegate.java
|
| @@ -0,0 +1,89 @@
|
| +// Copyright 2017 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.webapps;
|
| +
|
| +import android.content.ComponentName;
|
| +import android.net.Uri;
|
| +import android.os.Bundle;
|
| +import android.support.customtabs.CustomTabsCallback;
|
| +import android.support.customtabs.CustomTabsClient;
|
| +import android.support.customtabs.CustomTabsIntent;
|
| +import android.support.customtabs.CustomTabsServiceConnection;
|
| +import android.support.customtabs.CustomTabsSession;
|
| +
|
| +import org.chromium.chrome.browser.UrlConstants;
|
| +import org.chromium.chrome.browser.tab.InterceptNavigationDelegateImpl;
|
| +import org.chromium.chrome.browser.tab.Tab;
|
| +import org.chromium.chrome.browser.util.UrlUtilities;
|
| +import org.chromium.components.navigation_interception.NavigationParams;
|
| +
|
| +import javax.annotation.Nullable;
|
| +
|
| +/**
|
| + * Intercepts navigations made by the Web App and sends off-origin http(s) ones to a Custom Tab.
|
| + */
|
| +public class WebappInterceptNavigationDelegate extends InterceptNavigationDelegateImpl {
|
| + private final WebappActivity mActivity;
|
| + private CustomTabsClient mClient;
|
| +
|
| + public WebappInterceptNavigationDelegate(WebappActivity activity, Tab tab) {
|
| + super(tab);
|
| + this.mActivity = activity;
|
| +
|
| + CustomTabsServiceConnection connection = new CustomTabsServiceConnection() {
|
| + @Override
|
| + public void onCustomTabsServiceConnected(
|
| + ComponentName componentName, CustomTabsClient customTabsClient) {
|
| + mClient = customTabsClient;
|
| + }
|
| +
|
| + @Override
|
| + public void onServiceDisconnected(ComponentName componentName) {
|
| + mClient = null;
|
| + }
|
| + };
|
| +
|
| + CustomTabsClient.bindCustomTabsService(mActivity, mActivity.getPackageName(), connection);
|
| + }
|
| +
|
| + @Nullable
|
| + private CustomTabsSession getSession() {
|
| + if (mClient == null) {
|
| + return null;
|
| + }
|
| + return mClient.newSession(new CustomTabsCallback() {
|
| + @Override
|
| + public void onNavigationEvent(int navigationEvent, Bundle extras) {
|
| + super.onNavigationEvent(navigationEvent, extras);
|
| + }
|
| + });
|
| + }
|
| +
|
| + @Override
|
| + public boolean shouldIgnoreNavigation(NavigationParams navigationParams) {
|
| + if (super.shouldIgnoreNavigation(navigationParams)) {
|
| + return true;
|
| + }
|
| +
|
| + if (isHttpOrHttps(navigationParams.url)
|
| + && !UrlUtilities.sameDomainOrHost(
|
| + mActivity.mWebappInfo.uri().toString(), navigationParams.url, true)) {
|
| + CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder(getSession());
|
| + if (mActivity.mWebappInfo.hasValidThemeColor()) {
|
| + // Need to cast as themeColor is a long to contain possible error results.
|
| + intentBuilder.setToolbarColor((int) mActivity.mWebappInfo.themeColor());
|
| + }
|
| + intentBuilder.build().launchUrl(mActivity, Uri.parse(navigationParams.url));
|
| + return true;
|
| + }
|
| +
|
| + return false;
|
| + }
|
| +
|
| + private static boolean isHttpOrHttps(String url) {
|
| + return url.startsWith(UrlConstants.HTTP_URL_PREFIX)
|
| + || url.startsWith(UrlConstants.HTTPS_URL_PREFIX);
|
| + }
|
| +}
|
|
|