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 "completeDelayedRegistration" to finish the |
11 // event. | 11 // event. |
12 | 12 |
13 'use strict'; | 13 'use strict'; |
14 | 14 |
15 var resolveCallback = null; | 15 var resolveCallback = null; |
16 var rejectCallback = null; | 16 var rejectCallback = null; |
17 | 17 |
18 this.onmessage = function(event) { | 18 this.onmessage = function(event) { |
19 if (event.data['action'] === 'completeDelayedOneShot') { | 19 if (event.data['action'] === 'completeDelayedSyncEvent') { |
20 if (resolveCallback === null) { | 20 if (resolveCallback === null) { |
21 sendMessageToClients('sync', 'error - resolveCallback is null'); | 21 sendMessageToClients('sync', 'error - resolveCallback is null'); |
22 return; | 22 return; |
23 } | 23 } |
24 | 24 |
25 resolveCallback(); | 25 resolveCallback(); |
26 sendMessageToClients('sync', 'ok - delay completed'); | 26 sendMessageToClients('sync', 'ok - delay completed'); |
27 return; | 27 return; |
28 } | 28 } |
29 | 29 |
30 if (event.data['action'] === 'rejectDelayedOneShot') { | 30 if (event.data['action'] === 'rejectDelayedSyncEvent') { |
31 if (rejectCallback === null) { | 31 if (rejectCallback === null) { |
32 sendMessageToClients('sync', 'error - rejectCallback is null'); | 32 sendMessageToClients('sync', 'error - rejectCallback is null'); |
33 return; | 33 return; |
34 } | 34 } |
35 | 35 |
36 rejectCallback(); | 36 rejectCallback(); |
37 sendMessageToClients('sync', 'ok - delay rejected'); | 37 sendMessageToClients('sync', 'ok - delay rejected'); |
38 } | 38 } |
39 | 39 |
40 if (event.data['action'] === 'registerOneShot') { | 40 if (event.data['action'] === 'register') { |
41 var tag = event.data['tag']; | 41 var tag = event.data['tag']; |
42 registration.sync.register(tag) | 42 registration.sync.register(tag) |
43 .then(function () { | 43 .then(function () { |
44 sendMessageToClients('register', 'ok - ' + tag + ' registered in SW'); | 44 sendMessageToClients('register', 'ok - ' + tag + ' registered in SW'); |
45 }) | 45 }) |
46 .catch(sendSyncErrorToClients); | 46 .catch(sendSyncErrorToClients); |
47 } | 47 } |
48 | 48 |
49 if (event.data['action'] === 'getRegistrationOneShot') { | 49 if (event.data['action'] === 'hasTag') { |
50 var tag = event.data['tag']; | 50 var tag = event.data['tag']; |
51 registration.sync.getTags(tag) | 51 registration.sync.getTags() |
52 .then(function(tags) { | 52 .then(function(tags) { |
53 if (tags.indexOf(tag) >= 0) { | 53 if (tags.indexOf(tag) >= 0) { |
54 sendMessageToClients('register', 'ok - ' + tag + ' found'); | 54 sendMessageToClients('register', 'ok - ' + tag + ' found'); |
55 } else { | 55 } else { |
56 sendMessageToClients('register', 'error - ' + tag + ' not found'); | 56 sendMessageToClients('register', 'error - ' + tag + ' not found'); |
57 return; | 57 return; |
58 } | 58 } |
59 }) | 59 }) |
60 .catch(sendSyncErrorToClients); | 60 .catch(sendSyncErrorToClients); |
61 } | 61 } |
62 | 62 |
63 if (event.data['action'] === 'getRegistrationsOneShot') { | 63 if (event.data['action'] === 'getTags') { |
64 registration.sync.getTags() | 64 registration.sync.getTags() |
65 .then(function(tags) { | 65 .then(function(tags) { |
66 sendMessageToClients('register', 'ok - ' + tags.toString()); | 66 sendMessageToClients('register', 'ok - ' + tags.toString()); |
67 }) | 67 }) |
68 .catch(sendSyncErrorToClients); | 68 .catch(sendSyncErrorToClients); |
69 } | 69 } |
70 } | 70 } |
71 | 71 |
72 this.onsync = function(event) { | 72 this.onsync = function(event) { |
73 var eventProperties = [ | 73 var eventProperties = [ |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 client.postMessage({type, data}); | 110 client.postMessage({type, data}); |
111 }); | 111 }); |
112 }, function(error) { | 112 }, function(error) { |
113 console.log(error); | 113 console.log(error); |
114 }); | 114 }); |
115 } | 115 } |
116 | 116 |
117 function sendSyncErrorToClients(error) { | 117 function sendSyncErrorToClients(error) { |
118 sendMessageToClients('sync', error.name + ' - ' + error.message); | 118 sendMessageToClients('sync', error.name + ' - ' + error.message); |
119 } | 119 } |
OLD | NEW |