Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/webapps/WebApkUpdateManager.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebApkUpdateManager.java b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebApkUpdateManager.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b8d3ebe622369cb13c4e6ce01cf6b6934219a402 |
| --- /dev/null |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebApkUpdateManager.java |
| @@ -0,0 +1,119 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.chrome.browser.webapps; |
| + |
| +import android.content.pm.PackageInfo; |
| +import android.content.pm.PackageManager; |
| +import android.graphics.Bitmap; |
| + |
| +import org.chromium.base.ContextUtils; |
| +import org.chromium.base.annotations.CalledByNative; |
| +import org.chromium.chrome.browser.webapps.WebappRegistry.FetchWebappDataStorageCallback; |
| +import org.chromium.webapk.lib.common.WebApkConstants; |
| + |
| +import java.util.concurrent.TimeUnit; |
| + |
| +/** |
| + * WebApkUpdateManager manages when to check the Web Manifest updates of the WebAPK, and |
| + * sends request to the WebAPK Minting Server when a WebAPK needs update. |
| + */ |
| +public class WebApkUpdateManager { |
| + /** |
| + * The names of <meta-data> in the WebAPK's AndroidManifest.xml, must stay in sync with |
| + * {@linkorg.chromium.webapk.lib.runtime_library.HostBrowserLauncher}. |
| + */ |
| + private static final String META_DATA_WEB_MANIFEST_URL = |
| + "org.chromium.webapk.shell_apk.webManifestUrl"; |
| + |
| + /** Represents a period of 3 days in milliseconds */ |
| + static final long FULL_CHECK_UPDATE_DURATION = TimeUnit.DAYS.toMillis(3L); |
| + |
| + /** Represents a period of 12 hours in milliseconds */ |
| + static final long RETRY_UPDATE_DURATION = TimeUnit.HOURS.toMillis(12L); |
| + |
| + /** |
| + * Check Web Manifest updates if we haven't checked updates for at least |
| + * {@link FULL_CHECK_UPDATE_DURATION}. |
| + * If a request to update the WebAPK has been sent to the WebAPK Minting Server recently |
| + * {@link RETRY_UPDATE_DURATION} but failed, resends the update request to the server. |
| + * @param webApkId The ID of the WebAPK. |
| + * @param detector The ManfiestUpgradeDetector owned by the WebAPK. |
| + */ |
| + public static void checkUpdate(String webApkId, final ManifestUpgradeDetector detector) { |
| + WebappRegistry.getWebappDataStorage(ContextUtils.getApplicationContext(), webApkId, |
| + new WebappRegistry.FetchWebappDataStorageCallback() { |
| + @Override |
| + public void onWebappDataStorageRetrieved(WebappDataStorage storage) { |
| + if (storage == null) { |
| + checkUpdate(storage, detector); |
| + return; |
| + } |
| + |
| + long sinceLastCheckDuration = System.currentTimeMillis() |
| + - storage.getLastCheckForWebManifestUpdateTime(); |
| + long sinceLastRequestDuration = System.currentTimeMillis() |
| + - storage.getLastRequestForNewShellApkVersionTime(); |
| + if (sinceLastCheckDuration > FULL_CHECK_UPDATE_DURATION |
| + || sinceLastRequestDuration > RETRY_UPDATE_DURATION |
| + && !storage.getDoesLastRequestForNewShellApkVersionSucceed()) { |
|
pkotwicz
2016/08/08 18:59:15
Sorry for the confusion. I was suggesting changing
Xi Han
2016/08/08 21:25:05
As discussed offline, we will keep these two times
|
| + checkUpdate(storage, detector); |
| + } |
| + } |
| + }); |
| + } |
| + |
| + private static void checkUpdate(WebappDataStorage storage, ManifestUpgradeDetector detector) { |
| + storage.updateLastCheckForWebManifestUpdateTime(); |
| + detector.start(); |
| + } |
| + |
| + /** |
| + * Sends request to WebAPK Minting Server to update WebAPK. |
| + * @param webappInfo The new fetched Web Manifest data of the WebAPK. |
| + */ |
| + public static void updateAsync(WebappInfo webappInfo) { |
| + int version = getVersionFromMetaData(webappInfo.webApkPackageName()); |
| + nativeUpdateAsync(webappInfo.webApkPackageName(), webappInfo.uri().toString(), |
| + webappInfo.scopeUri().toString(), webappInfo.name(), webappInfo.shortName(), |
| + webappInfo.displayMode(), webappInfo.orientation(), webappInfo.themeColor(), |
| + webappInfo.backgroundColor(), webappInfo.webManifestUri().toString(), "", |
| + webappInfo.icon(), version); |
| + } |
| + |
| + private static int getVersionFromMetaData(String webApkPackage) { |
| + try { |
| + PackageManager packageManager = |
| + ContextUtils.getApplicationContext().getPackageManager(); |
| + PackageInfo info = packageManager.getPackageInfo(webApkPackage, 0); |
| + return info.versionCode; |
| + } catch (PackageManager.NameNotFoundException e) { |
| + e.printStackTrace(); |
| + } |
| + return 1; |
| + } |
| + |
| + /** |
| + * Called after the either a request to update the WebAPK has been sent or the creation |
| + * process of a new WebAPK fails. |
| + */ |
| + @CalledByNative |
| + public static void onBuiltWebApk(final boolean success, String webapkPackage) { |
| + WebappRegistry.getWebappDataStorage(ContextUtils.getApplicationContext(), |
| + WebApkConstants.WEBAPK_ID_PREFIX + webapkPackage, |
| + new FetchWebappDataStorageCallback() { |
| + @Override |
| + public void onWebappDataStorageRetrieved(WebappDataStorage storage) { |
| + // Update the request time and result together. It prevents getting a |
| + // correct request time but a result from the previous request. |
| + storage.updateLastRequestForNewShellApkVersionTime(); |
| + storage.updateDoesLastRequestForNewShellApkVersionSucceed(success); |
| + } |
| + }); |
| + } |
| + |
| + private static native void nativeUpdateAsync(String webApkPackage, String url, String scope, |
| + String name, String shortName, int displayMode, int orientation, long themeColor, |
| + long backgroundColor, String manifestUrl, String iconUrl, Bitmap icon, int version); |
| +} |