Web Inspector: [Resources] Prefactorings in DataGrid and CookieTable
[WebKit-https.git] / Source / WebCore / inspector / front-end / CookiesTable.js
1 /*
2  * Copyright (C) 2009 Apple Inc.  All rights reserved.
3  * Copyright (C) 2009 Joseph Pecoraro
4  * Copyright (C) 2010 Google Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1.  Redistributions of source code must retain the above copyright
11  *     notice, this list of conditions and the following disclaimer.
12  * 2.  Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in the
14  *     documentation and/or other materials provided with the distribution.
15  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
16  *     its contributors may be used to endorse or promote products derived
17  *     from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
20  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
23  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 /**
32  * @constructor
33  * @extends {WebInspector.View}
34  * @param {boolean} expandable
35  * @param {function()=} refreshCallback
36  */
37 WebInspector.CookiesTable = function(expandable, refreshCallback)
38 {
39     WebInspector.View.call(this);
40     this.element.className = "fill";
41
42     var readOnly = expandable;
43     this._refreshCallback = refreshCallback;
44
45     var columns = {name: {}, value: {}, domain: {}, path: {}, expires: {}, size: {}, httpOnly: {}, secure: {}};
46     columns.name.title = WebInspector.UIString("Name");
47     columns.name.sortable = true;
48     columns.name.disclosure = expandable;
49     columns.name.width = "24%";
50     columns.name.sort = "ascending";
51     columns.value.title = WebInspector.UIString("Value");
52     columns.value.sortable = true;
53     columns.value.width = "34%";
54     columns.domain.title = WebInspector.UIString("Domain");
55     columns.domain.sortable = true;
56     columns.domain.width = "7%";
57     columns.path.title = WebInspector.UIString("Path");
58     columns.path.sortable = true;
59     columns.path.width = "7%";
60     columns.expires.title = WebInspector.UIString("Expires / Max-Age");
61     columns.expires.sortable = true;
62     columns.expires.width = "7%";
63     columns.size.title = WebInspector.UIString("Size");
64     columns.size.aligned = "right";
65     columns.size.sortable = true;
66     columns.size.width = "7%";
67     columns.httpOnly.title = WebInspector.UIString("HTTP");
68     columns.httpOnly.aligned = "centered";
69     columns.httpOnly.sortable = true;
70     columns.httpOnly.width = "7%";
71     columns.secure.title = WebInspector.UIString("Secure");
72     columns.secure.aligned = "centered";
73     columns.secure.sortable = true;
74     columns.secure.width = "7%";
75
76     this._dataGrid = new WebInspector.DataGrid(columns, null, readOnly ? null : this._onDeleteCookie.bind(this), refreshCallback);
77     this._dataGrid.addEventListener("sorting changed", this._rebuildTable, this);
78
79     this._nextSelectedCookie = /** @type {?WebInspector.Cookie} */ (null);
80
81     this._dataGrid.show(this.element);
82     this._data = [];
83 }
84
85 WebInspector.CookiesTable.prototype = {
86     updateWidths: function()
87     {
88         if (this._dataGrid)
89             this._dataGrid.updateWidths();
90     },
91
92     /*
93      * @param {!Array.<!WebInspector.Cookie>} cookies
94      */
95     setCookies: function(cookies)
96     {
97         this.setCookieFolders([{cookies: cookies}]);
98     },
99
100     /**
101      * @param {!Array.<!{folderName: ?string, cookies: !Array.<!WebInspector.Cookie>}>} cookieFolders
102      */
103     setCookieFolders: function(cookieFolders)
104     {
105         this._data = cookieFolders;
106         this._rebuildTable();
107     },
108
109     /**
110      * @return {?WebInspector.Cookie}
111      */
112     selectedCookie: function()
113     {
114         var node = this._dataGrid.selectedNode;
115         return node ? node.cookie : null;
116     },
117
118     _rebuildTable: function()
119     {
120         var selectedCookie = this._nextSelectedCookie || this.selectedCookie();
121         this._nextSelectedCookie = null;
122         this._dataGrid.rootNode().removeChildren();
123         for (var i = 0; i < this._data.length; ++i) {
124             var item = this._data[i];
125             if (item.folderName) {
126                 var groupData = {name: item.folderName, value: "", domain: "", path: "", expires: "", size: this._totalSize(item.cookies), httpOnly: "", secure: ""};
127                 var groupNode = new WebInspector.DataGridNode(groupData);
128                 groupNode.selectable = true;
129                 this._dataGrid.rootNode().appendChild(groupNode);
130                 groupNode.element.addStyleClass("row-group");
131                 this._populateNode(groupNode, item.cookies, selectedCookie);
132                 groupNode.expand();
133             } else
134                 this._populateNode(this._dataGrid.rootNode(), item.cookies, selectedCookie);
135         }
136     },
137
138     /**
139      * @param {!WebInspector.DataGridNode} parentNode
140      * @param {?Array.<!WebInspector.Cookie>} cookies
141      * @param {?WebInspector.Cookie} selectedCookie
142      */
143     _populateNode: function(parentNode, cookies, selectedCookie)
144     {
145         parentNode.removeChildren();
146         if (!cookies)
147             return;
148
149         this._sortCookies(cookies);
150         for (var i = 0; i < cookies.length; ++i) {
151             var cookie = cookies[i];
152             var cookieNode = this._createGridNode(cookie);
153             parentNode.appendChild(cookieNode);
154             if (selectedCookie && selectedCookie.name() === cookie.name() && selectedCookie.domain() === cookie.domain() && selectedCookie.path() === cookie.path())
155                 cookieNode.select();
156         }
157     },
158
159     _totalSize: function(cookies)
160     {
161         var totalSize = 0;
162         for (var i = 0; cookies && i < cookies.length; ++i)
163             totalSize += cookies[i].size();
164         return totalSize;
165     },
166
167     /**
168      * @param {!Array.<!WebInspector.Cookie>} cookies
169      */
170     _sortCookies: function(cookies)
171     {
172         var sortDirection = this._dataGrid.sortOrder === "ascending" ? 1 : -1;
173
174         function compareTo(getter, cookie1, cookie2)
175         {
176             return sortDirection * (getter.apply(cookie1) + "").compareTo(getter.apply(cookie2) + "")
177         }
178
179         function numberCompare(getter, cookie1, cookie2)
180         {
181             return sortDirection * (getter.apply(cookie1) - getter.apply(cookie2));
182         }
183
184         function expiresCompare(cookie1, cookie2)
185         {
186             if (cookie1.session() !== cookie2.session())
187                 return sortDirection * (cookie1.session() ? 1 : -1);
188
189             if (cookie1.session())
190                 return 0;
191
192             if (cookie1.maxAge() && cookie2.maxAge())
193                 return sortDirection * (cookie1.maxAge() - cookie2.maxAge());
194             if (cookie1.expires() && cookie2.expires())
195                 return sortDirection * (cookie1.expires() - cookie2.expires());
196             return sortDirection * (cookie1.expires() ? 1 : -1);
197         }
198
199         var comparator;
200         switch (this._dataGrid.sortColumnIdentifier) {
201             case "name": comparator = compareTo.bind(null, WebInspector.Cookie.prototype.name); break;
202             case "value": comparator = compareTo.bind(null, WebInspector.Cookie.prototype.value); break;
203             case "domain": comparator = compareTo.bind(null, WebInspector.Cookie.prototype.domain); break;
204             case "path": comparator = compareTo.bind(null, WebInspector.Cookie.prototype.path); break;
205             case "expires": comparator = expiresCompare; break;
206             case "size": comparator = numberCompare.bind(null, WebInspector.Cookie.prototype.size); break;
207             case "httpOnly": comparator = compareTo.bind(null, WebInspector.Cookie.prototype.httpOnly); break;
208             case "secure": comparator = compareTo.bind(null, WebInspector.Cookie.prototype.secure); break;
209             default: compareTo.bind(null, WebInspector.Cookie.prototype.name);
210         }
211
212         cookies.sort(comparator);
213     },
214
215     /**
216      * @param {!WebInspector.Cookie} cookie
217      * @return {!WebInspector.DataGridNode}
218      */
219     _createGridNode: function(cookie)
220     {
221         var data = {};
222         data.name = cookie.name();
223         data.value = cookie.value();
224         data.domain = cookie.domain() || "";
225         data.path = cookie.path() || "";
226         if (cookie.type() === WebInspector.Cookie.Type.Request)
227             data.expires = "";
228         else if (cookie.maxAge())
229             data.expires = Number.secondsToString(parseInt(cookie.maxAge(), 10));
230         else if (cookie.expires())
231             data.expires = new Date(cookie.expires()).toGMTString();
232         else
233             data.expires = WebInspector.UIString("Session");
234         data.size = cookie.size();
235         const checkmark = "\u2713";
236         data.httpOnly = (cookie.httpOnly() ? checkmark : "");
237         data.secure = (cookie.secure() ? checkmark : "");
238
239         var node = new WebInspector.DataGridNode(data);
240         node.cookie = cookie;
241         node.selectable = true;
242         return node;
243     },
244
245     _onDeleteCookie: function(node)
246     {
247         var cookie = node.cookie;
248         var neighbour = node.traverseNextNode() || node.traversePreviousNode();
249         if (neighbour)
250             this._nextSelectedCookie = neighbour.cookie;
251         cookie.remove();
252         if (this._refreshCallback)
253             this._refreshCallback();
254     },
255
256     __proto__: WebInspector.View.prototype
257 }