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 {function(PageAgent.Cookie)=} deleteCallback
35 * @param {function()=} refreshCallback
37 WebInspector.CookiesTable = function(cookieDomain, expandable, deleteCallback, refreshCallback)
39 WebInspector.View.call(this);
40 this.element.className = "fill";
42 this._cookieDomain = cookieDomain;
44 var columns = { 0: {}, 1: {}, 2: {}, 3: {}, 4: {}, 5: {}, 6: {}, 7: {} };
45 columns[0].title = WebInspector.UIString("Name");
46 columns[0].sortable = true;
47 columns[0].disclosure = expandable;
48 columns[0].width = "24%";
49 columns[1].title = WebInspector.UIString("Value");
50 columns[1].sortable = true;
51 columns[1].width = "34%";
52 columns[2].title = WebInspector.UIString("Domain");
53 columns[2].sortable = true;
54 columns[2].width = "7%";
55 columns[3].title = WebInspector.UIString("Path");
56 columns[3].sortable = true;
57 columns[3].width = "7%";
58 columns[4].title = WebInspector.UIString("Expires");
59 columns[4].sortable = true;
60 columns[4].width = "7%";
61 columns[5].title = WebInspector.UIString("Size");
62 columns[5].aligned = "right";
63 columns[5].sortable = true;
64 columns[5].width = "7%";
65 columns[6].title = WebInspector.UIString("HTTP");
66 columns[6].aligned = "centered";
67 columns[6].sortable = true;
68 columns[6].width = "7%";
69 columns[7].title = WebInspector.UIString("Secure");
70 columns[7].aligned = "centered";
71 columns[7].sortable = true;
72 columns[7].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._dataGrid.show(this.element);
82 WebInspector.CookiesTable.prototype = {
83 updateWidths: function()
86 this._dataGrid.updateWidths();
89 setCookies: function(cookies)
91 this._data = [{cookies: cookies}];
95 addCookiesFolder: function(folderName, cookies)
97 this._data.push({cookies: cookies, folderName: folderName});
103 var node = this._dataGrid.selectedNode;
104 return node ? node.cookie : null;
107 _rebuildTable: function()
109 this._dataGrid.removeChildren();
110 for (var i = 0; i < this._data.length; ++i) {
111 var item = this._data[i];
112 if (item.folderName) {
113 var groupData = [ item.folderName, "", "", "", "", this._totalSize(item.cookies), "", "" ];
114 var groupNode = new WebInspector.DataGridNode(groupData);
115 groupNode.selectable = true;
116 this._dataGrid.appendChild(groupNode);
117 groupNode.element.addStyleClass("row-group");
118 this._populateNode(groupNode, item.cookies);
121 this._populateNode(this._dataGrid, item.cookies);
125 _populateNode: function(parentNode, cookies)
127 var selectedCookie = this.selectedCookie;
128 parentNode.removeChildren();
132 this._sortCookies(cookies);
133 for (var i = 0; i < cookies.length; ++i) {
134 var cookieNode = this._createGridNode(cookies[i]);
135 parentNode.appendChild(cookieNode);
136 if (selectedCookie === cookies[i])
137 cookieNode.selected = true;
141 _totalSize: function(cookies)
144 for (var i = 0; cookies && i < cookies.length; ++i)
145 totalSize += cookies[i].size;
149 _sortCookies: function(cookies)
151 var sortDirection = this._dataGrid.sortOrder === "ascending" ? 1 : -1;
153 function localeCompare(field, cookie1, cookie2)
155 return sortDirection * (cookie1[field] + "").localeCompare(cookie2[field] + "")
158 function numberCompare(field, cookie1, cookie2)
160 return sortDirection * (cookie1[field] - cookie2[field]);
163 function expiresCompare(cookie1, cookie2)
165 if (cookie1.session !== cookie2.session)
166 return sortDirection * (cookie1.session ? 1 : -1);
171 return sortDirection * (cookie1.expires - cookie2.expires);
175 switch (parseInt(this._dataGrid.sortColumnIdentifier, 10)) {
176 case 0: comparator = localeCompare.bind(this, "name"); break;
177 case 1: comparator = localeCompare.bind(this, "value"); break;
178 case 2: comparator = localeCompare.bind(this, "domain"); break;
179 case 3: comparator = localeCompare.bind(this, "path"); break;
180 case 4: comparator = expiresCompare; break;
181 case 5: comparator = numberCompare.bind(this, "size"); break;
182 case 6: comparator = localeCompare.bind(this, "httpOnly"); break;
183 case 7: comparator = localeCompare.bind(this, "secure"); break;
184 default: localeCompare.bind(this, "name");
187 cookies.sort(comparator);
191 * @param {PageAgent.Cookie} cookie
193 _createGridNode: function(cookie)
196 data[0] = cookie.name;
197 data[1] = cookie.value;
198 data[2] = cookie.domain || "";
199 data[3] = cookie.path || "";
200 data[4] = cookie.type === WebInspector.Cookie.Type.Request ? "" :
201 (cookie.session ? WebInspector.UIString("Session") : new Date(cookie.expires).toGMTString());
202 data[5] = cookie.size;
203 const checkmark = "\u2713";
204 data[6] = (cookie.httpOnly ? checkmark : "");
205 data[7] = (cookie.secure ? checkmark : "");
207 var node = new WebInspector.DataGridNode(data);
208 node.cookie = cookie;
209 node.selectable = true;
213 _onDeleteFromGrid: function(deleteCallback, node)
215 deleteCallback(node.cookie);
219 WebInspector.CookiesTable.prototype.__proto__ = WebInspector.View.prototype;