OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.chrome.browser.webapps; | |
6 | |
7 import android.content.pm.PackageInfo; | |
8 import android.content.pm.PackageManager; | |
9 import android.graphics.Bitmap; | |
10 | |
11 import org.chromium.base.ContextUtils; | |
12 import org.chromium.base.annotations.CalledByNative; | |
13 import org.chromium.chrome.browser.webapps.WebappRegistry.FetchWebappDataStorage Callback; | |
14 import org.chromium.webapk.lib.common.WebApkConstants; | |
15 | |
16 import java.util.concurrent.TimeUnit; | |
17 | |
18 /** | |
19 * WebApkUpdateManager manages when to check the Web Manifest updates of the Web APK, and | |
20 * sends request to the WebAPK Minting Server when a WebAPK needs update. | |
21 */ | |
22 public class WebApkUpdateManager { | |
23 /** | |
24 * The names of <meta-data> in the WebAPK's AndroidManifest.xml, must stay i n sync with | |
25 * {@linkorg.chromium.webapk.lib.runtime_library.HostBrowserLauncher}. | |
26 */ | |
27 private static final String META_DATA_WEB_MANIFEST_URL = | |
28 "org.chromium.webapk.shell_apk.webManifestUrl"; | |
29 | |
30 /** Represents a period of 3 days in milliseconds */ | |
31 static final long FULL_CHECK_UPDATE_DURATION = TimeUnit.DAYS.toMillis(3L); | |
32 | |
33 /** Represents a period of 12 hours in milliseconds */ | |
34 static final long RETRY_UPDATE_DURATION = TimeUnit.HOURS.toMillis(12L); | |
35 | |
36 /** | |
37 * Check Web Manifest updates if we haven't checked updates for at least | |
38 * {@link FULL_CHECK_UPDATE_DURATION}. | |
39 * If a request to update the WebAPK has been sent to the WebAPK Minting Ser ver recently | |
40 * {@link RETRY_UPDATE_DURATION} but failed, resends the update request to t he server. | |
41 * @param webApkId The ID of the WebAPK. | |
42 * @param detector The ManfiestUpgradeDetector owned by the WebAPK. | |
43 */ | |
44 public static void checkUpdate(String webApkId, final ManifestUpgradeDetecto r detector) { | |
45 WebappRegistry.getWebappDataStorage(ContextUtils.getApplicationContext() , webApkId, | |
46 new WebappRegistry.FetchWebappDataStorageCallback() { | |
47 @Override | |
48 public void onWebappDataStorageRetrieved(WebappDataStorage s torage) { | |
49 if (storage == null) { | |
50 checkUpdate(storage, detector); | |
51 return; | |
52 } | |
53 | |
54 long sinceLastCheckDuration = System.currentTimeMillis() | |
55 - storage.getLastCheckForWebManifestUpdateTime() ; | |
56 long sinceLastRequestDuration = System.currentTimeMillis () | |
57 - storage.getLastRequestForNewShellApkVersionTim e(); | |
58 if (sinceLastCheckDuration > FULL_CHECK_UPDATE_DURATION | |
59 || sinceLastRequestDuration > RETRY_UPDATE_DURAT ION | |
60 && !storage.getDoesLastRequestForNewShellApkVers ionSucceed()) { | |
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
| |
61 checkUpdate(storage, detector); | |
62 } | |
63 } | |
64 }); | |
65 } | |
66 | |
67 private static void checkUpdate(WebappDataStorage storage, ManifestUpgradeDe tector detector) { | |
68 storage.updateLastCheckForWebManifestUpdateTime(); | |
69 detector.start(); | |
70 } | |
71 | |
72 /** | |
73 * Sends request to WebAPK Minting Server to update WebAPK. | |
74 * @param webappInfo The new fetched Web Manifest data of the WebAPK. | |
75 */ | |
76 public static void updateAsync(WebappInfo webappInfo) { | |
77 int version = getVersionFromMetaData(webappInfo.webApkPackageName()); | |
78 nativeUpdateAsync(webappInfo.webApkPackageName(), webappInfo.uri().toStr ing(), | |
79 webappInfo.scopeUri().toString(), webappInfo.name(), webappInfo. shortName(), | |
80 webappInfo.displayMode(), webappInfo.orientation(), webappInfo.t hemeColor(), | |
81 webappInfo.backgroundColor(), webappInfo.webManifestUri().toStri ng(), "", | |
82 webappInfo.icon(), version); | |
83 } | |
84 | |
85 private static int getVersionFromMetaData(String webApkPackage) { | |
86 try { | |
87 PackageManager packageManager = | |
88 ContextUtils.getApplicationContext().getPackageManager(); | |
89 PackageInfo info = packageManager.getPackageInfo(webApkPackage, 0); | |
90 return info.versionCode; | |
91 } catch (PackageManager.NameNotFoundException e) { | |
92 e.printStackTrace(); | |
93 } | |
94 return 1; | |
95 } | |
96 | |
97 /** | |
98 * Called after the either a request to update the WebAPK has been sent or t he creation | |
99 * process of a new WebAPK fails. | |
100 */ | |
101 @CalledByNative | |
102 public static void onBuiltWebApk(final boolean success, String webapkPackage ) { | |
103 WebappRegistry.getWebappDataStorage(ContextUtils.getApplicationContext() , | |
104 WebApkConstants.WEBAPK_ID_PREFIX + webapkPackage, | |
105 new FetchWebappDataStorageCallback() { | |
106 @Override | |
107 public void onWebappDataStorageRetrieved(WebappDataStorage s torage) { | |
108 // Update the request time and result together. It preve nts getting a | |
109 // correct request time but a result from the previous r equest. | |
110 storage.updateLastRequestForNewShellApkVersionTime(); | |
111 storage.updateDoesLastRequestForNewShellApkVersionSuccee d(success); | |
112 } | |
113 }); | |
114 } | |
115 | |
116 private static native void nativeUpdateAsync(String webApkPackage, String ur l, String scope, | |
117 String name, String shortName, int displayMode, int orientation, lon g themeColor, | |
118 long backgroundColor, String manifestUrl, String iconUrl, Bitmap ico n, int version); | |
119 } | |
OLD | NEW |