Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 var pushData = new FutureData(); | |
| 2 | |
| 3 // Sends data back to the test. This must be in response to an earlier | |
| 4 // request, but it's ok to respond asynchronously. The request blocks until | |
| 5 // the response is sent. | |
| 6 function sendResultToTest(result) { | |
| 7 console.log('sendResultToTest: ' + result); | |
| 8 if (window.domAutomationController) { | |
| 9 domAutomationController.send('' + result); | |
| 10 } | |
| 11 } | |
| 12 | |
| 13 function sendErrorToTest(error) { | |
| 14 sendResultToTest(error.name + ' - ' + error.message); | |
| 15 } | |
| 16 | |
| 17 // A container for a single piece of data. The data does not have to be | |
| 18 // available yet when the getter is called, as all responses to the test are | |
| 19 // asynchronous. | |
| 20 function FutureData() { | |
| 21 this.data = null; | |
| 22 this.waiting = false; | |
| 23 } | |
| 24 | |
| 25 // Sends the data to the test if it is available. Otherwise sets the | |
| 26 // |waiting| flag. | |
| 27 FutureData.prototype.get = function() { | |
| 28 if (this.data) { | |
| 29 sendResultToTest(this.data); | |
| 30 } else { | |
| 31 this.waiting = true; | |
| 32 } | |
| 33 }; | |
| 34 | |
| 35 // Sets a new data value. If the |waiting| flag is on, it is turned off and | |
| 36 // the data is sent to the test. | |
| 37 FutureData.prototype.set = function(data) { | |
| 38 this.data = data; | |
| 39 if (this.waiting) { | |
| 40 sendResultToTest(data); | |
| 41 this.waiting = false; | |
| 42 } | |
| 43 }; | |
| 44 | |
| 45 FutureData.prototype.getImmediately = function() { | |
| 46 sendResultToTest(this.data); | |
| 47 }; | |
| 48 | |
| 49 // Notification permission has been coalesced with Push permission. After | |
| 50 // this is granted, Push API registration can succeed. | |
| 51 function requestNotificationPermission() { | |
| 52 Notification.requestPermission(function(permission) { | |
| 53 sendResultToTest('permission status - ' + permission); | |
| 54 }); | |
| 55 } | |
| 56 | |
| 57 function registerServiceWorker() { | |
| 58 navigator.serviceWorker.register('service_worker.js', {scope: './'}).then( | |
| 59 function(swRegistration) { | |
| 60 sendResultToTest('ok - service worker registered'); | |
| 61 }, sendErrorToTest); | |
| 62 } | |
| 63 | |
| 64 function unregisterServiceWorker() { | |
| 65 navigator.serviceWorker.getRegistration().then(function(swRegistration) { | |
| 66 swRegistration.unregister().then(function(result) { | |
| 67 sendResultToTest('service worker unregistration status: ' + result); | |
| 68 }) | |
| 69 }).catch(sendErrorToTest); | |
| 70 } | |
| 71 | |
| 72 function removeManifest() { | |
| 73 var element = document.querySelector('link[rel="manifest"]'); | |
| 74 if (element) { | |
| 75 element.parentNode.removeChild(element); | |
| 76 sendResultToTest('manifest removed'); | |
| 77 } else | |
| 78 sendResultToTest('unable to find manifest element'); | |
| 79 } | |
| 80 | |
| 81 function registerPush() { | |
| 82 navigator.serviceWorker.ready.then(function(swRegistration) { | |
| 83 // TODO(mvanouwerkerk): Cleanup once the final API is exposed. | |
| 84 var pushManager = swRegistration.pushManager || navigator.push; | |
| 85 pushManager.register().then(function(pushRegistration) { | |
|
Bernhard Bauer
2014/12/09 12:02:08
If you return this (the result of the then() call)
Miguel Garcia
2014/12/09 14:24:32
Done.
| |
| 86 sendResultToTest(pushRegistration.pushEndpoint + ' - ' + | |
| 87 pushRegistration.pushRegistrationId); | |
| 88 }, sendErrorToTest); | |
| 89 }, sendErrorToTest); | |
| 90 } | |
| 91 | |
| 92 function hasPermission() { | |
| 93 navigator.serviceWorker.ready.then(function(swRegistration) { | |
| 94 // TODO(mvanouwerkerk): Cleanup once the final API is exposed. | |
| 95 var pushManager = swRegistration.pushManager || navigator.push; | |
| 96 pushManager.hasPermission().then(function(permission) { | |
| 97 sendResultToTest('permission status - ' + permission); | |
| 98 }, sendErrorToTest); | |
| 99 }, sendErrorToTest); | |
| 100 } | |
| 101 | |
| 102 function isControlled() { | |
| 103 if (navigator.serviceWorker.controller) { | |
| 104 sendResultToTest('true - is controlled'); | |
| 105 } else { | |
| 106 sendResultToTest('false - is not controlled'); | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 addEventListener('message', function(event) { | |
| 111 var message = JSON.parse(event.data); | |
| 112 if (message.type == 'push') { | |
| 113 pushData.set(message.data); | |
| 114 } | |
| 115 }, false); | |
| OLD | NEW |