Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / common / extensions / docs / templates / articles / offline_storage.html
blobde022a3c95c75c50ccc014db79b2abb62a25dd9e
1 <h1>Managing HTML5 Offline Storage</h1>
3 <p>
4 HTML5 introduced many storage APIs that let you store a large amount of data locally in your users' browsers. But the amount of space allocated for each app is, by default, restricted to a few megabytes. Google Chrome lets you ask for a larger storage quota, beyond the previous limit of just 5 MB. </p>
5 <p>This document introduces you to the basic concepts around the types of storage used in Chrome and describes the experimental Quota Management API, which lets you manage your storage quota. The document assumes that you are already familiar with the general concepts of client-side storage and know how to use offline APIs. </p>
6 <h2 id="contents">Contents</h2>
7 <ol>
8 <li><a href="#types">Types of storage</a>
9 <ol>
10 <li><a href="#temporary">Temporary </a></li>
11 <li><a href="#persistent">Persistent</a></li>
12 <li><a href="#unlimited">Unlimited</a></li>
13 <li><a href="#table">Comparing Storage Types</a></li>
14 </ol>
15 </li>
17 <li><a href="#managing_quota">Managing your quota</a>
18 <ol>
19 <li><a href="#query">Query storage usage and availability</a></li>
20 <li><a href="#asking_more">Ask for more storage</a></li>
21 <li><a href="#reset">Reset quota for testing</a></li>
22 </ol>
24 </li>
25 <li><a href="#reference">API reference</a>
26 <ol>
27 <li><a href="#constants">Constants</a></li>
28 <li><a href="#method_overview">Method overview</a></li>
29 <li><a href="#methods">Methods</a></li>
30 </ol>
32 </li>
33 <li><a href="#future">Future development</a></li>
34 </ol>
35 <h2 id="types">Types of storage</h2>
36 <p>In Google Chrome, you can ask for three types of storage: </p>
37 <ul>
38 <li><a href="#temporary">Temporary </a></li>
39 <li><a href="#persistent">Persistent</a></li>
40 <li><a href="#unlimited">Unlimited</a></li>
41 </ul>
42 <p>These storage types are described in greater detail in the following sections and compared with each other in the <a href="#table">table</a> below. </p>
43 <h3 id="temporary">Temporary storage</h3>
44 <p id="temporary">Temporary storage is transient storage that is available to any web app. Chrome automatically gives your app temporary storage, so you do not need to request allocation. </p>
45 <h4>Sharing the pool</h4>
46 <p>Temporary storage is shared among all web apps running in the browser.{#It is also shared across all offline APIs, such as App Cache, IndexedDB, and File System. However, it does not include web storage APIs like Local Storage and Session Storage, which still has a limit of 5 MB per origin.#} The shared pool can be up to 1/3 of the of available disk space. Storage already used by apps is included in the calculation of the shared pool; that is to say, the calculation is based on <code>(available storage space + storage being used by apps) * .333</code> . </p>
47 <p>Each app can have up to 20% of the shared pool. As an example, if the total available disk space is 60 GB, the shared pool is 20 GB, and the app can have up to 4 GB. This is calculated from 20% (up to 4 GB) of 1/3 (up to 20 GB) of the available disk space (60 GB). </p>
48 <h4>Asking for more space</h4>
49 <p>Although you can <a href="#query">query</a> for the amount of storage space available for your app and the amount of data already stored for your app, you cannot ask for more temporary storage space. If an app exceeds the allocated quota, an error is thrown.</p>
50 <h4>Running out of storage</h4>
51 <p>Once the storage quota for the entire pool is exceeded, the entire data stored for the least recently used host gets deleted. The browser, however, will not expunge the data in LocalStorage and SessionStorage. For data stored in other offline APIs, the browser deletes the data in whole and not in part so that app data doesn't get corrupted in unexpected ways. </p>
52 <p>As each app is limited to a maximum of 20% of the storage pool, deletion is likely only if the user is actively running more than five offline apps that are each using the maximum storage. </p>
53 <p>However, available storage space can shrink as users add more files on their hard drives. When the available disk space gets tight (Remember, the shared pool only gets half of the <em>current</em> available disk space), the browser deletes all the data stored for the least recently used host. </p>
55 <h3 id="persistent">Persistent storage</h3>
56 <p>Persistent storage is storage that stays in the browser unless the user expunges it. It is available only to apps that use the File System API, but will eventually be available to other offline APIs like IndexedDB and Application Cache.</p>
57 <p>An application can have a larger quota for persistent storage than temporary storage, but you must request storage using the Quota Management API and the user must grant you permission to use more space. Chrome presents an info bar that prompts the user to grant the app more local storage space.</p>
59 <h3 id="unlimited">Unlimited storage</h3>
60 <p>Unlimited storage is similar to persistent storage, but it is available only to Chrome apps and extensions (.crx files). The size of unlimited storage is limited only by the availability of space in the user's hard drive. You can ask for the <code>unlimitedStorage</code> permission in the manifest file for an <a href="/chrome/apps/docs/developers_guide#manifest">app</a> or <a href="http://code.google.com/chrome/extensions/manifest.html#permissions">extension</a>. At installation, the user is informed of permissions required by the app or extension. By proceeding with the installation, the user implicitly grants permission for all pages whose URLs are listed in the manifest.json file.</p>
61 <p>To learn more, see the respective developer guides for <a href="/chrome/apps/docs/developers_guide#manifest">apps</a> and <a href="http://code.google.com/chrome/extensions/manifest.html">extensions</a>. </p>
63 <p class="backtotop"><a href="#top">Back to top</a></p>
65 <h3 id="table">Comparing Storage Types </h3>
66 <p>The following table describes the differences among the three types of storage. <a name="table"></p>
67 <table>
68 <tr>
69 <th width="215" scope="col">&nbsp;</th>
70 <th width="385" scope="col">Temporary storage</th>
71 <th width="385" scope="col">Persistent storage</th>
72 <th width="385" scope="col">Unlimited storage</th>
73 </tr>
74 <tr>
75 <td><strong>Basic description</strong></td>
76 <td><p>Transient storage that is available to any web app. </p>
77 <p>It is automatic and does not need to be requested. </p></td>
78 <td> Permanent storage that must be requested through the Quota Management API and granted by users.</td>
79 <td><p>Permanent storage for Chrome extensions and apps. </p>
80 <p>It is set in the manifest file and must be granted by users.</p></td>
81 </tr>
82 <tr>
83 <td><strong>Availability</strong></td>
84 <td><p>All web apps.</p></td>
85 <td>All web apps.</td>
86 <td>Unique to <a href="http://code.google.com/chrome/extensions/index.html">Chrome extensions</a> as well as hosted and installed <a href="http://code.google.com/chrome/apps/index.html">web apps</a>.</td>
87 </tr>
88 <tr>
89 <td><strong>Permission</strong></td>
90 <td>None. You can use it without explicitly requesting it. </td>
91 <td><p>You have to request more storage using the Quota Management API. </p></td>
92 <td>You can ask for the <code>unlimitedStorage</code> permission in the manifest file for the <a href="/chrome/apps/docs/developers_guide#manifest">app</a> or <a href="http://code.google.com/chrome/extensions/manifest.html#permissions">extension</a>. </td>
93 </tr>
94 <tr>
95 <td><strong>User experience at first use</strong></td>
96 <td>Invisible to the user. The app just runs.</td>
97 <td><p>Chrome displays an info bar that prompts the user to either accept or decline the storage request. </p>
98 <p>But if the amount of quota you request is actually less than the app's current allocation, no prompt is shown. The larger quota is kept. </p></td>
99 <td><p>At installation, the user is informed of permissions required by the app or extension. By proceeding with the installation, the user implicitly grants permission for all pages whose URLs are listed in the manifest.json file for <a href="/chrome/apps/docs/developers_guide#manifest">app</a> or <a href="http://code.google.com/chrome/extensions/manifest.html">extension</a>. </p></td>
100 </tr>
102 <tr>
103 <td><strong>User experience at subsequent requests for increased storage </strong></td>
104 <td>Not applicable. You cannot ask for more temporary storage.</td>
105 <td><p>Chrome prompts the user again. </p>
106 <p>&nbsp;</p>
107 <p></p></td>
108 <td>Chrome does not prompt the user after installation, regardless of the requests for increased quota by the app or extension. </td>
109 </tr>
110 <tr>
111 <td><strong>Persistence of data</strong></td>
112 <td><p>Transient. The browser can delete the data.</p></td>
113 <td><p>Persistent. The browser doesn't delete the data unless the user instructs it to. Data is available in subsequent accesses. </p>
114 <p>Do not assume that the data is permanent, because the user can delete it. </p> </td>
115 <td><p>Same as persistent storage. </p>
116 <p>&nbsp;</p></td>
117 </tr>
118 <tr>
119 <td><strong>Default storage space</strong></td>
120 <td><p>Up to 20% of the shared pool.</p></td>
121 <td>0 MB. You have to explicitly ask for a specific storage space. </td>
122 <td><p>0 MB. You have to explicitly ask for <code>unlimitedStorage</code> in the manifest file. </p>
123 <p>If you do not specify your storage requirements, Chrome allocates storage to the app from the shared pool of temporary storage. </p></td>
124 </tr>
125 <tr>
126 <td><strong>Maximum storage space</strong></td>
127 <td>Up to 20% of the shared pool. </td>
128 <td>As large as the available space on the hard drive. It has no fixed pool of storage.</td>
129 <td>As large as the available space on the hard drive. </td>
130 </tr>
131 <tr>
132 <td><strong>Recommended use case</strong></td>
133 <td>Caching.</td>
134 <td>Apps that work offline or have a large number of assets. </td>
135 <td>Apps that were designed to run in Google Chrome.</td>
136 </tr>
137 <tr>
138 <td><strong>APIs that can use it</strong></td>
139 <td> <p>Offline APIs</p>
140 <ul>
141 <li>App Cache </li>
142 <li> File System</li>
143 <li> IndexedDB </li>
144 <li> WebSQL (<a href="http://www.w3.org/TR/webdatabase/">deprecated</a> since November 18, 2010) </li>
145 </ul>
146 {% comment %}
147 <p class="note"><strong>Note:</strong> Web storage APIs like LocalStorage and SessionStorage remain fixed at 5 MB. </p>
149 {% endcomment %}</td>
150 <td>File System API</td>
151 <td><p>Offline APIs</p>
152 <ul>
153 <li>App Cache </li>
154 <li> File System</li>
155 <li> IndexedDB </li>
156 <li> WebSQL (deprecated) </li>
157 </ul>
158 {% comment %}
159 <p class="note"><strong>Note:</strong> Web storage APIs like LocalStorage and SessionStorage remain fixed at 5 MB.</p>
161 {% endcomment %}</td>
162 </td>
163 </tr>
164 </table>
166 <p class="backtotop"><a href="#top">Back to top</a></p>
168 <h2 id="managing_quota">Managing your quota</h2>
169 <p>With the Quota Management API, which was introduced in Chrome 13, you can do the following:</p>
170 <ul>
171 <li><a href="#query">Query storage usage and availability</a></li>
172 <li><a href="#asking_more">Ask for more storage</a></li>
173 <li><a href="#reset">Reset quota for testing</a></li>
174 </ul>
175 <p>The API is implemented with the global object <code>window.webkitStorageInfo</code>. </p>
176 <p>For the reference documentation, see the <a href="#reference">next section</a>.</p>
177 <h3 id="query">Querying storage usage and availability</h3>
178 <p>To query the storage size that is being used and the available space left for the host, call <code>queryUsageAndQuota()</code> with the following:</p>
179 <ul>
180 <li>Type of storage you want to check</li>
181 <li>Success callback </li>
182 </ul>
183 <p>The usage reported by the API might not match with the actual size of the user data, as each storage might need some extra bytes to store its metadata. Also, status updates can lag, resulting in the API not reflecting the most recent storage status. </p>
184 <p>The following code snippet shows how you can ask about storage space:</p>
185 <pre class="prettyprint">
186 // Request storage usage and capacity left
187 // Choose either Temporary or Persistent
188 navigator.webkitTemporaryStorage.queryUsageAndQuota (
189 function(usedBytes, grantedBytes) {
190 console.log('we are using ', usedBytes, ' of ', grantedBytes, 'bytes');
192 function(e) { console.log('Error', e); }
194 </pre>
195 <p>If you want to ask for the status of persistent storage, simply replace <span class="prettyprint"><code>webkitStorageInfo.<strong>TEMPORARY</strong></code></span> with <span class="prettyprint"><code>webkitStorageInfo.<strong>PERSISTENT</strong></code></span>. The enum is also in the <code>window</code> object (global namespace), so you can also use <code>window.<strong>PERSISTENT</strong></code> and <code>window.<strong>TEMPORARY</strong></code>.</p>
196 <p class="backtotop"><a href="#top">Back to top</a></p>
198 <h3 id="asking_more">Asking for more storage</h3>
199 <p>You don't need to ask for more temporary storage as the allocation is automatic, and you can't get beyond the maximum limit (as described in the <a href="#table">table</a>). </p>
200 <p>For persistent storage for File System API, the default quota is 0, so you need to explicitly request storage for your application. Call <code>requestQuota()</code> with the following:</p>
201 <ul>
202 <li>Type of storage</li>
203 <li>Size</li>
204 <li>Success callback</li>
205 </ul>
206 <p>Depending on what you ask for, the following happens:</p>
207 <ul>
208 <li>If you ask for a larger quota, the browser presents an info bar to the user and prompts them to either grant or deny permission for increased quota. In some cases, the request might be silently rejected, and the current quota or smaller quota is returned.</li>
209 <li>If the amount of quota you request is less than the app's current allocation, no prompt is shown. </li>
210 <li>If you ask for more storage than what is allowed, you get an error (<code>QUOTA_EXCEEDED_ERR</code>). </li>
211 <li>If you call <code>requestQuota()</code> again after the user has already granted permission, nothing happens. So don't bother calling the method again.</li>
212 </ul>
213 <p>The following shows how you can ask for more storage space:</p>
214 <pre class="prettyprint">
215 // Request Quota (only for File System API) </span>
216 var requestedBytes = 1024*1024*10; // 10MB
218 navigator.webkitPersistentStorage.requestQuota (
219 requestedBytes, function(grantedBytes) {
220 window.requestFileSystem(PERSISTENT, grantedBytes, onInitFs, errorHandler);
222 }, function(e) { console.log('Error', e); }
224 });</pre>
225 <p class="backtotop"><a href="#top">Back to top</a></p>
227 <h3 id="reset">Resetting quota for testing</h3>
228 <p>When you are testing storage in your app, you might want to clear the stored data so that you can test quota management afresh in your app. To do so:</p>
229 <ol>
230 <li>Enter <code>chrome://settings/cookies</code> in the omnibox (the address bar). </li>
231 <li>Search for your app.</li>
232 <li>Select your app. </li>
233 <li>Click the <strong>X</strong> on the right side of the highlighted selection.</li>
234 </ol>
237 <p class="backtotop"><a href="#top">Back to top</a></p>
239 <h2 id="reference">API reference</h2>
240 <p>This section documents the methods of the Quota Management API.</p>
241 <h3 id="constants">Constants</h3>
242 <p>The following are <code>webkitStorageInfo</code> constants, which indicate the type of storage. </p>
243 <table>
244 <thead>
245 <tr>
246 <th scope="col" width="111">Constant</th>
247 <th scope="col" width="53">Value</th>
248 <th scope="col" width="690">Description</th>
249 </tr>
250 </thead>
251 <tbody>
252 <tr>
253 <td><code><a name="const_done" id="const_done">TEMPORARY</a></code></td>
254 <td>0</td>
255 <td>Temporary storage.</td>
256 </tr>
257 <tr>
258 <td><code><a name="const_loading" id="const_loading">PERSISTENT</a></code></td>
259 <td>1</td>
260 <td>Persistent storage. </td>
261 </tr>
262 </tbody>
263 </table>
264 <h3 id="method_overview">Method overview</h3>
265 <table>
266 <tbody>
267 <tr>
268 <td><code>void <a href="#queryUsageAndQuota" title="#add">queryUsageAndQuota</a> (in unsigned short storageType, StorageInfoUsageCallback successCallback, in optional StorageInfoErrorCallback errorCallback) </code></td>
269 </tr>
270 <tr>
271 <td><code>void <a href="#requestQuota" title="#add">requestQuota</a> (in unsigned short storageType, unsigned long long newQuotaInBytes, optional StorageInfoQuotaCallback successCallback, optional StorageInfoErrorCallback errorCallback)</code></td>
272 </tr>
273 </tbody>
274 </table>
275 <h3 id="methods">Methods</h3>
276 <h4 id="queryUsageAndQuota">queryUsageAndQuota</h4>
277 <p>Check the storage size that is being used and the available space left for the host.</p>
279 <pre class="prettyprint"> // you could also use it from webkitPersistentStorage
280 navigator.webkitTemporaryStorage.queryUsageAndQuota(
281 successCallback,
282 errorCallback);</pre>
283 <dl>
284 <dt>successCallback</dt>
285 <dd>Optional callback with two parameters: <ul>
286 <li>The current number of bytes the app is using. </li>
287 <li>The number of bytes left in the quota. </li>
288 </ul>
289 </dd>
290 </dl>
291 <dl>
292 <dt>errorCallback</dt>
293 <dd>Optional error callback.</dd>
294 <dt>&nbsp;</dt>
295 </dl>
296 <p class="backtotop"><a href="#top">Back to top</a></p>
297 <hr />
298 <h4 id="requestQuota">requestQuota</h4>
300 <p>Ask for more storage. The browser presents an info bar to prompt user to grant or deny the app the permission to have more storage.</p>
302 <pre class="prettyprint"> // you could also use it from webkitTemporaryStorage
303 navigator.webkitPersistentStorage.requestQuota (
304 newQuotaInBytes,
305 quotaCallback,
306 errorCallback);</pre>
308 <h5>Parameters</h5>
309 <dl>
311 <dt>newQuotaInBytes</dt>
312 <dd>The amount of bytes you want in your storage quota. </dd>
313 </dl>
314 <dl>
315 <dt>successCallback</dt>
316 <dd>Optional callback that passes the amount of bytes granted. </dd>
317 </dl>
318 <dl>
319 <dt>errorCallback</dt>
320 <dd>Optional error callback.</dd>
321 </dl>
323 <h2 id="future">Future development</h2>
324 <p>The plan is to put all HTML5 offline storage APIs—including IndexedDB, Application Cache, File System,{# LocalStorage, SessionStorage,#} and other APIs that might be specified—under the Quota Management API. You will be able to manage all storage allocation with it. </p>