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 resultQueue = new ResultQueue(); | 7 var resultQueue = new ResultQueue(); |
8 var pushRegistration = null; | 8 var pushRegistration = null; |
9 | 9 |
10 // 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 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 function removeManifest() { | 85 function removeManifest() { |
86 var element = document.querySelector('link[rel="manifest"]'); | 86 var element = document.querySelector('link[rel="manifest"]'); |
87 if (element) { | 87 if (element) { |
88 element.parentNode.removeChild(element); | 88 element.parentNode.removeChild(element); |
89 sendResultToTest('manifest removed'); | 89 sendResultToTest('manifest removed'); |
90 } else { | 90 } else { |
91 sendResultToTest('unable to find manifest element'); | 91 sendResultToTest('unable to find manifest element'); |
92 } | 92 } |
93 } | 93 } |
94 | 94 |
| 95 // TODO(xiang): Remove this function after "ready" CL landed. |
| 96 function getReadyRegistration() { |
| 97 return navigator.serviceWorker.getRegistration().then(function(r) { |
| 98 if (r.active) |
| 99 return r; |
| 100 var worker = r.waiting ? r.waiting : r.installing; |
| 101 return new Promise(function(resolve) { |
| 102 worker.onstatechange = function() { |
| 103 if (worker.state === 'activating') |
| 104 resolve(r); |
| 105 }; |
| 106 }); |
| 107 }); |
| 108 } |
| 109 |
95 function registerPush() { | 110 function registerPush() { |
96 navigator.serviceWorker.ready.then(function(swRegistration) { | 111 getReadyRegistration().then(function(swRegistration) { |
97 var registerMethodName = | 112 var registerMethodName = |
98 swRegistration.pushManager.register ? 'register' : 'subscribe'; | 113 swRegistration.pushManager.register ? 'register' : 'subscribe'; |
99 return swRegistration.pushManager[registerMethodName]() | 114 return swRegistration.pushManager[registerMethodName]() |
100 .then(function(registration) { | 115 .then(function(registration) { |
101 pushRegistration = registration; | 116 pushRegistration = registration; |
102 sendResultToTest(registration.endpoint + ' - ' + | 117 sendResultToTest(registration.endpoint + ' - ' + |
103 (registration.registrationId || registration.subscriptionId)); | 118 (registration.registrationId || registration.subscriptionId)); |
104 }); | 119 }); |
105 }).catch(sendErrorToTest); | 120 }).catch(sendErrorToTest); |
106 } | 121 } |
107 | 122 |
108 function hasPermission() { | 123 function hasPermission() { |
109 navigator.serviceWorker.ready.then(function(swRegistration) { | 124 getReadyRegistration().then(function(swRegistration) { |
110 return swRegistration.pushManager.hasPermission() | 125 return swRegistration.pushManager.hasPermission() |
111 .then(function(permission) { | 126 .then(function(permission) { |
112 sendResultToTest('permission status - ' + permission); | 127 sendResultToTest('permission status - ' + permission); |
113 }); | 128 }); |
114 }).catch(sendErrorToTest); | 129 }).catch(sendErrorToTest); |
115 } | 130 } |
116 | 131 |
117 function isControlled() { | 132 function isControlled() { |
118 if (navigator.serviceWorker.controller) { | 133 if (navigator.serviceWorker.controller) { |
119 sendResultToTest('true - is controlled'); | 134 sendResultToTest('true - is controlled'); |
(...skipping 11 matching lines...) Expand all Loading... |
131 var unregisterMethodName = | 146 var unregisterMethodName = |
132 pushRegistration.unregister ? 'unregister' : 'unsubscribe'; | 147 pushRegistration.unregister ? 'unregister' : 'unsubscribe'; |
133 pushRegistration[unregisterMethodName]().then(function(result) { | 148 pushRegistration[unregisterMethodName]().then(function(result) { |
134 sendResultToTest('unregister result: ' + result); | 149 sendResultToTest('unregister result: ' + result); |
135 }, function(error) { | 150 }, function(error) { |
136 sendResultToTest('unregister error: ' + error.name + ': ' + error.message); | 151 sendResultToTest('unregister error: ' + error.name + ': ' + error.message); |
137 }); | 152 }); |
138 } | 153 } |
139 | 154 |
140 function hasRegistration() { | 155 function hasRegistration() { |
141 navigator.serviceWorker.ready.then(function(swRegistration) { | 156 getReadyRegistration().then(function(swRegistration) { |
142 return swRegistration.pushManager.getSubscription(); | 157 return swRegistration.pushManager.getSubscription(); |
143 }).then(function(subscription) { | 158 }).then(function(subscription) { |
144 sendResultToTest(subscription ? 'true - registered' | 159 sendResultToTest(subscription ? 'true - registered' |
145 : 'false - not registered'); | 160 : 'false - not registered'); |
146 }).catch(sendErrorToTest); | 161 }).catch(sendErrorToTest); |
147 } | 162 } |
148 | 163 |
149 addEventListener('message', function(event) { | 164 addEventListener('message', function(event) { |
150 var message = JSON.parse(event.data); | 165 var message = JSON.parse(event.data); |
151 if (message.type == 'push') | 166 if (message.type == 'push') |
152 resultQueue.push(message.data); | 167 resultQueue.push(message.data); |
153 }, false); | 168 }, false); |
OLD | NEW |