OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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. |
| 4 |
| 5 cr.define('serviceworker', function() { |
| 6 'use strict'; |
| 7 |
| 8 function update() { |
| 9 chrome.send('getAllRegistrations'); |
| 10 } |
| 11 |
| 12 function progressNodeFor(link) { |
| 13 return link.parentNode.querySelector('.operation-status'); |
| 14 } |
| 15 |
| 16 // All commands are sent with the partition_path and scope, and |
| 17 // are all completed with 'onOperationComplete'. |
| 18 var COMMANDS = ['unregister', 'start', 'stop']; |
| 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, |
| 24 link.scope]); |
| 25 return false; |
| 26 }; |
| 27 }; |
| 28 |
| 29 function withNode(selector, partition_path, scope, callback) { |
| 30 var links = document.querySelectorAll(selector); |
| 31 for (var i = 0; i < links.length; ++i) { |
| 32 var link = links[i]; |
| 33 if (partition_path == link.partition_path && |
| 34 scope == link.scope) { |
| 35 callback(link); |
| 36 } |
| 37 } |
| 38 } |
| 39 |
| 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'; |
| 45 }); |
| 46 update(); |
| 47 } |
| 48 |
| 49 // Fired once per partition from the backend. |
| 50 function onPartitionData(registrations, partition_path) { |
| 51 var template; |
| 52 var container = $('serviceworker-list'); |
| 53 |
| 54 // Existing templates are keyed by partition_path. This allows |
| 55 // the UI to be updated in-place rather than refreshing the |
| 56 // whole page. |
| 57 for (var i = 0; i < container.childNodes.length; ++i) { |
| 58 if (container.childNodes[i].partition_path == partition_path) { |
| 59 template = container.childNodes[i]; |
| 60 } |
| 61 } |
| 62 |
| 63 // This is probably the first time we're loading. |
| 64 if (!template) { |
| 65 template = jstGetTemplate('serviceworker-list-template'); |
| 66 container.appendChild(template); |
| 67 } |
| 68 |
| 69 jstProcess(new JsEvalContext({ registrations: registrations, |
| 70 partition_path: partition_path}), |
| 71 template); |
| 72 for (var i = 0; i < COMMANDS.length; ++i) { |
| 73 var handler = commandHandler(COMMANDS[i]); |
| 74 var links = container.querySelectorAll('button.' + COMMANDS[i]); |
| 75 for (var j = 0; j < links.length; ++j) { |
| 76 links[j].addEventListener('click', handler, false); |
| 77 } |
| 78 } |
| 79 } |
| 80 |
| 81 return { |
| 82 update: update, |
| 83 onOperationComplete: onOperationComplete, |
| 84 onPartitionData: onPartitionData, |
| 85 }; |
| 86 }); |
| 87 |
| 88 document.addEventListener('DOMContentLoaded', serviceworker.update); |
OLD | NEW |