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() { |
Yusuf
2017/05/03 17:56:06
I dont think you actually need to maintain this at
piotrs
2017/05/03 22:58:18
There are 2 reasons I went this way:
1) This allow
Yusuf
2017/05/04 19:14:25
once you build your CustomTabsIntent, CustomTabsIn
piotrs
2017/05/09 23:43:14
Done as suggested.
|
+ @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() { |
Yusuf
2017/05/03 17:56:05
At some point, we might do more with each session
piotrs
2017/05/03 22:58:18
See reply to the comment above.
|
+ @Override |
+ public void onNavigationEvent(int navigationEvent, Bundle extras) { |
+ super.onNavigationEvent(navigationEvent, extras); |
Yusuf
2017/05/03 17:56:05
super does nothing. This part is not needed.
piotrs
2017/05/03 22:58:18
Done, thanks for noticing.
|
+ } |
+ }); |
+ } |
+ |
+ @Override |
+ public boolean shouldIgnoreNavigation(NavigationParams navigationParams) { |
+ if (super.shouldIgnoreNavigation(navigationParams)) { |
+ return true; |
+ } |
+ |
+ if (isHttpOrHttps(navigationParams.url) |
Yusuf
2017/05/03 17:56:05
Is this strictly http or https only? Data? File? A
piotrs
2017/05/03 22:58:18
I know this intent would crash if I pass data: URL
Yusuf
2017/05/04 19:14:25
Thanks.
|
+ && !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); |
+ } |
+} |