Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(364)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/webapps/WebApkUpdateManager.java

Issue 2184913005: Add calls to the server to request WebAPK updates. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase and pkotwicz@'s comments. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..0014cd434d2b1fb52a0a86578a794f95429d5b1a
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebApkUpdateManager.java
@@ -0,0 +1,113 @@
+// 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.
+ */
pkotwicz 2016/08/09 14:35:42 How about: "WebApkUpdateManager manages when to ch
Yaron 2016/08/10 02:26:28 +1
Xi Han 2016/08/11 19:01:13 Acknowledged.
+public class WebApkUpdateManager {
+ /** Represents a period of 3 days in milliseconds */
pkotwicz 2016/08/09 14:35:42 The comment does not provide any information which
Xi Han 2016/08/11 19:01:14 Sounds good, updated.
+ static final long FULL_CHECK_UPDATE_DURATION = TimeUnit.DAYS.toMillis(3L);
+
+ /** Represents a period of 12 hours in milliseconds */
pkotwicz 2016/08/09 14:35:42 The comment does not provide any information which
Xi Han 2016/08/11 19:01:13 Done.
+ 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.
pkotwicz 2016/08/09 14:35:42 The specific times (FULL_CHECK_UPDATE_DURATION, RE
Xi Han 2016/08/11 19:01:14 Done.
+ * @param webApkId The ID of the WebAPK.
+ * @param detector The ManfiestUpgradeDetector owned by the WebAPK.
pkotwicz 2016/08/09 14:35:42 Nit ManfiestUpgradeDetector -> ManifestUpgradeDete
Xi Han 2016/08/11 19:01:14 Done.
+ */
+ public static void checkUpdate(String webApkId, final ManifestUpgradeDetector detector) {
pkotwicz 2016/08/09 14:35:41 Can you please add checks w.r.t to which thread th
Yaron 2016/08/10 02:26:28 I find this method name overloading confusing. Als
Yaron 2016/08/10 02:26:28 +1
Xi Han 2016/08/11 19:01:14 Done.
+ WebappRegistry.getWebappDataStorage(ContextUtils.getApplicationContext(), webApkId,
+ new WebappRegistry.FetchWebappDataStorageCallback() {
+ @Override
+ public void onWebappDataStorageRetrieved(WebappDataStorage storage) {
+ if (storage == null) {
Yaron 2016/08/10 02:26:28 you dereference storage in the next function which
Xi Han 2016/08/11 19:01:13 We repopulate the storage during the launching pro
+ 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())) {
+ checkUpdate(storage, detector);
+ }
+ }
+ });
+ }
+
+ private static void checkUpdate(WebappDataStorage storage, ManifestUpgradeDetector detector) {
pkotwicz 2016/08/09 14:35:42 My understanding is that this function is run on t
Yaron 2016/08/10 02:26:28 yes. also please rename updateCheckFinished
Xi Han 2016/08/11 19:01:14 Add the assert. Rename it to startCheckUpdate, sin
+ storage.updateLastCheckForWebManifestUpdateTime();
pkotwicz 2016/08/09 14:35:42 We call WebappDataStorage#updateLastCheckForWebMan
Xi Han 2016/08/11 19:01:14 Your concern is right. Filed a bug 636525.
+ detector.start();
+ }
+
+ /**
+ * Sends request to WebAPK Minting Server to update WebAPK.
pkotwicz 2016/08/09 14:35:42 "WebAPK Minting Server" -> "WebAPK server"
Xi Han 2016/08/11 19:01:14 Done.
+ * @param webappInfo The new fetched Web Manifest data of the WebAPK.
+ */
+ public static void updateAsync(WebappInfo webappInfo) {
+ int webApkVersion = getVersionFromMetaData(webappInfo.webApkPackageName());
+ nativeUpdateAsync(webappInfo.uri().toString(), webappInfo.scopeUri().toString(),
+ webappInfo.name(), webappInfo.shortName(), "", webappInfo.icon(),
+ webappInfo.displayMode(), webappInfo.orientation(), webappInfo.themeColor(),
+ webappInfo.backgroundColor(), webappInfo.webManifestUri().toString(),
+ webappInfo.webApkPackageName(), webApkVersion);
+ }
+
+ 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.
+ */
pkotwicz 2016/08/09 14:35:42 Nit: "the creation process of a new WebAPK fails."
Xi Han 2016/08/11 19:01:14 Done.
+ @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 url, String scope, String name,
pkotwicz 2016/08/09 14:35:41 Nit: |url| -> |startUrl|
Xi Han 2016/08/11 19:01:14 Done.
+ String shortName, String iconUrl, Bitmap icon, int displayMode, int orientation,
+ long themeColor, long backgroundColor, String manifestUrl, String webApkPackage,
+ int webApkVersion);
+}

Powered by Google App Engine
This is Rietveld 408576698