Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 cr.define('serviceworker', function() { | 5 cr.define('serviceworker', function() { |
| 6 'use strict'; | 6 'use strict'; |
|
falken
2014/05/28 08:21:40
it's late to point out now, but why does this file
horo
2014/05/28 09:06:39
Done.
| |
| 7 | 7 |
| 8 function initialize() { | 8 function initialize() { |
| 9 if (window.location.hash == "#iframe") { | 9 if (window.location.hash == "#iframe") { |
| 10 // This page is loaded from chrome://inspect. | 10 // This page is loaded from chrome://inspect. |
| 11 window.addEventListener('message', onMessage.bind(this), false); | 11 window.addEventListener('message', onMessage.bind(this), false); |
| 12 } | 12 } |
| 13 update(); | 13 update(); |
| 14 } | 14 } |
| 15 | 15 |
| 16 function onMessage(event) { | 16 function onMessage(event) { |
| 17 if (event.origin != 'chrome://inspect') { | 17 if (event.origin != 'chrome://inspect') { |
| 18 return; | 18 return; |
| 19 } | 19 } |
| 20 chrome.send(event.data.action, | 20 sendCommand(event.data.action, event.data.worker); |
| 21 [event.data.partition_path, event.data.scope]); | |
| 22 } | 21 } |
| 23 | 22 |
| 24 function update() { | 23 function update() { |
| 25 chrome.send('getAllRegistrations'); | 24 chrome.send('getAllRegistrations'); |
| 26 } | 25 } |
| 27 | 26 |
| 28 function progressNodeFor(link) { | 27 function progressNodeFor(link) { |
| 29 return link.parentNode.querySelector('.operation-status'); | 28 return link.parentNode.querySelector('.operation-status'); |
| 30 } | 29 } |
| 31 | 30 |
| 32 // All commands are sent with the partition_path and scope, and | 31 // All commands are sent with the partition_path and scope, and |
|
falken
2014/05/28 16:26:06
Is the "partition_path and scope" part of the comm
horo
2014/05/29 01:31:25
Done.
| |
| 33 // are all completed with 'onOperationComplete'. | 32 // are all completed with 'onOperationComplete'. |
| 34 var COMMANDS = ['unregister', 'start', 'stop', 'sync', 'inspect']; | 33 var COMMANDS = ['stop', 'sync', 'inspect', 'unregister', 'start']; |
| 35 function commandHandler(command) { | 34 function commandHandler(command) { |
| 36 return function(event) { | 35 return function(event) { |
| 37 var link = event.target; | 36 var link = event.target; |
| 38 progressNodeFor(link).style.display = 'inline'; | 37 progressNodeFor(link).style.display = 'inline'; |
| 39 chrome.send(command, [link.partition_path, | 38 sendCommand(command, link.cmdArgs, (function(status){ |
| 40 link.scope]); | 39 progressNodeFor(link).style.display = 'none'; |
| 40 }).bind(null, link)); | |
| 41 return false; | 41 return false; |
| 42 }; | 42 }; |
| 43 }; | 43 }; |
| 44 | 44 |
| 45 function withNode(selector, partition_path, scope, callback) { | 45 var commandCallbacks = []; |
| 46 var links = document.querySelectorAll(selector); | 46 function sendCommand(command, args, callback) { |
| 47 for (var i = 0; i < links.length; ++i) { | 47 var callbackId = 0; |
| 48 var link = links[i]; | 48 while (callbackId in commandCallbacks) { |
| 49 if (partition_path == link.partition_path && | 49 callbackId++; |
| 50 scope == link.scope) { | |
| 51 callback(link); | |
| 52 } | |
| 53 } | 50 } |
| 51 commandCallbacks[callbackId] = callback; | |
| 52 chrome.send(command, [callbackId, args]); | |
| 54 } | 53 } |
| 55 | 54 |
| 56 // Fired from the backend after the start call has completed | 55 // Fired from the backend after the command call has completed. |
| 57 function onOperationComplete(status, path, scope) { | 56 function onOperationComplete(status, callbackId) { |
| 58 // refreshes the ui, displaying any relevant buttons | 57 var callback = commandCallbacks[callbackId]; |
| 59 withNode('button', path, scope, function(link) { | 58 delete commandCallbacks[callbackId]; |
| 60 progressNodeFor(link).style.display = 'none'; | 59 if (callback) { |
| 61 }); | 60 callback(status); |
| 61 } | |
| 62 update(); | 62 update(); |
| 63 } | 63 } |
| 64 | 64 |
| 65 var allLogMessages = {}; | |
| 66 | |
| 67 // Send the active ServiceWorker information to chrome://inspect. | 65 // Send the active ServiceWorker information to chrome://inspect. |
| 68 function sendToInspectPage(registrations, partition_id, partition_path) { | 66 function sendToInspectPage(live_registrations, |
| 67 partition_id) { | |
| 69 var workers = []; | 68 var workers = []; |
| 70 for (var i = 0; i < registrations.length; i++) { | 69 live_registrations.forEach(function (registration) { |
|
falken
2014/05/28 08:21:40
should be no space after "function"? (and througho
horo
2014/05/28 09:06:39
Done.
| |
| 71 var registration = registrations[i]; | 70 [registration.active, |
| 72 if (!registration.active || | 71 registration.pending].forEach(function (version){ |
|
falken
2014/05/28 08:21:40
should be a space before "{"? (and throughout)
horo
2014/05/28 09:06:39
Done.
| |
| 73 registration.active.running_status != 'RUNNING') { | 72 if (!version || version.running_status != 'RUNNING') { |
| 74 continue; | 73 return; |
| 75 } | 74 } |
| 76 workers.push({ | 75 workers.push({ |
| 77 'partition_path': partition_path, | 76 'scope': registration.scope, |
| 78 'scope': registration.scope, | 77 'url': registration.script_url, |
| 79 'url': registration.script_url | 78 'partition_id': partition_id, |
| 80 }) | 79 'version_id': version.version_id, |
| 81 } | 80 'process_id': version.process_id, |
| 81 'devtools_agent_route_id': | |
| 82 version.devtools_agent_route_id | |
| 83 }); | |
| 84 }); | |
| 85 }); | |
| 82 window.parent.postMessage({ | 86 window.parent.postMessage({ |
| 83 'partition_id': partition_id, | 87 'partition_id': partition_id, |
| 84 'workers': workers, | 88 'workers': workers, |
| 85 }, 'chrome://inspect') | 89 }, 'chrome://inspect') |
| 86 } | 90 } |
| 87 | 91 |
| 92 var allLogMessages = {}; | |
| 93 // Set log for each worker versions. | |
|
falken
2014/05/28 08:21:40
This function is really filling the log for one ve
horo
2014/05/28 09:06:39
Done.
| |
| 94 function fillLogForVersion(partition_id, version) { | |
| 95 if (!version) { | |
| 96 return; | |
| 97 } | |
| 98 if (!(partition_id in allLogMessages)) { | |
| 99 allLogMessages[partition_id] = {}; | |
| 100 } | |
| 101 var logMessages = allLogMessages[partition_id]; | |
| 102 if (version.version_id in logMessages) { | |
| 103 version.log = logMessages[version.version_id]; | |
| 104 } else { | |
| 105 version.log = ''; | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 // Get the unregistered workers. | |
| 110 // |unregistered_registrations| will be filled with the registrations which | |
| 111 // are in |live_registrations| but not in |stored_registrations|. | |
| 112 // |unregistered_versions| will be filled with the versions which | |
| 113 // are in |live_versions| but not in |stored_registrations| nor in | |
| 114 // |live_registrations|. | |
| 115 function getUnregisteredWorkers(stored_registrations, | |
| 116 live_registrations, | |
| 117 live_versions, | |
| 118 unregistered_registrations, | |
| 119 unregistered_versions) { | |
| 120 var registration_id_set = {}; | |
| 121 var version_id_set = {}; | |
| 122 stored_registrations.forEach(function (registration) { | |
|
falken
2014/05/28 08:21:40
should be no space after "function"?
horo
2014/05/28 09:06:39
Done.
| |
| 123 registration_id_set[registration.registration_id] = true; | |
| 124 }); | |
| 125 [stored_registrations, live_registrations].forEach( | |
| 126 function (registrations) { | |
| 127 registrations.forEach(function (registration) { | |
| 128 [registration.active, | |
| 129 registration.pending].forEach( | |
| 130 function (version){ | |
| 131 if (version) { | |
| 132 version_id_set[version.version_id] = true; | |
| 133 } | |
| 134 }); | |
|
falken
2014/05/28 08:21:40
this close paren should be aligned with its open p
horo
2014/05/28 09:06:39
Done.
| |
| 135 }); | |
| 136 }); | |
| 137 live_registrations.forEach(function (registration) { | |
| 138 if (!registration_id_set[registration.registration_id]) { | |
| 139 registration.unregistered = true; | |
| 140 unregistered_registrations.push(registration); | |
| 141 } | |
| 142 }); | |
| 143 live_versions.forEach(function (version) { | |
| 144 if (!version_id_set[version.version_id]) { | |
| 145 unregistered_versions.push(version); | |
| 146 } | |
| 147 }); | |
| 148 } | |
| 149 | |
| 88 // Fired once per partition from the backend. | 150 // Fired once per partition from the backend. |
| 89 function onPartitionData(registrations, partition_id, partition_path) { | 151 function onPartitionData(live_registrations, |
| 152 live_versions, | |
| 153 stored_registrations, | |
| 154 partition_id, | |
| 155 partition_path) { | |
| 90 if (window.location.hash == "#iframe") { | 156 if (window.location.hash == "#iframe") { |
| 91 // This page is loaded from chrome://inspect. | 157 // This page is loaded from chrome://inspect. |
| 92 sendToInspectPage(registrations, partition_id, partition_path); | 158 sendToInspectPage(live_registrations, |
| 159 partition_id); | |
| 93 return; | 160 return; |
| 94 } | 161 } |
| 162 var unregistered_registrations = []; | |
| 163 var unregistered_versions = []; | |
| 164 getUnregisteredWorkers(stored_registrations, | |
| 165 live_registrations, | |
| 166 live_versions, | |
| 167 unregistered_registrations, | |
| 168 unregistered_versions); | |
| 95 var template; | 169 var template; |
| 96 var container = $('serviceworker-list'); | 170 var container = $('serviceworker-list'); |
| 97 | |
| 98 // Existing templates are keyed by partition_path. This allows | 171 // Existing templates are keyed by partition_path. This allows |
| 99 // the UI to be updated in-place rather than refreshing the | 172 // the UI to be updated in-place rather than refreshing the |
| 100 // whole page. | 173 // whole page. |
| 101 for (var i = 0; i < container.childNodes.length; ++i) { | 174 for (var i = 0; i < container.childNodes.length; ++i) { |
| 102 if (container.childNodes[i].partition_path == partition_path) { | 175 if (container.childNodes[i].partition_path == partition_path) { |
| 103 template = container.childNodes[i]; | 176 template = container.childNodes[i]; |
| 104 } | 177 } |
| 105 } | 178 } |
| 106 | |
| 107 // This is probably the first time we're loading. | 179 // This is probably the first time we're loading. |
| 108 if (!template) { | 180 if (!template) { |
| 109 template = jstGetTemplate('serviceworker-list-template'); | 181 template = jstGetTemplate('serviceworker-list-template'); |
| 110 container.appendChild(template); | 182 container.appendChild(template); |
| 111 } | 183 } |
| 112 | 184 var fillLogFunc = fillLogForVersion.bind(this, partition_id); |
| 113 // Set log for each worker versions. | 185 stored_registrations.forEach(function (registration) { |
| 114 if (!(partition_id in allLogMessages)) { | 186 [registration.active, registration.pending].forEach(fillLogFunc); |
| 115 allLogMessages[partition_id] = {}; | |
| 116 } | |
| 117 var logMessages = allLogMessages[partition_id]; | |
| 118 registrations.forEach(function (worker) { | |
| 119 [worker.active, worker.pending].forEach(function (version) { | |
| 120 if (version) { | |
| 121 if (version.version_id in logMessages) { | |
| 122 version.log = logMessages[version.version_id]; | |
| 123 } else { | |
| 124 version.log = ''; | |
| 125 } | |
| 126 } | |
| 127 }); | |
| 128 }); | 187 }); |
| 129 | 188 unregistered_registrations.forEach(function (registration) { |
| 130 jstProcess(new JsEvalContext({ registrations: registrations, | 189 [registration.active, registration.pending].forEach(fillLogFunc); |
| 131 partition_id: partition_id, | 190 }); |
| 132 partition_path: partition_path}), | 191 unregistered_versions.forEach(fillLogFunc); |
| 192 jstProcess(new JsEvalContext({ | |
| 193 stored_registrations: stored_registrations, | |
| 194 unregistered_registrations: unregistered_registrations, | |
| 195 unregistered_versions: unregistered_versions, | |
| 196 partition_id: partition_id, | |
| 197 partition_path: partition_path}), | |
| 133 template); | 198 template); |
| 134 for (var i = 0; i < COMMANDS.length; ++i) { | 199 for (var i = 0; i < COMMANDS.length; ++i) { |
| 135 var handler = commandHandler(COMMANDS[i]); | 200 var handler = commandHandler(COMMANDS[i]); |
| 136 var links = container.querySelectorAll('button.' + COMMANDS[i]); | 201 var links = container.querySelectorAll('button.' + COMMANDS[i]); |
| 137 for (var j = 0; j < links.length; ++j) { | 202 for (var j = 0; j < links.length; ++j) { |
| 138 if (!links[j].hasClickEvent) { | 203 if (!links[j].hasClickEvent) { |
| 139 links[j].addEventListener('click', handler, false); | 204 links[j].addEventListener('click', handler, false); |
| 140 links[j].hasClickEvent = true; | 205 links[j].hasClickEvent = true; |
| 141 } | 206 } |
| 142 } | 207 } |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 200 var logArea = logAreas[i]; | 265 var logArea = logAreas[i]; |
| 201 if (logArea.partition_id == partition_id && | 266 if (logArea.partition_id == partition_id && |
| 202 logArea.version_id == version_id) { | 267 logArea.version_id == version_id) { |
| 203 logArea.value += message; | 268 logArea.value += message; |
| 204 } | 269 } |
| 205 } | 270 } |
| 206 } | 271 } |
| 207 | 272 |
| 208 return { | 273 return { |
| 209 initialize: initialize, | 274 initialize: initialize, |
| 210 update: update, | |
| 211 onOperationComplete: onOperationComplete, | 275 onOperationComplete: onOperationComplete, |
| 212 onPartitionData: onPartitionData, | 276 onPartitionData: onPartitionData, |
| 213 onWorkerStarted: onWorkerStarted, | 277 onWorkerStarted: onWorkerStarted, |
| 214 onWorkerStopped: onWorkerStopped, | 278 onWorkerStopped: onWorkerStopped, |
| 215 onErrorReported: onErrorReported, | 279 onErrorReported: onErrorReported, |
| 216 onConsoleMessageReported: onConsoleMessageReported, | 280 onConsoleMessageReported: onConsoleMessageReported, |
| 217 onVersionStateChanged: onVersionStateChanged, | 281 onVersionStateChanged: onVersionStateChanged, |
| 218 onRegistrationStored: onRegistrationStored, | 282 onRegistrationStored: onRegistrationStored, |
| 219 onRegistrationDeleted: onRegistrationDeleted, | 283 onRegistrationDeleted: onRegistrationDeleted, |
| 220 }; | 284 }; |
| 221 }); | 285 }); |
| 222 | 286 |
| 223 document.addEventListener('DOMContentLoaded', serviceworker.initialize); | 287 document.addEventListener('DOMContentLoaded', serviceworker.initialize); |
| OLD | NEW |