initial import
[vuplus_webkit] / 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 WebInspector.CookiesTable = function(cookieDomain, expandable, deleteCallback, refreshCallback)
32 {
33     this._cookieDomain = cookieDomain;
34
35     var columns = { 0: {}, 1: {}, 2: {}, 3: {}, 4: {}, 5: {}, 6: {}, 7: {} };
36     columns[0].title = WebInspector.UIString("Name");
37     columns[0].sortable = true;
38     columns[0].disclosure = expandable;
39     columns[0].width = "24%";
40     columns[1].title = WebInspector.UIString("Value");
41     columns[1].sortable = true;
42     columns[1].width = "34%";
43     columns[2].title = WebInspector.UIString("Domain");
44     columns[2].sortable = true;
45     columns[2].width = "7%";
46     columns[3].title = WebInspector.UIString("Path");
47     columns[3].sortable = true;
48     columns[3].width = "7%";
49     columns[4].title = WebInspector.UIString("Expires");
50     columns[4].sortable = true;
51     columns[4].width = "7%";
52     columns[5].title = WebInspector.UIString("Size");
53     columns[5].aligned = "right";
54     columns[5].sortable = true;
55     columns[5].width = "7%";
56     columns[6].title = WebInspector.UIString("HTTP");
57     columns[6].aligned = "centered";
58     columns[6].sortable = true;
59     columns[6].width = "7%";
60     columns[7].title = WebInspector.UIString("Secure");
61     columns[7].aligned = "centered";
62     columns[7].sortable = true;
63     columns[7].width = "7%";
64
65     this._dataGrid = new WebInspector.DataGrid(columns, null, deleteCallback ? this._onDeleteFromGrid.bind(this) : null);
66     this._dataGrid.addEventListener("sorting changed", this._rebuildTable, this);
67     this._dataGrid.refreshCallback = refreshCallback;
68
69     this.element = this._dataGrid.element;
70     this._data = [];
71     this._deleteCallback = deleteCallback;
72 }
73
74 WebInspector.CookiesTable.prototype = {
75     updateWidths: function()
76     {
77         if (this._dataGrid)
78             this._dataGrid.updateWidths();
79     },
80
81     setCookies: function(cookies)
82     {
83         this._data = [{cookies: cookies}];
84         this._rebuildTable();
85     },
86
87     addCookiesFolder: function(folderName, cookies)
88     {
89         this._data.push({cookies: cookies, folderName: folderName});
90         this._rebuildTable();
91     },
92
93     get selectedCookie()
94     {
95         var node = this._dataGrid.selectedNode;
96         return node ? node.cookie : null;
97     },
98
99     _rebuildTable: function()
100     {
101         this._dataGrid.removeChildren();
102         for (var i = 0; i < this._data.length; ++i) {
103             var item = this._data[i];
104             if (item.folderName) {
105                 var groupData = [ item.folderName, "", "", "", "", this._totalSize(item.cookies), "", "" ];
106                 var groupNode = new WebInspector.DataGridNode(groupData);
107                 groupNode.selectable = true;
108                 this._dataGrid.appendChild(groupNode);
109                 groupNode.element.addStyleClass("row-group");
110                 this._populateNode(groupNode, item.cookies);
111                 groupNode.expand();
112             } else
113                 this._populateNode(this._dataGrid, item.cookies);
114         }
115     },
116
117     _populateNode: function(parentNode, cookies)
118     {
119         var selectedCookie = this.selectedCookie;
120         parentNode.removeChildren();
121         if (!cookies)
122             return;
123
124         this._sortCookies(cookies);
125         for (var i = 0; i < cookies.length; ++i) {
126             var cookieNode = this._createGridNode(cookies[i]);
127             parentNode.appendChild(cookieNode);
128             if (selectedCookie === cookies[i])
129                 cookieNode.selected = true;
130         }
131     },
132
133     _totalSize: function(cookies)
134     {
135         var totalSize = 0;
136         for (var i = 0; cookies && i < cookies.length; ++i)
137             totalSize += cookies[i].size;
138         return totalSize;
139     },
140
141     _sortCookies: function(cookies)
142     {
143         var sortDirection = this._dataGrid.sortOrder === "ascending" ? 1 : -1;
144
145         function localeCompare(field, cookie1, cookie2)
146         {
147             return sortDirection * (cookie1[field] + "").localeCompare(cookie2[field] + "")
148         }
149
150         function numberCompare(field, cookie1, cookie2)
151         {
152             return sortDirection * (cookie1[field] - cookie2[field]);
153         }
154
155         function expiresCompare(cookie1, cookie2)
156         {
157             if (cookie1.session !== cookie2.session)
158                 return sortDirection * (cookie1.session ? 1 : -1);
159
160             if (cookie1.session)
161                 return 0;
162
163             return sortDirection * (cookie1.expires - cookie2.expires);
164         }
165
166         var comparator;
167         switch (parseInt(this._dataGrid.sortColumnIdentifier)) {
168             case 0: comparator = localeCompare.bind(this, "name"); break;
169             case 1: comparator = localeCompare.bind(this, "value"); break;
170             case 2: comparator = localeCompare.bind(this, "domain"); break;
171             case 3: comparator = localeCompare.bind(this, "path"); break;
172             case 4: comparator = expiresCompare; break;
173             case 5: comparator = numberCompare.bind(this, "size"); break;
174             case 6: comparator = localeCompare.bind(this, "httpOnly"); break;
175             case 7: comparator = localeCompare.bind(this, "secure"); break;
176             default: localeCompare.bind(this, "name");
177         }
178
179         cookies.sort(comparator);
180     },
181
182     _createGridNode: function(cookie)
183     {
184         var data = {};
185         data[0] = cookie.name;
186         data[1] = cookie.value;
187         data[2] = cookie.domain || "";
188         data[3] = cookie.path || "";
189         data[4] = cookie.type === WebInspector.Cookie.Type.Request ? "" :
190             (cookie.session ? WebInspector.UIString("Session") : new Date(cookie.expires).toGMTString());
191         data[5] = cookie.size;
192         const checkmark = "\u2713";
193         data[6] = (cookie.httpOnly ? checkmark : "");
194         data[7] = (cookie.secure ? checkmark : "");
195
196         var node = new WebInspector.DataGridNode(data);
197         node.cookie = cookie;
198         node.selectable = true;
199         return node;
200     },
201
202     _onDeleteFromGrid: function(node)
203     {
204         this._deleteCallback(node.cookie);
205     }
206 }