Index: chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappTabDelegate.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappTabDelegate.java b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappTabDelegate.java |
index e3e1e9e9a2c74c14e48d61cf70a12c6cf87e00c1..a72f94914973504be65175396cf5acfbebbf382a 100644 |
--- a/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappTabDelegate.java |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappTabDelegate.java |
@@ -3,9 +3,16 @@ |
// found in the LICENSE file. |
package org.chromium.chrome.browser.webapps; |
+import android.content.Intent; |
import android.net.Uri; |
+import android.os.StrictMode; |
import android.support.customtabs.CustomTabsIntent; |
+import org.chromium.base.ContextUtils; |
+import org.chromium.base.Log; |
+import org.chromium.chrome.browser.IntentHandler; |
+import org.chromium.chrome.browser.customtabs.CustomTabIntentDataProvider; |
+import org.chromium.chrome.browser.externalnav.ExternalNavigationDelegateImpl; |
import org.chromium.chrome.browser.tab.Tab; |
import org.chromium.chrome.browser.tab.TabIdManager; |
import org.chromium.chrome.browser.tabmodel.AsyncTabParamsManager; |
@@ -13,30 +20,62 @@ import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType; |
import org.chromium.chrome.browser.tabmodel.document.AsyncTabCreationParams; |
import org.chromium.chrome.browser.tabmodel.document.TabDelegate; |
+import java.net.URISyntaxException; |
+ |
/** |
* Asynchronously creates Tabs for navigation originating from an installed PWA. |
* |
- * This is the same as the parent class with exception of opening a Custom Tab for |
- * {@code _blank} links and {@code window.open(url)} calls instead of creating a new tab in Chrome. |
+ * This is the same as the parent class with exception of checking for a specialized native handlers |
+ * first, and if none are found opening a Custom Tab instead of creating a new tab in Chrome. |
*/ |
public class WebappTabDelegate extends TabDelegate { |
- private final WebappActivity mActivity; |
+ private static final String TAG = "WebappTabDelegate"; |
- public WebappTabDelegate(WebappActivity activity, boolean incognito) { |
+ public WebappTabDelegate(boolean incognito) { |
super(incognito); |
- this.mActivity = activity; |
} |
@Override |
public void createNewTab(AsyncTabCreationParams asyncParams, TabLaunchType type, int parentId) { |
+ String url = asyncParams.getLoadUrlParams().getUrl(); |
+ if (maybeStartExternalActivity(url)) { |
+ return; |
+ } |
+ |
int assignedTabId = TabIdManager.getInstance().generateValidId(Tab.INVALID_TAB_ID); |
AsyncTabParamsManager.add(assignedTabId, asyncParams); |
- CustomTabsIntent customTabIntent = |
- new CustomTabsIntent.Builder().setShowTitle(true).build(); |
+ Intent intent = new CustomTabsIntent.Builder().setShowTitle(true).build().intent; |
+ intent.setData(Uri.parse(url)); |
+ intent.putExtra(CustomTabIntentDataProvider.EXTRA_SEND_TO_EXTERNAL_DEFAULT_HANDLER, true); |
+ intent.putExtra(CustomTabIntentDataProvider.EXTRA_IS_OPENED_BY_CHROME, true); |
+ addAsyncTabExtras(asyncParams, parentId, false /* isChromeUI */, assignedTabId, intent); |
+ |
+ IntentHandler.startActivityForTrustedIntent(intent); |
+ } |
+ |
+ private boolean maybeStartExternalActivity(String url) { |
+ Intent intent; |
+ try { |
+ intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME); |
+ } catch (URISyntaxException ex) { |
+ Log.w(TAG, "Bad URI %s", url, ex); |
+ return false; |
+ } |
+ |
+ // See http://crbug.com/613977 for more context. |
+ StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites(); |
+ try { |
+ // Launch a native app iff there is a specialized handler for a given URL. |
+ if (ExternalNavigationDelegateImpl.isPackageSpecializedHandler(null, intent)) { |
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
+ ContextUtils.getApplicationContext().startActivity(intent); |
+ return true; |
+ } |
+ } finally { |
+ StrictMode.setThreadPolicy(oldPolicy); |
+ } |
- customTabIntent.intent.setPackage(mActivity.getPackageName()); |
- addAsyncTabExtras(asyncParams, parentId, true, assignedTabId, customTabIntent.intent); |
- customTabIntent.launchUrl(mActivity, Uri.parse(asyncParams.getLoadUrlParams().getUrl())); |
+ return false; |
} |
} |