Chromium Code Reviews| Index: LayoutTests/http/tests/serviceworker/resources/install-phase-event-waituntil.js |
| diff --git a/LayoutTests/http/tests/serviceworker/resources/install-phase-event-waituntil.js b/LayoutTests/http/tests/serviceworker/resources/install-phase-event-waituntil.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a50b375188b3538504683df48a7fe19a96d8865f |
| --- /dev/null |
| +++ b/LayoutTests/http/tests/serviceworker/resources/install-phase-event-waituntil.js |
| @@ -0,0 +1,69 @@ |
| +pendingPorts = []; |
| +portResolves = []; |
| + |
| +onmessage = function(e) { |
| + var message = e.data; |
| + if ('port' in message) { |
| + var resolve = portResolves.shift(); |
| + if (resolve) |
| + resolve(message.port); |
| + else |
| + pendingPorts.push(message.port); |
| + } |
| +}; |
| + |
| +function fulfillPromise() { |
| + return new Promise(function(resolve) { |
| + // Make sure oninstall/onactivate callback finishes first. |
| + setTimeout(function() { |
| + var port = pendingPorts.shift(); |
| + if (port) |
| + resolve(port); |
| + else |
| + portResolves.push(resolve); |
| + }, 0); |
| + }).then(function(port) { |
| + port.postMessage('SYNC'); |
| + return new Promise(function(resolve) { |
| + port.onmessage = function(e) { |
| + if (e.data == 'ACK') |
| + resolve(); |
| + }; |
| + }); |
| + }); |
| +} |
| + |
| +function rejectPromise() { |
| + return new Promise(function(resolve, reject) { |
| + setTimeout(reject, 0); |
| + }); |
| +} |
| + |
| +function stripScopeName(scope) { |
| + return scope.split('/').slice(-1)[0]; |
| +} |
| + |
| +oninstall = function(e) { |
| + var test_switch = stripScopeName(scope); |
|
falken
2014/06/30 04:35:31
could you use "self.scope" to make clear it's a gl
xiang
2014/06/30 06:37:11
OK, I will change that
|
| + if (test_switch == 'install-fulfilled') |
| + e.waitUntil(fulfillPromise()); |
| + else if (test_switch == 'install-rejected') |
| + e.waitUntil(rejectPromise()); |
| +}; |
| + |
| +onactivate = function(e) { |
| + var test_switch = stripScopeName(scope); |
| + if (test_switch == 'activate-fulfilled') { |
| + e.waitUntil(fulfillPromise()); |
| + } else if (test_switch == 'activate-rejected') { |
|
falken
2014/06/30 04:35:31
these look like they'd be more readable as a switc
xiang
2014/06/30 06:37:11
Yes, will change, thanks.
|
| + e.waitUntil(rejectPromise()); |
| + } else if (test_switch == 'activate-multiple-fulfilled') { |
| + e.waitUntil(fulfillPromise()); |
| + e.waitUntil(fulfillPromise()); |
| + } else if (test_switch == 'activate-reject-precedence') { |
| + e.waitUntil(fulfillPromise()); |
| + e.waitUntil(new Promise(function(resolve, reject) { |
| + setTimeout(reject, 0); |
| + })); |
| + } |
| +}; |