8798eb14e50403f0852eade8d4110663f23afc39
[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(!WebInspector.Cookie)=} deleteCallback
36  * @param {function()=} refreshCallback
37  */
38 WebInspector.CookiesTable = function(expandable, deleteCallback, refreshCallback)
39 {
40     WebInspector.View.call(this);
41     this.element.className = "fill";
42
43     var columns = {name: {}, value: {}, domain: {}, path: {}, expires: {}, size: {}, httpOnly: {}, secure: {}};
44     columns.name.title = WebInspector.UIString("Name");
45     columns.name.sortable = true;
46     columns.name.disclosure = expandable;
47     columns.name.width = "24%";
48     columns.name.sort = "ascending";
49     columns.value.title = WebInspector.UIString("Value");
50     columns.value.sortable = true;
51     columns.value.width = "34%";
52     columns.domain.title = WebInspector.UIString("Domain");
53     columns.domain.sortable = true;
54     columns.domain.width = "7%";
55     columns.path.title = WebInspector.UIString("Path");
56     columns.path.sortable = true;
57     columns.path.width = "7%";
58     columns.expires.title = WebInspector.UIString("Expires / Max-Age");
59     columns.expires.sortable = true;
60     columns.expires.width = "7%";
61     columns.size.title = WebInspector.UIString("Size");
62     columns.size.aligned = "right";
63     columns.size.sortable = true;
64     columns.size.width = "7%";
65     columns.httpOnly.title = WebInspector.UIString("HTTP");
66     columns.httpOnly.aligned = "centered";
67     columns.httpOnly.sortable = true;
68     columns.httpOnly.width = "7%";
69     columns.secure.title = WebInspector.UIString("Secure");
70     columns.secure.aligned = "centered";
71     columns.secure.sortable = true;
72     columns.secure.width = "7%";
73
74     this._dataGrid = new WebInspector.DataGrid(columns, undefined, deleteCallback ? this._onDeleteFromGrid.bind(this, deleteCallback) : undefined);
75     this._dataGrid.addEventListener("sorting changed", this._rebuildTable, this);
76     this._dataGrid.refreshCallback = refreshCallback;
77
78     this._nextSelectedCookie = /** @type {?WebInspector.Cookie} */ (null);
79
80     this._dataGrid.show(this.element);
81     this._data = [];
82 }
83
84 WebInspector.CookiesTable.prototype = {
85     updateWidths: function()
86     {
87         if (this._dataGrid)
88             this._dataGrid.updateWidths();
89     },
90
91     /*
92      * @param {!Array.<!WebInspector.Cookie>} cookies
93      */
94     setCookies: function(cookies)
95     {
96         this.setCookieFolders([{cookies: cookies}]);
97     },
98
99     /**
100      * @param {!Array.<!{folderName: ?string, cookies: !Array.<!WebInspector.Cookie>}>} cookieFolders
101      */
102     setCookieFolders: function(cookieFolders)
103     {
104         this._data = cookieFolders;
105         this._rebuildTable();
106     },
107
108     /**
109      * @return {?WebInspector.Cookie}
110      */
111     selectedCookie: function()
112     {
113         var node = this._dataGrid.selectedNode;
114         return node ? node.cookie : null;
115     },
116
117     _rebuildTable: function()
118     {
119         var selectedCookie = this._nextSelectedCookie || this.selectedCookie();
120         this._nextSelectedCookie = null;
121         this._dataGrid.rootNode().removeChildren();
122         for (var i = 0; i < this._data.length; ++i) {
123             var item = this._data[i];
124             if (item.folderName) {
125                 var groupData = {name: item.folderName, value: "", domain: "", path: "", expires: "", size: this._totalSize(item.cookies), httpOnly: "", secure: ""};
126                 var groupNode = new WebInspector.DataGridNode(groupData);
127                 groupNode.selectable = true;
128                 this._dataGrid.rootNode().appendChild(groupNode);
129                 groupNode.element.addStyleClass("row-group");
130                 this._populateNode(groupNode, item.cookies, selectedCookie);
131                 groupNode.expand();
132             } else
133                 this._populateNode(this._dataGrid.rootNode(), item.cookies, selectedCookie);
134         }
135     },
136
137     /**
138      * @param {!WebInspector.DataGridNode} parentNode
139      * @param {?Array.<!WebInspector.Cookie>} cookies
140      * @param {?WebInspector.Cookie} selectedCookie
141      */
142     _populateNode: function(parentNode, cookies, selectedCookie)
143     {
144         parentNode.removeChildren();
145         if (!cookies)
146             return;
147
148         this._sortCookies(cookies);
149         for (var i = 0; i < cookies.length; ++i) {
150             var cookie = cookies[i];
151             var cookieNode = this._createGridNode(cookie);
152             parentNode.appendChild(cookieNode);
153             if (selectedCookie && selectedCookie.name() === cookie.name() && selectedCookie.domain() === cookie.domain() && selectedCookie.path() === cookie.path())
154                 cookieNode.select();
155         }
156     },
157
158     _totalSize: function(cookies)
159     {
160         var totalSize = 0;
161         for (var i = 0; cookies && i < cookies.length; ++i)
162             totalSize += cookies[i].size();
163         return totalSize;
164     },
165
166     /**
167      * @param {!Array.<!WebInspector.Cookie>} cookies
168      */
169     _sortCookies: function(cookies)
170     {
171         var sortDirection = this._dataGrid.sortOrder === "ascending" ? 1 : -1;
172
173         function compareTo(getter, cookie1, cookie2)
174         {
175             return sortDirection * (getter.apply(cookie1) + "").compareTo(getter.apply(cookie2) + "")
176         }
177
178         function numberCompare(getter, cookie1, cookie2)
179         {
180             return sortDirection * (getter.apply(cookie1) - getter.apply(cookie2));
181         }
182
183         function expiresCompare(cookie1, cookie2)
184         {
185             if (cookie1.session() !== cookie2.session())
186                 return sortDirection * (cookie1.session() ? 1 : -1);
187
188             if (cookie1.session())
189                 return 0;
190
191             if (cookie1.maxAge() && cookie2.maxAge())
192                 return sortDirection * (cookie1.maxAge() - cookie2.maxAge());
193             if (cookie1.expires() && cookie2.expires())
194                 return sortDirection * (cookie1.expires() - cookie2.expires());
195             return sortDirection * (cookie1.expires() ? 1 : -1);
196         }
197
198         var comparator;
199         switch (this._dataGrid.sortColumnIdentifier) {
200             case "name": comparator = compareTo.bind(null, WebInspector.Cookie.prototype.name); break;
201             case "value": comparator = compareTo.bind(null, WebInspector.Cookie.prototype.value); break;
202             case "domain": comparator = compareTo.bind(null, WebInspector.Cookie.prototype.domain); break;
203             case "path": comparator = compareTo.bind(null, WebInspector.Cookie.prototype.path); break;
204             case "expires": comparator = expiresCompare; break;
205             case "size": comparator = numberCompare.bind(null, WebInspector.Cookie.prototype.size); break;
206             case "httpOnly": comparator = compareTo.bind(null, WebInspector.Cookie.prototype.httpOnly); break;
207             case "secure": comparator = compareTo.bind(null, WebInspector.Cookie.prototype.secure); break;
208             default: compareTo.bind(null, WebInspector.Cookie.prototype.name);
209         }
210
211         cookies.sort(comparator);
212     },
213
214     /**
215      * @param {!WebInspector.Cookie} cookie
216      * @return {!WebInspector.DataGridNode}
217      */
218     _createGridNode: function(cookie)
219     {
220         var data = {};
221         data.name = cookie.name();
222         data.value = cookie.value();
223         data.domain = cookie.domain() || "";
224         data.path = cookie.path() || "";
225         if (cookie.type() === WebInspector.Cookie.Type.Request)
226             data.expires = "";
227         else if (cookie.maxAge())
228             data.expires = Number.secondsToString(parseInt(cookie.maxAge(), 10));
229         else if (cookie.expires())
230             data.expires = new Date(cookie.expires()).toGMTString();
231         else
232             data.expires = WebInspector.UIString("Session");
233         data.size = cookie.size();
234         const checkmark = "\u2713";
235         data.httpOnly = (cookie.httpOnly() ? checkmark : "");
236         data.secure = (cookie.secure() ? checkmark : "");
237
238         var node = new WebInspector.DataGridNode(data);
239         node.cookie = cookie;
240         node.selectable = true;
241         return node;
242     },
243
244     _onDeleteFromGrid: function(deleteCallback, node)
245     {
246         var cookie = node.cookie;
247         var neighbour = node.traverseNextNode() || node.traversePreviousNode();
248         if (neighbour)
249             this._nextSelectedCookie = neighbour.cookie;
250         deleteCallback(cookie);
251     },
252
253     __proto__: WebInspector.View.prototype
254 }