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. |
gone
2016/04/01 23:44:48
nit: IDs
dominickn
2016/04/04 07:26:24
Done.
| |
42 */ | 42 */ |
43 public interface FetchCallback { | 43 public interface FetchCallback { |
44 public void onWebappIdsRetrieved(Set<String> readObject); | 44 public 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 public void onWebappDataStorageRetrieved(WebappDataStorage storage); | |
53 } | |
54 | |
55 /** | |
56 * Registers the existence of a web app, creates the SharedPreference for it , and returns the | |
57 * resulting WebappDataStorage 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 * @return The storage object for the web app. | |
51 */ | 61 */ |
52 public static void registerWebapp(final Context context, final String webapp Id, | 62 public static WebappDataStorage registerWebapp(final Context context, final String webappId) { |
53 final String scope) { | 63 final WebappDataStorage storage = new WebappDataStorage(context, webappI d); |
54 new AsyncTask<Void, Void, Void>() { | 64 new AsyncTask<Void, Void, Void>() { |
55 @Override | 65 @Override |
56 protected final Void doInBackground(Void... nothing) { | 66 protected final Void doInBackground(Void... nothing) { |
57 SharedPreferences preferences = openSharedPreferences(context); | 67 SharedPreferences preferences = openSharedPreferences(context); |
68 // The set returned by getRegisteredWebappIds must be treated as immutable, so we | |
69 // make a copy to edit and save. | |
58 Set<String> webapps = new HashSet<String>(getRegisteredWebappIds (preferences)); | 70 Set<String> webapps = new HashSet<String>(getRegisteredWebappIds (preferences)); |
59 boolean added = webapps.add(webappId); | 71 boolean added = webapps.add(webappId); |
60 assert added; | 72 assert added; |
61 | 73 |
74 preferences.edit().putStringSet(KEY_WEBAPP_SET, webapps).apply() ; | |
75 | |
62 // Update the last used time, so we can guarantee that a web app which appears in | 76 // Update the last used time, so we can guarantee that a web app which appears in |
63 // the registry will have a last used time != WebappDataStorage. LAST_USED_INVALID. | 77 // the registry will have a last used time != WebappDataStorage. LAST_USED_INVALID. |
64 WebappDataStorage storage = new WebappDataStorage(context, webap pId); | |
65 storage.setScope(scope); | |
66 storage.updateLastUsedTime(); | 78 storage.updateLastUsedTime(); |
67 preferences.edit().putStringSet(KEY_WEBAPP_SET, webapps).apply() ; | |
68 return null; | 79 return null; |
69 } | 80 } |
70 }.execute(); | 81 }.execute(); |
82 | |
83 return storage; | |
84 } | |
85 | |
86 /** | |
87 * Runs the callback, supplying the WebappDataStorage object for webappId, o r null if the web | |
88 * app has not been registered. | |
89 * @param context Context to open the registry with. | |
90 * @param webappId The id of the web app to register. | |
91 * @return The storage object for the web app, or null if webappId is not re gistered. | |
92 */ | |
93 public static void getWebappDataStorage(final Context context, final String webappId, | |
94 final FetchWebappDataStorageCallback callback) { | |
95 new AsyncTask<Void, Void, Boolean>() { | |
96 @Override | |
97 protected final Boolean doInBackground(Void... nothing) { | |
98 SharedPreferences preferences = openSharedPreferences(context); | |
99 return getRegisteredWebappIds(preferences).contains(webappId); | |
100 } | |
101 | |
102 protected final void onPostExecute(Boolean exists) { | |
103 WebappDataStorage storage = null; | |
104 if (exists) { | |
105 storage = WebappDataStorage.open(context, webappId); | |
106 } | |
107 callback.onWebappDataStorageRetrieved(storage); | |
108 } | |
109 }.execute(); | |
71 } | 110 } |
72 | 111 |
73 /** | 112 /** |
74 * Asynchronously retrieves the list of web app IDs which this registry is a ware of. | 113 * Asynchronously retrieves the list of web app IDs which this registry is a ware of. |
75 * @param context Context to open the registry with. | 114 * @param context Context to open the registry with. |
76 * @param callback Called when the set has been retrieved. The set may be em pty. | 115 * @param callback Called when the set has been retrieved. The set may be em pty. |
77 */ | 116 */ |
78 @VisibleForTesting | 117 @VisibleForTesting |
79 public static void getRegisteredWebappIds(final Context context, final Fetch Callback callback) { | 118 public static void getRegisteredWebappIds(final Context context, final Fetch Callback callback) { |
80 new AsyncTask<Void, Void, Set<String>>() { | 119 new AsyncTask<Void, Void, Set<String>>() { |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
200 private static Set<String> getRegisteredWebappIds(SharedPreferences preferen ces) { | 239 private static Set<String> getRegisteredWebappIds(SharedPreferences preferen ces) { |
201 return preferences.getStringSet(KEY_WEBAPP_SET, Collections.<String>empt ySet()); | 240 return preferences.getStringSet(KEY_WEBAPP_SET, Collections.<String>empt ySet()); |
202 } | 241 } |
203 | 242 |
204 private WebappRegistry() { | 243 private WebappRegistry() { |
205 } | 244 } |
206 | 245 |
207 private static native void nativeOnWebappsUnregistered(long callbackPointer) ; | 246 private static native void nativeOnWebappsUnregistered(long callbackPointer) ; |
208 private static native void nativeOnClearedWebappHistory(long callbackPointer ); | 247 private static native void nativeOnClearedWebappHistory(long callbackPointer ); |
209 } | 248 } |
OLD | NEW |