Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(69)

Side by Side Diff: third_party/WebKit/Source/devtools/front_end/components_lazy/CookiesTable.js

Issue 2567873002: DevTools: Add ability to add and edit cookies (Closed)
Patch Set: Code review fixes - part 2. Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | third_party/WebKit/Source/devtools/front_end/resources/CookieItemsView.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2009 Apple Inc. All rights reserved. 2 * Copyright (C) 2009 Apple Inc. All rights reserved.
3 * Copyright (C) 2009 Joseph Pecoraro 3 * Copyright (C) 2009 Joseph Pecoraro
4 * Copyright (C) 2010 Google Inc. All rights reserved. 4 * Copyright (C) 2010 Google Inc. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions 7 * modification, are permitted provided that the following conditions
8 * are met: 8 * are met:
9 * 9 *
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
(...skipping 15 matching lines...) Expand all
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 /** 31 /**
32 * @unrestricted 32 * @unrestricted
33 */ 33 */
34 Components.CookiesTable = class extends UI.VBox { 34 Components.CookiesTable = class extends UI.VBox {
35 /** 35 /**
36 * @param {boolean} expandable 36 * @param {boolean} readOnly
37 * @param {function()=} refreshCallback 37 * @param {function()=} refreshCallback
38 * @param {function()=} selectedCallback 38 * @param {function()=} selectedCallback
39 * @param {string=} cookieDomain
39 */ 40 */
40 constructor(expandable, refreshCallback, selectedCallback) { 41 constructor(readOnly, refreshCallback, selectedCallback, cookieDomain) {
41 super(); 42 super();
42 43
43 var readOnly = expandable; 44 this._readOnly = readOnly;
44 this._refreshCallback = refreshCallback; 45 this._refreshCallback = refreshCallback;
46 this._cookieDomain = cookieDomain;
45 47
46 var columns = /** @type {!Array<!UI.DataGrid.ColumnDescriptor>} */ ([ 48 var columns = /** @type {!Array<!UI.DataGrid.ColumnDescriptor>} */ ([
47 { 49 {
48 id: 'name', 50 id: 'name',
49 title: Common.UIString('Name'), 51 title: Common.UIString('Name'),
50 sortable: true, 52 sortable: true,
51 disclosure: expandable, 53 disclosure: !this._readOnly,
52 sort: UI.DataGrid.Order.Ascending, 54 sort: UI.DataGrid.Order.Ascending,
53 longText: true, 55 longText: true,
54 weight: 24 56 weight: 24,
57 editable: true
allada 2016/12/14 03:04:15 Shouldn't we still address this? by setting it whe
kdzwinel 2016/12/14 14:26:50 Ah! It wasn't intended. I just missed this one.
55 }, 58 },
56 {id: 'value', title: Common.UIString('Value'), sortable: true, longText: t rue, weight: 34}, 59 {id: 'value', title: Common.UIString('Value'), sortable: true, longText: t rue, weight: 34, editable: !this._readOnly},
57 {id: 'domain', title: Common.UIString('Domain'), sortable: true, weight: 7 }, 60 {id: 'domain', title: Common.UIString('Domain'), sortable: true, weight: 7 , editable: !this._readOnly},
58 {id: 'path', title: Common.UIString('Path'), sortable: true, weight: 7}, 61 {id: 'path', title: Common.UIString('Path'), sortable: true, weight: 7, ed itable: !this._readOnly},
59 {id: 'expires', title: Common.UIString('Expires / Max-Age'), sortable: tru e, weight: 7}, 62 {id: 'expires', title: Common.UIString('Expires / Max-Age'), sortable: tru e, weight: 7, editable: !this._readOnly},
60 {id: 'size', title: Common.UIString('Size'), sortable: true, align: UI.Dat aGrid.Align.Right, weight: 7}, 63 {id: 'size', title: Common.UIString('Size'), sortable: true, align: UI.Dat aGrid.Align.Right, weight: 7},
61 {id: 'httpOnly', title: Common.UIString('HTTP'), sortable: true, align: UI .DataGrid.Align.Center, weight: 7}, 64 {id: 'httpOnly', title: Common.UIString('HTTP'), sortable: true, align: UI .DataGrid.Align.Center, weight: 7},
62 {id: 'secure', title: Common.UIString('Secure'), sortable: true, align: UI .DataGrid.Align.Center, weight: 7}, { 65 {id: 'secure', title: Common.UIString('Secure'), sortable: true, align: UI .DataGrid.Align.Center, weight: 7}, {
63 id: 'sameSite', 66 id: 'sameSite',
64 title: Common.UIString('SameSite'), 67 title: Common.UIString('SameSite'),
65 sortable: true, 68 sortable: true,
66 align: UI.DataGrid.Align.Center, 69 align: UI.DataGrid.Align.Center,
67 weight: 7 70 weight: 7
68 } 71 }
69 ]); 72 ]);
70 73
71 if (readOnly) { 74 if (this._readOnly) {
72 this._dataGrid = new UI.DataGrid(columns); 75 this._dataGrid = new UI.DataGrid(columns);
73 } else { 76 } else {
74 this._dataGrid = new UI.DataGrid(columns, undefined, this._onDeleteCookie. bind(this), refreshCallback); 77 this._dataGrid = new UI.DataGrid(columns, this._onUpdateCookie.bind(this), this._onDeleteCookie.bind(this), refreshCallback);
75 this._dataGrid.setRowContextMenuCallback(this._onRowContextMenu.bind(this) ); 78 this._dataGrid.setRowContextMenuCallback(this._onRowContextMenu.bind(this) );
76 } 79 }
77 80
78 this._dataGrid.setName('cookiesTable'); 81 this._dataGrid.setName('cookiesTable');
79 this._dataGrid.addEventListener(UI.DataGrid.Events.SortingChanged, this._reb uildTable, this); 82 this._dataGrid.addEventListener(UI.DataGrid.Events.SortingChanged, this._reb uildTable, this);
80 83
81 if (selectedCallback) 84 if (selectedCallback)
82 this._dataGrid.addEventListener(UI.DataGrid.Events.SelectedNode, selectedC allback, this); 85 this._dataGrid.addEventListener(UI.DataGrid.Events.SelectedNode, selectedC allback, this);
83 86
84 this._nextSelectedCookie = /** @type {?SDK.Cookie} */ (null); 87 this._nextSelectedCookie = /** @type {?SDK.Cookie} */ (null);
88 /** @type {?string} */
89 this._lastEditedColumnId = null;
85 90
86 this._dataGrid.asWidget().show(this.element); 91 this._dataGrid.asWidget().show(this.element);
87 this._data = []; 92 this._data = [];
88 } 93 }
89 94
90 /** 95 /**
91 * @param {?string} domain 96 * @param {?string} domain
92 */ 97 */
93 _clearAndRefresh(domain) { 98 _clearAndRefresh(domain) {
94 this.clear(domain); 99 this.clear(domain);
95 this._refresh(); 100 this._refresh();
96 } 101 }
97 102
98 /** 103 /**
99 * @param {!UI.ContextMenu} contextMenu 104 * @param {!UI.ContextMenu} contextMenu
100 * @param {!UI.DataGridNode} node 105 * @param {!UI.DataGridNode} node
101 */ 106 */
102 _onRowContextMenu(contextMenu, node) { 107 _onRowContextMenu(contextMenu, node) {
103 if (node === this._dataGrid.creationNode) 108 if (node.isCreationNode)
104 return; 109 return;
105 var domain = node.cookie.domain(); 110
111 const cookie = node.cookie;
112 const checkmark = '\u2713';
113 contextMenu.appendCheckboxItem(
114 Common.UIString('Secure flag'),
115 this._setCookieFlag.bind(this, node, 'secure', cookie.secure() ? '' : chec kmark),
116 cookie.secure(),
117 false
118 );
119 contextMenu.appendCheckboxItem(
120 Common.UIString('HttpOnly flag'),
121 this._setCookieFlag.bind(this, node, 'httpOnly', cookie.httpOnly() ? '' : checkmark),
122 cookie.httpOnly(),
123 false
124 );
125
126 var sameSiteSubmenu = contextMenu.appendSubMenuItem(Common.UIString('SameSit e flag'));
127 sameSiteSubmenu.appendItem(
128 Common.UIString('No Restriction'),
129 this._setCookieFlag.bind(this, node, 'sameSite', ''),
130 false
131 );
132 sameSiteSubmenu.appendCheckboxItem(
133 Protocol.Network.CookieSameSite.Lax,
134 this._setCookieFlag.bind(this, node, 'sameSite', Protocol.Network.CookieSa meSite.Lax),
135 cookie.sameSite() === Protocol.Network.CookieSameSite.Lax,
136 false
137 );
138 sameSiteSubmenu.appendCheckboxItem(
139 Protocol.Network.CookieSameSite.Strict,
140 this._setCookieFlag.bind(this, node, 'sameSite', Protocol.Network.CookieSa meSite.Strict),
141 cookie.sameSite() === Protocol.Network.CookieSameSite.Strict,
142 false
143 );
144 contextMenu.appendSeparator();
145
146 var domain = cookie.domain();
106 if (domain) { 147 if (domain) {
107 contextMenu.appendItem( 148 contextMenu.appendItem(
108 Common.UIString.capitalize('Clear ^all from "%s"', domain), this._clea rAndRefresh.bind(this, domain)); 149 Common.UIString.capitalize('Clear ^all from "%s"', domain), this._clea rAndRefresh.bind(this, domain));
109 } 150 }
110 contextMenu.appendItem(Common.UIString.capitalize('Clear ^all'), this._clear AndRefresh.bind(this, null)); 151 contextMenu.appendItem(Common.UIString.capitalize('Clear ^all'), this._clear AndRefresh.bind(this, null));
111 } 152 }
112 153
154 _setCookieFlag(node, flag, value) {
155 node.data[flag] = value;
156 node.refresh();
157 this._saveNode(node);
158 }
159
113 /** 160 /**
114 * @param {!Array.<!SDK.Cookie>} cookies 161 * @param {!Array.<!SDK.Cookie>} cookies
115 */ 162 */
116 setCookies(cookies) { 163 setCookies(cookies) {
117 this.setCookieFolders([{cookies: cookies}]); 164 this.setCookieFolders([{cookies: cookies}]);
118 } 165 }
119 166
120 /** 167 /**
121 * @param {!Array.<!{folderName: ?string, cookies: !Array.<!SDK.Cookie>}>} coo kieFolders 168 * @param {!Array.<!{folderName: ?string, cookies: !Array.<!SDK.Cookie>}>} coo kieFolders
122 */ 169 */
(...skipping 16 matching lines...) Expand all
139 clear(domain) { 186 clear(domain) {
140 for (var i = 0, length = this._data.length; i < length; ++i) { 187 for (var i = 0, length = this._data.length; i < length; ++i) {
141 var cookies = this._data[i].cookies; 188 var cookies = this._data[i].cookies;
142 for (var j = 0, cookieCount = cookies.length; j < cookieCount; ++j) { 189 for (var j = 0, cookieCount = cookies.length; j < cookieCount; ++j) {
143 if (!domain || cookies[j].domain() === domain) 190 if (!domain || cookies[j].domain() === domain)
144 cookies[j].remove(); 191 cookies[j].remove();
145 } 192 }
146 } 193 }
147 } 194 }
148 195
196 /**
197 * @override
198 */
199 willHide() {
200 this._lastEditedColumnId = null;
201 }
202
149 _rebuildTable() { 203 _rebuildTable() {
150 var selectedCookie = this._nextSelectedCookie || this.selectedCookie(); 204 var selectedCookie = this._nextSelectedCookie || this.selectedCookie();
205 var lastEditedColumnId = this._lastEditedColumnId;
151 this._nextSelectedCookie = null; 206 this._nextSelectedCookie = null;
207 this._lastEditedColumnId = null;
152 this._dataGrid.rootNode().removeChildren(); 208 this._dataGrid.rootNode().removeChildren();
153 for (var i = 0; i < this._data.length; ++i) { 209 for (var i = 0; i < this._data.length; ++i) {
154 var item = this._data[i]; 210 var item = this._data[i];
155 if (item.folderName) { 211 if (item.folderName) {
156 var groupData = { 212 var groupData = {
157 name: item.folderName, 213 name: item.folderName,
158 value: '', 214 value: '',
159 domain: '', 215 domain: '',
160 path: '', 216 path: '',
161 expires: '', 217 expires: '',
162 size: this._totalSize(item.cookies), 218 size: this._totalSize(item.cookies),
163 httpOnly: '', 219 httpOnly: '',
164 secure: '', 220 secure: '',
165 sameSite: '' 221 sameSite: ''
166 }; 222 };
167 var groupNode = new UI.DataGridNode(groupData); 223 var groupNode = new UI.DataGridNode(groupData);
168 groupNode.selectable = true; 224 groupNode.selectable = true;
169 this._dataGrid.rootNode().appendChild(groupNode); 225 this._dataGrid.rootNode().appendChild(groupNode);
170 groupNode.element().classList.add('row-group'); 226 groupNode.element().classList.add('row-group');
171 this._populateNode(groupNode, item.cookies, selectedCookie); 227 this._populateNode(groupNode, item.cookies, selectedCookie, lastEditedCo lumnId);
172 groupNode.expand(); 228 groupNode.expand();
173 } else { 229 } else {
174 this._populateNode(this._dataGrid.rootNode(), item.cookies, selectedCook ie); 230 this._populateNode(this._dataGrid.rootNode(), item.cookies, selectedCook ie, lastEditedColumnId);
175 } 231 }
176 } 232 }
233 if (!this._readOnly)
234 this._dataGrid.addCreationNode(false);
177 } 235 }
178 236
179 /** 237 /**
180 * @param {!UI.DataGridNode} parentNode 238 * @param {!UI.DataGridNode} parentNode
181 * @param {?Array.<!SDK.Cookie>} cookies 239 * @param {?Array.<!SDK.Cookie>} cookies
182 * @param {?SDK.Cookie} selectedCookie 240 * @param {?SDK.Cookie} selectedCookie
241 * @param {?string} lastEditedColumnId
183 */ 242 */
184 _populateNode(parentNode, cookies, selectedCookie) { 243 _populateNode(parentNode, cookies, selectedCookie, lastEditedColumnId) {
185 parentNode.removeChildren(); 244 parentNode.removeChildren();
186 if (!cookies) 245 if (!cookies)
187 return; 246 return;
188 247
189 this._sortCookies(cookies); 248 this._sortCookies(cookies);
190 for (var i = 0; i < cookies.length; ++i) { 249 for (var i = 0; i < cookies.length; ++i) {
191 var cookie = cookies[i]; 250 var cookie = cookies[i];
192 var cookieNode = this._createGridNode(cookie); 251 var cookieNode = this._createGridNode(cookie);
193 parentNode.appendChild(cookieNode); 252 parentNode.appendChild(cookieNode);
194 if (selectedCookie && selectedCookie.name() === cookie.name() && selectedC ookie.domain() === cookie.domain() && 253 if (selectedCookie && selectedCookie.name() === cookie.name() && selectedC ookie.domain() === cookie.domain() &&
195 selectedCookie.path() === cookie.path()) 254 selectedCookie.path() === cookie.path()) {
196 cookieNode.select(); 255 cookieNode.select();
256 if (lastEditedColumnId !== null)
257 this._dataGrid.startEditingNextEditableColumnOfDataGridNode(cookieNode , lastEditedColumnId);
258 }
197 } 259 }
198 } 260 }
199 261
200 _totalSize(cookies) { 262 _totalSize(cookies) {
201 var totalSize = 0; 263 var totalSize = 0;
202 for (var i = 0; cookies && i < cookies.length; ++i) 264 for (var i = 0; cookies && i < cookies.length; ++i)
203 totalSize += cookies[i].size(); 265 totalSize += cookies[i].size();
204 return totalSize; 266 return totalSize;
205 } 267 }
206 268
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
301 363
302 _onDeleteCookie(node) { 364 _onDeleteCookie(node) {
303 var cookie = node.cookie; 365 var cookie = node.cookie;
304 var neighbour = node.traverseNextNode() || node.traversePreviousNode(); 366 var neighbour = node.traverseNextNode() || node.traversePreviousNode();
305 if (neighbour) 367 if (neighbour)
306 this._nextSelectedCookie = neighbour.cookie; 368 this._nextSelectedCookie = neighbour.cookie;
307 cookie.remove(); 369 cookie.remove();
308 this._refresh(); 370 this._refresh();
309 } 371 }
310 372
373 /**
374 * @param {!UI.DataGridNode} editingNode
375 * @param {string} columnIdentifier
376 * @param {string} oldText
377 * @param {string} newText
378 */
379 _onUpdateCookie(editingNode, columnIdentifier, oldText, newText) {
380 this._lastEditedColumnId = columnIdentifier;
381 this._setDefaults(editingNode);
382 if (this._isValidCookieData(editingNode.data))
383 this._saveNode(editingNode);
384 else
385 editingNode.unsaved = true;
386 }
387
388 /**
389 * @param {!UI.DataGridNode} node
390 */
391 _setDefaults(node) {
392 if (node.data.name === null)
393 node.data.name = '';
394 if (node.data.value === null)
395 node.data.value = '';
396 if (node.data.domain === null)
397 node.data.domain = this._cookieDomain;
398 if (node.data.path === null)
399 node.data.path = '/';
400 if (node.data.expires === null)
401 node.data.expires = Common.UIString('Session');
402 }
403
404 /**
405 * @param {!UI.DataGridNode} node
406 */
407 _saveNode(node) {
408 var oldCookie = node.cookie;
409 var newCookie = this._createCookieFromData(node.data);
410 if (oldCookie && (newCookie.name() !== oldCookie.name() || newCookie.url() ! == oldCookie.url()))
411 oldCookie.remove();
412 node.cookie = newCookie;
413 newCookie.save((error, success) => {
414 if (success)
415 this._refresh();
416 else
417 node.unsaved = true;
418 });
419 this._nextSelectedCookie = newCookie;
420 }
421
422 /**
423 * @param {!Object.<string, *>} data
424 * @returns {SDK.Cookie}
425 */
426 _createCookieFromData(data) {
427 var target = SDK.targetManager.targets(SDK.Target.Capability.Network)[0];
428 var cookie = new SDK.Cookie(target, data.name, data.value, null);
429 cookie.addAttribute('domain', data.domain);
430 cookie.addAttribute('path', data.path);
431 if (data.expires && data.expires !== Common.UIString('Session')) {
432 var secondsSinceEpoch = Math.floor(Date.parse(data.expires) / 1000);
433 cookie.addAttribute('expires', secondsSinceEpoch || undefined);
434 }
435 if (data.httpOnly)
436 cookie.addAttribute('httpOnly');
437 if (data.secure)
438 cookie.addAttribute('secure');
439 if (data.sameSite)
440 cookie.addAttribute('sameSite', data.sameSite);
441 cookie.setSize(data.name.length + data.value.length);
442 return cookie;
443 }
444
445 /**
446 * @param {!Object.<string, *>} data
447 * @returns {boolean}
448 */
449 _isValidCookieData(data) {
450 return (data.name || data.value) && this._isValidDomain(data.domain) && this ._isValidPath(data.path) && this._isValidDate(data.expires);
451 }
452
453 /**
454 * @param {string} domain
455 * @returns {boolean}
456 */
457 _isValidDomain(domain) {
458 if (!domain)
459 return true;
460 var parsedURL = ('http://' + domain).asParsedURL();
461 return !!parsedURL && parsedURL.domain() === domain;
allada 2016/12/14 03:04:15 Any reason we are double-banging this? I thought i
kdzwinel 2016/12/14 14:26:50 without the double-bang this method may return nul
462 }
463
464 /**
465 * @param {string} path
466 * @returns {boolean}
467 */
468 _isValidPath(path) {
469 var parsedURL = ('http://example.com' + path).asParsedURL();
470 return !!parsedURL && parsedURL.path === path;
allada 2016/12/14 03:04:15 Same as above.
471 }
472
473 /**
474 * @param {string} date
475 * @returns {boolean}
476 */
477 _isValidDate(date) {
478 return date === '' || date === Common.UIString('Session') || !isNaN(Date.par se(date));
allada 2016/12/14 03:04:15 I think we can just this here: return !isNaN(Date.
kdzwinel 2016/12/14 14:26:50 This will cause empty string and `Common.UIString(
479 }
480
311 _refresh() { 481 _refresh() {
312 if (this._refreshCallback) 482 if (this._refreshCallback)
313 this._refreshCallback(); 483 this._refreshCallback();
314 } 484 }
315 }; 485 };
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Source/devtools/front_end/resources/CookieItemsView.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698