OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 'use strict'; |
| 6 |
| 7 var pushData = new FutureData(); |
| 8 |
| 9 // 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 // the response is sent. |
| 12 function sendResultToTest(result) { |
| 13 console.log('sendResultToTest: ' + result); |
| 14 if (window.domAutomationController) { |
| 15 domAutomationController.send('' + result); |
| 16 } |
| 17 } |
| 18 |
| 19 function sendErrorToTest(error) { |
| 20 sendResultToTest(error.name + ' - ' + error.message); |
| 21 } |
| 22 |
| 23 // A container for a single piece of data. The data does not have to be |
| 24 // available yet when the getter is called, as all responses to the test are |
| 25 // asynchronous. |
| 26 function FutureData() { |
| 27 this.data = null; |
| 28 this.waiting = false; |
| 29 } |
| 30 |
| 31 // Sends the data to the test if it is available. Otherwise sets the |
| 32 // |waiting| flag. |
| 33 FutureData.prototype.get = function() { |
| 34 if (this.data) { |
| 35 sendResultToTest(this.data); |
| 36 } else { |
| 37 this.waiting = true; |
| 38 } |
| 39 }; |
| 40 |
| 41 // Sets a new data value. If the |waiting| flag is on, it is turned off and |
| 42 // the data is sent to the test. |
| 43 FutureData.prototype.set = function(data) { |
| 44 this.data = data; |
| 45 if (this.waiting) { |
| 46 sendResultToTest(data); |
| 47 this.waiting = false; |
| 48 } |
| 49 }; |
| 50 |
| 51 FutureData.prototype.getImmediately = function() { |
| 52 sendResultToTest(this.data); |
| 53 }; |
| 54 |
| 55 // Notification permission has been coalesced with Push permission. After |
| 56 // this is granted, Push API registration can succeed. |
| 57 function requestNotificationPermission() { |
| 58 Notification.requestPermission(function(permission) { |
| 59 sendResultToTest('permission status - ' + permission); |
| 60 }); |
| 61 } |
| 62 |
| 63 function registerServiceWorker() { |
| 64 navigator.serviceWorker.register('service_worker.js', {scope: './'}).then( |
| 65 function(swRegistration) { |
| 66 sendResultToTest('ok - service worker registered'); |
| 67 }, sendErrorToTest); |
| 68 } |
| 69 |
| 70 function unregisterServiceWorker() { |
| 71 navigator.serviceWorker.getRegistration().then(function(swRegistration) { |
| 72 swRegistration.unregister().then(function(result) { |
| 73 sendResultToTest('service worker unregistration status: ' + result); |
| 74 }) |
| 75 }).catch(sendErrorToTest); |
| 76 } |
| 77 |
| 78 function removeManifest() { |
| 79 var element = document.querySelector('link[rel="manifest"]'); |
| 80 if (element) { |
| 81 element.parentNode.removeChild(element); |
| 82 sendResultToTest('manifest removed'); |
| 83 } else |
| 84 sendResultToTest('unable to find manifest element'); |
| 85 } |
| 86 |
| 87 function registerPush() { |
| 88 navigator.serviceWorker.ready.then(function(swRegistration) { |
| 89 // TODO(mvanouwerkerk): Cleanup once the final API is exposed. |
| 90 var pushManager = swRegistration.pushManager || navigator.push; |
| 91 return pushManager.register().then(function(pushRegistration) { |
| 92 sendResultToTest(pushRegistration.pushEndpoint + ' - ' + |
| 93 pushRegistration.pushRegistrationId); |
| 94 }); |
| 95 }).catch(sendErrorToTest); |
| 96 } |
| 97 |
| 98 function hasPermission() { |
| 99 navigator.serviceWorker.ready.then(function(swRegistration) { |
| 100 // TODO(mvanouwerkerk): Cleanup once the final API is exposed. |
| 101 var pushManager = swRegistration.pushManager || navigator.push; |
| 102 return pushManager.hasPermission().then(function(permission) { |
| 103 sendResultToTest('permission status - ' + permission); |
| 104 }); |
| 105 }).catch(sendErrorToTest); |
| 106 } |
| 107 |
| 108 function isControlled() { |
| 109 if (navigator.serviceWorker.controller) { |
| 110 sendResultToTest('true - is controlled'); |
| 111 } else { |
| 112 sendResultToTest('false - is not controlled'); |
| 113 } |
| 114 } |
| 115 |
| 116 addEventListener('message', function(event) { |
| 117 var message = JSON.parse(event.data); |
| 118 if (message.type == 'push') { |
| 119 pushData.set(message.data); |
| 120 } |
| 121 }, false); |
OLD | NEW |