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