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 'use strict'; | 5 'use strict'; |
| 6 | 6 |
| 7 var pushData = new FutureData(); | 7 var resultQueue = new ResultQueue(); |
|
Peter Beverloo
2015/01/13 16:53:38
nit: since this is Chromium, variables should be n
johnme
2015/01/13 18:03:04
Actually, http://google-styleguide.googlecode.com/
| |
| 8 var pushRegistration = null; | 8 var pushRegistration = null; |
| 9 | 9 |
| 10 // Sends data back to the test. This must be in response to an earlier | 10 // Sends data back to the test. This must be in response to an earlier |
| 11 // request, but it's ok to respond asynchronously. The request blocks until | 11 // request, but it's ok to respond asynchronously. The request blocks until |
| 12 // the response is sent. | 12 // the response is sent. |
| 13 function sendResultToTest(result) { | 13 function sendResultToTest(result) { |
| 14 console.log('sendResultToTest: ' + result); | 14 console.log('sendResultToTest: ' + result); |
| 15 if (window.domAutomationController) { | 15 if (window.domAutomationController) { |
| 16 domAutomationController.send('' + result); | 16 domAutomationController.send('' + result); |
| 17 } | 17 } |
| 18 } | 18 } |
| 19 | 19 |
| 20 function sendErrorToTest(error) { | 20 function sendErrorToTest(error) { |
| 21 sendResultToTest(error.name + ' - ' + error.message); | 21 sendResultToTest(error.name + ' - ' + error.message); |
| 22 } | 22 } |
| 23 | 23 |
| 24 // A container for a single piece of data. The data does not have to be | 24 // Queue storing asynchronous results received from the Service Worker. Results |
| 25 // available yet when the getter is called, as all responses to the test are | 25 // are sent to the test when requested. |
| 26 // asynchronous. | 26 function ResultQueue() { |
|
Michael van Ouwerkerk
2015/01/13 11:14:51
I don't mind a more powerful class here, but in th
Peter Beverloo
2015/01/13 16:53:38
Thanks for checking!
I think that the additional
| |
| 27 function FutureData() { | 27 // Invariant: this.queue.length == 0 || this.pendingGets == 0 |
| 28 this.data = null; | 28 this.queue = []; |
| 29 this.waiting = false; | 29 this.pendingGets = 0; |
| 30 } | 30 } |
| 31 | 31 |
| 32 // Sends the data to the test if it is available. Otherwise sets the | 32 // Adds a data item to the queue. Will be sent to the test if there are |
| 33 // |waiting| flag. | 33 // pendingGets. |
| 34 FutureData.prototype.get = function() { | 34 ResultQueue.prototype.push = function(data) { |
| 35 if (this.data) { | 35 if (this.pendingGets > 0) { |
| 36 sendResultToTest(this.data); | 36 this.pendingGets--; |
| 37 sendResultToTest(data); | |
| 37 } else { | 38 } else { |
| 38 this.waiting = true; | 39 this.queue.unshift(data); |
| 39 } | 40 } |
| 40 }; | 41 }; |
| 41 | 42 |
| 42 // Sets a new data value. If the |waiting| flag is on, it is turned off and | 43 // Called by native. Sends the next data item to the test if it is available. |
| 43 // the data is sent to the test. | 44 // Otherwise increments pendingGets so it will be delivered when received. |
| 44 FutureData.prototype.set = function(data) { | 45 ResultQueue.prototype.pop = function() { |
| 45 this.data = data; | 46 if (this.queue.length) { |
| 46 if (this.waiting) { | 47 sendResultToTest(this.queue.pop()); |
| 47 sendResultToTest(data); | 48 } else { |
| 48 this.waiting = false; | 49 this.pendingGets++; |
| 49 } | 50 } |
| 50 }; | 51 }; |
| 51 | 52 |
| 52 FutureData.prototype.getImmediately = function() { | 53 // Called by native. Immediately sends the next data item to the test if it is |
| 53 sendResultToTest(this.data); | 54 // available, otherwise sends null. |
| 55 ResultQueue.prototype.popImmediately = function() { | |
| 56 sendResultToTest(this.queue.length ? this.queue.pop() : null); | |
| 54 }; | 57 }; |
| 55 | 58 |
| 56 // Notification permission has been coalesced with Push permission. After | 59 // Notification permission has been coalesced with Push permission. After |
| 57 // this is granted, Push API registration can succeed. | 60 // this is granted, Push API registration can succeed. |
| 58 function requestNotificationPermission() { | 61 function requestNotificationPermission() { |
| 59 Notification.requestPermission(function(permission) { | 62 Notification.requestPermission(function(permission) { |
| 60 sendResultToTest('permission status - ' + permission); | 63 sendResultToTest('permission status - ' + permission); |
| 61 }); | 64 }); |
| 62 } | 65 } |
| 63 | 66 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 | 128 |
| 126 pushRegistration.unregister().then(function(result) { | 129 pushRegistration.unregister().then(function(result) { |
| 127 sendResultToTest('unregister result: ' + result); | 130 sendResultToTest('unregister result: ' + result); |
| 128 }, function(error) { | 131 }, function(error) { |
| 129 sendResultToTest('unregister error: ' + error.name + ': ' + error.message); | 132 sendResultToTest('unregister error: ' + error.name + ': ' + error.message); |
| 130 }); | 133 }); |
| 131 } | 134 } |
| 132 | 135 |
| 133 addEventListener('message', function(event) { | 136 addEventListener('message', function(event) { |
| 134 var message = JSON.parse(event.data); | 137 var message = JSON.parse(event.data); |
| 135 if (message.type == 'push') { | 138 if (message.type == 'push') |
| 136 pushData.set(message.data); | 139 resultQueue.push(message.data); |
| 137 } | |
| 138 }, false); | 140 }, false); |
| OLD | NEW |