Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(212)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/webapps/WebappDataStorage.java

Issue 1749603002: Store URLs in WebappDataStorage, and purge them when history is cleared. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add @VisibleForTesting to address test failures Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..afef764c75643ba0dd03e70b189d47be79d53c4c 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;
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.
gone 2016/03/08 22:40:39 Given that you're using two different values for I
+ // The next time the webapp is opened, set it to a valid value again.
+ storage.updateLastUsedTime(System.currentTimeMillis());
return null;
}
}.execute();
@@ -67,6 +69,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
public static void getLastUsedTime(final Context context, final String webappId,
final FetchCallback<Long> callback) {
new AsyncTask<Void, Void, Long>() {
@@ -86,6 +89,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
gone 2016/03/08 22:40:39 Venting, no action items here: For the record, thi
dominickn 2016/03/09 08:18:32 Definitely, definitely agreed.
+ 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 +142,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
dominickn 2016/03/09 08:18:32 Done.
+ }
+
+ /**
* Sets the factory used to generate WebappDataStorage objects.
*/
@VisibleForTesting
@@ -131,9 +191,19 @@ public class WebappDataStorage {
new UpdateTask(splashScreenImage).execute();
}
- void updateLastUsedTime() {
+ void updateOriginUrl(String originUrl) {
gone 2016/03/08 22:40:39 Add javadocs to these
dominickn 2016/03/09 08:18:32 Done.
+ 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 +274,4 @@ public class WebappDataStorage {
return null;
}
}
-}
+}

Powered by Google App Engine
This is Rietveld 408576698