summaryrefslogtreecommitdiffstats
path: root/dom/workers/test/serviceworkers/fetch/context/context_test.js
blob: b98d2ab3ca73e711039a4cc0c95aa5d76957c9ca (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
self.addEventListener("fetch", function(event) {
  if (event.request.url.indexOf("index.html") >= 0 ||
      event.request.url.indexOf("register.html") >= 0 ||
      event.request.url.indexOf("unregister.html") >= 0 ||
      event.request.url.indexOf("ping.html") >= 0 ||
      event.request.url.indexOf("xml.xml") >= 0 ||
      event.request.url.indexOf("csp-violate.sjs") >= 0) {
    // Handle pass-through requests
    event.respondWith(fetch(event.request));
  } else if (event.request.url.indexOf("fetch.txt") >= 0) {
    var body = event.request.context == "fetch" ?
               "so fetch" : "so unfetch";
    event.respondWith(new Response(body));
  } else if (event.request.url.indexOf("img.jpg") >= 0) {
    if (event.request.context == "image") {
      event.respondWith(fetch("realimg.jpg"));
    }
  } else if (event.request.url.indexOf("responsive.jpg") >= 0) {
    if (event.request.context == "imageset") {
      event.respondWith(fetch("realimg.jpg"));
    }
  } else if (event.request.url.indexOf("audio.ogg") >= 0) {
    if (event.request.context == "audio") {
      event.respondWith(fetch("realaudio.ogg"));
    }
  } else if (event.request.url.indexOf("video.ogg") >= 0) {
    if (event.request.context == "video") {
      event.respondWith(fetch("realaudio.ogg"));
    }
  } else if (event.request.url.indexOf("beacon.sjs") >= 0) {
    if (event.request.url.indexOf("queryContext") == -1) {
      event.respondWith(fetch("beacon.sjs?" + event.request.context));
    } else {
      event.respondWith(fetch(event.request));
    }
  } else if (event.request.url.indexOf("csp-report.sjs") >= 0) {
    respondToServiceWorker(event, "csp-report");
  } else if (event.request.url.indexOf("embed") >= 0) {
    respondToServiceWorker(event, "embed");
  } else if (event.request.url.indexOf("object") >= 0) {
    respondToServiceWorker(event, "object");
  } else if (event.request.url.indexOf("font") >= 0) {
    respondToServiceWorker(event, "font");
  } else if (event.request.url.indexOf("iframe") >= 0) {
    if (event.request.context == "iframe") {
      event.respondWith(fetch("context_test.js"));
    }
  } else if (event.request.url.indexOf("frame") >= 0) {
    if (event.request.context == "frame") {
      event.respondWith(fetch("context_test.js"));
    }
  } else if (event.request.url.indexOf("newwindow") >= 0) {
    respondToServiceWorker(event, "newwindow");
  } else if (event.request.url.indexOf("ping") >= 0) {
    respondToServiceWorker(event, "ping");
  } else if (event.request.url.indexOf("plugin") >= 0) {
    respondToServiceWorker(event, "plugin");
  } else if (event.request.url.indexOf("script.js") >= 0) {
    if (event.request.context == "script") {
      event.respondWith(new Response(""));
    }
  } else if (event.request.url.indexOf("style.css") >= 0) {
    respondToServiceWorker(event, "style");
  } else if (event.request.url.indexOf("track") >= 0) {
    respondToServiceWorker(event, "track");
  } else if (event.request.url.indexOf("xhr") >= 0) {
    if (event.request.context == "xmlhttprequest") {
      event.respondWith(new Response(""));
    }
  } else if (event.request.url.indexOf("xslt") >= 0) {
    respondToServiceWorker(event, "xslt");
   } else if (event.request.url.indexOf("myworker") >= 0) {
     if (event.request.context == "worker") {
       event.respondWith(fetch("worker.js"));
     }
   } else if (event.request.url.indexOf("myparentworker") >= 0) {
     if (event.request.context == "worker") {
       event.respondWith(fetch("parentworker.js"));
     }
   } else if (event.request.url.indexOf("mysharedworker") >= 0) {
     if (event.request.context == "sharedworker") {
       event.respondWith(fetch("sharedworker.js"));
     }
   } else if (event.request.url.indexOf("myparentsharedworker") >= 0) {
     if (event.request.context == "sharedworker") {
       event.respondWith(fetch("parentsharedworker.js"));
     }
  } else if (event.request.url.indexOf("cache") >= 0) {
    var cache;
    var origContext = event.request.context;
    event.respondWith(caches.open("cache")
      .then(function(c) {
        cache = c;
        // Store the Request in the cache.
        return cache.put(event.request, new Response("fake"));
      }).then(function() {
        // Read it back.
        return cache.keys(event.request);
      }).then(function(res) {
        var req = res[0];
        // Check to see if the context remained the same.
        var success = req.context === origContext;
        return clients.matchAll()
               .then(function(clients) {
                 // Report it back to the main page.
                 clients.forEach(function(c) {
                   c.postMessage({data: "cache", success: success});
                 });
      })}).then(function() {
        // Cleanup.
        return caches.delete("cache");
      }).then(function() {
        return new Response("ack");
      }));
  }
  // Fail any request that we don't know about.
  try {
    event.respondWith(Promise.reject(event.request.url));
    dump("Fetch event received invalid context value " + event.request.context +
         " for " + event.request.url + "\n");
  } catch(e) {
    // Eat up the possible InvalidStateError exception that we may get if some
    // code above has called respondWith too.
  }
});

function respondToServiceWorker(event, data) {
  event.respondWith(clients.matchAll()
                    .then(function(clients) {
                      clients.forEach(function(c) {
                        c.postMessage({data: data, context: event.request.context});
                      });
                      return new Response("ack");
                    }));
}