OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 // Accept messages from the test JavaScript to trigger worker based tests. |
| 6 self.addEventListener('message', function (event) { |
| 7 if (event.data.command == 'workerGet') { |
| 8 workerGetBudget(); |
| 9 } else if (event.data.command == 'workerReserve') { |
| 10 workerReserve(); |
| 11 } else { |
| 12 sendMessageToClients('message', 'error - unknown message request'); |
| 13 return; |
| 14 } |
| 15 }); |
| 16 |
| 17 // Query for the budget and return the current total. |
| 18 function workerGetBudget() { |
| 19 navigator.budget.getBudget().then(function(budget) { |
| 20 sendMessageToClients('message', |
| 21 'ok - budget returned value of ' + budget[0].budgetAt); |
| 22 }, function() { |
| 23 sendMessageToClients('message', 'failed - unable to get budget values'); |
| 24 }); |
| 25 } |
| 26 |
| 27 // Request a reservation for a silent push. |
| 28 function workerReserve() { |
| 29 navigator.budget.reserve('silent-push').then(function(reserved) { |
| 30 if (reserved) |
| 31 sendMessageToClients('message', 'ok - reserved budget'); |
| 32 else |
| 33 sendMessageToClients('message', 'failed - not able to reserve budget'); |
| 34 }, function() { |
| 35 sendMessageToClients('message', |
| 36 'failed - error while trying to reserve budget'); |
| 37 }); |
| 38 } |
| 39 |
| 40 function sendMessageToClients(type, data) { |
| 41 const message = JSON.stringify({ |
| 42 'type': type, |
| 43 'data': data |
| 44 }); |
| 45 clients.matchAll().then(function(clients) { |
| 46 clients.forEach(function(client) { |
| 47 client.postMessage(message); |
| 48 }); |
| 49 }, function(error) { |
| 50 console.log(error); |
| 51 }); |
| 52 } |
OLD | NEW |