Ben Murdoch | 7df1985 | 2009-04-22 13:07:58 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.webkit; |
| 18 | |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 19 | import android.os.Handler; |
| 20 | import android.os.Message; |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 21 | |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 22 | import java.util.Collection; |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 23 | import java.util.HashMap; |
Jonathan Dixon | 36196b6 | 2012-03-28 16:40:37 +0100 | [diff] [blame^] | 24 | import java.util.Map; |
Andrei Popescu | af9c77e | 2009-07-21 17:02:35 +0100 | [diff] [blame] | 25 | import java.util.Set; |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 26 | |
Ben Murdoch | 7df1985 | 2009-04-22 13:07:58 +0100 | [diff] [blame] | 27 | /** |
| 28 | * Functionality for manipulating the webstorage databases. |
| 29 | */ |
| 30 | public final class WebStorage { |
| 31 | |
| 32 | /** |
| 33 | * Encapsulates a callback function to be executed when a new quota is made |
| 34 | * available. We primarily want this to allow us to call back the sleeping |
| 35 | * WebCore thread from outside the WebViewCore class (as the native call |
| 36 | * is private). It is imperative that this the setDatabaseQuota method is |
| 37 | * executed once a decision to either allow or deny new quota is made, |
| 38 | * otherwise the WebCore thread will remain asleep. |
| 39 | */ |
| 40 | public interface QuotaUpdater { |
| 41 | public void updateQuota(long newQuota); |
| 42 | }; |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 43 | |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 44 | // Global instance of a WebStorage |
| 45 | private static WebStorage sWebStorage; |
| 46 | |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 47 | // Message ids |
| 48 | static final int UPDATE = 0; |
| 49 | static final int SET_QUOTA_ORIGIN = 1; |
| 50 | static final int DELETE_ORIGIN = 2; |
| 51 | static final int DELETE_ALL = 3; |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 52 | static final int GET_ORIGINS = 4; |
| 53 | static final int GET_USAGE_ORIGIN = 5; |
| 54 | static final int GET_QUOTA_ORIGIN = 6; |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 55 | |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 56 | // Message ids on the UI thread |
| 57 | static final int RETURN_ORIGINS = 0; |
| 58 | static final int RETURN_USAGE_ORIGIN = 1; |
| 59 | static final int RETURN_QUOTA_ORIGIN = 2; |
| 60 | |
| 61 | private static final String ORIGINS = "origins"; |
| 62 | private static final String ORIGIN = "origin"; |
| 63 | private static final String CALLBACK = "callback"; |
| 64 | private static final String USAGE = "usage"; |
| 65 | private static final String QUOTA = "quota"; |
| 66 | |
| 67 | private Map <String, Origin> mOrigins; |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 68 | |
| 69 | private Handler mHandler = null; |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 70 | private Handler mUIHandler = null; |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 71 | |
John Reck | 9b2a59b | 2011-01-24 17:31:39 -0800 | [diff] [blame] | 72 | /** |
| 73 | * Class containing the HTML5 database quota and usage for an origin. |
| 74 | */ |
John Reck | 87745ce | 2010-11-30 14:00:54 -0800 | [diff] [blame] | 75 | public static class Origin { |
| 76 | private String mOrigin = null; |
| 77 | private long mQuota = 0; |
| 78 | private long mUsage = 0; |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 79 | |
John Reck | 87745ce | 2010-11-30 14:00:54 -0800 | [diff] [blame] | 80 | private Origin(String origin, long quota, long usage) { |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 81 | mOrigin = origin; |
| 82 | mQuota = quota; |
| 83 | mUsage = usage; |
| 84 | } |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 85 | |
John Reck | 87745ce | 2010-11-30 14:00:54 -0800 | [diff] [blame] | 86 | private Origin(String origin, long quota) { |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 87 | mOrigin = origin; |
| 88 | mQuota = quota; |
| 89 | } |
| 90 | |
John Reck | 87745ce | 2010-11-30 14:00:54 -0800 | [diff] [blame] | 91 | private Origin(String origin) { |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 92 | mOrigin = origin; |
| 93 | } |
| 94 | |
John Reck | 9b2a59b | 2011-01-24 17:31:39 -0800 | [diff] [blame] | 95 | /** |
| 96 | * An origin string is created using WebCore::SecurityOrigin::toString(). |
| 97 | * Note that WebCore::SecurityOrigin uses 0 (which is not printed) for |
| 98 | * the port if the port is the default for the protocol. Eg |
| 99 | * http://www.google.com and http://www.google.com:80 both record a port |
| 100 | * of 0 and hence toString() == 'http://www.google.com' for both. |
| 101 | * @return The origin string. |
| 102 | */ |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 103 | public String getOrigin() { |
| 104 | return mOrigin; |
| 105 | } |
| 106 | |
John Reck | 9b2a59b | 2011-01-24 17:31:39 -0800 | [diff] [blame] | 107 | /** |
| 108 | * Returns the quota for this origin's HTML5 database. |
| 109 | * @return The quota in bytes. |
| 110 | */ |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 111 | public long getQuota() { |
| 112 | return mQuota; |
| 113 | } |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 114 | |
John Reck | 9b2a59b | 2011-01-24 17:31:39 -0800 | [diff] [blame] | 115 | /** |
| 116 | * Returns the usage for this origin's HTML5 database. |
| 117 | * @return The usage in bytes. |
| 118 | */ |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 119 | public long getUsage() { |
| 120 | return mUsage; |
| 121 | } |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /** |
| 125 | * @hide |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 126 | * Message handler, UI side |
| 127 | */ |
| 128 | public void createUIHandler() { |
| 129 | if (mUIHandler == null) { |
| 130 | mUIHandler = new Handler() { |
| 131 | @Override |
| 132 | public void handleMessage(Message msg) { |
| 133 | switch (msg.what) { |
| 134 | case RETURN_ORIGINS: { |
| 135 | Map values = (Map) msg.obj; |
| 136 | Map origins = (Map) values.get(ORIGINS); |
| 137 | ValueCallback<Map> callback = (ValueCallback<Map>) values.get(CALLBACK); |
| 138 | callback.onReceiveValue(origins); |
| 139 | } break; |
| 140 | |
| 141 | case RETURN_USAGE_ORIGIN: { |
| 142 | Map values = (Map) msg.obj; |
| 143 | ValueCallback<Long> callback = (ValueCallback<Long>) values.get(CALLBACK); |
| 144 | callback.onReceiveValue((Long)values.get(USAGE)); |
| 145 | } break; |
| 146 | |
| 147 | case RETURN_QUOTA_ORIGIN: { |
| 148 | Map values = (Map) msg.obj; |
| 149 | ValueCallback<Long> callback = (ValueCallback<Long>) values.get(CALLBACK); |
| 150 | callback.onReceiveValue((Long)values.get(QUOTA)); |
| 151 | } break; |
| 152 | } |
| 153 | } |
| 154 | }; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @hide |
| 160 | * Message handler, webcore side |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 161 | */ |
Steve Block | e4b2d4d | 2010-02-19 12:21:35 +0000 | [diff] [blame] | 162 | public synchronized void createHandler() { |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 163 | if (mHandler == null) { |
| 164 | mHandler = new Handler() { |
| 165 | @Override |
| 166 | public void handleMessage(Message msg) { |
| 167 | switch (msg.what) { |
| 168 | case SET_QUOTA_ORIGIN: { |
| 169 | Origin website = (Origin) msg.obj; |
| 170 | nativeSetQuotaForOrigin(website.getOrigin(), |
| 171 | website.getQuota()); |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 172 | } break; |
| 173 | |
| 174 | case DELETE_ORIGIN: { |
| 175 | Origin website = (Origin) msg.obj; |
| 176 | nativeDeleteOrigin(website.getOrigin()); |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 177 | } break; |
| 178 | |
| 179 | case DELETE_ALL: |
Andrei Popescu | af9c77e | 2009-07-21 17:02:35 +0100 | [diff] [blame] | 180 | nativeDeleteAllData(); |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 181 | break; |
| 182 | |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 183 | case GET_ORIGINS: { |
| 184 | syncValues(); |
| 185 | ValueCallback callback = (ValueCallback) msg.obj; |
| 186 | Map origins = new HashMap(mOrigins); |
| 187 | Map values = new HashMap<String, Object>(); |
| 188 | values.put(CALLBACK, callback); |
| 189 | values.put(ORIGINS, origins); |
| 190 | postUIMessage(Message.obtain(null, RETURN_ORIGINS, values)); |
| 191 | } break; |
| 192 | |
| 193 | case GET_USAGE_ORIGIN: { |
| 194 | syncValues(); |
| 195 | Map values = (Map) msg.obj; |
| 196 | String origin = (String) values.get(ORIGIN); |
| 197 | ValueCallback callback = (ValueCallback) values.get(CALLBACK); |
| 198 | Origin website = mOrigins.get(origin); |
| 199 | Map retValues = new HashMap<String, Object>(); |
| 200 | retValues.put(CALLBACK, callback); |
| 201 | if (website != null) { |
| 202 | long usage = website.getUsage(); |
| 203 | retValues.put(USAGE, new Long(usage)); |
| 204 | } |
| 205 | postUIMessage(Message.obtain(null, RETURN_USAGE_ORIGIN, retValues)); |
| 206 | } break; |
| 207 | |
| 208 | case GET_QUOTA_ORIGIN: { |
| 209 | syncValues(); |
| 210 | Map values = (Map) msg.obj; |
| 211 | String origin = (String) values.get(ORIGIN); |
| 212 | ValueCallback callback = (ValueCallback) values.get(CALLBACK); |
| 213 | Origin website = mOrigins.get(origin); |
| 214 | Map retValues = new HashMap<String, Object>(); |
| 215 | retValues.put(CALLBACK, callback); |
| 216 | if (website != null) { |
| 217 | long quota = website.getQuota(); |
| 218 | retValues.put(QUOTA, new Long(quota)); |
| 219 | } |
| 220 | postUIMessage(Message.obtain(null, RETURN_QUOTA_ORIGIN, retValues)); |
| 221 | } break; |
| 222 | |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 223 | case UPDATE: |
| 224 | syncValues(); |
| 225 | break; |
| 226 | } |
| 227 | } |
| 228 | }; |
| 229 | } |
| 230 | } |
| 231 | |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 232 | /* |
| 233 | * When calling getOrigins(), getUsageForOrigin() and getQuotaForOrigin(), |
| 234 | * we need to get the values from webcore, but we cannot block while doing so |
| 235 | * as we used to do, as this could result in a full deadlock (other webcore |
| 236 | * messages received while we are still blocked here, see http://b/2127737). |
| 237 | * |
| 238 | * We have to do everything asynchronously, by providing a callback function. |
| 239 | * We post a message on the webcore thread (mHandler) that will get the result |
| 240 | * from webcore, and we post it back on the UI thread (using mUIHandler). |
| 241 | * We can then use the callback function to return the value. |
| 242 | */ |
| 243 | |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 244 | /** |
John Reck | 9b2a59b | 2011-01-24 17:31:39 -0800 | [diff] [blame] | 245 | * Returns a list of origins having a database. The Map is of type |
| 246 | * Map<String, Origin>. |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 247 | */ |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 248 | public void getOrigins(ValueCallback<Map> callback) { |
| 249 | if (callback != null) { |
| 250 | if (WebViewCore.THREAD_NAME.equals(Thread.currentThread().getName())) { |
| 251 | syncValues(); |
| 252 | callback.onReceiveValue(mOrigins); |
| 253 | } else { |
| 254 | postMessage(Message.obtain(null, GET_ORIGINS, callback)); |
Andrei Popescu | 59e2ad9 | 2009-07-28 13:38:06 +0100 | [diff] [blame] | 255 | } |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 256 | } |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Returns a list of origins having a database |
| 261 | * should only be called from WebViewCore. |
| 262 | */ |
| 263 | Collection<Origin> getOriginsSync() { |
| 264 | if (WebViewCore.THREAD_NAME.equals(Thread.currentThread().getName())) { |
| 265 | update(); |
| 266 | return mOrigins.values(); |
| 267 | } |
| 268 | return null; |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /** |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 272 | * Returns the use for a given origin |
| 273 | */ |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 274 | public void getUsageForOrigin(String origin, ValueCallback<Long> callback) { |
| 275 | if (callback == null) { |
| 276 | return; |
| 277 | } |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 278 | if (origin == null) { |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 279 | callback.onReceiveValue(null); |
| 280 | return; |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 281 | } |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 282 | if (WebViewCore.THREAD_NAME.equals(Thread.currentThread().getName())) { |
| 283 | syncValues(); |
| 284 | Origin website = mOrigins.get(origin); |
| 285 | callback.onReceiveValue(new Long(website.getUsage())); |
| 286 | } else { |
| 287 | HashMap values = new HashMap<String, Object>(); |
| 288 | values.put(ORIGIN, origin); |
| 289 | values.put(CALLBACK, callback); |
| 290 | postMessage(Message.obtain(null, GET_USAGE_ORIGIN, values)); |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 291 | } |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | /** |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 295 | * Returns the quota for a given origin |
| 296 | */ |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 297 | public void getQuotaForOrigin(String origin, ValueCallback<Long> callback) { |
| 298 | if (callback == null) { |
| 299 | return; |
| 300 | } |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 301 | if (origin == null) { |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 302 | callback.onReceiveValue(null); |
| 303 | return; |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 304 | } |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 305 | if (WebViewCore.THREAD_NAME.equals(Thread.currentThread().getName())) { |
| 306 | syncValues(); |
| 307 | Origin website = mOrigins.get(origin); |
| 308 | callback.onReceiveValue(new Long(website.getUsage())); |
| 309 | } else { |
| 310 | HashMap values = new HashMap<String, Object>(); |
| 311 | values.put(ORIGIN, origin); |
| 312 | values.put(CALLBACK, callback); |
| 313 | postMessage(Message.obtain(null, GET_QUOTA_ORIGIN, values)); |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 314 | } |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | /** |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 318 | * Set the quota for a given origin |
| 319 | */ |
| 320 | public void setQuotaForOrigin(String origin, long quota) { |
| 321 | if (origin != null) { |
Ben Murdoch | 5647bb3 | 2009-06-18 17:48:34 +0100 | [diff] [blame] | 322 | if (WebViewCore.THREAD_NAME.equals(Thread.currentThread().getName())) { |
| 323 | nativeSetQuotaForOrigin(origin, quota); |
Ben Murdoch | 5647bb3 | 2009-06-18 17:48:34 +0100 | [diff] [blame] | 324 | } else { |
| 325 | postMessage(Message.obtain(null, SET_QUOTA_ORIGIN, |
| 326 | new Origin(origin, quota))); |
| 327 | } |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | |
| 331 | /** |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 332 | * Delete a given origin |
| 333 | */ |
| 334 | public void deleteOrigin(String origin) { |
| 335 | if (origin != null) { |
Ben Murdoch | 5647bb3 | 2009-06-18 17:48:34 +0100 | [diff] [blame] | 336 | if (WebViewCore.THREAD_NAME.equals(Thread.currentThread().getName())) { |
| 337 | nativeDeleteOrigin(origin); |
Ben Murdoch | 5647bb3 | 2009-06-18 17:48:34 +0100 | [diff] [blame] | 338 | } else { |
| 339 | postMessage(Message.obtain(null, DELETE_ORIGIN, |
| 340 | new Origin(origin))); |
| 341 | } |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | |
| 345 | /** |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 346 | * Delete all databases |
| 347 | */ |
Andrei Popescu | af9c77e | 2009-07-21 17:02:35 +0100 | [diff] [blame] | 348 | public void deleteAllData() { |
Ben Murdoch | 5647bb3 | 2009-06-18 17:48:34 +0100 | [diff] [blame] | 349 | if (WebViewCore.THREAD_NAME.equals(Thread.currentThread().getName())) { |
Andrei Popescu | af9c77e | 2009-07-21 17:02:35 +0100 | [diff] [blame] | 350 | nativeDeleteAllData(); |
Ben Murdoch | 5647bb3 | 2009-06-18 17:48:34 +0100 | [diff] [blame] | 351 | } else { |
| 352 | postMessage(Message.obtain(null, DELETE_ALL)); |
| 353 | } |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | /** |
Steve Block | 427efcc | 2010-03-19 15:26:42 +0000 | [diff] [blame] | 357 | * Sets the maximum size of the ApplicationCache. |
| 358 | * This should only ever be called on the WebKit thread. |
Jonathan Dixon | 4cde13a | 2011-12-16 18:36:20 +0000 | [diff] [blame] | 359 | * @hide |
Steve Block | 427efcc | 2010-03-19 15:26:42 +0000 | [diff] [blame] | 360 | */ |
| 361 | public void setAppCacheMaximumSize(long size) { |
| 362 | nativeSetAppCacheMaximumSize(size); |
| 363 | } |
| 364 | |
| 365 | /** |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 366 | * Utility function to send a message to our handler |
| 367 | */ |
Steve Block | e4b2d4d | 2010-02-19 12:21:35 +0000 | [diff] [blame] | 368 | private synchronized void postMessage(Message msg) { |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 369 | if (mHandler != null) { |
| 370 | mHandler.sendMessage(msg); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | /** |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 375 | * Utility function to send a message to the handler on the UI thread |
| 376 | */ |
| 377 | private void postUIMessage(Message msg) { |
| 378 | if (mUIHandler != null) { |
| 379 | mUIHandler.sendMessage(msg); |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | /** |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 384 | * Get the global instance of WebStorage. |
| 385 | * @return A single instance of WebStorage. |
| 386 | */ |
| 387 | public static WebStorage getInstance() { |
| 388 | if (sWebStorage == null) { |
| 389 | sWebStorage = new WebStorage(); |
| 390 | } |
| 391 | return sWebStorage; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * @hide |
| 396 | * Post a Sync request |
| 397 | */ |
| 398 | public void update() { |
Ben Murdoch | 5647bb3 | 2009-06-18 17:48:34 +0100 | [diff] [blame] | 399 | if (WebViewCore.THREAD_NAME.equals(Thread.currentThread().getName())) { |
| 400 | syncValues(); |
| 401 | } else { |
| 402 | postMessage(Message.obtain(null, UPDATE)); |
| 403 | } |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | /** |
| 407 | * Run on the webcore thread |
Andrei Popescu | 9a56bb8 | 2009-07-22 19:21:48 +0100 | [diff] [blame] | 408 | * set the local values with the current ones |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 409 | */ |
| 410 | private void syncValues() { |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 411 | Set<String> tmp = nativeGetOrigins(); |
| 412 | mOrigins = new HashMap<String, Origin>(); |
| 413 | for (String origin : tmp) { |
| 414 | Origin website = new Origin(origin, |
Andrei Popescu | 9840970 | 2010-01-08 16:41:23 +0000 | [diff] [blame] | 415 | nativeGetQuotaForOrigin(origin), |
| 416 | nativeGetUsageForOrigin(origin)); |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 417 | mOrigins.put(origin, website); |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 418 | } |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 419 | } |
| 420 | |
| 421 | // Native functions |
Andrei Popescu | af9c77e | 2009-07-21 17:02:35 +0100 | [diff] [blame] | 422 | private static native Set nativeGetOrigins(); |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 423 | private static native long nativeGetUsageForOrigin(String origin); |
| 424 | private static native long nativeGetQuotaForOrigin(String origin); |
| 425 | private static native void nativeSetQuotaForOrigin(String origin, long quota); |
| 426 | private static native void nativeDeleteOrigin(String origin); |
Andrei Popescu | af9c77e | 2009-07-21 17:02:35 +0100 | [diff] [blame] | 427 | private static native void nativeDeleteAllData(); |
Steve Block | 427efcc | 2010-03-19 15:26:42 +0000 | [diff] [blame] | 428 | private static native void nativeSetAppCacheMaximumSize(long size); |
Ben Murdoch | 7df1985 | 2009-04-22 13:07:58 +0100 | [diff] [blame] | 429 | } |