| 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 pushData = new FutureData(); |
| 8 var pushRegistration = null; |
| 8 | 9 |
| 9 // 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 |
| 10 // 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 |
| 11 // the response is sent. | 12 // the response is sent. |
| 12 function sendResultToTest(result) { | 13 function sendResultToTest(result) { |
| 13 console.log('sendResultToTest: ' + result); | 14 console.log('sendResultToTest: ' + result); |
| 14 if (window.domAutomationController) { | 15 if (window.domAutomationController) { |
| 15 domAutomationController.send('' + result); | 16 domAutomationController.send('' + result); |
| 16 } | 17 } |
| 17 } | 18 } |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 element.parentNode.removeChild(element); | 85 element.parentNode.removeChild(element); |
| 85 sendResultToTest('manifest removed'); | 86 sendResultToTest('manifest removed'); |
| 86 } else | 87 } else |
| 87 sendResultToTest('unable to find manifest element'); | 88 sendResultToTest('unable to find manifest element'); |
| 88 } | 89 } |
| 89 | 90 |
| 90 function registerPush() { | 91 function registerPush() { |
| 91 navigator.serviceWorker.ready.then(function(swRegistration) { | 92 navigator.serviceWorker.ready.then(function(swRegistration) { |
| 92 // TODO(mvanouwerkerk): Cleanup once the final API is exposed. | 93 // TODO(mvanouwerkerk): Cleanup once the final API is exposed. |
| 93 var pushManager = swRegistration.pushManager || navigator.push; | 94 var pushManager = swRegistration.pushManager || navigator.push; |
| 94 return pushManager.register().then(function(pushRegistration) { | 95 return pushManager.register().then(function(registration) { |
| 95 sendResultToTest(pushRegistration.pushEndpoint + ' - ' + | 96 pushRegistration = registration; |
| 96 pushRegistration.pushRegistrationId); | 97 sendResultToTest(registration.pushEndpoint + ' - ' + |
| 98 registration.pushRegistrationId); |
| 97 }); | 99 }); |
| 98 }).catch(sendErrorToTest); | 100 }).catch(sendErrorToTest); |
| 99 } | 101 } |
| 100 | 102 |
| 101 function hasPermission() { | 103 function hasPermission() { |
| 102 navigator.serviceWorker.ready.then(function(swRegistration) { | 104 navigator.serviceWorker.ready.then(function(swRegistration) { |
| 103 // TODO(mvanouwerkerk): Cleanup once the final API is exposed. | 105 // TODO(mvanouwerkerk): Cleanup once the final API is exposed. |
| 104 var pushManager = swRegistration.pushManager || navigator.push; | 106 var pushManager = swRegistration.pushManager || navigator.push; |
| 105 return pushManager.hasPermission().then(function(permission) { | 107 return pushManager.hasPermission().then(function(permission) { |
| 106 sendResultToTest('permission status - ' + permission); | 108 sendResultToTest('permission status - ' + permission); |
| 107 }); | 109 }); |
| 108 }).catch(sendErrorToTest); | 110 }).catch(sendErrorToTest); |
| 109 } | 111 } |
| 110 | 112 |
| 111 function isControlled() { | 113 function isControlled() { |
| 112 if (navigator.serviceWorker.controller) { | 114 if (navigator.serviceWorker.controller) { |
| 113 sendResultToTest('true - is controlled'); | 115 sendResultToTest('true - is controlled'); |
| 114 } else { | 116 } else { |
| 115 sendResultToTest('false - is not controlled'); | 117 sendResultToTest('false - is not controlled'); |
| 116 } | 118 } |
| 117 } | 119 } |
| 118 | 120 |
| 121 function unregister() { |
| 122 if (!pushRegistration) { |
| 123 sendErrorToTest('no registration'); |
| 124 return; |
| 125 } |
| 126 |
| 127 pushRegistration.unregister().then(function(result) { |
| 128 sendResultToTest('unregister result: ' + result); |
| 129 }); |
| 130 } |
| 131 |
| 119 addEventListener('message', function(event) { | 132 addEventListener('message', function(event) { |
| 120 var message = JSON.parse(event.data); | 133 var message = JSON.parse(event.data); |
| 121 if (message.type == 'push') { | 134 if (message.type == 'push') { |
| 122 pushData.set(message.data); | 135 pushData.set(message.data); |
| 123 } | 136 } |
| 124 }, false); | 137 }, false); |
| OLD | NEW |