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

Side by Side 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, 9 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 unified diff | Download patch
OLDNEW
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.graphics.Bitmap; 9 import android.graphics.Bitmap;
10 import android.os.AsyncTask; 10 import android.os.AsyncTask;
(...skipping 13 matching lines...) Expand all
24 * (1) UPDATING/RETRIEVING THE ICON (web app MUST have been registered in Webapp Registry) 24 * (1) UPDATING/RETRIEVING THE ICON (web app MUST have been registered in Webapp Registry)
25 * WebappDataStorage storage = WebappDataStorage.open(context, id); 25 * WebappDataStorage storage = WebappDataStorage.open(context, id);
26 * storage.updateSplashScreenImage(bitmap); 26 * storage.updateSplashScreenImage(bitmap);
27 * storage.getSplashScreenImage(callback); 27 * storage.getSplashScreenImage(callback);
28 */ 28 */
29 public class WebappDataStorage { 29 public class WebappDataStorage {
30 30
31 static final String SHARED_PREFS_FILE_PREFIX = "webapp_"; 31 static final String SHARED_PREFS_FILE_PREFIX = "webapp_";
32 static final String KEY_SPLASH_ICON = "splash_icon"; 32 static final String KEY_SPLASH_ICON = "splash_icon";
33 static final String KEY_LAST_USED = "last_used"; 33 static final String KEY_LAST_USED = "last_used";
34 static final String KEY_ORIGIN_URL = "origin_url";
35
36 // Null/invalid constants for last used times and origins. 0 is used as the null last used time
37 // as WebappRegistry assumes that this is always a valid timestamp.
38 static final long NULL_LAST_USED = 0;
34 static final long INVALID_LAST_USED = -1; 39 static final long INVALID_LAST_USED = -1;
40 static final String INVALID_ORIGIN_URL = "";
35 41
36 private static Factory sFactory = new Factory(); 42 private static Factory sFactory = new Factory();
37 43
38 private final SharedPreferences mPreferences; 44 private final SharedPreferences mPreferences;
39 45
40 /** 46 /**
41 * Opens an instance of WebappDataStorage for the web app specified. 47 * Opens an instance of WebappDataStorage for the web app specified.
42 * @param context The context to open the SharedPreferences. 48 * @param context The context to open the SharedPreferences.
43 * @param webappId The ID of the web app which is being opened. 49 * @param webappId The ID of the web app which is being opened.
44 */ 50 */
45 public static WebappDataStorage open(final Context context, final String web appId) { 51 public static WebappDataStorage open(final Context context, final String web appId) {
46 final WebappDataStorage storage = sFactory.create(context, webappId); 52 final WebappDataStorage storage = sFactory.create(context, webappId);
47 new AsyncTask<Void, Void, Void>() { 53 new AsyncTask<Void, Void, Void>() {
48 @Override 54 @Override
49 protected final Void doInBackground(Void... nothing) { 55 protected final Void doInBackground(Void... nothing) {
50 if (storage.getLastUsedTime() == INVALID_LAST_USED) { 56 // The last used time may be invalid if the user has cleared the ir history.
gone 2016/03/08 22:40:39 Given that you're using two different values for I
51 // If the last used time is invalid then assert that there i s no data 57 // The next time the webapp is opened, set it to a valid value a gain.
52 // in the WebappDataStorage which needs to be cleaned up. 58 storage.updateLastUsedTime(System.currentTimeMillis());
53 assert storage.getAllData().isEmpty();
54 } else {
55 storage.updateLastUsedTime();
56 }
57 return null; 59 return null;
58 } 60 }
59 }.execute(); 61 }.execute();
60 return storage; 62 return storage;
61 } 63 }
62 64
63 /** 65 /**
64 * Asynchronously retrieves the time which this WebappDataStorage was last 66 * Asynchronously retrieves the time which this WebappDataStorage was last
65 * opened using {@link WebappDataStorage#open(Context, String)}. 67 * opened using {@link WebappDataStorage#open(Context, String)}.
66 * @param context The context to read the SharedPreferences file. 68 * @param context The context to read the SharedPreferences file.
67 * @param webappId The ID of the web app the used time is being read for. 69 * @param webappId The ID of the web app the used time is being read for.
68 * @param callback Called when the last used time has been retrieved. 70 * @param callback Called when the last used time has been retrieved.
69 */ 71 */
72 @VisibleForTesting
70 public static void getLastUsedTime(final Context context, final String webap pId, 73 public static void getLastUsedTime(final Context context, final String webap pId,
71 final FetchCallback<Long> callback) { 74 final FetchCallback<Long> callback) {
72 new AsyncTask<Void, Void, Long>() { 75 new AsyncTask<Void, Void, Long>() {
73 @Override 76 @Override
74 protected final Long doInBackground(Void... nothing) { 77 protected final Long doInBackground(Void... nothing) {
75 long lastUsed = new WebappDataStorage(context.getApplicationCont ext(), webappId) 78 long lastUsed = new WebappDataStorage(context.getApplicationCont ext(), webappId)
76 .getLastUsedTime(); 79 .getLastUsedTime();
77 assert lastUsed != INVALID_LAST_USED; 80 assert lastUsed != INVALID_LAST_USED;
78 return lastUsed; 81 return lastUsed;
79 } 82 }
80 83
81 @Override 84 @Override
82 protected final void onPostExecute(Long lastUsed) { 85 protected final void onPostExecute(Long lastUsed) {
83 callback.onDataRetrieved(lastUsed); 86 callback.onDataRetrieved(lastUsed);
84 } 87 }
85 }.execute(); 88 }.execute();
86 } 89 }
87 90
88 /** 91 /**
92 * Asynchronously retrieves the origin URL stored in this WebappDataStorage.
93 * @param context The context to read the SharedPreferences file.
94 * @param webappId The ID of the web app the used time is being read for.
95 * @param callback Called when the origin has been retrieved.
96 */
97 @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.
98 public static void getOriginUrl(final Context context, final String webappId ,
99 final FetchCallback<String> callback) {
100 new AsyncTask<Void, Void, String>() {
101 @Override
102 protected final String doInBackground(Void... nothing) {
103 String originUrl = new WebappDataStorage(context.getApplicationC ontext(), webappId)
104 .getOriginUrl();
105 return originUrl;
106 }
107
108 @Override
109 protected final void onPostExecute(String originUrl) {
110 callback.onDataRetrieved(originUrl);
111 }
112 }.execute();
113 }
114
115 /**
116 * Asynchronously updates the origin URL stored in this WebappDataStorage.
117 * @param context The context to read the SharedPreferences file.
118 * @param webappId The ID of the web app the used time is being read for.
119 * @param origin The origin to set for the web app.
120 */
121 public static void updateOriginUrl(final Context context, final String webap pId,
122 final String originUrl) {
123 new AsyncTask<Void, Void, Void>() {
124 @Override
125 protected final Void doInBackground(Void... nothing) {
126 new WebappDataStorage(context.getApplicationContext(), webappId)
127 .updateOriginUrl(originUrl);
128 return null;
129 }
130 }.execute();
131 }
132
133 /**
89 * Deletes the data for a web app by clearing all the information inside the SharedPreferences 134 * Deletes the data for a web app by clearing all the information inside the SharedPreferences
90 * file. This does NOT delete the file itself but the file is left empty. 135 * file. This does NOT delete the file itself but the file is left empty.
91 * @param context The context to read the SharedPreferences file. 136 * @param context The context to read the SharedPreferences file.
92 * @param webappId The ID of the web app being deleted. 137 * @param webappId The ID of the web app being deleted.
93 */ 138 */
94 static void deleteDataForWebapp(final Context context, final String webappId ) { 139 static void deleteDataForWebapp(final Context context, final String webappId ) {
95 assert !ThreadUtils.runningOnUiThread(); 140 assert !ThreadUtils.runningOnUiThread();
96 openSharedPreferences(context, webappId).edit().clear().apply(); 141 openSharedPreferences(context, webappId).edit().clear().apply();
97 } 142 }
98 143
99 /** 144 /**
145 * Deletes the origin URL and sets last used time to 0 this web app in Share dPreferences.
146 * This does not remove the stored splash screen image (if any) for the app.
147 * @param context The context to read the SharedPreferences file.
148 * @param webappId The ID of the web app being deleted.
149 */
150 static void clearHistory(final Context context, final String webappId) {
151 // The last used time is set to 0 to ensure that a valid value is always present.
152 // If the webapp is not launched prior to the next cleanup, then its rem aining data will be
153 // removed. Otherwise, the next launch will update the last used time.
154 assert !ThreadUtils.runningOnUiThread();
155 openSharedPreferences(context, webappId)
156 .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.
157 }
158
159 /**
100 * Sets the factory used to generate WebappDataStorage objects. 160 * Sets the factory used to generate WebappDataStorage objects.
101 */ 161 */
102 @VisibleForTesting 162 @VisibleForTesting
103 public static void setFactoryForTests(Factory factory) { 163 public static void setFactoryForTests(Factory factory) {
104 sFactory = factory; 164 sFactory = factory;
105 } 165 }
106 166
107 private static SharedPreferences openSharedPreferences(Context context, Stri ng webappId) { 167 private static SharedPreferences openSharedPreferences(Context context, Stri ng webappId) {
108 return context.getApplicationContext().getSharedPreferences( 168 return context.getApplicationContext().getSharedPreferences(
109 SHARED_PREFS_FILE_PREFIX + webappId, Context.MODE_PRIVATE); 169 SHARED_PREFS_FILE_PREFIX + webappId, Context.MODE_PRIVATE);
(...skipping 14 matching lines...) Expand all
124 } 184 }
125 185
126 /* 186 /*
127 * Update the information associated with the web app with the specified dat a. 187 * Update the information associated with the web app with the specified dat a.
128 * @param splashScreenImage The image which should be shown on the splash sc reen of the web app. 188 * @param splashScreenImage The image which should be shown on the splash sc reen of the web app.
129 */ 189 */
130 public void updateSplashScreenImage(Bitmap splashScreenImage) { 190 public void updateSplashScreenImage(Bitmap splashScreenImage) {
131 new UpdateTask(splashScreenImage).execute(); 191 new UpdateTask(splashScreenImage).execute();
132 } 192 }
133 193
134 void updateLastUsedTime() { 194 void updateOriginUrl(String originUrl) {
gone 2016/03/08 22:40:39 Add javadocs to these
dominickn 2016/03/09 08:18:32 Done.
135 assert !ThreadUtils.runningOnUiThread(); 195 assert !ThreadUtils.runningOnUiThread();
136 mPreferences.edit().putLong(KEY_LAST_USED, System.currentTimeMillis()).a pply(); 196 mPreferences.edit().putString(KEY_ORIGIN_URL, originUrl).apply();
197 }
198
199 String getOriginUrl() {
200 assert !ThreadUtils.runningOnUiThread();
201 return mPreferences.getString(KEY_ORIGIN_URL, INVALID_ORIGIN_URL);
202 }
203
204 void updateLastUsedTime(long lastUsedTime) {
205 assert !ThreadUtils.runningOnUiThread();
206 mPreferences.edit().putLong(KEY_LAST_USED, lastUsedTime).apply();
137 } 207 }
138 208
139 long getLastUsedTime() { 209 long getLastUsedTime() {
140 assert !ThreadUtils.runningOnUiThread(); 210 assert !ThreadUtils.runningOnUiThread();
141 return mPreferences.getLong(KEY_LAST_USED, INVALID_LAST_USED); 211 return mPreferences.getLong(KEY_LAST_USED, INVALID_LAST_USED);
142 } 212 }
143 213
144 private Map<String, ?> getAllData() { 214 private Map<String, ?> getAllData() {
145 return mPreferences.getAll(); 215 return mPreferences.getAll();
146 } 216 }
(...skipping 13 matching lines...) Expand all
160 public static class Factory { 230 public static class Factory {
161 231
162 /** 232 /**
163 * Generates a WebappDataStorage class for a specified web app. 233 * Generates a WebappDataStorage class for a specified web app.
164 */ 234 */
165 public WebappDataStorage create(final Context context, final String weba ppId) { 235 public WebappDataStorage create(final Context context, final String weba ppId) {
166 return new WebappDataStorage(context, webappId); 236 return new WebappDataStorage(context, webappId);
167 } 237 }
168 } 238 }
169 239
170 private final class BitmapFetchTask extends AsyncTask<Void, Void, Bitmap> { 240 private final class BitmapFetchTask extends AsyncTask<Void, Void, Bitmap> {
gone 2016/03/10 23:27:21 So much \o/.
dominickn 2016/03/11 05:14:42 \o/ \o/
171 241
172 private final String mKey; 242 private final String mKey;
173 private final FetchCallback<Bitmap> mCallback; 243 private final FetchCallback<Bitmap> mCallback;
174 244
175 public BitmapFetchTask(String key, FetchCallback<Bitmap> callback) { 245 public BitmapFetchTask(String key, FetchCallback<Bitmap> callback) {
176 mKey = key; 246 mKey = key;
177 mCallback = callback; 247 mCallback = callback;
178 } 248 }
179 249
180 @Override 250 @Override
(...skipping 16 matching lines...) Expand all
197 } 267 }
198 268
199 @Override 269 @Override
200 protected Void doInBackground(Void... nothing) { 270 protected Void doInBackground(Void... nothing) {
201 mPreferences.edit() 271 mPreferences.edit()
202 .putString(KEY_SPLASH_ICON, ShortcutHelper.encodeBitmapAsStr ing(mSplashImage)) 272 .putString(KEY_SPLASH_ICON, ShortcutHelper.encodeBitmapAsStr ing(mSplashImage))
203 .apply(); 273 .apply();
204 return null; 274 return null;
205 } 275 }
206 } 276 }
207 } 277 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698