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

Side by Side Diff: chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappRegistry.java

Issue 1359383002: webapps: Add cleanup task when opening up WebappActivity to clean old web apps (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@webapp-cleanup
Patch Set: Fix remaining uissues Created 5 years, 3 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 unified diff | Download patch
OLDNEW
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
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 /** Represents a period of 4 weeks in milliseconds */
34 static final long FULL_CLEANUP_DURATION = 4 * 7 * 24 * 60 * 60 * 1000;
mlamouri (slow - plz ping) 2015/09/24 15:20:04 Could you use http://docs.oracle.com/javase/7/docs
Lalit Maganti 2015/09/24 16:09:38 Done.
35
36 /** Represents a period of 13 weeks in milliseconds */
37 static final long WEBAPP_UNOPENED_CLEANUP_DURATION = 13 * 7 * 24 * 60 * 60 * 1000;
mlamouri (slow - plz ping) 2015/09/24 15:20:04 ditto
Lalit Maganti 2015/09/24 16:09:37 Done.
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
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. Cleanup is run, at most, once a month.
93 * @param context Context to open the registry with.
94 * @param currentTime The current time which will be checked to decide if th e task should be run
95 * and if a web app should be cleaned up.
96 */
97 static void unregisterOldWebapps(final Context context, final long currentTi me) {
98 new AsyncTask<Void, Void, Void>() {
99 @Override
100 protected final Void doInBackground(Void... nothing) {
101 SharedPreferences preferences = openSharedPreferences(context);
102 long lastCleanup = preferences.getLong(KEY_LAST_CLEANUP, 0);
103 if (currentTime - lastCleanup < FULL_CLEANUP_DURATION) return nu ll;
mlamouri (slow - plz ping) 2015/09/24 15:20:04 nit: I would use parenthesis: |if ((currentTime -
Lalit Maganti 2015/09/24 16:09:37 Done.
104
105 Set<String> currentWebapps = getRegisteredWebappIds(preferences) ;
106 Set<String> retainedWebapps = new HashSet<String>(currentWebapps );
107 for (String id : currentWebapps) {
108 long lastUsed = new WebappDataStorage(context, id).getLastUs edTime();
109 if (currentTime - lastUsed < WEBAPP_UNOPENED_CLEANUP_DURATIO N) continue;
mlamouri (slow - plz ping) 2015/09/24 15:20:04 ditto
Lalit Maganti 2015/09/24 16:09:37 Done.
110
111 WebappDataStorage.deleteDataForWebapp(context, id);
112 retainedWebapps.remove(id);
113 }
114
115 preferences.edit()
116 .putLong(KEY_LAST_CLEANUP, currentTime)
117 .putStringSet(KEY_WEBAPP_SET, retainedWebapps)
118 .commit();
119 return null;
120 }
121 }.execute();
122 }
123
124 /**
84 * Deletes the data of all web apps, as well as the registry tracking the we b apps. 125 * Deletes the data of all web apps, as well as the registry tracking the we b apps.
85 */ 126 */
86 @VisibleForTesting 127 @VisibleForTesting
87 static void unregisterAllWebapps(final Context context, final Runnable callb ack) { 128 static void unregisterAllWebapps(final Context context, final Runnable callb ack) {
88 new AsyncTask<Void, Void, Void>() { 129 new AsyncTask<Void, Void, Void>() {
89 @Override 130 @Override
90 protected final Void doInBackground(Void... nothing) { 131 protected final Void doInBackground(Void... nothing) {
91 SharedPreferences preferences = openSharedPreferences(context); 132 SharedPreferences preferences = openSharedPreferences(context);
92 for (String id : getRegisteredWebappIds(preferences)) { 133 for (String id : getRegisteredWebappIds(preferences)) {
93 WebappDataStorage.deleteDataForWebapp(context, id); 134 WebappDataStorage.deleteDataForWebapp(context, id);
(...skipping 26 matching lines...) Expand all
120 161
121 private static Set<String> getRegisteredWebappIds(SharedPreferences preferen ces) { 162 private static Set<String> getRegisteredWebappIds(SharedPreferences preferen ces) {
122 return preferences.getStringSet(KEY_WEBAPP_SET, Collections.<String>empt ySet()); 163 return preferences.getStringSet(KEY_WEBAPP_SET, Collections.<String>empt ySet());
123 } 164 }
124 165
125 private WebappRegistry() { 166 private WebappRegistry() {
126 } 167 }
127 168
128 private static native void nativeOnWebappsUnregistered(long callbackPointer) ; 169 private static native void nativeOnWebappsUnregistered(long callbackPointer) ;
129 } 170 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698