| OLD | NEW |
| 1 // Allows a document to exercise the Push API within a service worker by sending
commands. | 1 // Allows a document to exercise the Push API within a service worker by sending
commands. |
| 2 | 2 |
| 3 // The port through which the document sends commands to the service worker. | 3 // The port through which the document sends commands to the service worker. |
| 4 var port = null; | 4 var port = null; |
| 5 | 5 |
| 6 // The most recently seen subscription. | 6 // The most recently seen subscription. |
| 7 var lastSeenSubscription = null; | 7 var lastSeenSubscription = null; |
| 8 | 8 |
| 9 self.addEventListener('message', function(workerEvent) { | 9 self.addEventListener('message', function(workerEvent) { |
| 10 port = workerEvent.data; | 10 port = workerEvent.data; |
| 11 | 11 |
| 12 // Listen to incoming commands on the message port. | 12 // Listen to incoming commands on the message port. |
| 13 port.onmessage = function(event) { | 13 port.onmessage = function(event) { |
| 14 if (typeof event.data != 'object' || !event.data.command) | 14 if (typeof event.data != 'object' || !event.data.command) |
| 15 return; | 15 return; |
| 16 | 16 var options = event.data.options || {} |
| 17 switch (event.data.command) { | 17 switch (event.data.command) { |
| 18 case 'permissionState': | 18 case 'permissionState': |
| 19 self.registration.pushManager.permissionState().then(function(pe
rmissionStatus) { | 19 self.registration.pushManager.permissionState(options).then(func
tion(permissionStatus) { |
| 20 port.postMessage({ command: event.data.command, | 20 port.postMessage({ command: event.data.command, |
| 21 success: true, | 21 success: true, |
| 22 permission: permissionStatus }); | 22 permission: permissionStatus }); |
| 23 }).catch(makeErrorHandler(event.data.command)); | 23 }).catch(makeErrorHandler(event.data.command)); |
| 24 break; | 24 break; |
| 25 | 25 |
| 26 case 'subscribe': | 26 case 'subscribe': |
| 27 self.registration.pushManager.subscribe().then(function(subscrip
tion) { | 27 self.registration.pushManager.subscribe(options).then(function(s
ubscription) { |
| 28 lastSeenSubscription = subscription; | 28 lastSeenSubscription = subscription; |
| 29 port.postMessage({ command: event.data.command, | 29 port.postMessage({ command: event.data.command, |
| 30 success: true, | 30 success: true, |
| 31 subscriptionId: subscription.subscription
Id, | 31 subscriptionId: subscription.subscription
Id, |
| 32 endpoint: subscription.endpoint }); | 32 endpoint: subscription.endpoint }); |
| 33 }).catch(makeErrorHandler(event.data.command)); | 33 }).catch(makeErrorHandler(event.data.command)); |
| 34 break; | 34 break; |
| 35 | 35 |
| 36 case 'getSubscription': | 36 case 'getSubscription': |
| 37 self.registration.pushManager.getSubscription().then(function(su
bscription) { | 37 self.registration.pushManager.getSubscription().then(function(su
bscription) { |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 }); | 73 }); |
| 74 | 74 |
| 75 function makeErrorHandler(command) { | 75 function makeErrorHandler(command) { |
| 76 return function(error) { | 76 return function(error) { |
| 77 var errorMessage = error ? error.message : 'unknown error'; | 77 var errorMessage = error ? error.message : 'unknown error'; |
| 78 port.postMessage({ command: command, | 78 port.postMessage({ command: command, |
| 79 success: false, | 79 success: false, |
| 80 errorMessage: errorMessage }); | 80 errorMessage: errorMessage }); |
| 81 }; | 81 }; |
| 82 } | 82 } |
| OLD | NEW |