OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 package org.chromium.chrome.browser.webapps; | 4 package org.chromium.chrome.browser.webapps; |
5 | 5 |
6 import android.content.Intent; | |
7 import android.content.pm.ResolveInfo; | |
6 import android.net.Uri; | 8 import android.net.Uri; |
7 import android.support.customtabs.CustomTabsIntent; | 9 import android.support.customtabs.CustomTabsIntent; |
8 | 10 |
11 import org.chromium.base.Log; | |
12 import org.chromium.chrome.browser.IntentHandler; | |
13 import org.chromium.chrome.browser.customtabs.CustomTabIntentDataProvider; | |
14 import org.chromium.chrome.browser.externalnav.ExternalNavigationDelegateImpl; | |
9 import org.chromium.chrome.browser.tab.Tab; | 15 import org.chromium.chrome.browser.tab.Tab; |
10 import org.chromium.chrome.browser.tab.TabIdManager; | 16 import org.chromium.chrome.browser.tab.TabIdManager; |
11 import org.chromium.chrome.browser.tabmodel.AsyncTabParamsManager; | 17 import org.chromium.chrome.browser.tabmodel.AsyncTabParamsManager; |
12 import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType; | 18 import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType; |
13 import org.chromium.chrome.browser.tabmodel.document.AsyncTabCreationParams; | 19 import org.chromium.chrome.browser.tabmodel.document.AsyncTabCreationParams; |
14 import org.chromium.chrome.browser.tabmodel.document.TabDelegate; | 20 import org.chromium.chrome.browser.tabmodel.document.TabDelegate; |
15 | 21 |
22 import java.net.URISyntaxException; | |
23 import java.util.List; | |
24 | |
16 /** | 25 /** |
17 * Asynchronously creates Tabs for navigation originating from an installed PWA. | 26 * Asynchronously creates Tabs for navigation originating from an installed PWA. |
18 * | 27 * |
19 * This is the same as the parent class with exception of opening a Custom Tab f or | 28 * This is the same as the parent class with exception of opening a Custom Tab f or |
20 * {@code _blank} links and {@code window.open(url)} calls instead of creating a new tab in Chrome. | 29 * {@code _blank} links and {@code window.open(url)} calls instead of creating a new tab in Chrome. |
21 */ | 30 */ |
22 public class WebappTabDelegate extends TabDelegate { | 31 public class WebappTabDelegate extends TabDelegate { |
32 private static final String TAG = "WebappTabDelegate"; | |
33 | |
23 private final WebappActivity mActivity; | 34 private final WebappActivity mActivity; |
35 private ExternalNavigationDelegateImpl mExternalNavigationDelegate; // Initi alized lazily | |
24 | 36 |
25 public WebappTabDelegate(WebappActivity activity, boolean incognito) { | 37 public WebappTabDelegate(WebappActivity activity, boolean incognito) { |
26 super(incognito); | 38 super(incognito); |
27 this.mActivity = activity; | 39 this.mActivity = activity; |
28 } | 40 } |
29 | 41 |
30 @Override | 42 @Override |
31 public void createNewTab(AsyncTabCreationParams asyncParams, TabLaunchType t ype, int parentId) { | 43 public void createNewTab(AsyncTabCreationParams asyncParams, TabLaunchType t ype, int parentId) { |
44 String url = asyncParams.getLoadUrlParams().getUrl(); | |
45 if (maybeStartExternalActivity(url)) { | |
46 return; | |
47 } | |
48 | |
32 int assignedTabId = TabIdManager.getInstance().generateValidId(Tab.INVAL ID_TAB_ID); | 49 int assignedTabId = TabIdManager.getInstance().generateValidId(Tab.INVAL ID_TAB_ID); |
33 AsyncTabParamsManager.add(assignedTabId, asyncParams); | 50 AsyncTabParamsManager.add(assignedTabId, asyncParams); |
34 | 51 |
35 CustomTabsIntent customTabIntent = | 52 Intent intent = new CustomTabsIntent.Builder().setShowTitle(true).build( ).intent; |
36 new CustomTabsIntent.Builder().setShowTitle(true).build(); | 53 intent.setData(Uri.parse(url)); |
54 intent.putExtra(CustomTabIntentDataProvider.EXTRA_SEND_TO_EXTERNAL_DEFAU LT_HANDLER, true); | |
55 intent.putExtra(CustomTabIntentDataProvider.EXTRA_IS_OPENED_BY_CHROME, t rue); | |
56 addAsyncTabExtras(asyncParams, parentId, false /* isChromeUI */, assigne dTabId, intent); | |
37 | 57 |
38 customTabIntent.intent.setPackage(mActivity.getPackageName()); | 58 IntentHandler.startActivityForTrustedIntent(intent); |
39 addAsyncTabExtras(asyncParams, parentId, true, assignedTabId, customTabI ntent.intent); | 59 } |
40 customTabIntent.launchUrl(mActivity, Uri.parse(asyncParams.getLoadUrlPar ams().getUrl())); | 60 |
61 private boolean maybeStartExternalActivity(String url) { | |
62 Intent intent; | |
63 try { | |
64 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); | |
65 } catch (URISyntaxException ex) { | |
66 Log.w(TAG, "Bad URI %s", url, ex); | |
67 return false; | |
68 } | |
69 | |
70 if (shouldStartExternalActivity(intent)) { | |
71 mActivity.startActivity(intent); | |
Yusuf
2017/07/19 03:33:37
I think dependence on ExternalNavigationDelegate h
piotrs
2017/07/19 04:06:25
I found isPackageSpecializedHandler, which is what
| |
72 return true; | |
73 } | |
74 | |
75 return false; | |
76 } | |
77 | |
78 private boolean shouldStartExternalActivity(Intent intent) { | |
79 if (intent.getPackage() != null) return true; | |
Yusuf
2017/07/19 03:33:37
is this needed? Explicit intents should be covered
piotrs
2017/07/19 04:06:25
Ack, removed.
| |
80 | |
81 if (mExternalNavigationDelegate == null) { | |
82 mExternalNavigationDelegate = | |
83 new ExternalNavigationDelegateImpl(mActivity.getActivityTab( )); | |
84 } | |
85 | |
86 List<ResolveInfo> infos = mExternalNavigationDelegate.queryIntentActivit ies(intent); | |
87 return mExternalNavigationDelegate.isSpecializedHandlerAvailable(infos); | |
41 } | 88 } |
42 } | 89 } |
OLD | NEW |