OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.Context; | 7 import android.content.Context; |
8 import android.content.Intent; | |
8 import android.content.SharedPreferences; | 9 import android.content.SharedPreferences; |
10 import android.content.pm.PackageInfo; | |
9 import android.content.pm.PackageManager; | 11 import android.content.pm.PackageManager; |
10 import android.content.pm.PackageManager.NameNotFoundException; | 12 import android.content.pm.PackageManager.NameNotFoundException; |
11 import android.os.AsyncTask; | 13 import android.os.AsyncTask; |
12 | 14 |
13 import org.chromium.base.ContextUtils; | 15 import org.chromium.base.ContextUtils; |
14 import org.chromium.base.VisibleForTesting; | 16 import org.chromium.base.VisibleForTesting; |
15 import org.chromium.base.annotations.CalledByNative; | 17 import org.chromium.base.annotations.CalledByNative; |
18 import org.chromium.chrome.browser.ShortcutHelper; | |
16 import org.chromium.chrome.browser.browsing_data.UrlFilter; | 19 import org.chromium.chrome.browser.browsing_data.UrlFilter; |
17 import org.chromium.chrome.browser.browsing_data.UrlFilterBridge; | 20 import org.chromium.chrome.browser.browsing_data.UrlFilterBridge; |
18 | 21 |
19 import java.util.Collections; | 22 import java.util.Collections; |
20 import java.util.HashMap; | 23 import java.util.HashMap; |
21 import java.util.Iterator; | 24 import java.util.Iterator; |
22 import java.util.Set; | 25 import java.util.Set; |
23 import java.util.concurrent.TimeUnit; | 26 import java.util.concurrent.TimeUnit; |
24 | 27 |
25 /** | 28 /** |
26 * Singleton class which tracks web apps backed by a SharedPreferences file (abs tracted by the | 29 * Singleton class which tracks web apps backed by a SharedPreferences file (abs tracted by the |
27 * WebappDataStorage class). This class must be used on the main thread, except when warming | 30 * WebappDataStorage class). This class must be used on the main thread, except when warming |
28 * SharedPreferences. | 31 * SharedPreferences. |
29 * | 32 * |
30 * Aside from web app registration, which is asynchronous as a new SharedPrefere nces file must be | 33 * Aside from web app registration, which is asynchronous as a new SharedPrefere nces file must be |
31 * opened, all methods in this class are synchronous. All web app SharedPreferen ces known to | 34 * opened, all methods in this class are synchronous. All web app SharedPreferen ces known to |
32 * WebappRegistry are pre-warmed on browser startup when creating the singleton WebappRegistry | 35 * WebappRegistry are pre-warmed on browser startup when creating the singleton WebappRegistry |
33 * instance, whilst registering a new web app will automatically cache the new S haredPreferences | 36 * instance, whilst registering a new web app will automatically cache the new S haredPreferences |
34 * after it is created. | 37 * after it is created. |
35 * | 38 * |
36 * This class is not a comprehensive list of installed web apps because it is im possible to know | 39 * This class is not a comprehensive list of installed web apps because it is im possible to know |
37 * when the user removes a web app from the home screen. The WebappDataStorage.w asLaunchedRecently() | 40 * when the user removes a web app from the home screen. The WebappDataStorage.w asLaunchedRecently() |
38 * heuristic attempts to compensate for this. | 41 * heuristic attempts to compensate for this. |
39 */ | 42 */ |
40 public class WebappRegistry { | 43 public class WebappRegistry { |
41 | 44 |
42 static final String REGISTRY_FILE_NAME = "webapp_registry"; | 45 static final String REGISTRY_FILE_NAME = "webapp_registry"; |
43 static final String KEY_WEBAPP_SET = "webapp_set"; | 46 static final String KEY_WEBAPP_SET = "webapp_set"; |
44 static final String KEY_LAST_CLEANUP = "last_cleanup"; | 47 static final String KEY_LAST_CLEANUP = "last_cleanup"; |
48 static final String WEB_APK_PACKAGE_NAME_PREFIX = "org.chromium.webapk"; | |
Xi Han
2017/01/13 15:50:15
You can use WebApkConstants#WEBAPK_PACKAGE_PREFIX
gonzalon
2017/01/13 20:24:39
Done.
| |
45 | 49 |
46 /** Represents a period of 4 weeks in milliseconds */ | 50 /** Represents a period of 4 weeks in milliseconds */ |
47 static final long FULL_CLEANUP_DURATION = TimeUnit.DAYS.toMillis(4L * 7L); | 51 static final long FULL_CLEANUP_DURATION = TimeUnit.DAYS.toMillis(4L * 7L); |
48 | 52 |
49 /** Represents a period of 13 weeks in milliseconds */ | 53 /** Represents a period of 13 weeks in milliseconds */ |
50 static final long WEBAPP_UNOPENED_CLEANUP_DURATION = TimeUnit.DAYS.toMillis( 13L * 7L); | 54 static final long WEBAPP_UNOPENED_CLEANUP_DURATION = TimeUnit.DAYS.toMillis( 13L * 7L); |
51 | 55 |
52 /** Initialization-on-demand holder. This exists for thread-safe lazy initia lization. */ | 56 /** Initialization-on-demand holder. This exists for thread-safe lazy initia lization. */ |
53 private static class Holder { | 57 private static class Holder { |
54 // Not final for testing. | 58 // Not final for testing. |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
161 * Returns the list of web app IDs which are written to SharedPreferences. | 165 * Returns the list of web app IDs which are written to SharedPreferences. |
162 */ | 166 */ |
163 @VisibleForTesting | 167 @VisibleForTesting |
164 public static Set<String> getRegisteredWebappIdsForTesting() { | 168 public static Set<String> getRegisteredWebappIdsForTesting() { |
165 // Wrap with unmodifiableSet to ensure it's never modified. See crbug.co m/568369. | 169 // Wrap with unmodifiableSet to ensure it's never modified. See crbug.co m/568369. |
166 return Collections.unmodifiableSet(openSharedPreferences().getStringSet( | 170 return Collections.unmodifiableSet(openSharedPreferences().getStringSet( |
167 KEY_WEBAPP_SET, Collections.<String>emptySet())); | 171 KEY_WEBAPP_SET, Collections.<String>emptySet())); |
168 } | 172 } |
169 | 173 |
170 /** | 174 /** |
175 * Fetches the information of all Web APKs installed on the device and retur ns them to the | |
176 * caller using a callback. | |
177 * | |
178 * @param webapkInfoCallback Callback to be called with each Web APK found o n the device. | |
179 */ | |
180 @CalledByNative | |
181 static void listWebAPKs(WebapkInfoCallback webapkInfoCallback) { | |
Xi Han
2017/01/13 15:50:15
Usually we put helper functions in ShorcutHelper.
gonzalon
2017/01/13 20:24:38
Sounds good! Sorry, I just didn't know where to pu
| |
182 PackageManager packageManager = ContextUtils.getApplicationContext().get PackageManager(); | |
183 for (PackageInfo packageInfo : packageManager.getInstalledPackages(0)) { | |
184 if (packageInfo.packageName.startsWith(WEB_APK_PACKAGE_NAME_PREFIX)) { | |
pkotwicz
2017/01/13 16:00:54
Perhaps, you can call WebApkValidator#isValidWebAp
gonzalon
2017/01/13 20:24:38
SG, done
| |
185 Intent intent = new Intent(); | |
186 intent.putExtra(ShortcutHelper.EXTRA_WEBAPK_PACKAGE_NAME, packag eInfo.packageName); | |
187 // Any URL is fine, since we're only doing this to get info on t he Web APK. | |
188 intent.putExtra(ShortcutHelper.EXTRA_URL, "https://www.google.co m/"); | |
hartmanng
2017/01/13 15:40:21
I don't understand why a URL is required at all, c
gonzalon
2017/01/13 20:24:38
As Xi mentioned in the other comment, it was an at
| |
189 WebApkInfo webApkInfo = WebApkInfo.create(intent); | |
Xi Han
2017/01/13 15:50:15
I understand you want to reuse the code, but it is
pkotwicz
2017/01/13 16:00:54
Can you create a new version of WebApkInfo#create(
pkotwicz
2017/01/13 16:23:19
I had code elsewhere which looked like what you ar
Xi Han
2017/01/13 17:52:43
That is fine, we can defer it to Dominick:)
| |
190 if (webApkInfo != null) { | |
191 String appName = webApkInfo.shortName(); | |
192 String packageName = webApkInfo.webApkPackageName(); | |
193 int shellApkVersion = webApkInfo.shellApkVersion(); | |
194 int versionCode = packageInfo.versionCode; | |
195 webapkInfoCallback.onWebApkFound( | |
196 appName, packageName, shellApkVersion, versionCode); | |
pkotwicz
2017/01/13 16:23:19
Nit: Inline lines 191 - 194
gonzalon
2017/01/13 20:24:38
Done.
| |
197 } | |
198 } | |
199 } | |
200 } | |
201 | |
202 /** | |
171 * Deletes the data for all "old" web apps, as well as all WebAPKs that have been uninstalled in | 203 * Deletes the data for all "old" web apps, as well as all WebAPKs that have been uninstalled in |
172 * the last month. "Old" web apps have not been opened by the user in the la st 3 months, or have | 204 * the last month. "Old" web apps have not been opened by the user in the la st 3 months, or have |
173 * had their last used time set to 0 by the user clearing their history. Cle anup is run, at | 205 * had their last used time set to 0 by the user clearing their history. Cle anup is run, at |
174 * most, once a month. | 206 * most, once a month. |
175 * @param currentTime The current time which will be checked to decide if th e task should be run | 207 * @param currentTime The current time which will be checked to decide if th e task should be run |
176 * and if a web app should be cleaned up. | 208 * and if a web app should be cleaned up. |
177 */ | 209 */ |
178 public void unregisterOldWebapps(long currentTime) { | 210 public void unregisterOldWebapps(long currentTime) { |
179 if ((currentTime - mPreferences.getLong(KEY_LAST_CLEANUP, 0)) < FULL_CLE ANUP_DURATION) { | 211 if ((currentTime - mPreferences.getLong(KEY_LAST_CLEANUP, 0)) < FULL_CLE ANUP_DURATION) { |
180 return; | 212 return; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
284 } | 316 } |
285 } | 317 } |
286 } else { | 318 } else { |
287 if (webapps.contains(idToInitialize) | 319 if (webapps.contains(idToInitialize) |
288 && (replaceExisting || !mStorages.containsKey(idToInitialize ))) { | 320 && (replaceExisting || !mStorages.containsKey(idToInitialize ))) { |
289 mStorages.put(idToInitialize, WebappDataStorage.open(idToInitial ize)); | 321 mStorages.put(idToInitialize, WebappDataStorage.open(idToInitial ize)); |
290 } | 322 } |
291 } | 323 } |
292 } | 324 } |
293 } | 325 } |
OLD | NEW |