Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 1 | // Copyright (c) 2015 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. |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 4 | |
Jan Scheffler | 52b1f35 | 2020-07-31 09:23:50 | [diff] [blame] | 5 | // @ts-nocheck |
| 6 | // TODO(crbug.com/1011811): Enable TypeScript compiler checks |
| 7 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 8 | import * as Common from '../common/common.js'; |
| 9 | import * as Components from '../components/components.js'; |
| 10 | import * as MobileThrottling from '../mobile_throttling/mobile_throttling.js'; |
| 11 | import * as SDK from '../sdk/sdk.js'; |
| 12 | import * as UI from '../ui/ui.js'; |
| 13 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 14 | /** |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 15 | * @implements {SDK.SDKModel.SDKModelObserver<!SDK.ServiceWorkerManager.ServiceWorkerManager>} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 16 | */ |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 17 | export class ServiceWorkersView extends UI.Widget.VBox { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 18 | constructor() { |
| 19 | super(true); |
| 20 | this.registerRequiredCSS('resources/serviceWorkersView.css'); |
| 21 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 22 | this._currentWorkersView = new UI.ReportView.ReportView(Common.UIString.UIString('Service Workers')); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 23 | this._currentWorkersView.setBodyScrollable(false); |
| 24 | this.contentElement.classList.add('service-worker-list'); |
| 25 | this._currentWorkersView.show(this.contentElement); |
| 26 | this._currentWorkersView.element.classList.add('service-workers-this-origin'); |
| 27 | |
| 28 | this._toolbar = this._currentWorkersView.createToolbar(); |
Erik Luo | 5e5a836 | 2018-05-31 23:43:22 | [diff] [blame] | 29 | this._toolbar.makeWrappable(true /* growVertically */); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 30 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 31 | /** @type {!Map<!SDK.ServiceWorkerManager.ServiceWorkerRegistration, !Section>} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 32 | this._sections = new Map(); |
Harley Li | 68cc13c | 2019-02-25 21:47:25 | [diff] [blame] | 33 | /** @type {symbol} */ |
| 34 | this._registrationSymbol = Symbol('Resources.ServiceWorkersView'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 35 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 36 | /** @type {?SDK.ServiceWorkerManager.ServiceWorkerManager} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 37 | this._manager = null; |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 38 | /** @type {?SDK.SecurityOriginManager.SecurityOriginManager} */ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 39 | this._securityOriginManager = null; |
| 40 | |
Nidhi Jaju | 1788bac | 2020-08-07 15:13:30 | [diff] [blame] | 41 | const othersDiv = this.contentElement.createChild('div', 'service-workers-other-origin'); |
| 42 | const othersView = new UI.ReportView.ReportView(); |
| 43 | othersView.setHeaderVisible(false); |
| 44 | othersView.show(othersDiv); |
| 45 | const othersSection = othersView.appendSection(Common.UIString.UIString('Service workers from other origins')); |
| 46 | const othersSectionRow = othersSection.appendRow(); |
| 47 | const seeOthers = UI.Fragment.html |
| 48 | `<a class="devtools-link" role="link" tabindex="0" href="chrome://serviceworker-internals" target="_blank" style="display: inline; cursor: pointer;">See all registrations</a>`; |
| 49 | self.onInvokeElement(seeOthers, event => { |
| 50 | const agent = SDK.SDKModel.TargetManager.instance().mainTarget().targetAgent(); |
| 51 | agent.invoke_createTarget({url: 'chrome://serviceworker-internals?devtools'}); |
| 52 | event.consume(true); |
| 53 | }); |
| 54 | othersSectionRow.appendChild(seeOthers); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 55 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 56 | this._toolbar.appendToolbarItem( |
| 57 | MobileThrottling.ThrottlingManager.throttlingManager().createOfflineToolbarCheckbox()); |
Paul Lewis | 2d7d65c | 2020-03-16 17:26:30 | [diff] [blame] | 58 | const updateOnReloadSetting = |
| 59 | Common.Settings.Settings.instance().createSetting('serviceWorkerUpdateOnReload', false); |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 60 | updateOnReloadSetting.setTitle(Common.UIString.UIString('Update on reload')); |
| 61 | const forceUpdate = new UI.Toolbar.ToolbarSettingCheckbox( |
Harley Li | 38795b3 | 2019-01-10 22:32:26 | [diff] [blame] | 62 | updateOnReloadSetting, ls`On page reload, force the service worker to update, and activate it`); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 63 | this._toolbar.appendToolbarItem(forceUpdate); |
Paul Lewis | 2d7d65c | 2020-03-16 17:26:30 | [diff] [blame] | 64 | const bypassServiceWorkerSetting = Common.Settings.Settings.instance().createSetting('bypassServiceWorker', false); |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 65 | bypassServiceWorkerSetting.setTitle(Common.UIString.UIString('Bypass for network')); |
| 66 | const fallbackToNetwork = new UI.Toolbar.ToolbarSettingCheckbox( |
Harley Li | 34d68fa | 2019-01-12 02:21:24 | [diff] [blame] | 67 | bypassServiceWorkerSetting, ls`Bypass the service worker and load resources from the network`); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 68 | this._toolbar.appendToolbarItem(fallbackToNetwork); |
| 69 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 70 | /** @type {!Map<!SDK.ServiceWorkerManager.ServiceWorkerManager, !Array<!Common.EventTarget.EventDescriptor>>}*/ |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 71 | this._eventListeners = new Map(); |
Paul Lewis | daac106 | 2020-03-05 14:37:10 | [diff] [blame] | 72 | SDK.SDKModel.TargetManager.instance().observeModels(SDK.ServiceWorkerManager.ServiceWorkerManager, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 73 | this._updateListVisibility(); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @override |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 78 | * @param {!SDK.ServiceWorkerManager.ServiceWorkerManager} serviceWorkerManager |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 79 | */ |
| 80 | modelAdded(serviceWorkerManager) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 81 | if (this._manager) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 82 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 83 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 84 | this._manager = serviceWorkerManager; |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 85 | this._securityOriginManager = serviceWorkerManager.target().model(SDK.SecurityOriginManager.SecurityOriginManager); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 86 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 87 | for (const registration of this._manager.registrations().values()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 88 | this._updateRegistration(registration); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 89 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 90 | |
| 91 | this._eventListeners.set(serviceWorkerManager, [ |
| 92 | this._manager.addEventListener( |
| 93 | SDK.ServiceWorkerManager.Events.RegistrationUpdated, this._registrationUpdated, this), |
| 94 | this._manager.addEventListener( |
| 95 | SDK.ServiceWorkerManager.Events.RegistrationDeleted, this._registrationDeleted, this), |
| 96 | this._securityOriginManager.addEventListener( |
| 97 | SDK.SecurityOriginManager.Events.SecurityOriginAdded, this._updateSectionVisibility, this), |
| 98 | this._securityOriginManager.addEventListener( |
| 99 | SDK.SecurityOriginManager.Events.SecurityOriginRemoved, this._updateSectionVisibility, this), |
| 100 | ]); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @override |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 105 | * @param {!SDK.ServiceWorkerManager.ServiceWorkerManager} serviceWorkerManager |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 106 | */ |
| 107 | modelRemoved(serviceWorkerManager) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 108 | if (!this._manager || this._manager !== serviceWorkerManager) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 109 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 110 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 111 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 112 | Common.EventTarget.EventTarget.removeEventListeners(this._eventListeners.get(serviceWorkerManager)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 113 | this._eventListeners.delete(serviceWorkerManager); |
| 114 | this._manager = null; |
| 115 | this._securityOriginManager = null; |
| 116 | } |
| 117 | |
Harley Li | 68cc13c | 2019-02-25 21:47:25 | [diff] [blame] | 118 | |
| 119 | /** |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 120 | * @param {!SDK.ServiceWorkerManager.ServiceWorkerRegistration} registration |
Harley Li | 68cc13c | 2019-02-25 21:47:25 | [diff] [blame] | 121 | * @return {number} |
| 122 | */ |
| 123 | _getTimeStamp(registration) { |
| 124 | const versions = registration.versionsByMode(); |
| 125 | |
Sigurd Schneider | a38b867 | 2020-05-20 11:31:45 | [diff] [blame] | 126 | /** @type {number|undefined} */ |
Harley Li | 68cc13c | 2019-02-25 21:47:25 | [diff] [blame] | 127 | let timestamp = 0; |
| 128 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 129 | const active = versions.get(SDK.ServiceWorkerManager.ServiceWorkerVersion.Modes.Active); |
| 130 | const installing = versions.get(SDK.ServiceWorkerManager.ServiceWorkerVersion.Modes.Installing); |
| 131 | const waiting = versions.get(SDK.ServiceWorkerManager.ServiceWorkerVersion.Modes.Waiting); |
| 132 | const redundant = versions.get(SDK.ServiceWorkerManager.ServiceWorkerVersion.Modes.Redundant); |
Harley Li | 68cc13c | 2019-02-25 21:47:25 | [diff] [blame] | 133 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 134 | if (active) { |
Harley Li | 68cc13c | 2019-02-25 21:47:25 | [diff] [blame] | 135 | timestamp = active.scriptResponseTime; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 136 | } else if (waiting) { |
Harley Li | 68cc13c | 2019-02-25 21:47:25 | [diff] [blame] | 137 | timestamp = waiting.scriptResponseTime; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 138 | } else if (installing) { |
Harley Li | 68cc13c | 2019-02-25 21:47:25 | [diff] [blame] | 139 | timestamp = installing.scriptResponseTime; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 140 | } else if (redundant) { |
Harley Li | 68cc13c | 2019-02-25 21:47:25 | [diff] [blame] | 141 | timestamp = redundant.scriptResponseTime; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 142 | } |
Harley Li | 68cc13c | 2019-02-25 21:47:25 | [diff] [blame] | 143 | |
Sigurd Schneider | a38b867 | 2020-05-20 11:31:45 | [diff] [blame] | 144 | return timestamp || 0; |
Harley Li | 68cc13c | 2019-02-25 21:47:25 | [diff] [blame] | 145 | } |
| 146 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 147 | _updateSectionVisibility() { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 148 | let hasThis = false; |
| 149 | const movedSections = []; |
| 150 | for (const section of this._sections.values()) { |
| 151 | const expectedView = this._getReportViewForOrigin(section._registration.securityOrigin); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 152 | hasThis |= expectedView === this._currentWorkersView; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 153 | if (section._section.parentWidget() !== expectedView) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 154 | movedSections.push(section); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 155 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | for (const section of movedSections) { |
| 159 | const registration = section._registration; |
| 160 | this._removeRegistrationFromList(registration); |
| 161 | this._updateRegistration(registration, true); |
| 162 | } |
| 163 | |
Harley Li | 68cc13c | 2019-02-25 21:47:25 | [diff] [blame] | 164 | this._currentWorkersView.sortSections((a, b) => { |
| 165 | const aTimestamp = this._getTimeStamp(a[this._registrationSymbol]); |
| 166 | const bTimestamp = this._getTimeStamp(b[this._registrationSymbol]); |
| 167 | // the newest (largest timestamp value) should be the first |
| 168 | return bTimestamp - aTimestamp; |
| 169 | }); |
| 170 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 171 | for (const section of this._sections.values()) { |
| 172 | if (section._section.parentWidget() === this._currentWorkersView || |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 173 | this._isRegistrationVisible(section._registration)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 174 | section._section.showWidget(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 175 | } else { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 176 | section._section.hideWidget(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 177 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 178 | } |
Tim van der Lippe | ffa7862 | 2019-09-16 12:07:12 | [diff] [blame] | 179 | this.contentElement.classList.toggle('service-worker-has-current', !!hasThis); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 180 | this._updateListVisibility(); |
| 181 | } |
| 182 | |
| 183 | /** |
Tim van der Lippe | c02a97c | 2020-02-14 14:39:27 | [diff] [blame] | 184 | * @param {!Common.EventTarget.EventTargetEvent} event |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 185 | */ |
| 186 | _registrationUpdated(event) { |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 187 | const registration = /** @type {!SDK.ServiceWorkerManager.ServiceWorkerRegistration} */ (event.data); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 188 | this._updateRegistration(registration); |
| 189 | this._gcRegistrations(); |
| 190 | } |
| 191 | |
| 192 | _gcRegistrations() { |
| 193 | let hasNonDeletedRegistrations = false; |
| 194 | const securityOrigins = new Set(this._securityOriginManager.securityOrigins()); |
| 195 | for (const registration of this._manager.registrations().values()) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 196 | if (!securityOrigins.has(registration.securityOrigin) && !this._isRegistrationVisible(registration)) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 197 | continue; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 198 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 199 | if (!registration.canBeRemoved()) { |
| 200 | hasNonDeletedRegistrations = true; |
| 201 | break; |
| 202 | } |
| 203 | } |
| 204 | |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 205 | if (!hasNonDeletedRegistrations) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 206 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 207 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 208 | |
| 209 | for (const registration of this._manager.registrations().values()) { |
| 210 | const visible = securityOrigins.has(registration.securityOrigin) || this._isRegistrationVisible(registration); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 211 | if (!visible && registration.canBeRemoved()) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 212 | this._removeRegistrationFromList(registration); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 213 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * @param {string} origin |
Nidhi Jaju | a3e34a8 | 2020-08-07 14:46:21 | [diff] [blame] | 219 | * @return {?UI.ReportView.ReportView} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 220 | */ |
| 221 | _getReportViewForOrigin(origin) { |
Harley Li | ddf2b68 | 2019-03-08 22:35:23 | [diff] [blame] | 222 | if (this._securityOriginManager.securityOrigins().includes(origin) || |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 223 | this._securityOriginManager.unreachableMainSecurityOrigin() === origin) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 224 | return this._currentWorkersView; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 225 | } |
Nidhi Jaju | a3e34a8 | 2020-08-07 14:46:21 | [diff] [blame] | 226 | return null; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /** |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 230 | * @param {!SDK.ServiceWorkerManager.ServiceWorkerRegistration} registration |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 231 | * @param {boolean=} skipUpdate |
| 232 | */ |
| 233 | _updateRegistration(registration, skipUpdate) { |
| 234 | let section = this._sections.get(registration); |
| 235 | if (!section) { |
Harley Li | 09a5871 | 2019-06-07 19:25:54 | [diff] [blame] | 236 | const title = registration.scopeURL; |
Nidhi Jaju | a3e34a8 | 2020-08-07 14:46:21 | [diff] [blame] | 237 | const reportView = this._getReportViewForOrigin(registration.securityOrigin); |
| 238 | if (!reportView) { |
| 239 | return; |
| 240 | } |
| 241 | const uiSection = reportView.appendSection(title); |
Rob Paveza | cb83218 | 2019-10-16 23:13:00 | [diff] [blame] | 242 | uiSection.setUiGroupTitle(ls`Service worker for ${title}`); |
Harley Li | 68cc13c | 2019-02-25 21:47:25 | [diff] [blame] | 243 | uiSection[this._registrationSymbol] = registration; |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 244 | section = new Section( |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 245 | /** @type {!SDK.ServiceWorkerManager.ServiceWorkerManager} */ (this._manager), uiSection, registration); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 246 | this._sections.set(registration, section); |
| 247 | } |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 248 | if (skipUpdate) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 249 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 250 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 251 | this._updateSectionVisibility(); |
| 252 | section._scheduleUpdate(); |
| 253 | } |
| 254 | |
| 255 | /** |
Tim van der Lippe | c02a97c | 2020-02-14 14:39:27 | [diff] [blame] | 256 | * @param {!Common.EventTarget.EventTargetEvent} event |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 257 | */ |
| 258 | _registrationDeleted(event) { |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 259 | const registration = /** @type {!SDK.ServiceWorkerManager.ServiceWorkerRegistration} */ (event.data); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 260 | this._removeRegistrationFromList(registration); |
| 261 | } |
| 262 | |
| 263 | /** |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 264 | * @param {!SDK.ServiceWorkerManager.ServiceWorkerRegistration} registration |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 265 | */ |
| 266 | _removeRegistrationFromList(registration) { |
| 267 | const section = this._sections.get(registration); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 268 | if (section) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 269 | section._section.detach(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 270 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 271 | this._sections.delete(registration); |
| 272 | this._updateSectionVisibility(); |
| 273 | } |
| 274 | |
| 275 | /** |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 276 | * @param {!SDK.ServiceWorkerManager.ServiceWorkerRegistration} registration |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 277 | * @return {boolean} |
| 278 | */ |
| 279 | _isRegistrationVisible(registration) { |
Nidhi Jaju | a3e34a8 | 2020-08-07 14:46:21 | [diff] [blame] | 280 | if (!registration.scopeURL) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 281 | return true; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 282 | } |
Nidhi Jaju | a3e34a8 | 2020-08-07 14:46:21 | [diff] [blame] | 283 | return false; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 284 | } |
| 285 | |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 286 | _updateListVisibility() { |
| 287 | this.contentElement.classList.toggle('service-worker-list-empty', this._sections.size === 0); |
| 288 | } |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 289 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 290 | |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 291 | export class Section { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 292 | /** |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 293 | * @param {!SDK.ServiceWorkerManager.ServiceWorkerManager} manager |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 294 | * @param {!UI.ReportView.Section} section |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 295 | * @param {!SDK.ServiceWorkerManager.ServiceWorkerRegistration} registration |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 296 | */ |
| 297 | constructor(manager, section, registration) { |
| 298 | this._manager = manager; |
| 299 | this._section = section; |
| 300 | this._registration = registration; |
| 301 | /** @type {?symbol} */ |
| 302 | this._fingerprint = null; |
Paul Lewis | 2d7d65c | 2020-03-16 17:26:30 | [diff] [blame] | 303 | this._pushNotificationDataSetting = Common.Settings.Settings.instance().createLocalSetting( |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 304 | 'pushData', Common.UIString.UIString('Test push message from DevTools.')); |
Paul Lewis | 2d7d65c | 2020-03-16 17:26:30 | [diff] [blame] | 305 | this._syncTagNameSetting = |
| 306 | Common.Settings.Settings.instance().createLocalSetting('syncTagName', 'test-tag-from-devtools'); |
Mugdha Lakhani | db7c9b1 | 2019-08-09 13:48:04 | [diff] [blame] | 307 | this._periodicSyncTagNameSetting = |
Paul Lewis | 2d7d65c | 2020-03-16 17:26:30 | [diff] [blame] | 308 | Common.Settings.Settings.instance().createLocalSetting('periodicSyncTagName', 'test-tag-from-devtools'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 309 | |
| 310 | this._toolbar = section.createToolbar(); |
| 311 | this._toolbar.renderAsLinks(); |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 312 | this._updateButton = |
| 313 | new UI.Toolbar.ToolbarButton(Common.UIString.UIString('Update'), undefined, Common.UIString.UIString('Update')); |
| 314 | this._updateButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, this._updateButtonClicked, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 315 | this._toolbar.appendToolbarItem(this._updateButton); |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 316 | this._deleteButton = new UI.Toolbar.ToolbarButton( |
| 317 | Common.UIString.UIString('Unregister service worker'), undefined, Common.UIString.UIString('Unregister')); |
| 318 | this._deleteButton.addEventListener(UI.Toolbar.ToolbarButton.Events.Click, this._unregisterButtonClicked, this); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 319 | this._toolbar.appendToolbarItem(this._deleteButton); |
| 320 | |
| 321 | // Preserve the order. |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 322 | this._sourceField = this._wrapWidget(this._section.appendField(Common.UIString.UIString('Source'))); |
| 323 | this._statusField = this._wrapWidget(this._section.appendField(Common.UIString.UIString('Status'))); |
| 324 | this._clientsField = this._wrapWidget(this._section.appendField(Common.UIString.UIString('Clients'))); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 325 | this._createSyncNotificationField( |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 326 | Common.UIString.UIString('Push'), this._pushNotificationDataSetting.get(), |
| 327 | Common.UIString.UIString('Push data'), this._push.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 328 | this._createSyncNotificationField( |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 329 | Common.UIString.UIString('Sync'), this._syncTagNameSetting.get(), Common.UIString.UIString('Sync tag'), |
| 330 | this._sync.bind(this)); |
Mugdha Lakhani | 71b51fa | 2020-01-02 15:04:58 | [diff] [blame] | 331 | this._createSyncNotificationField( |
| 332 | ls`Periodic Sync`, this._periodicSyncTagNameSetting.get(), ls`Periodic Sync tag`, |
| 333 | tag => this._periodicSync(tag)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 334 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 335 | this._linkifier = new Components.Linkifier.Linkifier(); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 336 | /** @type {!Map<string, !Protocol.Target.TargetInfo>} */ |
| 337 | this._clientInfoCache = new Map(); |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 338 | this._throttler = new Common.Throttler.Throttler(500); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /** |
| 342 | * @param {string} label |
| 343 | * @param {string} initialValue |
| 344 | * @param {string} placeholder |
| 345 | * @param {function(string)} callback |
| 346 | */ |
| 347 | _createSyncNotificationField(label, initialValue, placeholder, callback) { |
| 348 | const form = |
| 349 | this._wrapWidget(this._section.appendField(label)).createChild('form', 'service-worker-editor-with-button'); |
Wolfgang Beyer | 83ae101 | 2020-09-28 09:10:43 | [diff] [blame] | 350 | const editor = UI.UIUtils.createInput('source-code service-worker-notification-editor'); |
| 351 | form.appendChild(editor); |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 352 | const button = UI.UIUtils.createTextButton(label); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 353 | button.type = 'submit'; |
| 354 | form.appendChild(button); |
| 355 | |
| 356 | editor.value = initialValue; |
| 357 | editor.placeholder = placeholder; |
Junyi Xiao | d3e71a4 | 2019-04-23 04:49:04 | [diff] [blame] | 358 | UI.ARIAUtils.setAccessibleName(editor, label); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 359 | |
| 360 | form.addEventListener('submit', e => { |
| 361 | callback(editor.value || ''); |
| 362 | e.consume(true); |
| 363 | }); |
| 364 | } |
| 365 | |
| 366 | _scheduleUpdate() { |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 367 | if (ServiceWorkersView._noThrottle) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 368 | this._update(); |
| 369 | return; |
| 370 | } |
| 371 | this._throttler.schedule(this._update.bind(this)); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * @param {string} versionId |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 376 | * @return {?SDK.SDKModel.Target} |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 377 | */ |
| 378 | _targetForVersionId(versionId) { |
| 379 | const version = this._manager.findVersion(versionId); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 380 | if (!version || !version.targetId) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 381 | return null; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 382 | } |
Paul Lewis | daac106 | 2020-03-05 14:37:10 | [diff] [blame] | 383 | return SDK.SDKModel.TargetManager.instance().targetById(version.targetId); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /** |
| 387 | * @param {!Element} versionsStack |
| 388 | * @param {string} icon |
| 389 | * @param {string} label |
| 390 | * @return {!Element} |
| 391 | */ |
| 392 | _addVersion(versionsStack, icon, label) { |
| 393 | const installingEntry = versionsStack.createChild('div', 'service-worker-version'); |
| 394 | installingEntry.createChild('div', icon); |
Michael Liao | feb4151 | 2020-08-20 22:45:41 | [diff] [blame] | 395 | const statusString = installingEntry.createChild('span', 'service-worker-version-string'); |
| 396 | statusString.textContent = label; |
| 397 | UI.ARIAUtils.markAsAlert(statusString); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 398 | return installingEntry; |
| 399 | } |
| 400 | |
| 401 | /** |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 402 | * @param {!SDK.ServiceWorkerManager.ServiceWorkerVersion} version |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 403 | */ |
| 404 | _updateClientsField(version) { |
| 405 | this._clientsField.removeChildren(); |
Sigurd Schneider | a38b867 | 2020-05-20 11:31:45 | [diff] [blame] | 406 | this._section.setFieldVisible(Common.UIString.UIString('Clients'), !!version.controlledClients.length); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 407 | for (const client of version.controlledClients) { |
| 408 | const clientLabelText = this._clientsField.createChild('div', 'service-worker-client'); |
| 409 | if (this._clientInfoCache.has(client)) { |
| 410 | this._updateClientInfo( |
| 411 | clientLabelText, /** @type {!Protocol.Target.TargetInfo} */ (this._clientInfoCache.get(client))); |
| 412 | } |
| 413 | this._manager.target().targetAgent().getTargetInfo(client).then(this._onClientInfo.bind(this, clientLabelText)); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | /** |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 418 | * @param {!SDK.ServiceWorkerManager.ServiceWorkerVersion} version |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 419 | */ |
| 420 | _updateSourceField(version) { |
| 421 | this._sourceField.removeChildren(); |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 422 | const fileName = Common.ParsedURL.ParsedURL.extractName(version.scriptURL); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 423 | const name = this._sourceField.createChild('div', 'report-field-value-filename'); |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 424 | const link = Components.Linkifier.Linkifier.linkifyURL(version.scriptURL, {text: fileName}); |
Junyi Xiao | d3e71a4 | 2019-04-23 04:49:04 | [diff] [blame] | 425 | link.tabIndex = 0; |
| 426 | name.appendChild(link); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 427 | if (this._registration.errors.length) { |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 428 | const errorsLabel = UI.UIUtils.createIconLabel(String(this._registration.errors.length), 'smallicon-error'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 429 | errorsLabel.classList.add('link'); |
Junyi Xiao | d3e71a4 | 2019-04-23 04:49:04 | [diff] [blame] | 430 | errorsLabel.tabIndex = 0; |
| 431 | UI.ARIAUtils.setAccessibleName(errorsLabel, ls`${this._registration.errors.length} registration errors`); |
Paul Lewis | a83ea61 | 2020-03-04 13:01:36 | [diff] [blame] | 432 | self.onInvokeElement(errorsLabel, () => Common.Console.Console.instance().show()); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 433 | name.appendChild(errorsLabel); |
| 434 | } |
John Abd-El-Malek | 7fa90c8 | 2018-08-27 18:39:55 | [diff] [blame] | 435 | this._sourceField.createChild('div', 'report-field-value-subtitle').textContent = |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 436 | Common.UIString.UIString('Received %s', new Date(version.scriptResponseTime * 1000).toLocaleString()); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /** |
| 440 | * @return {!Promise} |
| 441 | */ |
| 442 | _update() { |
| 443 | const fingerprint = this._registration.fingerprint(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 444 | if (fingerprint === this._fingerprint) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 445 | return Promise.resolve(); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 446 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 447 | this._fingerprint = fingerprint; |
| 448 | |
| 449 | this._toolbar.setEnabled(!this._registration.isDeleted); |
| 450 | |
| 451 | const versions = this._registration.versionsByMode(); |
Harley Li | 09a5871 | 2019-06-07 19:25:54 | [diff] [blame] | 452 | const scopeURL = this._registration.scopeURL; |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 453 | const title = this._registration.isDeleted ? Common.UIString.UIString('%s - deleted', scopeURL) : scopeURL; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 454 | this._section.setTitle(title); |
| 455 | |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 456 | const active = versions.get(SDK.ServiceWorkerManager.ServiceWorkerVersion.Modes.Active); |
| 457 | const waiting = versions.get(SDK.ServiceWorkerManager.ServiceWorkerVersion.Modes.Waiting); |
| 458 | const installing = versions.get(SDK.ServiceWorkerManager.ServiceWorkerVersion.Modes.Installing); |
| 459 | const redundant = versions.get(SDK.ServiceWorkerManager.ServiceWorkerVersion.Modes.Redundant); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 460 | |
| 461 | this._statusField.removeChildren(); |
| 462 | const versionsStack = this._statusField.createChild('div', 'service-worker-version-stack'); |
| 463 | versionsStack.createChild('div', 'service-worker-version-stack-bar'); |
| 464 | |
| 465 | if (active) { |
| 466 | this._updateSourceField(active); |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 467 | const localizedRunningStatus = SDK.ServiceWorkerManager.ServiceWorkerVersion.RunningStatus[active.runningStatus]; |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 468 | const activeEntry = this._addVersion( |
Mandy Chen | 9c1200c | 2019-09-24 18:36:01 | [diff] [blame] | 469 | versionsStack, 'service-worker-active-circle', ls`#${active.id} activated and is ${localizedRunningStatus}`); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 470 | |
| 471 | if (active.isRunning() || active.isStarting()) { |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 472 | this._createLink(activeEntry, Common.UIString.UIString('stop'), this._stopButtonClicked.bind(this, active.id)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 473 | if (!this._targetForVersionId(active.id)) { |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 474 | this._createLink( |
| 475 | activeEntry, Common.UIString.UIString('inspect'), this._inspectButtonClicked.bind(this, active.id)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 476 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 477 | } else if (active.isStartable()) { |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 478 | this._createLink(activeEntry, Common.UIString.UIString('start'), this._startButtonClicked.bind(this)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 479 | } |
| 480 | this._updateClientsField(active); |
| 481 | } else if (redundant) { |
| 482 | this._updateSourceField(redundant); |
| 483 | this._addVersion( |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 484 | versionsStack, 'service-worker-redundant-circle', Common.UIString.UIString('#%s is redundant', redundant.id)); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 485 | this._updateClientsField(redundant); |
| 486 | } |
| 487 | |
| 488 | if (waiting) { |
| 489 | const waitingEntry = this._addVersion( |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 490 | versionsStack, 'service-worker-waiting-circle', |
| 491 | Common.UIString.UIString('#%s waiting to activate', waiting.id)); |
| 492 | this._createLink(waitingEntry, Common.UIString.UIString('skipWaiting'), this._skipButtonClicked.bind(this)); |
John Abd-El-Malek | 7fa90c8 | 2018-08-27 18:39:55 | [diff] [blame] | 493 | waitingEntry.createChild('div', 'service-worker-subtitle').textContent = |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 494 | Common.UIString.UIString('Received %s', new Date(waiting.scriptResponseTime * 1000).toLocaleString()); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 495 | if (!this._targetForVersionId(waiting.id) && (waiting.isRunning() || waiting.isStarting())) { |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 496 | this._createLink( |
| 497 | waitingEntry, Common.UIString.UIString('inspect'), this._inspectButtonClicked.bind(this, waiting.id)); |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 498 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 499 | } |
| 500 | if (installing) { |
| 501 | const installingEntry = this._addVersion( |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 502 | versionsStack, 'service-worker-installing-circle', |
| 503 | Common.UIString.UIString('#%s trying to install', installing.id)); |
John Abd-El-Malek | 7fa90c8 | 2018-08-27 18:39:55 | [diff] [blame] | 504 | installingEntry.createChild('div', 'service-worker-subtitle').textContent = |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 505 | Common.UIString.UIString('Received %s', new Date(installing.scriptResponseTime * 1000).toLocaleString()); |
Junyi Xiao | d3e71a4 | 2019-04-23 04:49:04 | [diff] [blame] | 506 | if (!this._targetForVersionId(installing.id) && (installing.isRunning() || installing.isStarting())) { |
| 507 | this._createLink( |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 508 | installingEntry, Common.UIString.UIString('inspect'), this._inspectButtonClicked.bind(this, installing.id)); |
Junyi Xiao | d3e71a4 | 2019-04-23 04:49:04 | [diff] [blame] | 509 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 510 | } |
| 511 | return Promise.resolve(); |
| 512 | } |
| 513 | |
| 514 | /** |
Junyi Xiao | d3e71a4 | 2019-04-23 04:49:04 | [diff] [blame] | 515 | * @param {!Element} parent |
| 516 | * @param {string} title |
| 517 | * @param {function()} listener |
| 518 | * @param {string=} className |
| 519 | * @param {boolean=} useCapture |
| 520 | * @return {!Element} |
| 521 | */ |
| 522 | _createLink(parent, title, listener, className, useCapture) { |
| 523 | const button = parent.createChild('button', className); |
Wolfgang Beyer | 83ae101 | 2020-09-28 09:10:43 | [diff] [blame] | 524 | button.classList.add('link', 'devtools-link'); |
Junyi Xiao | d3e71a4 | 2019-04-23 04:49:04 | [diff] [blame] | 525 | button.textContent = title; |
| 526 | button.tabIndex = 0; |
| 527 | button.addEventListener('click', listener, useCapture); |
| 528 | return button; |
| 529 | } |
| 530 | |
| 531 | /** |
Tim van der Lippe | c02a97c | 2020-02-14 14:39:27 | [diff] [blame] | 532 | * @param {!Common.EventTarget.EventTargetEvent} event |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 533 | */ |
| 534 | _unregisterButtonClicked(event) { |
| 535 | this._manager.deleteRegistration(this._registration.id); |
| 536 | } |
| 537 | |
| 538 | /** |
Tim van der Lippe | c02a97c | 2020-02-14 14:39:27 | [diff] [blame] | 539 | * @param {!Common.EventTarget.EventTargetEvent} event |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 540 | */ |
| 541 | _updateButtonClicked(event) { |
| 542 | this._manager.updateRegistration(this._registration.id); |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * @param {string} data |
| 547 | */ |
| 548 | _push(data) { |
| 549 | this._pushNotificationDataSetting.set(data); |
| 550 | this._manager.deliverPushMessage(this._registration.id, data); |
| 551 | } |
| 552 | |
| 553 | /** |
| 554 | * @param {string} tag |
| 555 | */ |
| 556 | _sync(tag) { |
| 557 | this._syncTagNameSetting.set(tag); |
| 558 | this._manager.dispatchSyncEvent(this._registration.id, tag, true); |
| 559 | } |
| 560 | |
| 561 | /** |
Mugdha Lakhani | db7c9b1 | 2019-08-09 13:48:04 | [diff] [blame] | 562 | * @param {string} tag |
| 563 | */ |
| 564 | _periodicSync(tag) { |
| 565 | this._periodicSyncTagNameSetting.set(tag); |
| 566 | this._manager.dispatchPeriodicSyncEvent(this._registration.id, tag); |
| 567 | } |
| 568 | |
| 569 | /** |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 570 | * @param {!Element} element |
| 571 | * @param {?Protocol.Target.TargetInfo} targetInfo |
| 572 | */ |
| 573 | _onClientInfo(element, targetInfo) { |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 574 | if (!targetInfo) { |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 575 | return; |
Tim van der Lippe | 1d6e57a | 2019-09-30 11:55:34 | [diff] [blame] | 576 | } |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 577 | this._clientInfoCache.set(targetInfo.targetId, targetInfo); |
| 578 | this._updateClientInfo(element, targetInfo); |
| 579 | } |
| 580 | |
| 581 | /** |
| 582 | * @param {!Element} element |
| 583 | * @param {!Protocol.Target.TargetInfo} targetInfo |
| 584 | */ |
| 585 | _updateClientInfo(element, targetInfo) { |
| 586 | if (targetInfo.type !== 'page' && targetInfo.type === 'iframe') { |
Harley Li | a8a622f | 2018-11-01 23:58:09 | [diff] [blame] | 587 | const clientString = element.createChild('span', 'service-worker-client-string'); |
Sigurd Schneider | 23c5297 | 2020-10-13 09:31:14 | [diff] [blame^] | 588 | UI.UIUtils.createTextChild(clientString, ls`Worker: ${targetInfo.url}`); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 589 | return; |
| 590 | } |
| 591 | element.removeChildren(); |
Harley Li | a8a622f | 2018-11-01 23:58:09 | [diff] [blame] | 592 | const clientString = element.createChild('span', 'service-worker-client-string'); |
Sigurd Schneider | 23c5297 | 2020-10-13 09:31:14 | [diff] [blame^] | 593 | UI.UIUtils.createTextChild(clientString, targetInfo.url); |
Junyi Xiao | d3e71a4 | 2019-04-23 04:49:04 | [diff] [blame] | 594 | this._createLink( |
| 595 | element, ls`focus`, this._activateTarget.bind(this, targetInfo.targetId), 'service-worker-client-focus-link'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 596 | } |
| 597 | |
| 598 | /** |
| 599 | * @param {string} targetId |
| 600 | */ |
| 601 | _activateTarget(targetId) { |
| 602 | this._manager.target().targetAgent().activateTarget(targetId); |
| 603 | } |
| 604 | |
| 605 | _startButtonClicked() { |
| 606 | this._manager.startWorker(this._registration.scopeURL); |
| 607 | } |
| 608 | |
| 609 | _skipButtonClicked() { |
| 610 | this._manager.skipWaiting(this._registration.scopeURL); |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * @param {string} versionId |
| 615 | */ |
| 616 | _stopButtonClicked(versionId) { |
| 617 | this._manager.stopWorker(versionId); |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * @param {string} versionId |
| 622 | */ |
| 623 | _inspectButtonClicked(versionId) { |
| 624 | this._manager.inspectWorker(versionId); |
| 625 | } |
| 626 | |
| 627 | /** |
| 628 | * @param {!Element} container |
| 629 | * @return {!Element} |
| 630 | */ |
| 631 | _wrapWidget(container) { |
Tim van der Lippe | 0efccf0 | 2020-02-12 15:15:39 | [diff] [blame] | 632 | const shadowRoot = UI.Utils.createShadowRootWithCoreStyles(container); |
| 633 | UI.Utils.appendStyle(shadowRoot, 'resources/serviceWorkersView.css'); |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 634 | const contentElement = createElement('div'); |
| 635 | shadowRoot.appendChild(contentElement); |
| 636 | return contentElement; |
| 637 | } |
Tim van der Lippe | 097cdec | 2020-01-06 14:44:17 | [diff] [blame] | 638 | } |