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'; |
7 | 7 |
| 8 function initialize() { |
| 9 if (window.location.hash == "#iframe") { |
| 10 // This page is loaded from chrome://inspect. |
| 11 window.addEventListener('message', onMessage.bind(this), false); |
| 12 } |
| 13 update(); |
| 14 } |
| 15 |
| 16 function onMessage(event) { |
| 17 if (event.origin != 'chrome://inspect') { |
| 18 return; |
| 19 } |
| 20 chrome.send(event.data.action, |
| 21 [event.data.partition_path, event.data.scope]); |
| 22 } |
| 23 |
8 function update() { | 24 function update() { |
9 chrome.send('getAllRegistrations'); | 25 chrome.send('getAllRegistrations'); |
10 } | 26 } |
11 | 27 |
12 function progressNodeFor(link) { | 28 function progressNodeFor(link) { |
13 return link.parentNode.querySelector('.operation-status'); | 29 return link.parentNode.querySelector('.operation-status'); |
14 } | 30 } |
15 | 31 |
16 // All commands are sent with the partition_path and scope, and | 32 // All commands are sent with the partition_path and scope, and |
17 // are all completed with 'onOperationComplete'. | 33 // are all completed with 'onOperationComplete'. |
(...skipping 23 matching lines...) Expand all Loading... |
41 function onOperationComplete(status, path, scope) { | 57 function onOperationComplete(status, path, scope) { |
42 // refreshes the ui, displaying any relevant buttons | 58 // refreshes the ui, displaying any relevant buttons |
43 withNode('button', path, scope, function(link) { | 59 withNode('button', path, scope, function(link) { |
44 progressNodeFor(link).style.display = 'none'; | 60 progressNodeFor(link).style.display = 'none'; |
45 }); | 61 }); |
46 update(); | 62 update(); |
47 } | 63 } |
48 | 64 |
49 var allLogMessages = {}; | 65 var allLogMessages = {}; |
50 | 66 |
| 67 // Send the active ServiceWorker information to chrome://inspect. |
| 68 function sendToInspectPage(registrations, partition_id, partition_path) { |
| 69 var workers = []; |
| 70 for (var i = 0; i < registrations.length; i++) { |
| 71 var registration = registrations[i]; |
| 72 if (!registration.active || |
| 73 registration.active.running_status != 'RUNNING') { |
| 74 continue; |
| 75 } |
| 76 workers.push({ |
| 77 'partition_path': partition_path, |
| 78 'scope': registration.scope, |
| 79 'url': registration.script_url |
| 80 }) |
| 81 } |
| 82 window.parent.postMessage({ |
| 83 'partition_id': partition_id, |
| 84 'workers': workers, |
| 85 }, 'chrome://inspect') |
| 86 } |
| 87 |
51 // Fired once per partition from the backend. | 88 // Fired once per partition from the backend. |
52 function onPartitionData(registrations, partition_id, partition_path) { | 89 function onPartitionData(registrations, partition_id, partition_path) { |
| 90 if (window.location.hash == "#iframe") { |
| 91 // This page is loaded from chrome://inspect. |
| 92 sendToInspectPage(registrations, partition_id, partition_path); |
| 93 return; |
| 94 } |
53 var template; | 95 var template; |
54 var container = $('serviceworker-list'); | 96 var container = $('serviceworker-list'); |
55 | 97 |
56 // Existing templates are keyed by partition_path. This allows | 98 // Existing templates are keyed by partition_path. This allows |
57 // the UI to be updated in-place rather than refreshing the | 99 // the UI to be updated in-place rather than refreshing the |
58 // whole page. | 100 // whole page. |
59 for (var i = 0; i < container.childNodes.length; ++i) { | 101 for (var i = 0; i < container.childNodes.length; ++i) { |
60 if (container.childNodes[i].partition_path == partition_path) { | 102 if (container.childNodes[i].partition_path == partition_path) { |
61 template = container.childNodes[i]; | 103 template = container.childNodes[i]; |
62 } | 104 } |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
157 for (var i = 0; i < logAreas.length; ++i) { | 199 for (var i = 0; i < logAreas.length; ++i) { |
158 var logArea = logAreas[i]; | 200 var logArea = logAreas[i]; |
159 if (logArea.partition_id == partition_id && | 201 if (logArea.partition_id == partition_id && |
160 logArea.version_id == version_id) { | 202 logArea.version_id == version_id) { |
161 logArea.value += message; | 203 logArea.value += message; |
162 } | 204 } |
163 } | 205 } |
164 } | 206 } |
165 | 207 |
166 return { | 208 return { |
| 209 initialize: initialize, |
167 update: update, | 210 update: update, |
168 onOperationComplete: onOperationComplete, | 211 onOperationComplete: onOperationComplete, |
169 onPartitionData: onPartitionData, | 212 onPartitionData: onPartitionData, |
170 onWorkerStarted: onWorkerStarted, | 213 onWorkerStarted: onWorkerStarted, |
171 onWorkerStopped: onWorkerStopped, | 214 onWorkerStopped: onWorkerStopped, |
172 onErrorReported: onErrorReported, | 215 onErrorReported: onErrorReported, |
173 onConsoleMessageReported: onConsoleMessageReported, | 216 onConsoleMessageReported: onConsoleMessageReported, |
174 onVersionStateChanged: onVersionStateChanged, | 217 onVersionStateChanged: onVersionStateChanged, |
175 onRegistrationStored: onRegistrationStored, | 218 onRegistrationStored: onRegistrationStored, |
176 onRegistrationDeleted: onRegistrationDeleted, | 219 onRegistrationDeleted: onRegistrationDeleted, |
177 }; | 220 }; |
178 }); | 221 }); |
179 | 222 |
180 document.addEventListener('DOMContentLoaded', serviceworker.update); | 223 document.addEventListener('DOMContentLoaded', serviceworker.initialize); |
OLD | NEW |