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 | |
Jonathan Dixon | 36196b6 | 2012-03-28 16:40:37 +0100 | [diff] [blame] | 19 | import java.util.Map; |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 20 | |
Ben Murdoch | 7df1985 | 2009-04-22 13:07:58 +0100 | [diff] [blame] | 21 | /** |
Steve Block | 285ddfc | 2012-03-28 12:46:02 +0100 | [diff] [blame] | 22 | * This class is used to manage the JavaScript storage APIs provided by the |
| 23 | * {@link WebView}. It manages the Application Cache API, the Web SQL Database |
| 24 | * API and the HTML5 Web Storage API. |
| 25 | * |
Selim Gurun | b632adf | 2012-06-28 11:21:05 -0700 | [diff] [blame] | 26 | * The Application Cache API provides a mechanism to create and maintain an |
| 27 | * application cache to power offline Web applications. Use of the Application |
| 28 | * Cache API can be attributed to an origin {@link WebStorage.Origin}, however |
| 29 | * it is not possible to set per-origin quotas. Note that there can be only |
| 30 | * one application cache per application. |
| 31 | * |
| 32 | * The Web SQL Database API provides storage which is private to a given origin. |
| 33 | * Similar to the Application Cache, use of the Web SQL Database can be attributed |
| 34 | * to an origin. It is also possible to set per-origin quotas. |
Ben Murdoch | 7df1985 | 2009-04-22 13:07:58 +0100 | [diff] [blame] | 35 | */ |
Jonathan Dixon | 939e504 | 2012-04-12 21:21:07 +0100 | [diff] [blame] | 36 | public class WebStorage { |
Ben Murdoch | 7df1985 | 2009-04-22 13:07:58 +0100 | [diff] [blame] | 37 | |
| 38 | /** |
Steve Block | 285ddfc | 2012-03-28 12:46:02 +0100 | [diff] [blame] | 39 | * Encapsulates a callback function which is used to provide a new quota |
Selim Gurun | b632adf | 2012-06-28 11:21:05 -0700 | [diff] [blame] | 40 | * for a JavaScript storage API. |
| 41 | * See |
Steve Block | 285ddfc | 2012-03-28 12:46:02 +0100 | [diff] [blame] | 42 | * {@link WebChromeClient#onExceededDatabaseQuota} and |
| 43 | * {@link WebChromeClient#onReachedMaxAppCacheSize}. |
Ben Murdoch | 7df1985 | 2009-04-22 13:07:58 +0100 | [diff] [blame] | 44 | */ |
Steve Block | 285ddfc | 2012-03-28 12:46:02 +0100 | [diff] [blame] | 45 | // We primarily want this to allow us to call back the sleeping WebCore |
| 46 | // thread from outside the WebViewCore class (as the native call is |
| 47 | // private). It is imperative that the setDatabaseQuota method is |
| 48 | // executed after a decision to either allow or deny new quota is made, |
| 49 | // otherwise the WebCore thread will remain asleep. |
Ben Murdoch | 7df1985 | 2009-04-22 13:07:58 +0100 | [diff] [blame] | 50 | public interface QuotaUpdater { |
Steve Block | 285ddfc | 2012-03-28 12:46:02 +0100 | [diff] [blame] | 51 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 52 | * Provides a new quota, specified in bytes. |
| 53 | * |
| 54 | * @param newQuota the new quota, in bytes |
Steve Block | 285ddfc | 2012-03-28 12:46:02 +0100 | [diff] [blame] | 55 | */ |
Ben Murdoch | 7df1985 | 2009-04-22 13:07:58 +0100 | [diff] [blame] | 56 | public void updateQuota(long newQuota); |
| 57 | }; |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 58 | |
John Reck | 9b2a59b | 2011-01-24 17:31:39 -0800 | [diff] [blame] | 59 | /** |
Steve Block | 285ddfc | 2012-03-28 12:46:02 +0100 | [diff] [blame] | 60 | * This class encapsulates information about the amount of storage |
| 61 | * currently used by an origin for the JavaScript storage APIs. |
Selim Gurun | b632adf | 2012-06-28 11:21:05 -0700 | [diff] [blame] | 62 | * An origin comprises the host, scheme and port of a URI. |
Steve Block | 285ddfc | 2012-03-28 12:46:02 +0100 | [diff] [blame] | 63 | * See {@link WebStorage} for details. |
John Reck | 9b2a59b | 2011-01-24 17:31:39 -0800 | [diff] [blame] | 64 | */ |
John Reck | 87745ce | 2010-11-30 14:00:54 -0800 | [diff] [blame] | 65 | public static class Origin { |
| 66 | private String mOrigin = null; |
| 67 | private long mQuota = 0; |
| 68 | private long mUsage = 0; |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 69 | |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 70 | /** @hide */ |
| 71 | protected Origin(String origin, long quota, long usage) { |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 72 | mOrigin = origin; |
| 73 | mQuota = quota; |
| 74 | mUsage = usage; |
| 75 | } |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 76 | |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 77 | /** @hide */ |
| 78 | protected Origin(String origin, long quota) { |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 79 | mOrigin = origin; |
| 80 | mQuota = quota; |
| 81 | } |
| 82 | |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 83 | /** @hide */ |
| 84 | protected Origin(String origin) { |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 85 | mOrigin = origin; |
| 86 | } |
| 87 | |
John Reck | 9b2a59b | 2011-01-24 17:31:39 -0800 | [diff] [blame] | 88 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 89 | * Gets the string representation of this origin. |
| 90 | * |
| 91 | * @return the string representation of this origin |
John Reck | 9b2a59b | 2011-01-24 17:31:39 -0800 | [diff] [blame] | 92 | */ |
Steve Block | 285ddfc | 2012-03-28 12:46:02 +0100 | [diff] [blame] | 93 | // An origin string is created using WebCore::SecurityOrigin::toString(). |
| 94 | // Note that WebCore::SecurityOrigin uses 0 (which is not printed) for |
| 95 | // the port if the port is the default for the protocol. Eg |
| 96 | // http://www.google.com and http://www.google.com:80 both record a port |
| 97 | // of 0 and hence toString() == 'http://www.google.com' for both. |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 98 | public String getOrigin() { |
| 99 | return mOrigin; |
| 100 | } |
| 101 | |
John Reck | 9b2a59b | 2011-01-24 17:31:39 -0800 | [diff] [blame] | 102 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 103 | * Gets the quota for this origin, for the Web SQL Database API, in |
Steve Block | 285ddfc | 2012-03-28 12:46:02 +0100 | [diff] [blame] | 104 | * bytes. If this origin does not use the Web SQL Database API, this |
| 105 | * quota will be set to zero. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 106 | * |
| 107 | * @return the quota, in bytes |
John Reck | 9b2a59b | 2011-01-24 17:31:39 -0800 | [diff] [blame] | 108 | */ |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 109 | public long getQuota() { |
| 110 | return mQuota; |
| 111 | } |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 112 | |
John Reck | 9b2a59b | 2011-01-24 17:31:39 -0800 | [diff] [blame] | 113 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 114 | * Gets the total amount of storage currently being used by this origin, |
Steve Block | 285ddfc | 2012-03-28 12:46:02 +0100 | [diff] [blame] | 115 | * for all JavaScript storage APIs, in bytes. |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 116 | * |
| 117 | * @return the total amount of storage, in bytes |
John Reck | 9b2a59b | 2011-01-24 17:31:39 -0800 | [diff] [blame] | 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 | |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 124 | /* |
| 125 | * When calling getOrigins(), getUsageForOrigin() and getQuotaForOrigin(), |
Steve Block | 285ddfc | 2012-03-28 12:46:02 +0100 | [diff] [blame] | 126 | * we need to get the values from WebCore, but we cannot block while doing so |
| 127 | * as we used to do, as this could result in a full deadlock (other WebCore |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 128 | * messages received while we are still blocked here, see http://b/2127737). |
| 129 | * |
| 130 | * We have to do everything asynchronously, by providing a callback function. |
Steve Block | 285ddfc | 2012-03-28 12:46:02 +0100 | [diff] [blame] | 131 | * We post a message on the WebCore thread (mHandler) that will get the result |
| 132 | * from WebCore, and we post it back on the UI thread (using mUIHandler). |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 133 | * We can then use the callback function to return the value. |
| 134 | */ |
| 135 | |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 136 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 137 | * Gets the origins currently using either the Application Cache or Web SQL |
Steve Block | 285ddfc | 2012-03-28 12:46:02 +0100 | [diff] [blame] | 138 | * Database APIs. This method operates asynchronously, with the result |
| 139 | * being provided via a {@link ValueCallback}. The origins are provided as |
| 140 | * a map, of type {@code Map<String, WebStorage.Origin>}, from the string |
| 141 | * representation of the origin to a {@link WebStorage.Origin} object. |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 142 | */ |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 143 | public void getOrigins(ValueCallback<Map> callback) { |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 144 | // Must be a no-op for backward compatibility: see the hidden constructor for reason. |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 148 | * Gets the amount of storage currently being used by both the Application |
Steve Block | 285ddfc | 2012-03-28 12:46:02 +0100 | [diff] [blame] | 149 | * Cache and Web SQL Database APIs by the given origin. The amount is given |
| 150 | * in bytes and the origin is specified using its string representation. |
| 151 | * This method operates asynchronously, with the result being provided via |
| 152 | * a {@link ValueCallback}. |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 153 | */ |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 154 | public void getUsageForOrigin(String origin, ValueCallback<Long> callback) { |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 155 | // Must be a no-op for backward compatibility: see the hidden constructor for reason. |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 159 | * Gets the storage quota for the Web SQL Database API for the given origin. |
Steve Block | 285ddfc | 2012-03-28 12:46:02 +0100 | [diff] [blame] | 160 | * The quota is given in bytes and the origin is specified using its string |
| 161 | * representation. This method operates asynchronously, with the result |
| 162 | * being provided via a {@link ValueCallback}. Note that a quota is not |
| 163 | * enforced on a per-origin basis for the Application Cache API. |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 164 | */ |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 165 | public void getQuotaForOrigin(String origin, ValueCallback<Long> callback) { |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 166 | // Must be a no-op for backward compatibility: see the hidden constructor for reason. |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 170 | * Sets the storage quota for the Web SQL Database API for the given origin. |
Steve Block | 285ddfc | 2012-03-28 12:46:02 +0100 | [diff] [blame] | 171 | * The quota is specified in bytes and the origin is specified using its string |
| 172 | * representation. Note that a quota is not enforced on a per-origin basis |
| 173 | * for the Application Cache API. |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 174 | */ |
| 175 | public void setQuotaForOrigin(String origin, long quota) { |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 176 | // Must be a no-op for backward compatibility: see the hidden constructor for reason. |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 180 | * Clears the storage currently being used by both the Application Cache and |
Steve Block | 285ddfc | 2012-03-28 12:46:02 +0100 | [diff] [blame] | 181 | * Web SQL Database APIs by the given origin. The origin is specified using |
| 182 | * its string representation. |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 183 | */ |
| 184 | public void deleteOrigin(String origin) { |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 185 | // Must be a no-op for backward compatibility: see the hidden constructor for reason. |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 189 | * Clears all storage currently being used by the JavaScript storage APIs. |
Steve Block | 285ddfc | 2012-03-28 12:46:02 +0100 | [diff] [blame] | 190 | * This includes the Application Cache, Web SQL Database and the HTML5 Web |
| 191 | * Storage APIs. |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 192 | */ |
Andrei Popescu | af9c77e | 2009-07-21 17:02:35 +0100 | [diff] [blame] | 193 | public void deleteAllData() { |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 194 | // Must be a no-op for backward compatibility: see the hidden constructor for reason. |
Nicolas Roard | 6c24b4d | 2009-09-22 18:44:52 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /** |
Steve Block | 4e584df | 2012-04-24 23:12:47 +0100 | [diff] [blame] | 198 | * Gets the singleton instance of this class. |
| 199 | * |
| 200 | * @return the singleton {@link WebStorage} instance |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 201 | */ |
| 202 | public static WebStorage getInstance() { |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 203 | return WebViewFactory.getProvider().getWebStorage(); |
Nicolas Roard | 11e8fe5 | 2009-05-11 15:04:16 +0100 | [diff] [blame] | 204 | } |
| 205 | |
Jonathan Dixon | a3dc86e | 2012-03-28 12:27:36 +0100 | [diff] [blame] | 206 | /** |
| 207 | * This class should not be instantiated directly, applications must only use |
| 208 | * {@link #getInstance()} to obtain the instance. |
| 209 | * Note this constructor was erroneously public and published in SDK levels prior to 16, but |
| 210 | * applications using it would receive a non-functional instance of this class (there was no |
| 211 | * way to call createHandler() and createUIHandler(), so it would not work). |
| 212 | * @hide |
| 213 | */ |
| 214 | public WebStorage() {} |
Ben Murdoch | 7df1985 | 2009-04-22 13:07:58 +0100 | [diff] [blame] | 215 | } |