17df1985e86635af006be3dfa65987d60e290b5deBen Murdoch/*
27df1985e86635af006be3dfa65987d60e290b5deBen Murdoch * Copyright (C) 2009 The Android Open Source Project
37df1985e86635af006be3dfa65987d60e290b5deBen Murdoch *
47df1985e86635af006be3dfa65987d60e290b5deBen Murdoch * Licensed under the Apache License, Version 2.0 (the "License");
57df1985e86635af006be3dfa65987d60e290b5deBen Murdoch * you may not use this file except in compliance with the License.
67df1985e86635af006be3dfa65987d60e290b5deBen Murdoch * You may obtain a copy of the License at
77df1985e86635af006be3dfa65987d60e290b5deBen Murdoch *
87df1985e86635af006be3dfa65987d60e290b5deBen Murdoch *      http://www.apache.org/licenses/LICENSE-2.0
97df1985e86635af006be3dfa65987d60e290b5deBen Murdoch *
107df1985e86635af006be3dfa65987d60e290b5deBen Murdoch * Unless required by applicable law or agreed to in writing, software
117df1985e86635af006be3dfa65987d60e290b5deBen Murdoch * distributed under the License is distributed on an "AS IS" BASIS,
127df1985e86635af006be3dfa65987d60e290b5deBen Murdoch * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137df1985e86635af006be3dfa65987d60e290b5deBen Murdoch * See the License for the specific language governing permissions and
147df1985e86635af006be3dfa65987d60e290b5deBen Murdoch * limitations under the License.
157df1985e86635af006be3dfa65987d60e290b5deBen Murdoch */
167df1985e86635af006be3dfa65987d60e290b5deBen Murdoch
177df1985e86635af006be3dfa65987d60e290b5deBen Murdochpackage android.webkit;
187df1985e86635af006be3dfa65987d60e290b5deBen Murdoch
19451e338c51e8c45efc0d21536dfae6f78f6d5e06Ignacio Sollaimport android.annotation.SystemApi;
20451e338c51e8c45efc0d21536dfae6f78f6d5e06Ignacio Solla
2136196b6d4a413b65c9f1f82112ae34a46dcedfc6Jonathan Dixonimport java.util.Map;
2211e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard
237df1985e86635af006be3dfa65987d60e290b5deBen Murdoch/**
24285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block * This class is used to manage the JavaScript storage APIs provided by the
25285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block * {@link WebView}. It manages the Application Cache API, the Web SQL Database
26285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block * API and the HTML5 Web Storage API.
27285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block *
28b632adfaf445ff6f0c07bfc54671d204ad0e5a67Selim Gurun * The Application Cache API provides a mechanism to create and maintain an
29b632adfaf445ff6f0c07bfc54671d204ad0e5a67Selim Gurun * application cache to power offline Web applications. Use of the Application
30b632adfaf445ff6f0c07bfc54671d204ad0e5a67Selim Gurun * Cache API can be attributed to an origin {@link WebStorage.Origin}, however
31b632adfaf445ff6f0c07bfc54671d204ad0e5a67Selim Gurun * it is not possible to set per-origin quotas. Note that there can be only
32b632adfaf445ff6f0c07bfc54671d204ad0e5a67Selim Gurun * one application cache per application.
33b632adfaf445ff6f0c07bfc54671d204ad0e5a67Selim Gurun *
34b632adfaf445ff6f0c07bfc54671d204ad0e5a67Selim Gurun * The Web SQL Database API provides storage which is private to a given origin.
35b632adfaf445ff6f0c07bfc54671d204ad0e5a67Selim Gurun * Similar to the Application Cache, use of the Web SQL Database can be attributed
36b632adfaf445ff6f0c07bfc54671d204ad0e5a67Selim Gurun * to an origin. It is also possible to set per-origin quotas.
377df1985e86635af006be3dfa65987d60e290b5deBen Murdoch */
38939e5040b51539be561db1d18dec18196f201f5cJonathan Dixonpublic class WebStorage {
397df1985e86635af006be3dfa65987d60e290b5deBen Murdoch
407df1985e86635af006be3dfa65987d60e290b5deBen Murdoch    /**
41285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * Encapsulates a callback function which is used to provide a new quota
42b632adfaf445ff6f0c07bfc54671d204ad0e5a67Selim Gurun     * for a JavaScript storage API.
43b632adfaf445ff6f0c07bfc54671d204ad0e5a67Selim Gurun     * See
44285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * {@link WebChromeClient#onExceededDatabaseQuota} and
45285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * {@link WebChromeClient#onReachedMaxAppCacheSize}.
465545d083d35620a625b65fafe97199660d85f059Jonathan Dixon     * @deprecated This class is obsolete and no longer used.
477df1985e86635af006be3dfa65987d60e290b5deBen Murdoch     */
485545d083d35620a625b65fafe97199660d85f059Jonathan Dixon    @Deprecated
497df1985e86635af006be3dfa65987d60e290b5deBen Murdoch    public interface QuotaUpdater {
50285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block        /**
514e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block         * Provides a new quota, specified in bytes.
524e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block         *
534e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block         * @param newQuota the new quota, in bytes
54285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block         */
557df1985e86635af006be3dfa65987d60e290b5deBen Murdoch        public void updateQuota(long newQuota);
567df1985e86635af006be3dfa65987d60e290b5deBen Murdoch    };
5711e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard
589b2a59bdd27cfa45c7bb3b58eb705b35589b7050John Reck    /**
59285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * This class encapsulates information about the amount of storage
60285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * currently used by an origin for the JavaScript storage APIs.
61b632adfaf445ff6f0c07bfc54671d204ad0e5a67Selim Gurun     * An origin comprises the host, scheme and port of a URI.
62285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * See {@link WebStorage} for details.
639b2a59bdd27cfa45c7bb3b58eb705b35589b7050John Reck     */
6487745ce21fe3f65b8cf7a92372c24227821318d3John Reck    public static class Origin {
6587745ce21fe3f65b8cf7a92372c24227821318d3John Reck        private String mOrigin = null;
6687745ce21fe3f65b8cf7a92372c24227821318d3John Reck        private long mQuota = 0;
6787745ce21fe3f65b8cf7a92372c24227821318d3John Reck        private long mUsage = 0;
686c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard
69d3101b1d300f5942fdb7dfa323dc8830c4edc007Jonathan Dixon        /** @hide */
70451e338c51e8c45efc0d21536dfae6f78f6d5e06Ignacio Solla        @SystemApi
71d3101b1d300f5942fdb7dfa323dc8830c4edc007Jonathan Dixon        protected Origin(String origin, long quota, long usage) {
726c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard            mOrigin = origin;
736c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard            mQuota = quota;
746c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard            mUsage = usage;
756c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard        }
7611e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard
779b2a59bdd27cfa45c7bb3b58eb705b35589b7050John Reck        /**
784e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block         * Gets the string representation of this origin.
794e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block         *
804e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block         * @return the string representation of this origin
819b2a59bdd27cfa45c7bb3b58eb705b35589b7050John Reck         */
82285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block        // An origin string is created using WebCore::SecurityOrigin::toString().
83285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block        // Note that WebCore::SecurityOrigin uses 0 (which is not printed) for
84285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block        // the port if the port is the default for the protocol. Eg
85285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block        // http://www.google.com and http://www.google.com:80 both record a port
86285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block        // of 0 and hence toString() == 'http://www.google.com' for both.
8711e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard        public String getOrigin() {
8811e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard            return mOrigin;
8911e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard        }
9011e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard
919b2a59bdd27cfa45c7bb3b58eb705b35589b7050John Reck        /**
924e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block         * Gets the quota for this origin, for the Web SQL Database API, in
93285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block         * bytes. If this origin does not use the Web SQL Database API, this
94285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block         * quota will be set to zero.
954e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block         *
964e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block         * @return the quota, in bytes
979b2a59bdd27cfa45c7bb3b58eb705b35589b7050John Reck         */
9811e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard        public long getQuota() {
9911e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard            return mQuota;
10011e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard        }
1016c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard
1029b2a59bdd27cfa45c7bb3b58eb705b35589b7050John Reck        /**
1034e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block         * Gets the total amount of storage currently being used by this origin,
104285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block         * for all JavaScript storage APIs, in bytes.
1054e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block         *
1064e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block         * @return the total amount of storage, in bytes
1079b2a59bdd27cfa45c7bb3b58eb705b35589b7050John Reck         */
1086c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard        public long getUsage() {
1096c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard            return mUsage;
1106c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard        }
1116c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard    }
1126c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard
1136c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard    /*
1146c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard     * When calling getOrigins(), getUsageForOrigin() and getQuotaForOrigin(),
115285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * we need to get the values from WebCore, but we cannot block while doing so
116285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * as we used to do, as this could result in a full deadlock (other WebCore
1176c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard     * messages received while we are still blocked here, see http://b/2127737).
1186c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard     *
1196c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard     * We have to do everything asynchronously, by providing a callback function.
120285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * We post a message on the WebCore thread (mHandler) that will get the result
121285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * from WebCore, and we post it back on the UI thread (using mUIHandler).
1226c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard     * We can then use the callback function to return the value.
1236c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard     */
1246c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard
12511e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard    /**
1264e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     * Gets the origins currently using either the Application Cache or Web SQL
127285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * Database APIs. This method operates asynchronously, with the result
128285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * being provided via a {@link ValueCallback}. The origins are provided as
129285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * a map, of type {@code Map<String, WebStorage.Origin>}, from the string
130285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * representation of the origin to a {@link WebStorage.Origin} object.
13111e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard     */
1326c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard    public void getOrigins(ValueCallback<Map> callback) {
133d3101b1d300f5942fdb7dfa323dc8830c4edc007Jonathan Dixon        // Must be a no-op for backward compatibility: see the hidden constructor for reason.
13411e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard    }
13511e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard
13611e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard    /**
1374e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     * Gets the amount of storage currently being used by both the Application
138285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * Cache and Web SQL Database APIs by the given origin. The amount is given
139285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * in bytes and the origin is specified using its string representation.
140285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * This method operates asynchronously, with the result being provided via
141285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * a {@link ValueCallback}.
14211e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard     */
1436c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard    public void getUsageForOrigin(String origin, ValueCallback<Long> callback) {
144d3101b1d300f5942fdb7dfa323dc8830c4edc007Jonathan Dixon        // Must be a no-op for backward compatibility: see the hidden constructor for reason.
14511e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard    }
14611e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard
14711e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard    /**
1484e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     * Gets the storage quota for the Web SQL Database API for the given origin.
149285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * The quota is given in bytes and the origin is specified using its string
150285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * representation. This method operates asynchronously, with the result
151285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * being provided via a {@link ValueCallback}. Note that a quota is not
152285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * enforced on a per-origin basis for the Application Cache API.
15311e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard     */
1546c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard    public void getQuotaForOrigin(String origin, ValueCallback<Long> callback) {
155d3101b1d300f5942fdb7dfa323dc8830c4edc007Jonathan Dixon        // Must be a no-op for backward compatibility: see the hidden constructor for reason.
15611e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard    }
15711e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard
15811e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard    /**
1594e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     * Sets the storage quota for the Web SQL Database API for the given origin.
160285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * The quota is specified in bytes and the origin is specified using its string
161285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * representation. Note that a quota is not enforced on a per-origin basis
162285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * for the Application Cache API.
163835b1fcc3193e0860ec33cca1c4cdff31f409473Jonathan Dixon     * @deprecated Controlling quota per-origin will not be supported in future.
16411e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard     */
165835b1fcc3193e0860ec33cca1c4cdff31f409473Jonathan Dixon    @Deprecated
16611e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard    public void setQuotaForOrigin(String origin, long quota) {
167d3101b1d300f5942fdb7dfa323dc8830c4edc007Jonathan Dixon        // Must be a no-op for backward compatibility: see the hidden constructor for reason.
16811e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard    }
16911e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard
17011e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard    /**
1714e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     * Clears the storage currently being used by both the Application Cache and
172285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * Web SQL Database APIs by the given origin. The origin is specified using
173285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * its string representation.
17411e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard     */
17511e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard    public void deleteOrigin(String origin) {
176d3101b1d300f5942fdb7dfa323dc8830c4edc007Jonathan Dixon        // Must be a no-op for backward compatibility: see the hidden constructor for reason.
17711e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard    }
17811e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard
17911e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard    /**
1804e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     * Clears all storage currently being used by the JavaScript storage APIs.
181285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * This includes the Application Cache, Web SQL Database and the HTML5 Web
182285ddfc8abfcdd252c50477b34c13f8173f9f3f4Steve Block     * Storage APIs.
18311e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard     */
184af9c77edbddfcf87a4dc17b95db84bf741674a9aAndrei Popescu    public void deleteAllData() {
185d3101b1d300f5942fdb7dfa323dc8830c4edc007Jonathan Dixon        // Must be a no-op for backward compatibility: see the hidden constructor for reason.
1866c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard    }
1876c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard
1886c24b4d10223cb522e6bdbf0e334f61e672f4366Nicolas Roard    /**
1894e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     * Gets the singleton instance of this class.
1904e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     *
1914e584df4cee8334bc371c04a67bcd0a32e2f9480Steve Block     * @return the singleton {@link WebStorage} instance
19211e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard     */
19311e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard    public static WebStorage getInstance() {
194d3101b1d300f5942fdb7dfa323dc8830c4edc007Jonathan Dixon      return WebViewFactory.getProvider().getWebStorage();
19511e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard    }
19611e8fe5a7208c9cd6afc2a0373761ae506d7707fNicolas Roard
197a3dc86e637873be115e68be50bd1b281beff7994Jonathan Dixon    /**
198a3dc86e637873be115e68be50bd1b281beff7994Jonathan Dixon     * This class should not be instantiated directly, applications must only use
199a3dc86e637873be115e68be50bd1b281beff7994Jonathan Dixon     * {@link #getInstance()} to obtain the instance.
200a3dc86e637873be115e68be50bd1b281beff7994Jonathan Dixon     * Note this constructor was erroneously public and published in SDK levels prior to 16, but
201a3dc86e637873be115e68be50bd1b281beff7994Jonathan Dixon     * applications using it would receive a non-functional instance of this class (there was no
202a3dc86e637873be115e68be50bd1b281beff7994Jonathan Dixon     * way to call createHandler() and createUIHandler(), so it would not work).
203a3dc86e637873be115e68be50bd1b281beff7994Jonathan Dixon     * @hide
204a3dc86e637873be115e68be50bd1b281beff7994Jonathan Dixon     */
205451e338c51e8c45efc0d21536dfae6f78f6d5e06Ignacio Solla    @SystemApi
206a3dc86e637873be115e68be50bd1b281beff7994Jonathan Dixon    public WebStorage() {}
2077df1985e86635af006be3dfa65987d60e290b5deBen Murdoch}
208