Web Inspector: Array/Collection Sizes should be visible and distinct
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Controllers / StorageManager.js
1 /*
2  * Copyright (C) 2013 Apple 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  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
18  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
24  * THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 WebInspector.StorageManager = function()
28 {
29     WebInspector.Object.call(this);
30
31     if (window.DOMStorageAgent)
32         DOMStorageAgent.enable();
33     if (window.DatabaseAgent)
34         DatabaseAgent.enable();
35     if (window.IndexedDBAgent)
36         IndexedDBAgent.enable();
37
38     WebInspector.Frame.addEventListener(WebInspector.Frame.Event.MainResourceDidChange, this._mainResourceDidChange, this);
39
40     WebInspector.notifications.addEventListener(WebInspector.Notification.ExtraDomainsActivated, this._extraDomainsActivated, this);
41
42     // COMPATIBILITY (iOS 6): DOMStorage was discovered via a DOMStorageObserver event. Now DOM Storage
43     // is added whenever a new securityOrigin is discovered. Check for DOMStorageAgent.getDOMStorageItems,
44     // which was renamed at the same time the change to start using securityOrigin was made.
45     if (window.DOMStorageAgent && DOMStorageAgent.getDOMStorageItems)
46         WebInspector.Frame.addEventListener(WebInspector.Frame.Event.SecurityOriginDidChange, this._securityOriginDidChange, this);
47
48     this.initialize();
49 };
50
51 WebInspector.StorageManager.Event = {
52     CookieStorageObjectWasAdded: "storage-manager-cookie-storage-object-was-added",
53     DOMStorageObjectWasAdded: "storage-manager-dom-storage-object-was-added",
54     DOMStorageObjectWasInspected: "storage-dom-object-was-inspected",
55     DatabaseWasAdded: "storage-manager-database-was-added",
56     DatabaseWasInspected: "storage-object-was-inspected",
57     IndexedDatabaseWasAdded: "storage-manager-indexed-database-was-added",
58     Cleared: "storage-manager-cleared"
59 };
60
61 WebInspector.StorageManager.prototype = {
62     constructor: WebInspector.StorageManager,
63     __proto__: WebInspector.Object.prototype,
64
65     // Public
66
67     initialize: function()
68     {
69         this._domStorageObjects = [];
70         this._databaseObjects = [];
71         this._indexedDatabases = [];
72         this._cookieStorageObjects = {};
73     },
74
75     domStorageWasAdded: function(id, host, isLocalStorage)
76     {
77         var domStorage = new WebInspector.DOMStorageObject(id, host, isLocalStorage);
78
79         this._domStorageObjects.push(domStorage);
80         this.dispatchEventToListeners(WebInspector.StorageManager.Event.DOMStorageObjectWasAdded, {domStorage: domStorage});
81     },
82
83     databaseWasAdded: function(id, host, name, version)
84     {
85         var database = new WebInspector.DatabaseObject(id, host, name, version);
86
87         this._databaseObjects.push(database);
88         this.dispatchEventToListeners(WebInspector.StorageManager.Event.DatabaseWasAdded, {database: database});
89     },
90
91     domStorageWasUpdated: function(id)
92     {
93         this.dispatchEventToListeners(WebInspector.StorageManager.Event.DOMStorageWasUpdated, id);
94     },
95
96     itemsCleared: function(storageId)
97     {
98         this._domStorageForIdentifier(storageId).itemsCleared(storageId);
99     },
100
101     itemRemoved: function(storageId, key)
102     {
103         this._domStorageForIdentifier(storageId).itemRemoved(key);
104     },
105
106     itemAdded: function(storageId, key, value)
107     {
108         this._domStorageForIdentifier(storageId).itemAdded(key, value);
109     },
110
111     itemUpdated: function(storageId, key, oldValue, value)
112     {
113         this._domStorageForIdentifier(storageId).itemUpdated(key, oldValue, value);
114     },
115
116     inspectDatabase: function(id)
117     {
118         var database = this._databaseForIdentifier(id);
119         console.assert(database);
120         if (!database)
121             return;
122         this.dispatchEventToListeners(WebInspector.StorageManager.Event.DatabaseWasInspected, {database: database});
123     },
124
125     inspectDOMStorage: function(id)
126     {
127         var domStorage = this._domStorageForIdentifier(id);
128         console.assert(domStorage);
129         if (!domStorage)
130             return;
131         this.dispatchEventToListeners(WebInspector.StorageManager.Event.DOMStorageObjectWasInspected, {domStorage: domStorage});
132     },
133
134     // Protected
135
136     requestIndexedDatabaseData: function(objectStore, objectStoreIndex, startEntryIndex, maximumEntryCount, callback)
137     {
138         console.assert(window.IndexedDBAgent);
139         console.assert(objectStore);
140         console.assert(callback);
141
142         function processData(error, entryPayloads, moreAvailable)
143         {
144             if (error) {
145                 callback(null, false);
146                 return;
147             }
148
149             var entries = [];
150
151             for (var entryPayload of entryPayloads) {
152                 var entry = {};
153                 entry.primaryKey = WebInspector.RemoteObject.fromPayload(entryPayload.primaryKey);
154                 entry.key = WebInspector.RemoteObject.fromPayload(entryPayload.key);
155                 entry.value = WebInspector.RemoteObject.fromPayload(entryPayload.value);
156                 entries.push(entry);
157             }
158
159             callback(entries, moreAvailable);
160         }
161
162         var requestArguments = {
163             securityOrigin: objectStore.parentDatabase.securityOrigin,
164             databaseName: objectStore.parentDatabase.name,
165             objectStoreName: objectStore.name,
166             indexName: objectStoreIndex && objectStoreIndex.name || "",
167             skipCount: startEntryIndex || 0,
168             pageSize: maximumEntryCount || 100
169         };
170
171         IndexedDBAgent.requestData.invoke(requestArguments, processData);
172     },
173
174     // Private
175
176     _domStorageForIdentifier: function(id)
177     {
178         for (var storageObject of this._domStorageObjects) {
179             // The id is an object, so we need to compare the properties using Object.shallowEqual.
180             // COMPATIBILITY (iOS 6): The id was a string. Object.shallowEqual works for both.
181             if (Object.shallowEqual(storageObject.id, id))
182                 return storageObject;
183         }
184
185         return null;
186     },
187
188     _mainResourceDidChange: function(event)
189     {
190         console.assert(event.target instanceof WebInspector.Frame);
191
192         if (event.target.isMainFrame()) {
193             // If we are dealing with the main frame, we want to clear our list of objects, because we are navigating to a new page.
194             this.initialize();
195             this.dispatchEventToListeners(WebInspector.StorageManager.Event.Cleared);
196
197             this._addDOMStorageIfNeeded(event.target);
198             this._addIndexedDBDatabasesIfNeeded(event.target);
199         }
200
201         // Add the host of the frame that changed the main resource to the list of hosts there could be cookies for.
202         var host = parseURL(event.target.url).host;
203         if (!host)
204             return;
205
206         if (this._cookieStorageObjects[host])
207             return;
208
209         this._cookieStorageObjects[host] = new WebInspector.CookieStorageObject(host);
210         this.dispatchEventToListeners(WebInspector.StorageManager.Event.CookieStorageObjectWasAdded, {cookieStorage: this._cookieStorageObjects[host]});
211     },
212
213     _addDOMStorageIfNeeded: function(frame)
214     {
215         // Don't show storage if we don't have a security origin (about:blank).
216         if (!frame.securityOrigin || frame.securityOrigin === "://")
217             return;
218
219         // FIXME: Consider passing the other parts of the origin along to domStorageWasAdded.
220
221         var localStorageIdentifier = {securityOrigin: frame.securityOrigin, isLocalStorage: true};
222         if (!this._domStorageForIdentifier(localStorageIdentifier))
223             this.domStorageWasAdded(localStorageIdentifier, frame.mainResource.urlComponents.host, true);
224
225         var sessionStorageIdentifier = {securityOrigin: frame.securityOrigin, isLocalStorage: false};
226         if (!this._domStorageForIdentifier(sessionStorageIdentifier))
227             this.domStorageWasAdded(sessionStorageIdentifier, frame.mainResource.urlComponents.host, false);
228     },
229
230     _addIndexedDBDatabasesIfNeeded: function(frame)
231     {
232         if (!window.IndexedDBAgent)
233             return;
234
235         var securityOrigin = frame.securityOrigin;
236
237         // Don't show storage if we don't have a security origin (about:blank).
238         if (!securityOrigin || securityOrigin === "://")
239             return;
240
241         function processDatabaseNames(error, names)
242         {
243             if (error || !names)
244                 return;
245
246             for (var name of names)
247                 IndexedDBAgent.requestDatabase(securityOrigin, name, processDatabase.bind(this));
248         }
249
250         function processDatabase(error, databasePayload)
251         {
252             if (error || !databasePayload)
253                 return;
254
255             var objectStores = databasePayload.objectStores.map(processObjectStore);
256             var indexedDatabase = new WebInspector.IndexedDatabase(databasePayload.name, securityOrigin, databasePayload.version, objectStores);
257
258             this._indexedDatabases.push(indexedDatabase);
259             this.dispatchEventToListeners(WebInspector.StorageManager.Event.IndexedDatabaseWasAdded, {indexedDatabase: indexedDatabase});
260         }
261
262         function processKeyPath(keyPathPayload)
263         {
264             switch (keyPathPayload.type) {
265             case "null":
266                 return null;
267             case "string":
268                 return keyPathPayload.string;
269             case "array":
270                 return keyPathPayload.array;
271             default:
272                 console.error("Unknown KeyPath type:", keyPathPayload.type);
273                 return null;
274             }
275         }
276
277         function processObjectStore(objectStorePayload)
278         {
279             var keyPath = processKeyPath(objectStorePayload.keyPath);
280             var indexes = objectStorePayload.indexes.map(processObjectStoreIndex);
281             return new WebInspector.IndexedDatabaseObjectStore(objectStorePayload.name, keyPath, objectStorePayload.autoIncrement, indexes);
282         }
283
284         function processObjectStoreIndex(objectStoreIndexPayload)
285         {
286             var keyPath = processKeyPath(objectStoreIndexPayload.keyPath);
287             return new WebInspector.IndexedDatabaseObjectStoreIndex(objectStoreIndexPayload.name, keyPath, objectStoreIndexPayload.unique, objectStoreIndexPayload.multiEntry);
288         }
289
290         IndexedDBAgent.requestDatabaseNames(securityOrigin, processDatabaseNames.bind(this));
291     },
292
293     _securityOriginDidChange: function(event)
294     {
295         console.assert(event.target instanceof WebInspector.Frame);
296
297         this._addDOMStorageIfNeeded(event.target);
298         this._addIndexedDBDatabasesIfNeeded(event.target);
299     },
300
301     _databaseForIdentifier: function(id)
302     {
303         for (var i = 0; i < this._databaseObjects.length; ++i) {
304             if (this._databaseObjects[i].id === id)
305                 return this._databaseObjects[i];
306         }
307
308         return null;
309     },
310
311     _extraDomainsActivated: function(event)
312     {
313         
314         if (event.data.domains.contains("DOMStorage") && window.DOMStorageAgent && DOMStorageAgent.getDOMStorageItems)
315             WebInspector.Frame.addEventListener(WebInspector.Frame.Event.SecurityOriginDidChange, this._securityOriginDidChange, this);
316     }
317 };