Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / resources / CookieItemsView.js
blob96df23c5b8d4a0791ceb714ab5b389b086e08277
1 /*
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
7 * are met:
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 /**
31 * @constructor
32 * @extends {WebInspector.VBox}
34 WebInspector.CookieItemsView = function(treeElement, cookieDomain)
36 WebInspector.VBox.call(this);
38 this.element.classList.add("storage-view");
40 this._deleteButton = new WebInspector.ToolbarButton(WebInspector.UIString("Delete"), "delete-toolbar-item");
41 this._deleteButton.setVisible(false);
42 this._deleteButton.addEventListener("click", this._deleteButtonClicked, this);
44 this._clearButton = new WebInspector.ToolbarButton(WebInspector.UIString("Clear"), "clear-toolbar-item");
45 this._clearButton.setVisible(false);
46 this._clearButton.addEventListener("click", this._clearButtonClicked, this);
48 this._refreshButton = new WebInspector.ToolbarButton(WebInspector.UIString("Refresh"), "refresh-toolbar-item");
49 this._refreshButton.addEventListener("click", this._refreshButtonClicked, this);
51 this._treeElement = treeElement;
52 this._cookieDomain = cookieDomain;
54 this._emptyWidget = new WebInspector.EmptyWidget(cookieDomain ? WebInspector.UIString("This site has no cookies.") : WebInspector.UIString("By default cookies are disabled for local files.\nYou could override this by starting the browser with --enable-file-cookies command line flag."));
55 this._emptyWidget.show(this.element);
57 this.element.addEventListener("contextmenu", this._contextMenu.bind(this), true);
60 WebInspector.CookieItemsView.prototype = {
61 /**
62 * @return {!Array.<!WebInspector.ToolbarItem>}
64 toolbarItems: function()
66 return [this._refreshButton, this._clearButton, this._deleteButton];
69 wasShown: function()
71 this._update();
74 willHide: function()
76 this._deleteButton.setVisible(false);
79 _update: function()
81 WebInspector.Cookies.getCookiesAsync(this._updateWithCookies.bind(this));
84 /**
85 * @param {!Array.<!WebInspector.Cookie>} allCookies
87 _updateWithCookies: function(allCookies)
89 this._cookies = this._filterCookiesForDomain(allCookies);
91 if (!this._cookies.length) {
92 // Nothing to show.
93 this._emptyWidget.show(this.element);
94 this._clearButton.setVisible(false);
95 this._deleteButton.setVisible(false);
96 if (this._cookiesTable)
97 this._cookiesTable.detach();
98 return;
101 if (!this._cookiesTable)
102 this._cookiesTable = new WebInspector.CookiesTable(false, this._update.bind(this), this._showDeleteButton.bind(this));
104 this._cookiesTable.setCookies(this._cookies);
105 this._emptyWidget.detach();
106 this._cookiesTable.show(this.element);
107 this._treeElement.subtitle = String.sprintf(WebInspector.UIString("%d cookies (%s)"), this._cookies.length,
108 Number.bytesToString(this._totalSize));
109 this._clearButton.setVisible(true);
110 this._deleteButton.setVisible(!!this._cookiesTable.selectedCookie());
114 * @param {!Array.<!WebInspector.Cookie>} allCookies
116 _filterCookiesForDomain: function(allCookies)
118 var cookies = [];
119 var resourceURLsForDocumentURL = [];
120 this._totalSize = 0;
123 * @this {WebInspector.CookieItemsView}
125 function populateResourcesForDocuments(resource)
127 var url = resource.documentURL.asParsedURL();
128 if (url && url.host == this._cookieDomain)
129 resourceURLsForDocumentURL.push(resource.url);
131 WebInspector.forAllResources(populateResourcesForDocuments.bind(this));
133 for (var i = 0; i < allCookies.length; ++i) {
134 var pushed = false;
135 var size = allCookies[i].size();
136 for (var j = 0; j < resourceURLsForDocumentURL.length; ++j) {
137 var resourceURL = resourceURLsForDocumentURL[j];
138 if (WebInspector.Cookies.cookieMatchesResourceURL(allCookies[i], resourceURL)) {
139 this._totalSize += size;
140 if (!pushed) {
141 pushed = true;
142 cookies.push(allCookies[i]);
147 return cookies;
150 clear: function()
152 this._cookiesTable.clear();
153 this._update();
156 _clearButtonClicked: function()
158 this.clear();
161 _showDeleteButton: function()
163 this._deleteButton.setVisible(true);
166 _deleteButtonClicked: function()
168 var selectedCookie = this._cookiesTable.selectedCookie();
169 if (selectedCookie) {
170 selectedCookie.remove();
171 this._update();
175 _refreshButtonClicked: function(event)
177 this._update();
180 _contextMenu: function(event)
182 if (!this._cookies.length) {
183 var contextMenu = new WebInspector.ContextMenu(event);
184 contextMenu.appendItem(WebInspector.UIString("Refresh"), this._update.bind(this));
185 contextMenu.show();
189 __proto__: WebInspector.VBox.prototype