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; |
6 import android.net.Uri; | 7 import android.net.Uri; |
| 8 import android.os.StrictMode; |
7 import android.support.customtabs.CustomTabsIntent; | 9 import android.support.customtabs.CustomTabsIntent; |
8 | 10 |
| 11 import org.chromium.base.ContextUtils; |
| 12 import org.chromium.base.Log; |
| 13 import org.chromium.chrome.browser.IntentHandler; |
| 14 import org.chromium.chrome.browser.customtabs.CustomTabIntentDataProvider; |
| 15 import org.chromium.chrome.browser.externalnav.ExternalNavigationDelegateImpl; |
9 import org.chromium.chrome.browser.tab.Tab; | 16 import org.chromium.chrome.browser.tab.Tab; |
10 import org.chromium.chrome.browser.tab.TabIdManager; | 17 import org.chromium.chrome.browser.tab.TabIdManager; |
11 import org.chromium.chrome.browser.tabmodel.AsyncTabParamsManager; | 18 import org.chromium.chrome.browser.tabmodel.AsyncTabParamsManager; |
12 import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType; | 19 import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType; |
13 import org.chromium.chrome.browser.tabmodel.document.AsyncTabCreationParams; | 20 import org.chromium.chrome.browser.tabmodel.document.AsyncTabCreationParams; |
14 import org.chromium.chrome.browser.tabmodel.document.TabDelegate; | 21 import org.chromium.chrome.browser.tabmodel.document.TabDelegate; |
15 | 22 |
| 23 import java.net.URISyntaxException; |
| 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 checking for a special
ized native handlers |
20 * {@code _blank} links and {@code window.open(url)} calls instead of creating a
new tab in Chrome. | 29 * first, and if none are found opening a Custom Tab instead of creating a new t
ab in Chrome. |
21 */ | 30 */ |
22 public class WebappTabDelegate extends TabDelegate { | 31 public class WebappTabDelegate extends TabDelegate { |
23 private final WebappActivity mActivity; | 32 private static final String TAG = "WebappTabDelegate"; |
24 | 33 |
25 public WebappTabDelegate(WebappActivity activity, boolean incognito) { | 34 public WebappTabDelegate(boolean incognito) { |
26 super(incognito); | 35 super(incognito); |
27 this.mActivity = activity; | |
28 } | 36 } |
29 | 37 |
30 @Override | 38 @Override |
31 public void createNewTab(AsyncTabCreationParams asyncParams, TabLaunchType t
ype, int parentId) { | 39 public void createNewTab(AsyncTabCreationParams asyncParams, TabLaunchType t
ype, int parentId) { |
| 40 String url = asyncParams.getLoadUrlParams().getUrl(); |
| 41 if (maybeStartExternalActivity(url)) { |
| 42 return; |
| 43 } |
| 44 |
32 int assignedTabId = TabIdManager.getInstance().generateValidId(Tab.INVAL
ID_TAB_ID); | 45 int assignedTabId = TabIdManager.getInstance().generateValidId(Tab.INVAL
ID_TAB_ID); |
33 AsyncTabParamsManager.add(assignedTabId, asyncParams); | 46 AsyncTabParamsManager.add(assignedTabId, asyncParams); |
34 | 47 |
35 CustomTabsIntent customTabIntent = | 48 Intent intent = new CustomTabsIntent.Builder().setShowTitle(true).build(
).intent; |
36 new CustomTabsIntent.Builder().setShowTitle(true).build(); | 49 intent.setData(Uri.parse(url)); |
| 50 intent.putExtra(CustomTabIntentDataProvider.EXTRA_SEND_TO_EXTERNAL_DEFAU
LT_HANDLER, true); |
| 51 intent.putExtra(CustomTabIntentDataProvider.EXTRA_IS_OPENED_BY_CHROME, t
rue); |
| 52 addAsyncTabExtras(asyncParams, parentId, false /* isChromeUI */, assigne
dTabId, intent); |
37 | 53 |
38 customTabIntent.intent.setPackage(mActivity.getPackageName()); | 54 IntentHandler.startActivityForTrustedIntent(intent); |
39 addAsyncTabExtras(asyncParams, parentId, true, assignedTabId, customTabI
ntent.intent); | 55 } |
40 customTabIntent.launchUrl(mActivity, Uri.parse(asyncParams.getLoadUrlPar
ams().getUrl())); | 56 |
| 57 private boolean maybeStartExternalActivity(String url) { |
| 58 Intent intent; |
| 59 try { |
| 60 intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); |
| 61 } catch (URISyntaxException ex) { |
| 62 Log.w(TAG, "Bad URI %s", url, ex); |
| 63 return false; |
| 64 } |
| 65 |
| 66 // See http://crbug.com/613977 for more context. |
| 67 StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites(); |
| 68 try { |
| 69 // Launch a native app iff there is a specialized handler for a give
n URL. |
| 70 if (ExternalNavigationDelegateImpl.isPackageSpecializedHandler(null,
intent)) { |
| 71 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 72 ContextUtils.getApplicationContext().startActivity(intent); |
| 73 return true; |
| 74 } |
| 75 } finally { |
| 76 StrictMode.setThreadPolicy(oldPolicy); |
| 77 } |
| 78 |
| 79 return false; |
41 } | 80 } |
42 } | 81 } |
OLD | NEW |