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 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 | 108 |
109 function rejectDelayedOneShot() { | 109 function rejectDelayedOneShot() { |
110 navigator.serviceWorker.ready | 110 navigator.serviceWorker.ready |
111 .then(function(swRegistration) { | 111 .then(function(swRegistration) { |
112 swRegistration.active.postMessage({action: 'rejectDelayedOneShot'}); | 112 swRegistration.active.postMessage({action: 'rejectDelayedOneShot'}); |
113 sendResultToTest('ok - delay rejecting'); | 113 sendResultToTest('ok - delay rejecting'); |
114 }) | 114 }) |
115 .catch(sendErrorToTest); | 115 .catch(sendErrorToTest); |
116 } | 116 } |
117 | 117 |
| 118 function createFrame(url) { |
| 119 return new Promise(function(resolve) { |
| 120 var frame = document.createElement('iframe'); |
| 121 frame.src = url; |
| 122 frame.onload = function() { resolve(frame); }; |
| 123 document.body.appendChild(frame); |
| 124 }); |
| 125 } |
| 126 |
| 127 function registerOneShotFromLocalFrame(frame_url) { |
| 128 var frameWindow; |
| 129 return createFrame(frame_url) |
| 130 .then(function(frame) { |
| 131 frameWindow = frame.contentWindow; |
| 132 return frameWindow.navigator.serviceWorker.register('service_worker.js'); |
| 133 }) |
| 134 .then(function() { |
| 135 return frameWindow.navigator.serviceWorker.ready; |
| 136 }) |
| 137 .then(function(frame_registration) { |
| 138 return frame_registration.sync.register('foo'); |
| 139 }) |
| 140 .then(function() { |
| 141 sendResultToTest('error - iframe registered sync'); |
| 142 }, function(e) { |
| 143 if (e.name != 'AbortError' || e.message !== 'Registration failed - not ' + |
| 144 'called from a main frame.') { |
| 145 sendErrorToTest(e); |
| 146 return; |
| 147 } |
| 148 sendResultToTest('ok - iframe failed to register sync'); |
| 149 }); |
| 150 } |
| 151 |
| 152 function receiveMessage() { |
| 153 return new Promise(function(resolve) { |
| 154 window.addEventListener('message', function(message) { |
| 155 resolve(message.data); |
| 156 }); |
| 157 }); |
| 158 } |
| 159 |
| 160 function registerOneShotFromCrossOriginServiceWorker(cross_frame_url) { |
| 161 return createFrame(cross_frame_url) |
| 162 .then(function(frame) { |
| 163 return receiveMessage(); |
| 164 }) |
| 165 .then(function(message) { |
| 166 console.log(message); |
| 167 if (message !== 'registration failed') { |
| 168 sendResultToTest('failed - ' + message); |
| 169 return; |
| 170 } |
| 171 sendResultToTest('ok - worker failed to register sync'); |
| 172 }); |
| 173 } |
118 | 174 |
119 // Queue storing asynchronous results received from the Service Worker. Results | 175 // Queue storing asynchronous results received from the Service Worker. Results |
120 // are sent to the test when requested. | 176 // are sent to the test when requested. |
121 function ResultQueue() { | 177 function ResultQueue() { |
122 // Invariant: this.queue.length == 0 || this.pendingGets == 0 | 178 // Invariant: this.queue.length == 0 || this.pendingGets == 0 |
123 this.queue = []; | 179 this.queue = []; |
124 this.pendingGets = 0; | 180 this.pendingGets = 0; |
125 } | 181 } |
126 | 182 |
127 // Adds a data item to the queue. Will be sent to the test if there are | 183 // Adds a data item to the queue. Will be sent to the test if there are |
(...skipping 21 matching lines...) Expand all Loading... |
149 // available, otherwise sends null. | 205 // available, otherwise sends null. |
150 ResultQueue.prototype.popImmediately = function() { | 206 ResultQueue.prototype.popImmediately = function() { |
151 sendResultToTest(this.queue.length ? this.queue.pop() : null); | 207 sendResultToTest(this.queue.length ? this.queue.pop() : null); |
152 }; | 208 }; |
153 | 209 |
154 navigator.serviceWorker.addEventListener('message', function(event) { | 210 navigator.serviceWorker.addEventListener('message', function(event) { |
155 var message = event.data; | 211 var message = event.data; |
156 if (message.type == 'sync' || message.type === 'register') | 212 if (message.type == 'sync' || message.type === 'register') |
157 resultQueue.push(message.data); | 213 resultQueue.push(message.data); |
158 }, false); | 214 }, false); |
OLD | NEW |