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 // The "onsync" event currently understands commands (passed as | 5 // The "onsync" event currently understands commands (passed as |
6 // registration tags) coming from the test. Any other tag is | 6 // registration tags) coming from the test. Any other tag is |
7 // passed through to the document unchanged. | 7 // passed through to the document unchanged. |
8 // | 8 // |
9 // "delay" - Delays finishing the sync event with event.waitUntil. | 9 // "delay" - Delays finishing the sync event with event.waitUntil. |
10 // Send a postMessage of "completeDelayedOneShot" to finish the | 10 // Send a postMessage of "completeDelayedOneShot" to finish the |
11 // event. | 11 // event. |
| 12 // "unregister" - Unregisters the sync registration from within the sync event. |
12 | 13 |
13 'use strict'; | 14 'use strict'; |
14 | 15 |
15 var resolveCallback = null; | 16 var resolveCallback = null; |
16 var rejectCallback = null; | 17 var rejectCallback = null; |
17 | 18 |
18 this.onmessage = function(event) { | 19 this.onmessage = function(event) { |
19 if (event.data === 'completeDelayedOneShot') { | 20 if (event.data === 'completeDelayedOneShot') { |
20 if (resolveCallback === null) { | 21 if (resolveCallback === null) { |
21 sendMessageToClients('sync', 'error - resolveCallback is null'); | 22 sendMessageToClients('sync', 'error - resolveCallback is null'); |
(...skipping 10 matching lines...) Expand all Loading... |
32 sendMessageToClients('sync', 'error - rejectCallback is null'); | 33 sendMessageToClients('sync', 'error - rejectCallback is null'); |
33 return; | 34 return; |
34 } | 35 } |
35 | 36 |
36 rejectCallback(); | 37 rejectCallback(); |
37 sendMessageToClients('sync', 'ok - delay rejected'); | 38 sendMessageToClients('sync', 'ok - delay rejected'); |
38 } | 39 } |
39 } | 40 } |
40 | 41 |
41 this.onsync = function(event) { | 42 this.onsync = function(event) { |
| 43 var eventProperties = [ |
| 44 // Extract name from toString result: "[object <Class>]" |
| 45 Object.prototype.toString.call(event).match(/\s([a-zA-Z]+)/)[1], |
| 46 (typeof event.waitUntil) |
| 47 ]; |
| 48 |
| 49 if (eventProperties[0] != 'SyncEvent') { |
| 50 sendMessageToClients('sync', 'error - wrong event type'); |
| 51 return; |
| 52 } |
| 53 |
| 54 if (eventProperties[1] != 'function') { |
| 55 sendMessageToClients('sync', 'error - wrong wait until type'); |
| 56 } |
| 57 |
42 if (event.registration === undefined) { | 58 if (event.registration === undefined) { |
43 sendMessageToClients('sync', 'error - event missing registration'); | 59 sendMessageToClients('sync', 'error - event missing registration'); |
44 return; | 60 return; |
45 } | 61 } |
46 | 62 |
47 if (event.registration.tag === undefined) { | 63 if (event.registration.tag === undefined) { |
48 sendMessageToClients('sync', 'error - registration missing tag'); | 64 sendMessageToClients('sync', 'error - registration missing tag'); |
49 return; | 65 return; |
50 } | 66 } |
51 | 67 |
52 var tag = event.registration.tag; | 68 var tag = event.registration.tag; |
53 | 69 |
54 if (tag === 'delay') { | 70 if (tag === 'delay') { |
55 var syncPromise = new Promise(function(resolve, reject) { | 71 var syncPromise = new Promise(function(resolve, reject) { |
56 resolveCallback = resolve; | 72 resolveCallback = resolve; |
57 rejectCallback = reject; | 73 rejectCallback = reject; |
58 }); | 74 }); |
59 event.waitUntil(syncPromise); | 75 event.waitUntil(syncPromise); |
60 return; | 76 return; |
61 } | 77 } |
62 | 78 |
| 79 if (tag === 'unregister') { |
| 80 event.waitUntil(event.registration.unregister() |
| 81 .then(function() { |
| 82 sendMessageToClients('sync', 'ok - unregister completed'); |
| 83 })); |
| 84 return; |
| 85 } |
| 86 |
63 sendMessageToClients('sync', tag + ' fired'); | 87 sendMessageToClients('sync', tag + ' fired'); |
64 }; | 88 }; |
65 | 89 |
66 function sendMessageToClients(type, data) { | 90 function sendMessageToClients(type, data) { |
67 clients.matchAll().then(function(clients) { | 91 clients.matchAll().then(function(clients) { |
68 clients.forEach(function(client) { | 92 clients.forEach(function(client) { |
69 client.postMessage({type, data}); | 93 client.postMessage({type, data}); |
70 }); | 94 }); |
71 }, function(error) { | 95 }, function(error) { |
72 console.log(error); | 96 console.log(error); |
73 }); | 97 }); |
74 } | 98 } |
OLD | NEW |