summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/resources/clients-get-worker.js
blob: 9ac2c22645897ad1f15c19170b635cb4b3db077c (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
self.onfetch = function(e) {
  if (e.request.url.indexOf("clients-get-frame.html") >= 0) {
    if (e.clientId === null) {
      e.respondWith(fetch(e.request));
    } else {
      e.respondWith(Response.error());
    }
    return;
  }
  e.respondWith(new Response(e.clientId));
};

self.onmessage = function(e) {
  var port = e.data.port;
  if (e.data.message == 'get_client_ids') {
    var clientIds = e.data.clientIds;
    var message = [];

    Promise.all(
        clientIds.map(function(clientId) {
          return self.clients.get(clientId);
        }).concat(self.clients.get("invalid-id"))
      ).then(function(clients) {
        clients.forEach(function(client) {
            if (client instanceof Client) {
              message.push([client.visibilityState,
                            client.focused,
                            client.url,
                            client.frameType]);
            } else {
              message.push(client);
            }
          });
        port.postMessage(message);
      });
  } else if (e.data.message == 'get_other_client_id') {
    var clientId = e.data.clientId;
    var message;

    self.clients.get(clientId)
      .then(function(client) {
          if (client instanceof Client) {
            message = [client.visibilityState,
                       client.focused,
                       client.url,
                       client.frameType];
          } else {
            message = client;
          }
          port.postMessage(message);
        });
  }
};