OLD | NEW |
---|---|
(Empty) | |
1 // 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.
| |
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 mConnection = new CustomTabsServiceConnectio n() { | |
dominickn
2017/05/01 00:41:41
Nit: the m prefix is just for member variables.
piotrs
2017/05/01 02:55:06
Done.
| |
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(), mConnection); | |
49 } | |
50 | |
51 @Nullable | |
52 private CustomTabsSession getSession() { | |
53 if (mClient == null) { | |
54 return null; | |
55 } | |
56 return mClient.newSession(new CustomTabsCallback() { | |
57 @Override | |
58 public void onNavigationEvent(int navigationEvent, Bundle extras) { | |
59 super.onNavigationEvent(navigationEvent, extras); | |
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) | |
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 |