| 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 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 forceNavigation = IntentUtils.safeGetBooleanExtra( |
| 60 intent, WebApkConstants.EXTRA_WEBAPK_FORCE_NAVIGATION, false); |
| 61 |
| 62 return create(webApkPackageName, url, source, forceNavigation); |
| 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. |
| 67 * @param source Source that the WebAPK was launched from. | 71 * @param source Source that the WebAPK was launched from. |
| 72 * @param forceNavigation Whether the WebAPK should navigate to {@link url}
if it is already |
| 73 * running. |
| 68 */ | 74 */ |
| 69 public static WebApkInfo create(String webApkPackageName, String url, int so
urce) { | 75 public static WebApkInfo create( |
| 76 String webApkPackageName, String url, int source, boolean forceNavig
ation) { |
| 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,
forceNavigation, |
| 104 new Icon(icon), name, shortName, displayMode, orientation, sourc
e, themeColor, | 111 scope, new Icon(icon), name, shortName, displayMode, orientation
, source, |
| 105 backgroundColor, webApkPackageName, shellApkVersion, manifestUrl
, manifestStartUrl, | 112 themeColor, backgroundColor, webApkPackageName, shellApkVersion,
manifestUrl, |
| 106 iconUrlToMurmur2HashMap); | 113 manifestStartUrl, iconUrlToMurmur2HashMap); |
| 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 whe
n launched. |
| 121 * @param forceNavigation Whether the WebAPK should navigate to {@li
nk url} if the |
| 122 * WebAPK is already open. |
| 114 * @param scope Scope for the WebAPK. | 123 * @param scope Scope for the WebAPK. |
| 115 * @param icon Icon to show for the WebAPK. | 124 * @param icon Icon to show for the WebAPK. |
| 116 * @param name Name of the WebAPK. | 125 * @param name Name of the WebAPK. |
| 117 * @param shortName The short name of the WebAPK. | 126 * @param shortName The short name of the WebAPK. |
| 118 * @param displayMode Display mode of the WebAPK. | 127 * @param displayMode Display mode of the WebAPK. |
| 119 * @param orientation Orientation of the WebAPK. | 128 * @param orientation Orientation of the WebAPK. |
| 120 * @param source Source that the WebAPK was launched from. | 129 * @param source Source that the WebAPK was launched from. |
| 121 * @param themeColor The theme color of the WebAPK. | 130 * @param themeColor The theme color of the WebAPK. |
| 122 * @param backgroundColor The background color of the WebAPK. | 131 * @param backgroundColor The background color of the WebAPK. |
| 123 * @param webApkPackageName The package of the WebAPK. | 132 * @param webApkPackageName The package of the WebAPK. |
| 124 * @param shellApkVersion Version of the code in //chrome/android/we
bapk/shell_apk. | 133 * @param shellApkVersion Version of the code in //chrome/android/we
bapk/shell_apk. |
| 125 * @param manifestUrl URL of the Web Manifest. | 134 * @param manifestUrl URL of the Web Manifest. |
| 126 * @param manifestStartUrl URL that the WebAPK should navigate to whe
n launched from the | 135 * @param manifestStartUrl URL that the WebAPK should navigate to whe
n launched from the |
| 127 * homescreen. Different from the {@link url}
parameter if the | 136 * homescreen. Different from the {@link url}
parameter if the |
| 128 * WebAPK is launched from a deep link. | 137 * WebAPK is launched from a deep link. |
| 129 * @param iconUrlToMurmur2HashMap Map of the WebAPK's icon URLs to Murmur2 h
ashes of the | 138 * @param iconUrlToMurmur2HashMap Map of the WebAPK's icon URLs to Murmur2 h
ashes of the |
| 130 * icon untransformed bytes. | 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 forceNavigati
on, String scope, |
| 133 String shortName, int displayMode, int orientation, int source, long
themeColor, | 142 Icon icon, String name, String shortName, int displayMode, int orien
tation, int source, |
| 134 long backgroundColor, String webApkPackageName, int shellApkVersion,
String manifestUrl, | 143 long themeColor, long backgroundColor, String webApkPackageName, int
shellApkVersion, |
| 135 String manifestStartUrl, Map<String, String> iconUrlToMurmur2HashMap
) { | 144 String manifestUrl, String manifestStartUrl, |
| 145 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, forceNavigation, scope, icon, name, short
Name, displayMode, |
| 151 orientation, source, themeColor, backgroundColor, webApkPackageN
ame, | 161 orientation, source, themeColor, backgroundColor, webApkPackageN
ame, |
| 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 forceNavigation, String
scope, Icon icon, |
| 156 String shortName, int displayMode, int orientation, int source, long
themeColor, | 166 String name, String shortName, int displayMode, int orientation, 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 mForceNavigation = forceNavigation; |
| 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 shouldForceNavigation() { |
| 184 return mForceNavigation; |
| 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_FORCE_NAVIGATION, mForceNav
igation); |
| 197 } | 215 } |
| 198 | 216 |
| 199 /** | 217 /** |
| 200 * Extracts meta data from a WebAPK's Android Manifest. | 218 * Extracts meta data from a WebAPK's Android Manifest. |
| 201 * @param webApkPackageName WebAPK's package name. | 219 * @param webApkPackageName WebAPK's package name. |
| 202 * @return Bundle with the extracted meta data. | 220 * @return Bundle with the extracted meta data. |
| 203 */ | 221 */ |
| 204 private static Bundle extractWebApkMetaData(String webApkPackageName) { | 222 private static Bundle extractWebApkMetaData(String webApkPackageName) { |
| 205 PackageManager packageManager = ContextUtils.getApplicationContext().get
PackageManager(); | 223 PackageManager packageManager = ContextUtils.getApplicationContext().get
PackageManager(); |
| 206 try { | 224 try { |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 350 return ScreenOrientationValues.PORTRAIT; | 368 return ScreenOrientationValues.PORTRAIT; |
| 351 } else if (orientation.equals("portrait-primary")) { | 369 } else if (orientation.equals("portrait-primary")) { |
| 352 return ScreenOrientationValues.PORTRAIT_PRIMARY; | 370 return ScreenOrientationValues.PORTRAIT_PRIMARY; |
| 353 } else if (orientation.equals("portrait-secondary")) { | 371 } else if (orientation.equals("portrait-secondary")) { |
| 354 return ScreenOrientationValues.PORTRAIT_SECONDARY; | 372 return ScreenOrientationValues.PORTRAIT_SECONDARY; |
| 355 } else { | 373 } else { |
| 356 return ScreenOrientationValues.DEFAULT; | 374 return ScreenOrientationValues.DEFAULT; |
| 357 } | 375 } |
| 358 } | 376 } |
| 359 } | 377 } |
| OLD | NEW |