Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(488)

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/ServiceTabLauncher.java

Issue 2974573002: Refactor WebApkServiceConnectionManager. (Closed)
Patch Set: Call the callback on UI thread if bindService() fails. Created 3 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 4
5 package org.chromium.chrome.browser; 5 package org.chromium.chrome.browser;
6 6
7 import android.app.Activity; 7 import android.app.Activity;
8 import android.content.Context;
8 import android.content.Intent; 9 import android.content.Intent;
9 import android.net.Uri; 10 import android.net.Uri;
10 import android.os.AsyncTask; 11 import android.os.AsyncTask;
11 import android.provider.Browser; 12 import android.provider.Browser;
12 import android.support.annotation.Nullable; 13 import android.support.annotation.Nullable;
13 import android.support.customtabs.CustomTabsIntent; 14 import android.support.customtabs.CustomTabsIntent;
14 15
15 import org.chromium.base.ApplicationStatus; 16 import org.chromium.base.ApplicationStatus;
16 import org.chromium.base.ContextUtils; 17 import org.chromium.base.ContextUtils;
17 import org.chromium.base.ThreadUtils; 18 import org.chromium.base.ThreadUtils;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 * @param requestId Id of the request for launching this tab. 53 * @param requestId Id of the request for launching this tab.
53 * @param incognito Whether the tab should be launched in incognito mod e. 54 * @param incognito Whether the tab should be launched in incognito mod e.
54 * @param url The URL which should be launched in a tab. 55 * @param url The URL which should be launched in a tab.
55 * @param disposition The disposition requested by the navigation source. 56 * @param disposition The disposition requested by the navigation source.
56 * @param referrerUrl URL of the referrer which is opening the page. 57 * @param referrerUrl URL of the referrer which is opening the page.
57 * @param referrerPolicy The referrer policy to consider when applying the r eferrer. 58 * @param referrerPolicy The referrer policy to consider when applying the r eferrer.
58 * @param extraHeaders Extra headers to apply when requesting the tab's UR L. 59 * @param extraHeaders Extra headers to apply when requesting the tab's UR L.
59 * @param postData Post-data to include in the tab URL's request body. 60 * @param postData Post-data to include in the tab URL's request body.
60 */ 61 */
61 @CalledByNative 62 @CalledByNative
62 public static void launchTab(final int requestId, final boolean incognito, f inal String url, 63 public static void launchTab(final int requestId, boolean incognito, String url,
63 final int disposition, final String referrerUrl, final int referrerP olicy, 64 int disposition, String referrerUrl, int referrerPolicy, String extr aHeaders,
64 final String extraHeaders, final ResourceRequestBody postData) { 65 ResourceRequestBody postData) {
65 // Open popup window in custom tab. 66 // Open popup window in custom tab.
66 // Note that this is used by PaymentRequestEvent.openWindow(). 67 // Note that this is used by PaymentRequestEvent.openWindow().
67 if (disposition == WindowOpenDisposition.NEW_POPUP) { 68 if (disposition == WindowOpenDisposition.NEW_POPUP) {
68 if (!createPopupCustomTab(requestId, url, incognito)) { 69 if (!createPopupCustomTab(requestId, url, incognito)) {
69 ThreadUtils.postOnUiThread(new Runnable() { 70 ThreadUtils.postOnUiThread(new Runnable() {
70 @Override 71 @Override
71 public void run() { 72 public void run() {
72 onWebContentsForRequestAvailable(requestId, null); 73 onWebContentsForRequestAvailable(requestId, null);
73 } 74 }
74 }); 75 });
75 } 76 }
76 return; 77 return;
77 } 78 }
78 79
79 final TabDelegate tabDelegate = new TabDelegate(incognito);
80
81 // 1. Launch WebAPK if one matches the target URL.
82 String webApkPackageName = 80 String webApkPackageName =
83 WebApkValidator.queryWebApkPackage(ContextUtils.getApplicationCo ntext(), url); 81 WebApkValidator.queryWebApkPackage(ContextUtils.getApplicationCo ntext(), url);
84 if (webApkPackageName != null) { 82 if (webApkPackageName == null) {
85 Intent intent = WebApkNavigationClient.createLaunchWebApkIntent( 83 launchTabOrWebapp(
86 webApkPackageName, url, true /* forceNavigation */); 84 requestId, incognito, url, referrerUrl, referrerPolicy, extr aHeaders, postData);
87 if (intent != null) {
88 intent.putExtra(ShortcutHelper.EXTRA_SOURCE, ShortcutSource.NOTI FICATION);
89 ContextUtils.getApplicationContext().startActivity(intent);
90 }
91 return; 85 return;
92 } 86 }
93 87
94 // 2. Launch WebappActivity if one matches the target URL and was opened recently. 88 tryLaunchWebApk(webApkPackageName, requestId, incognito, url, referrerUr l, referrerPolicy,
89 extraHeaders, postData);
90 }
91
92 /** Launches a WebAPK if it is owned by the Chrome. Otherwise, launches a ta b for the |url|. */
93 private static void tryLaunchWebApk(final String webApkPackageName, final in t requestId,
94 final boolean incognito, final String url, final String referrerUrl,
95 final int referrerPolicy, final String extraHeaders,
96 final ResourceRequestBody postData) {
97 final Context context = ContextUtils.getApplicationContext();
98
99 // Launch WebAPK if one matches the target URL.
100 WebApkValidator.IsOwnedCheckCallback callback = new WebApkValidator.IsOw nedCheckCallback() {
101 @Override
102 public void onIsOwnedChecked(boolean isOwned) {
103 if (isOwned) {
104 Intent intent = WebApkNavigationClient.createLaunchWebApkInt ent(
105 webApkPackageName, url, true /* forceNavigation */);
106 if (intent != null) {
107 intent.putExtra(ShortcutHelper.EXTRA_SOURCE, ShortcutSou rce.NOTIFICATION);
108 context.startActivity(intent);
109 return;
110 }
111 }
112 launchTab(requestId, incognito, url, referrerUrl, referrerPolicy , extraHeaders,
113 postData);
114 }
115 };
116 WebApkValidator.isOwnedWebApkAsync(context, webApkPackageName, callback) ;
117 }
118
119 /** Launches the WebappActivity or a tab for the |url|. */
120 private static void launchTabOrWebapp(int requestId, final boolean incognito , final String url,
121 String referrerUrl, int referrerPolicy, String extraHeaders,
122 ResourceRequestBody postData) {
123 // Launch WebappActivity if one matches the target URL and was opened re cently.
95 // Otherwise, open the URL in a tab. 124 // Otherwise, open the URL in a tab.
96 final WebappDataStorage storage = 125 final WebappDataStorage storage =
97 WebappRegistry.getInstance().getWebappDataStorageForUrl(url); 126 WebappRegistry.getInstance().getWebappDataStorageForUrl(url);
98 127
99 // Open a new tab if: 128 // Open a new tab if:
100 // - We did not find a WebappDataStorage corresponding to this URL. 129 // - We did not find a WebappDataStorage corresponding to this URL.
101 // OR 130 // OR
102 // - The WebappDataStorage hasn't been opened recently enough. 131 // - The WebappDataStorage hasn't been opened recently enough.
103 if (storage == null || !storage.wasUsedRecently()) { 132 if (storage == null || !storage.wasUsedRecently()) {
104 LoadUrlParams loadUrlParams = new LoadUrlParams(url, PageTransition. LINK); 133 launchTab(
105 loadUrlParams.setPostData(postData); 134 requestId, incognito, url, referrerUrl, referrerPolicy, extr aHeaders, postData);
106 loadUrlParams.setVerbatimHeaders(extraHeaders); 135 return;
107 loadUrlParams.setReferrer(new Referrer(referrerUrl, referrerPolicy)) ; 136 }
108 137
109 AsyncTabCreationParams asyncParams = new AsyncTabCreationParams(load UrlParams, 138 // The URL is within the scope of a recently launched standalone-capable web app
110 requestId); 139 // on the home screen, so open it a standalone web app frame. An AsyncTa sk is
111 tabDelegate.createNewTab(asyncParams, TabLaunchType.FROM_CHROME_UI, 140 // used because WebappDataStorage.createWebappLaunchIntent contains a Bi tmap
112 Tab.INVALID_TAB_ID); 141 // decode operation and should not be run on the UI thread.
113 } else { 142 //
114 // The URL is within the scope of a recently launched standalone-cap able web app 143 // This currently assumes that the only source is notifications; any fut ure use
115 // on the home screen, so open it a standalone web app frame. An Asy ncTask is 144 // which adds a different source will need to change this.
116 // used because WebappDataStorage.createWebappLaunchIntent contains a Bitmap 145 new AsyncTask<Void, Void, Intent>() {
117 // decode operation and should not be run on the UI thread. 146 @Override
118 // 147 protected final Intent doInBackground(Void... nothing) {
119 // This currently assumes that the only source is notifications; any future use 148 return storage.createWebappLaunchIntent();
120 // which adds a different source will need to change this. 149 }
121 new AsyncTask<Void, Void, Intent>() {
122 @Override
123 protected final Intent doInBackground(Void... nothing) {
124 return storage.createWebappLaunchIntent();
125 }
126 150
127 @Override 151 @Override
128 protected final void onPostExecute(Intent intent) { 152 protected final void onPostExecute(Intent intent) {
129 // Replace the web app URL with the URL from the notificatio n. This is 153 // Replace the web app URL with the URL from the notification. T his is
130 // within the webapp's scope, so it is valid. 154 // within the webapp's scope, so it is valid.
131 intent.putExtra(ShortcutHelper.EXTRA_URL, url); 155 intent.putExtra(ShortcutHelper.EXTRA_URL, url);
132 intent.putExtra(ShortcutHelper.EXTRA_SOURCE, 156 intent.putExtra(ShortcutHelper.EXTRA_SOURCE, ShortcutSource.NOTI FICATION);
133 ShortcutSource.NOTIFICATION); 157 TabDelegate tabDelegate = new TabDelegate(incognito);
134 tabDelegate.createNewStandaloneFrame(intent); 158 tabDelegate.createNewStandaloneFrame(intent);
135 } 159 }
136 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); 160 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
137 } 161 }
162
163 /** Launches a tab for the |url|. */
164 private static void launchTab(int requestId, boolean incognito, String url, String referrerUrl,
165 int referrerPolicy, String extraHeaders, ResourceRequestBody postDat a) {
166 TabDelegate tabDelegate = new TabDelegate(incognito);
167
168 LoadUrlParams loadUrlParams = new LoadUrlParams(url, PageTransition.LINK );
169 loadUrlParams.setPostData(postData);
170 loadUrlParams.setVerbatimHeaders(extraHeaders);
171 loadUrlParams.setReferrer(new Referrer(referrerUrl, referrerPolicy));
172
173 AsyncTabCreationParams asyncParams = new AsyncTabCreationParams(loadUrlP arams, requestId);
174 tabDelegate.createNewTab(asyncParams, TabLaunchType.FROM_CHROME_UI, Tab. INVALID_TAB_ID);
138 } 175 }
139 176
140 /** 177 /**
141 * Creates a popup custom tab to open the url. The popup tab is animated in from bottom to top 178 * Creates a popup custom tab to open the url. The popup tab is animated in from bottom to top
142 * and out from top to bottom. 179 * and out from top to bottom.
143 * Note that this is used by PaymentRequestEvent.openWindow(). 180 * Note that this is used by PaymentRequestEvent.openWindow().
144 * 181 *
145 * @param requestId The tab launch request ID from the {@link ServiceTabLa uncher}. 182 * @param requestId The tab launch request ID from the {@link ServiceTabLa uncher}.
146 * @param url The url to open in the new tab. 183 * @param url The url to open in the new tab.
147 */ 184 */
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
180 * @param webContents The WebContents instance associated with this request. 217 * @param webContents The WebContents instance associated with this request.
181 */ 218 */
182 public static void onWebContentsForRequestAvailable( 219 public static void onWebContentsForRequestAvailable(
183 int requestId, @Nullable WebContents webContents) { 220 int requestId, @Nullable WebContents webContents) {
184 nativeOnWebContentsForRequestAvailable(requestId, webContents); 221 nativeOnWebContentsForRequestAvailable(requestId, webContents);
185 } 222 }
186 223
187 private static native void nativeOnWebContentsForRequestAvailable( 224 private static native void nativeOnWebContentsForRequestAvailable(
188 int requestId, WebContents webContents); 225 int requestId, WebContents webContents);
189 } 226 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698