| 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 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 */ | 42 */ |
| 43 public interface FetchCallback { | 43 public interface FetchCallback { |
| 44 public void onWebappIdsRetrieved(Set<String> readObject); | 44 public void onWebappIdsRetrieved(Set<String> readObject); |
| 45 } | 45 } |
| 46 | 46 |
| 47 /** | 47 /** |
| 48 * 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. |
| 49 * @param context Context to open the registry with. | 49 * @param context Context to open the registry with. |
| 50 * @param webappId The id of the web app to register. | 50 * @param webappId The id of the web app to register. |
| 51 */ | 51 */ |
| 52 public static void registerWebapp(final Context context, final String webapp
Id) { | 52 public static void registerWebapp(final Context context, final String webapp
Id, |
| 53 final String scope) { |
| 53 new AsyncTask<Void, Void, Void>() { | 54 new AsyncTask<Void, Void, Void>() { |
| 54 @Override | 55 @Override |
| 55 protected final Void doInBackground(Void... nothing) { | 56 protected final Void doInBackground(Void... nothing) { |
| 56 SharedPreferences preferences = openSharedPreferences(context); | 57 SharedPreferences preferences = openSharedPreferences(context); |
| 57 Set<String> webapps = new HashSet<String>(getRegisteredWebappIds
(preferences)); | 58 Set<String> webapps = new HashSet<String>(getRegisteredWebappIds
(preferences)); |
| 58 boolean added = webapps.add(webappId); | 59 boolean added = webapps.add(webappId); |
| 59 assert added; | 60 assert added; |
| 60 | 61 |
| 61 // Update the last used time of the {@link WebappDataStorage} so
we can guarantee | 62 // Update the last used time, so we can guarantee that a web app
which appears in |
| 62 // that the used time will be set (ie. != WebappDataStorage.INVA
LID_LAST_USED) if a | 63 // the registry will have a last used time != WebappDataStorage.
LAST_USED_INVALID. |
| 63 // web app appears in the registry. | 64 WebappDataStorage storage = new WebappDataStorage(context, webap
pId); |
| 64 new WebappDataStorage(context, webappId).updateLastUsedTime(); | 65 storage.setScope(scope); |
| 66 storage.updateLastUsedTime(); |
| 65 preferences.edit().putStringSet(KEY_WEBAPP_SET, webapps).apply()
; | 67 preferences.edit().putStringSet(KEY_WEBAPP_SET, webapps).apply()
; |
| 66 return null; | 68 return null; |
| 67 } | 69 } |
| 68 }.execute(); | 70 }.execute(); |
| 69 } | 71 } |
| 70 | 72 |
| 71 /** | 73 /** |
| 72 * Asynchronously retrieves the list of web app IDs which this registry is a
ware of. | 74 * Asynchronously retrieves the list of web app IDs which this registry is a
ware of. |
| 73 * @param context Context to open the registry with. | 75 * @param context Context to open the registry with. |
| 74 * @param callback Called when the set has been retrieved. The set may be em
pty. | 76 * @param callback Called when the set has been retrieved. The set may be em
pty. |
| 75 */ | 77 */ |
| 76 @VisibleForTesting | 78 @VisibleForTesting |
| 77 public static void getRegisteredWebappIds(final Context context, final Fetch
Callback callback) { | 79 public static void getRegisteredWebappIds(final Context context, final Fetch
Callback callback) { |
| 78 new AsyncTask<Void, Void, Set<String>>() { | 80 new AsyncTask<Void, Void, Set<String>>() { |
| 79 @Override | 81 @Override |
| 80 protected final Set<String> doInBackground(Void... nothing) { | 82 protected final Set<String> doInBackground(Void... nothing) { |
| 81 return getRegisteredWebappIds(openSharedPreferences(context)); | 83 return getRegisteredWebappIds(openSharedPreferences(context)); |
| 82 } | 84 } |
| 83 | 85 |
| 84 @Override | 86 @Override |
| 85 protected final void onPostExecute(Set<String> result) { | 87 protected final void onPostExecute(Set<String> result) { |
| 86 callback.onWebappIdsRetrieved(result); | 88 callback.onWebappIdsRetrieved(result); |
| 87 } | 89 } |
| 88 }.execute(); | 90 }.execute(); |
| 89 } | 91 } |
| 90 | 92 |
| 91 /** | 93 /** |
| 92 * Deletes the data for all "old" web apps. i.e. web apps which have not bee
n opened by the user | 94 * Deletes the data for all "old" web apps. |
| 93 * in the last 3 months. Cleanup is run, at most, once a month. | 95 * "Old" web apps have not been opened by the user in the last 3 months, or
have had their last |
| 96 * used time set to 0 by the user clearing their history. Cleanup is run, at
most, once a month. |
| 97 * |
| 94 * @param context Context to open the registry with. | 98 * @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 | 99 * @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. | 100 * and if a web app should be cleaned up. |
| 97 */ | 101 */ |
| 98 static void unregisterOldWebapps(final Context context, final long currentTi
me) { | 102 static void unregisterOldWebapps(final Context context, final long currentTi
me) { |
| 99 new AsyncTask<Void, Void, Void>() { | 103 new AsyncTask<Void, Void, Void>() { |
| 100 @Override | 104 @Override |
| 101 protected final Void doInBackground(Void... nothing) { | 105 protected final Void doInBackground(Void... nothing) { |
| 102 SharedPreferences preferences = openSharedPreferences(context); | 106 SharedPreferences preferences = openSharedPreferences(context); |
| 103 long lastCleanup = preferences.getLong(KEY_LAST_CLEANUP, 0); | 107 long lastCleanup = preferences.getLong(KEY_LAST_CLEANUP, 0); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 133 SharedPreferences preferences = openSharedPreferences(context); | 137 SharedPreferences preferences = openSharedPreferences(context); |
| 134 for (String id : getRegisteredWebappIds(preferences)) { | 138 for (String id : getRegisteredWebappIds(preferences)) { |
| 135 WebappDataStorage.deleteDataForWebapp(context, id); | 139 WebappDataStorage.deleteDataForWebapp(context, id); |
| 136 } | 140 } |
| 137 preferences.edit().clear().apply(); | 141 preferences.edit().clear().apply(); |
| 138 return null; | 142 return null; |
| 139 } | 143 } |
| 140 | 144 |
| 141 @Override | 145 @Override |
| 142 protected final void onPostExecute(Void nothing) { | 146 protected final void onPostExecute(Void nothing) { |
| 143 if (callback == null) return; | 147 assert callback != null; |
| 144 callback.run(); | 148 callback.run(); |
| 145 } | 149 } |
| 146 }.execute(); | 150 }.execute(); |
| 147 } | 151 } |
| 148 | 152 |
| 149 @CalledByNative | 153 @CalledByNative |
| 150 static void unregisterAllWebapps(Context context, final long callbackPointer
) { | 154 static void unregisterAllWebapps(Context context, final long callbackPointer
) { |
| 151 unregisterAllWebapps(context, new Runnable() { | 155 unregisterAllWebapps(context, new Runnable() { |
| 152 @Override | 156 @Override |
| 153 public void run() { | 157 public void run() { |
| 154 nativeOnWebappsUnregistered(callbackPointer); | 158 nativeOnWebappsUnregistered(callbackPointer); |
| 155 } | 159 } |
| 156 }); | 160 }); |
| 157 } | 161 } |
| 158 | 162 |
| 163 /** |
| 164 * Deletes the scope and sets the last used time to 0 for all web apps. |
| 165 */ |
| 166 @VisibleForTesting |
| 167 static void clearWebappHistory(final Context context, final Runnable callbac
k) { |
| 168 new AsyncTask<Void, Void, Void>() { |
| 169 @Override |
| 170 protected final Void doInBackground(Void... nothing) { |
| 171 SharedPreferences preferences = openSharedPreferences(context); |
| 172 for (String id : getRegisteredWebappIds(preferences)) { |
| 173 WebappDataStorage.clearHistory(context, id); |
| 174 } |
| 175 return null; |
| 176 } |
| 177 |
| 178 @Override |
| 179 protected final void onPostExecute(Void nothing) { |
| 180 assert callback != null; |
| 181 callback.run(); |
| 182 } |
| 183 }.execute(); |
| 184 } |
| 185 |
| 186 @CalledByNative |
| 187 static void clearWebappHistory(Context context, final long callbackPointer)
{ |
| 188 clearWebappHistory(context, new Runnable() { |
| 189 @Override |
| 190 public void run() { |
| 191 nativeOnClearedWebappHistory(callbackPointer); |
| 192 } |
| 193 }); |
| 194 } |
| 195 |
| 159 private static SharedPreferences openSharedPreferences(Context context) { | 196 private static SharedPreferences openSharedPreferences(Context context) { |
| 160 return context.getSharedPreferences(REGISTRY_FILE_NAME, Context.MODE_PRI
VATE); | 197 return context.getSharedPreferences(REGISTRY_FILE_NAME, Context.MODE_PRI
VATE); |
| 161 } | 198 } |
| 162 | 199 |
| 163 private static Set<String> getRegisteredWebappIds(SharedPreferences preferen
ces) { | 200 private static Set<String> getRegisteredWebappIds(SharedPreferences preferen
ces) { |
| 164 return preferences.getStringSet(KEY_WEBAPP_SET, Collections.<String>empt
ySet()); | 201 return preferences.getStringSet(KEY_WEBAPP_SET, Collections.<String>empt
ySet()); |
| 165 } | 202 } |
| 166 | 203 |
| 167 private WebappRegistry() { | 204 private WebappRegistry() { |
| 168 } | 205 } |
| 169 | 206 |
| 170 private static native void nativeOnWebappsUnregistered(long callbackPointer)
; | 207 private static native void nativeOnWebappsUnregistered(long callbackPointer)
; |
| 171 } | 208 private static native void nativeOnClearedWebappHistory(long callbackPointer
); |
| 209 } |
| OLD | NEW |