|
| 1 | +// Copyright 2011 The Closure Library Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +/** |
| 16 | + * @fileoverview Base class that implements functionality common |
| 17 | + * across both session and local web storage mechanisms. |
| 18 | + * |
| 19 | + */ |
| 20 | + |
| 21 | +goog.provide('goog.storage.mechanism.HTML5WebStorage'); |
| 22 | + |
| 23 | +goog.require('goog.asserts'); |
| 24 | +goog.require('goog.iter.Iterator'); |
| 25 | +goog.require('goog.iter.StopIteration'); |
| 26 | +goog.require('goog.storage.mechanism.ErrorCode'); |
| 27 | +goog.require('goog.storage.mechanism.IterableMechanism'); |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +/** |
| 32 | + * Provides a storage mechanism that uses HTML5 Web storage. |
| 33 | + * |
| 34 | + * @param {Storage} storage The Web storage object. |
| 35 | + * @constructor |
| 36 | + * @extends {goog.storage.mechanism.IterableMechanism} |
| 37 | + */ |
| 38 | +goog.storage.mechanism.HTML5WebStorage = function(storage) { |
| 39 | + goog.storage.mechanism.HTML5WebStorage.base(this, 'constructor'); |
| 40 | + |
| 41 | + /** |
| 42 | + * The web storage object (window.localStorage or window.sessionStorage). |
| 43 | + * @private {Storage} |
| 44 | + */ |
| 45 | + this.storage_ = storage; |
| 46 | +}; |
| 47 | +goog.inherits(goog.storage.mechanism.HTML5WebStorage, |
| 48 | + goog.storage.mechanism.IterableMechanism); |
| 49 | + |
| 50 | + |
| 51 | +/** |
| 52 | + * The key used to check if the storage instance is available. |
| 53 | + * @private {string} |
| 54 | + * @const |
| 55 | + */ |
| 56 | +goog.storage.mechanism.HTML5WebStorage.STORAGE_AVAILABLE_KEY_ = '__sak'; |
| 57 | + |
| 58 | + |
| 59 | +/** |
| 60 | + * Determines whether or not the mechanism is available. |
| 61 | + * It works only if the provided web storage object exists and is enabled. |
| 62 | + * |
| 63 | + * @return {boolean} True if the mechanism is available. |
| 64 | + */ |
| 65 | +goog.storage.mechanism.HTML5WebStorage.prototype.isAvailable = function() { |
| 66 | + if (!this.storage_) { |
| 67 | + return false; |
| 68 | + } |
| 69 | + /** @preserveTry */ |
| 70 | + try { |
| 71 | + // setItem will throw an exception if we cannot access WebStorage (e.g., |
| 72 | + // Safari in private mode). |
| 73 | + this.storage_.setItem( |
| 74 | + goog.storage.mechanism.HTML5WebStorage.STORAGE_AVAILABLE_KEY_, '1'); |
| 75 | + this.storage_.removeItem( |
| 76 | + goog.storage.mechanism.HTML5WebStorage.STORAGE_AVAILABLE_KEY_); |
| 77 | + return true; |
| 78 | + } catch (e) { |
| 79 | + return false; |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | + |
| 84 | +/** @override */ |
| 85 | +goog.storage.mechanism.HTML5WebStorage.prototype.set = function(key, value) { |
| 86 | + /** @preserveTry */ |
| 87 | + try { |
| 88 | + // May throw an exception if storage quota is exceeded. |
| 89 | + this.storage_.setItem(key, value); |
| 90 | + } catch (e) { |
| 91 | + // In Safari Private mode, conforming to the W3C spec, invoking |
| 92 | + // Storage.prototype.setItem will allways throw a QUOTA_EXCEEDED_ERR |
| 93 | + // exception. Since it's impossible to verify if we're in private browsing |
| 94 | + // mode, we throw a different exception if the storage is empty. |
| 95 | + if (this.storage_.length == 0) { |
| 96 | + throw goog.storage.mechanism.ErrorCode.STORAGE_DISABLED; |
| 97 | + } else { |
| 98 | + throw goog.storage.mechanism.ErrorCode.QUOTA_EXCEEDED; |
| 99 | + } |
| 100 | + } |
| 101 | +}; |
| 102 | + |
| 103 | + |
| 104 | +/** @override */ |
| 105 | +goog.storage.mechanism.HTML5WebStorage.prototype.get = function(key) { |
| 106 | + // According to W3C specs, values can be of any type. Since we only save |
| 107 | + // strings, any other type is a storage error. If we returned nulls for |
| 108 | + // such keys, i.e., treated them as non-existent, this would lead to a |
| 109 | + // paradox where a key exists, but it does not when it is retrieved. |
| 110 | + // http://www.w3.org/TR/2009/WD-webstorage-20091029/#the-storage-interface |
| 111 | + var value = this.storage_.getItem(key); |
| 112 | + if (!goog.isString(value) && !goog.isNull(value)) { |
| 113 | + throw goog.storage.mechanism.ErrorCode.INVALID_VALUE; |
| 114 | + } |
| 115 | + return value; |
| 116 | +}; |
| 117 | + |
| 118 | + |
| 119 | +/** @override */ |
| 120 | +goog.storage.mechanism.HTML5WebStorage.prototype.remove = function(key) { |
| 121 | + this.storage_.removeItem(key); |
| 122 | +}; |
| 123 | + |
| 124 | + |
| 125 | +/** @override */ |
| 126 | +goog.storage.mechanism.HTML5WebStorage.prototype.getCount = function() { |
| 127 | + return this.storage_.length; |
| 128 | +}; |
| 129 | + |
| 130 | + |
| 131 | +/** @override */ |
| 132 | +goog.storage.mechanism.HTML5WebStorage.prototype.__iterator__ = function( |
| 133 | + opt_keys) { |
| 134 | + var i = 0; |
| 135 | + var storage = this.storage_; |
| 136 | + var newIter = new goog.iter.Iterator(); |
| 137 | + newIter.next = function() { |
| 138 | + if (i >= storage.length) { |
| 139 | + throw goog.iter.StopIteration; |
| 140 | + } |
| 141 | + var key = goog.asserts.assertString(storage.key(i++)); |
| 142 | + if (opt_keys) { |
| 143 | + return key; |
| 144 | + } |
| 145 | + var value = storage.getItem(key); |
| 146 | + // The value must exist and be a string, otherwise it is a storage error. |
| 147 | + if (!goog.isString(value)) { |
| 148 | + throw goog.storage.mechanism.ErrorCode.INVALID_VALUE; |
| 149 | + } |
| 150 | + return value; |
| 151 | + }; |
| 152 | + return newIter; |
| 153 | +}; |
| 154 | + |
| 155 | + |
| 156 | +/** @override */ |
| 157 | +goog.storage.mechanism.HTML5WebStorage.prototype.clear = function() { |
| 158 | + this.storage_.clear(); |
| 159 | +}; |
| 160 | + |
| 161 | + |
| 162 | +/** |
| 163 | + * Gets the key for a given key index. If an index outside of |
| 164 | + * [0..this.getCount()) is specified, this function returns null. |
| 165 | + * @param {number} index A key index. |
| 166 | + * @return {?string} A storage key, or null if the specified index is out of |
| 167 | + * range. |
| 168 | + */ |
| 169 | +goog.storage.mechanism.HTML5WebStorage.prototype.key = function(index) { |
| 170 | + return this.storage_.key(index); |
| 171 | +}; |
0 commit comments