summaryrefslogtreecommitdiffstats
path: root/dom/push/test/worker.js
blob: bcdbf0e0adf7ee1a89e83c90e1de4358351cc891 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// 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.

/* globals PushEvent */

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" });
}