OLD | NEW |
---|---|
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.content.Context; | 7 import android.content.Context; |
8 import android.content.Intent; | |
9 import android.os.AsyncTask; | |
8 | 10 |
9 import org.chromium.chrome.browser.document.DocumentMetricIds; | 11 import org.chromium.chrome.browser.document.DocumentMetricIds; |
10 import org.chromium.chrome.browser.tab.Tab; | 12 import org.chromium.chrome.browser.tab.Tab; |
11 import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType; | 13 import org.chromium.chrome.browser.tabmodel.TabModel.TabLaunchType; |
12 import org.chromium.chrome.browser.tabmodel.document.AsyncTabCreationParams; | 14 import org.chromium.chrome.browser.tabmodel.document.AsyncTabCreationParams; |
13 import org.chromium.chrome.browser.tabmodel.document.TabDelegate; | 15 import org.chromium.chrome.browser.tabmodel.document.TabDelegate; |
16 import org.chromium.chrome.browser.webapps.WebappDataStorage; | |
17 import org.chromium.chrome.browser.webapps.WebappRegistry; | |
18 import org.chromium.chrome.browser.webapps.WebappRegistry.FetchWebappDataStorage Callback; | |
14 import org.chromium.components.service_tab_launcher.ServiceTabLauncher; | 19 import org.chromium.components.service_tab_launcher.ServiceTabLauncher; |
15 import org.chromium.content_public.browser.LoadUrlParams; | 20 import org.chromium.content_public.browser.LoadUrlParams; |
16 import org.chromium.content_public.common.Referrer; | 21 import org.chromium.content_public.common.Referrer; |
17 import org.chromium.ui.base.PageTransition; | 22 import org.chromium.ui.base.PageTransition; |
18 | 23 |
19 /** | 24 /** |
20 * Service Tab Launcher implementation for Chrome. Provides the ability for Andr oid Services | 25 * Service Tab Launcher implementation for Chrome. Provides the ability for Andr oid Services |
21 * running in Chrome to launch tabs, without having access to an activity. | 26 * running in Chrome to launch URLs, without having access to an activity. |
22 * | 27 * |
23 * This class is referred to from the ServiceTabLauncher implementation in Chrom ium using a | 28 * This class is referred to from the ServiceTabLauncher implementation in Chrom ium using a |
24 * meta-data value in the Android manifest file. The ServiceTabLauncher class ha s more | 29 * meta-data value in the Android manifest file. The ServiceTabLauncher class ha s more |
25 * documentation about why this is necessary. | 30 * documentation about why this is necessary. |
26 * | 31 * |
32 * URLs within the scope of a recently launched standalone-capable web app on th e Android home | |
33 * screen are launched in the standalone web app frame. | |
34 * | |
27 * TODO(peter): after upstreaming, merge this with ServiceTabLauncher and remove reflection calls | 35 * TODO(peter): after upstreaming, merge this with ServiceTabLauncher and remove reflection calls |
28 * in ServiceTabLauncher. | 36 * in ServiceTabLauncher. |
29 */ | 37 */ |
30 public class ChromeServiceTabLauncher extends ServiceTabLauncher { | 38 public class ChromeServiceTabLauncher extends ServiceTabLauncher { |
39 | |
31 @Override | 40 @Override |
32 public void launchTab(Context context, int requestId, boolean incognito, Str ing url, | 41 public void launchTab(final Context context, final int requestId, final bool ean incognito, |
33 int disposition, String referrerUrl, int referrerPolic y, | 42 final String url, final int disposition, final String referrerUrl, |
34 String extraHeaders, byte[] postData) { | 43 final int referrerPolicy, final String extraHeaders, final byte[] po stData) { |
35 // TODO(peter): Determine the intent source based on the |disposition| w ith which the | 44 final TabDelegate tabDelegate = new TabDelegate(incognito); |
36 // tab is being launched. Right now this is gated by a check in the nati ve implementation. | |
37 int intentSource = DocumentMetricIds.STARTED_BY_WINDOW_OPEN; | |
38 | 45 |
39 LoadUrlParams loadUrlParams = new LoadUrlParams(url, PageTransition.LINK ); | 46 // Try and retrieve a WebappDataStorage object with scope corresponding to the URL to be |
40 loadUrlParams.setPostData(postData); | 47 // opened. If one is found, and it has been opened recently, create an i ntent to launch the |
41 loadUrlParams.setVerbatimHeaders(extraHeaders); | 48 // URL in a standalone web app frame. Otherwise, open the URL in a tab. |
42 loadUrlParams.setReferrer(new Referrer(referrerUrl, referrerPolicy)); | 49 FetchWebappDataStorageCallback callback = new FetchWebappDataStorageCall back() { |
50 @Override | |
51 public void onWebappDataStorageRetrieved(final WebappDataStorage sto rage) { | |
52 // If we do not find a WebappDataStorage corresponding to this U RL, or if it | |
53 // hasn't been opened recently enough, open the URL in a tab. | |
54 if (storage == null || !storage.wasLaunchedRecently()) { | |
55 // TODO(peter): Determine the intent source based on the |di sposition| | |
56 // with which the tab is being launched. Right now this is g ated by a | |
57 // check in the native implementation. | |
58 int intentSource = DocumentMetricIds.STARTED_BY_WINDOW_OPEN; | |
43 | 59 |
44 AsyncTabCreationParams asyncParams = new AsyncTabCreationParams(loadUrlP arams, requestId); | 60 LoadUrlParams loadUrlParams = new LoadUrlParams(url, |
45 asyncParams.setDocumentStartedBy(intentSource); | 61 PageTransition.LINK); |
62 loadUrlParams.setPostData(postData); | |
63 loadUrlParams.setVerbatimHeaders(extraHeaders); | |
64 loadUrlParams.setReferrer(new Referrer(referrerUrl, referrer Policy)); | |
46 | 65 |
47 TabDelegate tabDelegate = new TabDelegate(incognito); | 66 AsyncTabCreationParams asyncParams = new AsyncTabCreationPar ams( |
48 tabDelegate.createNewTab( | 67 loadUrlParams, requestId); |
49 asyncParams, TabLaunchType.FROM_CHROME_UI, Tab.INVALID_TAB_ID); | 68 asyncParams.setDocumentStartedBy(intentSource); |
69 | |
70 tabDelegate.createNewTab( | |
71 asyncParams, TabLaunchType.FROM_CHROME_UI, Tab.INVAL ID_TAB_ID); | |
72 } else { | |
73 // The URL is within the scope of a recently launched standa lone-capable | |
gone
2016/04/08 01:14:07
nit: reindent all the things
dominickn
2016/04/08 02:58:44
Done.
| |
74 // web app on the home screen, so open it a standalone web a pp frame. An | |
75 // AsyncTask is used because WebappDataStorage.createWebappL aunchIntent | |
76 // contains a Bitmap decode operation and should not be run on the UI | |
77 // thread. | |
78 // | |
79 // This currently assumes that the only source is notificati ons; any | |
80 // future use which adds a different source will need to cha nge this. | |
81 new AsyncTask<Void, Void, Intent>() { | |
82 @Override | |
83 protected final Intent doInBackground(Void... nothing) { | |
84 return storage.createWebappLaunchIntent(); | |
85 } | |
86 | |
87 @Override | |
88 protected final void onPostExecute(Intent intent) { | |
89 intent.putExtra(ShortcutHelper.EXTRA_SOURCE, | |
90 ShortcutSource.NOTIFICATION); | |
91 tabDelegate.createNewStandaloneFrame(intent); | |
92 } | |
93 }.execute(); | |
94 } | |
95 } | |
96 }; | |
97 WebappRegistry.getWebappDataStorageForUrl(context, url, callback); | |
50 } | 98 } |
51 } | 99 } |
OLD | NEW |