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 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
45 sendMessageToClients('sync', 'ok - ' + tag + ' done'); | 45 sendMessageToClients('sync', 'ok - ' + tag + ' done'); |
46 return syncRegistration.done; | 46 return syncRegistration.done; |
47 }) | 47 }) |
48 .then(function(success) { | 48 .then(function(success) { |
49 sendMessageToClients('sync', tag + " done result: " + success); | 49 sendMessageToClients('sync', tag + " done result: " + success); |
50 }, function(error) { | 50 }, function(error) { |
51 sendMessageToClients('sync', tag + " done result: error"); | 51 sendMessageToClients('sync', tag + " done result: error"); |
52 }) | 52 }) |
53 .catch(sendSyncErrorToClients); | 53 .catch(sendSyncErrorToClients); |
54 } | 54 } |
| 55 |
| 56 if (event.data['action'] === 'registerOneShot') { |
| 57 var tag = event.data['tag']; |
| 58 registration.sync.register({'tag': tag}) |
| 59 .then(function (syncRegistration) { |
| 60 sendMessageToClients('register', 'ok - ' + tag + ' registered in SW'); |
| 61 }) |
| 62 .catch(sendSyncErrorToClients); |
| 63 } |
| 64 |
| 65 if (event.data['action'] === 'getRegistrationOneShot') { |
| 66 var tag = event.data['tag']; |
| 67 registration.sync.getRegistration(tag) |
| 68 .then(function(syncRegistration) { |
| 69 if (!syncRegistration) { |
| 70 sendMessageToClients('register', 'error - ' + tag + ' not found'); |
| 71 return; |
| 72 } |
| 73 sendMessageToClients('register', 'ok - ' + tag + ' found'); |
| 74 }) |
| 75 .catch(sendSyncErrorToClients); |
| 76 } |
| 77 |
| 78 if (event.data['action'] === 'getRegistrationsOneShot') { |
| 79 registration.sync.getRegistrations() |
| 80 .then(function(syncRegistrations) { |
| 81 var tags = syncRegistrations.map(function(syncRegistration) { |
| 82 return syncRegistration.tag; |
| 83 }); |
| 84 sendMessageToClients('register', 'ok - ' + tags.toString()); |
| 85 }) |
| 86 .catch(sendSyncErrorToClients); |
| 87 } |
55 } | 88 } |
56 | 89 |
57 this.onsync = function(event) { | 90 this.onsync = function(event) { |
58 var eventProperties = [ | 91 var eventProperties = [ |
59 // Extract name from toString result: "[object <Class>]" | 92 // Extract name from toString result: "[object <Class>]" |
60 Object.prototype.toString.call(event).match(/\s([a-zA-Z]+)/)[1], | 93 Object.prototype.toString.call(event).match(/\s([a-zA-Z]+)/)[1], |
61 (typeof event.waitUntil) | 94 (typeof event.waitUntil) |
62 ]; | 95 ]; |
63 | 96 |
64 if (eventProperties[0] != 'SyncEvent') { | 97 if (eventProperties[0] != 'SyncEvent') { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 client.postMessage({type, data}); | 141 client.postMessage({type, data}); |
109 }); | 142 }); |
110 }, function(error) { | 143 }, function(error) { |
111 console.log(error); | 144 console.log(error); |
112 }); | 145 }); |
113 } | 146 } |
114 | 147 |
115 function sendSyncErrorToClients(error) { | 148 function sendSyncErrorToClients(error) { |
116 sendMessageToClients('sync', error.name + ' - ' + error.message); | 149 sendMessageToClients('sync', error.name + ' - ' + error.message); |
117 } | 150 } |
OLD | NEW |