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.
7 * @extends {WebInspector.SidebarPane}
8 * @implements {WebInspector.TargetManager.Observer}
10 WebInspector
.ServiceWorkersSidebarPane = function()
12 WebInspector
.SidebarPane
.call(this, WebInspector
.UIString("\u2699 Service Workers"));
13 this.registerRequiredCSS("sources/serviceWorkersSidebar.css");
14 this._bodyElement
= this.element
.createChild("div", "vbox");
15 this.setVisible(false);
17 /** @type {?WebInspector.ServiceWorkerManager} */
19 WebInspector
.targetManager
.observeTargets(this, WebInspector
.Target
.Type
.Page
);
20 this._placeholderElement
= createElementWithClass("div", "info");
21 this._placeholderElement
.textContent
= WebInspector
.UIString("No service workers control this page");
23 /** @type {!Map.<string, !Element>} */
24 this._versionIdCheckBoxMap
= new Map();
27 WebInspector
.ServiceWorkersSidebarPane
.prototype = {
30 * @param {!WebInspector.Target} target
32 targetAdded: function(target
)
36 this._manager
= target
.serviceWorkerManager
;
37 this._updateVisibility();
38 target
.serviceWorkerManager
.addEventListener(WebInspector
.ServiceWorkerManager
.Events
.WorkersUpdated
, this._update
, this);
39 target
.serviceWorkerManager
.addEventListener(WebInspector
.ServiceWorkerManager
.Events
.RegistrationUpdated
, this._registrationUpdated
, this);
44 * @param {!WebInspector.Target} target
46 targetRemoved: function(target
)
48 target
.serviceWorkerManager
.removeEventListener(WebInspector
.ServiceWorkerManager
.Events
.WorkersUpdated
, this._update
, this);
49 target
.serviceWorkerManager
.removeEventListener(WebInspector
.ServiceWorkerManager
.Events
.RegistrationUpdated
, this._registrationUpdated
, this);
50 this._updateVisibility();
55 this._updateVisibility();
56 this._bodyElement
.removeChildren();
57 this._versionIdCheckBoxMap
.clear();
59 if (!this.isShowing() || !this._manager
)
62 if (!this._manager
.hasWorkers()) {
63 this._bodyElement
.appendChild(this._placeholderElement
);
67 for (var worker
of this._manager
.workers()) {
68 var workerElement
= this._bodyElement
.createChild("div", "service-worker");
69 var leftBox
= workerElement
.createChild("div", "vbox flex-auto");
70 leftBox
.appendChild(WebInspector
.linkifyURLAsNode(worker
.url(), worker
.name()));
71 var scopeElement
= leftBox
.createChild("span", "service-worker-scope");
72 scopeElement
.textContent
= worker
.scope();
73 scopeElement
.title
= worker
.scope();
74 var forceUpdateOnPageLoadCheckboxLabel
= createCheckboxLabel(WebInspector
.UIString("Force update on page load"));
75 var forceUpdateOnPageLoadCheckbox
= forceUpdateOnPageLoadCheckboxLabel
.checkboxElement
;
76 this._versionIdCheckBoxMap
.set(worker
.versionId(), forceUpdateOnPageLoadCheckbox
);
77 forceUpdateOnPageLoadCheckbox
.addEventListener("click", this._forceUpdateOnPageLoadCheckboxClicked
.bind(this, forceUpdateOnPageLoadCheckbox
, worker
.versionId()), false);
78 var version
= this._manager
.findVersion(worker
.versionId());
80 forceUpdateOnPageLoadCheckbox
.checked
= version
.registration
.forceUpdateOnPageLoad
;
81 leftBox
.appendChild(forceUpdateOnPageLoadCheckboxLabel
);
82 workerElement
.appendChild(createTextButton(WebInspector
.UIString("Unregister"), worker
.stop
.bind(worker
)));
87 * @param {!WebInspector.Event} event
89 _registrationUpdated: function(event
)
91 var registration
= /** @type {!WebInspector.ServiceWorkerRegistration} */ (event
.data
);
92 for (var version
of registration
.versions
.values()) {
93 var checkBox
= this._versionIdCheckBoxMap
.get(version
.id
);
95 checkBox
.checked
= registration
.forceUpdateOnPageLoad
;
100 * @param {!Element} checkbox
101 * @param {string} versionId
102 * @param {!Event} event
104 _forceUpdateOnPageLoadCheckboxClicked: function(checkbox
, versionId
, event
)
106 event
.preventDefault()
107 var version
= this._manager
.findVersion(versionId
);
110 this._manager
.setForceUpdateOnPageLoad(version
.registration
.id
, checkbox
.checked
);
113 _updateVisibility: function()
115 this._wasVisibleAtLeastOnce
= this._wasVisibleAtLeastOnce
|| !!this._manager
&& this._manager
.hasWorkers();
116 this.setVisible(this._wasVisibleAtLeastOnce
);
124 __proto__
: WebInspector
.SidebarPane
.prototype