Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappDataStorage.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappDataStorage.java b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappDataStorage.java |
| index a7fd579268f336d49bf383d1bfee84165fa57f37..d06f072b63f0a3f37c4d192a1405c50f92df9d63 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappDataStorage.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappDataStorage.java |
| @@ -31,7 +31,13 @@ public class WebappDataStorage { |
| static final String SHARED_PREFS_FILE_PREFIX = "webapp_"; |
| static final String KEY_SPLASH_ICON = "splash_icon"; |
| static final String KEY_LAST_USED = "last_used"; |
| + static final String KEY_ORIGIN_URL = "origin_url"; |
| + |
| + // Null/invalid constants for last used times and origins. 0 is used as the null last used time |
| + // as WebappRegistry assumes that this is always a valid timestamp. |
| + static final long NULL_LAST_USED = 0; |
|
gone
2016/03/08 22:40:39
It's kind of weird to have both an INVALID and a N
|
| static final long INVALID_LAST_USED = -1; |
| + static final String INVALID_ORIGIN_URL = ""; |
| private static Factory sFactory = new Factory(); |
| @@ -47,13 +53,9 @@ public class WebappDataStorage { |
| new AsyncTask<Void, Void, Void>() { |
| @Override |
| protected final Void doInBackground(Void... nothing) { |
| - if (storage.getLastUsedTime() == INVALID_LAST_USED) { |
| - // If the last used time is invalid then assert that there is no data |
| - // in the WebappDataStorage which needs to be cleaned up. |
| - assert storage.getAllData().isEmpty(); |
| - } else { |
| - storage.updateLastUsedTime(); |
| - } |
| + // The last used time may be invalid if the user has cleared their history. |
| + // The next time the webapp is opened, set it to a valid value again. |
| + storage.updateLastUsedTime(System.currentTimeMillis()); |
| return null; |
| } |
| }.execute(); |
| @@ -86,6 +88,47 @@ public class WebappDataStorage { |
| } |
| /** |
| + * Asynchronously retrieves the origin URL stored in this WebappDataStorage. |
| + * @param context The context to read the SharedPreferences file. |
| + * @param webappId The ID of the web app the used time is being read for. |
| + * @param callback Called when the origin has been retrieved. |
| + */ |
| + public static void getOriginUrl(final Context context, final String webappId, |
| + final FetchCallback<String> callback) { |
| + new AsyncTask<Void, Void, String>() { |
| + @Override |
| + protected final String doInBackground(Void... nothing) { |
| + String originUrl = new WebappDataStorage(context.getApplicationContext(), webappId) |
| + .getOriginUrl(); |
| + return originUrl; |
| + } |
| + |
| + @Override |
| + protected final void onPostExecute(String originUrl) { |
| + callback.onDataRetrieved(originUrl); |
| + } |
| + }.execute(); |
| + } |
| + |
| + /** |
| + * Asynchronously updates the origin URL stored in this WebappDataStorage. |
| + * @param context The context to read the SharedPreferences file. |
| + * @param webappId The ID of the web app the used time is being read for. |
| + * @param origin The origin to set for the web app. |
| + */ |
| + public static void updateOriginUrl(final Context context, final String webappId, |
| + final String originUrl) { |
| + new AsyncTask<Void, Void, Void>() { |
| + @Override |
| + protected final Void doInBackground(Void... nothing) { |
| + new WebappDataStorage(context.getApplicationContext(), webappId) |
| + .updateOriginUrl(originUrl); |
| + return null; |
| + } |
| + }.execute(); |
| + } |
| + |
| + /** |
| * Deletes the data for a web app by clearing all the information inside the SharedPreferences |
| * file. This does NOT delete the file itself but the file is left empty. |
| * @param context The context to read the SharedPreferences file. |
| @@ -97,6 +140,21 @@ public class WebappDataStorage { |
| } |
| /** |
| + * Deletes the origin URL and sets last used time to 0 this web app in SharedPreferences. |
| + * This does not remove the stored splash screen image (if any) for the app. |
| + * @param context The context to read the SharedPreferences file. |
| + * @param webappId The ID of the web app being deleted. |
| + */ |
| + static void clearHistory(final Context context, final String webappId) { |
| + // The last used time is set to 0 to ensure that a valid value is always present. |
| + // If the webapp is not launched prior to the next cleanup, then its remaining data will be |
| + // removed. Otherwise, the next launch will update the last used time. |
| + assert !ThreadUtils.runningOnUiThread(); |
| + openSharedPreferences(context, webappId) |
| + .edit().putLong(KEY_LAST_USED, NULL_LAST_USED).remove(KEY_ORIGIN_URL).apply(); |
|
gone
2016/03/08 22:40:39
nit: indent by 8
|
| + } |
| + |
| + /** |
| * Sets the factory used to generate WebappDataStorage objects. |
| */ |
| @VisibleForTesting |
| @@ -131,9 +189,19 @@ public class WebappDataStorage { |
| new UpdateTask(splashScreenImage).execute(); |
| } |
| - void updateLastUsedTime() { |
| + void updateOriginUrl(String originUrl) { |
| + assert !ThreadUtils.runningOnUiThread(); |
| + mPreferences.edit().putString(KEY_ORIGIN_URL, originUrl).apply(); |
| + } |
| + |
| + String getOriginUrl() { |
| + assert !ThreadUtils.runningOnUiThread(); |
| + return mPreferences.getString(KEY_ORIGIN_URL, INVALID_ORIGIN_URL); |
| + } |
| + |
| + void updateLastUsedTime(long lastUsedTime) { |
| assert !ThreadUtils.runningOnUiThread(); |
| - mPreferences.edit().putLong(KEY_LAST_USED, System.currentTimeMillis()).apply(); |
| + mPreferences.edit().putLong(KEY_LAST_USED, lastUsedTime).apply(); |
| } |
| long getLastUsedTime() { |
| @@ -204,4 +272,4 @@ public class WebappDataStorage { |
| return null; |
| } |
| } |
| -} |
| +} |