blob: 76674f42194b30d81d4adc8abb2986fabdf47d1f [file] [log] [blame]
Ben Murdoch7df19852009-04-22 13:07:58 +01001/*
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
17package android.webkit;
18
Jonathan Dixon36196b62012-03-28 16:40:37 +010019import java.util.Map;
Nicolas Roard11e8fe52009-05-11 15:04:16 +010020
Ben Murdoch7df19852009-04-22 13:07:58 +010021/**
Steve Block285ddfc2012-03-28 12:46:02 +010022 * 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 *
26 * The Web SQL Database API provides storage which is private to a given
27 * origin, where an origin comprises the host, scheme and port of a URI.
28 * Similarly, use of the Application Cache API can be attributed to an origin.
29 * This class provides access to the storage use and quotas for these APIs for
30 * a given origin. Origins are represented using {@link WebStorage.Origin}.
Ben Murdoch7df19852009-04-22 13:07:58 +010031 */
Jonathan Dixon939e5042012-04-12 21:21:07 +010032public class WebStorage {
Ben Murdoch7df19852009-04-22 13:07:58 +010033
34 /**
Steve Block285ddfc2012-03-28 12:46:02 +010035 * Encapsulates a callback function which is used to provide a new quota
36 * for a JavaScript storage API. See
37 * {@link WebChromeClient#onExceededDatabaseQuota} and
38 * {@link WebChromeClient#onReachedMaxAppCacheSize}.
Ben Murdoch7df19852009-04-22 13:07:58 +010039 */
Steve Block285ddfc2012-03-28 12:46:02 +010040 // We primarily want this to allow us to call back the sleeping WebCore
41 // thread from outside the WebViewCore class (as the native call is
42 // private). It is imperative that the setDatabaseQuota method is
43 // executed after a decision to either allow or deny new quota is made,
44 // otherwise the WebCore thread will remain asleep.
Ben Murdoch7df19852009-04-22 13:07:58 +010045 public interface QuotaUpdater {
Steve Block285ddfc2012-03-28 12:46:02 +010046 /**
Steve Block4e584df2012-04-24 23:12:47 +010047 * Provides a new quota, specified in bytes.
48 *
49 * @param newQuota the new quota, in bytes
Steve Block285ddfc2012-03-28 12:46:02 +010050 */
Ben Murdoch7df19852009-04-22 13:07:58 +010051 public void updateQuota(long newQuota);
52 };
Nicolas Roard11e8fe52009-05-11 15:04:16 +010053
John Reck9b2a59b2011-01-24 17:31:39 -080054 /**
Steve Block285ddfc2012-03-28 12:46:02 +010055 * This class encapsulates information about the amount of storage
56 * currently used by an origin for the JavaScript storage APIs.
57 * See {@link WebStorage} for details.
John Reck9b2a59b2011-01-24 17:31:39 -080058 */
John Reck87745ce2010-11-30 14:00:54 -080059 public static class Origin {
60 private String mOrigin = null;
61 private long mQuota = 0;
62 private long mUsage = 0;
Nicolas Roard6c24b4d2009-09-22 18:44:52 +010063
Jonathan Dixond3101b12012-04-12 20:51:51 +010064 /** @hide */
65 protected Origin(String origin, long quota, long usage) {
Nicolas Roard6c24b4d2009-09-22 18:44:52 +010066 mOrigin = origin;
67 mQuota = quota;
68 mUsage = usage;
69 }
Nicolas Roard11e8fe52009-05-11 15:04:16 +010070
Jonathan Dixond3101b12012-04-12 20:51:51 +010071 /** @hide */
72 protected Origin(String origin, long quota) {
Nicolas Roard11e8fe52009-05-11 15:04:16 +010073 mOrigin = origin;
74 mQuota = quota;
75 }
76
Jonathan Dixond3101b12012-04-12 20:51:51 +010077 /** @hide */
78 protected Origin(String origin) {
Nicolas Roard11e8fe52009-05-11 15:04:16 +010079 mOrigin = origin;
80 }
81
John Reck9b2a59b2011-01-24 17:31:39 -080082 /**
Steve Block4e584df2012-04-24 23:12:47 +010083 * Gets the string representation of this origin.
84 *
85 * @return the string representation of this origin
John Reck9b2a59b2011-01-24 17:31:39 -080086 */
Steve Block285ddfc2012-03-28 12:46:02 +010087 // An origin string is created using WebCore::SecurityOrigin::toString().
88 // Note that WebCore::SecurityOrigin uses 0 (which is not printed) for
89 // the port if the port is the default for the protocol. Eg
90 // http://www.google.com and http://www.google.com:80 both record a port
91 // of 0 and hence toString() == 'http://www.google.com' for both.
Nicolas Roard11e8fe52009-05-11 15:04:16 +010092 public String getOrigin() {
93 return mOrigin;
94 }
95
John Reck9b2a59b2011-01-24 17:31:39 -080096 /**
Steve Block4e584df2012-04-24 23:12:47 +010097 * Gets the quota for this origin, for the Web SQL Database API, in
Steve Block285ddfc2012-03-28 12:46:02 +010098 * bytes. If this origin does not use the Web SQL Database API, this
99 * quota will be set to zero.
Steve Block4e584df2012-04-24 23:12:47 +0100100 *
101 * @return the quota, in bytes
John Reck9b2a59b2011-01-24 17:31:39 -0800102 */
Nicolas Roard11e8fe52009-05-11 15:04:16 +0100103 public long getQuota() {
104 return mQuota;
105 }
Nicolas Roard6c24b4d2009-09-22 18:44:52 +0100106
John Reck9b2a59b2011-01-24 17:31:39 -0800107 /**
Steve Block4e584df2012-04-24 23:12:47 +0100108 * Gets the total amount of storage currently being used by this origin,
Steve Block285ddfc2012-03-28 12:46:02 +0100109 * for all JavaScript storage APIs, in bytes.
Steve Block4e584df2012-04-24 23:12:47 +0100110 *
111 * @return the total amount of storage, in bytes
John Reck9b2a59b2011-01-24 17:31:39 -0800112 */
Nicolas Roard6c24b4d2009-09-22 18:44:52 +0100113 public long getUsage() {
114 return mUsage;
115 }
Nicolas Roard11e8fe52009-05-11 15:04:16 +0100116 }
117
Nicolas Roard6c24b4d2009-09-22 18:44:52 +0100118 /*
119 * When calling getOrigins(), getUsageForOrigin() and getQuotaForOrigin(),
Steve Block285ddfc2012-03-28 12:46:02 +0100120 * we need to get the values from WebCore, but we cannot block while doing so
121 * as we used to do, as this could result in a full deadlock (other WebCore
Nicolas Roard6c24b4d2009-09-22 18:44:52 +0100122 * messages received while we are still blocked here, see http://b/2127737).
123 *
124 * We have to do everything asynchronously, by providing a callback function.
Steve Block285ddfc2012-03-28 12:46:02 +0100125 * We post a message on the WebCore thread (mHandler) that will get the result
126 * from WebCore, and we post it back on the UI thread (using mUIHandler).
Nicolas Roard6c24b4d2009-09-22 18:44:52 +0100127 * We can then use the callback function to return the value.
128 */
129
Nicolas Roard11e8fe52009-05-11 15:04:16 +0100130 /**
Steve Block4e584df2012-04-24 23:12:47 +0100131 * Gets the origins currently using either the Application Cache or Web SQL
Steve Block285ddfc2012-03-28 12:46:02 +0100132 * Database APIs. This method operates asynchronously, with the result
133 * being provided via a {@link ValueCallback}. The origins are provided as
134 * a map, of type {@code Map<String, WebStorage.Origin>}, from the string
135 * representation of the origin to a {@link WebStorage.Origin} object.
Nicolas Roard11e8fe52009-05-11 15:04:16 +0100136 */
Nicolas Roard6c24b4d2009-09-22 18:44:52 +0100137 public void getOrigins(ValueCallback<Map> callback) {
Jonathan Dixond3101b12012-04-12 20:51:51 +0100138 // Must be a no-op for backward compatibility: see the hidden constructor for reason.
Nicolas Roard11e8fe52009-05-11 15:04:16 +0100139 }
140
141 /**
Steve Block4e584df2012-04-24 23:12:47 +0100142 * Gets the amount of storage currently being used by both the Application
Steve Block285ddfc2012-03-28 12:46:02 +0100143 * Cache and Web SQL Database APIs by the given origin. The amount is given
144 * in bytes and the origin is specified using its string representation.
145 * This method operates asynchronously, with the result being provided via
146 * a {@link ValueCallback}.
Nicolas Roard11e8fe52009-05-11 15:04:16 +0100147 */
Nicolas Roard6c24b4d2009-09-22 18:44:52 +0100148 public void getUsageForOrigin(String origin, ValueCallback<Long> callback) {
Jonathan Dixond3101b12012-04-12 20:51:51 +0100149 // Must be a no-op for backward compatibility: see the hidden constructor for reason.
Nicolas Roard11e8fe52009-05-11 15:04:16 +0100150 }
151
152 /**
Steve Block4e584df2012-04-24 23:12:47 +0100153 * Gets the storage quota for the Web SQL Database API for the given origin.
Steve Block285ddfc2012-03-28 12:46:02 +0100154 * The quota is given in bytes and the origin is specified using its string
155 * representation. This method operates asynchronously, with the result
156 * being provided via a {@link ValueCallback}. Note that a quota is not
157 * enforced on a per-origin basis for the Application Cache API.
Nicolas Roard11e8fe52009-05-11 15:04:16 +0100158 */
Nicolas Roard6c24b4d2009-09-22 18:44:52 +0100159 public void getQuotaForOrigin(String origin, ValueCallback<Long> callback) {
Jonathan Dixond3101b12012-04-12 20:51:51 +0100160 // Must be a no-op for backward compatibility: see the hidden constructor for reason.
Nicolas Roard11e8fe52009-05-11 15:04:16 +0100161 }
162
163 /**
Steve Block4e584df2012-04-24 23:12:47 +0100164 * Sets the storage quota for the Web SQL Database API for the given origin.
Steve Block285ddfc2012-03-28 12:46:02 +0100165 * The quota is specified in bytes and the origin is specified using its string
166 * representation. Note that a quota is not enforced on a per-origin basis
167 * for the Application Cache API.
Nicolas Roard11e8fe52009-05-11 15:04:16 +0100168 */
169 public void setQuotaForOrigin(String origin, long quota) {
Jonathan Dixond3101b12012-04-12 20:51:51 +0100170 // Must be a no-op for backward compatibility: see the hidden constructor for reason.
Nicolas Roard11e8fe52009-05-11 15:04:16 +0100171 }
172
173 /**
Steve Block4e584df2012-04-24 23:12:47 +0100174 * Clears the storage currently being used by both the Application Cache and
Steve Block285ddfc2012-03-28 12:46:02 +0100175 * Web SQL Database APIs by the given origin. The origin is specified using
176 * its string representation.
Nicolas Roard11e8fe52009-05-11 15:04:16 +0100177 */
178 public void deleteOrigin(String origin) {
Jonathan Dixond3101b12012-04-12 20:51:51 +0100179 // Must be a no-op for backward compatibility: see the hidden constructor for reason.
Nicolas Roard11e8fe52009-05-11 15:04:16 +0100180 }
181
182 /**
Steve Block4e584df2012-04-24 23:12:47 +0100183 * Clears all storage currently being used by the JavaScript storage APIs.
Steve Block285ddfc2012-03-28 12:46:02 +0100184 * This includes the Application Cache, Web SQL Database and the HTML5 Web
185 * Storage APIs.
Nicolas Roard11e8fe52009-05-11 15:04:16 +0100186 */
Andrei Popescuaf9c77e2009-07-21 17:02:35 +0100187 public void deleteAllData() {
Jonathan Dixond3101b12012-04-12 20:51:51 +0100188 // Must be a no-op for backward compatibility: see the hidden constructor for reason.
Nicolas Roard6c24b4d2009-09-22 18:44:52 +0100189 }
190
191 /**
Steve Block4e584df2012-04-24 23:12:47 +0100192 * Gets the singleton instance of this class.
193 *
194 * @return the singleton {@link WebStorage} instance
Nicolas Roard11e8fe52009-05-11 15:04:16 +0100195 */
196 public static WebStorage getInstance() {
Jonathan Dixond3101b12012-04-12 20:51:51 +0100197 return WebViewFactory.getProvider().getWebStorage();
Nicolas Roard11e8fe52009-05-11 15:04:16 +0100198 }
199
Jonathan Dixona3dc86e2012-03-28 12:27:36 +0100200 /**
201 * This class should not be instantiated directly, applications must only use
202 * {@link #getInstance()} to obtain the instance.
203 * Note this constructor was erroneously public and published in SDK levels prior to 16, but
204 * applications using it would receive a non-functional instance of this class (there was no
205 * way to call createHandler() and createUIHandler(), so it would not work).
206 * @hide
207 */
208 public WebStorage() {}
Ben Murdoch7df19852009-04-22 13:07:58 +0100209}