summaryrefslogtreecommitdiffstats
path: root/dom/serviceworkers/test/browser_storage_permission.js
blob: 68c2c18bade9dbe788d3b45d0860c7d00bba6868 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
"use strict";

const { PermissionTestUtils } = ChromeUtils.import(
  "resource://testing-common/PermissionTestUtils.jsm"
);

const BASE_URI = "http://mochi.test:8888/browser/dom/serviceworkers/test/";
const PAGE_URI = BASE_URI + "empty.html";
const SCOPE = PAGE_URI + "?storage_permission";
const SW_SCRIPT = BASE_URI + "empty.js";

add_setup(async function() {
  await SpecialPowers.pushPrefEnv({
    set: [
      // Until the e10s refactor is complete, use a single process to avoid
      // service worker propagation race.
      ["dom.ipc.processCount", 1],
      ["dom.serviceWorkers.enabled", true],
      ["dom.serviceWorkers.testing.enabled", true],
    ],
  });

  let tab = BrowserTestUtils.addTab(gBrowser, PAGE_URI);
  let browser = gBrowser.getBrowserForTab(tab);
  await BrowserTestUtils.browserLoaded(browser);

  await SpecialPowers.spawn(
    browser,
    [{ script: SW_SCRIPT, scope: SCOPE }],
    async function(opts) {
      let reg = await content.navigator.serviceWorker.register(opts.script, {
        scope: opts.scope,
      });
      let worker = reg.installing || reg.waiting || reg.active;
      await new Promise(resolve => {
        if (worker.state === "activated") {
          resolve();
          return;
        }
        worker.addEventListener("statechange", function onStateChange() {
          if (worker.state === "activated") {
            worker.removeEventListener("statechange", onStateChange);
            resolve();
          }
        });
      });
    }
  );

  BrowserTestUtils.removeTab(tab);
});

add_task(async function test_allow_permission() {
  PermissionTestUtils.add(
    PAGE_URI,
    "cookie",
    Ci.nsICookiePermission.ACCESS_ALLOW
  );

  let tab = BrowserTestUtils.addTab(gBrowser, SCOPE);
  let browser = gBrowser.getBrowserForTab(tab);
  await BrowserTestUtils.browserLoaded(browser);

  let controller = await SpecialPowers.spawn(browser, [], async function() {
    return content.navigator.serviceWorker.controller;
  });

  ok(!!controller, "page should be controlled with storage allowed");

  BrowserTestUtils.removeTab(tab);
});

add_task(async function test_deny_permission() {
  PermissionTestUtils.add(
    PAGE_URI,
    "cookie",
    Ci.nsICookiePermission.ACCESS_DENY
  );

  let tab = BrowserTestUtils.addTab(gBrowser, SCOPE);
  let browser = gBrowser.getBrowserForTab(tab);
  await BrowserTestUtils.browserLoaded(browser);

  let controller = await SpecialPowers.spawn(browser, [], async function() {
    return content.navigator.serviceWorker.controller;
  });

  is(controller, null, "page should be not controlled with storage denied");

  BrowserTestUtils.removeTab(tab);
  PermissionTestUtils.remove(PAGE_URI, "cookie");
});

add_task(async function test_session_permission() {
  PermissionTestUtils.add(
    PAGE_URI,
    "cookie",
    Ci.nsICookiePermission.ACCESS_SESSION
  );

  let tab = BrowserTestUtils.addTab(gBrowser, SCOPE);
  let browser = gBrowser.getBrowserForTab(tab);
  await BrowserTestUtils.browserLoaded(browser);

  let controller = await SpecialPowers.spawn(browser, [], async function() {
    return content.navigator.serviceWorker.controller;
  });

  is(controller, null, "page should be not controlled with session storage");

  BrowserTestUtils.removeTab(tab);
  PermissionTestUtils.remove(PAGE_URI, "cookie");
});

// Test to verify an about:blank iframe successfully inherits the
// parent's controller when storage is blocked between opening the
// parent page and creating the iframe.
add_task(async function test_block_storage_before_blank_iframe() {
  PermissionTestUtils.remove(PAGE_URI, "cookie");

  let tab = BrowserTestUtils.addTab(gBrowser, SCOPE);
  let browser = gBrowser.getBrowserForTab(tab);
  await BrowserTestUtils.browserLoaded(browser);

  let controller = await SpecialPowers.spawn(browser, [], async function() {
    return content.navigator.serviceWorker.controller;
  });

  ok(!!controller, "page should be controlled with storage allowed");

  let controller2 = await SpecialPowers.spawn(browser, [], async function() {
    let f = content.document.createElement("iframe");
    content.document.body.appendChild(f);
    await new Promise(resolve => (f.onload = resolve));
    return !!f.contentWindow.navigator.serviceWorker.controller;
  });

  ok(!!controller2, "page should be controlled with storage allowed");

  await SpecialPowers.pushPrefEnv({
    set: [
      ["network.cookie.cookieBehavior", Ci.nsICookieService.BEHAVIOR_REJECT],
    ],
  });

  let controller3 = await SpecialPowers.spawn(browser, [], async function() {
    let f = content.document.createElement("iframe");
    content.document.body.appendChild(f);
    await new Promise(resolve => (f.onload = resolve));
    return !!f.contentWindow.navigator.serviceWorker.controller;
  });

  ok(!!controller3, "page should be controlled with storage allowed");

  await SpecialPowers.popPrefEnv();
  BrowserTestUtils.removeTab(tab);
});

// Test to verify a blob URL iframe successfully inherits the
// parent's controller when storage is blocked between opening the
// parent page and creating the iframe.
add_task(async function test_block_storage_before_blob_iframe() {
  PermissionTestUtils.remove(PAGE_URI, "cookie");

  let tab = BrowserTestUtils.addTab(gBrowser, SCOPE);
  let browser = gBrowser.getBrowserForTab(tab);
  await BrowserTestUtils.browserLoaded(browser);

  let controller = await SpecialPowers.spawn(browser, [], async function() {
    return content.navigator.serviceWorker.controller;
  });

  ok(!!controller, "page should be controlled with storage allowed");

  let controller2 = await SpecialPowers.spawn(browser, [], async function() {
    let b = new content.Blob(["<!DOCTYPE html><html></html>"], {
      type: "text/html",
    });
    let f = content.document.createElement("iframe");
    // No need to call revokeObjectURL() since the window will be closed shortly.
    f.src = content.URL.createObjectURL(b);
    content.document.body.appendChild(f);
    await new Promise(resolve => (f.onload = resolve));
    return !!f.contentWindow.navigator.serviceWorker.controller;
  });

  ok(!!controller2, "page should be controlled with storage allowed");

  await SpecialPowers.pushPrefEnv({
    set: [
      ["network.cookie.cookieBehavior", Ci.nsICookieService.BEHAVIOR_REJECT],
    ],
  });

  let controller3 = await SpecialPowers.spawn(browser, [], async function() {
    let b = new content.Blob(["<!DOCTYPE html><html></html>"], {
      type: "text/html",
    });
    let f = content.document.createElement("iframe");
    // No need to call revokeObjectURL() since the window will be closed shortly.
    f.src = content.URL.createObjectURL(b);
    content.document.body.appendChild(f);
    await new Promise(resolve => (f.onload = resolve));
    return !!f.contentWindow.navigator.serviceWorker.controller;
  });

  ok(!!controller3, "page should be controlled with storage allowed");

  await SpecialPowers.popPrefEnv();
  BrowserTestUtils.removeTab(tab);
});

// Test to verify a blob worker script does not hit our service
// worker storage assertions when storage is blocked between opening
// the parent page and creating the worker.  Note, we cannot
// explicitly check if the worker is controlled since we don't expose
// WorkerNavigator.serviceWorkers.controller yet.
add_task(async function test_block_storage_before_blob_worker() {
  PermissionTestUtils.remove(PAGE_URI, "cookie");

  let tab = BrowserTestUtils.addTab(gBrowser, SCOPE);
  let browser = gBrowser.getBrowserForTab(tab);
  await BrowserTestUtils.browserLoaded(browser);

  let controller = await SpecialPowers.spawn(browser, [], async function() {
    return content.navigator.serviceWorker.controller;
  });

  ok(!!controller, "page should be controlled with storage allowed");

  let scriptURL = await SpecialPowers.spawn(browser, [], async function() {
    let b = new content.Blob(
      ["self.postMessage(self.location.href);self.close()"],
      { type: "application/javascript" }
    );
    // No need to call revokeObjectURL() since the window will be closed shortly.
    let u = content.URL.createObjectURL(b);
    let w = new content.Worker(u);
    return await new Promise(resolve => {
      w.onmessage = e => resolve(e.data);
    });
  });

  ok(scriptURL.startsWith("blob:"), "blob URL worker should run");

  await SpecialPowers.pushPrefEnv({
    set: [
      ["network.cookie.cookieBehavior", Ci.nsICookieService.BEHAVIOR_REJECT],
    ],
  });

  let scriptURL2 = await SpecialPowers.spawn(browser, [], async function() {
    let b = new content.Blob(
      ["self.postMessage(self.location.href);self.close()"],
      { type: "application/javascript" }
    );
    // No need to call revokeObjectURL() since the window will be closed shortly.
    let u = content.URL.createObjectURL(b);
    let w = new content.Worker(u);
    return await new Promise(resolve => {
      w.onmessage = e => resolve(e.data);
    });
  });

  ok(scriptURL2.startsWith("blob:"), "blob URL worker should run");

  await SpecialPowers.popPrefEnv();
  BrowserTestUtils.removeTab(tab);
});

add_task(async function cleanup() {
  PermissionTestUtils.remove(PAGE_URI, "cookie");

  let tab = BrowserTestUtils.addTab(gBrowser, PAGE_URI);
  let browser = gBrowser.getBrowserForTab(tab);
  await BrowserTestUtils.browserLoaded(browser);

  await SpecialPowers.spawn(browser, [SCOPE], async function(uri) {
    let reg = await content.navigator.serviceWorker.getRegistration(uri);
    let worker = reg.active;
    await reg.unregister();
    await new Promise(resolve => {
      if (worker.state === "redundant") {
        resolve();
        return;
      }
      worker.addEventListener("statechange", function onStateChange() {
        if (worker.state === "redundant") {
          worker.removeEventListener("statechange", onStateChange);
          resolve();
        }
      });
    });
  });

  BrowserTestUtils.removeTab(tab);
});