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