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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/test/data/push_messaging/service_worker.js
diff --git a/chrome/test/data/push_messaging/service_worker.js b/chrome/test/data/push_messaging/service_worker.js
index a338fdc12b67916945a433b11fde0aa64f25debb..fb30f61415d36721e620dca37777b8cfd1ef34ff 100644
--- a/chrome/test/data/push_messaging/service_worker.js
+++ b/chrome/test/data/push_messaging/service_worker.js
@@ -2,6 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+self.importScripts('push_constants.js');
+
+var pushSubscriptionOptions = {
+ userVisibleOnly: true
+};
+
// The "onpush" event currently understands two values as message payload
// data coming from the test. Any other input is passed through to the
// document unchanged.
@@ -38,6 +44,31 @@ this.onpush = function(event) {
}));
};
+self.addEventListener('message', function handler (event) {
+ if (event.data.command == 'workerSubscribe') {
+ pushSubscriptionOptions.applicationServerKey = kApplicationServerKey.buffer;
+ } 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.
+ pushSubscriptionOptions.applicationServerKey = kApplicationServerKey.buffer;
+ pushSubscriptionOptions.userVisibleOnly = false;
+ } else if (event.data.command == 'workerSubscribeNoKey') {
+ // Nothing to set up
+ } else {
+ sendMessageToClients('message', 'error - unknown message request');
+ return;
+ }
+
+ self.registration.pushManager.subscribe(pushSubscriptionOptions)
+ .then(function(subscription) {
+ sendMessageToClients('message', subscription.endpoint);
+ }, function(error) {
+ sendErrorToClients(error);
+ });
+});
+
+function sendErrorToClients(error) {
+ sendMessageToClients('error', error.name + ' - ' + error.message);
+}
+
function sendMessageToClients(type, data) {
var message = JSON.stringify({
'type': type,

Powered by Google App Engine
This is Rietveld 408576698