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