2 * Copyright (C) 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Joseph Pecoraro
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
30 WebInspector.CookieItemsView = function(treeElement, cookieDomain)
32 WebInspector.View.call(this);
34 this.element.addStyleClass("storage-view");
36 this._deleteButton = new WebInspector.StatusBarButton(WebInspector.UIString("Delete"), "delete-storage-status-bar-item");
37 this._deleteButton.visible = false;
38 this._deleteButton.addEventListener("click", this._deleteButtonClicked.bind(this), false);
40 this._refreshButton = new WebInspector.StatusBarButton(WebInspector.UIString("Refresh"), "refresh-storage-status-bar-item");
41 this._refreshButton.addEventListener("click", this._refreshButtonClicked.bind(this), false);
43 this._treeElement = treeElement;
44 this._cookieDomain = cookieDomain;
46 this._emptyMsgElement = document.createElement("div");
47 this._emptyMsgElement.className = "storage-empty-view";
48 this._emptyMsgElement.textContent = WebInspector.UIString("This site has no cookies.");
49 this.element.appendChild(this._emptyMsgElement);
52 WebInspector.CookieItemsView.prototype = {
55 return [this._refreshButton.element, this._deleteButton.element];
58 show: function(parentElement)
60 WebInspector.View.prototype.show.call(this, parentElement);
66 WebInspector.View.prototype.hide.call(this);
67 this._deleteButton.visible = false;
72 if (this._cookiesTable)
73 this._cookiesTable.updateWidths();
78 WebInspector.Cookies.getCookiesAsync(this._updateWithCookies.bind(this));
81 _updateWithCookies: function(allCookies, isAdvanced)
83 this._cookies = isAdvanced ? this._filterCookiesForDomain(allCookies) : allCookies;
85 if (!this._cookies.length) {
87 this._emptyMsgElement.removeStyleClass("hidden");
88 this._deleteButton.visible = false;
89 if (this._cookiesTable)
90 this._cookiesTable.element.addStyleClass("hidden");
94 if (!this._cookiesTable) {
95 this._cookiesTable = isAdvanced ? new WebInspector.CookiesTable(this._cookieDomain, false, this._deleteCookie.bind(this)) : new WebInspector.SimpleCookiesTable();
96 this.element.appendChild(this._cookiesTable.element);
99 this._cookiesTable.setCookies(this._cookies);
100 this._cookiesTable.element.removeStyleClass("hidden");
101 this._emptyMsgElement.addStyleClass("hidden");
103 this._treeElement.subtitle = String.sprintf(WebInspector.UIString("%d cookies (%s)"), this._cookies.length,
104 Number.bytesToString(this._totalSize));
105 this._deleteButton.visible = true;
107 this._cookiesTable.updateWidths();
110 _filterCookiesForDomain: function(allCookies)
113 var resourceURLsForDocumentURL = [];
116 function populateResourcesForDocuments(resource)
118 var url = resource.documentURL.asParsedURL();
119 if (url && url.host == this._cookieDomain)
120 resourceURLsForDocumentURL.push(resource.url);
122 WebInspector.forAllResources(populateResourcesForDocuments.bind(this));
124 for (var i = 0; i < allCookies.length; ++i) {
126 var size = allCookies[i].size;
127 for (var j = 0; j < resourceURLsForDocumentURL.length; ++j) {
128 var resourceURL = resourceURLsForDocumentURL[j];
129 if (WebInspector.Cookies.cookieMatchesResourceURL(allCookies[i], resourceURL)) {
130 this._totalSize += size;
133 cookies.push(allCookies[i]);
141 _deleteCookie: function(cookie)
143 InspectorBackend.deleteCookie(cookie.name, this._cookieDomain);
147 _deleteButtonClicked: function()
149 if (this._cookiesTable.selectedCookie)
150 this._deleteCookie(this._cookiesTable.selectedCookie);
153 _refreshButtonClicked: function(event)
159 WebInspector.CookieItemsView.prototype.__proto__ = WebInspector.View.prototype;
161 WebInspector.SimpleCookiesTable = function()
163 this.element = document.createElement("div");
167 columns[0].title = WebInspector.UIString("Name");
168 columns[1].title = WebInspector.UIString("Value");
170 this._dataGrid = new WebInspector.DataGrid(columns);
171 this._dataGrid.autoSizeColumns(20, 80);
172 this.element.appendChild(this._dataGrid.element);
173 this._dataGrid.updateWidths();
176 WebInspector.SimpleCookiesTable.prototype = {
177 setCookies: function(cookies)
179 this._dataGrid.removeChildren();
180 var addedCookies = {};
181 for (var i = 0; i < cookies.length; ++i) {
182 if (addedCookies[cookies[i].name])
184 addedCookies[cookies[i].name] = true;
186 data[0] = cookies[i].name;
187 data[1] = cookies[i].value;
189 var node = new WebInspector.DataGridNode(data, false);
190 node.selectable = true;
191 this._dataGrid.appendChild(node);
193 this._dataGrid.children[0].selected = true;
199 this._dataGrid.updateWidths();