| 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 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 97 * @return The storage object for the web app, or null if webappId is not re
gistered. | 97 * @return The storage object for the web app, or null if webappId is not re
gistered. |
| 98 */ | 98 */ |
| 99 public static void getWebappDataStorage(final Context context, final String
webappId, | 99 public static void getWebappDataStorage(final Context context, final String
webappId, |
| 100 final FetchWebappDataStorageCallback callback) { | 100 final FetchWebappDataStorageCallback callback) { |
| 101 new AsyncTask<Void, Void, WebappDataStorage>() { | 101 new AsyncTask<Void, Void, WebappDataStorage>() { |
| 102 @Override | 102 @Override |
| 103 protected final WebappDataStorage doInBackground(Void... nothing) { | 103 protected final WebappDataStorage doInBackground(Void... nothing) { |
| 104 SharedPreferences preferences = openSharedPreferences(context); | 104 SharedPreferences preferences = openSharedPreferences(context); |
| 105 if (getRegisteredWebappIds(preferences).contains(webappId)) { | 105 if (getRegisteredWebappIds(preferences).contains(webappId)) { |
| 106 WebappDataStorage storage = WebappDataStorage.open(context,
webappId); | 106 WebappDataStorage storage = WebappDataStorage.open(context,
webappId); |
| 107 storage.updateLastUsedTime(); | |
| 108 return storage; | 107 return storage; |
| 109 } | 108 } |
| 110 return null; | 109 return null; |
| 111 } | 110 } |
| 112 | 111 |
| 113 @Override | 112 @Override |
| 114 protected final void onPostExecute(WebappDataStorage storage) { | 113 protected final void onPostExecute(WebappDataStorage storage) { |
| 115 assert callback != null; | 114 assert callback != null; |
| 116 callback.onWebappDataStorageRetrieved(storage); | 115 callback.onWebappDataStorageRetrieved(storage); |
| 117 } | 116 } |
| 118 }.execute(); | 117 }.execute(); |
| 119 } | 118 } |
| 120 | 119 |
| 121 /** | 120 /** |
| 121 * Runs the callback, supplying the WebappDataStorage object whose scope mos
t closely matches |
| 122 * the provided URL, or null if a matching web app cannot be found. The most
closely matching |
| 123 * scope is the longest scope which has the same prefix as the URL to open. |
| 124 * @param context Context to open the registry with. |
| 125 * @param url The URL to search for. |
| 126 * @return The storage object for the web app, or null if webappId is not re
gistered. |
| 127 */ |
| 128 public static void getWebappDataStorageForUrl(final Context context, final S
tring url, |
| 129 final FetchWebappDataStorageCallback callback) { |
| 130 new AsyncTask<Void, Void, WebappDataStorage>() { |
| 131 @Override |
| 132 protected final WebappDataStorage doInBackground(Void... nothing) { |
| 133 SharedPreferences preferences = openSharedPreferences(context); |
| 134 WebappDataStorage bestMatch = null; |
| 135 int largestOverlap = 0; |
| 136 for (String id : getRegisteredWebappIds(preferences)) { |
| 137 WebappDataStorage storage = WebappDataStorage.open(context,
id); |
| 138 String scope = storage.getScope(); |
| 139 if (url.startsWith(scope) && scope.length() > largestOverlap
) { |
| 140 bestMatch = storage; |
| 141 largestOverlap = scope.length(); |
| 142 } |
| 143 } |
| 144 return bestMatch; |
| 145 } |
| 146 |
| 147 protected final void onPostExecute(WebappDataStorage storage) { |
| 148 assert callback != null; |
| 149 callback.onWebappDataStorageRetrieved(storage); |
| 150 } |
| 151 }.execute(); |
| 152 } |
| 153 |
| 154 /** |
| 122 * Asynchronously retrieves the list of web app IDs which this registry is a
ware of. | 155 * Asynchronously retrieves the list of web app IDs which this registry is a
ware of. |
| 123 * @param context Context to open the registry with. | 156 * @param context Context to open the registry with. |
| 124 * @param callback Called when the set has been retrieved. The set may be em
pty. | 157 * @param callback Called when the set has been retrieved. The set may be em
pty. |
| 125 */ | 158 */ |
| 126 @VisibleForTesting | 159 @VisibleForTesting |
| 127 public static void getRegisteredWebappIds(final Context context, final Fetch
Callback callback) { | 160 public static void getRegisteredWebappIds(final Context context, final Fetch
Callback callback) { |
| 128 new AsyncTask<Void, Void, Set<String>>() { | 161 new AsyncTask<Void, Void, Set<String>>() { |
| 129 @Override | 162 @Override |
| 130 protected final Set<String> doInBackground(Void... nothing) { | 163 protected final Set<String> doInBackground(Void... nothing) { |
| 131 return getRegisteredWebappIds(openSharedPreferences(context)); | 164 return getRegisteredWebappIds(openSharedPreferences(context)); |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 251 return Collections.unmodifiableSet( | 284 return Collections.unmodifiableSet( |
| 252 preferences.getStringSet(KEY_WEBAPP_SET, Collections.<String>emp
tySet())); | 285 preferences.getStringSet(KEY_WEBAPP_SET, Collections.<String>emp
tySet())); |
| 253 } | 286 } |
| 254 | 287 |
| 255 private WebappRegistry() { | 288 private WebappRegistry() { |
| 256 } | 289 } |
| 257 | 290 |
| 258 private static native void nativeOnWebappsUnregistered(long callbackPointer)
; | 291 private static native void nativeOnWebappsUnregistered(long callbackPointer)
; |
| 259 private static native void nativeOnClearedWebappHistory(long callbackPointer
); | 292 private static native void nativeOnClearedWebappHistory(long callbackPointer
); |
| 260 } | 293 } |
| OLD | NEW |