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.SharedPreferences; | 8 import android.content.SharedPreferences; |
9 import android.os.AsyncTask; | 9 import android.os.AsyncTask; |
10 | 10 |
11 import org.chromium.base.VisibleForTesting; | 11 import org.chromium.base.VisibleForTesting; |
12 import org.chromium.base.annotations.CalledByNative; | 12 import org.chromium.base.annotations.CalledByNative; |
13 | 13 |
14 import java.util.Collections; | 14 import java.util.Collections; |
15 import java.util.HashSet; | 15 import java.util.HashSet; |
16 import java.util.Set; | 16 import java.util.Set; |
| 17 import java.util.concurrent.TimeUnit; |
17 | 18 |
18 /** | 19 /** |
19 * Keeps track of web apps which have created a SharedPreference file (through t
he used of the | 20 * Keeps track of web apps which have created a SharedPreference file (through t
he used of the |
20 * WebappDataStorage class) which may need to be cleaned up in the future. | 21 * WebappDataStorage class) which may need to be cleaned up in the future. |
21 * | 22 * |
22 * It is NOT intended to be 100% accurate nor a comprehensive list of all instal
led web apps | 23 * It is NOT intended to be 100% accurate nor a comprehensive list of all instal
led web apps |
23 * because it is impossible to track when the user removes a web app from the Ho
me screen and it | 24 * because it is impossible to track when the user removes a web app from the Ho
me screen and it |
24 * is similarily impossible to track pre-registry era web apps (this case is not
a problem anyway | 25 * is similarily impossible to track pre-registry era web apps (this case is not
a problem anyway |
25 * as these web apps have no external data to cleanup). | 26 * as these web apps have no external data to cleanup). |
26 */ | 27 */ |
27 public class WebappRegistry { | 28 public class WebappRegistry { |
28 | 29 |
29 static final String REGISTRY_FILE_NAME = "webapp_registry"; | 30 static final String REGISTRY_FILE_NAME = "webapp_registry"; |
30 static final String KEY_WEBAPP_SET = "webapp_set"; | 31 static final String KEY_WEBAPP_SET = "webapp_set"; |
| 32 static final String KEY_LAST_CLEANUP = "last_cleanup"; |
| 33 |
| 34 /** Represents a period of 4 weeks in milliseconds */ |
| 35 static final long FULL_CLEANUP_DURATION = TimeUnit.DAYS.toMillis(4L * 7L); |
| 36 |
| 37 /** Represents a period of 13 weeks in milliseconds */ |
| 38 static final long WEBAPP_UNOPENED_CLEANUP_DURATION = TimeUnit.DAYS.toMillis(
13L * 7L); |
31 | 39 |
32 /** | 40 /** |
33 * Called when a retrieval of the stored web apps occurs. | 41 * Called when a retrieval of the stored web apps occurs. |
34 */ | 42 */ |
35 public interface FetchCallback { | 43 public interface FetchCallback { |
36 public void onWebappIdsRetrieved(Set<String> readObject); | 44 public void onWebappIdsRetrieved(Set<String> readObject); |
37 } | 45 } |
38 | 46 |
39 /** | 47 /** |
40 * Registers the existence of a web app and creates the SharedPreference for
it. | 48 * Registers the existence of a web app and creates the SharedPreference for
it. |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 } | 82 } |
75 | 83 |
76 @Override | 84 @Override |
77 protected final void onPostExecute(Set<String> result) { | 85 protected final void onPostExecute(Set<String> result) { |
78 callback.onWebappIdsRetrieved(result); | 86 callback.onWebappIdsRetrieved(result); |
79 } | 87 } |
80 }.execute(); | 88 }.execute(); |
81 } | 89 } |
82 | 90 |
83 /** | 91 /** |
| 92 * Deletes the data for all "old" web apps. i.e. web apps which have not bee
n opened by the user |
| 93 * in the last 3 months. Cleanup is run, at most, once a month. |
| 94 * @param context Context to open the registry with. |
| 95 * @param currentTime The current time which will be checked to decide if th
e task should be run |
| 96 * and if a web app should be cleaned up. |
| 97 */ |
| 98 static void unregisterOldWebapps(final Context context, final long currentTi
me) { |
| 99 new AsyncTask<Void, Void, Void>() { |
| 100 @Override |
| 101 protected final Void doInBackground(Void... nothing) { |
| 102 SharedPreferences preferences = openSharedPreferences(context); |
| 103 long lastCleanup = preferences.getLong(KEY_LAST_CLEANUP, 0); |
| 104 if ((currentTime - lastCleanup) < FULL_CLEANUP_DURATION) return
null; |
| 105 |
| 106 Set<String> currentWebapps = getRegisteredWebappIds(preferences)
; |
| 107 Set<String> retainedWebapps = new HashSet<String>(currentWebapps
); |
| 108 for (String id : currentWebapps) { |
| 109 long lastUsed = new WebappDataStorage(context, id).getLastUs
edTime(); |
| 110 if ((currentTime - lastUsed) < WEBAPP_UNOPENED_CLEANUP_DURAT
ION) continue; |
| 111 |
| 112 WebappDataStorage.deleteDataForWebapp(context, id); |
| 113 retainedWebapps.remove(id); |
| 114 } |
| 115 |
| 116 preferences.edit() |
| 117 .putLong(KEY_LAST_CLEANUP, currentTime) |
| 118 .putStringSet(KEY_WEBAPP_SET, retainedWebapps) |
| 119 .commit(); |
| 120 return null; |
| 121 } |
| 122 }.execute(); |
| 123 } |
| 124 |
| 125 /** |
84 * Deletes the data of all web apps, as well as the registry tracking the we
b apps. | 126 * Deletes the data of all web apps, as well as the registry tracking the we
b apps. |
85 */ | 127 */ |
86 @VisibleForTesting | 128 @VisibleForTesting |
87 static void unregisterAllWebapps(final Context context, final Runnable callb
ack) { | 129 static void unregisterAllWebapps(final Context context, final Runnable callb
ack) { |
88 new AsyncTask<Void, Void, Void>() { | 130 new AsyncTask<Void, Void, Void>() { |
89 @Override | 131 @Override |
90 protected final Void doInBackground(Void... nothing) { | 132 protected final Void doInBackground(Void... nothing) { |
91 SharedPreferences preferences = openSharedPreferences(context); | 133 SharedPreferences preferences = openSharedPreferences(context); |
92 for (String id : getRegisteredWebappIds(preferences)) { | 134 for (String id : getRegisteredWebappIds(preferences)) { |
93 WebappDataStorage.deleteDataForWebapp(context, id); | 135 WebappDataStorage.deleteDataForWebapp(context, id); |
(...skipping 26 matching lines...) Expand all Loading... |
120 | 162 |
121 private static Set<String> getRegisteredWebappIds(SharedPreferences preferen
ces) { | 163 private static Set<String> getRegisteredWebappIds(SharedPreferences preferen
ces) { |
122 return preferences.getStringSet(KEY_WEBAPP_SET, Collections.<String>empt
ySet()); | 164 return preferences.getStringSet(KEY_WEBAPP_SET, Collections.<String>empt
ySet()); |
123 } | 165 } |
124 | 166 |
125 private WebappRegistry() { | 167 private WebappRegistry() { |
126 } | 168 } |
127 | 169 |
128 private static native void nativeOnWebappsUnregistered(long callbackPointer)
; | 170 private static native void nativeOnWebappsUnregistered(long callbackPointer)
; |
129 } | 171 } |
OLD | NEW |