OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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.webapps; | 5 package org.chromium.chrome.browser.webapps; |
6 | 6 |
7 import android.content.Intent; | 7 import android.content.Intent; |
8 import android.content.pm.ApplicationInfo; | 8 import android.content.pm.ApplicationInfo; |
9 import android.content.pm.PackageManager; | 9 import android.content.pm.PackageManager; |
10 import android.content.res.Resources; | 10 import android.content.res.Resources; |
(...skipping 13 matching lines...) Expand all Loading... | |
24 | 24 |
25 import java.util.HashMap; | 25 import java.util.HashMap; |
26 import java.util.Map; | 26 import java.util.Map; |
27 | 27 |
28 /** | 28 /** |
29 * Stores info for WebAPK. | 29 * Stores info for WebAPK. |
30 */ | 30 */ |
31 public class WebApkInfo extends WebappInfo { | 31 public class WebApkInfo extends WebappInfo { |
32 private static final String TAG = "WebApkInfo"; | 32 private static final String TAG = "WebApkInfo"; |
33 | 33 |
34 private boolean mShouldNavigateIfAlreadyRunning; | |
dominickn
2017/03/21 02:09:33
I would call this mForceNavigation;
| |
34 private String mWebApkPackageName; | 35 private String mWebApkPackageName; |
35 private int mShellApkVersion; | 36 private int mShellApkVersion; |
36 private String mManifestUrl; | 37 private String mManifestUrl; |
37 private String mManifestStartUrl; | 38 private String mManifestStartUrl; |
38 private Map<String, String> mIconUrlToMurmur2HashMap; | 39 private Map<String, String> mIconUrlToMurmur2HashMap; |
39 | 40 |
40 public static WebApkInfo createEmpty() { | 41 public static WebApkInfo createEmpty() { |
41 return new WebApkInfo(); | 42 return new WebApkInfo(); |
42 } | 43 } |
43 | 44 |
44 /** | 45 /** |
45 * Constructs a WebApkInfo from the passed in Intent and <meta-data> in the WebAPK's Android | 46 * Constructs a WebApkInfo from the passed in Intent and <meta-data> in the WebAPK's Android |
46 * manifest. | 47 * manifest. |
47 * @param intent Intent containing info about the app. | 48 * @param intent Intent containing info about the app. |
48 */ | 49 */ |
49 public static WebApkInfo create(Intent intent) { | 50 public static WebApkInfo create(Intent intent) { |
50 String webApkPackageName = | 51 String webApkPackageName = |
51 IntentUtils.safeGetStringExtra(intent, WebApkConstants.EXTRA_WEB APK_PACKAGE_NAME); | 52 IntentUtils.safeGetStringExtra(intent, WebApkConstants.EXTRA_WEB APK_PACKAGE_NAME); |
52 if (TextUtils.isEmpty(webApkPackageName)) { | 53 if (TextUtils.isEmpty(webApkPackageName)) { |
53 return null; | 54 return null; |
54 } | 55 } |
55 | 56 |
56 String url = urlFromIntent(intent); | 57 String url = urlFromIntent(intent); |
57 int source = sourceFromIntent(intent); | 58 int source = sourceFromIntent(intent); |
58 return create(webApkPackageName, url, source); | 59 boolean navigateIfAlreadyRunning = IntentUtils.safeGetBooleanExtra( |
60 intent, WebApkConstants.EXTRA_WEBAPK_NAVIGATE_IF_ALREADY_OPEN, f alse); | |
61 | |
62 return create(webApkPackageName, url, navigateIfAlreadyRunning, source); | |
59 } | 63 } |
60 | 64 |
61 /** | 65 /** |
62 * Constructs a WebApkInfo from the passed in parameters and <meta-data> in the WebAPK's Android | 66 * Constructs a WebApkInfo from the passed in parameters and <meta-data> in the WebAPK's Android |
63 * manifest. | 67 * manifest. |
64 * | 68 * |
65 * @param webApkPackageName The package name of the WebAPK. | 69 * @param webApkPackageName The package name of the WebAPK. |
66 * @param url Url that the WebAPK should navigate to when launched. | 70 * @param url Url that the WebAPK should navigate to when launched. |
71 * @param navigateIfAlreadyRunning Whether the WebAPK should navigate to {@l ink uri} if it is | |
72 * already running. | |
67 * @param source Source that the WebAPK was launched from. | 73 * @param source Source that the WebAPK was launched from. |
68 */ | 74 */ |
69 public static WebApkInfo create(String webApkPackageName, String url, int so urce) { | 75 public static WebApkInfo create( |
76 String webApkPackageName, String url, boolean navigateIfAlreadyRunni ng, int source) { | |
dominickn
2017/03/21 02:09:33
Nit: I would put the boolean last.
Nit: I would c
| |
70 // Unlike non-WebAPK web apps, WebAPK ids are predictable. A malicious a ctor may send an | 77 // Unlike non-WebAPK web apps, WebAPK ids are predictable. A malicious a ctor may send an |
71 // intent with a valid start URL and arbitrary other data. Only use the start URL, the | 78 // intent with a valid start URL and arbitrary other data. Only use the start URL, the |
72 // package name and the ShortcutSource from the launch intent and extrac t the remaining data | 79 // package name and the ShortcutSource from the launch intent and extrac t the remaining data |
73 // from the <meta-data> in the WebAPK's Android manifest. | 80 // from the <meta-data> in the WebAPK's Android manifest. |
74 | 81 |
75 Bundle bundle = extractWebApkMetaData(webApkPackageName); | 82 Bundle bundle = extractWebApkMetaData(webApkPackageName); |
76 if (bundle == null) { | 83 if (bundle == null) { |
77 return null; | 84 return null; |
78 } | 85 } |
79 | 86 |
(...skipping 13 matching lines...) Expand all Loading... | |
93 int shellApkVersion = | 100 int shellApkVersion = |
94 IntentUtils.safeGetInt(bundle, WebApkMetaDataKeys.SHELL_APK_VERS ION, 0); | 101 IntentUtils.safeGetInt(bundle, WebApkMetaDataKeys.SHELL_APK_VERS ION, 0); |
95 | 102 |
96 String manifestUrl = IntentUtils.safeGetString(bundle, WebApkMetaDataKey s.WEB_MANIFEST_URL); | 103 String manifestUrl = IntentUtils.safeGetString(bundle, WebApkMetaDataKey s.WEB_MANIFEST_URL); |
97 String manifestStartUrl = IntentUtils.safeGetString(bundle, WebApkMetaDa taKeys.START_URL); | 104 String manifestStartUrl = IntentUtils.safeGetString(bundle, WebApkMetaDa taKeys.START_URL); |
98 Map<String, String> iconUrlToMurmur2HashMap = getIconUrlAndIconMurmur2Ha shMap(bundle); | 105 Map<String, String> iconUrlToMurmur2HashMap = getIconUrlAndIconMurmur2Ha shMap(bundle); |
99 | 106 |
100 int iconId = IntentUtils.safeGetInt(bundle, WebApkMetaDataKeys.ICON_ID, 0); | 107 int iconId = IntentUtils.safeGetInt(bundle, WebApkMetaDataKeys.ICON_ID, 0); |
101 Bitmap icon = decodeImageResource(webApkPackageName, iconId); | 108 Bitmap icon = decodeImageResource(webApkPackageName, iconId); |
102 | 109 |
103 return create(WebApkConstants.WEBAPK_ID_PREFIX + webApkPackageName, url, scope, | 110 return create(WebApkConstants.WEBAPK_ID_PREFIX + webApkPackageName, url, |
104 new Icon(icon), name, shortName, displayMode, orientation, sourc e, themeColor, | 111 navigateIfAlreadyRunning, scope, new Icon(icon), name, shortName , displayMode, |
105 backgroundColor, webApkPackageName, shellApkVersion, manifestUrl , manifestStartUrl, | 112 orientation, source, themeColor, backgroundColor, webApkPackageN ame, |
106 iconUrlToMurmur2HashMap); | 113 shellApkVersion, manifestUrl, manifestStartUrl, iconUrlToMurmur2 HashMap); |
107 } | 114 } |
108 | 115 |
109 /** | 116 /** |
110 * Construct a {@link WebApkInfo} instance. | 117 * Construct a {@link WebApkInfo} instance. |
111 * | 118 * |
112 * @param id ID for the WebAPK. | 119 * @param id ID for the WebAPK. |
113 * @param url URL that the WebAPK should navigate to whe n launched. | 120 * @param url URL that the WebAPK should navigate to wh en launched. |
114 * @param scope Scope for the WebAPK. | 121 * @param navigateIfAlreadyRunning Whether the WebAPK should navigate to {@l ink uri} if the |
115 * @param icon Icon to show for the WebAPK. | 122 * WebAPK is already open. |
116 * @param name Name of the WebAPK. | 123 * @param scope Scope for the WebAPK. |
117 * @param shortName The short name of the WebAPK. | 124 * @param icon Icon to show for the WebAPK. |
118 * @param displayMode Display mode of the WebAPK. | 125 * @param name Name of the WebAPK. |
119 * @param orientation Orientation of the WebAPK. | 126 * @param shortName The short name of the WebAPK. |
120 * @param source Source that the WebAPK was launched from. | 127 * @param displayMode Display mode of the WebAPK. |
121 * @param themeColor The theme color of the WebAPK. | 128 * @param orientation Orientation of the WebAPK. |
122 * @param backgroundColor The background color of the WebAPK. | 129 * @param source Source that the WebAPK was launched from. |
123 * @param webApkPackageName The package of the WebAPK. | 130 * @param themeColor The theme color of the WebAPK. |
124 * @param shellApkVersion Version of the code in //chrome/android/we bapk/shell_apk. | 131 * @param backgroundColor The background color of the WebAPK. |
125 * @param manifestUrl URL of the Web Manifest. | 132 * @param webApkPackageName The package of the WebAPK. |
126 * @param manifestStartUrl URL that the WebAPK should navigate to whe n launched from the | 133 * @param shellApkVersion Version of the code in //chrome/android/w ebapk/shell_apk. |
127 * homescreen. Different from the {@link url} parameter if the | 134 * @param manifestUrl URL of the Web Manifest. |
128 * WebAPK is launched from a deep link. | 135 * @param manifestStartUrl URL that the WebAPK should navigate to wh en launched from the |
129 * @param iconUrlToMurmur2HashMap Map of the WebAPK's icon URLs to Murmur2 h ashes of the | 136 * homescreen. Different from the {@link url } parameter if the |
130 * icon untransformed bytes. | 137 * WebAPK is launched from a deep link. |
138 * @param iconUrlToMurmur2HashMap Map of the WebAPK's icon URLs to Murmur2 hashes of the | |
139 * icon untransformed bytes. | |
131 */ | 140 */ |
132 public static WebApkInfo create(String id, String url, String scope, Icon ic on, String name, | 141 public static WebApkInfo create(String id, String url, boolean navigateIfAlr eadyRunning, |
133 String shortName, int displayMode, int orientation, int source, long themeColor, | 142 String scope, Icon icon, String name, String shortName, int displayM ode, |
134 long backgroundColor, String webApkPackageName, int shellApkVersion, String manifestUrl, | 143 int orientation, int source, long themeColor, long backgroundColor, |
144 String webApkPackageName, int shellApkVersion, String manifestUrl, | |
135 String manifestStartUrl, Map<String, String> iconUrlToMurmur2HashMap ) { | 145 String manifestStartUrl, Map<String, String> iconUrlToMurmur2HashMap ) { |
136 if (id == null || url == null || manifestStartUrl == null || webApkPacka geName == null) { | 146 if (id == null || url == null || manifestStartUrl == null || webApkPacka geName == null) { |
137 Log.e(TAG, | 147 Log.e(TAG, |
138 "Incomplete data provided: " + id + ", " + url + ", " + mani festStartUrl + ", " | 148 "Incomplete data provided: " + id + ", " + url + ", " + mani festStartUrl + ", " |
139 + webApkPackageName); | 149 + webApkPackageName); |
140 return null; | 150 return null; |
141 } | 151 } |
142 | 152 |
143 // The default scope should be computed from the Web Manifest start URL. If the WebAPK was | 153 // The default scope should be computed from the Web Manifest start URL. If the WebAPK was |
144 // launched from a deep link {@link startUrl} may be different from the Web Manifest start | 154 // launched from a deep link {@link startUrl} may be different from the Web Manifest start |
145 // URL. | 155 // URL. |
146 if (TextUtils.isEmpty(scope)) { | 156 if (TextUtils.isEmpty(scope)) { |
147 scope = ShortcutHelper.getScopeFromUrl(manifestStartUrl); | 157 scope = ShortcutHelper.getScopeFromUrl(manifestStartUrl); |
148 } | 158 } |
149 | 159 |
150 return new WebApkInfo(id, url, scope, icon, name, shortName, displayMode , | 160 return new WebApkInfo(id, url, navigateIfAlreadyRunning, scope, icon, na me, shortName, |
151 orientation, source, themeColor, backgroundColor, webApkPackageN ame, | 161 displayMode, orientation, source, themeColor, backgroundColor, w ebApkPackageName, |
152 shellApkVersion, manifestUrl, manifestStartUrl, iconUrlToMurmur2 HashMap); | 162 shellApkVersion, manifestUrl, manifestStartUrl, iconUrlToMurmur2 HashMap); |
153 } | 163 } |
154 | 164 |
155 protected WebApkInfo(String id, String url, String scope, Icon icon, String name, | 165 protected WebApkInfo(String id, String url, boolean navigateIfAlreadyRunning , String scope, |
156 String shortName, int displayMode, int orientation, int source, long themeColor, | 166 Icon icon, String name, String shortName, int displayMode, int orien tation, int source, |
157 long backgroundColor, String webApkPackageName, int shellApkVersion, String manifestUrl, | 167 long themeColor, long backgroundColor, String webApkPackageName, int shellApkVersion, |
158 String manifestStartUrl, Map<String, String> iconUrlToMurmur2HashMap ) { | 168 String manifestUrl, String manifestStartUrl, |
169 Map<String, String> iconUrlToMurmur2HashMap) { | |
159 super(id, url, scope, icon, name, shortName, displayMode, orientation, s ource, themeColor, | 170 super(id, url, scope, icon, name, shortName, displayMode, orientation, s ource, themeColor, |
160 backgroundColor, false); | 171 backgroundColor, false); |
172 mShouldNavigateIfAlreadyRunning = navigateIfAlreadyRunning; | |
161 mWebApkPackageName = webApkPackageName; | 173 mWebApkPackageName = webApkPackageName; |
162 mShellApkVersion = shellApkVersion; | 174 mShellApkVersion = shellApkVersion; |
163 mManifestUrl = manifestUrl; | 175 mManifestUrl = manifestUrl; |
164 mManifestStartUrl = manifestStartUrl; | 176 mManifestStartUrl = manifestStartUrl; |
165 mIconUrlToMurmur2HashMap = iconUrlToMurmur2HashMap; | 177 mIconUrlToMurmur2HashMap = iconUrlToMurmur2HashMap; |
166 } | 178 } |
167 | 179 |
168 protected WebApkInfo() {} | 180 protected WebApkInfo() {} |
169 | 181 |
170 @Override | 182 @Override |
183 public boolean shouldNavigateIfAlreadyRunning() { | |
dominickn
2017/03/21 02:09:32
shouldForceNavigation()
| |
184 return mShouldNavigateIfAlreadyRunning; | |
185 } | |
186 | |
187 @Override | |
171 public String webApkPackageName() { | 188 public String webApkPackageName() { |
172 return mWebApkPackageName; | 189 return mWebApkPackageName; |
173 } | 190 } |
174 | 191 |
175 public int shellApkVersion() { | 192 public int shellApkVersion() { |
176 return mShellApkVersion; | 193 return mShellApkVersion; |
177 } | 194 } |
178 | 195 |
179 public String manifestUrl() { | 196 public String manifestUrl() { |
180 return mManifestUrl; | 197 return mManifestUrl; |
181 } | 198 } |
182 | 199 |
183 public String manifestStartUrl() { | 200 public String manifestStartUrl() { |
184 return mManifestStartUrl; | 201 return mManifestStartUrl; |
185 } | 202 } |
186 | 203 |
187 public Map<String, String> iconUrlToMurmur2HashMap() { | 204 public Map<String, String> iconUrlToMurmur2HashMap() { |
188 return mIconUrlToMurmur2HashMap; | 205 return mIconUrlToMurmur2HashMap; |
189 } | 206 } |
190 | 207 |
191 @Override | 208 @Override |
192 public void setWebappIntentExtras(Intent intent) { | 209 public void setWebappIntentExtras(Intent intent) { |
193 // For launching a {@link WebApkActivity}. | 210 // For launching a {@link WebApkActivity}. |
194 intent.putExtra(ShortcutHelper.EXTRA_URL, uri().toString()); | 211 intent.putExtra(ShortcutHelper.EXTRA_URL, uri().toString()); |
195 intent.putExtra(ShortcutHelper.EXTRA_SOURCE, source()); | 212 intent.putExtra(ShortcutHelper.EXTRA_SOURCE, source()); |
196 intent.putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, webApkPackage Name()); | 213 intent.putExtra(WebApkConstants.EXTRA_WEBAPK_PACKAGE_NAME, webApkPackage Name()); |
214 intent.putExtra(WebApkConstants.EXTRA_WEBAPK_NAVIGATE_IF_ALREADY_OPEN, | |
215 mShouldNavigateIfAlreadyRunning); | |
197 } | 216 } |
198 | 217 |
199 /** | 218 /** |
200 * Extracts meta data from a WebAPK's Android Manifest. | 219 * Extracts meta data from a WebAPK's Android Manifest. |
201 * @param webApkPackageName WebAPK's package name. | 220 * @param webApkPackageName WebAPK's package name. |
202 * @return Bundle with the extracted meta data. | 221 * @return Bundle with the extracted meta data. |
203 */ | 222 */ |
204 private static Bundle extractWebApkMetaData(String webApkPackageName) { | 223 private static Bundle extractWebApkMetaData(String webApkPackageName) { |
205 PackageManager packageManager = ContextUtils.getApplicationContext().get PackageManager(); | 224 PackageManager packageManager = ContextUtils.getApplicationContext().get PackageManager(); |
206 try { | 225 try { |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
350 return ScreenOrientationValues.PORTRAIT; | 369 return ScreenOrientationValues.PORTRAIT; |
351 } else if (orientation.equals("portrait-primary")) { | 370 } else if (orientation.equals("portrait-primary")) { |
352 return ScreenOrientationValues.PORTRAIT_PRIMARY; | 371 return ScreenOrientationValues.PORTRAIT_PRIMARY; |
353 } else if (orientation.equals("portrait-secondary")) { | 372 } else if (orientation.equals("portrait-secondary")) { |
354 return ScreenOrientationValues.PORTRAIT_SECONDARY; | 373 return ScreenOrientationValues.PORTRAIT_SECONDARY; |
355 } else { | 374 } else { |
356 return ScreenOrientationValues.DEFAULT; | 375 return ScreenOrientationValues.DEFAULT; |
357 } | 376 } |
358 } | 377 } |
359 } | 378 } |
OLD | NEW |