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 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 originUrl) { | |
| 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 of the {@link WebappDataStorage} so we can |
| 62 // that the used time will be set (ie. != WebappDataStorage.INVA LID_LAST_USED) if a | 63 // guarantee that the used time will be set (ie. != |
| 63 // web app appears in the registry. | 64 // WebappDataStorage.INVALID_LAST_USED) if a web app appears in the registry. |
|
gone
2016/03/08 22:40:39
nit: Rework this comment so that i.e. != WebappDat
dominickn
2016/03/09 08:18:32
Done.
| |
| 64 new WebappDataStorage(context, webappId).updateLastUsedTime(); | 65 WebappDataStorage storage = new WebappDataStorage(context, webap pId); |
| 66 storage.updateOriginUrl(originUrl); | |
| 67 storage.updateLastUsedTime(System.currentTimeMillis()); | |
| 65 preferences.edit().putStringSet(KEY_WEBAPP_SET, webapps).apply() ; | 68 preferences.edit().putStringSet(KEY_WEBAPP_SET, webapps).apply() ; |
| 66 return null; | 69 return null; |
| 67 } | 70 } |
| 68 }.execute(); | 71 }.execute(); |
| 69 } | 72 } |
| 70 | 73 |
| 71 /** | 74 /** |
| 72 * Asynchronously retrieves the list of web app IDs which this registry is a ware of. | 75 * Asynchronously retrieves the list of web app IDs which this registry is a ware of. |
| 73 * @param context Context to open the registry with. | 76 * @param context Context to open the registry with. |
| 74 * @param callback Called when the set has been retrieved. The set may be em pty. | 77 * @param callback Called when the set has been retrieved. The set may be em pty. |
| 75 */ | 78 */ |
| 76 @VisibleForTesting | 79 @VisibleForTesting |
| 77 public static void getRegisteredWebappIds(final Context context, final Fetch Callback callback) { | 80 public static void getRegisteredWebappIds(final Context context, final Fetch Callback callback) { |
| 78 new AsyncTask<Void, Void, Set<String>>() { | 81 new AsyncTask<Void, Void, Set<String>>() { |
| 79 @Override | 82 @Override |
| 80 protected final Set<String> doInBackground(Void... nothing) { | 83 protected final Set<String> doInBackground(Void... nothing) { |
| 81 return getRegisteredWebappIds(openSharedPreferences(context)); | 84 return getRegisteredWebappIds(openSharedPreferences(context)); |
| 82 } | 85 } |
| 83 | 86 |
| 84 @Override | 87 @Override |
| 85 protected final void onPostExecute(Set<String> result) { | 88 protected final void onPostExecute(Set<String> result) { |
| 86 callback.onWebappIdsRetrieved(result); | 89 callback.onWebappIdsRetrieved(result); |
| 87 } | 90 } |
| 88 }.execute(); | 91 }.execute(); |
| 89 } | 92 } |
| 90 | 93 |
| 91 /** | 94 /** |
| 92 * Deletes the data for all "old" web apps. i.e. web apps which have not bee n opened by the user | 95 * Deletes the data for all "old" web apps. |
| 93 * in the last 3 months. Cleanup is run, at most, once a month. | 96 * "Old" web apps have not been opened by the user in the last 3 months, or have had their last |
| 97 * used time set to 0 by the user clearing their history. Cleanup is run, at most, once a month. | |
|
gone
2016/03/08 22:40:39
nit: newline between this and @param
dominickn
2016/03/09 08:18:32
Done.
| |
| 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 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 origin URL 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 if (callback == null) return; | |
|
gone
2016/03/08 22:40:39
Is this callback == null check here only for tests
dominickn
2016/03/09 08:18:32
I just copied from the block above this one. I'll
| |
| 181 callback.run(); | |
| 182 } | |
| 183 }.execute(); | |
| 184 } | |
| 185 | |
| 186 @CalledByNative | |
| 187 static void clearWebappHistory(Context context, final long callbackPointer) { | |
|
gone
2016/03/08 22:40:39
If this is only called by native code, use private
dominickn
2016/03/09 08:18:32
I did this in patchset #3, but it fails to compile
gone
2016/03/10 23:27:21
Strange... it's definitely called, isn't it? I me
dominickn
2016/03/11 05:14:42
Done.
| |
| 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 |