Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / resources / DOMStorageModel.js
blob411ea013da8f8580822a8236a0d4a4639c2874bf
1 /*
2 * Copyright (C) 2008 Nokia Inc. All rights reserved.
3 * Copyright (C) 2013 Samsung Electronics. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
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.
30 /**
31 * @constructor
32 * @extends {WebInspector.Object}
33 * @param {!WebInspector.DOMStorageModel} model
34 * @param {string} securityOrigin
35 * @param {boolean} isLocalStorage
37 WebInspector.DOMStorage = function(model, securityOrigin, isLocalStorage)
39 this._model = model;
40 this._securityOrigin = securityOrigin;
41 this._isLocalStorage = isLocalStorage;
44 /**
45 * @param {string} securityOrigin
46 * @param {boolean} isLocalStorage
47 * @return {!DOMStorageAgent.StorageId}
49 WebInspector.DOMStorage.storageId = function(securityOrigin, isLocalStorage)
51 return { securityOrigin: securityOrigin, isLocalStorage: isLocalStorage };
54 WebInspector.DOMStorage.Events = {
55 DOMStorageItemsCleared: "DOMStorageItemsCleared",
56 DOMStorageItemRemoved: "DOMStorageItemRemoved",
57 DOMStorageItemAdded: "DOMStorageItemAdded",
58 DOMStorageItemUpdated: "DOMStorageItemUpdated"
61 WebInspector.DOMStorage.prototype = {
63 /** @return {!DOMStorageAgent.StorageId} */
64 get id()
66 return WebInspector.DOMStorage.storageId(this._securityOrigin, this._isLocalStorage);
69 /** @return {string} */
70 get securityOrigin()
72 return this._securityOrigin;
75 /** @return {boolean} */
76 get isLocalStorage()
78 return this._isLocalStorage;
81 /**
82 * @param {function(?Protocol.Error, !Array.<!DOMStorageAgent.Item>):void=} callback
84 getItems: function(callback)
86 this._model._agent.getDOMStorageItems(this.id, callback);
89 /**
90 * @param {string} key
91 * @param {string} value
93 setItem: function(key, value)
95 this._model._agent.setDOMStorageItem(this.id, key, value);
98 /**
99 * @param {string} key
101 removeItem: function(key)
103 this._model._agent.removeDOMStorageItem(this.id, key);
106 __proto__: WebInspector.Object.prototype
110 * @constructor
111 * @extends {WebInspector.SDKModel}
112 * @param {!WebInspector.Target} target
114 WebInspector.DOMStorageModel = function(target)
116 WebInspector.SDKModel.call(this, WebInspector.DOMStorageModel, target);
118 /** @type {!Object.<string, !WebInspector.DOMStorage>} */
119 this._storages = {};
120 this._agent = target.domstorageAgent();
123 WebInspector.DOMStorageModel.Events = {
124 DOMStorageAdded: "DOMStorageAdded",
125 DOMStorageRemoved: "DOMStorageRemoved"
128 WebInspector.DOMStorageModel.prototype = {
129 enable: function()
131 if (this._enabled)
132 return;
134 this.target().registerDOMStorageDispatcher(new WebInspector.DOMStorageDispatcher(this));
135 this.target().resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.SecurityOriginAdded, this._securityOriginAdded, this);
136 this.target().resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.SecurityOriginRemoved, this._securityOriginRemoved, this);
137 this._agent.enable();
139 var securityOrigins = this.target().resourceTreeModel.securityOrigins();
140 for (var i = 0; i < securityOrigins.length; ++i)
141 this._addOrigin(securityOrigins[i]);
143 this._enabled = true;
147 * @param {!WebInspector.Event} event
149 _securityOriginAdded: function(event)
151 this._addOrigin(/** @type {string} */ (event.data));
156 * @param {string} securityOrigin
158 _addOrigin: function(securityOrigin)
160 var localStorageKey = this._storageKey(securityOrigin, true);
161 console.assert(!this._storages[localStorageKey]);
162 var localStorage = new WebInspector.DOMStorage(this, securityOrigin, true);
163 this._storages[localStorageKey] = localStorage;
164 this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageAdded, localStorage);
166 var sessionStorageKey = this._storageKey(securityOrigin, false);
167 console.assert(!this._storages[sessionStorageKey]);
168 var sessionStorage = new WebInspector.DOMStorage(this, securityOrigin, false);
169 this._storages[sessionStorageKey] = sessionStorage;
170 this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageAdded, sessionStorage);
174 * @param {!WebInspector.Event} event
176 _securityOriginRemoved: function(event)
178 var securityOrigin = /** @type {string} */ (event.data);
179 var localStorageKey = this._storageKey(securityOrigin, true);
180 var localStorage = this._storages[localStorageKey];
181 console.assert(localStorage);
182 delete this._storages[localStorageKey];
183 this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageRemoved, localStorage);
185 var sessionStorageKey = this._storageKey(securityOrigin, false);
186 var sessionStorage = this._storages[sessionStorageKey];
187 console.assert(sessionStorage);
188 delete this._storages[sessionStorageKey];
189 this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageRemoved, sessionStorage);
193 * @param {string} securityOrigin
194 * @param {boolean} isLocalStorage
195 * @return {string}
197 _storageKey: function(securityOrigin, isLocalStorage)
199 return JSON.stringify(WebInspector.DOMStorage.storageId(securityOrigin, isLocalStorage));
203 * @param {!DOMStorageAgent.StorageId} storageId
205 _domStorageItemsCleared: function(storageId)
207 var domStorage = this.storageForId(storageId);
208 if (!domStorage)
209 return;
211 var eventData = {};
212 domStorage.dispatchEventToListeners(WebInspector.DOMStorage.Events.DOMStorageItemsCleared, eventData);
216 * @param {!DOMStorageAgent.StorageId} storageId
217 * @param {string} key
219 _domStorageItemRemoved: function(storageId, key)
221 var domStorage = this.storageForId(storageId);
222 if (!domStorage)
223 return;
225 var eventData = { key: key };
226 domStorage.dispatchEventToListeners(WebInspector.DOMStorage.Events.DOMStorageItemRemoved, eventData);
230 * @param {!DOMStorageAgent.StorageId} storageId
231 * @param {string} key
232 * @param {string} value
234 _domStorageItemAdded: function(storageId, key, value)
236 var domStorage = this.storageForId(storageId);
237 if (!domStorage)
238 return;
240 var eventData = { key: key, value: value };
241 domStorage.dispatchEventToListeners(WebInspector.DOMStorage.Events.DOMStorageItemAdded, eventData);
245 * @param {!DOMStorageAgent.StorageId} storageId
246 * @param {string} key
247 * @param {string} oldValue
248 * @param {string} value
250 _domStorageItemUpdated: function(storageId, key, oldValue, value)
252 var domStorage = this.storageForId(storageId);
253 if (!domStorage)
254 return;
256 var eventData = { key: key, oldValue: oldValue, value: value };
257 domStorage.dispatchEventToListeners(WebInspector.DOMStorage.Events.DOMStorageItemUpdated, eventData);
261 * @param {!DOMStorageAgent.StorageId} storageId
262 * @return {!WebInspector.DOMStorage}
264 storageForId: function(storageId)
266 return this._storages[JSON.stringify(storageId)];
270 * @return {!Array.<!WebInspector.DOMStorage>}
272 storages: function()
274 var result = [];
275 for (var id in this._storages)
276 result.push(this._storages[id]);
277 return result;
280 __proto__: WebInspector.SDKModel.prototype
284 * @constructor
285 * @implements {DOMStorageAgent.Dispatcher}
286 * @param {!WebInspector.DOMStorageModel} model
288 WebInspector.DOMStorageDispatcher = function(model)
290 this._model = model;
293 WebInspector.DOMStorageDispatcher.prototype = {
296 * @override
297 * @param {!DOMStorageAgent.StorageId} storageId
299 domStorageItemsCleared: function(storageId)
301 this._model._domStorageItemsCleared(storageId);
305 * @override
306 * @param {!DOMStorageAgent.StorageId} storageId
307 * @param {string} key
309 domStorageItemRemoved: function(storageId, key)
311 this._model._domStorageItemRemoved(storageId, key);
315 * @override
316 * @param {!DOMStorageAgent.StorageId} storageId
317 * @param {string} key
318 * @param {string} value
320 domStorageItemAdded: function(storageId, key, value)
322 this._model._domStorageItemAdded(storageId, key, value);
326 * @override
327 * @param {!DOMStorageAgent.StorageId} storageId
328 * @param {string} key
329 * @param {string} oldValue
330 * @param {string} value
332 domStorageItemUpdated: function(storageId, key, oldValue, value)
334 this._model._domStorageItemUpdated(storageId, key, oldValue, value);
338 WebInspector.DOMStorageModel._symbol = Symbol("DomStorage");
340 * @param {!WebInspector.Target} target
341 * @return {!WebInspector.DOMStorageModel}
343 WebInspector.DOMStorageModel.fromTarget = function(target)
345 if (!target[WebInspector.DOMStorageModel._symbol])
346 target[WebInspector.DOMStorageModel._symbol] = new WebInspector.DOMStorageModel(target);
348 return target[WebInspector.DOMStorageModel._symbol];