1 // Copyright 2014 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.
5 cr.define('serviceworker', function() {
9 chrome.send('getAllRegistrations');
12 function progressNodeFor(link) {
13 return link.parentNode.querySelector('.operation-status');
16 // All commands are sent with the partition_path and scope, and
17 // are all completed with 'onOperationComplete'.
18 var COMMANDS = ['unregister', 'start', 'stop', 'sync'];
19 function commandHandler(command) {
20 return function(event) {
21 var link = event.target;
22 progressNodeFor(link).style.display = 'inline';
23 chrome.send(command, [link.partition_path,
29 function withNode(selector, partition_path, scope, callback) {
30 var links = document.querySelectorAll(selector);
31 for (var i = 0; i < links.length; ++i) {
33 if (partition_path == link.partition_path &&
34 scope == link.scope) {
40 // Fired from the backend after the start call has completed
41 function onOperationComplete(status, path, scope) {
42 // refreshes the ui, displaying any relevant buttons
43 withNode('button', path, scope, function(link) {
44 progressNodeFor(link).style.display = 'none';
49 var allLogMessages = {};
51 // Fired once per partition from the backend.
52 function onPartitionData(registrations, partition_id, partition_path) {
54 var container = $('serviceworker-list');
56 // Existing templates are keyed by partition_path. This allows
57 // the UI to be updated in-place rather than refreshing the
59 for (var i = 0; i < container.childNodes.length; ++i) {
60 if (container.childNodes[i].partition_path == partition_path) {
61 template = container.childNodes[i];
65 // This is probably the first time we're loading.
67 template = jstGetTemplate('serviceworker-list-template');
68 container.appendChild(template);
71 // Set log for each worker versions.
72 if (!(partition_id in allLogMessages)) {
73 allLogMessages[partition_id] = {};
75 var logMessages = allLogMessages[partition_id];
76 registrations.forEach(function (worker) {
77 [worker.active, worker.pending].forEach(function (version) {
79 if (version.version_id in logMessages) {
80 version.log = logMessages[version.version_id];
88 jstProcess(new JsEvalContext({ registrations: registrations,
89 partition_id: partition_id,
90 partition_path: partition_path}),
92 for (var i = 0; i < COMMANDS.length; ++i) {
93 var handler = commandHandler(COMMANDS[i]);
94 var links = container.querySelectorAll('button.' + COMMANDS[i]);
95 for (var j = 0; j < links.length; ++j) {
96 if (!links[j].hasClickEvent) {
97 links[j].addEventListener('click', handler, false);
98 links[j].hasClickEvent = true;
104 function onWorkerStarted(partition_id, version_id, process_id, thread_id) {
108 function onWorkerStopped(partition_id, version_id, process_id, thread_id) {
112 function onErrorReported(partition_id,
117 outputLogMessage(partition_id,
119 'Error: ' + JSON.stringify(error_info) + '\n');
122 function onConsoleMessageReported(partition_id,
127 outputLogMessage(partition_id,
129 'Console: ' + JSON.stringify(message) + '\n');
132 function onVersionStateChanged(partition_id, version_id) {
136 function outputLogMessage(partition_id, version_id, message) {
137 if (!(partition_id in allLogMessages)) {
138 allLogMessages[partition_id] = {};
140 var logMessages = allLogMessages[partition_id];
141 if (version_id in logMessages) {
142 logMessages[version_id] += message;
144 logMessages[version_id] = message;
148 document.querySelectorAll('textarea.serviceworker-log');
149 for (var i = 0; i < logAreas.length; ++i) {
150 var logArea = logAreas[i];
151 if (logArea.partition_id == partition_id &&
152 logArea.version_id == version_id) {
153 logArea.value += message;
160 onOperationComplete: onOperationComplete,
161 onPartitionData: onPartitionData,
162 onWorkerStarted: onWorkerStarted,
163 onWorkerStopped: onWorkerStopped,
164 onErrorReported: onErrorReported,
165 onConsoleMessageReported: onConsoleMessageReported,
166 onVersionStateChanged: onVersionStateChanged,
170 document.addEventListener('DOMContentLoaded', serviceworker.update);