OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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 // Queue storing asynchronous results received from the Service Worker. Results |
| 6 // are sent to the test when requested. |
| 7 function ResultQueue() { |
| 8 // Invariant: this.queue.length == 0 || this.pendingGets == 0 |
| 9 this.queue = []; |
| 10 this.pendingGets = 0; |
| 11 } |
| 12 |
| 13 // Adds a data item to the queue. Will be sent to the test if there are |
| 14 // pendingGets. |
| 15 ResultQueue.prototype.push = function(data) { |
| 16 if (this.pendingGets > 0) { |
| 17 this.pendingGets--; |
| 18 sendResultToTest(data); |
| 19 } else { |
| 20 this.queue.unshift(data); |
| 21 } |
| 22 }; |
| 23 |
| 24 // Called by native. Sends the next data item to the test if it is available. |
| 25 // Otherwise increments pendingGets so it will be delivered when received. |
| 26 ResultQueue.prototype.pop = function() { |
| 27 if (this.queue.length) { |
| 28 sendResultToTest(this.queue.pop()); |
| 29 } else { |
| 30 this.pendingGets++; |
| 31 } |
| 32 }; |
| 33 |
| 34 // Called by native. Immediately sends the next data item to the test if it is |
| 35 // available, otherwise sends null. |
| 36 ResultQueue.prototype.popImmediately = function() { |
| 37 sendResultToTest(this.queue.length ? this.queue.pop() : null); |
| 38 }; |
| 39 |
| 40 // Sends data back to the test. This must be in response to an earlier |
| 41 // request, but it's ok to respond asynchronously. The request blocks until |
| 42 // the response is sent. |
| 43 function sendResultToTest(result) { |
| 44 console.log('sendResultToTest: ' + result); |
| 45 if (window.domAutomationController) { |
| 46 domAutomationController.send('' + result); |
| 47 } |
| 48 } |
| 49 |
| 50 function sendErrorToTest(error) { |
| 51 sendResultToTest(error.name + ' - ' + error.message); |
| 52 } |
OLD | NEW |