blob: 73f09cf5d043e8cac2bc69f14f95899d9b5aeddd [file] [log] [blame]
Blink Reformat4c46d092018-04-07 15:32:371// 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 Lippe097cdec2020-01-06 14:44:174
Jan Scheffler52b1f352020-07-31 09:23:505// @ts-nocheck
6// TODO(crbug.com/1011811): Enable TypeScript compiler checks
7
Tim van der Lippe0efccf02020-02-12 15:15:398import * as Common from '../common/common.js';
9import * as Components from '../components/components.js';
10import * as MobileThrottling from '../mobile_throttling/mobile_throttling.js';
11import * as SDK from '../sdk/sdk.js';
12import * as UI from '../ui/ui.js';
13
Blink Reformat4c46d092018-04-07 15:32:3714/**
Tim van der Lippe0efccf02020-02-12 15:15:3915 * @implements {SDK.SDKModel.SDKModelObserver<!SDK.ServiceWorkerManager.ServiceWorkerManager>}
Blink Reformat4c46d092018-04-07 15:32:3716 */
Tim van der Lippe0efccf02020-02-12 15:15:3917export class ServiceWorkersView extends UI.Widget.VBox {
Blink Reformat4c46d092018-04-07 15:32:3718 constructor() {
19 super(true);
20 this.registerRequiredCSS('resources/serviceWorkersView.css');
21
Tim van der Lippe0efccf02020-02-12 15:15:3922 this._currentWorkersView = new UI.ReportView.ReportView(Common.UIString.UIString('Service Workers'));
Blink Reformat4c46d092018-04-07 15:32:3723 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 Luo5e5a8362018-05-31 23:43:2229 this._toolbar.makeWrappable(true /* growVertically */);
Blink Reformat4c46d092018-04-07 15:32:3730
Tim van der Lippe0efccf02020-02-12 15:15:3931 /** @type {!Map<!SDK.ServiceWorkerManager.ServiceWorkerRegistration, !Section>} */
Blink Reformat4c46d092018-04-07 15:32:3732 this._sections = new Map();
Harley Li68cc13c2019-02-25 21:47:2533 /** @type {symbol} */
34 this._registrationSymbol = Symbol('Resources.ServiceWorkersView');
Blink Reformat4c46d092018-04-07 15:32:3735
Tim van der Lippe0efccf02020-02-12 15:15:3936 /** @type {?SDK.ServiceWorkerManager.ServiceWorkerManager} */
Blink Reformat4c46d092018-04-07 15:32:3737 this._manager = null;
Tim van der Lippe0efccf02020-02-12 15:15:3938 /** @type {?SDK.SecurityOriginManager.SecurityOriginManager} */
Blink Reformat4c46d092018-04-07 15:32:3739 this._securityOriginManager = null;
40
Nidhi Jaju1788bac2020-08-07 15:13:3041 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 Reformat4c46d092018-04-07 15:32:3755
Tim van der Lippe0efccf02020-02-12 15:15:3956 this._toolbar.appendToolbarItem(
57 MobileThrottling.ThrottlingManager.throttlingManager().createOfflineToolbarCheckbox());
Paul Lewis2d7d65c2020-03-16 17:26:3058 const updateOnReloadSetting =
59 Common.Settings.Settings.instance().createSetting('serviceWorkerUpdateOnReload', false);
Tim van der Lippe0efccf02020-02-12 15:15:3960 updateOnReloadSetting.setTitle(Common.UIString.UIString('Update on reload'));
61 const forceUpdate = new UI.Toolbar.ToolbarSettingCheckbox(
Harley Li38795b32019-01-10 22:32:2662 updateOnReloadSetting, ls`On page reload, force the service worker to update, and activate it`);
Blink Reformat4c46d092018-04-07 15:32:3763 this._toolbar.appendToolbarItem(forceUpdate);
Paul Lewis2d7d65c2020-03-16 17:26:3064 const bypassServiceWorkerSetting = Common.Settings.Settings.instance().createSetting('bypassServiceWorker', false);
Tim van der Lippe0efccf02020-02-12 15:15:3965 bypassServiceWorkerSetting.setTitle(Common.UIString.UIString('Bypass for network'));
66 const fallbackToNetwork = new UI.Toolbar.ToolbarSettingCheckbox(
Harley Li34d68fa2019-01-12 02:21:2467 bypassServiceWorkerSetting, ls`Bypass the service worker and load resources from the network`);
Blink Reformat4c46d092018-04-07 15:32:3768 this._toolbar.appendToolbarItem(fallbackToNetwork);
69
Tim van der Lippe0efccf02020-02-12 15:15:3970 /** @type {!Map<!SDK.ServiceWorkerManager.ServiceWorkerManager, !Array<!Common.EventTarget.EventDescriptor>>}*/
Blink Reformat4c46d092018-04-07 15:32:3771 this._eventListeners = new Map();
Paul Lewisdaac1062020-03-05 14:37:1072 SDK.SDKModel.TargetManager.instance().observeModels(SDK.ServiceWorkerManager.ServiceWorkerManager, this);
Blink Reformat4c46d092018-04-07 15:32:3773 this._updateListVisibility();
74 }
75
76 /**
77 * @override
Tim van der Lippe0efccf02020-02-12 15:15:3978 * @param {!SDK.ServiceWorkerManager.ServiceWorkerManager} serviceWorkerManager
Blink Reformat4c46d092018-04-07 15:32:3779 */
80 modelAdded(serviceWorkerManager) {
Tim van der Lippe1d6e57a2019-09-30 11:55:3481 if (this._manager) {
Blink Reformat4c46d092018-04-07 15:32:3782 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:3483 }
Blink Reformat4c46d092018-04-07 15:32:3784 this._manager = serviceWorkerManager;
Tim van der Lippe0efccf02020-02-12 15:15:3985 this._securityOriginManager = serviceWorkerManager.target().model(SDK.SecurityOriginManager.SecurityOriginManager);
Blink Reformat4c46d092018-04-07 15:32:3786
Tim van der Lippe1d6e57a2019-09-30 11:55:3487 for (const registration of this._manager.registrations().values()) {
Blink Reformat4c46d092018-04-07 15:32:3788 this._updateRegistration(registration);
Tim van der Lippe1d6e57a2019-09-30 11:55:3489 }
Blink Reformat4c46d092018-04-07 15:32:3790
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 Lippe0efccf02020-02-12 15:15:39105 * @param {!SDK.ServiceWorkerManager.ServiceWorkerManager} serviceWorkerManager
Blink Reformat4c46d092018-04-07 15:32:37106 */
107 modelRemoved(serviceWorkerManager) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34108 if (!this._manager || this._manager !== serviceWorkerManager) {
Blink Reformat4c46d092018-04-07 15:32:37109 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34110 }
Blink Reformat4c46d092018-04-07 15:32:37111
Tim van der Lippe0efccf02020-02-12 15:15:39112 Common.EventTarget.EventTarget.removeEventListeners(this._eventListeners.get(serviceWorkerManager));
Blink Reformat4c46d092018-04-07 15:32:37113 this._eventListeners.delete(serviceWorkerManager);
114 this._manager = null;
115 this._securityOriginManager = null;
116 }
117
Harley Li68cc13c2019-02-25 21:47:25118
119 /**
Tim van der Lippe0efccf02020-02-12 15:15:39120 * @param {!SDK.ServiceWorkerManager.ServiceWorkerRegistration} registration
Harley Li68cc13c2019-02-25 21:47:25121 * @return {number}
122 */
123 _getTimeStamp(registration) {
124 const versions = registration.versionsByMode();
125
Sigurd Schneidera38b8672020-05-20 11:31:45126 /** @type {number|undefined} */
Harley Li68cc13c2019-02-25 21:47:25127 let timestamp = 0;
128
Tim van der Lippe0efccf02020-02-12 15:15:39129 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 Li68cc13c2019-02-25 21:47:25133
Tim van der Lippe1d6e57a2019-09-30 11:55:34134 if (active) {
Harley Li68cc13c2019-02-25 21:47:25135 timestamp = active.scriptResponseTime;
Tim van der Lippe1d6e57a2019-09-30 11:55:34136 } else if (waiting) {
Harley Li68cc13c2019-02-25 21:47:25137 timestamp = waiting.scriptResponseTime;
Tim van der Lippe1d6e57a2019-09-30 11:55:34138 } else if (installing) {
Harley Li68cc13c2019-02-25 21:47:25139 timestamp = installing.scriptResponseTime;
Tim van der Lippe1d6e57a2019-09-30 11:55:34140 } else if (redundant) {
Harley Li68cc13c2019-02-25 21:47:25141 timestamp = redundant.scriptResponseTime;
Tim van der Lippe1d6e57a2019-09-30 11:55:34142 }
Harley Li68cc13c2019-02-25 21:47:25143
Sigurd Schneidera38b8672020-05-20 11:31:45144 return timestamp || 0;
Harley Li68cc13c2019-02-25 21:47:25145 }
146
Blink Reformat4c46d092018-04-07 15:32:37147 _updateSectionVisibility() {
Blink Reformat4c46d092018-04-07 15:32:37148 let hasThis = false;
149 const movedSections = [];
150 for (const section of this._sections.values()) {
151 const expectedView = this._getReportViewForOrigin(section._registration.securityOrigin);
Blink Reformat4c46d092018-04-07 15:32:37152 hasThis |= expectedView === this._currentWorkersView;
Tim van der Lippe1d6e57a2019-09-30 11:55:34153 if (section._section.parentWidget() !== expectedView) {
Blink Reformat4c46d092018-04-07 15:32:37154 movedSections.push(section);
Tim van der Lippe1d6e57a2019-09-30 11:55:34155 }
Blink Reformat4c46d092018-04-07 15:32:37156 }
157
158 for (const section of movedSections) {
159 const registration = section._registration;
160 this._removeRegistrationFromList(registration);
161 this._updateRegistration(registration, true);
162 }
163
Harley Li68cc13c2019-02-25 21:47:25164 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 Reformat4c46d092018-04-07 15:32:37171 for (const section of this._sections.values()) {
172 if (section._section.parentWidget() === this._currentWorkersView ||
Tim van der Lippe1d6e57a2019-09-30 11:55:34173 this._isRegistrationVisible(section._registration)) {
Blink Reformat4c46d092018-04-07 15:32:37174 section._section.showWidget();
Tim van der Lippe1d6e57a2019-09-30 11:55:34175 } else {
Blink Reformat4c46d092018-04-07 15:32:37176 section._section.hideWidget();
Tim van der Lippe1d6e57a2019-09-30 11:55:34177 }
Blink Reformat4c46d092018-04-07 15:32:37178 }
Tim van der Lippeffa78622019-09-16 12:07:12179 this.contentElement.classList.toggle('service-worker-has-current', !!hasThis);
Blink Reformat4c46d092018-04-07 15:32:37180 this._updateListVisibility();
181 }
182
183 /**
Tim van der Lippec02a97c2020-02-14 14:39:27184 * @param {!Common.EventTarget.EventTargetEvent} event
Blink Reformat4c46d092018-04-07 15:32:37185 */
186 _registrationUpdated(event) {
Tim van der Lippe0efccf02020-02-12 15:15:39187 const registration = /** @type {!SDK.ServiceWorkerManager.ServiceWorkerRegistration} */ (event.data);
Blink Reformat4c46d092018-04-07 15:32:37188 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 Lippe1d6e57a2019-09-30 11:55:34196 if (!securityOrigins.has(registration.securityOrigin) && !this._isRegistrationVisible(registration)) {
Blink Reformat4c46d092018-04-07 15:32:37197 continue;
Tim van der Lippe1d6e57a2019-09-30 11:55:34198 }
Blink Reformat4c46d092018-04-07 15:32:37199 if (!registration.canBeRemoved()) {
200 hasNonDeletedRegistrations = true;
201 break;
202 }
203 }
204
Tim van der Lippe1d6e57a2019-09-30 11:55:34205 if (!hasNonDeletedRegistrations) {
Blink Reformat4c46d092018-04-07 15:32:37206 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34207 }
Blink Reformat4c46d092018-04-07 15:32:37208
209 for (const registration of this._manager.registrations().values()) {
210 const visible = securityOrigins.has(registration.securityOrigin) || this._isRegistrationVisible(registration);
Tim van der Lippe1d6e57a2019-09-30 11:55:34211 if (!visible && registration.canBeRemoved()) {
Blink Reformat4c46d092018-04-07 15:32:37212 this._removeRegistrationFromList(registration);
Tim van der Lippe1d6e57a2019-09-30 11:55:34213 }
Blink Reformat4c46d092018-04-07 15:32:37214 }
215 }
216
217 /**
218 * @param {string} origin
Nidhi Jajua3e34a82020-08-07 14:46:21219 * @return {?UI.ReportView.ReportView}
Blink Reformat4c46d092018-04-07 15:32:37220 */
221 _getReportViewForOrigin(origin) {
Harley Liddf2b682019-03-08 22:35:23222 if (this._securityOriginManager.securityOrigins().includes(origin) ||
Tim van der Lippe1d6e57a2019-09-30 11:55:34223 this._securityOriginManager.unreachableMainSecurityOrigin() === origin) {
Blink Reformat4c46d092018-04-07 15:32:37224 return this._currentWorkersView;
Tim van der Lippe1d6e57a2019-09-30 11:55:34225 }
Nidhi Jajua3e34a82020-08-07 14:46:21226 return null;
Blink Reformat4c46d092018-04-07 15:32:37227 }
228
229 /**
Tim van der Lippe0efccf02020-02-12 15:15:39230 * @param {!SDK.ServiceWorkerManager.ServiceWorkerRegistration} registration
Blink Reformat4c46d092018-04-07 15:32:37231 * @param {boolean=} skipUpdate
232 */
233 _updateRegistration(registration, skipUpdate) {
234 let section = this._sections.get(registration);
235 if (!section) {
Harley Li09a58712019-06-07 19:25:54236 const title = registration.scopeURL;
Nidhi Jajua3e34a82020-08-07 14:46:21237 const reportView = this._getReportViewForOrigin(registration.securityOrigin);
238 if (!reportView) {
239 return;
240 }
241 const uiSection = reportView.appendSection(title);
Rob Pavezacb832182019-10-16 23:13:00242 uiSection.setUiGroupTitle(ls`Service worker for ${title}`);
Harley Li68cc13c2019-02-25 21:47:25243 uiSection[this._registrationSymbol] = registration;
Tim van der Lippe097cdec2020-01-06 14:44:17244 section = new Section(
Tim van der Lippe0efccf02020-02-12 15:15:39245 /** @type {!SDK.ServiceWorkerManager.ServiceWorkerManager} */ (this._manager), uiSection, registration);
Blink Reformat4c46d092018-04-07 15:32:37246 this._sections.set(registration, section);
247 }
Tim van der Lippe1d6e57a2019-09-30 11:55:34248 if (skipUpdate) {
Blink Reformat4c46d092018-04-07 15:32:37249 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34250 }
Blink Reformat4c46d092018-04-07 15:32:37251 this._updateSectionVisibility();
252 section._scheduleUpdate();
253 }
254
255 /**
Tim van der Lippec02a97c2020-02-14 14:39:27256 * @param {!Common.EventTarget.EventTargetEvent} event
Blink Reformat4c46d092018-04-07 15:32:37257 */
258 _registrationDeleted(event) {
Tim van der Lippe0efccf02020-02-12 15:15:39259 const registration = /** @type {!SDK.ServiceWorkerManager.ServiceWorkerRegistration} */ (event.data);
Blink Reformat4c46d092018-04-07 15:32:37260 this._removeRegistrationFromList(registration);
261 }
262
263 /**
Tim van der Lippe0efccf02020-02-12 15:15:39264 * @param {!SDK.ServiceWorkerManager.ServiceWorkerRegistration} registration
Blink Reformat4c46d092018-04-07 15:32:37265 */
266 _removeRegistrationFromList(registration) {
267 const section = this._sections.get(registration);
Tim van der Lippe1d6e57a2019-09-30 11:55:34268 if (section) {
Blink Reformat4c46d092018-04-07 15:32:37269 section._section.detach();
Tim van der Lippe1d6e57a2019-09-30 11:55:34270 }
Blink Reformat4c46d092018-04-07 15:32:37271 this._sections.delete(registration);
272 this._updateSectionVisibility();
273 }
274
275 /**
Tim van der Lippe0efccf02020-02-12 15:15:39276 * @param {!SDK.ServiceWorkerManager.ServiceWorkerRegistration} registration
Blink Reformat4c46d092018-04-07 15:32:37277 * @return {boolean}
278 */
279 _isRegistrationVisible(registration) {
Nidhi Jajua3e34a82020-08-07 14:46:21280 if (!registration.scopeURL) {
Blink Reformat4c46d092018-04-07 15:32:37281 return true;
Tim van der Lippe1d6e57a2019-09-30 11:55:34282 }
Nidhi Jajua3e34a82020-08-07 14:46:21283 return false;
Blink Reformat4c46d092018-04-07 15:32:37284 }
285
Blink Reformat4c46d092018-04-07 15:32:37286 _updateListVisibility() {
287 this.contentElement.classList.toggle('service-worker-list-empty', this._sections.size === 0);
288 }
Tim van der Lippe097cdec2020-01-06 14:44:17289}
Blink Reformat4c46d092018-04-07 15:32:37290
Tim van der Lippe097cdec2020-01-06 14:44:17291export class Section {
Blink Reformat4c46d092018-04-07 15:32:37292 /**
Tim van der Lippe0efccf02020-02-12 15:15:39293 * @param {!SDK.ServiceWorkerManager.ServiceWorkerManager} manager
Blink Reformat4c46d092018-04-07 15:32:37294 * @param {!UI.ReportView.Section} section
Tim van der Lippe0efccf02020-02-12 15:15:39295 * @param {!SDK.ServiceWorkerManager.ServiceWorkerRegistration} registration
Blink Reformat4c46d092018-04-07 15:32:37296 */
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 Lewis2d7d65c2020-03-16 17:26:30303 this._pushNotificationDataSetting = Common.Settings.Settings.instance().createLocalSetting(
Tim van der Lippe0efccf02020-02-12 15:15:39304 'pushData', Common.UIString.UIString('Test push message from DevTools.'));
Paul Lewis2d7d65c2020-03-16 17:26:30305 this._syncTagNameSetting =
306 Common.Settings.Settings.instance().createLocalSetting('syncTagName', 'test-tag-from-devtools');
Mugdha Lakhanidb7c9b12019-08-09 13:48:04307 this._periodicSyncTagNameSetting =
Paul Lewis2d7d65c2020-03-16 17:26:30308 Common.Settings.Settings.instance().createLocalSetting('periodicSyncTagName', 'test-tag-from-devtools');
Blink Reformat4c46d092018-04-07 15:32:37309
310 this._toolbar = section.createToolbar();
311 this._toolbar.renderAsLinks();
Tim van der Lippe0efccf02020-02-12 15:15:39312 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 Reformat4c46d092018-04-07 15:32:37315 this._toolbar.appendToolbarItem(this._updateButton);
Tim van der Lippe0efccf02020-02-12 15:15:39316 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 Reformat4c46d092018-04-07 15:32:37319 this._toolbar.appendToolbarItem(this._deleteButton);
320
321 // Preserve the order.
Tim van der Lippe0efccf02020-02-12 15:15:39322 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 Reformat4c46d092018-04-07 15:32:37325 this._createSyncNotificationField(
Tim van der Lippe0efccf02020-02-12 15:15:39326 Common.UIString.UIString('Push'), this._pushNotificationDataSetting.get(),
327 Common.UIString.UIString('Push data'), this._push.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37328 this._createSyncNotificationField(
Tim van der Lippe0efccf02020-02-12 15:15:39329 Common.UIString.UIString('Sync'), this._syncTagNameSetting.get(), Common.UIString.UIString('Sync tag'),
330 this._sync.bind(this));
Mugdha Lakhani71b51fa2020-01-02 15:04:58331 this._createSyncNotificationField(
332 ls`Periodic Sync`, this._periodicSyncTagNameSetting.get(), ls`Periodic Sync tag`,
333 tag => this._periodicSync(tag));
Blink Reformat4c46d092018-04-07 15:32:37334
Tim van der Lippe0efccf02020-02-12 15:15:39335 this._linkifier = new Components.Linkifier.Linkifier();
Blink Reformat4c46d092018-04-07 15:32:37336 /** @type {!Map<string, !Protocol.Target.TargetInfo>} */
337 this._clientInfoCache = new Map();
Tim van der Lippe0efccf02020-02-12 15:15:39338 this._throttler = new Common.Throttler.Throttler(500);
Blink Reformat4c46d092018-04-07 15:32:37339 }
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 Beyer83ae1012020-09-28 09:10:43350 const editor = UI.UIUtils.createInput('source-code service-worker-notification-editor');
351 form.appendChild(editor);
Tim van der Lippe0efccf02020-02-12 15:15:39352 const button = UI.UIUtils.createTextButton(label);
Blink Reformat4c46d092018-04-07 15:32:37353 button.type = 'submit';
354 form.appendChild(button);
355
356 editor.value = initialValue;
357 editor.placeholder = placeholder;
Junyi Xiaod3e71a42019-04-23 04:49:04358 UI.ARIAUtils.setAccessibleName(editor, label);
Blink Reformat4c46d092018-04-07 15:32:37359
360 form.addEventListener('submit', e => {
361 callback(editor.value || '');
362 e.consume(true);
363 });
364 }
365
366 _scheduleUpdate() {
Tim van der Lippe097cdec2020-01-06 14:44:17367 if (ServiceWorkersView._noThrottle) {
Blink Reformat4c46d092018-04-07 15:32:37368 this._update();
369 return;
370 }
371 this._throttler.schedule(this._update.bind(this));
372 }
373
374 /**
375 * @param {string} versionId
Tim van der Lippe0efccf02020-02-12 15:15:39376 * @return {?SDK.SDKModel.Target}
Blink Reformat4c46d092018-04-07 15:32:37377 */
378 _targetForVersionId(versionId) {
379 const version = this._manager.findVersion(versionId);
Tim van der Lippe1d6e57a2019-09-30 11:55:34380 if (!version || !version.targetId) {
Blink Reformat4c46d092018-04-07 15:32:37381 return null;
Tim van der Lippe1d6e57a2019-09-30 11:55:34382 }
Paul Lewisdaac1062020-03-05 14:37:10383 return SDK.SDKModel.TargetManager.instance().targetById(version.targetId);
Blink Reformat4c46d092018-04-07 15:32:37384 }
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 Liaofeb41512020-08-20 22:45:41395 const statusString = installingEntry.createChild('span', 'service-worker-version-string');
396 statusString.textContent = label;
397 UI.ARIAUtils.markAsAlert(statusString);
Blink Reformat4c46d092018-04-07 15:32:37398 return installingEntry;
399 }
400
401 /**
Tim van der Lippe0efccf02020-02-12 15:15:39402 * @param {!SDK.ServiceWorkerManager.ServiceWorkerVersion} version
Blink Reformat4c46d092018-04-07 15:32:37403 */
404 _updateClientsField(version) {
405 this._clientsField.removeChildren();
Sigurd Schneidera38b8672020-05-20 11:31:45406 this._section.setFieldVisible(Common.UIString.UIString('Clients'), !!version.controlledClients.length);
Blink Reformat4c46d092018-04-07 15:32:37407 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 Lippe0efccf02020-02-12 15:15:39418 * @param {!SDK.ServiceWorkerManager.ServiceWorkerVersion} version
Blink Reformat4c46d092018-04-07 15:32:37419 */
420 _updateSourceField(version) {
421 this._sourceField.removeChildren();
Tim van der Lippe0efccf02020-02-12 15:15:39422 const fileName = Common.ParsedURL.ParsedURL.extractName(version.scriptURL);
Blink Reformat4c46d092018-04-07 15:32:37423 const name = this._sourceField.createChild('div', 'report-field-value-filename');
Tim van der Lippe0efccf02020-02-12 15:15:39424 const link = Components.Linkifier.Linkifier.linkifyURL(version.scriptURL, {text: fileName});
Junyi Xiaod3e71a42019-04-23 04:49:04425 link.tabIndex = 0;
426 name.appendChild(link);
Blink Reformat4c46d092018-04-07 15:32:37427 if (this._registration.errors.length) {
Tim van der Lippe0efccf02020-02-12 15:15:39428 const errorsLabel = UI.UIUtils.createIconLabel(String(this._registration.errors.length), 'smallicon-error');
Blink Reformat4c46d092018-04-07 15:32:37429 errorsLabel.classList.add('link');
Junyi Xiaod3e71a42019-04-23 04:49:04430 errorsLabel.tabIndex = 0;
431 UI.ARIAUtils.setAccessibleName(errorsLabel, ls`${this._registration.errors.length} registration errors`);
Paul Lewisa83ea612020-03-04 13:01:36432 self.onInvokeElement(errorsLabel, () => Common.Console.Console.instance().show());
Blink Reformat4c46d092018-04-07 15:32:37433 name.appendChild(errorsLabel);
434 }
John Abd-El-Malek7fa90c82018-08-27 18:39:55435 this._sourceField.createChild('div', 'report-field-value-subtitle').textContent =
Tim van der Lippe0efccf02020-02-12 15:15:39436 Common.UIString.UIString('Received %s', new Date(version.scriptResponseTime * 1000).toLocaleString());
Blink Reformat4c46d092018-04-07 15:32:37437 }
438
439 /**
440 * @return {!Promise}
441 */
442 _update() {
443 const fingerprint = this._registration.fingerprint();
Tim van der Lippe1d6e57a2019-09-30 11:55:34444 if (fingerprint === this._fingerprint) {
Blink Reformat4c46d092018-04-07 15:32:37445 return Promise.resolve();
Tim van der Lippe1d6e57a2019-09-30 11:55:34446 }
Blink Reformat4c46d092018-04-07 15:32:37447 this._fingerprint = fingerprint;
448
449 this._toolbar.setEnabled(!this._registration.isDeleted);
450
451 const versions = this._registration.versionsByMode();
Harley Li09a58712019-06-07 19:25:54452 const scopeURL = this._registration.scopeURL;
Tim van der Lippe0efccf02020-02-12 15:15:39453 const title = this._registration.isDeleted ? Common.UIString.UIString('%s - deleted', scopeURL) : scopeURL;
Blink Reformat4c46d092018-04-07 15:32:37454 this._section.setTitle(title);
455
Tim van der Lippe0efccf02020-02-12 15:15:39456 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 Reformat4c46d092018-04-07 15:32:37460
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 Lippe0efccf02020-02-12 15:15:39467 const localizedRunningStatus = SDK.ServiceWorkerManager.ServiceWorkerVersion.RunningStatus[active.runningStatus];
Blink Reformat4c46d092018-04-07 15:32:37468 const activeEntry = this._addVersion(
Mandy Chen9c1200c2019-09-24 18:36:01469 versionsStack, 'service-worker-active-circle', ls`#${active.id} activated and is ${localizedRunningStatus}`);
Blink Reformat4c46d092018-04-07 15:32:37470
471 if (active.isRunning() || active.isStarting()) {
Tim van der Lippe0efccf02020-02-12 15:15:39472 this._createLink(activeEntry, Common.UIString.UIString('stop'), this._stopButtonClicked.bind(this, active.id));
Tim van der Lippe1d6e57a2019-09-30 11:55:34473 if (!this._targetForVersionId(active.id)) {
Tim van der Lippe0efccf02020-02-12 15:15:39474 this._createLink(
475 activeEntry, Common.UIString.UIString('inspect'), this._inspectButtonClicked.bind(this, active.id));
Tim van der Lippe1d6e57a2019-09-30 11:55:34476 }
Blink Reformat4c46d092018-04-07 15:32:37477 } else if (active.isStartable()) {
Tim van der Lippe0efccf02020-02-12 15:15:39478 this._createLink(activeEntry, Common.UIString.UIString('start'), this._startButtonClicked.bind(this));
Blink Reformat4c46d092018-04-07 15:32:37479 }
480 this._updateClientsField(active);
481 } else if (redundant) {
482 this._updateSourceField(redundant);
483 this._addVersion(
Tim van der Lippe0efccf02020-02-12 15:15:39484 versionsStack, 'service-worker-redundant-circle', Common.UIString.UIString('#%s is redundant', redundant.id));
Blink Reformat4c46d092018-04-07 15:32:37485 this._updateClientsField(redundant);
486 }
487
488 if (waiting) {
489 const waitingEntry = this._addVersion(
Tim van der Lippe0efccf02020-02-12 15:15:39490 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-Malek7fa90c82018-08-27 18:39:55493 waitingEntry.createChild('div', 'service-worker-subtitle').textContent =
Tim van der Lippe0efccf02020-02-12 15:15:39494 Common.UIString.UIString('Received %s', new Date(waiting.scriptResponseTime * 1000).toLocaleString());
Tim van der Lippe1d6e57a2019-09-30 11:55:34495 if (!this._targetForVersionId(waiting.id) && (waiting.isRunning() || waiting.isStarting())) {
Tim van der Lippe0efccf02020-02-12 15:15:39496 this._createLink(
497 waitingEntry, Common.UIString.UIString('inspect'), this._inspectButtonClicked.bind(this, waiting.id));
Tim van der Lippe1d6e57a2019-09-30 11:55:34498 }
Blink Reformat4c46d092018-04-07 15:32:37499 }
500 if (installing) {
501 const installingEntry = this._addVersion(
Tim van der Lippe0efccf02020-02-12 15:15:39502 versionsStack, 'service-worker-installing-circle',
503 Common.UIString.UIString('#%s trying to install', installing.id));
John Abd-El-Malek7fa90c82018-08-27 18:39:55504 installingEntry.createChild('div', 'service-worker-subtitle').textContent =
Tim van der Lippe0efccf02020-02-12 15:15:39505 Common.UIString.UIString('Received %s', new Date(installing.scriptResponseTime * 1000).toLocaleString());
Junyi Xiaod3e71a42019-04-23 04:49:04506 if (!this._targetForVersionId(installing.id) && (installing.isRunning() || installing.isStarting())) {
507 this._createLink(
Tim van der Lippe0efccf02020-02-12 15:15:39508 installingEntry, Common.UIString.UIString('inspect'), this._inspectButtonClicked.bind(this, installing.id));
Junyi Xiaod3e71a42019-04-23 04:49:04509 }
Blink Reformat4c46d092018-04-07 15:32:37510 }
511 return Promise.resolve();
512 }
513
514 /**
Junyi Xiaod3e71a42019-04-23 04:49:04515 * @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 Beyer83ae1012020-09-28 09:10:43524 button.classList.add('link', 'devtools-link');
Junyi Xiaod3e71a42019-04-23 04:49:04525 button.textContent = title;
526 button.tabIndex = 0;
527 button.addEventListener('click', listener, useCapture);
528 return button;
529 }
530
531 /**
Tim van der Lippec02a97c2020-02-14 14:39:27532 * @param {!Common.EventTarget.EventTargetEvent} event
Blink Reformat4c46d092018-04-07 15:32:37533 */
534 _unregisterButtonClicked(event) {
535 this._manager.deleteRegistration(this._registration.id);
536 }
537
538 /**
Tim van der Lippec02a97c2020-02-14 14:39:27539 * @param {!Common.EventTarget.EventTargetEvent} event
Blink Reformat4c46d092018-04-07 15:32:37540 */
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 Lakhanidb7c9b12019-08-09 13:48:04562 * @param {string} tag
563 */
564 _periodicSync(tag) {
565 this._periodicSyncTagNameSetting.set(tag);
566 this._manager.dispatchPeriodicSyncEvent(this._registration.id, tag);
567 }
568
569 /**
Blink Reformat4c46d092018-04-07 15:32:37570 * @param {!Element} element
571 * @param {?Protocol.Target.TargetInfo} targetInfo
572 */
573 _onClientInfo(element, targetInfo) {
Tim van der Lippe1d6e57a2019-09-30 11:55:34574 if (!targetInfo) {
Blink Reformat4c46d092018-04-07 15:32:37575 return;
Tim van der Lippe1d6e57a2019-09-30 11:55:34576 }
Blink Reformat4c46d092018-04-07 15:32:37577 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 Lia8a622f2018-11-01 23:58:09587 const clientString = element.createChild('span', 'service-worker-client-string');
Sigurd Schneider23c52972020-10-13 09:31:14588 UI.UIUtils.createTextChild(clientString, ls`Worker: ${targetInfo.url}`);
Blink Reformat4c46d092018-04-07 15:32:37589 return;
590 }
591 element.removeChildren();
Harley Lia8a622f2018-11-01 23:58:09592 const clientString = element.createChild('span', 'service-worker-client-string');
Sigurd Schneider23c52972020-10-13 09:31:14593 UI.UIUtils.createTextChild(clientString, targetInfo.url);
Junyi Xiaod3e71a42019-04-23 04:49:04594 this._createLink(
595 element, ls`focus`, this._activateTarget.bind(this, targetInfo.targetId), 'service-worker-client-focus-link');
Blink Reformat4c46d092018-04-07 15:32:37596 }
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 Lippe0efccf02020-02-12 15:15:39632 const shadowRoot = UI.Utils.createShadowRootWithCoreStyles(container);
633 UI.Utils.appendStyle(shadowRoot, 'resources/serviceWorkersView.css');
Blink Reformat4c46d092018-04-07 15:32:37634 const contentElement = createElement('div');
635 shadowRoot.appendChild(contentElement);
636 return contentElement;
637 }
Tim van der Lippe097cdec2020-01-06 14:44:17638}