| 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 // The ResultQueue is a mechanism for passing messages back to the test |
| 8 // framework. |
| 7 var resultQueue = new ResultQueue(); | 9 var resultQueue = new ResultQueue(); |
| 8 | 10 |
| 9 var pushSubscriptionOptions = { | 11 var pushSubscriptionOptions = { |
| 10 userVisibleOnly: true | 12 userVisibleOnly: true |
| 11 }; | 13 }; |
| 12 | 14 |
| 13 // Sends data back to the test. This must be in response to an earlier | |
| 14 // request, but it's ok to respond asynchronously. The request blocks until | |
| 15 // the response is sent. | |
| 16 function sendResultToTest(result) { | |
| 17 console.log('sendResultToTest: ' + result); | |
| 18 if (window.domAutomationController) { | |
| 19 domAutomationController.send('' + result); | |
| 20 } | |
| 21 } | |
| 22 | |
| 23 function sendErrorToTest(error) { | |
| 24 sendResultToTest(error.name + ' - ' + error.message); | |
| 25 } | |
| 26 | |
| 27 // Queue storing asynchronous results received from the Service Worker. Results | |
| 28 // are sent to the test when requested. | |
| 29 function ResultQueue() { | |
| 30 // Invariant: this.queue.length == 0 || this.pendingGets == 0 | |
| 31 this.queue = []; | |
| 32 this.pendingGets = 0; | |
| 33 } | |
| 34 | |
| 35 // Adds a data item to the queue. Will be sent to the test if there are | |
| 36 // pendingGets. | |
| 37 ResultQueue.prototype.push = function(data) { | |
| 38 if (this.pendingGets > 0) { | |
| 39 this.pendingGets--; | |
| 40 sendResultToTest(data); | |
| 41 } else { | |
| 42 this.queue.unshift(data); | |
| 43 } | |
| 44 }; | |
| 45 | |
| 46 // Called by native. Sends the next data item to the test if it is available. | |
| 47 // Otherwise increments pendingGets so it will be delivered when received. | |
| 48 ResultQueue.prototype.pop = function() { | |
| 49 if (this.queue.length) { | |
| 50 sendResultToTest(this.queue.pop()); | |
| 51 } else { | |
| 52 this.pendingGets++; | |
| 53 } | |
| 54 }; | |
| 55 | |
| 56 // Called by native. Immediately sends the next data item to the test if it is | |
| 57 // available, otherwise sends null. | |
| 58 ResultQueue.prototype.popImmediately = function() { | |
| 59 sendResultToTest(this.queue.length ? this.queue.pop() : null); | |
| 60 }; | |
| 61 | |
| 62 // Notification permission has been coalesced with Push permission. After | 15 // Notification permission has been coalesced with Push permission. After |
| 63 // this is granted, Push API subscription can succeed. | 16 // this is granted, Push API subscription can succeed. |
| 64 function requestNotificationPermission() { | 17 function requestNotificationPermission() { |
| 65 Notification.requestPermission(function(permission) { | 18 Notification.requestPermission(function(permission) { |
| 66 sendResultToTest('permission status - ' + permission); | 19 sendResultToTest('permission status - ' + permission); |
| 67 }); | 20 }); |
| 68 } | 21 } |
| 69 | 22 |
| 70 function registerServiceWorker() { | 23 function registerServiceWorker() { |
| 71 // The base dir used to resolve service_worker.js and the scope depends on | 24 // The base dir used to resolve service_worker.js and the scope depends on |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 }).catch(sendErrorToTest); | 162 }).catch(sendErrorToTest); |
| 210 } | 163 } |
| 211 | 164 |
| 212 navigator.serviceWorker.addEventListener('message', function(event) { | 165 navigator.serviceWorker.addEventListener('message', function(event) { |
| 213 var message = JSON.parse(event.data); | 166 var message = JSON.parse(event.data); |
| 214 if (message.type == 'push') | 167 if (message.type == 'push') |
| 215 resultQueue.push(message.data); | 168 resultQueue.push(message.data); |
| 216 else | 169 else |
| 217 sendResultToTest(message.data); | 170 sendResultToTest(message.data); |
| 218 }, false); | 171 }, false); |
| OLD | NEW |