OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 var SYNC_TAG = 'send-chats'; | |
Devlin
2015/11/17 19:21:17
I would either use this variable at both line 16 a
lazyboy
2015/11/17 21:41:28
Done.
| |
6 | |
7 var startServiceWorker = new Promise(function(resolve, reject) { | |
jkarlin
2015/11/17 20:36:15
Perhaps rename to registerSyncOnServiceWorker sinc
lazyboy
2015/11/17 21:41:28
Done.
| |
8 var serviceWorker; | |
9 navigator.serviceWorker.register('sw.js').then(function() { | |
10 // Wait until the service worker is active. | |
11 return navigator.serviceWorker.ready; | |
12 }).then(function(registration) { | |
13 serviceWorker = registration.active; | |
14 return registration; | |
15 }).then(function(serviceWorkerRegistration) { | |
jkarlin
2015/11/17 20:36:15
You don't need the above two lines. You can:
lazyboy
2015/11/17 21:41:28
Done.
| |
16 return serviceWorkerRegistration.sync.register(SYNC_TAG); | |
17 }).then(function() { | |
18 resolve(serviceWorker); | |
19 }).catch(function(err) { | |
20 reject(err); | |
21 }); | |
22 }); | |
23 | |
24 window.runServiceWorker = function() { | |
25 startServiceWorker.then(function(serviceWorker) { | |
26 var mc = new MessageChannel(); | |
27 // Called when ServiceWorker.onsync fires. | |
28 mc.port1.onmessage = function(e) { | |
29 if (e.data != 'SYNC: send-chats') { | |
30 console.log('SW returned incorrect data: ' + e.data); | |
31 chrome.test.sendMessage('FAIL'); // Fails the test fast. | |
32 return; | |
33 } | |
34 chrome.test.sendMessage(e.data); | |
35 }; | |
36 serviceWorker.postMessage('connect', [mc.port2]); | |
37 window.domAutomationController.send('SERVICE_WORKER_READY'); | |
38 }).catch(function(err) { | |
39 window.domAutomationController.send(err); | |
40 }); | |
41 }; | |
OLD | NEW |