123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- /*
- * Copyright (C) 2013 Apple Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
- * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
- * THE POSSIBILITY OF SUCH DAMAGE.
- */
- WebInspector.CookieStorageContentView = function(representedObject)
- {
- WebInspector.ContentView.call(this, representedObject);
- this.element.classList.add(WebInspector.CookieStorageContentView.StyleClassName);
- this.update();
- };
- WebInspector.CookieStorageContentView.StyleClassName = "cookie-storage";
- WebInspector.CookieStorageContentView.prototype = {
- constructor: WebInspector.CookieStorageContentView,
- // Public
- update: function()
- {
- function callback(error, cookies, cookiesString)
- {
- if (error)
- return;
- this._cookies = this._filterCookies(cookies);
- this._rebuildTable();
- }
-
- PageAgent.getCookies(callback.bind(this));
- },
- updateLayout: function()
- {
- if (this._dataGrid)
- this._dataGrid.updateLayout();
- },
- get scrollableElements()
- {
- if (!this._dataGrid)
- return [];
- return [this._dataGrid.scrollContainer];
- },
- // Private
- _rebuildTable: function()
- {
- // FIXME: If there are no cookies, do we want to show an empty datagrid, or do something like the old
- // inspector and show some text saying there are no cookies?
- if (!this._dataGrid) {
- var columns = { 0: {}, 1: {}, 2: {}, 3: {}, 4: {}, 5: {}, 6: {}, 7: {} };
- columns[0].title = WebInspector.UIString("Name");
- columns[0].sortable = true;
- columns[0].width = "24%";
- columns[1].title = WebInspector.UIString("Value");
- columns[1].sortable = true;
- columns[1].width = "34%";
- columns[2].title = WebInspector.UIString("Domain");
- columns[2].sortable = true;
- columns[2].width = "7%";
- columns[3].title = WebInspector.UIString("Path");
- columns[3].sortable = true;
- columns[3].width = "7%";
- columns[4].title = WebInspector.UIString("Expires");
- columns[4].sortable = true;
- columns[4].width = "7%";
- columns[5].title = WebInspector.UIString("Size");
- columns[5].aligned = "right";
- columns[5].sortable = true;
- columns[5].width = "7%";
- columns[6].title = WebInspector.UIString("HTTP");
- columns[6].aligned = "centered";
- columns[6].sortable = true;
- columns[6].width = "7%";
- columns[7].title = WebInspector.UIString("Secure");
- columns[7].aligned = "centered";
- columns[7].sortable = true;
- columns[7].width = "7%";
- this._dataGrid = new WebInspector.DataGrid(columns, null, this._deleteCallback.bind(this));
- this._dataGrid.addEventListener(WebInspector.DataGrid.Event.SortChanged, this._rebuildTable, this);
- this.element.appendChild(this._dataGrid.element);
- this._dataGrid.updateLayout();
- }
-
- console.assert(this._dataGrid);
- this._dataGrid.removeChildren();
- this._sortCookies(this._cookies);
- for (var i = 0; i < this._cookies.length; ++i) {
- const cookie = this._cookies[i];
- var data = {};
- data[0] = cookie.name;
- data[1] = cookie.value;
- data[2] = cookie.domain || "";
- data[3] = cookie.path || "";
- if (cookie.type === WebInspector.CookieType.Request)
- data[4] = "";
- else
- data[4] = cookie.session ? WebInspector.UIString("Session") : new Date(cookie.expires).toLocaleString();
- data[5] = Number.bytesToString(cookie.size);
- const checkmark = "\u2713";
- data[6] = cookie.httpOnly ? checkmark : "";
- data[7] = cookie.secure ? checkmark : "";
- var node = new WebInspector.DataGridNode(data);
- node.cookie = cookie;
- node.selectable = true;
-
- this._dataGrid.appendChild(node);
- }
- },
- _filterCookies: function(cookies)
- {
- var filteredCookies = [];
- var resourcesForDomain = [];
- var frames = WebInspector.frameResourceManager.frames;
- for (var i = 0; i < frames.length; ++i) {
- var resources = frames[i].resources;
- for (var j = 0; j < resources.length; ++j) {
- var urlComponents = resources[j].urlComponents;
- if (urlComponents && urlComponents.host && urlComponents.host === this.representedObject.host)
- resourcesForDomain.push(resources[j].url);
- }
- // The main resource isn't always in the list of resources, make sure to add it to the list of resources
- // we get the URLs from.
- var mainResourceURLComponents = frames[i].mainResource.urlComponents;
- if (mainResourceURLComponents && mainResourceURLComponents.host && mainResourceURLComponents.host == this.representedObject.host)
- resourcesForDomain.push(frames[i].mainResource.url);
- }
- for (var i = 0; i < cookies.length; ++i) {
- for (var j = 0; j < resourcesForDomain.length; ++j) {
- if (WebInspector.cookieMatchesResourceURL(cookies[i], resourcesForDomain[j])) {
- filteredCookies.push(cookies[i]);
- break;
- }
- }
- }
-
- return filteredCookies;
- },
-
- _sortCookies: function(cookies)
- {
- var sortDirection = this._dataGrid.sortOrder === "ascending" ? 1 : -1;
- function localeCompare(field, cookie1, cookie2)
- {
- return sortDirection * (cookie1[field] + "").localeCompare(cookie2[field] + "")
- }
- function numberCompare(field, cookie1, cookie2)
- {
- return sortDirection * (cookie1[field] - cookie2[field]);
- }
- function expiresCompare(cookie1, cookie2)
- {
- if (cookie1.session !== cookie2.session)
- return sortDirection * (cookie1.session ? 1 : -1);
- if (cookie1.session)
- return 0;
- return sortDirection * (cookie1.expires - cookie2.expires);
- }
- var comparator;
- switch (parseInt(this._dataGrid.sortColumnIdentifier, 10)) {
- case 0: comparator = localeCompare.bind(this, "name"); break;
- case 1: comparator = localeCompare.bind(this, "value"); break;
- case 2: comparator = localeCompare.bind(this, "domain"); break;
- case 3: comparator = localeCompare.bind(this, "path"); break;
- case 4: comparator = expiresCompare; break;
- case 5: comparator = numberCompare.bind(this, "size"); break;
- case 6: comparator = localeCompare.bind(this, "httpOnly"); break;
- case 7: comparator = localeCompare.bind(this, "secure"); break;
- default: localeCompare.bind(this, "name");
- }
- cookies.sort(comparator);
- },
- _deleteCallback: function(node)
- {
- if (!node || !node.cookie)
- return;
- var cookie = node.cookie;
- var cookieURL = (cookie.secure ? "https://" : "http://") + cookie.domain + cookie.path;
- // COMPATIBILITY (iOS 6): PageAgent.deleteCookie used to take 'domain', now takes 'url'. Send both.
- PageAgent.deleteCookie.invoke({cookieName: cookie.name, domain: cookie.domain, url: cookieURL});
- this.update();
- }
- };
- WebInspector.CookieStorageContentView.prototype.__proto__ = WebInspector.ContentView.prototype;
- WebInspector.cookieMatchesResourceURL = function(cookie, resourceURL)
- {
- var parsedURL = parseURL(resourceURL);
- if (!parsedURL || !WebInspector.cookieDomainMatchesResourceDomain(cookie.domain, parsedURL.host))
- return false;
- return (parsedURL.path.startsWith(cookie.path)
- && (!cookie.port || parsedURL.port == cookie.port)
- && (!cookie.secure || parsedURL.scheme === "https"));
- }
- WebInspector.cookieDomainMatchesResourceDomain = function(cookieDomain, resourceDomain)
- {
- if (cookieDomain.charAt(0) !== '.')
- return resourceDomain === cookieDomain;
- return !!resourceDomain.match(new RegExp("^([^\\.]+\\.)?" + cookieDomain.substring(1).escapeForRegExp() + "$"), "i");
- }
- WebInspector.CookieType = {
- Request: 0,
- Response: 1
- };
|