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..103897ee191c9f99401b08bc426c78f3c1eb8551 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 long INVALID_LAST_USED = -1; |
| + static final String KEY_ORIGIN_URL = "origin_url"; |
| + |
| + // Unset/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 LAST_USED_UNSET = 0; |
| + static final long LAST_USED_INVALID = -1; |
| + static final String ORIGIN_URL_INVALID = ""; |
| private static Factory sFactory = new Factory(); |
| @@ -47,12 +53,12 @@ public class WebappDataStorage { |
| new AsyncTask<Void, Void, Void>() { |
| @Override |
| protected final Void doInBackground(Void... nothing) { |
| - if (storage.getLastUsedTime() == INVALID_LAST_USED) { |
| + if (storage.getLastUsedTime() == LAST_USED_INVALID) { |
| // 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(); |
| + storage.updateLastUsedTime(System.currentTimeMillis()); |
| } |
| return null; |
| } |
| @@ -67,6 +73,7 @@ public class WebappDataStorage { |
| * @param webappId The ID of the web app the used time is being read for. |
| * @param callback Called when the last used time has been retrieved. |
| */ |
| + @VisibleForTesting |
|
mlamouri (slow - plz ping)
2016/03/16 13:34:28
Can you remove this? I'm pretty sure it is not vis
dominickn
2016/03/17 04:31:26
It's used in the ClearBrowsingDataPreferencesTest,
|
| public static void getLastUsedTime(final Context context, final String webappId, |
| final FetchCallback<Long> callback) { |
| new AsyncTask<Void, Void, Long>() { |
| @@ -74,7 +81,7 @@ public class WebappDataStorage { |
| protected final Long doInBackground(Void... nothing) { |
| long lastUsed = new WebappDataStorage(context.getApplicationContext(), webappId) |
| .getLastUsedTime(); |
| - assert lastUsed != INVALID_LAST_USED; |
| + assert lastUsed != LAST_USED_INVALID; |
| return lastUsed; |
| } |
| @@ -86,6 +93,48 @@ 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. |
| + */ |
| + @VisibleForTesting |
| + 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 +146,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. |
|
mlamouri (slow - plz ping)
2016/03/16 13:34:28
Deleting the origin sounds odd as in not really re
dominickn
2016/03/17 04:31:26
I guess the combination of having a last used time
mlamouri (slow - plz ping)
2016/03/24 10:36:42
Sure. What I meant is that the splashscreen image
|
| + * @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, LAST_USED_UNSET).remove(KEY_ORIGIN_URL).apply(); |
| + } |
| + |
| + /** |
| * Sets the factory used to generate WebappDataStorage objects. |
| */ |
| @VisibleForTesting |
| @@ -119,26 +183,70 @@ public class WebappDataStorage { |
| * @param callback Called when the splash screen image has been retrieved. |
| * May be null if no image was found. |
| */ |
| - public void getSplashScreenImage(FetchCallback<Bitmap> callback) { |
| - new BitmapFetchTask(KEY_SPLASH_ICON, callback).execute(); |
| + public void getSplashScreenImage(final FetchCallback<Bitmap> callback) { |
| + new AsyncTask<Void, Void, Bitmap>() { |
| + @Override |
| + protected final Bitmap doInBackground(Void... nothing) { |
| + return ShortcutHelper.decodeBitmapFromString( |
| + mPreferences.getString(KEY_SPLASH_ICON, null)); |
| + } |
| + |
| + @Override |
| + protected final void onPostExecute(Bitmap result) { |
| + callback.onDataRetrieved(result); |
| + } |
| + }.execute(); |
| } |
| /* |
| * Update the information associated with the web app with the specified data. |
| * @param splashScreenImage The image which should be shown on the splash screen of the web app. |
| */ |
| - public void updateSplashScreenImage(Bitmap splashScreenImage) { |
| - new UpdateTask(splashScreenImage).execute(); |
| + public void updateSplashScreenImage(final Bitmap splashScreenImage) { |
| + new AsyncTask<Void, Void, Void>() { |
| + @Override |
| + protected final Void doInBackground(Void... nothing) { |
| + mPreferences.edit() |
| + .putString(KEY_SPLASH_ICON, |
| + ShortcutHelper.encodeBitmapAsString(splashScreenImage)) |
| + .apply(); |
| + return null; |
| + } |
| + }.execute(); |
| } |
| - void updateLastUsedTime() { |
| + /** |
| + * Updates the origin URL stored in this object. |
| + * @param originUrl the new origin URL |
| + */ |
| + void updateOriginUrl(String originUrl) { |
| + assert !ThreadUtils.runningOnUiThread(); |
| + mPreferences.edit().putString(KEY_ORIGIN_URL, originUrl).apply(); |
| + } |
| + |
| + /** |
| + * Returns the origin URL stored in this object, or "" if it is not stored. |
| + */ |
| + String getOriginUrl() { |
| + assert !ThreadUtils.runningOnUiThread(); |
| + return mPreferences.getString(KEY_ORIGIN_URL, ORIGIN_URL_INVALID); |
| + } |
| + |
| + /** |
| + * Updates the last used time of this object. |
| + * @param lastUsedTime the new last used time. |
| + */ |
| + void updateLastUsedTime(long lastUsedTime) { |
|
mlamouri (slow - plz ping)
2016/03/16 13:34:28
Did you consider updateLastUsedTime() and resetLas
dominickn
2016/03/17 04:31:26
Originally, the reset method used this mechanism,
|
| assert !ThreadUtils.runningOnUiThread(); |
| - mPreferences.edit().putLong(KEY_LAST_USED, System.currentTimeMillis()).apply(); |
| + mPreferences.edit().putLong(KEY_LAST_USED, lastUsedTime).apply(); |
| } |
| + /** |
| + * Returns the last used time of this object, or -1 if it is not stored. |
| + */ |
| long getLastUsedTime() { |
| assert !ThreadUtils.runningOnUiThread(); |
| - return mPreferences.getLong(KEY_LAST_USED, INVALID_LAST_USED); |
| + return mPreferences.getLong(KEY_LAST_USED, LAST_USED_INVALID); |
| } |
| private Map<String, ?> getAllData() { |
| @@ -166,42 +274,4 @@ public class WebappDataStorage { |
| return new WebappDataStorage(context, webappId); |
| } |
| } |
| - |
| - private final class BitmapFetchTask extends AsyncTask<Void, Void, Bitmap> { |
| - |
| - private final String mKey; |
| - private final FetchCallback<Bitmap> mCallback; |
| - |
| - public BitmapFetchTask(String key, FetchCallback<Bitmap> callback) { |
| - mKey = key; |
| - mCallback = callback; |
| - } |
| - |
| - @Override |
| - protected final Bitmap doInBackground(Void... nothing) { |
| - return ShortcutHelper.decodeBitmapFromString(mPreferences.getString(mKey, null)); |
| - } |
| - |
| - @Override |
| - protected final void onPostExecute(Bitmap result) { |
| - mCallback.onDataRetrieved(result); |
| - } |
| - } |
| - |
| - private final class UpdateTask extends AsyncTask<Void, Void, Void> { |
| - |
| - private final Bitmap mSplashImage; |
| - |
| - public UpdateTask(Bitmap splashImage) { |
| - mSplashImage = splashImage; |
| - } |
| - |
| - @Override |
| - protected Void doInBackground(Void... nothing) { |
| - mPreferences.edit() |
| - .putString(KEY_SPLASH_ICON, ShortcutHelper.encodeBitmapAsString(mSplashImage)) |
| - .apply(); |
| - return null; |
| - } |
| - } |
| -} |
| +} |