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

Side by Side Diff: chrome/test/data/push_messaging/service_worker.js

Issue 1816123002: Add testing for subscription from service workers (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Code review comments incorporated and all tests working Created 4 years, 9 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 self.importScripts('push_constants.js');
6
7 var pushSubscriptionOptions = {
8 userVisibleOnly: true
9 };
10
5 // The "onpush" event currently understands two values as message payload 11 // The "onpush" event currently understands two values as message payload
6 // data coming from the test. Any other input is passed through to the 12 // data coming from the test. Any other input is passed through to the
7 // document unchanged. 13 // document unchanged.
8 // 14 //
9 // "shownotification" - Display a Web Notification with event.waitUntil(). 15 // "shownotification" - Display a Web Notification with event.waitUntil().
10 // "shownotification-without-waituntil" 16 // "shownotification-without-waituntil"
11 // - Display a Web Notification without using event.waitUntil(). 17 // - Display a Web Notification without using event.waitUntil().
12 this.onpush = function(event) { 18 this.onpush = function(event) {
13 if (event.data === null) { 19 if (event.data === null) {
14 sendMessageToClients('push', '[NULL]'); 20 sendMessageToClients('push', '[NULL]');
(...skipping 16 matching lines...) Expand all
31 return; 37 return;
32 } 38 }
33 39
34 event.waitUntil(result.then(function() { 40 event.waitUntil(result.then(function() {
35 sendMessageToClients('push', data); 41 sendMessageToClients('push', data);
36 }, function(ex) { 42 }, function(ex) {
37 sendMessageToClients('push', String(ex)); 43 sendMessageToClients('push', String(ex));
38 })); 44 }));
39 }; 45 };
40 46
47 self.addEventListener('message', function handler (event) {
48 if (event.data.command == 'workerSubscribe') {
49 pushSubscriptionOptions.applicationServerKey = kApplicationServerKey.buffer;
50 } else if (event.data.command == 'workerSubscribePermissionFail') {
Michael van Ouwerkerk 2016/03/30 09:50:04 This case appears to be unused?
harkness 2016/03/30 10:51:49 Done.
51 pushSubscriptionOptions.applicationServerKey = kApplicationServerKey.buffer;
52 pushSubscriptionOptions.userVisibleOnly = false;
53 } else if (event.data.command == 'workerSubscribeNoKey') {
54 // Nothing to set up
55 } else {
56 sendMessageToClients('message', 'error - unknown message request');
57 return;
58 }
59
60 self.registration.pushManager.subscribe(pushSubscriptionOptions)
61 .then(function(subscription) {
62 sendMessageToClients('message', subscription.endpoint);
63 }, function(error) {
64 sendErrorToClients(error);
65 });
66 });
67
68 function sendErrorToClients(error) {
69 sendMessageToClients('error', error.name + ' - ' + error.message);
70 }
71
41 function sendMessageToClients(type, data) { 72 function sendMessageToClients(type, data) {
42 var message = JSON.stringify({ 73 var message = JSON.stringify({
43 'type': type, 74 'type': type,
44 'data': data 75 'data': data
45 }); 76 });
46 clients.matchAll().then(function(clients) { 77 clients.matchAll().then(function(clients) {
47 clients.forEach(function(client) { 78 clients.forEach(function(client) {
48 client.postMessage(message); 79 client.postMessage(message);
49 }); 80 });
50 }, function(error) { 81 }, function(error) {
51 console.log(error); 82 console.log(error);
52 }); 83 });
53 } 84 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698