Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 <!DOCTYPE html> | 1 <!DOCTYPE html> |
| 2 <html> | 2 <html> |
| 3 <head> | 3 <head> |
| 4 <title>Push API Test</title> | 4 <title>Push API Test</title> |
| 5 <link rel="manifest" href="manifest.json"> | 5 <link rel="manifest" href="manifest.json"> |
| 6 <script> | 6 <script> |
| 7 var pushData = new FutureData(); | |
| 8 | |
| 9 // Sends data back to the test. This must be in response to an earlier | |
| 10 // request, but it's ok to respond asynchronously. The request blocks until | |
| 11 // the response is sent. | |
| 7 function sendResultToTest(result) { | 12 function sendResultToTest(result) { |
| 8 console.log(result); | 13 console.log('sendResultToTest: ' + result); |
| 9 if (window.domAutomationController) { | 14 if (window.domAutomationController) { |
| 10 domAutomationController.send('' + result); | 15 domAutomationController.send('' + result); |
| 11 } | 16 } |
| 12 } | 17 } |
| 13 | 18 |
| 14 function sendErrorToTest(error) { | 19 function sendErrorToTest(error) { |
| 15 sendResultToTest(error.name + ' - ' + error.message); | 20 sendResultToTest(error.name + ' - ' + error.message); |
| 16 } | 21 } |
| 17 | 22 |
| 23 // A container for a single piece of data. The data does not have to be | |
| 24 // available yet when the getter is called, as all responses to the test are | |
| 25 // asynchronous. | |
| 26 function FutureData() { | |
| 27 this.data = null; | |
| 28 this.waiting = false; | |
| 29 } | |
| 30 | |
| 31 // Sends the data to the test if it is available. Otherwise sets the | |
| 32 // |waiting| flag. | |
| 33 FutureData.prototype.get = function() { | |
| 34 if (this.data) { | |
|
Peter Beverloo
2014/10/27 15:20:49
Maybe strict check on === null? Also, isn't the pu
Michael van Ouwerkerk
2014/10/27 15:27:18
Something for a later patch, perhaps? For its curr
| |
| 35 sendResultToTest(this.data); | |
| 36 } else { | |
| 37 this.waiting = true; | |
| 38 } | |
| 39 }; | |
| 40 | |
| 41 // Sets a new data value. If the |waiting| flag is on, it is turned off and | |
| 42 // the data is sent to the test. | |
| 43 FutureData.prototype.set = function(data) { | |
| 44 this.data = data; | |
| 45 if (this.waiting) { | |
| 46 sendResultToTest(data); | |
| 47 this.waiting = false; | |
| 48 } | |
| 49 }; | |
| 50 | |
| 18 function registerServiceWorker() { | 51 function registerServiceWorker() { |
| 19 navigator.serviceWorker.register('service_worker.js', {scope: './'}).then( function(swRegistration) { | 52 navigator.serviceWorker.register('service_worker.js', {scope: './'}).then( |
| 20 console.log(swRegistration); | 53 function(swRegistration) { |
| 21 sendResultToTest('ok'); | 54 sendResultToTest('ok - service worker registered'); |
| 22 }, sendErrorToTest); | 55 }, sendErrorToTest); |
| 23 } | 56 } |
| 24 | 57 |
| 25 function registerPush() { | 58 function registerPush() { |
| 26 navigator.serviceWorker.ready.then(function() { | 59 navigator.serviceWorker.ready.then(function() { |
| 27 navigator.push.register().then(function(pushRegistration) { | 60 navigator.push.register().then(function(pushRegistration) { |
| 28 sendResultToTest(pushRegistration.pushEndpoint + ' - ' + pushRegistrat ion.pushRegistrationId); | 61 sendResultToTest(pushRegistration.pushEndpoint + ' - ' + pushRegistrat ion.pushRegistrationId); |
|
Peter Beverloo
2014/10/27 15:20:49
nit: break the line. sorry for missing this!
Michael van Ouwerkerk
2014/10/27 15:27:18
Done.
| |
| 29 }, sendErrorToTest); | 62 }, sendErrorToTest); |
| 30 }, sendErrorToTest); | 63 }, sendErrorToTest); |
| 31 } | 64 } |
| 65 | |
| 66 function isControlled() { | |
|
Peter Beverloo
2014/10/27 15:20:49
This is not being used.
Michael van Ouwerkerk
2014/10/27 15:27:18
It is, see push_messaging_browsertest.cc
| |
| 67 if (navigator.serviceWorker.controller) { | |
| 68 sendResultToTest('true - is controlled'); | |
| 69 } else { | |
| 70 sendResultToTest('false - is not controlled'); | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 addEventListener('message', function(event) { | |
| 75 var message = JSON.parse(event.data); | |
| 76 if (message.type == 'push') { | |
| 77 pushData.set(message.data); | |
| 78 } | |
| 79 }, false); | |
| 32 </script> | 80 </script> |
| 33 </head> | 81 </head> |
| 34 <body>Push API Test</body> | 82 <body> |
| 83 <h1>Push API Test</h1> | |
| 84 </body> | |
| 35 </html> | 85 </html> |
| OLD | NEW |