Rayan Kanso | 6890420 | 2019-02-21 14:16:25 | [diff] [blame] | 1 | // Copyright 2019 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 | 877f04a | 2021-07-23 11:02:54 | [diff] [blame] | 5 | import type * as Common from '../../core/common/common.js'; |
Tim van der Lippe | bb352e6 | 2021-04-01 17:57:28 | [diff] [blame] | 6 | import * as i18n from '../../core/i18n/i18n.js'; |
Tim van der Lippe | aa1ed7a | 2021-03-31 14:38:27 | [diff] [blame] | 7 | import * as Platform from '../../core/platform/platform.js'; |
Tim van der Lippe | e00b92f | 2021-03-31 16:52:17 | [diff] [blame] | 8 | import * as SDK from '../../core/sdk/sdk.js'; |
Kateryna Prokopenko | fbb2247 | 2022-05-10 15:36:34 | [diff] [blame] | 9 | import * as Protocol from '../../generated/protocol.js'; |
Tim van der Lippe | 959b6f0 | 2021-04-07 09:07:59 | [diff] [blame] | 10 | import * as Bindings from '../../models/bindings/bindings.js'; |
Tim van der Lippe | 8499fe2 | 2021-04-12 16:42:47 | [diff] [blame] | 11 | import * as DataGrid from '../../ui/legacy/components/data_grid/data_grid.js'; |
Kateryna Prokopenko | fbb2247 | 2022-05-10 15:36:34 | [diff] [blame] | 12 | // eslint-disable-next-line rulesdir/es_modules_import |
| 13 | import emptyWidgetStyles from '../../ui/legacy/emptyWidget.css.js'; |
Tim van der Lippe | aa61faf | 2021-04-07 15:32:07 | [diff] [blame] | 14 | import * as UI from '../../ui/legacy/legacy.js'; |
Kateryna Prokopenko | fbb2247 | 2022-05-10 15:36:34 | [diff] [blame] | 15 | |
| 16 | import backgroundServiceViewStyles from './backgroundServiceView.css.js'; |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 17 | |
Jack Franklin | 3a80260 | 2022-07-13 08:39:42 | [diff] [blame] | 18 | import {Events, type BackgroundServiceModel} from './BackgroundServiceModel.js'; |
Paul Lewis | ddf5834 | 2020-01-15 14:18:07 | [diff] [blame] | 19 | |
Simon Zünd | 697fb0b | 2021-03-01 10:12:42 | [diff] [blame] | 20 | const UIStrings = { |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame] | 21 | /** |
| 22 | *@description Text in Background Service View of the Application panel |
| 23 | */ |
| 24 | backgroundFetch: 'Background Fetch', |
| 25 | /** |
| 26 | *@description Text in Background Service View of the Application panel |
| 27 | */ |
| 28 | backgroundSync: 'Background Sync', |
| 29 | /** |
| 30 | *@description Text in Background Service View of the Application panel |
| 31 | */ |
| 32 | pushMessaging: 'Push Messaging', |
| 33 | /** |
| 34 | *@description Text in Background Service View of the Application panel |
| 35 | */ |
| 36 | notifications: 'Notifications', |
| 37 | /** |
| 38 | *@description Text in Background Service View of the Application panel |
| 39 | */ |
| 40 | paymentHandler: 'Payment Handler', |
| 41 | /** |
| 42 | *@description Text in the Periodic Background Service View of the Application panel |
| 43 | */ |
| 44 | periodicBackgroundSync: 'Periodic Background Sync', |
| 45 | /** |
| 46 | *@description Text to clear content |
| 47 | */ |
| 48 | clear: 'Clear', |
| 49 | /** |
| 50 | *@description Tooltip text that appears when hovering over the largeicon download button in the Background Service View of the Application panel |
| 51 | */ |
| 52 | saveEvents: 'Save events', |
| 53 | /** |
| 54 | *@description Text in Background Service View of the Application panel |
| 55 | */ |
| 56 | showEventsFromOtherDomains: 'Show events from other domains', |
| 57 | /** |
Kateryna Prokopenko | 35d6bf6 | 2022-11-30 13:56:19 | [diff] [blame^] | 58 | *@description Text of a checkbox to show events for other dtorage keys |
| 59 | */ |
| 60 | showEventsForOtherStorageKeys: 'Show events from other storage partitions', |
| 61 | /** |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame] | 62 | *@description Title of an action under the Background Services category that can be invoked through the Command Menu |
| 63 | */ |
| 64 | stopRecordingEvents: 'Stop recording events', |
| 65 | /** |
| 66 | *@description Title of an action under the Background Services category that can be invoked through the Command Menu |
| 67 | */ |
| 68 | startRecordingEvents: 'Start recording events', |
| 69 | /** |
| 70 | *@description Text for timestamps of items |
| 71 | */ |
| 72 | timestamp: 'Timestamp', |
| 73 | /** |
| 74 | *@description Text that refers to some events |
| 75 | */ |
| 76 | event: 'Event', |
| 77 | /** |
| 78 | *@description Text for the origin of something |
| 79 | */ |
| 80 | origin: 'Origin', |
| 81 | /** |
Kateryna Prokopenko | 35d6bf6 | 2022-11-30 13:56:19 | [diff] [blame^] | 82 | *@description Text for the storage key of something |
| 83 | */ |
| 84 | storageKey: 'Storage Key', |
| 85 | /** |
Sigurd Schneider | bee57a8 | 2021-02-23 12:58:24 | [diff] [blame] | 86 | *@description Text in Background Service View of the Application panel. The Scope is a URL associated with the Service Worker, which limits which pages/sites the Service Worker operates on. |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame] | 87 | */ |
Sigurd Schneider | bee57a8 | 2021-02-23 12:58:24 | [diff] [blame] | 88 | swScope: 'Service Worker Scope', |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame] | 89 | /** |
| 90 | *@description Text in Background Service View of the Application panel |
| 91 | */ |
| 92 | instanceId: 'Instance ID', |
| 93 | /** |
| 94 | *@description Text in Application Panel Sidebar of the Application panel |
| 95 | */ |
| 96 | backgroundServices: 'Background Services', |
| 97 | /** |
| 98 | *@description Text that is usually a hyperlink to more documentation |
| 99 | */ |
| 100 | learnMore: 'Learn more', |
| 101 | /** |
| 102 | *@description Text in Background Service View of the Application panel |
| 103 | */ |
| 104 | selectAnEntryToViewMetadata: 'Select an entry to view metadata', |
| 105 | /** |
| 106 | *@description Text in Background Service View of the Application panel |
| 107 | *@example {Background Fetch} PH1 |
| 108 | */ |
| 109 | recordingSActivity: 'Recording {PH1} activity...', |
| 110 | /** |
| 111 | *@description Inform users that DevTools are recording/waiting for events in the Periodic Background Sync tool of the Application panel |
| 112 | *@example {Background Fetch} PH1 |
| 113 | */ |
| 114 | devtoolsWillRecordAllSActivity: 'DevTools will record all {PH1} activity for up to 3 days, even when closed.', |
| 115 | /** |
| 116 | *@description Text in Background Service View of the Application panel |
| 117 | *@example {record} PH1 |
| 118 | *@example {Ctrl + R} PH2 |
| 119 | */ |
| 120 | clickTheRecordButtonSOrHitSTo: 'Click the record button {PH1} or hit {PH2} to start recording.', |
| 121 | /** |
| 122 | *@description Text to show an item is empty |
| 123 | */ |
| 124 | empty: 'empty', |
| 125 | /** |
| 126 | *@description Text in Background Service View of the Application panel |
| 127 | */ |
| 128 | noMetadataForThisEvent: 'No metadata for this event', |
| 129 | }; |
Tim van der Lippe | c59708f | 2021-03-31 15:07:19 | [diff] [blame] | 130 | const str_ = i18n.i18n.registerUIStrings('panels/application/BackgroundServiceView.ts', UIStrings); |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame] | 131 | const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_); |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 132 | export class BackgroundServiceView extends UI.Widget.VBox { |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 133 | private readonly serviceName: Protocol.BackgroundService.ServiceName; |
| 134 | private readonly model: BackgroundServiceModel; |
| 135 | private readonly serviceWorkerManager: SDK.ServiceWorkerManager.ServiceWorkerManager|null; |
| 136 | private readonly securityOriginManager: SDK.SecurityOriginManager.SecurityOriginManager; |
Kateryna Prokopenko | 35d6bf6 | 2022-11-30 13:56:19 | [diff] [blame^] | 137 | private readonly storageKeyManager: SDK.StorageKeyManager.StorageKeyManager; |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 138 | private recordAction: UI.ActionRegistration.Action; |
| 139 | private recordButton!: UI.Toolbar.ToolbarToggle; |
| 140 | private originCheckbox!: UI.Toolbar.ToolbarCheckbox; |
Kateryna Prokopenko | 35d6bf6 | 2022-11-30 13:56:19 | [diff] [blame^] | 141 | private storageKeyCheckbox!: UI.Toolbar.ToolbarCheckbox; |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 142 | private saveButton!: UI.Toolbar.ToolbarButton; |
| 143 | private readonly toolbar: UI.Toolbar.Toolbar; |
| 144 | private readonly splitWidget: UI.SplitWidget.SplitWidget; |
| 145 | private readonly dataGrid: DataGrid.DataGrid.DataGridImpl<EventData>; |
| 146 | private readonly previewPanel: UI.Widget.VBox; |
| 147 | private selectedEventNode: EventDataNode|null; |
| 148 | private preview: UI.Widget.Widget|null; |
Jan Scheffler | 1026b9d | 2021-02-26 17:12:00 | [diff] [blame] | 149 | |
| 150 | static getUIString(serviceName: string): string { |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 151 | switch (serviceName) { |
| 152 | case Protocol.BackgroundService.ServiceName.BackgroundFetch: |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame] | 153 | return i18nString(UIStrings.backgroundFetch); |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 154 | case Protocol.BackgroundService.ServiceName.BackgroundSync: |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame] | 155 | return i18nString(UIStrings.backgroundSync); |
Fergus Dall | 0be4f4c | 2019-05-21 00:01:29 | [diff] [blame] | 156 | case Protocol.BackgroundService.ServiceName.PushMessaging: |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame] | 157 | return i18nString(UIStrings.pushMessaging); |
Fergus Dall | 0be4f4c | 2019-05-21 00:01:29 | [diff] [blame] | 158 | case Protocol.BackgroundService.ServiceName.Notifications: |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame] | 159 | return i18nString(UIStrings.notifications); |
Rayan Kanso | 8d7918a | 2019-07-03 21:03:38 | [diff] [blame] | 160 | case Protocol.BackgroundService.ServiceName.PaymentHandler: |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame] | 161 | return i18nString(UIStrings.paymentHandler); |
Rayan Kanso | 5480967 | 2019-07-24 18:40:28 | [diff] [blame] | 162 | case Protocol.BackgroundService.ServiceName.PeriodicBackgroundSync: |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame] | 163 | return i18nString(UIStrings.periodicBackgroundSync); |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 164 | default: |
| 165 | return ''; |
| 166 | } |
| 167 | } |
| 168 | |
Jan Scheffler | 1026b9d | 2021-02-26 17:12:00 | [diff] [blame] | 169 | constructor(serviceName: Protocol.BackgroundService.ServiceName, model: BackgroundServiceModel) { |
Rayan Kanso | 6890420 | 2019-02-21 14:16:25 | [diff] [blame] | 170 | super(true); |
Rayan Kanso | 6890420 | 2019-02-21 14:16:25 | [diff] [blame] | 171 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 172 | this.serviceName = serviceName; |
Rayan Kanso | 6890420 | 2019-02-21 14:16:25 | [diff] [blame] | 173 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 174 | this.model = model; |
| 175 | this.model.addEventListener(Events.RecordingStateChanged, this.onRecordingStateChanged, this); |
| 176 | this.model.addEventListener(Events.BackgroundServiceEventReceived, this.onEventReceived, this); |
| 177 | this.model.enable(this.serviceName); |
Rayan Kanso | 8fe8ee2 | 2019-03-04 14:58:46 | [diff] [blame] | 178 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 179 | this.serviceWorkerManager = this.model.target().model(SDK.ServiceWorkerManager.ServiceWorkerManager); |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 180 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 181 | this.securityOriginManager = this.model.target().model(SDK.SecurityOriginManager.SecurityOriginManager) as |
Jan Scheffler | 1026b9d | 2021-02-26 17:12:00 | [diff] [blame] | 182 | SDK.SecurityOriginManager.SecurityOriginManager; |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 183 | if (!this.securityOriginManager) { |
Alex Rudenko | 60a3e94 | 2020-11-02 12:27:57 | [diff] [blame] | 184 | throw new Error('SecurityOriginManager instance is missing'); |
| 185 | } |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 186 | this.securityOriginManager.addEventListener( |
| 187 | SDK.SecurityOriginManager.Events.MainSecurityOriginChanged, () => this.onOriginChanged()); |
Kateryna Prokopenko | 35d6bf6 | 2022-11-30 13:56:19 | [diff] [blame^] | 188 | |
| 189 | this.storageKeyManager = |
| 190 | this.model.target().model(SDK.StorageKeyManager.StorageKeyManager) as SDK.StorageKeyManager.StorageKeyManager; |
| 191 | if (!this.storageKeyManager) { |
| 192 | throw new Error('StorageKeyManager instance is missing'); |
| 193 | } |
| 194 | this.storageKeyManager.addEventListener( |
| 195 | SDK.StorageKeyManager.Events.MainStorageKeyChanged, () => this.onStorageKeyChanged()); |
| 196 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 197 | this.recordAction = |
Jan Scheffler | 1026b9d | 2021-02-26 17:12:00 | [diff] [blame] | 198 | (UI.ActionRegistry.ActionRegistry.instance().action('background-service.toggle-recording') as |
| 199 | UI.ActionRegistration.Action); |
Rayan Kanso | 8fe8ee2 | 2019-03-04 14:58:46 | [diff] [blame] | 200 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 201 | this.toolbar = new UI.Toolbar.Toolbar('background-service-toolbar', this.contentElement); |
Tim van der Lippe | 2d9a95c | 2022-01-04 15:18:03 | [diff] [blame] | 202 | void this.setupToolbar(); |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 203 | |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 204 | /** |
| 205 | * This will contain the DataGrid for displaying events, and a panel at the bottom for showing |
| 206 | * extra metadata related to the selected event. |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 207 | */ |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 208 | this.splitWidget = new UI.SplitWidget.SplitWidget(/* isVertical= */ false, /* secondIsSidebar= */ true); |
| 209 | this.splitWidget.show(this.contentElement); |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 210 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 211 | this.dataGrid = this.createDataGrid(); |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 212 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 213 | this.previewPanel = new UI.Widget.VBox(); |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 214 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 215 | this.selectedEventNode = null; |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 216 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 217 | this.preview = null; |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 218 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 219 | this.splitWidget.setMainWidget(this.dataGrid.asWidget()); |
| 220 | this.splitWidget.setSidebarWidget(this.previewPanel); |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 221 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 222 | this.showPreview(null); |
Rayan Kanso | 6890420 | 2019-02-21 14:16:25 | [diff] [blame] | 223 | } |
| 224 | |
Kateryna Prokopenko | 35d6bf6 | 2022-11-30 13:56:19 | [diff] [blame^] | 225 | getDataGrid(): DataGrid.DataGrid.DataGridImpl<EventData> { |
| 226 | return this.dataGrid; |
| 227 | } |
| 228 | |
Rayan Kanso | 6890420 | 2019-02-21 14:16:25 | [diff] [blame] | 229 | /** |
| 230 | * Creates the toolbar UI element. |
| 231 | */ |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 232 | private async setupToolbar(): Promise<void> { |
| 233 | this.recordButton = (UI.Toolbar.Toolbar.createActionButton(this.recordAction) as UI.Toolbar.ToolbarToggle); |
| 234 | this.toolbar.appendToolbarItem(this.recordButton); |
Rayan Kanso | 6890420 | 2019-02-21 14:16:25 | [diff] [blame] | 235 | |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame] | 236 | const clearButton = new UI.Toolbar.ToolbarButton(i18nString(UIStrings.clear), 'largeicon-clear'); |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 237 | clearButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, () => this.clearEvents()); |
| 238 | this.toolbar.appendToolbarItem(clearButton); |
Rayan Kanso | 6890420 | 2019-02-21 14:16:25 | [diff] [blame] | 239 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 240 | this.toolbar.appendSeparator(); |
Rayan Kanso | 6890420 | 2019-02-21 14:16:25 | [diff] [blame] | 241 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 242 | this.saveButton = new UI.Toolbar.ToolbarButton(i18nString(UIStrings.saveEvents), 'largeicon-download'); |
| 243 | this.saveButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, _event => { |
Tim van der Lippe | 2d9a95c | 2022-01-04 15:18:03 | [diff] [blame] | 244 | void this.saveToFile(); |
Tim van der Lippe | 37a35ff | 2020-03-03 13:49:02 | [diff] [blame] | 245 | }); |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 246 | this.saveButton.setEnabled(false); |
| 247 | this.toolbar.appendToolbarItem(this.saveButton); |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 248 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 249 | this.toolbar.appendSeparator(); |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 250 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 251 | this.originCheckbox = new UI.Toolbar.ToolbarCheckbox( |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame] | 252 | i18nString(UIStrings.showEventsFromOtherDomains), i18nString(UIStrings.showEventsFromOtherDomains), |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 253 | () => this.refreshView()); |
| 254 | this.toolbar.appendToolbarItem(this.originCheckbox); |
Kateryna Prokopenko | 35d6bf6 | 2022-11-30 13:56:19 | [diff] [blame^] | 255 | |
| 256 | this.storageKeyCheckbox = new UI.Toolbar.ToolbarCheckbox( |
| 257 | i18nString(UIStrings.showEventsForOtherStorageKeys), i18nString(UIStrings.showEventsForOtherStorageKeys), |
| 258 | () => this.refreshView()); |
| 259 | this.toolbar.appendToolbarItem(this.storageKeyCheckbox); |
Rayan Kanso | 6890420 | 2019-02-21 14:16:25 | [diff] [blame] | 260 | } |
Rayan Kanso | 8fe8ee2 | 2019-03-04 14:58:46 | [diff] [blame] | 261 | |
| 262 | /** |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 263 | * Displays all available events in the grid. |
| 264 | */ |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 265 | private refreshView(): void { |
| 266 | this.clearView(); |
| 267 | const events = this.model.getEvents(this.serviceName).filter(event => this.acceptEvent(event)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 268 | for (const event of events) { |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 269 | this.addEvent(event); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 270 | } |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Clears the grid and panel. |
| 275 | */ |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 276 | private clearView(): void { |
| 277 | this.selectedEventNode = null; |
| 278 | this.dataGrid.rootNode().removeChildren(); |
| 279 | this.saveButton.setEnabled(false); |
| 280 | this.showPreview(null); |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /** |
Rayan Kanso | 8fe8ee2 | 2019-03-04 14:58:46 | [diff] [blame] | 284 | * Called when the `Toggle Record` button is clicked. |
| 285 | */ |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 286 | toggleRecording(): void { |
| 287 | this.model.setRecording(!this.recordButton.toggled(), this.serviceName); |
Rayan Kanso | 8fe8ee2 | 2019-03-04 14:58:46 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /** |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 291 | * Called when the `Clear` button is clicked. |
| 292 | */ |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 293 | private clearEvents(): void { |
| 294 | this.model.clearEvents(this.serviceName); |
| 295 | this.clearView(); |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 296 | } |
| 297 | |
Benedikt Meurer | 5ea6942 | 2021-09-22 06:59:36 | [diff] [blame] | 298 | private onRecordingStateChanged({data: state}: Common.EventTarget.EventTargetEvent<RecordingState>): void { |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 299 | if (state.serviceName !== this.serviceName) { |
Rayan Kanso | 8fe8ee2 | 2019-03-04 14:58:46 | [diff] [blame] | 300 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 301 | } |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 302 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 303 | if (state.isRecording === this.recordButton.toggled()) { |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 304 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 305 | } |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 306 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 307 | this.recordButton.setToggled(state.isRecording); |
| 308 | this.updateRecordButtonTooltip(); |
| 309 | this.showPreview(this.selectedEventNode); |
Rayan Kanso | 8fe8ee2 | 2019-03-04 14:58:46 | [diff] [blame] | 310 | } |
Rayan Kanso | f40e315 | 2019-03-11 13:49:43 | [diff] [blame] | 311 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 312 | private updateRecordButtonTooltip(): void { |
| 313 | const buttonTooltip = this.recordButton.toggled() ? i18nString(UIStrings.stopRecordingEvents) : |
| 314 | i18nString(UIStrings.startRecordingEvents); |
| 315 | this.recordButton.setTitle(buttonTooltip, 'background-service.toggle-recording'); |
Christy Chen | 13a12ff | 2020-08-20 07:43:26 | [diff] [blame] | 316 | } |
| 317 | |
Benedikt Meurer | 5ea6942 | 2021-09-22 06:59:36 | [diff] [blame] | 318 | private onEventReceived({ |
| 319 | data: serviceEvent, |
| 320 | }: Common.EventTarget.EventTargetEvent<Protocol.BackgroundService.BackgroundServiceEvent>): void { |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 321 | if (!this.acceptEvent(serviceEvent)) { |
Rayan Kanso | f40e315 | 2019-03-11 13:49:43 | [diff] [blame] | 322 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 323 | } |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 324 | this.addEvent(serviceEvent); |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 325 | } |
| 326 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 327 | private onOriginChanged(): void { |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 328 | // No need to refresh the view if we are already showing all events. |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 329 | if (this.originCheckbox.checked()) { |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 330 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 331 | } |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 332 | this.refreshView(); |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 333 | } |
| 334 | |
Kateryna Prokopenko | 35d6bf6 | 2022-11-30 13:56:19 | [diff] [blame^] | 335 | private onStorageKeyChanged(): void { |
| 336 | if (this.storageKeyCheckbox.checked()) { |
| 337 | return; |
| 338 | } |
| 339 | this.refreshView(); |
| 340 | } |
| 341 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 342 | private addEvent(serviceEvent: Protocol.BackgroundService.BackgroundServiceEvent): void { |
| 343 | const data = this.createEventData(serviceEvent); |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 344 | const dataNode = new EventDataNode(data, serviceEvent.eventMetadata); |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 345 | this.dataGrid.rootNode().appendChild(dataNode); |
Rayan Kanso | b852ba8 | 2019-04-08 13:48:07 | [diff] [blame] | 346 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 347 | if (this.dataGrid.rootNode().children.length === 1) { |
| 348 | this.saveButton.setEnabled(true); |
| 349 | this.showPreview(this.selectedEventNode); |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 350 | } |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 351 | } |
| 352 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 353 | private createDataGrid(): DataGrid.DataGrid.DataGridImpl<EventData> { |
Jan Scheffler | 1026b9d | 2021-02-26 17:12:00 | [diff] [blame] | 354 | const columns = ([ |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame] | 355 | {id: 'id', title: '#', weight: 1}, |
Kateryna Prokopenko | 35d6bf6 | 2022-11-30 13:56:19 | [diff] [blame^] | 356 | {id: 'timestamp', title: i18nString(UIStrings.timestamp), weight: 7}, |
| 357 | {id: 'eventName', title: i18nString(UIStrings.event), weight: 8}, |
| 358 | {id: 'origin', title: i18nString(UIStrings.origin), weight: 8}, |
| 359 | {id: 'storageKey', title: i18nString(UIStrings.storageKey), weight: 8}, |
| 360 | {id: 'swScope', title: i18nString(UIStrings.swScope), weight: 4}, |
| 361 | {id: 'instanceId', title: i18nString(UIStrings.instanceId), weight: 8}, |
Jan Scheffler | 1026b9d | 2021-02-26 17:12:00 | [diff] [blame] | 362 | ] as DataGrid.DataGrid.ColumnDescriptor[]); |
Alex Rudenko | 60a3e94 | 2020-11-02 12:27:57 | [diff] [blame] | 363 | const dataGrid = new DataGrid.DataGrid.DataGridImpl({ |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame] | 364 | displayName: i18nString(UIStrings.backgroundServices), |
Alex Rudenko | 60a3e94 | 2020-11-02 12:27:57 | [diff] [blame] | 365 | columns, |
| 366 | editCallback: undefined, |
| 367 | refreshCallback: undefined, |
Jan Scheffler | 1026b9d | 2021-02-26 17:12:00 | [diff] [blame] | 368 | deleteCallback: undefined, |
Alex Rudenko | 60a3e94 | 2020-11-02 12:27:57 | [diff] [blame] | 369 | }); |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 370 | dataGrid.setStriped(true); |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 371 | |
| 372 | dataGrid.addEventListener( |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 373 | DataGrid.DataGrid.Events.SelectedNode, event => this.showPreview((event.data as EventDataNode))); |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 374 | |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 375 | return dataGrid; |
| 376 | } |
| 377 | |
| 378 | /** |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 379 | * Creates the data object to pass to the DataGrid Node. |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 380 | */ |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 381 | private createEventData(serviceEvent: Protocol.BackgroundService.BackgroundServiceEvent): EventData { |
Rayan Kanso | cc02ea3 | 2019-05-02 21:26:52 | [diff] [blame] | 382 | let swScope = ''; |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 383 | |
Rayan Kanso | cc02ea3 | 2019-05-02 21:26:52 | [diff] [blame] | 384 | // Try to get the scope of the Service Worker registration to be more user-friendly. |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 385 | const registration = this.serviceWorkerManager ? |
| 386 | this.serviceWorkerManager.registrations().get(serviceEvent.serviceWorkerRegistrationId) : |
Alex Rudenko | 60a3e94 | 2020-11-02 12:27:57 | [diff] [blame] | 387 | undefined; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 388 | if (registration) { |
Rayan Kanso | cc02ea3 | 2019-05-02 21:26:52 | [diff] [blame] | 389 | swScope = registration.scopeURL.substr(registration.securityOrigin.length); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 390 | } |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 391 | |
| 392 | return { |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 393 | id: this.dataGrid.rootNode().children.length + 1, |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 394 | timestamp: UI.UIUtils.formatTimestamp(serviceEvent.timestamp * 1000, /* full= */ true), |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 395 | origin: serviceEvent.origin, |
Kateryna Prokopenko | 35d6bf6 | 2022-11-30 13:56:19 | [diff] [blame^] | 396 | storageKey: serviceEvent.storageKey, |
Rayan Kanso | cc02ea3 | 2019-05-02 21:26:52 | [diff] [blame] | 397 | swScope, |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 398 | eventName: serviceEvent.eventName, |
| 399 | instanceId: serviceEvent.instanceId, |
| 400 | }; |
| 401 | } |
| 402 | |
| 403 | /** |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 404 | * Filtration function to know whether event should be shown or not. |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 405 | */ |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 406 | private acceptEvent(event: Protocol.BackgroundService.BackgroundServiceEvent): boolean { |
| 407 | if (event.service !== this.serviceName) { |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 408 | return false; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 409 | } |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 410 | |
Kateryna Prokopenko | 35d6bf6 | 2022-11-30 13:56:19 | [diff] [blame^] | 411 | if (this.originCheckbox.checked() || this.storageKeyCheckbox.checked()) { |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 412 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 413 | } |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 414 | |
| 415 | // Trim the trailing '/'. |
| 416 | const origin = event.origin.substr(0, event.origin.length - 1); |
Kateryna Prokopenko | 35d6bf6 | 2022-11-30 13:56:19 | [diff] [blame^] | 417 | const storageKey = event.storageKey; |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 418 | |
Kateryna Prokopenko | 35d6bf6 | 2022-11-30 13:56:19 | [diff] [blame^] | 419 | return this.securityOriginManager.securityOrigins().includes(origin) || |
| 420 | this.storageKeyManager.storageKeys().includes(storageKey); |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 421 | } |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 422 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 423 | private createLearnMoreLink(): Element { |
Jecelyn | 90c0416 | 2021-04-07 08:41:35 | [diff] [blame] | 424 | let url = 'https://developer.chrome.com/docs/devtools/javascript/background-services/?utm_source=devtools'; |
Rayan Kanso | 42c0f9e | 2019-09-02 12:21:09 | [diff] [blame] | 425 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 426 | switch (this.serviceName) { |
Rayan Kanso | 42c0f9e | 2019-09-02 12:21:09 | [diff] [blame] | 427 | case Protocol.BackgroundService.ServiceName.BackgroundFetch: |
| 428 | url += '#fetch'; |
| 429 | break; |
| 430 | case Protocol.BackgroundService.ServiceName.BackgroundSync: |
| 431 | url += '#sync'; |
| 432 | break; |
| 433 | case Protocol.BackgroundService.ServiceName.PushMessaging: |
| 434 | url += '#push'; |
| 435 | break; |
| 436 | case Protocol.BackgroundService.ServiceName.Notifications: |
| 437 | url += '#notifications'; |
| 438 | break; |
| 439 | default: |
| 440 | break; |
| 441 | } |
| 442 | |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame] | 443 | return UI.XLink.XLink.create(url, i18nString(UIStrings.learnMore)); |
Rayan Kanso | 42c0f9e | 2019-09-02 12:21:09 | [diff] [blame] | 444 | } |
| 445 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 446 | private showPreview(dataNode: EventDataNode|null): void { |
| 447 | if (this.selectedEventNode && this.selectedEventNode === dataNode) { |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 448 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 449 | } |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 450 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 451 | this.selectedEventNode = dataNode; |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 452 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 453 | if (this.preview) { |
| 454 | this.preview.detach(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 455 | } |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 456 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 457 | if (this.selectedEventNode) { |
| 458 | this.preview = this.selectedEventNode.createPreview(); |
| 459 | this.preview.show(this.previewPanel.contentElement); |
Rayan Kanso | 4476ca5 | 2019-07-22 11:51:24 | [diff] [blame] | 460 | return; |
| 461 | } |
| 462 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 463 | this.preview = new UI.Widget.VBox(); |
| 464 | this.preview.contentElement.classList.add('background-service-preview', 'fill'); |
| 465 | const centered = this.preview.contentElement.createChild('div'); |
Rayan Kanso | 4476ca5 | 2019-07-22 11:51:24 | [diff] [blame] | 466 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 467 | if (this.dataGrid.rootNode().children.length) { |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 468 | // Inform users that grid entries are clickable. |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame] | 469 | centered.createChild('p').textContent = i18nString(UIStrings.selectAnEntryToViewMetadata); |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 470 | } else if (this.recordButton.toggled()) { |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 471 | // Inform users that we are recording/waiting for events. |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 472 | const featureName = BackgroundServiceView.getUIString(this.serviceName); |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame] | 473 | centered.createChild('p').textContent = i18nString(UIStrings.recordingSActivity, {PH1: featureName}); |
| 474 | centered.createChild('p').textContent = i18nString(UIStrings.devtoolsWillRecordAllSActivity, {PH1: featureName}); |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 475 | } else { |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 476 | const landingRecordButton = UI.Toolbar.Toolbar.createActionButton(this.recordAction); |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 477 | |
Tim van der Lippe | f49e232 | 2020-05-01 15:03:09 | [diff] [blame] | 478 | const recordKey = document.createElement('b'); |
| 479 | recordKey.classList.add('background-service-shortcut'); |
Tim van der Lippe | 9c9fb12 | 2020-09-08 15:06:17 | [diff] [blame] | 480 | recordKey.textContent = UI.ShortcutRegistry.ShortcutRegistry.instance() |
| 481 | .shortcutsForAction('background-service.toggle-recording')[0] |
| 482 | .title(); |
Rayan Kanso | 6156d22 | 2019-04-29 23:40:55 | [diff] [blame] | 483 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 484 | const inlineButton = UI.UIUtils.createInlineButton(landingRecordButton); |
Rayan Kanso | f402ef3 | 2019-08-12 13:05:18 | [diff] [blame] | 485 | inlineButton.classList.add('background-service-record-inline-button'); |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame] | 486 | centered.createChild('p').appendChild(i18n.i18n.getFormatLocalizedString( |
| 487 | str_, UIStrings.clickTheRecordButtonSOrHitSTo, {PH1: inlineButton, PH2: recordKey})); |
Rayan Kanso | 42c0f9e | 2019-09-02 12:21:09 | [diff] [blame] | 488 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 489 | centered.appendChild(this.createLearnMoreLink()); |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 490 | } |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 491 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 492 | this.preview.show(this.previewPanel.contentElement); |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 493 | } |
Rayan Kanso | b852ba8 | 2019-04-08 13:48:07 | [diff] [blame] | 494 | |
| 495 | /** |
| 496 | * Saves all currently displayed events in a file (JSON format). |
| 497 | */ |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 498 | private async saveToFile(): Promise<void> { |
Kateryna Prokopenko | 380fdfa | 2022-03-16 16:39:32 | [diff] [blame] | 499 | const fileName = `${this.serviceName}-${Platform.DateUtilities.toISO8601Compact(new Date())}.json` as |
| 500 | Platform.DevToolsPath.RawPathString; |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 501 | const stream = new Bindings.FileUtils.FileOutputStream(); |
Rayan Kanso | b852ba8 | 2019-04-08 13:48:07 | [diff] [blame] | 502 | |
| 503 | const accepted = await stream.open(fileName); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 504 | if (!accepted) { |
Rayan Kanso | b852ba8 | 2019-04-08 13:48:07 | [diff] [blame] | 505 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 506 | } |
Rayan Kanso | b852ba8 | 2019-04-08 13:48:07 | [diff] [blame] | 507 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 508 | const events = this.model.getEvents(this.serviceName).filter(event => this.acceptEvent(event)); |
Rayan Kanso | b852ba8 | 2019-04-08 13:48:07 | [diff] [blame] | 509 | await stream.write(JSON.stringify(events, undefined, 2)); |
Tim van der Lippe | 2d9a95c | 2022-01-04 15:18:03 | [diff] [blame] | 510 | void stream.close(); |
Rayan Kanso | b852ba8 | 2019-04-08 13:48:07 | [diff] [blame] | 511 | } |
Kriti Sapra | 928b70c | 2021-08-17 09:03:41 | [diff] [blame] | 512 | wasShown(): void { |
| 513 | super.wasShown(); |
| 514 | this.registerCSSFiles([emptyWidgetStyles, backgroundServiceViewStyles]); |
| 515 | } |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 516 | } |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 517 | |
Jan Scheffler | 1026b9d | 2021-02-26 17:12:00 | [diff] [blame] | 518 | export class EventDataNode extends DataGrid.DataGrid.DataGridNode<EventData> { |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 519 | private readonly eventMetadata: Protocol.BackgroundService.EventMetadata[]; |
Jan Scheffler | 1026b9d | 2021-02-26 17:12:00 | [diff] [blame] | 520 | |
| 521 | constructor(data: EventData, eventMetadata: Protocol.BackgroundService.EventMetadata[]) { |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 522 | super(data); |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 523 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 524 | this.eventMetadata = eventMetadata.sort((m1, m2) => Platform.StringUtilities.compare(m1.key, m2.key)); |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 525 | } |
| 526 | |
Jan Scheffler | 1026b9d | 2021-02-26 17:12:00 | [diff] [blame] | 527 | createPreview(): UI.Widget.VBox { |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 528 | const preview = new UI.Widget.VBox(); |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 529 | preview.element.classList.add('background-service-metadata'); |
| 530 | |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 531 | for (const entry of this.eventMetadata) { |
Tim van der Lippe | f49e232 | 2020-05-01 15:03:09 | [diff] [blame] | 532 | const div = document.createElement('div'); |
| 533 | div.classList.add('background-service-metadata-entry'); |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 534 | div.createChild('div', 'background-service-metadata-name').textContent = entry.key + ': '; |
Rayan Kanso | 4476ca5 | 2019-07-22 11:51:24 | [diff] [blame] | 535 | if (entry.value) { |
| 536 | div.createChild('div', 'background-service-metadata-value source-code').textContent = entry.value; |
| 537 | } else { |
| 538 | div.createChild('div', 'background-service-metadata-value background-service-empty-value').textContent = |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame] | 539 | i18nString(UIStrings.empty); |
Rayan Kanso | 4476ca5 | 2019-07-22 11:51:24 | [diff] [blame] | 540 | } |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 541 | preview.element.appendChild(div); |
| 542 | } |
| 543 | |
| 544 | if (!preview.element.children.length) { |
Tim van der Lippe | f49e232 | 2020-05-01 15:03:09 | [diff] [blame] | 545 | const div = document.createElement('div'); |
| 546 | div.classList.add('background-service-metadata-entry'); |
Sigurd Schneider | fec75bb | 2021-03-19 10:23:34 | [diff] [blame] | 547 | div.createChild('div', 'background-service-metadata-name background-service-empty-value').textContent = |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame] | 548 | i18nString(UIStrings.noMetadataForThisEvent); |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 549 | preview.element.appendChild(div); |
| 550 | } |
| 551 | |
| 552 | return preview; |
Rayan Kanso | f40e315 | 2019-03-11 13:49:43 | [diff] [blame] | 553 | } |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 554 | } |
Rayan Kanso | 6156d22 | 2019-04-29 23:40:55 | [diff] [blame] | 555 | |
Jan Scheffler | 1026b9d | 2021-02-26 17:12:00 | [diff] [blame] | 556 | let actionDelegateInstance: ActionDelegate; |
Andres Olivares | 7957851 | 2021-02-02 00:27:49 | [diff] [blame] | 557 | |
Jan Scheffler | 1026b9d | 2021-02-26 17:12:00 | [diff] [blame] | 558 | export class ActionDelegate implements UI.ActionRegistration.ActionDelegate { |
| 559 | static instance(opts: { |
| 560 | forceNew: boolean|null, |
| 561 | } = {forceNew: null}): ActionDelegate { |
Andres Olivares | 7957851 | 2021-02-02 00:27:49 | [diff] [blame] | 562 | const {forceNew} = opts; |
| 563 | if (!actionDelegateInstance || forceNew) { |
| 564 | actionDelegateInstance = new ActionDelegate(); |
| 565 | } |
| 566 | |
| 567 | return actionDelegateInstance; |
| 568 | } |
| 569 | |
Jan Scheffler | 1026b9d | 2021-02-26 17:12:00 | [diff] [blame] | 570 | handleAction(context: UI.Context.Context, actionId: string): boolean { |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 571 | const view = context.flavor(BackgroundServiceView); |
Rayan Kanso | 6156d22 | 2019-04-29 23:40:55 | [diff] [blame] | 572 | switch (actionId) { |
Alex Rudenko | 60a3e94 | 2020-11-02 12:27:57 | [diff] [blame] | 573 | case 'background-service.toggle-recording': { |
| 574 | if (!view) { |
| 575 | throw new Error('BackgroundServiceView instance is missing'); |
| 576 | } |
Jan Scheffler | cd49c32 | 2021-08-13 18:56:33 | [diff] [blame] | 577 | view.toggleRecording(); |
Rayan Kanso | 6156d22 | 2019-04-29 23:40:55 | [diff] [blame] | 578 | return true; |
Alex Rudenko | 60a3e94 | 2020-11-02 12:27:57 | [diff] [blame] | 579 | } |
Rayan Kanso | 6156d22 | 2019-04-29 23:40:55 | [diff] [blame] | 580 | } |
| 581 | return false; |
| 582 | } |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 583 | } |
Jan Scheffler | 1026b9d | 2021-02-26 17:12:00 | [diff] [blame] | 584 | export interface RecordingState { |
| 585 | isRecording: boolean; |
| 586 | serviceName: Protocol.BackgroundService.ServiceName; |
| 587 | } |
| 588 | export interface EventData { |
| 589 | id: number; |
| 590 | timestamp: string; |
| 591 | origin: string; |
Kateryna Prokopenko | 35d6bf6 | 2022-11-30 13:56:19 | [diff] [blame^] | 592 | storageKey: string; |
Jan Scheffler | 1026b9d | 2021-02-26 17:12:00 | [diff] [blame] | 593 | swScope: string; |
| 594 | eventName: string; |
| 595 | instanceId: string; |
| 596 | } |