1/* 2 * Copyright (C) 2008 Nokia Inc. All rights reserved. 3 * Copyright (C) 2013 Samsung Electronics. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 15 * its contributors may be used to endorse or promote products derived 16 * from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY 19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30/** 31 * @constructor 32 * @param {string} securityOrigin 33 * @param {boolean} isLocalStorage 34 */ 35WebInspector.DOMStorage = function(securityOrigin, isLocalStorage) 36{ 37 this._securityOrigin = securityOrigin; 38 this._isLocalStorage = isLocalStorage; 39} 40 41/** 42 * @param {string} securityOrigin 43 * @param {boolean} isLocalStorage 44 * @return {DOMStorageAgent.StorageId} 45 */ 46WebInspector.DOMStorage.storageId = function(securityOrigin, isLocalStorage) 47{ 48 return { securityOrigin: securityOrigin, isLocalStorage: isLocalStorage }; 49} 50 51WebInspector.DOMStorage.prototype = { 52 53 /** @return {DOMStorageAgent.StorageId} */ 54 get id() 55 { 56 return WebInspector.DOMStorage.storageId(this._securityOrigin, this._isLocalStorage); 57 }, 58 59 /** @return {string} */ 60 get securityOrigin() 61 { 62 return this._securityOrigin; 63 }, 64 65 /** @return {boolean} */ 66 get isLocalStorage() 67 { 68 return this._isLocalStorage; 69 }, 70 71 /** 72 * @param {function(?Protocol.Error, Array.<DOMStorageAgent.Item>):void=} callback 73 */ 74 getItems: function(callback) 75 { 76 DOMStorageAgent.getDOMStorageItems(this.id, callback); 77 }, 78 79 /** 80 * @param {string} key 81 * @param {string} value 82 * @param {function(?Protocol.Error):void=} callback 83 */ 84 setItem: function(key, value, callback) 85 { 86 DOMStorageAgent.setDOMStorageItem(this.id, key, value, callback); 87 }, 88 89 /** 90 * @param {string} key 91 * @param {function(?Protocol.Error):void=} callback 92 */ 93 removeItem: function(key, callback) 94 { 95 DOMStorageAgent.removeDOMStorageItem(this.id, key, callback); 96 } 97} 98 99/** 100 * @constructor 101 * @extends {WebInspector.Object} 102 */ 103WebInspector.DOMStorageModel = function() 104{ 105 this._storages = {}; 106 InspectorBackend.registerDOMStorageDispatcher(new WebInspector.DOMStorageDispatcher(this)); 107 DOMStorageAgent.enable(); 108 WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.SecurityOriginAdded, this._securityOriginAdded, this); 109 WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.SecurityOriginRemoved, this._securityOriginRemoved, this); 110} 111 112WebInspector.DOMStorageModel.Events = { 113 DOMStorageAdded: "DOMStorageAdded", 114 DOMStorageRemoved: "DOMStorageRemoved", 115 DOMStorageItemsCleared: "DOMStorageItemsCleared", 116 DOMStorageItemRemoved: "DOMStorageItemRemoved", 117 DOMStorageItemAdded: "DOMStorageItemAdded", 118 DOMStorageItemUpdated: "DOMStorageItemUpdated" 119} 120 121WebInspector.DOMStorageModel.prototype = { 122 123 /** 124 * @param {WebInspector.Event} event 125 */ 126 _securityOriginAdded: function(event) 127 { 128 var securityOrigin = /** @type {string} */ (event.data); 129 var localStorageKey = this._storageKey(securityOrigin, true); 130 console.assert(!this._storages[localStorageKey]); 131 var localStorage = new WebInspector.DOMStorage(securityOrigin, true); 132 this._storages[localStorageKey] = localStorage; 133 this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageAdded, localStorage); 134 135 var sessionStorageKey = this._storageKey(securityOrigin, false); 136 console.assert(!this._storages[sessionStorageKey]); 137 var sessionStorage = new WebInspector.DOMStorage(securityOrigin, false); 138 this._storages[sessionStorageKey] = sessionStorage; 139 this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageAdded, sessionStorage); 140 }, 141 142 /** 143 * @param {WebInspector.Event} event 144 */ 145 _securityOriginRemoved: function(event) 146 { 147 var securityOrigin = /** @type {string} */ (event.data); 148 var localStorageKey = this._storageKey(securityOrigin, true); 149 var localStorage = this._storages[localStorageKey]; 150 console.assert(localStorage); 151 delete this._storages[localStorageKey]; 152 this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageRemoved, localStorage); 153 154 var sessionStorageKey = this._storageKey(securityOrigin, false); 155 var sessionStorage = this._storages[sessionStorageKey]; 156 console.assert(sessionStorage); 157 delete this._storages[sessionStorageKey]; 158 this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageRemoved, sessionStorage); 159 }, 160 161 /** 162 * @param {string} securityOrigin 163 * @param {boolean} isLocalStorage 164 * @return {string} 165 */ 166 _storageKey: function(securityOrigin, isLocalStorage) 167 { 168 return JSON.stringify(WebInspector.DOMStorage.storageId(securityOrigin, isLocalStorage)); 169 }, 170 171 /** 172 * @param {DOMStorageAgent.StorageId} storageId 173 */ 174 _domStorageItemsCleared: function(storageId) 175 { 176 var domStorage = this.storageForId(storageId); 177 var storageData = { 178 storage: domStorage 179 }; 180 this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageItemsCleared, storageData); 181 }, 182 183 /** 184 * @param {DOMStorageAgent.StorageId} storageId 185 * @param {string} key 186 */ 187 _domStorageItemRemoved: function(storageId, key) 188 { 189 var domStorage = this.storageForId(storageId); 190 var storageData = { 191 storage: domStorage, 192 key: key 193 }; 194 this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageItemRemoved, storageData); 195 }, 196 197 /** 198 * @param {DOMStorageAgent.StorageId} storageId 199 * @param {string} key 200 * @param {string} newValue 201 */ 202 _domStorageItemAdded: function(storageId, key, newValue) 203 { 204 var domStorage = this.storageForId(storageId); 205 var storageData = { 206 storage: domStorage, 207 key: key, 208 newValue: newValue 209 }; 210 this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageItemAdded, storageData); 211 }, 212 213 /** 214 * @param {DOMStorageAgent.StorageId} storageId 215 * @param {string} key 216 * @param {string} oldValue 217 * @param {string} newValue 218 */ 219 _domStorageItemUpdated: function(storageId, key, oldValue, newValue) 220 { 221 var domStorage = this._storages[storageId]; 222 var storageData = { 223 storage: domStorage, 224 key: key, 225 oldValue: oldValue, 226 newValue: newValue 227 }; 228 this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageItemUpdated, storageData); 229 }, 230 231 /** 232 * @param {DOMStorageAgent.StorageId} storageId 233 * @return {WebInspector.DOMStorage} 234 */ 235 storageForId: function(storageId) 236 { 237 return this._storages[JSON.stringify(storageId)]; 238 }, 239 240 /** 241 * @return {Array.<WebInspector.DOMStorage>} 242 */ 243 storages: function() 244 { 245 var result = []; 246 for (var id in this._storages) 247 result.push(this._storages[id]); 248 return result; 249 }, 250 251 __proto__: WebInspector.Object.prototype 252} 253 254/** 255 * @constructor 256 * @implements {DOMStorageAgent.Dispatcher} 257 * @param {WebInspector.DOMStorageModel} model 258 */ 259WebInspector.DOMStorageDispatcher = function(model) 260{ 261 this._model = model; 262} 263 264WebInspector.DOMStorageDispatcher.prototype = { 265 266 /** 267 * @param {DOMStorageAgent.StorageId} storageId 268 */ 269 domStorageItemsCleared: function(storageId) 270 { 271 this._model._domStorageItemsCleared(storageId); 272 }, 273 274 /** 275 * @param {DOMStorageAgent.StorageId} storageId 276 * @param {string} key 277 */ 278 domStorageItemRemoved: function(storageId, key) 279 { 280 this._model._domStorageItemRemoved(storageId, key); 281 }, 282 283 /** 284 * @param {DOMStorageAgent.StorageId} storageId 285 * @param {string} key 286 * @param {string} newValue 287 */ 288 domStorageItemAdded: function(storageId, key, newValue) 289 { 290 this._model._domStorageItemAdded(storageId, key, newValue); 291 }, 292 293 /** 294 * @param {DOMStorageAgent.StorageId} storageId 295 * @param {string} key 296 * @param {string} oldValue 297 * @param {string} newValue 298 */ 299 domStorageItemUpdated: function(storageId, key, oldValue, newValue) 300 { 301 this._model._domStorageItemUpdated(storageId, key, oldValue, newValue); 302 }, 303} 304 305/** 306 * @type {WebInspector.DOMStorageModel} 307 */ 308WebInspector.domStorageModel = null; 309