Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(28)

Side by Side Diff: content/test/data/background_sync/service_worker.js

Issue 1344843003: [BackgroundSync] Add browser side support for SyncRegistration.done (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ncn_max
Patch Set: Extract some of the changes to new CLs Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // "unregister" - Unregisters the sync registration from within the sync event.
13 13
14 'use strict'; 14 'use strict';
15 15
16 var resolveCallback = null; 16 var resolveCallback = null;
17 var rejectCallback = null; 17 var rejectCallback = null;
18 18
19 this.onmessage = function(event) { 19 this.onmessage = function(event) {
20 if (event.data === 'completeDelayedOneShot') { 20 if (event.data['action'] === 'completeDelayedOneShot') {
21 if (resolveCallback === null) { 21 if (resolveCallback === null) {
22 sendMessageToClients('sync', 'error - resolveCallback is null'); 22 sendMessageToClients('sync', 'error - resolveCallback is null');
23 return; 23 return;
24 } 24 }
25 25
26 resolveCallback(); 26 resolveCallback();
27 sendMessageToClients('sync', 'ok - delay completed'); 27 sendMessageToClients('sync', 'ok - delay completed');
28 return; 28 return;
29 } 29 }
30 30
31 if (event.data === 'rejectDelayedOneShot') { 31 if (event.data['action'] === 'rejectDelayedOneShot') {
32 if (rejectCallback === null) { 32 if (rejectCallback === null) {
33 sendMessageToClients('sync', 'error - rejectCallback is null'); 33 sendMessageToClients('sync', 'error - rejectCallback is null');
34 return; 34 return;
35 } 35 }
36 36
37 rejectCallback(); 37 rejectCallback();
38 sendMessageToClients('sync', 'ok - delay rejected'); 38 sendMessageToClients('sync', 'ok - delay rejected');
39 } 39 }
40
41 if (event.data['action'] === 'notifyWhenDone') {
42 var tag = event.data['tag'];
43 registration.sync.getRegistration(tag)
44 .then(function (syncRegistration) {
45 sendMessageToClients('sync', 'ok - ' + tag + ' done');
46 return syncRegistration.done;
47 })
48 .then(function(success) {
49 sendMessageToClients('sync', tag + " done result: " + success);
50 }, function(error) {
51 sendMessageToClients('sync', tag + " done result: error");
52 })
53 .catch(sendSyncErrorToClients);
54 }
40 } 55 }
41 56
42 this.onsync = function(event) { 57 this.onsync = function(event) {
43 var eventProperties = [ 58 var eventProperties = [
44 // Extract name from toString result: "[object <Class>]" 59 // Extract name from toString result: "[object <Class>]"
45 Object.prototype.toString.call(event).match(/\s([a-zA-Z]+)/)[1], 60 Object.prototype.toString.call(event).match(/\s([a-zA-Z]+)/)[1],
46 (typeof event.waitUntil) 61 (typeof event.waitUntil)
47 ]; 62 ];
48 63
49 if (eventProperties[0] != 'SyncEvent') { 64 if (eventProperties[0] != 'SyncEvent') {
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 104
90 function sendMessageToClients(type, data) { 105 function sendMessageToClients(type, data) {
91 clients.matchAll().then(function(clients) { 106 clients.matchAll().then(function(clients) {
92 clients.forEach(function(client) { 107 clients.forEach(function(client) {
93 client.postMessage({type, data}); 108 client.postMessage({type, data});
94 }); 109 });
95 }, function(error) { 110 }, function(error) {
96 console.log(error); 111 console.log(error);
97 }); 112 });
98 } 113 }
114
115 function sendSyncErrorToClients(error) {
iclelland 2015/09/17 13:48:10 I'm sure there's an opportunity to refactor the re
jkarlin 2015/09/17 17:40:25 Acknowledged.
116 sendMessageToClients('sync', error.name + ' - ' + error.message);
117 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698