| 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.ContentResolver; | 7 import android.content.ContentResolver; |
| 8 import android.content.pm.PackageInfo; | 8 import android.content.pm.PackageInfo; |
| 9 import android.content.pm.PackageManager; | 9 import android.content.pm.PackageManager; |
| 10 import android.graphics.Bitmap; | 10 import android.graphics.Bitmap; |
| 11 import android.provider.Settings; | 11 import android.provider.Settings; |
| 12 | 12 |
| 13 import org.chromium.base.CommandLine; | 13 import org.chromium.base.CommandLine; |
| 14 import org.chromium.base.ContextUtils; | 14 import org.chromium.base.ContextUtils; |
| 15 import org.chromium.base.Log; | 15 import org.chromium.base.Log; |
| 16 import org.chromium.base.annotations.CalledByNative; | 16 import org.chromium.base.annotations.CalledByNative; |
| 17 import org.chromium.chrome.browser.ChromeSwitches; | 17 import org.chromium.chrome.browser.ChromeSwitches; |
| 18 import org.chromium.chrome.browser.tab.Tab; | 18 import org.chromium.chrome.browser.tab.Tab; |
| 19 import org.chromium.chrome.browser.webapps.ManifestUpgradeDetectorFetcher.Fetche
dManifestData; |
| 19 import org.chromium.webapk.lib.client.WebApkVersion; | 20 import org.chromium.webapk.lib.client.WebApkVersion; |
| 20 | 21 |
| 22 import java.util.Set; |
| 21 import java.util.concurrent.TimeUnit; | 23 import java.util.concurrent.TimeUnit; |
| 22 | 24 |
| 23 /** | 25 /** |
| 24 * WebApkUpdateManager manages when to check for updates to the WebAPK's Web Man
ifest, and sends | 26 * WebApkUpdateManager manages when to check for updates to the WebAPK's Web Man
ifest, and sends |
| 25 * an update request to the WebAPK Server when an update is needed. | 27 * an update request to the WebAPK Server when an update is needed. |
| 26 */ | 28 */ |
| 27 public class WebApkUpdateManager implements ManifestUpgradeDetector.Callback { | 29 public class WebApkUpdateManager implements ManifestUpgradeDetector.Callback { |
| 28 private static final String TAG = "WebApkUpdateManager"; | 30 private static final String TAG = "WebApkUpdateManager"; |
| 29 | 31 |
| 30 /** Number of milliseconds between checks for whether the WebAPK's Web Manif
est has changed. */ | 32 /** Number of milliseconds between checks for whether the WebAPK's Web Manif
est has changed. */ |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 77 mUpgradeDetector = buildManifestUpgradeDetector(tab, mMetaData); | 79 mUpgradeDetector = buildManifestUpgradeDetector(tab, mMetaData); |
| 78 mUpgradeDetector.start(); | 80 mUpgradeDetector.start(); |
| 79 } | 81 } |
| 80 | 82 |
| 81 public void destroy() { | 83 public void destroy() { |
| 82 destroyUpgradeDetector(); | 84 destroyUpgradeDetector(); |
| 83 } | 85 } |
| 84 | 86 |
| 85 @Override | 87 @Override |
| 86 public void onFinishedFetchingWebManifestForInitialUrl( | 88 public void onFinishedFetchingWebManifestForInitialUrl( |
| 87 boolean needsUpgrade, ManifestUpgradeDetector.FetchedManifestData da
ta) { | 89 boolean needsUpgrade, FetchedManifestData data) { |
| 88 onGotManifestData(needsUpgrade, data); | 90 onGotManifestData(needsUpgrade, data); |
| 89 } | 91 } |
| 90 | 92 |
| 91 @Override | 93 @Override |
| 92 public void onGotManifestData( | 94 public void onGotManifestData(boolean needsUpgrade, FetchedManifestData data
) { |
| 93 boolean needsUpgrade, ManifestUpgradeDetector.FetchedManifestData da
ta) { | |
| 94 WebappDataStorage storage = WebappRegistry.getInstance().getWebappDataSt
orage(mId); | 95 WebappDataStorage storage = WebappRegistry.getInstance().getWebappDataSt
orage(mId); |
| 95 storage.updateTimeOfLastCheckForUpdatedWebManifest(); | 96 storage.updateTimeOfLastCheckForUpdatedWebManifest(); |
| 96 | 97 |
| 97 boolean gotManifest = (data != null); | 98 boolean gotManifest = (data != null); |
| 98 needsUpgrade |= isShellApkVersionOutOfDate(mMetaData); | 99 needsUpgrade |= isShellApkVersionOutOfDate(mMetaData); |
| 99 Log.v(TAG, "Got Manifest: " + gotManifest); | 100 Log.v(TAG, "Got Manifest: " + gotManifest); |
| 100 Log.v(TAG, "WebAPK upgrade needed: " + needsUpgrade); | 101 Log.v(TAG, "WebAPK upgrade needed: " + needsUpgrade); |
| 101 | 102 |
| 102 // If the Web Manifest was not found and an upgrade is requested, stop f
etching Web | 103 // If the Web Manifest was not found and an upgrade is requested, stop f
etching Web |
| 103 // Manifests as the user navigates to avoid sending multiple WebAPK upda
te requests. In | 104 // Manifests as the user navigates to avoid sending multiple WebAPK upda
te requests. In |
| (...skipping 16 matching lines...) Expand all Loading... |
| 120 recordUpdate(storage, true); | 121 recordUpdate(storage, true); |
| 121 } | 122 } |
| 122 return; | 123 return; |
| 123 } | 124 } |
| 124 | 125 |
| 125 // Set WebAPK update as having failed in case that Chrome is killed prio
r to | 126 // Set WebAPK update as having failed in case that Chrome is killed prio
r to |
| 126 // {@link onBuiltWebApk} being called. | 127 // {@link onBuiltWebApk} being called. |
| 127 recordUpdate(storage, false); | 128 recordUpdate(storage, false); |
| 128 | 129 |
| 129 if (data != null) { | 130 if (data != null) { |
| 130 updateAsync(data.startUrl, data.scopeUrl, data.name, data.shortName,
data.iconUrl, | 131 updateAsync(data.startUrl, data.scopeUrl, data.name, data.shortName,
data.bestIconUrl, |
| 131 data.iconMurmur2Hash, data.icon, data.displayMode, data.orie
ntation, | 132 data.bestIconMurmur2Hash, data.bestIcon, data.iconUrls, data
.displayMode, |
| 132 data.themeColor, data.backgroundColor); | 133 data.orientation, data.themeColor, data.backgroundColor); |
| 133 return; | 134 return; |
| 134 } | 135 } |
| 135 | 136 |
| 136 updateAsyncUsingAndroidManifestMetaData(); | 137 updateAsyncUsingAndroidManifestMetaData(); |
| 137 } | 138 } |
| 138 | 139 |
| 139 /** | 140 /** |
| 140 * Builds {@link ManifestUpgradeDetector}. In a separate function for the sa
ke of tests. | 141 * Builds {@link ManifestUpgradeDetector}. In a separate function for the sa
ke of tests. |
| 141 */ | 142 */ |
| 142 protected ManifestUpgradeDetector buildManifestUpgradeDetector( | 143 protected ManifestUpgradeDetector buildManifestUpgradeDetector( |
| 143 Tab tab, WebApkMetaData metaData) { | 144 Tab tab, WebApkMetaData metaData) { |
| 144 return new ManifestUpgradeDetector(tab, metaData, this); | 145 return new ManifestUpgradeDetector(tab, metaData, this); |
| 145 } | 146 } |
| 146 | 147 |
| 147 /** | 148 /** |
| 148 * Sends a request to WebAPK Server to update WebAPK using the meta data fro
m the WebAPK's | 149 * Sends a request to WebAPK Server to update WebAPK using the meta data fro
m the WebAPK's |
| 149 * Android Manifest. | 150 * Android Manifest. |
| 150 */ | 151 */ |
| 151 private void updateAsyncUsingAndroidManifestMetaData() { | 152 private void updateAsyncUsingAndroidManifestMetaData() { |
| 153 // If the Web Manifest isn't available anymore, it is impossible to know
which is the best |
| 154 // icon URL. In this case, pass an empty URL to tell the server that the
re isn't a best |
| 155 // icon URL. |
| 156 Set<String> iconUrlsSet = mMetaData.iconUrlAndIconMurmur2HashMap.keySet(
); |
| 157 String[] iconUrls = new String[iconUrlsSet.size()]; |
| 152 updateAsync(mMetaData.startUrl, mMetaData.scope, mMetaData.name, mMetaDa
ta.shortName, | 158 updateAsync(mMetaData.startUrl, mMetaData.scope, mMetaData.name, mMetaDa
ta.shortName, |
| 153 mMetaData.iconUrl, mMetaData.iconMurmur2Hash, mIcon, mMetaData.d
isplayMode, | 159 "", "", mIcon, iconUrlsSet.toArray(iconUrls), mMetaData.displayM
ode, |
| 154 mMetaData.orientation, mMetaData.themeColor, mMetaData.backgroun
dColor); | 160 mMetaData.orientation, mMetaData.themeColor, mMetaData.backgroun
dColor); |
| 155 } | 161 } |
| 156 | 162 |
| 157 /** | 163 /** |
| 158 * Sends request to WebAPK Server to update WebAPK. | 164 * Sends request to WebAPK Server to update WebAPK. |
| 159 */ | 165 */ |
| 160 protected void updateAsync(String startUrl, String scopeUrl, String name, St
ring shortName, | 166 protected void updateAsync(String startUrl, String scopeUrl, String name, St
ring shortName, |
| 161 String iconUrl, String iconMurmur2Hash, Bitmap icon, int displayMode
, int orientation, | 167 String bestIconUrl, String bestIconMurmur2Hash, Bitmap bestIcon, Str
ing[] iconUrls, |
| 162 long themeColor, long backgroundColor) { | 168 int displayMode, int orientation, long themeColor, long backgroundCo
lor) { |
| 163 int versionCode = readVersionCodeFromAndroidManifest(mWebApkPackageName)
; | 169 int versionCode = readVersionCodeFromAndroidManifest(mWebApkPackageName)
; |
| 164 nativeUpdateAsync(mId, startUrl, scopeUrl, name, shortName, iconUrl, ico
nMurmur2Hash, icon, | 170 nativeUpdateAsync(mId, startUrl, scopeUrl, name, shortName, bestIconUrl, |
| 165 displayMode, orientation, themeColor, backgroundColor, mMetaData
.manifestUrl, | 171 bestIconMurmur2Hash, bestIcon, iconUrls, displayMode, orientatio
n, themeColor, |
| 166 mWebApkPackageName, versionCode); | 172 backgroundColor, mMetaData.manifestUrl, mWebApkPackageName, vers
ionCode); |
| 167 } | 173 } |
| 168 | 174 |
| 169 /** | 175 /** |
| 170 * Destroys {@link mUpgradeDetector}. In a separate function for the sake of
tests. | 176 * Destroys {@link mUpgradeDetector}. In a separate function for the sake of
tests. |
| 171 */ | 177 */ |
| 172 protected void destroyUpgradeDetector() { | 178 protected void destroyUpgradeDetector() { |
| 173 if (mUpgradeDetector == null) return; | 179 if (mUpgradeDetector == null) return; |
| 174 | 180 |
| 175 mUpgradeDetector.destroy(); | 181 mUpgradeDetector.destroy(); |
| 176 mUpgradeDetector = null; | 182 mUpgradeDetector = null; |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 * Called after either a request to update the WebAPK has been sent or the u
pdate process | 287 * Called after either a request to update the WebAPK has been sent or the u
pdate process |
| 282 * fails. | 288 * fails. |
| 283 */ | 289 */ |
| 284 @CalledByNative | 290 @CalledByNative |
| 285 private static void onBuiltWebApk(String id, boolean success) { | 291 private static void onBuiltWebApk(String id, boolean success) { |
| 286 WebappDataStorage storage = WebappRegistry.getInstance().getWebappDataSt
orage(id); | 292 WebappDataStorage storage = WebappRegistry.getInstance().getWebappDataSt
orage(id); |
| 287 recordUpdate(storage, success); | 293 recordUpdate(storage, success); |
| 288 } | 294 } |
| 289 | 295 |
| 290 private static native void nativeUpdateAsync(String id, String startUrl, Str
ing scope, | 296 private static native void nativeUpdateAsync(String id, String startUrl, Str
ing scope, |
| 291 String name, String shortName, String iconUrl, String iconMurmur2Has
h, Bitmap icon, | 297 String name, String shortName, String bestIconUrl, String bestIconMu
rmur2Hash, |
| 292 int displayMode, int orientation, long themeColor, long backgroundCo
lor, | 298 Bitmap bestIcon, String[] iconUrls, int displayMode, int orientation
, long themeColor, |
| 293 String manifestUrl, String webApkPackage, int webApkVersion); | 299 long backgroundColor, String manifestUrl, String webApkPackage, int
webApkVersion); |
| 294 } | 300 } |
| OLD | NEW |