Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / front_end / DOMStorage.js
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  * @extends {WebInspector.Object}
33  * @param {string} securityOrigin
34  * @param {boolean} isLocalStorage
35  */
36 WebInspector.DOMStorage = function(securityOrigin, isLocalStorage)
37 {
38     this._securityOrigin = securityOrigin;
39     this._isLocalStorage = isLocalStorage;
40 }
41
42 /**
43  * @param {string} securityOrigin
44  * @param {boolean} isLocalStorage
45  * @return {!DOMStorageAgent.StorageId}
46  */
47 WebInspector.DOMStorage.storageId = function(securityOrigin, isLocalStorage)
48 {
49     return { securityOrigin: securityOrigin, isLocalStorage: isLocalStorage };
50 }
51
52 WebInspector.DOMStorage.Events = {
53     DOMStorageItemsCleared: "DOMStorageItemsCleared",
54     DOMStorageItemRemoved: "DOMStorageItemRemoved",
55     DOMStorageItemAdded: "DOMStorageItemAdded",
56     DOMStorageItemUpdated: "DOMStorageItemUpdated"
57 }
58
59 WebInspector.DOMStorage.prototype = {
60
61     /** @return {!DOMStorageAgent.StorageId} */
62     get id()
63     {
64         return WebInspector.DOMStorage.storageId(this._securityOrigin, this._isLocalStorage);
65     },
66
67     /** @return {string} */
68     get securityOrigin()
69     {
70         return this._securityOrigin;
71     },
72
73     /** @return {boolean} */
74     get isLocalStorage()
75     {
76         return this._isLocalStorage;
77     },
78
79     /**
80      * @param {function(?Protocol.Error, !Array.<!DOMStorageAgent.Item>):void=} callback
81      */
82     getItems: function(callback)
83     {
84         DOMStorageAgent.getDOMStorageItems(this.id, callback);
85     },
86
87     /**
88      * @param {string} key
89      * @param {string} value
90      */
91     setItem: function(key, value)
92     {
93         DOMStorageAgent.setDOMStorageItem(this.id, key, value);
94     },
95
96     /**
97      * @param {string} key
98      */
99     removeItem: function(key)
100     {
101         DOMStorageAgent.removeDOMStorageItem(this.id, key);
102     },
103
104     __proto__: WebInspector.Object.prototype
105 }
106
107 /**
108  * @constructor
109  * @extends {WebInspector.Object}
110  */
111 WebInspector.DOMStorageModel = function()
112 {
113     /** @type {!Object.<string, !WebInspector.DOMStorage>} */
114     this._storages = {};
115     InspectorBackend.registerDOMStorageDispatcher(new WebInspector.DOMStorageDispatcher(this));
116     DOMStorageAgent.enable();
117     WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.SecurityOriginAdded, this._securityOriginAdded, this);
118     WebInspector.resourceTreeModel.addEventListener(WebInspector.ResourceTreeModel.EventTypes.SecurityOriginRemoved, this._securityOriginRemoved, this);
119 }
120
121 WebInspector.DOMStorageModel.Events = {
122     DOMStorageAdded: "DOMStorageAdded",
123     DOMStorageRemoved: "DOMStorageRemoved"
124 }
125
126 WebInspector.DOMStorageModel.prototype = {
127
128     /**
129      * @param {!WebInspector.Event} event
130      */
131     _securityOriginAdded: function(event)
132     {
133         var securityOrigin = /** @type {string} */ (event.data);
134         var localStorageKey = this._storageKey(securityOrigin, true);
135         console.assert(!this._storages[localStorageKey]);
136         var localStorage = new WebInspector.DOMStorage(securityOrigin, true);
137         this._storages[localStorageKey] = localStorage;
138         this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageAdded, localStorage);
139
140         var sessionStorageKey = this._storageKey(securityOrigin, false);
141         console.assert(!this._storages[sessionStorageKey]);
142         var sessionStorage = new WebInspector.DOMStorage(securityOrigin, false);
143         this._storages[sessionStorageKey] = sessionStorage;
144         this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageAdded, sessionStorage);
145     },
146
147     /**
148      * @param {!WebInspector.Event} event
149      */
150     _securityOriginRemoved: function(event)
151     {
152         var securityOrigin = /** @type {string} */ (event.data);
153         var localStorageKey = this._storageKey(securityOrigin, true);
154         var localStorage = this._storages[localStorageKey];
155         console.assert(localStorage);
156         delete this._storages[localStorageKey];
157         this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageRemoved, localStorage);
158
159         var sessionStorageKey = this._storageKey(securityOrigin, false);
160         var sessionStorage = this._storages[sessionStorageKey];
161         console.assert(sessionStorage);
162         delete this._storages[sessionStorageKey];
163         this.dispatchEventToListeners(WebInspector.DOMStorageModel.Events.DOMStorageRemoved, sessionStorage);
164     },
165
166     /**
167      * @param {string} securityOrigin
168      * @param {boolean} isLocalStorage
169      * @return {string}
170      */
171     _storageKey: function(securityOrigin, isLocalStorage)
172     {
173         return JSON.stringify(WebInspector.DOMStorage.storageId(securityOrigin, isLocalStorage));
174     },
175
176     /**
177      * @param {!DOMStorageAgent.StorageId} storageId
178      */
179     _domStorageItemsCleared: function(storageId)
180     {
181         var domStorage = this.storageForId(storageId);
182         if (!domStorage)
183             return;
184
185         var eventData = {};
186         domStorage.dispatchEventToListeners(WebInspector.DOMStorage.Events.DOMStorageItemsCleared, eventData);
187     },
188
189     /**
190      * @param {!DOMStorageAgent.StorageId} storageId
191      * @param {string} key
192      */
193     _domStorageItemRemoved: function(storageId, key)
194     {
195         var domStorage = this.storageForId(storageId);
196         if (!domStorage)
197             return;
198
199         var eventData = { key: key };
200         domStorage.dispatchEventToListeners(WebInspector.DOMStorage.Events.DOMStorageItemRemoved, eventData);
201     },
202
203     /**
204      * @param {!DOMStorageAgent.StorageId} storageId
205      * @param {string} key
206      * @param {string} value
207      */
208     _domStorageItemAdded: function(storageId, key, value)
209     {
210         var domStorage = this.storageForId(storageId);
211         if (!domStorage)
212             return;
213
214         var eventData = { key: key, value: value };
215         domStorage.dispatchEventToListeners(WebInspector.DOMStorage.Events.DOMStorageItemAdded, eventData);
216     },
217
218     /**
219      * @param {!DOMStorageAgent.StorageId} storageId
220      * @param {string} key
221      * @param {string} oldValue
222      * @param {string} value
223      */
224     _domStorageItemUpdated: function(storageId, key, oldValue, value)
225     {
226         var domStorage = this.storageForId(storageId);
227         if (!domStorage)
228             return;
229
230         var eventData = { key: key, oldValue: oldValue, value: value };
231         domStorage.dispatchEventToListeners(WebInspector.DOMStorage.Events.DOMStorageItemUpdated, eventData);
232     },
233
234     /**
235      * @param {!DOMStorageAgent.StorageId} storageId
236      * @return {!WebInspector.DOMStorage}
237      */
238     storageForId: function(storageId)
239     {
240         return this._storages[JSON.stringify(storageId)];
241     },
242
243     /**
244      * @return {!Array.<!WebInspector.DOMStorage>}
245      */
246     storages: function()
247     {
248         var result = [];
249         for (var id in this._storages)
250             result.push(this._storages[id]);
251         return result;
252     },
253
254     __proto__: WebInspector.Object.prototype
255 }
256
257 /**
258  * @constructor
259  * @implements {DOMStorageAgent.Dispatcher}
260  * @param {!WebInspector.DOMStorageModel} model
261  */
262 WebInspector.DOMStorageDispatcher = function(model)
263 {
264     this._model = model;
265 }
266
267 WebInspector.DOMStorageDispatcher.prototype = {
268
269     /**
270      * @param {!DOMStorageAgent.StorageId} storageId
271      */
272     domStorageItemsCleared: function(storageId)
273     {
274         this._model._domStorageItemsCleared(storageId);
275     },
276
277     /**
278      * @param {!DOMStorageAgent.StorageId} storageId
279      * @param {string} key
280      */
281     domStorageItemRemoved: function(storageId, key)
282     {
283         this._model._domStorageItemRemoved(storageId, key);
284     },
285
286     /**
287      * @param {!DOMStorageAgent.StorageId} storageId
288      * @param {string} key
289      * @param {string} value
290      */
291     domStorageItemAdded: function(storageId, key, value)
292     {
293         this._model._domStorageItemAdded(storageId, key, value);
294     },
295
296     /**
297      * @param {!DOMStorageAgent.StorageId} storageId
298      * @param {string} key
299      * @param {string} oldValue
300      * @param {string} value
301      */
302     domStorageItemUpdated: function(storageId, key, oldValue, value)
303     {
304         this._model._domStorageItemUpdated(storageId, key, oldValue, value);
305     },
306 }
307
308 /**
309  * @type {!WebInspector.DOMStorageModel}
310  */
311 WebInspector.domStorageModel;