Web Inspector: IndexedDB data not showing for MDN example page
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / StorageSidebarPanel.js
1 /*
2  * Copyright (C) 2013, 2015 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 WebInspector.StorageSidebarPanel = class StorageSidebarPanel extends WebInspector.NavigationSidebarPanel
27 {
28     constructor(contentBrowser)
29     {
30         super("storage", WebInspector.UIString("Storage"));
31
32         this.contentBrowser = contentBrowser;
33
34         this.filterBar.placeholder = WebInspector.UIString("Filter Storage List");
35
36         this._localStorageRootTreeElement = null;
37         this._sessionStorageRootTreeElement = null;
38
39         this._databaseRootTreeElement = null;
40         this._databaseHostTreeElementMap = {};
41
42         this._indexedDatabaseRootTreeElement = null;
43         this._indexedDatabaseHostTreeElementMap = {};
44
45         this._cookieStorageRootTreeElement = null;
46
47         this._applicationCacheRootTreeElement = null;
48         this._applicationCacheURLTreeElementMap = {};
49
50         WebInspector.storageManager.addEventListener(WebInspector.StorageManager.Event.CookieStorageObjectWasAdded, this._cookieStorageObjectWasAdded, this);
51         WebInspector.storageManager.addEventListener(WebInspector.StorageManager.Event.DOMStorageObjectWasAdded, this._domStorageObjectWasAdded, this);
52         WebInspector.storageManager.addEventListener(WebInspector.StorageManager.Event.DOMStorageObjectWasInspected, this._domStorageObjectWasInspected, this);
53         WebInspector.storageManager.addEventListener(WebInspector.StorageManager.Event.DatabaseWasAdded, this._databaseWasAdded, this);
54         WebInspector.storageManager.addEventListener(WebInspector.StorageManager.Event.DatabaseWasInspected, this._databaseWasInspected, this);
55         WebInspector.storageManager.addEventListener(WebInspector.StorageManager.Event.IndexedDatabaseWasAdded, this._indexedDatabaseWasAdded, this);
56         WebInspector.storageManager.addEventListener(WebInspector.StorageManager.Event.Cleared, this._storageCleared, this);
57
58         WebInspector.applicationCacheManager.addEventListener(WebInspector.ApplicationCacheManager.Event.FrameManifestAdded, this._frameManifestAdded, this);
59         WebInspector.applicationCacheManager.addEventListener(WebInspector.ApplicationCacheManager.Event.FrameManifestRemoved, this._frameManifestRemoved, this);
60
61         this.contentTreeOutline.onselect = this._treeElementSelected.bind(this);
62
63         for (var domStorageObject of WebInspector.storageManager.domStorageObjects)
64             this._addDOMStorageObject(domStorageObject);
65
66         for (var cookieStorageObject of WebInspector.storageManager.cookieStorageObjects)
67             this._addCookieStorageObject(cookieStorageObject);
68
69         for (var database of WebInspector.storageManager.databases)
70             this._addDatabase(database);
71
72         for (var indexedDatabase of WebInspector.storageManager.indexedDatabases)
73             this._addIndexedDatabase(indexedDatabase);
74
75         for (var applicationCacheObject of WebInspector.applicationCacheManager.applicationCacheObjects)
76             this._addFrameManifest(applicationCacheObject);
77     }
78
79     // Public
80
81     showDefaultContentView()
82     {
83         // Don't show anything by default. It doesn't make a whole lot of sense here.
84     }
85
86     closed()
87     {
88         super.closed();
89
90         WebInspector.storageManager.removeEventListener(null, null, this);
91         WebInspector.applicationCacheManager.removeEventListener(null, null, this);
92     }
93
94     // Private
95
96     _treeElementSelected(treeElement, selectedByUser)
97     {
98         if (treeElement instanceof WebInspector.FolderTreeElement || treeElement instanceof WebInspector.DatabaseHostTreeElement ||
99             treeElement instanceof WebInspector.IndexedDatabaseHostTreeElement || treeElement instanceof WebInspector.IndexedDatabaseTreeElement)
100             return;
101
102         if (treeElement instanceof WebInspector.StorageTreeElement || treeElement instanceof WebInspector.DatabaseTableTreeElement ||
103             treeElement instanceof WebInspector.DatabaseTreeElement || treeElement instanceof WebInspector.ApplicationCacheFrameTreeElement ||
104             treeElement instanceof WebInspector.IndexedDatabaseObjectStoreTreeElement || treeElement instanceof WebInspector.IndexedDatabaseObjectStoreIndexTreeElement) {
105             WebInspector.showRepresentedObject(treeElement.representedObject);
106             return;
107         }
108
109         console.error("Unknown tree element", treeElement);
110     }
111
112     _domStorageObjectWasAdded(event)
113     {
114         var domStorage = event.data.domStorage;
115         this._addDOMStorageObject(event.data.domStorage);
116     }
117
118     _addDOMStorageObject(domStorage)
119     {
120         var storageElement = new WebInspector.DOMStorageTreeElement(domStorage);
121
122         if (domStorage.isLocalStorage())
123             this._localStorageRootTreeElement = this._addStorageChild(storageElement, this._localStorageRootTreeElement, WebInspector.UIString("Local Storage"));
124         else
125             this._sessionStorageRootTreeElement = this._addStorageChild(storageElement, this._sessionStorageRootTreeElement, WebInspector.UIString("Session Storage"));
126     }
127
128     _domStorageObjectWasInspected(event)
129     {
130         var domStorage = event.data.domStorage;
131         var treeElement = this.treeElementForRepresentedObject(domStorage);
132         treeElement.revealAndSelect(true);
133     }
134
135     _databaseWasAdded(event)
136     {
137         var database = event.data.database;
138         this._addDatabase(event.data.database);
139     }
140
141     _addDatabase(database)
142     {
143         console.assert(database instanceof WebInspector.DatabaseObject);
144
145         if (!this._databaseHostTreeElementMap[database.host]) {
146             this._databaseHostTreeElementMap[database.host] = new WebInspector.DatabaseHostTreeElement(database.host);
147             this._databaseRootTreeElement = this._addStorageChild(this._databaseHostTreeElementMap[database.host], this._databaseRootTreeElement, WebInspector.UIString("Databases"));
148         }
149
150         var databaseElement = new WebInspector.DatabaseTreeElement(database);
151         this._databaseHostTreeElementMap[database.host].appendChild(databaseElement);
152     }
153
154     _databaseWasInspected(event)
155     {
156         var database = event.data.database;
157         var treeElement = this.treeElementForRepresentedObject(database);
158         treeElement.revealAndSelect(true);
159     }
160
161     _indexedDatabaseWasAdded(event)
162     {
163         this._addIndexedDatabase(event.data.indexedDatabase);
164     }
165
166     _addIndexedDatabase(indexedDatabase)
167     {
168         console.assert(indexedDatabase instanceof WebInspector.IndexedDatabase);
169
170         if (!this._indexedDatabaseHostTreeElementMap[indexedDatabase.host]) {
171             this._indexedDatabaseHostTreeElementMap[indexedDatabase.host] = new WebInspector.IndexedDatabaseHostTreeElement(indexedDatabase.host);
172             this._indexedDatabaseRootTreeElement = this._addStorageChild(this._indexedDatabaseHostTreeElementMap[indexedDatabase.host], this._indexedDatabaseRootTreeElement, WebInspector.UIString("Indexed Databases"));
173         }
174
175         var indexedDatabaseElement = new WebInspector.IndexedDatabaseTreeElement(indexedDatabase);
176         this._indexedDatabaseHostTreeElementMap[indexedDatabase.host].appendChild(indexedDatabaseElement);
177     }
178
179     _cookieStorageObjectWasAdded(event)
180     {
181         this._addCookieStorageObject(event.data.cookieStorage);
182     }
183
184     _addCookieStorageObject(cookieStorage)
185     {
186         console.assert(cookieStorage instanceof WebInspector.CookieStorageObject);
187
188         var cookieElement = new WebInspector.CookieStorageTreeElement(cookieStorage);
189         this._cookieStorageRootTreeElement = this._addStorageChild(cookieElement, this._cookieStorageRootTreeElement, WebInspector.UIString("Cookies"));
190     }
191
192     _frameManifestAdded(event)
193     {
194         this._addFrameManifest(event.data.frameManifest);
195     }
196
197     _addFrameManifest(frameManifest)
198     {
199         console.assert(frameManifest instanceof WebInspector.ApplicationCacheFrame);
200
201         var manifest = frameManifest.manifest;
202         var manifestURL = manifest.manifestURL;
203         if (!this._applicationCacheURLTreeElementMap[manifestURL]) {
204             this._applicationCacheURLTreeElementMap[manifestURL] = new WebInspector.ApplicationCacheManifestTreeElement(manifest);
205             this._applicationCacheRootTreeElement = this._addStorageChild(this._applicationCacheURLTreeElementMap[manifestURL], this._applicationCacheRootTreeElement, WebInspector.UIString("Application Cache"));
206         }
207
208         var frameCacheElement = new WebInspector.ApplicationCacheFrameTreeElement(frameManifest);
209         this._applicationCacheURLTreeElementMap[manifestURL].appendChild(frameCacheElement);
210     }
211
212     _frameManifestRemoved(event)
213     {
214          // FIXME: Implement this.
215     }
216
217     _compareTreeElements(a, b)
218     {
219         console.assert(a.mainTitle);
220         console.assert(b.mainTitle);
221
222         return (a.mainTitle || "").localeCompare(b.mainTitle || "");
223     }
224
225     _addStorageChild(childElement, parentElement, folderName)
226     {
227         if (!parentElement) {
228             childElement.flattened = true;
229
230             this.contentTreeOutline.insertChild(childElement, insertionIndexForObjectInListSortedByFunction(childElement, this.contentTreeOutline.children, this._compareTreeElements));
231
232             return childElement;
233         }
234
235         if (parentElement instanceof WebInspector.StorageTreeElement) {
236             console.assert(parentElement.flattened);
237
238             var previousOnlyChild = parentElement;
239             previousOnlyChild.flattened = false;
240             this.contentTreeOutline.removeChild(previousOnlyChild);
241
242             var folderElement = new WebInspector.FolderTreeElement(folderName);
243             this.contentTreeOutline.insertChild(folderElement, insertionIndexForObjectInListSortedByFunction(folderElement, this.contentTreeOutline.children, this._compareTreeElements));
244
245             folderElement.appendChild(previousOnlyChild);
246             folderElement.insertChild(childElement, insertionIndexForObjectInListSortedByFunction(childElement, folderElement.children, this._compareTreeElements));
247
248             return folderElement;
249         }
250
251         console.assert(parentElement instanceof WebInspector.FolderTreeElement);
252         parentElement.insertChild(childElement, insertionIndexForObjectInListSortedByFunction(childElement, parentElement.children, this._compareTreeElements));
253
254         return parentElement;
255     }
256
257     _storageCleared(event)
258     {
259         // Close all DOM and cookie storage content views since the main frame has navigated and all storages are cleared.
260         this.contentBrowser.contentViewContainer.closeAllContentViewsOfPrototype(WebInspector.CookieStorageContentView);
261         this.contentBrowser.contentViewContainer.closeAllContentViewsOfPrototype(WebInspector.DOMStorageContentView);
262         this.contentBrowser.contentViewContainer.closeAllContentViewsOfPrototype(WebInspector.DatabaseTableContentView);
263         this.contentBrowser.contentViewContainer.closeAllContentViewsOfPrototype(WebInspector.DatabaseContentView);
264         this.contentBrowser.contentViewContainer.closeAllContentViewsOfPrototype(WebInspector.ApplicationCacheFrameContentView);
265
266         if (this._localStorageRootTreeElement && this._localStorageRootTreeElement.parent)
267             this._localStorageRootTreeElement.parent.removeChild(this._localStorageRootTreeElement);
268
269         if (this._sessionStorageRootTreeElement && this._sessionStorageRootTreeElement.parent)
270             this._sessionStorageRootTreeElement.parent.removeChild(this._sessionStorageRootTreeElement);
271
272         if (this._databaseRootTreeElement && this._databaseRootTreeElement.parent)
273             this._databaseRootTreeElement.parent.removeChild(this._databaseRootTreeElement);
274
275         if (this._indexedDatabaseRootTreeElement && this._indexedDatabaseRootTreeElement.parent)
276             this._indexedDatabaseRootTreeElement.parent.removeChild(this._indexedDatabaseRootTreeElement);
277
278         if (this._cookieStorageRootTreeElement && this._cookieStorageRootTreeElement.parent)
279             this._cookieStorageRootTreeElement.parent.removeChild(this._cookieStorageRootTreeElement);
280
281         if (this._applicationCacheRootTreeElement && this._applicationCacheRootTreeElement.parent)
282             this._applicationCacheRootTreeElement.parent.removeChild(this._applicationCacheRootTreeElement);
283
284         this._localStorageRootTreeElement = null;
285         this._sessionStorageRootTreeElement = null;
286         this._databaseRootTreeElement = null;
287         this._databaseHostTreeElementMap = {};
288         this._indexedDatabaseRootTreeElement = null;
289         this._indexedDatabaseHostTreeElementMap = {};
290         this._cookieStorageRootTreeElement = null;
291         this._applicationCacheRootTreeElement = null;
292         this._applicationCacheURLTreeElementMap = {};
293     }
294 };