Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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.chrome.browser.webapps; | |
| 6 | |
| 7 import android.content.ComponentName; | |
| 8 import android.net.Uri; | |
| 9 import android.os.Bundle; | |
| 10 import android.support.customtabs.CustomTabsCallback; | |
| 11 import android.support.customtabs.CustomTabsClient; | |
| 12 import android.support.customtabs.CustomTabsIntent; | |
| 13 import android.support.customtabs.CustomTabsServiceConnection; | |
| 14 import android.support.customtabs.CustomTabsSession; | |
| 15 | |
| 16 import org.chromium.chrome.browser.UrlConstants; | |
| 17 import org.chromium.chrome.browser.tab.InterceptNavigationDelegateImpl; | |
| 18 import org.chromium.chrome.browser.tab.Tab; | |
| 19 import org.chromium.chrome.browser.util.UrlUtilities; | |
| 20 import org.chromium.components.navigation_interception.NavigationParams; | |
| 21 | |
| 22 import javax.annotation.Nullable; | |
| 23 | |
| 24 /** | |
| 25 * Intercepts navigations made by the Web App and sends off-origin http(s) ones to a Custom Tab. | |
| 26 */ | |
| 27 public class WebappInterceptNavigationDelegate extends InterceptNavigationDelega teImpl { | |
| 28 private final WebappActivity mActivity; | |
| 29 private CustomTabsClient mClient; | |
| 30 | |
| 31 public WebappInterceptNavigationDelegate(WebappActivity activity, Tab tab) { | |
| 32 super(tab); | |
| 33 this.mActivity = activity; | |
| 34 | |
| 35 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.
| |
| 36 @Override | |
| 37 public void onCustomTabsServiceConnected( | |
| 38 ComponentName componentName, CustomTabsClient customTabsClie nt) { | |
| 39 mClient = customTabsClient; | |
| 40 } | |
| 41 | |
| 42 @Override | |
| 43 public void onServiceDisconnected(ComponentName componentName) { | |
| 44 mClient = null; | |
| 45 } | |
| 46 }; | |
| 47 | |
| 48 CustomTabsClient.bindCustomTabsService(mActivity, mActivity.getPackageNa me(), connection); | |
| 49 } | |
| 50 | |
| 51 @Nullable | |
| 52 private CustomTabsSession getSession() { | |
| 53 if (mClient == null) { | |
| 54 return null; | |
| 55 } | |
| 56 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.
| |
| 57 @Override | |
| 58 public void onNavigationEvent(int navigationEvent, Bundle extras) { | |
| 59 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.
| |
| 60 } | |
| 61 }); | |
| 62 } | |
| 63 | |
| 64 @Override | |
| 65 public boolean shouldIgnoreNavigation(NavigationParams navigationParams) { | |
| 66 if (super.shouldIgnoreNavigation(navigationParams)) { | |
| 67 return true; | |
| 68 } | |
| 69 | |
| 70 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.
| |
| 71 && !UrlUtilities.sameDomainOrHost( | |
| 72 mActivity.mWebappInfo.uri().toString(), navigationPar ams.url, true)) { | |
| 73 CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builde r(getSession()); | |
| 74 if (mActivity.mWebappInfo.hasValidThemeColor()) { | |
| 75 // Need to cast as themeColor is a long to contain possible erro r results. | |
| 76 intentBuilder.setToolbarColor((int) mActivity.mWebappInfo.themeC olor()); | |
| 77 } | |
| 78 intentBuilder.build().launchUrl(mActivity, Uri.parse(navigationParam s.url)); | |
| 79 return true; | |
| 80 } | |
| 81 | |
| 82 return false; | |
| 83 } | |
| 84 | |
| 85 private static boolean isHttpOrHttps(String url) { | |
| 86 return url.startsWith(UrlConstants.HTTP_URL_PREFIX) | |
| 87 || url.startsWith(UrlConstants.HTTPS_URL_PREFIX); | |
| 88 } | |
| 89 } | |
| OLD | NEW |