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

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: Fix asan errors 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
« no previous file with comments | « chrome/test/data/push_messaging/push_test.js ('k') | chrome/test/data/push_messaging/subscope1/test.html » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..f32068849a8fb2b3995e84fd895d82133efc97a0 100644
--- a/chrome/test/data/push_messaging/service_worker.js
+++ b/chrome/test/data/push_messaging/service_worker.js
@@ -2,6 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+// Use an absolute path since this could be loaded from a different scope,
+// which would affect the scope of the importScripts call here.
+self.importScripts('/push_messaging/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 +46,28 @@ this.onpush = function(event) {
}));
};
+self.addEventListener('message', function handler (event) {
+ if (event.data.command == 'workerSubscribe') {
+ pushSubscriptionOptions.applicationServerKey = kApplicationServerKey.buffer;
+ } 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,
« no previous file with comments | « chrome/test/data/push_messaging/push_test.js ('k') | chrome/test/data/push_messaging/subscope1/test.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698