OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 | 8 |
9 // Sends data back to the test. This must be in response to an earlier | 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 | 10 // request, but it's ok to respond asynchronously. The request blocks until |
(...skipping 13 matching lines...) Expand all Loading... |
24 navigator.serviceWorker.register('service_worker.js', {scope: './'}) | 24 navigator.serviceWorker.register('service_worker.js', {scope: './'}) |
25 .then(function() { | 25 .then(function() { |
26 return navigator.serviceWorker.ready; | 26 return navigator.serviceWorker.ready; |
27 }) | 27 }) |
28 .then(function(swRegistration) { | 28 .then(function(swRegistration) { |
29 sendResultToTest('ok - service worker registered'); | 29 sendResultToTest('ok - service worker registered'); |
30 }) | 30 }) |
31 .catch(sendErrorToTest); | 31 .catch(sendErrorToTest); |
32 } | 32 } |
33 | 33 |
34 function registerOneShot(tag) { | 34 function register(tag) { |
35 navigator.serviceWorker.ready | 35 navigator.serviceWorker.ready |
36 .then(function(swRegistration) { | 36 .then(function(swRegistration) { |
37 return swRegistration.sync.register(tag); | 37 return swRegistration.sync.register(tag); |
38 }) | 38 }) |
39 .then(function() { | 39 .then(function() { |
40 sendResultToTest('ok - ' + tag + ' registered'); | 40 sendResultToTest('ok - ' + tag + ' registered'); |
41 }) | 41 }) |
42 .catch(sendErrorToTest); | 42 .catch(sendErrorToTest); |
43 } | 43 } |
44 | 44 |
45 function registerOneShotFromServiceWorker(tag) { | 45 function registerFromServiceWorker(tag) { |
46 navigator.serviceWorker.ready | 46 navigator.serviceWorker.ready |
47 .then(function(swRegistration) { | 47 .then(function(swRegistration) { |
48 swRegistration.active.postMessage({action: 'registerOneShot', tag: tag}); | 48 swRegistration.active.postMessage({action: 'register', tag: tag}); |
49 sendResultToTest('ok - ' + tag + ' register sent to SW'); | 49 sendResultToTest('ok - ' + tag + ' register sent to SW'); |
50 }) | 50 }) |
51 .catch(sendErrorToTest); | 51 .catch(sendErrorToTest); |
52 } | 52 } |
53 | 53 |
54 function getRegistrationOneShot(tag) { | 54 function hasTag(tag) { |
55 navigator.serviceWorker.ready | 55 navigator.serviceWorker.ready |
56 .then(function(swRegistration) { | 56 .then(function(swRegistration) { |
57 return swRegistration.sync.getTags(); | 57 return swRegistration.sync.getTags(); |
58 }) | 58 }) |
59 .then(function(tags) { | 59 .then(function(tags) { |
60 if (tags.indexOf(tag) >= 0) { | 60 if (tags.indexOf(tag) >= 0) { |
61 sendResultToTest('ok - ' + tag + ' found'); | 61 sendResultToTest('ok - ' + tag + ' found'); |
62 } else { | 62 } else { |
63 sendResultToTest('error - ' + tag + ' not found'); | 63 sendResultToTest('error - ' + tag + ' not found'); |
64 return; | 64 return; |
65 } | 65 } |
66 }) | 66 }) |
67 .catch(sendErrorToTest); | 67 .catch(sendErrorToTest); |
68 } | 68 } |
69 | 69 |
70 function getRegistrationOneShotFromServiceWorker(tag) { | 70 function hasTagFromServiceWorker(tag) { |
71 navigator.serviceWorker.ready | 71 navigator.serviceWorker.ready |
72 .then(function(swRegistration) { | 72 .then(function(swRegistration) { |
73 swRegistration.active.postMessage( | 73 swRegistration.active.postMessage( |
74 {action: 'getRegistrationOneShot', tag: tag}); | 74 {action: 'hasTag', tag: tag}); |
75 sendResultToTest('ok - getRegistration sent to SW'); | 75 sendResultToTest('ok - hasTag sent to SW'); |
76 }) | 76 }) |
77 .catch(sendErrorToTest); | 77 .catch(sendErrorToTest); |
78 } | 78 } |
79 | 79 |
80 function getRegistrationsOneShot(tag) { | 80 function getTags() { |
81 navigator.serviceWorker.ready | 81 navigator.serviceWorker.ready |
82 .then(function(swRegistration) { | 82 .then(function(swRegistration) { |
83 return swRegistration.sync.getTags(); | 83 return swRegistration.sync.getTags(); |
84 }) | 84 }) |
85 .then(function(tags) { | 85 .then(function(tags) { |
86 sendResultToTest('ok - ' + tags.toString()); | 86 sendResultToTest('ok - ' + tags.toString()); |
87 }) | 87 }) |
88 .catch(sendErrorToTest); | 88 .catch(sendErrorToTest); |
89 } | 89 } |
90 | 90 |
91 function getRegistrationsOneShotFromServiceWorker() { | 91 function getTagsFromServiceWorker() { |
92 navigator.serviceWorker.ready | 92 navigator.serviceWorker.ready |
93 .then(function(swRegistration) { | 93 .then(function(swRegistration) { |
94 swRegistration.active.postMessage({action: 'getRegistrationsOneShot'}); | 94 swRegistration.active.postMessage({action: 'getTags'}); |
95 sendResultToTest('ok - getRegistrations sent to SW'); | 95 sendResultToTest('ok - getTags sent to SW'); |
96 }) | 96 }) |
97 .catch(sendErrorToTest); | 97 .catch(sendErrorToTest); |
98 } | 98 } |
99 | 99 |
100 function completeDelayedOneShot() { | 100 function completeDelayedSyncEvent() { |
101 navigator.serviceWorker.ready | 101 navigator.serviceWorker.ready |
102 .then(function(swRegistration) { | 102 .then(function(swRegistration) { |
103 swRegistration.active.postMessage({action: 'completeDelayedOneShot'}); | 103 swRegistration.active.postMessage({ |
| 104 action: 'completeDelayedSyncEvent' |
| 105 }); |
104 sendResultToTest('ok - delay completing'); | 106 sendResultToTest('ok - delay completing'); |
105 }) | 107 }) |
106 .catch(sendErrorToTest); | 108 .catch(sendErrorToTest); |
107 } | 109 } |
108 | 110 |
109 function rejectDelayedOneShot() { | 111 function rejectDelayedSyncEvent() { |
110 navigator.serviceWorker.ready | 112 navigator.serviceWorker.ready |
111 .then(function(swRegistration) { | 113 .then(function(swRegistration) { |
112 swRegistration.active.postMessage({action: 'rejectDelayedOneShot'}); | 114 swRegistration.active.postMessage({action: 'rejectDelayedSyncEvent'}); |
113 sendResultToTest('ok - delay rejecting'); | 115 sendResultToTest('ok - delay rejecting'); |
114 }) | 116 }) |
115 .catch(sendErrorToTest); | 117 .catch(sendErrorToTest); |
116 } | 118 } |
117 | 119 |
118 function createFrame(url) { | 120 function createFrame(url) { |
119 return new Promise(function(resolve) { | 121 return new Promise(function(resolve) { |
120 var frame = document.createElement('iframe'); | 122 var frame = document.createElement('iframe'); |
121 frame.src = url; | 123 frame.src = url; |
122 frame.onload = function() { resolve(frame); }; | 124 frame.onload = function() { resolve(frame); }; |
123 document.body.appendChild(frame); | 125 document.body.appendChild(frame); |
124 }); | 126 }); |
125 } | 127 } |
126 | 128 |
127 function registerOneShotFromLocalFrame(frame_url) { | 129 function registerFromLocalFrame(frame_url) { |
128 var frameWindow; | 130 var frameWindow; |
129 return createFrame(frame_url) | 131 return createFrame(frame_url) |
130 .then(function(frame) { | 132 .then(function(frame) { |
131 frameWindow = frame.contentWindow; | 133 frameWindow = frame.contentWindow; |
132 return frameWindow.navigator.serviceWorker.register('service_worker.js'); | 134 return frameWindow.navigator.serviceWorker.register('service_worker.js'); |
133 }) | 135 }) |
134 .then(function() { | 136 .then(function() { |
135 return frameWindow.navigator.serviceWorker.ready; | 137 return frameWindow.navigator.serviceWorker.ready; |
136 }) | 138 }) |
137 .then(function(frame_registration) { | 139 .then(function(frame_registration) { |
(...skipping 12 matching lines...) Expand all Loading... |
150 } | 152 } |
151 | 153 |
152 function receiveMessage() { | 154 function receiveMessage() { |
153 return new Promise(function(resolve) { | 155 return new Promise(function(resolve) { |
154 window.addEventListener('message', function(message) { | 156 window.addEventListener('message', function(message) { |
155 resolve(message.data); | 157 resolve(message.data); |
156 }); | 158 }); |
157 }); | 159 }); |
158 } | 160 } |
159 | 161 |
160 function registerOneShotFromCrossOriginServiceWorker(cross_frame_url) { | 162 function registerFromCrossOriginServiceWorker(cross_frame_url) { |
161 return createFrame(cross_frame_url) | 163 return createFrame(cross_frame_url) |
162 .then(function(frame) { | 164 .then(function(frame) { |
163 return receiveMessage(); | 165 return receiveMessage(); |
164 }) | 166 }) |
165 .then(function(message) { | 167 .then(function(message) { |
166 console.log(message); | 168 console.log(message); |
167 if (message !== 'registration failed') { | 169 if (message !== 'registration failed') { |
168 sendResultToTest('failed - ' + message); | 170 sendResultToTest('failed - ' + message); |
169 return; | 171 return; |
170 } | 172 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 // available, otherwise sends null. | 207 // available, otherwise sends null. |
206 ResultQueue.prototype.popImmediately = function() { | 208 ResultQueue.prototype.popImmediately = function() { |
207 sendResultToTest(this.queue.length ? this.queue.pop() : null); | 209 sendResultToTest(this.queue.length ? this.queue.pop() : null); |
208 }; | 210 }; |
209 | 211 |
210 navigator.serviceWorker.addEventListener('message', function(event) { | 212 navigator.serviceWorker.addEventListener('message', function(event) { |
211 var message = event.data; | 213 var message = event.data; |
212 if (message.type == 'sync' || message.type === 'register') | 214 if (message.type == 'sync' || message.type === 'register') |
213 resultQueue.push(message.data); | 215 resultQueue.push(message.data); |
214 }, false); | 216 }, false); |
OLD | NEW |