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 | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 5 | import * as Bindings from '../bindings/bindings.js'; |
Tim van der Lippe | c02a97c | 2020-02-14 14:39:27 | [diff] [blame] | 6 | import * as Common from '../common/common.js'; // eslint-disable-line no-unused-vars |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 7 | import * as DataGrid from '../data_grid/data_grid.js'; |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame^] | 8 | import * as i18n from '../i18n/i18n.js'; |
Simon Zünd | 2c704cd | 2020-06-04 09:08:35 | [diff] [blame] | 9 | import * as Platform from '../platform/platform.js'; |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 10 | import * as SDK from '../sdk/sdk.js'; |
| 11 | import * as UI from '../ui/ui.js'; |
| 12 | |
Paul Lewis | ddf5834 | 2020-01-15 14:18:07 | [diff] [blame] | 13 | import {BackgroundServiceModel, Events} from './BackgroundServiceModel.js'; // eslint-disable-line no-unused-vars |
| 14 | |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame^] | 15 | export const UIStrings = { |
| 16 | /** |
| 17 | *@description Text in Background Service View of the Application panel |
| 18 | */ |
| 19 | backgroundFetch: 'Background Fetch', |
| 20 | /** |
| 21 | *@description Text in Background Service View of the Application panel |
| 22 | */ |
| 23 | backgroundSync: 'Background Sync', |
| 24 | /** |
| 25 | *@description Text in Background Service View of the Application panel |
| 26 | */ |
| 27 | pushMessaging: 'Push Messaging', |
| 28 | /** |
| 29 | *@description Text in Background Service View of the Application panel |
| 30 | */ |
| 31 | notifications: 'Notifications', |
| 32 | /** |
| 33 | *@description Text in Background Service View of the Application panel |
| 34 | */ |
| 35 | paymentHandler: 'Payment Handler', |
| 36 | /** |
| 37 | *@description Text in the Periodic Background Service View of the Application panel |
| 38 | */ |
| 39 | periodicBackgroundSync: 'Periodic Background Sync', |
| 40 | /** |
| 41 | *@description Text to clear content |
| 42 | */ |
| 43 | clear: 'Clear', |
| 44 | /** |
| 45 | *@description Tooltip text that appears when hovering over the largeicon download button in the Background Service View of the Application panel |
| 46 | */ |
| 47 | saveEvents: 'Save events', |
| 48 | /** |
| 49 | *@description Text in Background Service View of the Application panel |
| 50 | */ |
| 51 | showEventsFromOtherDomains: 'Show events from other domains', |
| 52 | /** |
| 53 | *@description Title of an action under the Background Services category that can be invoked through the Command Menu |
| 54 | */ |
| 55 | stopRecordingEvents: 'Stop recording events', |
| 56 | /** |
| 57 | *@description Title of an action under the Background Services category that can be invoked through the Command Menu |
| 58 | */ |
| 59 | startRecordingEvents: 'Start recording events', |
| 60 | /** |
| 61 | *@description Text for timestamps of items |
| 62 | */ |
| 63 | timestamp: 'Timestamp', |
| 64 | /** |
| 65 | *@description Text that refers to some events |
| 66 | */ |
| 67 | event: 'Event', |
| 68 | /** |
| 69 | *@description Text for the origin of something |
| 70 | */ |
| 71 | origin: 'Origin', |
| 72 | /** |
| 73 | *@description Text in Background Service View of the Application panel |
| 74 | */ |
| 75 | swScope: 'SW Scope', |
| 76 | /** |
| 77 | *@description Text in Background Service View of the Application panel |
| 78 | */ |
| 79 | instanceId: 'Instance ID', |
| 80 | /** |
| 81 | *@description Text in Application Panel Sidebar of the Application panel |
| 82 | */ |
| 83 | backgroundServices: 'Background Services', |
| 84 | /** |
| 85 | *@description Text that is usually a hyperlink to more documentation |
| 86 | */ |
| 87 | learnMore: 'Learn more', |
| 88 | /** |
| 89 | *@description Text in Background Service View of the Application panel |
| 90 | */ |
| 91 | selectAnEntryToViewMetadata: 'Select an entry to view metadata', |
| 92 | /** |
| 93 | *@description Text in Background Service View of the Application panel |
| 94 | *@example {Background Fetch} PH1 |
| 95 | */ |
| 96 | recordingSActivity: 'Recording {PH1} activity...', |
| 97 | /** |
| 98 | *@description Inform users that DevTools are recording/waiting for events in the Periodic Background Sync tool of the Application panel |
| 99 | *@example {Background Fetch} PH1 |
| 100 | */ |
| 101 | devtoolsWillRecordAllSActivity: 'DevTools will record all {PH1} activity for up to 3 days, even when closed.', |
| 102 | /** |
| 103 | *@description Text in Background Service View of the Application panel |
| 104 | *@example {record} PH1 |
| 105 | *@example {Ctrl + R} PH2 |
| 106 | */ |
| 107 | clickTheRecordButtonSOrHitSTo: 'Click the record button {PH1} or hit {PH2} to start recording.', |
| 108 | /** |
| 109 | *@description Text to show an item is empty |
| 110 | */ |
| 111 | empty: 'empty', |
| 112 | /** |
| 113 | *@description Text in Background Service View of the Application panel |
| 114 | */ |
| 115 | noMetadataForThisEvent: 'No metadata for this event', |
| 116 | }; |
| 117 | const str_ = i18n.i18n.registerUIStrings('resources/BackgroundServiceView.js', UIStrings); |
| 118 | const i18nString = i18n.i18n.getLocalizedString.bind(undefined, str_); |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 119 | export class BackgroundServiceView extends UI.Widget.VBox { |
Rayan Kanso | 6890420 | 2019-02-21 14:16:25 | [diff] [blame] | 120 | /** |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 121 | * @param {string} serviceName The name of the background service. |
| 122 | * @return {string} The UI String to display. |
| 123 | */ |
| 124 | static getUIString(serviceName) { |
| 125 | switch (serviceName) { |
| 126 | case Protocol.BackgroundService.ServiceName.BackgroundFetch: |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame^] | 127 | return i18nString(UIStrings.backgroundFetch); |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 128 | case Protocol.BackgroundService.ServiceName.BackgroundSync: |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame^] | 129 | return i18nString(UIStrings.backgroundSync); |
Fergus Dall | 0be4f4c | 2019-05-21 00:01:29 | [diff] [blame] | 130 | case Protocol.BackgroundService.ServiceName.PushMessaging: |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame^] | 131 | return i18nString(UIStrings.pushMessaging); |
Fergus Dall | 0be4f4c | 2019-05-21 00:01:29 | [diff] [blame] | 132 | case Protocol.BackgroundService.ServiceName.Notifications: |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame^] | 133 | return i18nString(UIStrings.notifications); |
Rayan Kanso | 8d7918a | 2019-07-03 21:03:38 | [diff] [blame] | 134 | case Protocol.BackgroundService.ServiceName.PaymentHandler: |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame^] | 135 | return i18nString(UIStrings.paymentHandler); |
Rayan Kanso | 5480967 | 2019-07-24 18:40:28 | [diff] [blame] | 136 | case Protocol.BackgroundService.ServiceName.PeriodicBackgroundSync: |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame^] | 137 | return i18nString(UIStrings.periodicBackgroundSync); |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 138 | default: |
| 139 | return ''; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
Rayan Kanso | 8fe8ee2 | 2019-03-04 14:58:46 | [diff] [blame] | 144 | * @param {!Protocol.BackgroundService.ServiceName} serviceName |
Paul Lewis | ddf5834 | 2020-01-15 14:18:07 | [diff] [blame] | 145 | * @param {!BackgroundServiceModel} model |
Rayan Kanso | 6890420 | 2019-02-21 14:16:25 | [diff] [blame] | 146 | */ |
Rayan Kanso | 8fe8ee2 | 2019-03-04 14:58:46 | [diff] [blame] | 147 | constructor(serviceName, model) { |
Rayan Kanso | 6890420 | 2019-02-21 14:16:25 | [diff] [blame] | 148 | super(true); |
Jack Franklin | 71519f8 | 2020-11-03 12:08:59 | [diff] [blame] | 149 | this.registerRequiredCSS('resources/backgroundServiceView.css', {enableLegacyPatching: true}); |
Jack Franklin | 7cdd342 | 2020-12-07 11:32:12 | [diff] [blame] | 150 | this.registerRequiredCSS('ui/emptyWidget.css', {enableLegacyPatching: false}); |
Rayan Kanso | 6890420 | 2019-02-21 14:16:25 | [diff] [blame] | 151 | |
Rayan Kanso | 8fe8ee2 | 2019-03-04 14:58:46 | [diff] [blame] | 152 | /** @const {!Protocol.BackgroundService.ServiceName} */ |
Rayan Kanso | 6890420 | 2019-02-21 14:16:25 | [diff] [blame] | 153 | this._serviceName = serviceName; |
| 154 | |
Paul Lewis | ddf5834 | 2020-01-15 14:18:07 | [diff] [blame] | 155 | /** @const {!BackgroundServiceModel} */ |
Rayan Kanso | 8fe8ee2 | 2019-03-04 14:58:46 | [diff] [blame] | 156 | this._model = model; |
Paul Lewis | ddf5834 | 2020-01-15 14:18:07 | [diff] [blame] | 157 | this._model.addEventListener(Events.RecordingStateChanged, this._onRecordingStateChanged, this); |
| 158 | this._model.addEventListener(Events.BackgroundServiceEventReceived, this._onEventReceived, this); |
Rayan Kanso | 8fe8ee2 | 2019-03-04 14:58:46 | [diff] [blame] | 159 | this._model.enable(this._serviceName); |
| 160 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 161 | /** @const {?SDK.ServiceWorkerManager.ServiceWorkerManager} */ |
| 162 | this._serviceWorkerManager = this._model.target().model(SDK.ServiceWorkerManager.ServiceWorkerManager); |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 163 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 164 | /** @const {?SDK.SecurityOriginManager.SecurityOriginManager} */ |
| 165 | this._securityOriginManager = this._model.target().model(SDK.SecurityOriginManager.SecurityOriginManager); |
Alex Rudenko | 60a3e94 | 2020-11-02 12:27:57 | [diff] [blame] | 166 | if (!this._securityOriginManager) { |
| 167 | throw new Error('SecurityOriginManager instance is missing'); |
| 168 | } |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 169 | this._securityOriginManager.addEventListener( |
| 170 | SDK.SecurityOriginManager.Events.MainSecurityOriginChanged, () => this._onOriginChanged()); |
| 171 | |
Rayan Kanso | cc02ea3 | 2019-05-02 21:26:52 | [diff] [blame] | 172 | |
Andres Olivares | 6ff6d04 | 2020-11-17 14:31:57 | [diff] [blame] | 173 | /** @type {!UI.ActionRegistration.Action} */ |
Paul Lewis | 24cb740 | 2020-01-24 13:46:35 | [diff] [blame] | 174 | this._recordAction = |
Andres Olivares | 6ff6d04 | 2020-11-17 14:31:57 | [diff] [blame] | 175 | /** @type {!UI.ActionRegistration.Action} */ ( |
Tim van der Lippe | 177c0b2 | 2020-08-19 14:56:02 | [diff] [blame] | 176 | UI.ActionRegistry.ActionRegistry.instance().action('background-service.toggle-recording')); |
Rayan Kanso | 8fe8ee2 | 2019-03-04 14:58:46 | [diff] [blame] | 177 | |
Alex Rudenko | 60a3e94 | 2020-11-02 12:27:57 | [diff] [blame] | 178 | /** |
| 179 | * Initialised in the _setupToolbar() call below. |
| 180 | * @type {!UI.Toolbar.ToolbarToggle} |
| 181 | */ |
| 182 | this._recordButton; |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 183 | |
Alex Rudenko | 60a3e94 | 2020-11-02 12:27:57 | [diff] [blame] | 184 | /** |
| 185 | * Initialised in the _setupToolbar() call below. |
| 186 | * @type {!UI.Toolbar.ToolbarCheckbox} |
| 187 | */ |
| 188 | this._originCheckbox; |
| 189 | |
| 190 | /** |
| 191 | * Initialised in the _setupToolbar() call below. |
| 192 | * @type {!UI.Toolbar.ToolbarButton} |
| 193 | */ |
| 194 | this._saveButton; |
Rayan Kanso | b852ba8 | 2019-04-08 13:48:07 | [diff] [blame] | 195 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 196 | /** @const {!UI.Toolbar.Toolbar} */ |
| 197 | this._toolbar = new UI.Toolbar.Toolbar('background-service-toolbar', this.contentElement); |
Rayan Kanso | 6890420 | 2019-02-21 14:16:25 | [diff] [blame] | 198 | this._setupToolbar(); |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 199 | |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 200 | /** |
| 201 | * This will contain the DataGrid for displaying events, and a panel at the bottom for showing |
| 202 | * extra metadata related to the selected event. |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 203 | * @const {!UI.SplitWidget.SplitWidget} |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 204 | */ |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 205 | this._splitWidget = new UI.SplitWidget.SplitWidget(/* isVertical= */ false, /* secondIsSidebar= */ true); |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 206 | this._splitWidget.show(this.contentElement); |
| 207 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 208 | /** @const {!DataGrid.DataGrid.DataGridImpl} */ |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 209 | this._dataGrid = this._createDataGrid(); |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 210 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 211 | /** @const {!UI.Widget.VBox} */ |
| 212 | this._previewPanel = new UI.Widget.VBox(); |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 213 | |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 214 | /** @type {?EventDataNode} */ |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 215 | this._selectedEventNode = null; |
| 216 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 217 | /** @type {?UI.Widget.Widget} */ |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 218 | this._preview = null; |
| 219 | |
| 220 | this._splitWidget.setMainWidget(this._dataGrid.asWidget()); |
| 221 | this._splitWidget.setSidebarWidget(this._previewPanel); |
| 222 | |
| 223 | this._showPreview(null); |
Rayan Kanso | 6890420 | 2019-02-21 14:16:25 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | /** |
| 227 | * Creates the toolbar UI element. |
| 228 | */ |
Rayan Kanso | 8fe8ee2 | 2019-03-04 14:58:46 | [diff] [blame] | 229 | async _setupToolbar() { |
Alex Rudenko | 60a3e94 | 2020-11-02 12:27:57 | [diff] [blame] | 230 | this._recordButton = |
| 231 | /** @type {!UI.Toolbar.ToolbarToggle} */ (UI.Toolbar.Toolbar.createActionButton(this._recordAction)); |
Rayan Kanso | 8fe8ee2 | 2019-03-04 14:58:46 | [diff] [blame] | 232 | this._toolbar.appendToolbarItem(this._recordButton); |
Rayan Kanso | 6890420 | 2019-02-21 14:16:25 | [diff] [blame] | 233 | |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame^] | 234 | const clearButton = new UI.Toolbar.ToolbarButton(i18nString(UIStrings.clear), 'largeicon-clear'); |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 235 | clearButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, () => this._clearEvents()); |
Rayan Kanso | 6890420 | 2019-02-21 14:16:25 | [diff] [blame] | 236 | this._toolbar.appendToolbarItem(clearButton); |
| 237 | |
| 238 | this._toolbar.appendSeparator(); |
| 239 | |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame^] | 240 | this._saveButton = new UI.Toolbar.ToolbarButton(i18nString(UIStrings.saveEvents), 'largeicon-download'); |
Tim van der Lippe | 37a35ff | 2020-03-03 13:49:02 | [diff] [blame] | 241 | this._saveButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, event => { |
| 242 | this._saveToFile(); |
| 243 | }); |
Rayan Kanso | b852ba8 | 2019-04-08 13:48:07 | [diff] [blame] | 244 | this._saveButton.setEnabled(false); |
| 245 | this._toolbar.appendToolbarItem(this._saveButton); |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 246 | |
| 247 | this._toolbar.appendSeparator(); |
| 248 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 249 | this._originCheckbox = new UI.Toolbar.ToolbarCheckbox( |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame^] | 250 | i18nString(UIStrings.showEventsFromOtherDomains), i18nString(UIStrings.showEventsFromOtherDomains), |
| 251 | () => this._refreshView()); |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 252 | this._toolbar.appendToolbarItem(this._originCheckbox); |
Rayan Kanso | 6890420 | 2019-02-21 14:16:25 | [diff] [blame] | 253 | } |
Rayan Kanso | 8fe8ee2 | 2019-03-04 14:58:46 | [diff] [blame] | 254 | |
| 255 | /** |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 256 | * Displays all available events in the grid. |
| 257 | */ |
| 258 | _refreshView() { |
| 259 | this._clearView(); |
| 260 | 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] | 261 | for (const event of events) { |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 262 | this._addEvent(event); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 263 | } |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Clears the grid and panel. |
| 268 | */ |
| 269 | _clearView() { |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 270 | this._selectedEventNode = null; |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 271 | this._dataGrid.rootNode().removeChildren(); |
Rayan Kanso | b852ba8 | 2019-04-08 13:48:07 | [diff] [blame] | 272 | this._saveButton.setEnabled(false); |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 273 | this._showPreview(null); |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /** |
Rayan Kanso | 8fe8ee2 | 2019-03-04 14:58:46 | [diff] [blame] | 277 | * Called when the `Toggle Record` button is clicked. |
| 278 | */ |
| 279 | _toggleRecording() { |
| 280 | this._model.setRecording(!this._recordButton.toggled(), this._serviceName); |
| 281 | } |
| 282 | |
| 283 | /** |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 284 | * Called when the `Clear` button is clicked. |
| 285 | */ |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 286 | _clearEvents() { |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 287 | this._model.clearEvents(this._serviceName); |
| 288 | this._clearView(); |
| 289 | } |
| 290 | |
| 291 | /** |
Tim van der Lippe | c02a97c | 2020-02-14 14:39:27 | [diff] [blame] | 292 | * @param {!Common.EventTarget.EventTargetEvent} event |
Rayan Kanso | 8fe8ee2 | 2019-03-04 14:58:46 | [diff] [blame] | 293 | */ |
| 294 | _onRecordingStateChanged(event) { |
Tim van der Lippe | ff5ad47 | 2020-02-17 12:35:09 | [diff] [blame] | 295 | const state = /** @type {!RecordingState} */ (event.data); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 296 | if (state.serviceName !== this._serviceName) { |
Rayan Kanso | 8fe8ee2 | 2019-03-04 14:58:46 | [diff] [blame] | 297 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 298 | } |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 299 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 300 | if (state.isRecording === this._recordButton.toggled()) { |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 301 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 302 | } |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 303 | |
Rayan Kanso | 0b30aba | 2019-05-17 13:39:59 | [diff] [blame] | 304 | this._recordButton.setToggled(state.isRecording); |
Christy Chen | 13a12ff | 2020-08-20 07:43:26 | [diff] [blame] | 305 | this._updateRecordButtonTooltip(); |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 306 | this._showPreview(this._selectedEventNode); |
Rayan Kanso | 8fe8ee2 | 2019-03-04 14:58:46 | [diff] [blame] | 307 | } |
Rayan Kanso | f40e315 | 2019-03-11 13:49:43 | [diff] [blame] | 308 | |
Christy Chen | 13a12ff | 2020-08-20 07:43:26 | [diff] [blame] | 309 | _updateRecordButtonTooltip() { |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame^] | 310 | const buttonTooltip = this._recordButton.toggled() ? i18nString(UIStrings.stopRecordingEvents) : |
| 311 | i18nString(UIStrings.startRecordingEvents); |
Christy Chen | 13a12ff | 2020-08-20 07:43:26 | [diff] [blame] | 312 | this._recordButton.setTitle(buttonTooltip, 'background-service.toggle-recording'); |
| 313 | } |
| 314 | |
Rayan Kanso | f40e315 | 2019-03-11 13:49:43 | [diff] [blame] | 315 | /** |
Tim van der Lippe | c02a97c | 2020-02-14 14:39:27 | [diff] [blame] | 316 | * @param {!Common.EventTarget.EventTargetEvent} event |
Rayan Kanso | f40e315 | 2019-03-11 13:49:43 | [diff] [blame] | 317 | */ |
| 318 | _onEventReceived(event) { |
| 319 | const serviceEvent = /** @type {!Protocol.BackgroundService.BackgroundServiceEvent} */ (event.data); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 320 | if (!this._acceptEvent(serviceEvent)) { |
Rayan Kanso | f40e315 | 2019-03-11 13:49:43 | [diff] [blame] | 321 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 322 | } |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 323 | this._addEvent(serviceEvent); |
| 324 | } |
| 325 | |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 326 | _onOriginChanged() { |
| 327 | // No need to refresh the view if we are already showing all events. |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 328 | if (this._originCheckbox.checked()) { |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 329 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 330 | } |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 331 | this._refreshView(); |
| 332 | } |
| 333 | |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 334 | /** |
| 335 | * @param {!Protocol.BackgroundService.BackgroundServiceEvent} serviceEvent |
| 336 | */ |
| 337 | _addEvent(serviceEvent) { |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 338 | const data = this._createEventData(serviceEvent); |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 339 | const dataNode = new EventDataNode(data, serviceEvent.eventMetadata); |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 340 | this._dataGrid.rootNode().appendChild(dataNode); |
Rayan Kanso | b852ba8 | 2019-04-08 13:48:07 | [diff] [blame] | 341 | |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 342 | if (this._dataGrid.rootNode().children.length === 1) { |
| 343 | this._saveButton.setEnabled(true); |
| 344 | this._showPreview(this._selectedEventNode); |
| 345 | } |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | /** |
Alex Rudenko | 60a3e94 | 2020-11-02 12:27:57 | [diff] [blame] | 349 | * @return {!DataGrid.DataGrid.DataGridImpl<!Object<string, string|number>>} |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 350 | */ |
| 351 | _createDataGrid() { |
Tim van der Lippe | 9a9aba8 | 2020-02-14 14:52:29 | [diff] [blame] | 352 | const columns = /** @type {!Array<!DataGrid.DataGrid.ColumnDescriptor>} */ ([ |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame^] | 353 | {id: 'id', title: '#', weight: 1}, |
| 354 | {id: 'timestamp', title: i18nString(UIStrings.timestamp), weight: 8}, |
| 355 | {id: 'eventName', title: i18nString(UIStrings.event), weight: 10}, |
| 356 | {id: 'origin', title: i18nString(UIStrings.origin), weight: 10}, |
| 357 | {id: 'swScope', title: i18nString(UIStrings.swScope), weight: 2}, |
| 358 | {id: 'instanceId', title: i18nString(UIStrings.instanceId), weight: 10}, |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 359 | ]); |
Alex Rudenko | 60a3e94 | 2020-11-02 12:27:57 | [diff] [blame] | 360 | const dataGrid = new DataGrid.DataGrid.DataGridImpl({ |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame^] | 361 | displayName: i18nString(UIStrings.backgroundServices), |
Alex Rudenko | 60a3e94 | 2020-11-02 12:27:57 | [diff] [blame] | 362 | columns, |
| 363 | editCallback: undefined, |
| 364 | refreshCallback: undefined, |
| 365 | deleteCallback: undefined |
| 366 | }); |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 367 | dataGrid.setStriped(true); |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 368 | |
| 369 | dataGrid.addEventListener( |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 370 | DataGrid.DataGrid.Events.SelectedNode, event => this._showPreview(/** @type {!EventDataNode} */ (event.data))); |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 371 | |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 372 | return dataGrid; |
| 373 | } |
| 374 | |
| 375 | /** |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 376 | * Creates the data object to pass to the DataGrid Node. |
| 377 | * @param {!Protocol.BackgroundService.BackgroundServiceEvent} serviceEvent |
Tim van der Lippe | ff5ad47 | 2020-02-17 12:35:09 | [diff] [blame] | 378 | * @return {!EventData} |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 379 | */ |
| 380 | _createEventData(serviceEvent) { |
Rayan Kanso | cc02ea3 | 2019-05-02 21:26:52 | [diff] [blame] | 381 | let swScope = ''; |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 382 | |
Rayan Kanso | cc02ea3 | 2019-05-02 21:26:52 | [diff] [blame] | 383 | // Try to get the scope of the Service Worker registration to be more user-friendly. |
Alex Rudenko | 60a3e94 | 2020-11-02 12:27:57 | [diff] [blame] | 384 | const registration = this._serviceWorkerManager ? |
| 385 | this._serviceWorkerManager.registrations().get(serviceEvent.serviceWorkerRegistrationId) : |
| 386 | undefined; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 387 | if (registration) { |
Rayan Kanso | cc02ea3 | 2019-05-02 21:26:52 | [diff] [blame] | 388 | swScope = registration.scopeURL.substr(registration.securityOrigin.length); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 389 | } |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 390 | |
| 391 | return { |
Rayan Kanso | c79a8bb | 2019-05-29 21:08:38 | [diff] [blame] | 392 | id: this._dataGrid.rootNode().children.length + 1, |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 393 | timestamp: UI.UIUtils.formatTimestamp(serviceEvent.timestamp * 1000, /* full= */ true), |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 394 | origin: serviceEvent.origin, |
Rayan Kanso | cc02ea3 | 2019-05-02 21:26:52 | [diff] [blame] | 395 | swScope, |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 396 | eventName: serviceEvent.eventName, |
| 397 | instanceId: serviceEvent.instanceId, |
| 398 | }; |
| 399 | } |
| 400 | |
| 401 | /** |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 402 | * Filtration function to know whether event should be shown or not. |
| 403 | * @param {!Protocol.BackgroundService.BackgroundServiceEvent} event |
| 404 | * @return {boolean} |
| 405 | */ |
| 406 | _acceptEvent(event) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 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 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 411 | if (this._originCheckbox.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); |
| 417 | |
| 418 | return this._securityOriginManager.securityOrigins().includes(origin); |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 419 | } |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 420 | |
| 421 | /** |
Rayan Kanso | 42c0f9e | 2019-09-02 12:21:09 | [diff] [blame] | 422 | * @return {!Element} |
| 423 | */ |
| 424 | _createLearnMoreLink() { |
| 425 | let url = |
| 426 | 'https://developers.google.com/web/tools/chrome-devtools/javascript/background-services?utm_source=devtools'; |
| 427 | |
| 428 | switch (this._serviceName) { |
| 429 | case Protocol.BackgroundService.ServiceName.BackgroundFetch: |
| 430 | url += '#fetch'; |
| 431 | break; |
| 432 | case Protocol.BackgroundService.ServiceName.BackgroundSync: |
| 433 | url += '#sync'; |
| 434 | break; |
| 435 | case Protocol.BackgroundService.ServiceName.PushMessaging: |
| 436 | url += '#push'; |
| 437 | break; |
| 438 | case Protocol.BackgroundService.ServiceName.Notifications: |
| 439 | url += '#notifications'; |
| 440 | break; |
| 441 | default: |
| 442 | break; |
| 443 | } |
| 444 | |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame^] | 445 | return UI.XLink.XLink.create(url, i18nString(UIStrings.learnMore)); |
Rayan Kanso | 42c0f9e | 2019-09-02 12:21:09 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /** |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 449 | * @param {?EventDataNode} dataNode |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 450 | */ |
| 451 | _showPreview(dataNode) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 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 | |
| 456 | this._selectedEventNode = dataNode; |
| 457 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 458 | if (this._preview) { |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 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 | |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 462 | if (this._selectedEventNode) { |
| 463 | this._preview = this._selectedEventNode.createPreview(); |
Rayan Kanso | 4476ca5 | 2019-07-22 11:51:24 | [diff] [blame] | 464 | this._preview.show(this._previewPanel.contentElement); |
| 465 | return; |
| 466 | } |
| 467 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 468 | this._preview = new UI.Widget.VBox(); |
Rayan Kanso | 4476ca5 | 2019-07-22 11:51:24 | [diff] [blame] | 469 | this._preview.contentElement.classList.add('background-service-preview', 'fill'); |
| 470 | const centered = this._preview.contentElement.createChild('div'); |
| 471 | |
| 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); |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 475 | } else if (this._recordButton.toggled()) { |
| 476 | // Inform users that we are recording/waiting for events. |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 477 | const featureName = BackgroundServiceView.getUIString(this._serviceName); |
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 { |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [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 | |
| 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 | |
| 497 | this._preview.show(this._previewPanel.contentElement); |
| 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 | */ |
| 503 | async _saveToFile() { |
Simon Zünd | 2c704cd | 2020-06-04 09:08:35 | [diff] [blame] | 504 | const fileName = `${this._serviceName}-${Platform.DateUtilities.toISO8601Compact(new Date())}.json`; |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 505 | const stream = new Bindings.FileUtils.FileOutputStream(); |
Rayan Kanso | b852ba8 | 2019-04-08 13:48:07 | [diff] [blame] | 506 | |
| 507 | const accepted = await stream.open(fileName); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 508 | if (!accepted) { |
Rayan Kanso | b852ba8 | 2019-04-08 13:48:07 | [diff] [blame] | 509 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 510 | } |
Rayan Kanso | b852ba8 | 2019-04-08 13:48:07 | [diff] [blame] | 511 | |
| 512 | const events = this._model.getEvents(this._serviceName).filter(event => this._acceptEvent(event)); |
| 513 | await stream.write(JSON.stringify(events, undefined, 2)); |
| 514 | stream.close(); |
| 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 | |
Alex Rudenko | 60a3e94 | 2020-11-02 12:27:57 | [diff] [blame] | 518 | /** |
| 519 | * @extends {DataGrid.DataGrid.DataGridNode<!Object<string, string|number>>} |
| 520 | */ |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 521 | export class EventDataNode extends DataGrid.DataGrid.DataGridNode { |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 522 | /** |
Alex Rudenko | 60a3e94 | 2020-11-02 12:27:57 | [diff] [blame] | 523 | * @param {!Object<string, string|number>} data |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 524 | * @param {!Array<!Protocol.BackgroundService.EventMetadata>} eventMetadata |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 525 | */ |
Rayan Kanso | aca06e7 | 2019-03-27 11:57:06 | [diff] [blame] | 526 | constructor(data, eventMetadata) { |
| 527 | super(data); |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 528 | |
| 529 | /** @const {!Array<!Protocol.BackgroundService.EventMetadata>} */ |
Jack Franklin | 056c400 | 2021-01-13 13:53:20 | [diff] [blame] | 530 | this._eventMetadata = eventMetadata.sort((m1, m2) => Platform.StringUtilities.compare(m1.key, m2.key)); |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /** |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 534 | * @return {!UI.Widget.VBox} |
Rayan Kanso | 3252d5e | 2019-03-27 11:37:24 | [diff] [blame] | 535 | */ |
Rayan Kanso | b451b4f | 2019-04-04 23:12:11 | [diff] [blame] | 536 | createPreview() { |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 537 | const preview = new UI.Widget.VBox(); |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 538 | preview.element.classList.add('background-service-metadata'); |
| 539 | |
| 540 | for (const entry of this._eventMetadata) { |
Tim van der Lippe | f49e232 | 2020-05-01 15:03:09 | [diff] [blame] | 541 | const div = document.createElement('div'); |
| 542 | div.classList.add('background-service-metadata-entry'); |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 543 | div.createChild('div', 'background-service-metadata-name').textContent = entry.key + ': '; |
Rayan Kanso | 4476ca5 | 2019-07-22 11:51:24 | [diff] [blame] | 544 | if (entry.value) { |
| 545 | div.createChild('div', 'background-service-metadata-value source-code').textContent = entry.value; |
| 546 | } else { |
| 547 | 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^] | 548 | i18nString(UIStrings.empty); |
Rayan Kanso | 4476ca5 | 2019-07-22 11:51:24 | [diff] [blame] | 549 | } |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 550 | preview.element.appendChild(div); |
| 551 | } |
| 552 | |
| 553 | if (!preview.element.children.length) { |
Tim van der Lippe | f49e232 | 2020-05-01 15:03:09 | [diff] [blame] | 554 | const div = document.createElement('div'); |
| 555 | div.classList.add('background-service-metadata-entry'); |
Vidal Guillermo Diazleal Ortega | 0a997bb | 2021-02-09 04:33:01 | [diff] [blame^] | 556 | div.createChild('div', 'background-service-metadata-name').textContent = |
| 557 | i18nString(UIStrings.noMetadataForThisEvent); |
Rayan Kanso | c0bfdd8 | 2019-04-24 12:32:22 | [diff] [blame] | 558 | preview.element.appendChild(div); |
| 559 | } |
| 560 | |
| 561 | return preview; |
Rayan Kanso | f40e315 | 2019-03-11 13:49:43 | [diff] [blame] | 562 | } |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 563 | } |
Rayan Kanso | 6156d22 | 2019-04-29 23:40:55 | [diff] [blame] | 564 | |
Andres Olivares | 7957851 | 2021-02-02 00:27:49 | [diff] [blame] | 565 | /** @type {!ActionDelegate} */ |
| 566 | let actionDelegateInstance; |
| 567 | |
Rayan Kanso | 6156d22 | 2019-04-29 23:40:55 | [diff] [blame] | 568 | /** |
Andres Olivares | 6ff6d04 | 2020-11-17 14:31:57 | [diff] [blame] | 569 | * @implements {UI.ActionRegistration.ActionDelegate} |
Rayan Kanso | 6156d22 | 2019-04-29 23:40:55 | [diff] [blame] | 570 | */ |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 571 | export class ActionDelegate { |
Rayan Kanso | 6156d22 | 2019-04-29 23:40:55 | [diff] [blame] | 572 | /** |
Andres Olivares | 7957851 | 2021-02-02 00:27:49 | [diff] [blame] | 573 | * @param {{forceNew: ?boolean}} opts |
| 574 | */ |
| 575 | static instance(opts = {forceNew: null}) { |
| 576 | const {forceNew} = opts; |
| 577 | if (!actionDelegateInstance || forceNew) { |
| 578 | actionDelegateInstance = new ActionDelegate(); |
| 579 | } |
| 580 | |
| 581 | return actionDelegateInstance; |
| 582 | } |
| 583 | |
| 584 | /** |
Rayan Kanso | 6156d22 | 2019-04-29 23:40:55 | [diff] [blame] | 585 | * @override |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 586 | * @param {!UI.Context.Context} context |
Rayan Kanso | 6156d22 | 2019-04-29 23:40:55 | [diff] [blame] | 587 | * @param {string} actionId |
| 588 | * @return {boolean} |
| 589 | */ |
| 590 | handleAction(context, actionId) { |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 591 | const view = context.flavor(BackgroundServiceView); |
Rayan Kanso | 6156d22 | 2019-04-29 23:40:55 | [diff] [blame] | 592 | switch (actionId) { |
Alex Rudenko | 60a3e94 | 2020-11-02 12:27:57 | [diff] [blame] | 593 | case 'background-service.toggle-recording': { |
| 594 | if (!view) { |
| 595 | throw new Error('BackgroundServiceView instance is missing'); |
| 596 | } |
Rayan Kanso | 6156d22 | 2019-04-29 23:40:55 | [diff] [blame] | 597 | view._toggleRecording(); |
| 598 | return true; |
Alex Rudenko | 60a3e94 | 2020-11-02 12:27:57 | [diff] [blame] | 599 | } |
Rayan Kanso | 6156d22 | 2019-04-29 23:40:55 | [diff] [blame] | 600 | } |
| 601 | return false; |
| 602 | } |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 603 | } |
Tim van der Lippe | ff5ad47 | 2020-02-17 12:35:09 | [diff] [blame] | 604 | |
| 605 | /** |
| 606 | * @typedef {!{isRecording: boolean, serviceName: !Protocol.BackgroundService.ServiceName}} |
| 607 | */ |
Alex Rudenko | 60a3e94 | 2020-11-02 12:27:57 | [diff] [blame] | 608 | // @ts-ignore typedef |
Tim van der Lippe | ff5ad47 | 2020-02-17 12:35:09 | [diff] [blame] | 609 | export let RecordingState; |
| 610 | |
| 611 | /** |
| 612 | * @typedef {{ |
| 613 | * id: number, |
| 614 | * timestamp: string, |
| 615 | * origin: string, |
| 616 | * swScope: string, |
| 617 | * eventName: string, |
| 618 | * instanceId: string, |
| 619 | * }} |
| 620 | */ |
Alex Rudenko | 60a3e94 | 2020-11-02 12:27:57 | [diff] [blame] | 621 | // @ts-ignore typedef |
Tim van der Lippe | ff5ad47 | 2020-02-17 12:35:09 | [diff] [blame] | 622 | export let EventData; |