1 /* 2 * Copyright (C) 2009 Apple Inc. All rights reserved. 3 * Copyright (C) 2009 Joseph Pecoraro 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of 15 * its contributors may be used to endorse or promote products derived 16 * from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 /** 31 * @constructor 32 * @extends {WebInspector.View} 33 */ 34 WebInspector.CookieItemsView = function(treeElement, cookieDomain) 35 { 36 WebInspector.View.call(this); 37 38 this.element.classList.add("storage-view"); 39 40 this._deleteButton = new WebInspector.StatusBarButton(WebInspector.UIString("Delete"), "delete-storage-status-bar-item"); 41 this._deleteButton.visible = false; 42 this._deleteButton.addEventListener("click", this._deleteButtonClicked, this); 43 44 this._clearButton = new WebInspector.StatusBarButton(WebInspector.UIString("Clear"), "clear-storage-status-bar-item"); 45 this._clearButton.visible = false; 46 this._clearButton.addEventListener("click", this._clearButtonClicked, this); 47 48 this._refreshButton = new WebInspector.StatusBarButton(WebInspector.UIString("Refresh"), "refresh-storage-status-bar-item"); 49 this._refreshButton.addEventListener("click", this._refreshButtonClicked, this); 50 51 this._treeElement = treeElement; 52 this._cookieDomain = cookieDomain; 53 54 this._emptyView = new WebInspector.EmptyView(WebInspector.UIString("This site has no cookies.")); 55 this._emptyView.show(this.element); 56 57 this.element.addEventListener("contextmenu", this._contextMenu.bind(this), true); 58 } 59 60 WebInspector.CookieItemsView.prototype = { 61 get statusBarItems() 62 { 63 return [this._refreshButton.element, this._clearButton.element, this._deleteButton.element]; 64 }, 65 66 wasShown: function() 67 { 68 this._update(); 69 }, 70 71 willHide: function() 72 { 73 this._deleteButton.visible = false; 74 }, 75 76 _update: function() 77 { 78 WebInspector.Cookies.getCookiesAsync(this._updateWithCookies.bind(this)); 79 }, 80 81 /** 82 * @param {!Array.<!WebInspector.Cookie>} allCookies 83 */ 84 _updateWithCookies: function(allCookies) 85 { 86 this._cookies = this._filterCookiesForDomain(allCookies); 87 88 if (!this._cookies.length) { 89 // Nothing to show. 90 this._emptyView.show(this.element); 91 this._clearButton.visible = false; 92 this._deleteButton.visible = false; 93 if (this._cookiesTable) 94 this._cookiesTable.detach(); 95 return; 96 } 97 98 if (!this._cookiesTable) 99 this._cookiesTable = new WebInspector.CookiesTable(false, this._update.bind(this), this._showDeleteButton.bind(this)); 100 101 this._cookiesTable.setCookies(this._cookies); 102 this._emptyView.detach(); 103 this._cookiesTable.show(this.element); 104 this._treeElement.subtitle = String.sprintf(WebInspector.UIString("%d cookies (%s)"), this._cookies.length, 105 Number.bytesToString(this._totalSize)); 106 this._clearButton.visible = true; 107 this._deleteButton.visible = !!this._cookiesTable.selectedCookie(); 108 }, 109 110 /** 111 * @param {!Array.<!WebInspector.Cookie>} allCookies 112 */ 113 _filterCookiesForDomain: function(allCookies) 114 { 115 var cookies = []; 116 var resourceURLsForDocumentURL = []; 117 this._totalSize = 0; 118 119 /** 120 * @this {WebInspector.CookieItemsView} 121 */ 122 function populateResourcesForDocuments(resource) 123 { 124 var url = resource.documentURL.asParsedURL(); 125 if (url && url.host == this._cookieDomain) 126 resourceURLsForDocumentURL.push(resource.url); 127 } 128 WebInspector.forAllResources(populateResourcesForDocuments.bind(this)); 129 130 for (var i = 0; i < allCookies.length; ++i) { 131 var pushed = false; 132 var size = allCookies[i].size(); 133 for (var j = 0; j < resourceURLsForDocumentURL.length; ++j) { 134 var resourceURL = resourceURLsForDocumentURL[j]; 135 if (WebInspector.Cookies.cookieMatchesResourceURL(allCookies[i], resourceURL)) { 136 this._totalSize += size; 137 if (!pushed) { 138 pushed = true; 139 cookies.push(allCookies[i]); 140 } 141 } 142 } 143 } 144 return cookies; 145 }, 146 147 clear: function() 148 { 149 this._cookiesTable.clear(); 150 this._update(); 151 }, 152 153 _clearButtonClicked: function() 154 { 155 this.clear(); 156 }, 157 158 _showDeleteButton: function() 159 { 160 this._deleteButton.visible = true; 161 }, 162 163 _deleteButtonClicked: function() 164 { 165 var selectedCookie = this._cookiesTable.selectedCookie(); 166 if (selectedCookie) { 167 selectedCookie.remove(); 168 this._update(); 169 } 170 }, 171 172 _refreshButtonClicked: function(event) 173 { 174 this._update(); 175 }, 176 177 _contextMenu: function(event) 178 { 179 if (!this._cookies.length) { 180 var contextMenu = new WebInspector.ContextMenu(event); 181 contextMenu.appendItem(WebInspector.UIString("Refresh"), this._update.bind(this)); 182 contextMenu.show(); 183 } 184 }, 185 186 __proto__: WebInspector.View.prototype 187 } 188