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 20 matching lines...) Expand all Loading... |
31 static final String KEY_WEBAPP_SET = "webapp_set"; | 31 static final String KEY_WEBAPP_SET = "webapp_set"; |
32 static final String KEY_LAST_CLEANUP = "last_cleanup"; | 32 static final String KEY_LAST_CLEANUP = "last_cleanup"; |
33 | 33 |
34 /** Represents a period of 4 weeks in milliseconds */ | 34 /** Represents a period of 4 weeks in milliseconds */ |
35 static final long FULL_CLEANUP_DURATION = TimeUnit.DAYS.toMillis(4L * 7L); | 35 static final long FULL_CLEANUP_DURATION = TimeUnit.DAYS.toMillis(4L * 7L); |
36 | 36 |
37 /** Represents a period of 13 weeks in milliseconds */ | 37 /** Represents a period of 13 weeks in milliseconds */ |
38 static final long WEBAPP_UNOPENED_CLEANUP_DURATION = TimeUnit.DAYS.toMillis(
13L * 7L); | 38 static final long WEBAPP_UNOPENED_CLEANUP_DURATION = TimeUnit.DAYS.toMillis(
13L * 7L); |
39 | 39 |
40 /** | 40 /** |
41 * Called when a retrieval of the stored web apps occurs. | 41 * Called when a retrieval of the set of stored web app IDs occurs. |
42 */ | 42 */ |
43 public interface FetchCallback { | 43 public interface FetchCallback { |
44 public void onWebappIdsRetrieved(Set<String> readObject); | 44 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 * Called when a retrieval of the stored WebappDataStorage occurs. The stora
ge parameter will |
| 49 * be null if the web app queried for was not in the registry. |
| 50 */ |
| 51 public interface FetchWebappDataStorageCallback { |
| 52 void onWebappDataStorageRetrieved(WebappDataStorage storage); |
| 53 } |
| 54 |
| 55 /** |
| 56 * Registers the existence of a web app, creates the SharedPreference for it
, and runs the |
| 57 * supplied callback (if not null) on the UI thread with the resulting Webap
pDataStorage object. |
49 * @param context Context to open the registry with. | 58 * @param context Context to open the registry with. |
50 * @param webappId The id of the web app to register. | 59 * @param webappId The id of the web app to register. |
| 60 * @param callback The callback to run with the WebappDataStorage argument. |
| 61 * @return The storage object for the web app. |
51 */ | 62 */ |
52 public static void registerWebapp(final Context context, final String webapp
Id, | 63 public static void registerWebapp(final Context context, final String webapp
Id, |
53 final String scope) { | 64 final FetchWebappDataStorageCallback callback) { |
54 new AsyncTask<Void, Void, Void>() { | 65 new AsyncTask<Void, Void, WebappDataStorage>() { |
55 @Override | 66 @Override |
56 protected final Void doInBackground(Void... nothing) { | 67 protected final WebappDataStorage doInBackground(Void... nothing) { |
57 SharedPreferences preferences = openSharedPreferences(context); | 68 SharedPreferences preferences = openSharedPreferences(context); |
| 69 // The set returned by getRegisteredWebappIds must be treated as
immutable, so we |
| 70 // make a copy to edit and save. |
58 Set<String> webapps = new HashSet<String>(getRegisteredWebappIds
(preferences)); | 71 Set<String> webapps = new HashSet<String>(getRegisteredWebappIds
(preferences)); |
59 boolean added = webapps.add(webappId); | 72 boolean added = webapps.add(webappId); |
60 assert added; | 73 assert added; |
61 | 74 |
62 // Update the last used time, so we can guarantee that a web app
which appears in | 75 preferences.edit().putStringSet(KEY_WEBAPP_SET, webapps).apply()
; |
63 // the registry will have a last used time != WebappDataStorage.
LAST_USED_INVALID. | 76 |
| 77 // Create the WebappDataStorage and update the last used time, s
o we can guarantee |
| 78 // that a web app which appears in the registry will have a |
| 79 // last used time != WebappDataStorage.LAST_USED_INVALID. |
64 WebappDataStorage storage = new WebappDataStorage(context, webap
pId); | 80 WebappDataStorage storage = new WebappDataStorage(context, webap
pId); |
65 storage.setScope(scope); | |
66 storage.updateLastUsedTime(); | 81 storage.updateLastUsedTime(); |
67 preferences.edit().putStringSet(KEY_WEBAPP_SET, webapps).apply()
; | 82 return storage; |
68 return null; | 83 } |
| 84 |
| 85 @Override |
| 86 protected final void onPostExecute(WebappDataStorage storage) { |
| 87 if (callback != null) callback.onWebappDataStorageRetrieved(stor
age); |
69 } | 88 } |
70 }.execute(); | 89 }.execute(); |
71 } | 90 } |
72 | 91 |
| 92 /** |
| 93 * Runs the callback, supplying the WebappDataStorage object for webappId, o
r null if the web |
| 94 * app has not been registered. |
| 95 * @param context Context to open the registry with. |
| 96 * @param webappId The id of the web app to register. |
| 97 * @return The storage object for the web app, or null if webappId is not re
gistered. |
| 98 */ |
| 99 public static void getWebappDataStorage(final Context context, final String
webappId, |
| 100 final FetchWebappDataStorageCallback callback) { |
| 101 new AsyncTask<Void, Void, WebappDataStorage>() { |
| 102 @Override |
| 103 protected final WebappDataStorage doInBackground(Void... nothing) { |
| 104 SharedPreferences preferences = openSharedPreferences(context); |
| 105 if (getRegisteredWebappIds(preferences).contains(webappId)) { |
| 106 WebappDataStorage storage = WebappDataStorage.open(context,
webappId); |
| 107 storage.updateLastUsedTime(); |
| 108 return storage; |
| 109 } |
| 110 return null; |
| 111 } |
| 112 |
| 113 @Override |
| 114 protected final void onPostExecute(WebappDataStorage storage) { |
| 115 assert callback != null; |
| 116 callback.onWebappDataStorageRetrieved(storage); |
| 117 } |
| 118 }.execute(); |
| 119 } |
| 120 |
73 /** | 121 /** |
74 * Asynchronously retrieves the list of web app IDs which this registry is a
ware of. | 122 * Asynchronously retrieves the list of web app IDs which this registry is a
ware of. |
75 * @param context Context to open the registry with. | 123 * @param context Context to open the registry with. |
76 * @param callback Called when the set has been retrieved. The set may be em
pty. | 124 * @param callback Called when the set has been retrieved. The set may be em
pty. |
77 */ | 125 */ |
78 @VisibleForTesting | 126 @VisibleForTesting |
79 public static void getRegisteredWebappIds(final Context context, final Fetch
Callback callback) { | 127 public static void getRegisteredWebappIds(final Context context, final Fetch
Callback callback) { |
80 new AsyncTask<Void, Void, Set<String>>() { | 128 new AsyncTask<Void, Void, Set<String>>() { |
81 @Override | 129 @Override |
82 protected final Set<String> doInBackground(Void... nothing) { | 130 protected final Set<String> doInBackground(Void... nothing) { |
83 return getRegisteredWebappIds(openSharedPreferences(context)); | 131 return getRegisteredWebappIds(openSharedPreferences(context)); |
84 } | 132 } |
85 | 133 |
86 @Override | 134 @Override |
87 protected final void onPostExecute(Set<String> result) { | 135 protected final void onPostExecute(Set<String> result) { |
| 136 assert callback != null; |
88 callback.onWebappIdsRetrieved(result); | 137 callback.onWebappIdsRetrieved(result); |
89 } | 138 } |
90 }.execute(); | 139 }.execute(); |
91 } | 140 } |
92 | 141 |
93 /** | 142 /** |
94 * Deletes the data for all "old" web apps. | 143 * Deletes the data for all "old" web apps. |
95 * "Old" web apps have not been opened by the user in the last 3 months, or
have had their last | 144 * "Old" web apps have not been opened by the user in the last 3 months, or
have had their last |
96 * used time set to 0 by the user clearing their history. Cleanup is run, at
most, once a month. | 145 * used time set to 0 by the user clearing their history. Cleanup is run, at
most, once a month. |
97 * | 146 * |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 static void unregisterAllWebapps(Context context, final long callbackPointer
) { | 203 static void unregisterAllWebapps(Context context, final long callbackPointer
) { |
155 unregisterAllWebapps(context, new Runnable() { | 204 unregisterAllWebapps(context, new Runnable() { |
156 @Override | 205 @Override |
157 public void run() { | 206 public void run() { |
158 nativeOnWebappsUnregistered(callbackPointer); | 207 nativeOnWebappsUnregistered(callbackPointer); |
159 } | 208 } |
160 }); | 209 }); |
161 } | 210 } |
162 | 211 |
163 /** | 212 /** |
164 * Deletes the scope and sets the last used time to 0 for all web apps. | 213 * Deletes the URL and scope, and sets the last used time to 0 for all web a
pps. |
165 */ | 214 */ |
166 @VisibleForTesting | 215 @VisibleForTesting |
167 static void clearWebappHistory(final Context context, final Runnable callbac
k) { | 216 static void clearWebappHistory(final Context context, final Runnable callbac
k) { |
168 new AsyncTask<Void, Void, Void>() { | 217 new AsyncTask<Void, Void, Void>() { |
169 @Override | 218 @Override |
170 protected final Void doInBackground(Void... nothing) { | 219 protected final Void doInBackground(Void... nothing) { |
171 SharedPreferences preferences = openSharedPreferences(context); | 220 SharedPreferences preferences = openSharedPreferences(context); |
172 for (String id : getRegisteredWebappIds(preferences)) { | 221 for (String id : getRegisteredWebappIds(preferences)) { |
173 WebappDataStorage.clearHistory(context, id); | 222 WebappDataStorage.clearHistory(context, id); |
174 } | 223 } |
(...skipping 27 matching lines...) Expand all Loading... |
202 return Collections.unmodifiableSet( | 251 return Collections.unmodifiableSet( |
203 preferences.getStringSet(KEY_WEBAPP_SET, Collections.<String>emp
tySet())); | 252 preferences.getStringSet(KEY_WEBAPP_SET, Collections.<String>emp
tySet())); |
204 } | 253 } |
205 | 254 |
206 private WebappRegistry() { | 255 private WebappRegistry() { |
207 } | 256 } |
208 | 257 |
209 private static native void nativeOnWebappsUnregistered(long callbackPointer)
; | 258 private static native void nativeOnWebappsUnregistered(long callbackPointer)
; |
210 private static native void nativeOnClearedWebappHistory(long callbackPointer
); | 259 private static native void nativeOnClearedWebappHistory(long callbackPointer
); |
211 } | 260 } |
OLD | NEW |