2 * Copyright (C) 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Joseph Pecoraro
4 * Copyright (C) 2010 Google Inc. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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.
33 * @extends {WebInspector.View}
34 * @param {boolean} expandable
35 * @param {function(!WebInspector.Cookie)=} deleteCallback
36 * @param {function()=} refreshCallback
38 WebInspector.CookiesTable = function(expandable, deleteCallback, refreshCallback)
40 WebInspector.View.call(this);
41 this.element.className = "fill";
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%";
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;
78 this._nextSelectedCookie = /** @type {?WebInspector.Cookie} */ (null);
80 this._dataGrid.show(this.element);
84 WebInspector.CookiesTable.prototype = {
85 updateWidths: function()
88 this._dataGrid.updateWidths();
92 * @param {!Array.<!WebInspector.Cookie>} cookies
94 setCookies: function(cookies)
96 this.setCookieFolders([{cookies: cookies}]);
100 * @param {!Array.<!{folderName: ?string, cookies: !Array.<!WebInspector.Cookie>}>} cookieFolders
102 setCookieFolders: function(cookieFolders)
104 this._data = cookieFolders;
105 this._rebuildTable();
109 * @return {?WebInspector.Cookie}
111 selectedCookie: function()
113 var node = this._dataGrid.selectedNode;
114 return node ? node.cookie : null;
117 _rebuildTable: function()
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);
133 this._populateNode(this._dataGrid.rootNode(), item.cookies, selectedCookie);
138 * @param {!WebInspector.DataGridNode} parentNode
139 * @param {?Array.<!WebInspector.Cookie>} cookies
140 * @param {?WebInspector.Cookie} selectedCookie
142 _populateNode: function(parentNode, cookies, selectedCookie)
144 parentNode.removeChildren();
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())
158 _totalSize: function(cookies)
161 for (var i = 0; cookies && i < cookies.length; ++i)
162 totalSize += cookies[i].size();
167 * @param {!Array.<!WebInspector.Cookie>} cookies
169 _sortCookies: function(cookies)
171 var sortDirection = this._dataGrid.sortOrder === "ascending" ? 1 : -1;
173 function compareTo(getter, cookie1, cookie2)
175 return sortDirection * (getter.apply(cookie1) + "").compareTo(getter.apply(cookie2) + "")
178 function numberCompare(getter, cookie1, cookie2)
180 return sortDirection * (getter.apply(cookie1) - getter.apply(cookie2));
183 function expiresCompare(cookie1, cookie2)
185 if (cookie1.session() !== cookie2.session())
186 return sortDirection * (cookie1.session() ? 1 : -1);
188 if (cookie1.session())
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);
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);
211 cookies.sort(comparator);
215 * @param {!WebInspector.Cookie} cookie
216 * @return {!WebInspector.DataGridNode}
218 _createGridNode: function(cookie)
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)
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();
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 : "");
238 var node = new WebInspector.DataGridNode(data);
239 node.cookie = cookie;
240 node.selectable = true;
244 _onDeleteFromGrid: function(deleteCallback, node)
246 var cookie = node.cookie;
247 var neighbour = node.traverseNextNode() || node.traversePreviousNode();
249 this._nextSelectedCookie = neighbour.cookie;
250 deleteCallback(cookie);
253 __proto__: WebInspector.View.prototype