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

Unified Diff: LayoutTests/http/tests/notifications/resources/instrumentation-service-worker.js

Issue 868053003: Expose ServiceWorkerRegistration.showNotification() in Service Workers. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 5 years, 11 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: LayoutTests/http/tests/notifications/resources/instrumentation-service-worker.js
diff --git a/LayoutTests/http/tests/notifications/resources/instrumentation-service-worker.js b/LayoutTests/http/tests/notifications/resources/instrumentation-service-worker.js
new file mode 100644
index 0000000000000000000000000000000000000000..a0253c2096403b714cf5cce783718802847317c4
--- /dev/null
+++ b/LayoutTests/http/tests/notifications/resources/instrumentation-service-worker.js
@@ -0,0 +1,45 @@
+// Allows the controlling document to instrument Web Notification behavior
+// within a Service Worker by sending commands.
+var messagePort = null;
+
+addEventListener('message', function(workerEvent) {
+ messagePort = workerEvent.data;
+
+ // Listen to incoming commands on the message port.
+ messagePort.onmessage = function(event) {
+ if (typeof event.data != 'object' || !event.data.command)
+ return;
+
+ switch (event.data.command) {
+ case 'permission':
+ messagePort.postMessage({ command: 'permission', value: Notification.permission });
+ break;
+
+ case 'show':
+ registration.showNotification(event.data.title, event.data.options).then(function() {
+ messagePort.postMessage({ command: 'show', success: true });
+ }, function(error) {
+ messagePort.postMessage({ command: 'show', success: false, message: error.message });
+ });
+ break;
+
+ default:
+ messagePort.postMessage({ command: 'error', message: 'Invalid command: ' + event.data.command });
+ break;
+ }
+ };
+
+ // Notify the controller that the worker is now available.
+ messagePort.postMessage('ready');
+});
+
+addEventListener('notificationclick', function(event) {
+ messagePort.postMessage({ command: 'click', notification: {
+ title: event.notification.title
+ }});
+
+ // Notifications containing "ACTION:CLOSE" in their message will be closed
+ // immediately by the Service Worker.
+ if (event.notification.body.indexOf('ACTION:CLOSE') != -1)
+ event.notification.close();
+});

Powered by Google App Engine
This is Rietveld 408576698