00492d5fbf4fa15b587cc1a72a3975b194c404d5
[WebKit-https.git] / Source / WebCore / inspector / front-end / CookieItemsView.js
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.addStyleClass("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._refreshButton = new WebInspector.StatusBarButton(WebInspector.UIString("Refresh"), "refresh-storage-status-bar-item");
45     this._refreshButton.addEventListener("click", this._refreshButtonClicked, this);
46
47     this._treeElement = treeElement;
48     this._cookieDomain = cookieDomain;
49
50     this._emptyView = new WebInspector.EmptyView(WebInspector.UIString("This site has no cookies."));
51     this._emptyView.show(this.element);
52
53     this.element.addEventListener("contextmenu", this._contextMenu.bind(this), true);
54 }
55
56 WebInspector.CookieItemsView.prototype = {
57     get statusBarItems()
58     {
59         return [this._refreshButton.element, this._deleteButton.element];
60     },
61
62     wasShown: function()
63     {
64         this._update();
65     },
66
67     willHide: function()
68     {
69         this._deleteButton.visible = false;
70     },
71
72     _update: function()
73     {
74         WebInspector.Cookies.getCookiesAsync(this._updateWithCookies.bind(this));
75     },
76
77     /**
78      * @param {Array.<WebInspector.Cookie>} allCookies
79      * @param {boolean} isAdvanced
80      */
81     _updateWithCookies: function(allCookies, isAdvanced)
82     {
83         this._cookies = isAdvanced ? this._filterCookiesForDomain(allCookies) : allCookies;
84
85         if (!this._cookies.length) {
86             // Nothing to show.
87             this._emptyView.show(this.element);
88             this._deleteButton.visible = false;
89             if (this._cookiesTable)
90                 this._cookiesTable.detach();
91             return;
92         }
93
94         if (!this._cookiesTable)
95             this._cookiesTable = isAdvanced ? new WebInspector.CookiesTable(false, this._deleteCookie.bind(this), this._update.bind(this)) : new WebInspector.SimpleCookiesTable();
96
97         this._cookiesTable.setCookies(this._cookies);
98         this._emptyView.detach();
99         this._cookiesTable.show(this.element);
100         if (isAdvanced) {
101             this._treeElement.subtitle = String.sprintf(WebInspector.UIString("%d cookies (%s)"), this._cookies.length,
102                 Number.bytesToString(this._totalSize));
103             this._deleteButton.visible = true;
104         }
105     },
106
107     /**
108      * @param {Array.<WebInspector.Cookie>} allCookies
109      */
110     _filterCookiesForDomain: function(allCookies)
111     {
112         var cookies = [];
113         var resourceURLsForDocumentURL = [];
114         this._totalSize = 0;
115
116         function populateResourcesForDocuments(resource)
117         {
118             var url = resource.documentURL.asParsedURL();
119             if (url && url.host == this._cookieDomain)
120                 resourceURLsForDocumentURL.push(resource.url);
121         }
122         WebInspector.forAllResources(populateResourcesForDocuments.bind(this));
123
124         for (var i = 0; i < allCookies.length; ++i) {
125             var pushed = false;
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;
131                     if (!pushed) {
132                         pushed = true;
133                         cookies.push(allCookies[i]);
134                     }
135                 }
136             }
137         }
138         return cookies;
139     },
140
141     /**
142      * @param {!WebInspector.Cookie} cookie
143      */
144     _deleteCookie: function(cookie)
145     {
146         PageAgent.deleteCookie(cookie.name(), (cookie.secure() ? "https://" : "http://") + cookie.domain() + cookie.path());
147         this._update();
148     },
149
150     _deleteButtonClicked: function()
151     {
152         var selectedCookie = this._cookiesTable.selectedCookie();
153         if (selectedCookie)
154             this._deleteCookie(selectedCookie);
155     },
156
157     _refreshButtonClicked: function(event)
158     {
159         this._update();
160     },
161
162     _contextMenu: function(event)
163     {
164         if (!this._cookies.length) {
165             var contextMenu = new WebInspector.ContextMenu(event);
166             contextMenu.appendItem(WebInspector.UIString("Refresh"), this._update.bind(this));
167             contextMenu.show();
168         }
169     },
170
171     __proto__: WebInspector.View.prototype
172 }
173
174 /**
175  * @constructor
176  * @extends {WebInspector.View}
177  */
178 WebInspector.SimpleCookiesTable = function()
179 {
180     WebInspector.View.call(this);
181
182     var columns = {};
183     columns[0] = {};
184     columns[1] = {};
185     columns[0].title = WebInspector.UIString("Name");
186     columns[1].title = WebInspector.UIString("Value");
187
188     this._dataGrid = new WebInspector.DataGrid(columns);
189     this._dataGrid.autoSizeColumns(20, 80);
190     this._dataGrid.show(this.element);
191 }
192
193 WebInspector.SimpleCookiesTable.prototype = {
194     /**
195      * @param {Array.<WebInspector.Cookie>} cookies
196      */
197     setCookies: function(cookies)
198     {
199         this._dataGrid.rootNode().removeChildren();
200         var addedCookies = {};
201         for (var i = 0; i < cookies.length; ++i) {
202             if (addedCookies[cookies[i].name()])
203                 continue;
204             addedCookies[cookies[i].name()] = true;
205             var data = {};
206             data[0] = cookies[i].name();
207             data[1] = cookies[i].value();
208
209             var node = new WebInspector.DataGridNode(data, false);
210             node.selectable = true;
211             this._dataGrid.rootNode().appendChild(node);
212         }
213         this._dataGrid.rootNode().children[0].selected = true;
214     },
215
216     __proto__: WebInspector.View.prototype
217 }