Chromium Code Reviews| 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 10:27:11
/** Represents a period of 4 weeks in milliseconds
Lalit Maganti
2015/09/24 12:35:44
Done.
| |
| 34 static final long FULL_CLEANUP_DURATION = 4 * 7 * 24 * 60 * 60 * 1000; | |
| 35 | |
| 36 /* Represnts a period of 13 weeks in milliseconds */ | |
|
gone
2015/09/24 10:27:12
same fix as above. rename to WEBAPP_UNOPENED_CLEA
Lalit Maganti
2015/09/24 12:35:44
Done.
| |
| 37 static final long WEBAPP_NON_OPEN_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. webapps which have not been opened by the user | |
|
gone
2015/09/24 10:27:11
nit: web apps or webapps.
instead of leaving usef
Lalit Maganti
2015/09/24 12:35:44
Done.
| |
| 92 * in a certain period of time. | |
| 93 * @param context Context to open the registry with. | |
| 94 */ | |
| 95 static void unregisterOldWebapps(final Context context) { | |
| 96 new AsyncTask<Void, Void, Void>() { | |
| 97 @Override | |
| 98 protected final Void doInBackground(Void... nothing) { | |
| 99 SharedPreferences preferences = openSharedPreferences(context); | |
| 100 long currentTime = System.currentTimeMillis(); | |
| 101 long lastCleanup = preferences.getLong(KEY_LAST_CLEANUP, 0); | |
| 102 if (currentTime - lastCleanup < FULL_CLEANUP_DURATION) return nu ll; | |
| 103 | |
| 104 Set<String> currentWebapps = getRegisteredWebappIds(preferences) ; | |
| 105 Set<String> retainedWebapps = new HashSet<String>(currentWebapps ); | |
| 106 for (String id : currentWebapps) { | |
| 107 long lastUsed = new WebappDataStorage(context, id).getLastUs edTime(); | |
| 108 if (currentTime - lastUsed >= WEBAPP_NON_OPEN_CLEANUP_DURATI ON) { | |
| 109 WebappDataStorage.deleteDataForWebapp(context, id); | |
| 110 retainedWebapps.remove(id); | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 preferences.edit() | |
| 115 .putLong(KEY_LAST_CLEANUP, currentTime) | |
| 116 .putStringSet(KEY_WEBAPP_SET, retainedWebapps) | |
| 117 .commit(); | |
| 118 return null; | |
| 119 } | |
| 120 }.execute(); | |
| 121 } | |
| 122 | |
| 123 /** | |
| 84 * Deletes the data of all web apps, as well as the registry tracking the we b apps. | 124 * Deletes the data of all web apps, as well as the registry tracking the we b apps. |
| 85 */ | 125 */ |
| 86 @VisibleForTesting | 126 @VisibleForTesting |
| 87 static void unregisterAllWebapps(final Context context, final Runnable callb ack) { | 127 static void unregisterAllWebapps(final Context context, final Runnable callb ack) { |
| 88 new AsyncTask<Void, Void, Void>() { | 128 new AsyncTask<Void, Void, Void>() { |
| 89 @Override | 129 @Override |
| 90 protected final Void doInBackground(Void... nothing) { | 130 protected final Void doInBackground(Void... nothing) { |
| 91 SharedPreferences preferences = openSharedPreferences(context); | 131 SharedPreferences preferences = openSharedPreferences(context); |
| 92 for (String id : getRegisteredWebappIds(preferences)) { | 132 for (String id : getRegisteredWebappIds(preferences)) { |
| 93 WebappDataStorage.deleteDataForWebapp(context, id); | 133 WebappDataStorage.deleteDataForWebapp(context, id); |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 120 | 160 |
| 121 private static Set<String> getRegisteredWebappIds(SharedPreferences preferen ces) { | 161 private static Set<String> getRegisteredWebappIds(SharedPreferences preferen ces) { |
| 122 return preferences.getStringSet(KEY_WEBAPP_SET, Collections.<String>empt ySet()); | 162 return preferences.getStringSet(KEY_WEBAPP_SET, Collections.<String>empt ySet()); |
| 123 } | 163 } |
| 124 | 164 |
| 125 private WebappRegistry() { | 165 private WebappRegistry() { |
| 126 } | 166 } |
| 127 | 167 |
| 128 private static native void nativeOnWebappsUnregistered(long callbackPointer) ; | 168 private static native void nativeOnWebappsUnregistered(long callbackPointer) ; |
| 129 } | 169 } |
| OLD | NEW |