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 |
(...skipping 10 matching lines...) Expand all Loading... | |
21 * | 21 * |
22 * It is NOT intended to be 100% accurate nor a comprehensive list of all instal led web apps | 22 * 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 | 23 * 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 | 24 * 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). | 25 * as these web apps have no external data to cleanup). |
26 */ | 26 */ |
27 public class WebappRegistry { | 27 public class WebappRegistry { |
28 | 28 |
29 static final String REGISTRY_FILE_NAME = "webapp_registry"; | 29 static final String REGISTRY_FILE_NAME = "webapp_registry"; |
30 static final String KEY_WEBAPP_SET = "webapp_set"; | 30 static final String KEY_WEBAPP_SET = "webapp_set"; |
31 static final String KEY_LAST_CLEANUP = "last_cleanup"; | |
32 | |
33 /** Represnts a period of 4 weeks in milliseconds */ | |
gone
2015/09/24 12:45:09
Represents.
Lalit Maganti
2015/09/24 12:49:25
Done and below too.
| |
34 static final long FULL_CLEANUP_DURATION = 4 * 7 * 24 * 60 * 60 * 1000; | |
35 | |
36 /** Represnts a period of 13 weeks in milliseconds */ | |
37 static final long WEBAPP_UNOPENED_CLEANUP_DURATION = 13 * 7 * 24 * 60 * 60 * 1000; | |
31 | 38 |
32 /** | 39 /** |
33 * Called when a retrieval of the stored web apps occurs. | 40 * Called when a retrieval of the stored web apps occurs. |
34 */ | 41 */ |
35 public interface FetchCallback { | 42 public interface FetchCallback { |
36 public void onWebappIdsRetrieved(Set<String> readObject); | 43 public void onWebappIdsRetrieved(Set<String> readObject); |
37 } | 44 } |
38 | 45 |
39 /** | 46 /** |
40 * Registers the existence of a web app and creates the SharedPreference for it. | 47 * 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 } | 81 } |
75 | 82 |
76 @Override | 83 @Override |
77 protected final void onPostExecute(Set<String> result) { | 84 protected final void onPostExecute(Set<String> result) { |
78 callback.onWebappIdsRetrieved(result); | 85 callback.onWebappIdsRetrieved(result); |
79 } | 86 } |
80 }.execute(); | 87 }.execute(); |
81 } | 88 } |
82 | 89 |
83 /** | 90 /** |
91 * Deletes the data for all "old" web apps. i.e. web apps which have not bee n opened by the user | |
92 * in the last 3 months. The task also checks if it has been run in the past month and if so | |
93 * does not proceed with cleanup. | |
gone
2015/09/24 12:45:09
"Cleanup is run at most once a month"?
Lalit Maganti
2015/09/24 12:49:25
Done.
| |
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 nu ll; | |
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_DURATI ON) { | |
111 WebappDataStorage.deleteDataForWebapp(context, id); | |
112 retainedWebapps.remove(id); | |
113 } | |
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 |