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..3617d2e1955ad77432bb1e95aca6d32fc9f12cba |
--- /dev/null |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappInterceptNavigationDelegate.java |
@@ -0,0 +1,89 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
dominickn
2017/05/01 00:41:41
Nit: new file, use 2017.
piotrs
2017/05/01 02:55:06
Done.
|
+// 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 mConnection = new CustomTabsServiceConnection() { |
dominickn
2017/05/01 00:41:41
Nit: the m prefix is just for member variables.
piotrs
2017/05/01 02:55:06
Done.
|
+ @Override |
+ public void onCustomTabsServiceConnected( |
+ ComponentName componentName, CustomTabsClient customTabsClient) { |
+ mClient = customTabsClient; |
+ } |
+ |
+ @Override |
+ public void onServiceDisconnected(ComponentName componentName) { |
+ mClient = null; |
+ } |
+ }; |
+ |
+ CustomTabsClient.bindCustomTabsService(mActivity, mActivity.getPackageName(), mConnection); |
+ } |
+ |
+ @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); |
+ } |
+} |