| OLD | NEW |
| 1 "use strict"; | 1 "use strict"; |
| 2 | 2 |
| 3 // Subscribes and unsubscribes to push once so that the manifest details are sto
red in service | 3 // Subscribes and unsubscribes to push once so that the manifest details are |
| 4 // worker storage. After this, subscribe can succeed from inside the service wor
ker. | 4 // stored in service worker storage. After this, subscribe can succeed from |
| 5 // This triggers an infobar unless a permission decision was previously set. | 5 // inside the service worker. This triggers an infobar unless a permission |
| 6 // decision was previously set. |
| 6 function subscribeAndUnsubscribePush(registration) { | 7 function subscribeAndUnsubscribePush(registration) { |
| 7 return new Promise(function(resolve, reject) { | 8 return new Promise(function(resolve, reject) { |
| 8 // 1. Call subscribe in document context. The manifest details are store
d in the service | 9 // 1. Call subscribe in document context. The manifest details are stored |
| 9 // worker storage for later use in a service worker context where there
is no manifest. | 10 // in the service worker storage for later use in a service worker context |
| 10 registration.pushManager.subscribe() | 11 // where there is no manifest. |
| 11 .then(function(subscription) { | 12 registration.pushManager.subscribe().then(function(subscription) { |
| 12 // 2. Call unsubscribe so we can subscribe again later inside a
service worker. | 13 // 2. Call unsubscribe so we can subscribe again later inside a |
| 13 return subscription.unsubscribe(); | 14 // service worker. |
| 14 }) | 15 return subscription.unsubscribe(); |
| 15 .then(function(unsubscription_result) { | 16 }) |
| 16 resolve(); | 17 .then(function(unsubscription_result) { |
| 17 }) | 18 resolve(); |
| 18 .catch(function(e) { | 19 }) |
| 19 reject(e); | 20 .catch(function(e) { |
| 20 }); | 21 reject(e); |
| 21 }); | 22 }); |
| 23 }); |
| 22 } | 24 } |
| 23 | 25 |
| 24 // Runs |command| in the service worker connected to |port|. Returns a promise t
hat will be resolved | 26 // Registers a service worker and subscribes to push using the given string |
| 25 // with the response data of the command. | 27 // as an applicationServerKey. |
| 28 function registerAndSubscribePushWithString(test, serverKeyString) { |
| 29 return registerAndSubscribePush(test, |
| 30 new TextEncoder().encode(serverKeyString)); |
| 31 } |
| 32 |
| 33 // Subscribes to push with the given application server key. serverKey should be |
| 34 // a Uint8Array. |
| 35 function registerAndSubscribePush(test, serverKey) { |
| 36 const workerUrl = 'resources/empty_worker.js'; |
| 37 const workerScope = 'resources/scope/' + location.pathname; |
| 38 let swRegistration; |
| 39 |
| 40 return service_worker_unregister_and_register(test, workerUrl, workerScope) |
| 41 .then(function(serviceWorkerRegistration) { |
| 42 swRegistration = serviceWorkerRegistration; |
| 43 return wait_for_state(test, swRegistration.installing, 'activated'); |
| 44 }) |
| 45 .then(function() { |
| 46 if (window.testRunner) { |
| 47 testRunner.setPermission('push-messaging', 'granted', location.origin, |
| 48 location.origin); |
| 49 } |
| 50 return swRegistration.pushManager.subscribe({ |
| 51 userVisibleOnly: true, |
| 52 applicationServerKey: serverKey |
| 53 }); |
| 54 }); |
| 55 } |
| 56 |
| 57 // Runs |command| in the service worker connected to |port|. Returns a promise |
| 58 // that will be resolved with the response data of the command. |
| 26 function runCommandInServiceWorker(port, command) { | 59 function runCommandInServiceWorker(port, command) { |
| 27 return new Promise(function(resolve, reject) { | 60 return new Promise(function(resolve, reject) { |
| 28 port.addEventListener('message', function listener(event) { | 61 port.addEventListener('message', function listener(event) { |
| 29 // To make this listener a oneshot, remove it the first time it runs
. | 62 // To make this listener a oneshot, remove it the first time it runs. |
| 30 port.removeEventListener('message', listener); | 63 port.removeEventListener('message', listener); |
| 31 | 64 |
| 32 if (typeof event.data != 'object' || !event.data.command) | 65 if (typeof event.data != 'object' || !event.data.command) |
| 33 assert_unreached('Invalid message from the service worker'); | 66 assert_unreached('Invalid message from the service worker'); |
| 34 | 67 |
| 35 assert_equals(event.data.command, command); | 68 assert_equals(event.data.command, command); |
| 36 if (event.data.success) | 69 if (event.data.success) |
| 37 resolve(event.data); | 70 resolve(event.data); |
| 38 else | 71 else |
| 39 reject(new Error(event.data.errorMessage)); | 72 reject(new Error(event.data.errorMessage)); |
| 40 }); | |
| 41 port.postMessage({command: command}); | |
| 42 }); | 73 }); |
| 74 port.postMessage({command: command}); |
| 75 }); |
| 43 } | 76 } |
| OLD | NEW |