text: import*as Common from"../common/common.js";import*as DataGrid from"../data_grid/data_grid.js";import*as Network from"../network/network.js";import*as SDK from"../sdk/sdk.js";import*as UI from"../ui/ui.js";const expiresSessionValue=Common.UIString.UIString("Session");export class CookiesTable extends UI.Widget.VBox{constructor(e,t,i,o,a){super(),this.registerRequiredCSS("cookie_table/cookiesTable.css"),this.element.classList.add("cookies-table"),this._saveCallback=t,this._refreshCallback=i,this._deleteCallback=a;const s=!!t,r=[{id:SDK.Cookie.Attributes.Name,title:ls`Name`,sortable:!0,disclosure:s,sort:DataGrid.DataGrid.Order.Ascending,longText:!0,weight:24,editable:s},{id:SDK.Cookie.Attributes.Value,title:ls`Value`,sortable:!0,longText:!0,weight:34,editable:s},{id:SDK.Cookie.Attributes.Domain,title:ls`Domain`,sortable:!0,weight:7,editable:s},{id:SDK.Cookie.Attributes.Path,title:ls`Path`,sortable:!0,weight:7,editable:s},{id:SDK.Cookie.Attributes.Expires,title:ls`Expires / Max-Age`,sortable:!0,weight:7,editable:s},{id:SDK.Cookie.Attributes.Size,title:ls`Size`,sortable:!0,align:DataGrid.DataGrid.Align.Right,weight:7},{id:SDK.Cookie.Attributes.HttpOnly,title:ls`HttpOnly`,sortable:!0,align:DataGrid.DataGrid.Align.Center,weight:7,dataType:DataGrid.DataGrid.DataType.Boolean,editable:s},{id:SDK.Cookie.Attributes.Secure,title:ls`Secure`,sortable:!0,align:DataGrid.DataGrid.Align.Center,weight:7,dataType:DataGrid.DataGrid.DataType.Boolean,editable:s},{id:SDK.Cookie.Attributes.SameSite,title:ls`SameSite`,sortable:!0,weight:7,editable:s},{id:SDK.Cookie.Attributes.Priority,title:ls`Priority`,sortable:!0,sort:DataGrid.DataGrid.Order.Descending,weight:7,editable:s}];this._dataGrid=s?new DataGrid.DataGrid.DataGridImpl({displayName:ls`Editable Cookies`,columns:r,editCallback:this._onUpdateCookie.bind(this),deleteCallback:this._onDeleteCookie.bind(this),refreshCallback:i}):new DataGrid.DataGrid.DataGridImpl({displayName:ls`Cookies`,columns:r}),this._dataGrid.setStriped(!0),this._dataGrid.setName("cookiesTable"),this._dataGrid.addEventListener(DataGrid.DataGrid.Events.SortingChanged,this._rebuildTable,this),this._dataGrid.setRowContextMenuCallback(this._populateContextMenu.bind(this)),e&&this._dataGrid.renderInline(),o&&this._dataGrid.addEventListener(DataGrid.DataGrid.Events.SelectedNode,o,this),this._lastEditedColumnId=null,this._dataGrid.asWidget().show(this.element),this._data=[],this._cookieDomain="",this._cookieToBlockedReasons=null}setCookies(e,t){this.setCookieFolders([{cookies:e}],t)}setCookieFolders(e,t){this._data=e,this._cookieToBlockedReasons=t||null,this._rebuildTable()}setCookieDomain(e){this._cookieDomain=e}selectedCookie(){const e=this._dataGrid.selectedNode;return e?e.cookie:null}_getSelectionCookies(){const e=this._dataGrid.selectedNode,t=e&&e.traverseNextNode(!0),i=e&&e.traversePreviousNode(!0);return{current:e&&e.cookie,neighbor:t&&t.cookie||i&&i.cookie}}willHide(){this._lastEditedColumnId=null}_findSelectedCookie(e,t){if(!t)return null;const i=e.current,o=t.find(e=>this._isSameCookie(e,i));if(o)return o;const a=e.neighbor,s=t.find(e=>this._isSameCookie(e,a));return s||null}_isSameCookie(e,t){return!!t&&t.name()===e.name()&&t.domain()===e.domain()&&t.path()===e.path()}_rebuildTable(){const e=this._getSelectionCookies(),t=this._lastEditedColumnId;this._lastEditedColumnId=null,this._dataGrid.rootNode().removeChildren();for(let i=0;i<this._data.length;++i){const o=this._data[i],a=this._findSelectedCookie(e,o.cookies);if(o.folderName){const e={};e[SDK.Cookie.Attributes.Name]=o.folderName,e[SDK.Cookie.Attributes.Value]="",e[SDK.Cookie.Attributes.Size]=this._totalSize(o.cookies),e[SDK.Cookie.Attributes.Domain]="",e[SDK.Cookie.Attributes.Path]="",e[SDK.Cookie.Attributes.Expires]="",e[SDK.Cookie.Attributes.HttpOnly]="",e[SDK.Cookie.Attributes.Secure]="",e[SDK.Cookie.Attributes.SameSite]="",e[SDK.Cookie.Attributes.Priority]="";const i=new DataGrid.DataGrid.DataGridNode(e);i.selectable=!0,this._dataGrid.rootNode().appendChild(i),i.element().classList.add("row-group"),this._populateNode(i,o.cookies,a,t),i.expand()}else this._populateNode(this._dataGrid.rootNode(),o.cookies,a,t)}e.current&&t&&!this._dataGrid.selectedNode&&this._addInactiveNode(this._dataGrid.rootNode(),e.current,t),this._saveCallback&&this._dataGrid.addCreationNode(!1)}_populateNode(e,t,i,o){if(e.removeChildren(),t){this._sortCookies(t);for(let a=0;a<t.length;++a){const s=t[a],r=this._createGridNode(s);e.appendChild(r),this._isSameCookie(s,i)&&(r.select(),null!==o&&this._dataGrid.startEditingNextEditableColumnOfDataGridNode(r,o))}}}_addInactiveNode(e,t,i){const o=this._createGridNode(t);e.appendChild(o),o.select(),o.setInactive(!0),null!==i&&this._dataGrid.startEditingNextEditableColumnOfDataGridNode(o,i)}_totalSize(e){let t=0;for(let i=0;e&&i<e.length;++i)t+=e[i].size();return t}_sortCookies(e){const t=this._dataGrid.isSortOrderAscending()?1:-1;function i(e,t){return"function"==typeof e[t]?String(e[t]()):String(e.name())}let o;const a=this._dataGrid.sortColumnId()||"name";o="expires"===a?function(e,i){return e.session()!==i.session()?t*(e.session()?1:-1):e.session()?0:e.maxAge()&&i.maxAge()?t*(e.maxAge()-i.maxAge()):e.expires()&&i.expires()?t*(e.expires()-i.expires()):t*(e.expires()?1:-1)}:"size"===a?function(e,i){return t*(e.size()-i.size())}:"priority"===a?function(e,i){const o=[Protocol.Network.CookiePriority.Low,Protocol.Network.CookiePriority.Medium,Protocol.Network.CookiePriority.High],a=o.indexOf(e.priority()),s=o.indexOf(i.priority());return t*(a-s)}:function(e,o,a){return t*i(o,e).compareTo(i(a,e))}.bind(null,a),e.sort(o)}_createGridNode(e){const t={};t[SDK.Cookie.Attributes.Name]=e.name(),t[SDK.Cookie.Attributes.Value]=e.value(),e.type()===SDK.Cookie.Type.Request?(t[SDK.Cookie.Attributes.Domain]=e.domain()?e.domain():ls`N/A`,t[SDK.Cookie.Attributes.Path]=e.path()?e.path():ls`N/A`):(t[SDK.Cookie.Attributes.Domain]=e.domain()||"",t[SDK.Cookie.Attributes.Path]=e.path()||""),e.maxAge()?t[SDK.Cookie.Attributes.Expires]=Number.secondsToString(parseInt(e.maxAge(),10)):e.expires()?e.expires()<0?t[SDK.Cookie.Attributes.Expires]=expiresSessionValue:t[SDK.Cookie.Attributes.Expires]=new Date(e.expires()).toISOString():t[SDK.Cookie.Attributes.Expires]=e.type()===SDK.Cookie.Type.Request?ls`N/A`:expiresSessionValue,t[SDK.Cookie.Attributes.Size]=e.size(),t[SDK.Cookie.Attributes.HttpOnly]=e.httpOnly(),t[SDK.Cookie.Attributes.Secure]=e.secure(),t[SDK.Cookie.Attributes.SameSite]=e.sameSite()||"",t[SDK.Cookie.Attributes.Priority]=e.priority()||"";const i=new DataGridNode(t,e,this._cookieToBlockedReasons?this._cookieToBlockedReasons.get(e):null);return i.selectable=!0,i}_onDeleteCookie(e){e.cookie&&this._deleteCallback&&this._deleteCallback(e.cookie,()=>this._refresh())}_onUpdateCookie(e,t,i,o){this._lastEditedColumnId=t,this._setDefaults(e),this._isValidCookieData(e.data)?this._saveNode(e):e.setDirty(!0)}_setDefaults(e){null===e.data[SDK.Cookie.Attributes.Name]&&(e.data[SDK.Cookie.Attributes.Name]=""),null===e.data[SDK.Cookie.Attributes.Value]&&(e.data[SDK.Cookie.Attributes.Value]=""),null===e.data[SDK.Cookie.Attributes.Domain]&&(e.data[SDK.Cookie.Attributes.Domain]=this._cookieDomain),null===e.data[SDK.Cookie.Attributes.Path]&&(e.data[SDK.Cookie.Attributes.Path]="/"),null===e.data[SDK.Cookie.Attributes.Expires]&&(e.data[SDK.Cookie.Attributes.Expires]=expiresSessionValue)}_saveNode(e){const t=e.cookie,i=this._createCookieFromData(e.data);e.cookie=i,this._saveCallback(i,t).then(t=>{t?this._refresh():e.setDirty(!0)})}_createCookieFromData(e){const t=new SDK.Cookie.Cookie(e[SDK.Cookie.Attributes.Name],e[SDK.Cookie.Attributes.Value],null,e[SDK.Cookie.Attributes.Priority]);return t.addAttribute(SDK.Cookie.Attributes.Domain,e[SDK.Cookie.Attributes.Domain]),t.addAttribute(SDK.Cookie.Attributes.Path,e[SDK.Cookie.Attributes.Path]),e.expires&&e.expires!==expiresSessionValue&&t.addAttribute(SDK.Cookie.Attributes.Expires,new Date(e[SDK.Cookie.Attributes.Expires]).toUTCString()),e[SDK.Cookie.Attributes.HttpOnly]&&t.addAttribute(SDK.Cookie.Attributes.HttpOnly),e[SDK.Cookie.Attributes.Secure]&&t.addAttribute(SDK.Cookie.Attributes.Secure),e[SDK.Cookie.Attributes.SameSite]&&t.addAttribute(SDK.Cookie.Attributes.SameSite,e[SDK.Cookie.Attributes.SameSite]),t.setSize(e[SDK.Cookie.Attributes.Name].length+e[SDK.Cookie.Attributes.Value].length),t}_isValidCookieData(e){return(e.name||e.value)&&this._isValidDomain(e.domain)&&this._isValidPath(e.path)&&this._isValidDate(e.expires)}_isValidDomain(e){if(!e)return!0;const t=Common.ParsedURL.ParsedURL.fromString("http://"+e);return!!t&&t.domain()===e}_isValidPath(e){const t=Common.ParsedURL.ParsedURL.fromString("http://example.com"+e);return!!t&&t.path===e}_isValidDate(e){return""===e||e===expiresSessionValue||!isNaN(Date.parse(e))}_refresh(){this._refreshCallback&&this._refreshCallback()}_populateContextMenu(e,t){const i=t.cookie;e.revealSection().appendItem(ls`Show Requests With This Cookie`,()=>{Network.NetworkPanel.NetworkPanel.revealAndFilter([{filterType:"cookie-domain",filterValue:i.domain()},{filterType:"cookie-name",filterValue:i.name()}])})}}export class DataGridNode extends DataGrid.DataGrid.DataGridNode{constructor(e,t,i){super(e),this.cookie=t,this._blockedReasons=i}createCells(e){super.createCells(e),this._blockedReasons&&this._blockedReasons.length&&e.classList.add("flagged-cookie-attribute-row")}createCell(e){const t=super.createCell(e);t.title=t.textContent;let i="";if(this._blockedReasons)for(const t of this._blockedReasons){const o=t.attribute===e,a=!t.attribute&&e===SDK.Cookie.Attributes.Name;(o||a)&&(i&&(i+="\n"),i+=t.uiString)}if(i){const e=UI.Icon.Icon.create("smallicon-info","cookie-warning-icon");e.title=i,t.insertBefore(e,t.firstChild),t.classList.add("flagged-cookie-attribute-cell")}return t}}