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 | 91 |
91 function registerPush() { | 92 function registerPush() { |
92 navigator.serviceWorker.ready.then(function(swRegistration) { | 93 navigator.serviceWorker.ready.then(function(swRegistration) { |
93 return swRegistration.pushManager.register() | 94 return swRegistration.pushManager.register() |
94 .then(function(pushRegistration) { | 95 .then(function(registration) { |
95 // TODO: Cleanup once the final API is exposed. | 96 // TODO: Cleanup once the final API is exposed. |
| 97 pushRegistration = registration; |
96 var endpoint = pushRegistration.endpoint || | 98 var endpoint = pushRegistration.endpoint || |
97 pushRegistration.pushEndpoint; | 99 pushRegistration.pushEndpoint; |
98 var registrationId = pushRegistration.registrationId || | 100 var registrationId = pushRegistration.registrationId || |
99 pushRegistration.pushRegistrationId; | 101 pushRegistration.pushRegistrationId; |
100 sendResultToTest(endpoint + ' - ' + registrationId); | 102 sendResultToTest(endpoint + ' - ' + registrationId); |
101 }); | 103 }); |
102 }).catch(sendErrorToTest); | 104 }).catch(sendErrorToTest); |
103 } | 105 } |
104 | 106 |
105 function hasPermission() { | 107 function hasPermission() { |
106 navigator.serviceWorker.ready.then(function(swRegistration) { | 108 navigator.serviceWorker.ready.then(function(swRegistration) { |
107 return swRegistration.pushManager.hasPermission() | 109 return swRegistration.pushManager.hasPermission() |
108 .then(function(permission) { | 110 .then(function(permission) { |
109 sendResultToTest('permission status - ' + permission); | 111 sendResultToTest('permission status - ' + permission); |
110 }); | 112 }); |
111 }).catch(sendErrorToTest); | 113 }).catch(sendErrorToTest); |
112 } | 114 } |
113 | 115 |
114 function isControlled() { | 116 function isControlled() { |
115 if (navigator.serviceWorker.controller) { | 117 if (navigator.serviceWorker.controller) { |
116 sendResultToTest('true - is controlled'); | 118 sendResultToTest('true - is controlled'); |
117 } else { | 119 } else { |
118 sendResultToTest('false - is not controlled'); | 120 sendResultToTest('false - is not controlled'); |
119 } | 121 } |
120 } | 122 } |
121 | 123 |
| 124 function unregister() { |
| 125 if (!pushRegistration) { |
| 126 sendResultToTest('unregister error: no registration'); |
| 127 return; |
| 128 } |
| 129 |
| 130 pushRegistration.unregister().then(function(result) { |
| 131 sendResultToTest('unregister result: ' + result); |
| 132 }, function(error) { |
| 133 sendResultToTest('unregister error: ' + error.name + ': ' + error.message); |
| 134 }); |
| 135 } |
| 136 |
122 addEventListener('message', function(event) { | 137 addEventListener('message', function(event) { |
123 var message = JSON.parse(event.data); | 138 var message = JSON.parse(event.data); |
124 if (message.type == 'push') { | 139 if (message.type == 'push') { |
125 pushData.set(message.data); | 140 pushData.set(message.data); |
126 } | 141 } |
127 }, false); | 142 }, false); |
OLD | NEW |