| 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. | |
| 13 | 12 |
| 14 'use strict'; | 13 'use strict'; |
| 15 | 14 |
| 16 var resolveCallback = null; | 15 var resolveCallback = null; |
| 17 var rejectCallback = null; | 16 var rejectCallback = null; |
| 18 | 17 |
| 19 this.onmessage = function(event) { | 18 this.onmessage = function(event) { |
| 20 if (event.data['action'] === 'completeDelayedOneShot') { | 19 if (event.data['action'] === 'completeDelayedOneShot') { |
| 21 if (resolveCallback === null) { | 20 if (resolveCallback === null) { |
| 22 sendMessageToClients('sync', 'error - resolveCallback is null'); | 21 sendMessageToClients('sync', 'error - resolveCallback is null'); |
| 23 return; | 22 return; |
| 24 } | 23 } |
| 25 | 24 |
| 26 resolveCallback(); | 25 resolveCallback(); |
| 27 sendMessageToClients('sync', 'ok - delay completed'); | 26 sendMessageToClients('sync', 'ok - delay completed'); |
| 28 return; | 27 return; |
| 29 } | 28 } |
| 30 | 29 |
| 31 if (event.data['action'] === 'rejectDelayedOneShot') { | 30 if (event.data['action'] === 'rejectDelayedOneShot') { |
| 32 if (rejectCallback === null) { | 31 if (rejectCallback === null) { |
| 33 sendMessageToClients('sync', 'error - rejectCallback is null'); | 32 sendMessageToClients('sync', 'error - rejectCallback is null'); |
| 34 return; | 33 return; |
| 35 } | 34 } |
| 36 | 35 |
| 37 rejectCallback(); | 36 rejectCallback(); |
| 38 sendMessageToClients('sync', 'ok - delay rejected'); | 37 sendMessageToClients('sync', 'ok - delay rejected'); |
| 39 } | 38 } |
| 40 | 39 |
| 41 if (event.data['action'] === 'notifyWhenFinished') { | |
| 42 var tag = event.data['tag']; | |
| 43 registration.sync.getRegistration(tag) | |
| 44 .then(function (syncRegistration) { | |
| 45 sendMessageToClients('sync', 'ok - ' + tag + ' finished'); | |
| 46 return syncRegistration.finished; | |
| 47 }) | |
| 48 .then(function() { | |
| 49 sendMessageToClients('sync', tag + " finished result: true"); | |
| 50 }, function(error) { | |
| 51 sendMessageToClients('sync', tag + " finished result: false"); | |
| 52 }) | |
| 53 .catch(sendSyncErrorToClients); | |
| 54 } | |
| 55 | |
| 56 if (event.data['action'] === 'registerOneShot') { | 40 if (event.data['action'] === 'registerOneShot') { |
| 57 var tag = event.data['tag']; | 41 var tag = event.data['tag']; |
| 58 registration.sync.register({'tag': tag}) | 42 registration.sync.register(tag) |
| 59 .then(function (syncRegistration) { | 43 .then(function () { |
| 60 sendMessageToClients('register', 'ok - ' + tag + ' registered in SW'); | 44 sendMessageToClients('register', 'ok - ' + tag + ' registered in SW'); |
| 61 }) | 45 }) |
| 62 .catch(sendSyncErrorToClients); | 46 .catch(sendSyncErrorToClients); |
| 63 } | 47 } |
| 64 | 48 |
| 65 if (event.data['action'] === 'getRegistrationOneShot') { | 49 if (event.data['action'] === 'getRegistrationOneShot') { |
| 66 var tag = event.data['tag']; | 50 var tag = event.data['tag']; |
| 67 registration.sync.getRegistration(tag) | 51 registration.sync.getTags(tag) |
| 68 .then(function(syncRegistration) { | 52 .then(function(tags) { |
| 69 if (!syncRegistration) { | 53 if (tags.indexOf(tag) >= 0) { |
| 54 sendMessageToClients('register', 'ok - ' + tag + ' found'); |
| 55 } else { |
| 70 sendMessageToClients('register', 'error - ' + tag + ' not found'); | 56 sendMessageToClients('register', 'error - ' + tag + ' not found'); |
| 71 return; | 57 return; |
| 72 } | 58 } |
| 73 sendMessageToClients('register', 'ok - ' + tag + ' found'); | |
| 74 }) | 59 }) |
| 75 .catch(sendSyncErrorToClients); | 60 .catch(sendSyncErrorToClients); |
| 76 } | 61 } |
| 77 | 62 |
| 78 if (event.data['action'] === 'getRegistrationsOneShot') { | 63 if (event.data['action'] === 'getRegistrationsOneShot') { |
| 79 registration.sync.getRegistrations() | 64 registration.sync.getTags() |
| 80 .then(function(syncRegistrations) { | 65 .then(function(tags) { |
| 81 var tags = syncRegistrations.map(function(syncRegistration) { | |
| 82 return syncRegistration.tag; | |
| 83 }); | |
| 84 sendMessageToClients('register', 'ok - ' + tags.toString()); | 66 sendMessageToClients('register', 'ok - ' + tags.toString()); |
| 85 }) | 67 }) |
| 86 .catch(sendSyncErrorToClients); | 68 .catch(sendSyncErrorToClients); |
| 87 } | 69 } |
| 88 } | 70 } |
| 89 | 71 |
| 90 this.onsync = function(event) { | 72 this.onsync = function(event) { |
| 91 var eventProperties = [ | 73 var eventProperties = [ |
| 92 // Extract name from toString result: "[object <Class>]" | 74 // Extract name from toString result: "[object <Class>]" |
| 93 Object.prototype.toString.call(event).match(/\s([a-zA-Z]+)/)[1], | 75 Object.prototype.toString.call(event).match(/\s([a-zA-Z]+)/)[1], |
| 94 (typeof event.waitUntil) | 76 (typeof event.waitUntil) |
| 95 ]; | 77 ]; |
| 96 | 78 |
| 97 if (eventProperties[0] != 'SyncEvent') { | 79 if (eventProperties[0] != 'SyncEvent') { |
| 98 sendMessageToClients('sync', 'error - wrong event type'); | 80 sendMessageToClients('sync', 'error - wrong event type'); |
| 99 return; | 81 return; |
| 100 } | 82 } |
| 101 | 83 |
| 102 if (eventProperties[1] != 'function') { | 84 if (eventProperties[1] != 'function') { |
| 103 sendMessageToClients('sync', 'error - wrong wait until type'); | 85 sendMessageToClients('sync', 'error - wrong wait until type'); |
| 104 } | 86 } |
| 105 | 87 |
| 106 if (event.registration === undefined) { | 88 if (event.tag === undefined) { |
| 107 sendMessageToClients('sync', 'error - event missing registration'); | |
| 108 return; | |
| 109 } | |
| 110 | |
| 111 if (event.registration.tag === undefined) { | |
| 112 sendMessageToClients('sync', 'error - registration missing tag'); | 89 sendMessageToClients('sync', 'error - registration missing tag'); |
| 113 return; | 90 return; |
| 114 } | 91 } |
| 115 | 92 |
| 116 var tag = event.registration.tag; | 93 var tag = event.tag; |
| 117 | 94 |
| 118 if (tag === 'delay') { | 95 if (tag === 'delay') { |
| 119 var syncPromise = new Promise(function(resolve, reject) { | 96 var syncPromise = new Promise(function(resolve, reject) { |
| 120 resolveCallback = resolve; | 97 resolveCallback = resolve; |
| 121 rejectCallback = reject; | 98 rejectCallback = reject; |
| 122 }); | 99 }); |
| 123 event.waitUntil(syncPromise); | 100 event.waitUntil(syncPromise); |
| 124 return; | 101 return; |
| 125 } | 102 } |
| 126 | 103 |
| 127 if (tag === 'unregister') { | |
| 128 event.waitUntil(event.registration.unregister() | |
| 129 .then(function() { | |
| 130 sendMessageToClients('sync', 'ok - unregister completed'); | |
| 131 })); | |
| 132 return; | |
| 133 } | |
| 134 | |
| 135 sendMessageToClients('sync', tag + ' fired'); | 104 sendMessageToClients('sync', tag + ' fired'); |
| 136 }; | 105 }; |
| 137 | 106 |
| 138 function sendMessageToClients(type, data) { | 107 function sendMessageToClients(type, data) { |
| 139 clients.matchAll().then(function(clients) { | 108 clients.matchAll().then(function(clients) { |
| 140 clients.forEach(function(client) { | 109 clients.forEach(function(client) { |
| 141 client.postMessage({type, data}); | 110 client.postMessage({type, data}); |
| 142 }); | 111 }); |
| 143 }, function(error) { | 112 }, function(error) { |
| 144 console.log(error); | 113 console.log(error); |
| 145 }); | 114 }); |
| 146 } | 115 } |
| 147 | 116 |
| 148 function sendSyncErrorToClients(error) { | 117 function sendSyncErrorToClients(error) { |
| 149 sendMessageToClients('sync', error.name + ' - ' + error.message); | 118 sendMessageToClients('sync', error.name + ' - ' + error.message); |
| 150 } | 119 } |
| OLD | NEW |