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'; | |
6 | |
7 var startServiceWorker = new Promise(function(resolve, reject) { | |
8 var serviceWorker; | |
9 var serviceWorkerRegistration; | |
10 navigator.serviceWorker.register('sw.js').then(function() { | |
11 // Wait until the service worker is active. | |
12 return navigator.serviceWorker.ready; | |
13 }).then(function(registration) { | |
14 serviceWorker = registration.active; | |
15 serviceWorkerRegistration = registration; | |
16 return registration.sync.permissionState(); | |
17 }).then(function(syncPermissionState) { | |
18 if (syncPermissionState != 'granted') { | |
lazyboy
2015/11/14 00:08:23
I'm not sure why I'm getting "granted" here. ?
Devlin
2015/11/16 18:12:41
I'm guessing: https://code.google.com/p/chromium/c
| |
19 reject('permissionState: ' + syncPermissionState); | |
20 } | |
21 return serviceWorkerRegistration.sync.register({tag: SYNC_TAG}); | |
22 }).then(function() { | |
23 resolve(serviceWorker); | |
24 }).catch(function(err) { | |
25 reject(err); | |
26 }); | |
27 }); | |
28 | |
29 window.runServiceWorker = function() { | |
30 startServiceWorker.then(function(serviceWorker) { | |
31 var mc = new MessageChannel(); | |
32 // Called when ServiceWorker.onsync fires. | |
33 mc.port1.onmessage = function(e) { | |
34 if (e.data != 'SYNC: send-chats') { | |
35 console.log('message: ' + e.data); | |
36 chrome.test.sendMessage('FAIL'); // Fails the test fast. | |
37 return; | |
38 } | |
39 chrome.test.sendMessage('' + e.data); | |
40 }; | |
41 serviceWorker.postMessage('connect', [mc.port2]); | |
42 window.domAutomationController.send('SERVICE_WORKER_READY'); | |
43 }).catch(function(err) { | |
44 window.domAutomationController.send(err); | |
45 }); | |
46 }; | |
OLD | NEW |