| 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 // The ResultQueue is a mechanism for passing messages back to the test | 7 // The ResultQueue is a mechanism for passing messages back to the test |
| 8 // framework. | 8 // framework. |
| 9 var resultQueue = new ResultQueue(); | 9 var resultQueue = new ResultQueue(); |
| 10 | 10 |
| 11 var pushSubscriptionOptions = { | |
| 12 userVisibleOnly: true | |
| 13 }; | |
| 14 | |
| 15 // Waits for the given ServiceWorkerRegistration to become ready. | 11 // Waits for the given ServiceWorkerRegistration to become ready. |
| 16 // Shim for https://github.com/w3c/ServiceWorker/issues/770. | 12 // Shim for https://github.com/w3c/ServiceWorker/issues/770. |
| 17 function swRegistrationReady(reg) { | 13 function swRegistrationReady(reg) { |
| 18 return new Promise((resolve, reject) => { | 14 return new Promise((resolve, reject) => { |
| 19 if (reg.active) { | 15 if (reg.active) { |
| 20 resolve(); | 16 resolve(); |
| 21 return; | 17 return; |
| 22 } | 18 } |
| 23 | 19 |
| 24 if (!reg.installing && !reg.waiting) { | 20 if (!reg.installing && !reg.waiting) { |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 sendResultToTest('sender id removed from manifest'); | 83 sendResultToTest('sender id removed from manifest'); |
| 88 } else { | 84 } else { |
| 89 sendResultToTest('unable to find manifest element'); | 85 sendResultToTest('unable to find manifest element'); |
| 90 } | 86 } |
| 91 } | 87 } |
| 92 | 88 |
| 93 // This is the old style of push subscriptions which we are phasing away | 89 // This is the old style of push subscriptions which we are phasing away |
| 94 // from, where the subscription used a sender ID instead of public key. | 90 // from, where the subscription used a sender ID instead of public key. |
| 95 function documentSubscribePushWithoutKey() { | 91 function documentSubscribePushWithoutKey() { |
| 96 navigator.serviceWorker.ready.then(function(swRegistration) { | 92 navigator.serviceWorker.ready.then(function(swRegistration) { |
| 97 return swRegistration.pushManager.subscribe( | 93 return swRegistration.pushManager.subscribe({userVisibleOnly: true}) |
| 98 pushSubscriptionOptions) | |
| 99 .then(function(subscription) { | 94 .then(function(subscription) { |
| 100 sendResultToTest(subscription.endpoint); | 95 sendResultToTest(subscription.endpoint); |
| 101 }); | 96 }); |
| 97 }).catch(sendErrorToTest); |
| 98 } |
| 99 |
| 100 function documentSubscribePushWithEmptyOptions() { |
| 101 navigator.serviceWorker.ready.then(function(swRegistration) { |
| 102 return swRegistration.pushManager.subscribe() |
| 103 .then(function(subscription) { |
| 104 sendResultToTest(subscription.endpoint); |
| 105 }); |
| 102 }).catch(sendErrorToTest); | 106 }).catch(sendErrorToTest); |
| 103 } | 107 } |
| 104 | 108 |
| 105 function documentSubscribePush() { | 109 function documentSubscribePush() { |
| 106 navigator.serviceWorker.ready.then(function(swRegistration) { | 110 navigator.serviceWorker.ready.then(function(swRegistration) { |
| 107 pushSubscriptionOptions.applicationServerKey = kApplicationServerKey.buffer; | 111 return swRegistration.pushManager.subscribe({ |
| 108 return swRegistration.pushManager.subscribe(pushSubscriptionOptions) | 112 userVisibleOnly: true, |
| 113 applicationServerKey: kApplicationServerKey.buffer |
| 114 }) |
| 109 .then(function(subscription) { | 115 .then(function(subscription) { |
| 110 sendResultToTest(subscription.endpoint); | 116 sendResultToTest(subscription.endpoint); |
| 111 }); | 117 }); |
| 112 }).catch(sendErrorToTest); | 118 }).catch(sendErrorToTest); |
| 113 } | 119 } |
| 114 | 120 |
| 115 function documentSubscribePushBadKey() { | 121 function documentSubscribePushBadKey() { |
| 116 navigator.serviceWorker.ready.then(function(swRegistration) { | 122 navigator.serviceWorker.ready.then(function(swRegistration) { |
| 117 var invalidApplicationServerKey = new Uint8Array(300); | 123 var invalidApplicationServerKey = new Uint8Array(300); |
| 118 invalidApplicationServerKey.fill('0x05', 1, 300); | 124 invalidApplicationServerKey.fill('0x05', 1, 300); |
| 119 pushSubscriptionOptions.applicationServerKey = | 125 return swRegistration.pushManager.subscribe({ |
| 120 invalidApplicationServerKey.buffer; | 126 userVisibleOnly: true, |
| 121 return swRegistration.pushManager.subscribe(pushSubscriptionOptions) | 127 applicationServerKey: invalidApplicationServerKey.buffer |
| 128 }) |
| 122 .then(function(subscription) { | 129 .then(function(subscription) { |
| 123 sendResultToTest(subscription.endpoint); | 130 sendResultToTest(subscription.endpoint); |
| 124 }); | 131 }); |
| 125 }).catch(sendErrorToTest); | 132 }).catch(sendErrorToTest); |
| 126 } | 133 } |
| 127 | 134 |
| 128 function workerSubscribePush() { | 135 function workerSubscribePush() { |
| 129 // Send the message to the worker for it to subscribe | 136 // Send the message to the worker for it to subscribe |
| 130 navigator.serviceWorker.controller.postMessage({command: 'workerSubscribe'}); | 137 navigator.serviceWorker.controller.postMessage({command: 'workerSubscribe'}); |
| 131 } | 138 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 142 return swRegistration.pushManager.getSubscription() | 149 return swRegistration.pushManager.getSubscription() |
| 143 .then(function(subscription) { | 150 .then(function(subscription) { |
| 144 sendResultToTest(btoa(String.fromCharCode.apply(null, | 151 sendResultToTest(btoa(String.fromCharCode.apply(null, |
| 145 new Uint8Array(subscription.getKey('p256dh'))))); | 152 new Uint8Array(subscription.getKey('p256dh'))))); |
| 146 }); | 153 }); |
| 147 }).catch(sendErrorToTest); | 154 }).catch(sendErrorToTest); |
| 148 } | 155 } |
| 149 | 156 |
| 150 function permissionState() { | 157 function permissionState() { |
| 151 navigator.serviceWorker.ready.then(function(swRegistration) { | 158 navigator.serviceWorker.ready.then(function(swRegistration) { |
| 152 return swRegistration.pushManager.permissionState(pushSubscriptionOptions) | 159 return swRegistration.pushManager.permissionState({userVisibleOnly: true}) |
| 153 .then(function(permission) { | 160 .then(function(permission) { |
| 154 sendResultToTest('permission status - ' + permission); | 161 sendResultToTest('permission status - ' + permission); |
| 155 }); | 162 }); |
| 156 }).catch(sendErrorToTest); | 163 }).catch(sendErrorToTest); |
| 157 } | 164 } |
| 158 | 165 |
| 159 function isControlled() { | 166 function isControlled() { |
| 160 if (navigator.serviceWorker.controller) { | 167 if (navigator.serviceWorker.controller) { |
| 161 sendResultToTest('true - is controlled'); | 168 sendResultToTest('true - is controlled'); |
| 162 } else { | 169 } else { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 }).catch(sendErrorToTest); | 218 }).catch(sendErrorToTest); |
| 212 } | 219 } |
| 213 | 220 |
| 214 navigator.serviceWorker.addEventListener('message', function(event) { | 221 navigator.serviceWorker.addEventListener('message', function(event) { |
| 215 var message = JSON.parse(event.data); | 222 var message = JSON.parse(event.data); |
| 216 if (message.type == 'push') | 223 if (message.type == 'push') |
| 217 resultQueue.push(message.data); | 224 resultQueue.push(message.data); |
| 218 else | 225 else |
| 219 sendResultToTest(message.data); | 226 sendResultToTest(message.data); |
| 220 }, false); | 227 }, false); |
| OLD | NEW |