summaryrefslogtreecommitdiffstats
path: root/dom/push/test/worker.js
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /dom/push/test/worker.js
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'dom/push/test/worker.js')
-rw-r--r--dom/push/test/worker.js152
1 files changed, 152 insertions, 0 deletions
diff --git a/dom/push/test/worker.js b/dom/push/test/worker.js
new file mode 100644
index 000000000..0e26f228d
--- /dev/null
+++ b/dom/push/test/worker.js
@@ -0,0 +1,152 @@
+// Any copyright is dedicated to the Public Domain.
+// http://creativecommons.org/licenses/publicdomain/
+
+// This worker is used for two types of tests. `handlePush` sends messages to
+// `frame.html`, which verifies that the worker can receive push messages.
+
+// `handleMessage` receives messages from `test_push_manager_worker.html`
+// and `test_data.html`, and verifies that `PushManager` can be used from
+// the worker.
+
+this.onpush = handlePush;
+this.onmessage = handleMessage;
+this.onpushsubscriptionchange = handlePushSubscriptionChange;
+
+function getJSON(data) {
+ var result = {
+ ok: false,
+ };
+ try {
+ result.value = data.json();
+ result.ok = true;
+ } catch (e) {
+ // Ignore syntax errors for invalid JSON.
+ }
+ return result;
+}
+
+function assert(value, message) {
+ if (!value) {
+ throw new Error(message);
+ }
+}
+
+function broadcast(event, promise) {
+ event.waitUntil(Promise.resolve(promise).then(message => {
+ return self.clients.matchAll().then(clients => {
+ clients.forEach(client => client.postMessage(message));
+ });
+ }));
+}
+
+function reply(event, promise) {
+ event.waitUntil(Promise.resolve(promise).then(result => {
+ event.ports[0].postMessage(result);
+ }).catch(error => {
+ event.ports[0].postMessage({
+ error: String(error),
+ });
+ }));
+}
+
+function handlePush(event) {
+ if (event instanceof PushEvent) {
+ if (!('data' in event)) {
+ broadcast(event, {type: "finished", okay: "yes"});
+ return;
+ }
+ var message = {
+ type: "finished",
+ okay: "yes",
+ };
+ if (event.data) {
+ message.data = {
+ text: event.data.text(),
+ arrayBuffer: event.data.arrayBuffer(),
+ json: getJSON(event.data),
+ blob: event.data.blob(),
+ };
+ }
+ broadcast(event, message);
+ return;
+ }
+ broadcast(event, {type: "finished", okay: "no"});
+}
+
+var testHandlers = {
+ publicKey(data) {
+ return self.registration.pushManager.getSubscription().then(
+ subscription => ({
+ p256dh: subscription.getKey("p256dh"),
+ auth: subscription.getKey("auth"),
+ })
+ );
+ },
+
+ resubscribe(data) {
+ return self.registration.pushManager.getSubscription().then(
+ subscription => {
+ assert(subscription.endpoint == data.endpoint,
+ "Wrong push endpoint in worker");
+ return subscription.unsubscribe();
+ }
+ ).then(result => {
+ assert(result, "Error unsubscribing in worker");
+ return self.registration.pushManager.getSubscription();
+ }).then(subscription => {
+ assert(!subscription, "Subscription not removed in worker");
+ return self.registration.pushManager.subscribe();
+ }).then(subscription => {
+ return {
+ endpoint: subscription.endpoint,
+ };
+ });
+ },
+
+ denySubscribe(data) {
+ return self.registration.pushManager.getSubscription().then(
+ subscription => {
+ assert(!subscription,
+ "Should not return worker subscription with revoked permission");
+ return self.registration.pushManager.subscribe().then(_ => {
+ assert(false, "Expected error subscribing with revoked permission");
+ }, error => {
+ return {
+ isDOMException: error instanceof DOMException,
+ name: error.name,
+ };
+ });
+ }
+ );
+ },
+
+ subscribeWithKey(data) {
+ return self.registration.pushManager.subscribe({
+ applicationServerKey: data.key,
+ }).then(subscription => {
+ return {
+ endpoint: subscription.endpoint,
+ key: subscription.options.applicationServerKey,
+ };
+ }, error => {
+ return {
+ isDOMException: error instanceof DOMException,
+ name: error.name,
+ };
+ });
+ },
+};
+
+function handleMessage(event) {
+ var handler = testHandlers[event.data.type];
+ if (handler) {
+ reply(event, handler(event.data));
+ } else {
+ reply(event, Promise.reject(
+ "Invalid message type: " + event.data.type));
+ }
+}
+
+function handlePushSubscriptionChange(event) {
+ broadcast(event, {type: "changed", okay: "yes"});
+}