Skip to content

Commit

Permalink
[DevTools] Use a single CookieItemsView instance
Browse files Browse the repository at this point in the history
BUG=

Review-Url: https://codereview.chromium.org/2707283003
Cr-Commit-Position: refs/heads/master@{#452559}
  • Loading branch information
eostroukhov authored and Commit bot committed Feb 23, 2017
1 parent 599b999 commit a3ec910
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 57 deletions.
48 changes: 17 additions & 31 deletions front_end/cookie_table/CookiesTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,47 +33,35 @@
*/
CookieTable.CookiesTable = class extends UI.VBox {
/**
* @param {!SDK.Target} target
* @param {boolean} readOnly
* @param {function(!SDK.Cookie, ?SDK.Cookie, function(?string))=} saveCallback
* @param {function()=} refreshCallback
* @param {function()=} selectedCallback
* @param {string=} cookieDomain
*/
constructor(target, readOnly, refreshCallback, selectedCallback, cookieDomain) {
constructor(saveCallback, refreshCallback, selectedCallback, cookieDomain) {
super();

this._model = SDK.CookieModel.fromTarget(target);
this._readOnly = readOnly;
this._saveCallback = saveCallback;
this._refreshCallback = refreshCallback;
this._cookieDomain = cookieDomain;

var editable = !!saveCallback;

var columns = /** @type {!Array<!DataGrid.DataGrid.ColumnDescriptor>} */ ([
{
id: 'name',
title: Common.UIString('Name'),
sortable: true,
disclosure: !this._readOnly,
disclosure: editable,
sort: DataGrid.DataGrid.Order.Ascending,
longText: true,
weight: 24,
editable: !this._readOnly
},
{
id: 'value',
title: Common.UIString('Value'),
sortable: true,
longText: true,
weight: 34,
editable: !this._readOnly
},
{id: 'domain', title: Common.UIString('Domain'), sortable: true, weight: 7, editable: !this._readOnly},
{id: 'path', title: Common.UIString('Path'), sortable: true, weight: 7, editable: !this._readOnly}, {
id: 'expires',
title: Common.UIString('Expires / Max-Age'),
sortable: true,
weight: 7,
editable: !this._readOnly
editable: editable
},
{id: 'value', title: Common.UIString('Value'), sortable: true, longText: true, weight: 34, editable: editable},
{id: 'domain', title: Common.UIString('Domain'), sortable: true, weight: 7, editable: editable},
{id: 'path', title: Common.UIString('Path'), sortable: true, weight: 7, editable: editable},
{id: 'expires', title: Common.UIString('Expires / Max-Age'), sortable: true, weight: 7, editable: editable},
{id: 'size', title: Common.UIString('Size'), sortable: true, align: DataGrid.DataGrid.Align.Right, weight: 7}, {
id: 'httpOnly',
title: Common.UIString('HTTP'),
Expand All @@ -97,11 +85,11 @@ CookieTable.CookiesTable = class extends UI.VBox {
}
]);

if (this._readOnly) {
this._dataGrid = new DataGrid.DataGrid(columns);
} else {
if (editable) {
this._dataGrid = new DataGrid.DataGrid(
columns, this._onUpdateCookie.bind(this), this._onDeleteCookie.bind(this), refreshCallback);
} else {
this._dataGrid = new DataGrid.DataGrid(columns);
}

this._dataGrid.setName('cookiesTable');
Expand Down Expand Up @@ -180,7 +168,7 @@ CookieTable.CookiesTable = class extends UI.VBox {
}
if (selectedCookie && lastEditedColumnId && !this._dataGrid.selectedNode)
this._addInactiveNode(this._dataGrid.rootNode(), selectedCookie, lastEditedColumnId);
if (!this._readOnly)
if (this._saveCallback)
this._dataGrid.addCreationNode(false);
}

Expand Down Expand Up @@ -371,11 +359,9 @@ CookieTable.CookiesTable = class extends UI.VBox {
_saveNode(node) {
var oldCookie = node.cookie;
var newCookie = this._createCookieFromData(node.data);
if (oldCookie && (newCookie.name() !== oldCookie.name() || newCookie.url() !== oldCookie.url()))
oldCookie.remove();
node.cookie = newCookie;
this._model.saveCookie(newCookie, success => {
if (success)
this._saveCallback(newCookie, oldCookie, error => {
if (!error)
this._refresh();
else
node.setDirty(true);
Expand Down
2 changes: 1 addition & 1 deletion front_end/network/RequestCookiesView.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Network.RequestCookiesView = class extends UI.VBox {
_buildCookiesTable() {
this.detachChildWidgets();

this._cookiesTable = new CookieTable.CookiesTable(this._request.target(), true);
this._cookiesTable = new CookieTable.CookiesTable();
this._cookiesTable.setCookieFolders([
{folderName: Common.UIString('Request Cookies'), cookies: this._request.requestCookies},
{folderName: Common.UIString('Response Cookies'), cookies: this._request.responseCookies}
Expand Down
36 changes: 30 additions & 6 deletions front_end/resources/CookieItemsView.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,47 @@
Resources.CookieItemsView = class extends Resources.StorageItemsView {
/**
* @param {!Resources.CookieTreeElement} treeElement
* @param {!SDK.Target} target
* @param {!SDK.CookieModel} model
* @param {string} cookieDomain
*/
constructor(treeElement, target, cookieDomain) {
constructor(treeElement, model, cookieDomain) {
super(Common.UIString('Cookies'), 'cookiesPanel');

this.element.classList.add('storage-view');

this._model = SDK.CookieModel.fromTarget(target);
this._model = model;
this._treeElement = treeElement;
this._cookieDomain = cookieDomain;

this._totalSize = 0;
/** @type {?CookieTable.CookiesTable} */
this._cookiesTable = null;
this.setCookiesDomain(model, cookieDomain);
}

/**
* @param {!SDK.CookieModel} model
* @param {string} domain
*/
setCookiesDomain(model, domain) {
this._model = model;
this._cookieDomain = domain;
this.refreshItems();
}

/**
* @param {!SDK.Cookie} newCookie
* @param {?SDK.Cookie} oldCookie
* @param {function(?string)} callback
*/
_saveCookie(newCookie, oldCookie, callback) {
if (!this._model) {
callback(Common.UIString('Unable to save the cookie'));
return;
}
if (oldCookie && (newCookie.name() !== oldCookie.name() || newCookie.url() !== oldCookie.url()))
this._model.deleteCookie(oldCookie);
this._model.saveCookie(newCookie, callback);
}

/**
Expand All @@ -57,14 +83,12 @@ Resources.CookieItemsView = class extends Resources.StorageItemsView {
const parsedURL = this._cookieDomain.asParsedURL();
const domain = parsedURL ? parsedURL.host : '';
this._cookiesTable = new CookieTable.CookiesTable(
this._model.target(), false, this.refreshItems.bind(this), () => this.setCanDeleteSelected(true), domain);
this._saveCookie.bind(this), this.refreshItems.bind(this), () => this.setCanDeleteSelected(true), domain);
}

var shownCookies = this.filter(allCookies, cookie => `${cookie.name()} ${cookie.value()} ${cookie.domain()}`);
this._cookiesTable.setCookies(shownCookies);
this._cookiesTable.show(this.element);
this._treeElement.subtitle =
String.sprintf(Common.UIString('%d cookies (%s)'), allCookies.length, Number.bytesToString(this._totalSize));
this.setCanFilter(true);
this.setCanDeleteAll(true);
this.setCanDeleteSelected(!!this._cookiesTable.selectedCookie());
Expand Down
42 changes: 23 additions & 19 deletions front_end/resources/ResourcesPanel.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ Resources.ResourcesPanel = class extends UI.PanelWithSidebar {
this._databaseTreeElements = new Map();
/** @type {!Map.<!Resources.DOMStorage, !Resources.DOMStorageTreeElement>} */
this._domStorageTreeElements = new Map();
/** @type {!Object.<string, !Resources.CookieItemsView>} */
this._cookieViews = {};
/** @type {!Object.<string, boolean>} */
this._domains = {};

/** @type {?Resources.DOMStorageItemsView} */
this._domStorageView = null;
/** @type {?Resources.CookieItemsView} */
this._cookieView = null;

this.panelSidebarElement().addEventListener('mousemove', this._onmousemove.bind(this), false);
this.panelSidebarElement().addEventListener('mouseleave', this._onmouseleave.bind(this), false);
Expand Down Expand Up @@ -269,7 +269,6 @@ Resources.ResourcesPanel = class extends UI.PanelWithSidebar {
this.visibleView.detach();
delete this.visibleView;
}
this._cookieViews = {};
this.cookieListTreeElement.removeChildren();
}

Expand Down Expand Up @@ -596,21 +595,23 @@ Resources.ResourcesPanel = class extends UI.PanelWithSidebar {
* @param {!SDK.Target} cookieFrameTarget
*/
showCookies(treeElement, cookieDomain, cookieFrameTarget) {
var view = this._cookieViews[cookieDomain];
if (!view) {
view = new Resources.CookieItemsView(treeElement, cookieFrameTarget, cookieDomain);
this._cookieViews[cookieDomain] = view;
}

this._innerShowView(view);
var model = SDK.CookieModel.fromTarget(cookieFrameTarget);
if (!this._cookieView)
this._cookieView = new Resources.CookieItemsView(treeElement, model, cookieDomain);
else
this._cookieView.setCookiesDomain(model, cookieDomain);
this._innerShowView(this._cookieView);
}

/**
* @param {!SDK.Target} target
* @param {string} cookieDomain
*/
_clearCookies(cookieDomain) {
if (this._cookieViews[cookieDomain])
this._cookieViews[cookieDomain].deleteAllItems();
_clearCookies(target, cookieDomain) {
SDK.CookieModel.fromTarget(target).clear(cookieDomain, () => {
if (this._cookieView)
this._cookieView.refreshItems();
});
}

showApplicationCache(frameId) {
Expand Down Expand Up @@ -1945,13 +1946,15 @@ Resources.DOMStorageTreeElement = class extends Resources.BaseStorageTreeElement
}
};

/**
* @unrestricted
*/
Resources.CookieTreeElement = class extends Resources.BaseStorageTreeElement {
/**
* @param {!Resources.ResourcesPanel} storagePanel
* @param {!SDK.ResourceTreeFrame} frame
* @param {string} cookieDomain
*/
constructor(storagePanel, frame, cookieDomain) {
super(storagePanel, cookieDomain ? cookieDomain : Common.UIString('Local Files'), false);
this._frame = frame;
this._target = frame.target();
this._cookieDomain = cookieDomain;
var icon = UI.Icon.create('mediumicon-cookie', 'resource-tree-item');
this.setLeadingIcons([icon]);
Expand All @@ -1974,7 +1977,8 @@ Resources.CookieTreeElement = class extends Resources.BaseStorageTreeElement {
*/
_handleContextMenuEvent(event) {
var contextMenu = new UI.ContextMenu(event);
contextMenu.appendItem(Common.UIString('Clear'), () => this._storagePanel._clearCookies(this._cookieDomain));
contextMenu.appendItem(
Common.UIString('Clear'), () => this._storagePanel._clearCookies(this._target, this._cookieDomain));
contextMenu.show();
}

Expand All @@ -1984,7 +1988,7 @@ Resources.CookieTreeElement = class extends Resources.BaseStorageTreeElement {
*/
onselect(selectedByUser) {
super.onselect(selectedByUser);
this._storagePanel.showCookies(this, this._cookieDomain, this._frame.target());
this._storagePanel.showCookies(this, this._cookieDomain, this._target);
return false;
}
};
Expand Down

0 comments on commit a3ec910

Please sign in to comment.