blob: 5660b83f0f70d6cd7cb2dd8d363b04a0a69e7c77 [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371// Copyright 2017 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Tim van der Lippe0efccf02020-02-12 15:15:395import * as Common from '../common/common.js'; // eslint-disable-line no-unused-vars
6import * as SDK from '../sdk/sdk.js';
7import * as SourceFrame from '../source_frame/source_frame.js';
8import * as UI from '../ui/ui.js';
9
Wolfgang Beyerc10beeb2020-10-13 14:46:5610import {ApplicationPanelSidebar, CookieTreeElement, StorageCategoryView} from './ApplicationPanelSidebar.js'; // eslint-disable-line no-unused-vars
Paul Lewisddf58342020-01-15 14:18:0711import {CookieItemsView} from './CookieItemsView.js';
12import {DatabaseQueryView} from './DatabaseQueryView.js';
13import {DatabaseTableView} from './DatabaseTableView.js';
14import {DOMStorageItemsView} from './DOMStorageItemsView.js';
15import {DOMStorage} from './DOMStorageModel.js'; // eslint-disable-line no-unused-vars
16import {StorageItemsView} from './StorageItemsView.js';
17
Tim van der Lipped87271d2020-09-28 15:17:0618/** @type {!ResourcesPanel} */
19let resourcesPanelInstance;
20
Tim van der Lippe0efccf02020-02-12 15:15:3921export class ResourcesPanel extends UI.Panel.PanelWithSidebar {
Tim van der Lipped87271d2020-09-28 15:17:0622 /**
23 * @private
24 */
Blink Reformat4c46d092018-04-07 15:32:3725 constructor() {
26 super('resources');
Jack Franklin71519f82020-11-03 12:08:5927 this.registerRequiredCSS('resources/resourcesPanel.css', {enableLegacyPatching: true});
Blink Reformat4c46d092018-04-07 15:32:3728
Paul Lewis2d7d65c2020-03-16 17:26:3029 this._resourcesLastSelectedItemSetting =
30 Common.Settings.Settings.instance().createSetting('resourcesLastSelectedElementPath', []);
Blink Reformat4c46d092018-04-07 15:32:3731
Tim van der Lippe0efccf02020-02-12 15:15:3932 /** @type {?UI.Widget.Widget} */
Blink Reformat4c46d092018-04-07 15:32:3733 this.visibleView = null;
34
Tim van der Lippe0efccf02020-02-12 15:15:3935 /** @type {?Promise<!UI.Widget.Widget>} */
Blink Reformat4c46d092018-04-07 15:32:3736 this._pendingViewPromise = null;
37
Paul Lewisddf58342020-01-15 14:18:0738 /** @type {?StorageCategoryView} */
Blink Reformat4c46d092018-04-07 15:32:3739 this._categoryView = null;
40
Tim van der Lippe0efccf02020-02-12 15:15:3941 const mainContainer = new UI.Widget.VBox();
Blink Reformat4c46d092018-04-07 15:32:3742 this.storageViews = mainContainer.element.createChild('div', 'vbox flex-auto');
Tim van der Lippe0efccf02020-02-12 15:15:3943 this._storageViewToolbar = new UI.Toolbar.Toolbar('resources-toolbar', mainContainer.element);
Blink Reformat4c46d092018-04-07 15:32:3744 this.splitWidget().setMainWidget(mainContainer);
45
Paul Lewisddf58342020-01-15 14:18:0746 /** @type {?DOMStorageItemsView} */
Blink Reformat4c46d092018-04-07 15:32:3747 this._domStorageView = null;
48
Paul Lewisddf58342020-01-15 14:18:0749 /** @type {?CookieItemsView} */
Blink Reformat4c46d092018-04-07 15:32:3750 this._cookieView = null;
51
Tim van der Lippe0efccf02020-02-12 15:15:3952 /** @type {?UI.EmptyWidget.EmptyWidget} */
Blink Reformat4c46d092018-04-07 15:32:3753 this._emptyWidget = null;
54
Paul Lewisddf58342020-01-15 14:18:0755 this._sidebar = new ApplicationPanelSidebar(this);
Blink Reformat4c46d092018-04-07 15:32:3756 this._sidebar.show(this.panelSidebarElement());
57 }
58
59 /**
Tim van der Lipped87271d2020-09-28 15:17:0660 * @param {{forceNew: ?boolean}} opts
61 */
62 static instance(opts = {forceNew: null}) {
63 const {forceNew} = opts;
64 if (!resourcesPanelInstance || forceNew) {
65 resourcesPanelInstance = new ResourcesPanel();
66 }
67
68 return resourcesPanelInstance;
69 }
70
71 /**
Tim van der Lippe097cdec2020-01-06 14:44:1772 * @return {!ResourcesPanel}
Blink Reformat4c46d092018-04-07 15:32:3773 */
74 static _instance() {
Tim van der Lipped87271d2020-09-28 15:17:0675 return ResourcesPanel.instance();
Blink Reformat4c46d092018-04-07 15:32:3776 }
77
78 /**
Tim van der Lippe0efccf02020-02-12 15:15:3979 * @param {!UI.Widget.Widget} view
Blink Reformat4c46d092018-04-07 15:32:3780 * @return {boolean}
81 */
82 static _shouldCloseOnReset(view) {
83 const viewClassesToClose = [
Tim van der Lippe0efccf02020-02-12 15:15:3984 SourceFrame.ResourceSourceFrame.ResourceSourceFrame, SourceFrame.ImageView.ImageView,
85 SourceFrame.FontView.FontView, StorageItemsView, DatabaseQueryView, DatabaseTableView
Blink Reformat4c46d092018-04-07 15:32:3786 ];
87 return viewClassesToClose.some(type => view instanceof type);
88 }
89
90 /**
91 * @override
92 */
93 focus() {
94 this._sidebar.focus();
95 }
96
97 /**
98 * @return {!Array<string>}
99 */
100 lastSelectedItemPath() {
101 return this._resourcesLastSelectedItemSetting.get();
102 }
103
104 /**
105 * @param {!Array<string>} path
106 */
107 setLastSelectedItemPath(path) {
108 this._resourcesLastSelectedItemSetting.set(path);
109 }
110
111 resetView() {
Tim van der Lippe097cdec2020-01-06 14:44:17112 if (this.visibleView && ResourcesPanel._shouldCloseOnReset(this.visibleView)) {
Blink Reformat4c46d092018-04-07 15:32:37113 this.showView(null);
Tim van der Lippe1d6e57a2019-09-30 11:55:34114 }
Blink Reformat4c46d092018-04-07 15:32:37115 }
116
117 /**
Tim van der Lippe0efccf02020-02-12 15:15:39118 * @param {?UI.Widget.Widget} view
Blink Reformat4c46d092018-04-07 15:32:37119 */
120 showView(view) {
121 this._pendingViewPromise = null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34122 if (this.visibleView === view) {
Blink Reformat4c46d092018-04-07 15:32:37123 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34124 }
Blink Reformat4c46d092018-04-07 15:32:37125
Tim van der Lippe1d6e57a2019-09-30 11:55:34126 if (this.visibleView) {
Blink Reformat4c46d092018-04-07 15:32:37127 this.visibleView.detach();
Tim van der Lippe1d6e57a2019-09-30 11:55:34128 }
Blink Reformat4c46d092018-04-07 15:32:37129
Tim van der Lippe1d6e57a2019-09-30 11:55:34130 if (view) {
Blink Reformat4c46d092018-04-07 15:32:37131 view.show(this.storageViews);
Tim van der Lippe1d6e57a2019-09-30 11:55:34132 }
Blink Reformat4c46d092018-04-07 15:32:37133 this.visibleView = view;
134
135 this._storageViewToolbar.removeToolbarItems();
Tim van der Lippe0efccf02020-02-12 15:15:39136 if (view instanceof UI.View.SimpleView) {
Simon Zünd308c74f2020-01-16 06:53:37137 view.toolbarItems().then(items => {
138 items.map(item => this._storageViewToolbar.appendToolbarItem(item));
139 this._storageViewToolbar.element.classList.toggle('hidden', !items.length);
140 });
Tim van der Lippe1d6e57a2019-09-30 11:55:34141 }
Blink Reformat4c46d092018-04-07 15:32:37142 }
143
144 /**
Tim van der Lippe0efccf02020-02-12 15:15:39145 * @param {!Promise<!UI.Widget.Widget>} viewPromise
146 * @return {!Promise<?UI.Widget.Widget>}
Blink Reformat4c46d092018-04-07 15:32:37147 */
148 async scheduleShowView(viewPromise) {
149 this._pendingViewPromise = viewPromise;
150 const view = await viewPromise;
Tim van der Lippe1d6e57a2019-09-30 11:55:34151 if (this._pendingViewPromise !== viewPromise) {
Blink Reformat4c46d092018-04-07 15:32:37152 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34153 }
Blink Reformat4c46d092018-04-07 15:32:37154 this.showView(view);
155 return view;
156 }
157
158 /**
159 * @param {string} categoryName
Kayce Basques1ec0c932019-04-30 18:38:53160 * @param {string|null} categoryLink
Blink Reformat4c46d092018-04-07 15:32:37161 */
Kayce Basques1ec0c932019-04-30 18:38:53162 showCategoryView(categoryName, categoryLink) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34163 if (!this._categoryView) {
Paul Lewisddf58342020-01-15 14:18:07164 this._categoryView = new StorageCategoryView();
Tim van der Lippe1d6e57a2019-09-30 11:55:34165 }
Blink Reformat4c46d092018-04-07 15:32:37166 this._categoryView.setText(categoryName);
Kayce Basques1ec0c932019-04-30 18:38:53167 this._categoryView.setLink(categoryLink);
Blink Reformat4c46d092018-04-07 15:32:37168 this.showView(this._categoryView);
169 }
170
171 /**
Paul Lewisddf58342020-01-15 14:18:07172 * @param {!DOMStorage} domStorage
Blink Reformat4c46d092018-04-07 15:32:37173 */
174 showDOMStorage(domStorage) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34175 if (!domStorage) {
Blink Reformat4c46d092018-04-07 15:32:37176 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34177 }
Blink Reformat4c46d092018-04-07 15:32:37178
Tim van der Lippe1d6e57a2019-09-30 11:55:34179 if (!this._domStorageView) {
Paul Lewisddf58342020-01-15 14:18:07180 this._domStorageView = new DOMStorageItemsView(domStorage);
Tim van der Lippe1d6e57a2019-09-30 11:55:34181 } else {
Blink Reformat4c46d092018-04-07 15:32:37182 this._domStorageView.setStorage(domStorage);
Tim van der Lippe1d6e57a2019-09-30 11:55:34183 }
Blink Reformat4c46d092018-04-07 15:32:37184 this.showView(this._domStorageView);
185 }
186
187 /**
Tim van der Lippe0efccf02020-02-12 15:15:39188 * @param {!SDK.SDKModel.Target} cookieFrameTarget
Blink Reformat4c46d092018-04-07 15:32:37189 * @param {string} cookieDomain
190 */
191 showCookies(cookieFrameTarget, cookieDomain) {
Tim van der Lippe0efccf02020-02-12 15:15:39192 const model = cookieFrameTarget.model(SDK.CookieModel.CookieModel);
Tim van der Lippe1d6e57a2019-09-30 11:55:34193 if (!model) {
Blink Reformat4c46d092018-04-07 15:32:37194 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34195 }
196 if (!this._cookieView) {
Paul Lewisddf58342020-01-15 14:18:07197 this._cookieView = new CookieItemsView(model, cookieDomain);
Tim van der Lippe1d6e57a2019-09-30 11:55:34198 } else {
Blink Reformat4c46d092018-04-07 15:32:37199 this._cookieView.setCookiesDomain(model, cookieDomain);
Tim van der Lippe1d6e57a2019-09-30 11:55:34200 }
Blink Reformat4c46d092018-04-07 15:32:37201 this.showView(this._cookieView);
202 }
203
204 /**
Tim van der Lippe0efccf02020-02-12 15:15:39205 * @param {!SDK.SDKModel.Target} target
Blink Reformat4c46d092018-04-07 15:32:37206 * @param {string} cookieDomain
207 */
208 clearCookies(target, cookieDomain) {
Sigurd Schneider578a7ff2020-05-15 08:39:09209 const model = /** @type {?SDK.CookieModel.CookieModel} */ (target.model(SDK.CookieModel.CookieModel));
Tim van der Lippe1d6e57a2019-09-30 11:55:34210 if (!model) {
Blink Reformat4c46d092018-04-07 15:32:37211 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34212 }
Sigurd Schneider578a7ff2020-05-15 08:39:09213 model.clear(cookieDomain).then(() => {
Tim van der Lippe1d6e57a2019-09-30 11:55:34214 if (this._cookieView) {
Blink Reformat4c46d092018-04-07 15:32:37215 this._cookieView.refreshItems();
Tim van der Lippe1d6e57a2019-09-30 11:55:34216 }
Blink Reformat4c46d092018-04-07 15:32:37217 });
218 }
Tim van der Lippe097cdec2020-01-06 14:44:17219}
Blink Reformat4c46d092018-04-07 15:32:37220
221/**
Tim van der Lippe0efccf02020-02-12 15:15:39222 * @implements {Common.Revealer.Revealer}
Blink Reformat4c46d092018-04-07 15:32:37223 */
Tim van der Lippe097cdec2020-01-06 14:44:17224export class ResourceRevealer {
Blink Reformat4c46d092018-04-07 15:32:37225 /**
226 * @override
227 * @param {!Object} resource
Wolfgang Beyerc10beeb2020-10-13 14:46:56228 * @return {!Promise<void>}
Blink Reformat4c46d092018-04-07 15:32:37229 */
230 async reveal(resource) {
Tim van der Lippe0efccf02020-02-12 15:15:39231 if (!(resource instanceof SDK.Resource.Resource)) {
Blink Reformat4c46d092018-04-07 15:32:37232 return Promise.reject(new Error('Internal error: not a resource'));
Tim van der Lippe1d6e57a2019-09-30 11:55:34233 }
Tim van der Lippe097cdec2020-01-06 14:44:17234 const sidebar = ResourcesPanel._instance()._sidebar;
Paul Lewis75c7d0d2020-03-19 12:17:26235 await UI.ViewManager.ViewManager.instance().showView('resources');
Blink Reformat4c46d092018-04-07 15:32:37236 await sidebar.showResource(resource);
237 }
Tim van der Lippe097cdec2020-01-06 14:44:17238}
Sigurd Schneider293aa362020-03-11 13:41:58239
240/**
241 * @implements {Common.Revealer.Revealer}
242 */
243export class CookieReferenceRevealer {
244 /**
245 * @override
246 * @param {!Object} cookie
Wolfgang Beyerc10beeb2020-10-13 14:46:56247 * @return {!Promise<void>}
Sigurd Schneider293aa362020-03-11 13:41:58248 */
249 async reveal(cookie) {
250 if (!(cookie instanceof SDK.Cookie.CookieReference)) {
251 throw new Error('Internal error: not a cookie reference');
252 }
253
254 const sidebar = ResourcesPanel._instance()._sidebar;
Paul Lewis75c7d0d2020-03-19 12:17:26255 await UI.ViewManager.ViewManager.instance().showView('resources');
Sigurd Schneider293aa362020-03-11 13:41:58256 await sidebar.cookieListTreeElement.select();
257
258 const contextUrl = cookie.contextUrl();
259 if (contextUrl && await this._revealByDomain(sidebar, contextUrl)) {
260 return;
261 }
262 // Fallback: try to reveal the cookie using its domain as context, which may not work, because the
263 // Application Panel shows cookies grouped by context, see crbug.com/1060563.
264 this._revealByDomain(sidebar, cookie.domain());
265 }
266
267 /**
268 * @param {!ApplicationPanelSidebar} sidebar
269 * @param {string} domain
270 * @returns {!Promise<boolean>}
271 */
272 async _revealByDomain(sidebar, domain) {
Wolfgang Beyerc10beeb2020-10-13 14:46:56273 const item = sidebar.cookieListTreeElement.children().find(
274 c => /** @type {!CookieTreeElement} */ (c).cookieDomain().endsWith(domain));
Sigurd Schneider293aa362020-03-11 13:41:58275 if (item) {
276 await item.revealAndSelect();
277 return true;
278 }
279 return false;
280 }
281}