Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | // 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 Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 5 | import * as Common from '../common/common.js'; // eslint-disable-line no-unused-vars |
| 6 | import * as SDK from '../sdk/sdk.js'; |
| 7 | import * as SourceFrame from '../source_frame/source_frame.js'; |
| 8 | import * as UI from '../ui/ui.js'; |
| 9 | |
Wolfgang Beyer | c10beeb | 2020-10-13 14:46:56 | [diff] [blame] | 10 | import {ApplicationPanelSidebar, CookieTreeElement, StorageCategoryView} from './ApplicationPanelSidebar.js'; // eslint-disable-line no-unused-vars |
Paul Lewis | ddf5834 | 2020-01-15 14:18:07 | [diff] [blame] | 11 | import {CookieItemsView} from './CookieItemsView.js'; |
| 12 | import {DatabaseQueryView} from './DatabaseQueryView.js'; |
| 13 | import {DatabaseTableView} from './DatabaseTableView.js'; |
| 14 | import {DOMStorageItemsView} from './DOMStorageItemsView.js'; |
| 15 | import {DOMStorage} from './DOMStorageModel.js'; // eslint-disable-line no-unused-vars |
| 16 | import {StorageItemsView} from './StorageItemsView.js'; |
| 17 | |
Tim van der Lippe | d87271d | 2020-09-28 15:17:06 | [diff] [blame] | 18 | /** @type {!ResourcesPanel} */ |
| 19 | let resourcesPanelInstance; |
| 20 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 21 | export class ResourcesPanel extends UI.Panel.PanelWithSidebar { |
Tim van der Lippe | d87271d | 2020-09-28 15:17:06 | [diff] [blame] | 22 | /** |
| 23 | * @private |
| 24 | */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 25 | constructor() { |
| 26 | super('resources'); |
Jack Franklin | 71519f8 | 2020-11-03 12:08:59 | [diff] [blame^] | 27 | this.registerRequiredCSS('resources/resourcesPanel.css', {enableLegacyPatching: true}); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 28 | |
Paul Lewis | 2d7d65c | 2020-03-16 17:26:30 | [diff] [blame] | 29 | this._resourcesLastSelectedItemSetting = |
| 30 | Common.Settings.Settings.instance().createSetting('resourcesLastSelectedElementPath', []); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 31 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 32 | /** @type {?UI.Widget.Widget} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 33 | this.visibleView = null; |
| 34 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 35 | /** @type {?Promise<!UI.Widget.Widget>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 36 | this._pendingViewPromise = null; |
| 37 | |
Paul Lewis | ddf5834 | 2020-01-15 14:18:07 | [diff] [blame] | 38 | /** @type {?StorageCategoryView} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 39 | this._categoryView = null; |
| 40 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 41 | const mainContainer = new UI.Widget.VBox(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 42 | this.storageViews = mainContainer.element.createChild('div', 'vbox flex-auto'); |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 43 | this._storageViewToolbar = new UI.Toolbar.Toolbar('resources-toolbar', mainContainer.element); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 44 | this.splitWidget().setMainWidget(mainContainer); |
| 45 | |
Paul Lewis | ddf5834 | 2020-01-15 14:18:07 | [diff] [blame] | 46 | /** @type {?DOMStorageItemsView} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 47 | this._domStorageView = null; |
| 48 | |
Paul Lewis | ddf5834 | 2020-01-15 14:18:07 | [diff] [blame] | 49 | /** @type {?CookieItemsView} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 50 | this._cookieView = null; |
| 51 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 52 | /** @type {?UI.EmptyWidget.EmptyWidget} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 53 | this._emptyWidget = null; |
| 54 | |
Paul Lewis | ddf5834 | 2020-01-15 14:18:07 | [diff] [blame] | 55 | this._sidebar = new ApplicationPanelSidebar(this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 56 | this._sidebar.show(this.panelSidebarElement()); |
| 57 | } |
| 58 | |
| 59 | /** |
Tim van der Lippe | d87271d | 2020-09-28 15:17:06 | [diff] [blame] | 60 | * @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 Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 72 | * @return {!ResourcesPanel} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 73 | */ |
| 74 | static _instance() { |
Tim van der Lippe | d87271d | 2020-09-28 15:17:06 | [diff] [blame] | 75 | return ResourcesPanel.instance(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 79 | * @param {!UI.Widget.Widget} view |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 80 | * @return {boolean} |
| 81 | */ |
| 82 | static _shouldCloseOnReset(view) { |
| 83 | const viewClassesToClose = [ |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 84 | SourceFrame.ResourceSourceFrame.ResourceSourceFrame, SourceFrame.ImageView.ImageView, |
| 85 | SourceFrame.FontView.FontView, StorageItemsView, DatabaseQueryView, DatabaseTableView |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 86 | ]; |
| 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 Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 112 | if (this.visibleView && ResourcesPanel._shouldCloseOnReset(this.visibleView)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 113 | this.showView(null); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 114 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 118 | * @param {?UI.Widget.Widget} view |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 119 | */ |
| 120 | showView(view) { |
| 121 | this._pendingViewPromise = null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 122 | if (this.visibleView === view) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 123 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 124 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 125 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 126 | if (this.visibleView) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 127 | this.visibleView.detach(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 128 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 129 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 130 | if (view) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 131 | view.show(this.storageViews); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 132 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 133 | this.visibleView = view; |
| 134 | |
| 135 | this._storageViewToolbar.removeToolbarItems(); |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 136 | if (view instanceof UI.View.SimpleView) { |
Simon Zünd | 308c74f | 2020-01-16 06:53:37 | [diff] [blame] | 137 | 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 Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 141 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 145 | * @param {!Promise<!UI.Widget.Widget>} viewPromise |
| 146 | * @return {!Promise<?UI.Widget.Widget>} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 147 | */ |
| 148 | async scheduleShowView(viewPromise) { |
| 149 | this._pendingViewPromise = viewPromise; |
| 150 | const view = await viewPromise; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 151 | if (this._pendingViewPromise !== viewPromise) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 152 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 153 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 154 | this.showView(view); |
| 155 | return view; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @param {string} categoryName |
Kayce Basques | 1ec0c93 | 2019-04-30 18:38:53 | [diff] [blame] | 160 | * @param {string|null} categoryLink |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 161 | */ |
Kayce Basques | 1ec0c93 | 2019-04-30 18:38:53 | [diff] [blame] | 162 | showCategoryView(categoryName, categoryLink) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 163 | if (!this._categoryView) { |
Paul Lewis | ddf5834 | 2020-01-15 14:18:07 | [diff] [blame] | 164 | this._categoryView = new StorageCategoryView(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 165 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 166 | this._categoryView.setText(categoryName); |
Kayce Basques | 1ec0c93 | 2019-04-30 18:38:53 | [diff] [blame] | 167 | this._categoryView.setLink(categoryLink); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 168 | this.showView(this._categoryView); |
| 169 | } |
| 170 | |
| 171 | /** |
Paul Lewis | ddf5834 | 2020-01-15 14:18:07 | [diff] [blame] | 172 | * @param {!DOMStorage} domStorage |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 173 | */ |
| 174 | showDOMStorage(domStorage) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 175 | if (!domStorage) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 176 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 177 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 178 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 179 | if (!this._domStorageView) { |
Paul Lewis | ddf5834 | 2020-01-15 14:18:07 | [diff] [blame] | 180 | this._domStorageView = new DOMStorageItemsView(domStorage); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 181 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 182 | this._domStorageView.setStorage(domStorage); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 183 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 184 | this.showView(this._domStorageView); |
| 185 | } |
| 186 | |
| 187 | /** |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 188 | * @param {!SDK.SDKModel.Target} cookieFrameTarget |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 189 | * @param {string} cookieDomain |
| 190 | */ |
| 191 | showCookies(cookieFrameTarget, cookieDomain) { |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 192 | const model = cookieFrameTarget.model(SDK.CookieModel.CookieModel); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 193 | if (!model) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 194 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 195 | } |
| 196 | if (!this._cookieView) { |
Paul Lewis | ddf5834 | 2020-01-15 14:18:07 | [diff] [blame] | 197 | this._cookieView = new CookieItemsView(model, cookieDomain); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 198 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 199 | this._cookieView.setCookiesDomain(model, cookieDomain); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 200 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 201 | this.showView(this._cookieView); |
| 202 | } |
| 203 | |
| 204 | /** |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 205 | * @param {!SDK.SDKModel.Target} target |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 206 | * @param {string} cookieDomain |
| 207 | */ |
| 208 | clearCookies(target, cookieDomain) { |
Sigurd Schneider | 578a7ff | 2020-05-15 08:39:09 | [diff] [blame] | 209 | const model = /** @type {?SDK.CookieModel.CookieModel} */ (target.model(SDK.CookieModel.CookieModel)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 210 | if (!model) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 211 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 212 | } |
Sigurd Schneider | 578a7ff | 2020-05-15 08:39:09 | [diff] [blame] | 213 | model.clear(cookieDomain).then(() => { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 214 | if (this._cookieView) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 215 | this._cookieView.refreshItems(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 216 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 217 | }); |
| 218 | } |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 219 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 220 | |
| 221 | /** |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 222 | * @implements {Common.Revealer.Revealer} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 223 | */ |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 224 | export class ResourceRevealer { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 225 | /** |
| 226 | * @override |
| 227 | * @param {!Object} resource |
Wolfgang Beyer | c10beeb | 2020-10-13 14:46:56 | [diff] [blame] | 228 | * @return {!Promise<void>} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 229 | */ |
| 230 | async reveal(resource) { |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 231 | if (!(resource instanceof SDK.Resource.Resource)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 232 | return Promise.reject(new Error('Internal error: not a resource')); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 233 | } |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 234 | const sidebar = ResourcesPanel._instance()._sidebar; |
Paul Lewis | 75c7d0d | 2020-03-19 12:17:26 | [diff] [blame] | 235 | await UI.ViewManager.ViewManager.instance().showView('resources'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 236 | await sidebar.showResource(resource); |
| 237 | } |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 238 | } |
Sigurd Schneider | 293aa36 | 2020-03-11 13:41:58 | [diff] [blame] | 239 | |
| 240 | /** |
| 241 | * @implements {Common.Revealer.Revealer} |
| 242 | */ |
| 243 | export class CookieReferenceRevealer { |
| 244 | /** |
| 245 | * @override |
| 246 | * @param {!Object} cookie |
Wolfgang Beyer | c10beeb | 2020-10-13 14:46:56 | [diff] [blame] | 247 | * @return {!Promise<void>} |
Sigurd Schneider | 293aa36 | 2020-03-11 13:41:58 | [diff] [blame] | 248 | */ |
| 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 Lewis | 75c7d0d | 2020-03-19 12:17:26 | [diff] [blame] | 255 | await UI.ViewManager.ViewManager.instance().showView('resources'); |
Sigurd Schneider | 293aa36 | 2020-03-11 13:41:58 | [diff] [blame] | 256 | 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 Beyer | c10beeb | 2020-10-13 14:46:56 | [diff] [blame] | 273 | const item = sidebar.cookieListTreeElement.children().find( |
| 274 | c => /** @type {!CookieTreeElement} */ (c).cookieDomain().endsWith(domain)); |
Sigurd Schneider | 293aa36 | 2020-03-11 13:41:58 | [diff] [blame] | 275 | if (item) { |
| 276 | await item.revealAndSelect(); |
| 277 | return true; |
| 278 | } |
| 279 | return false; |
| 280 | } |
| 281 | } |