summaryrefslogtreecommitdiffstats
path: root/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_favicon.js
blob: eea0ab07cae239d9daec54dfb17f9b7d72d4f2f3 (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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// This test make sure that the favicon of the private browsing is isolated.

const TEST_SITE = "https://example.com";
const TEST_CACHE_SITE = "https://test1.example.com";
const TEST_DIRECTORY =
  "/browser/browser/components/privatebrowsing/test/browser/";

const TEST_PAGE = TEST_SITE + TEST_DIRECTORY + "file_favicon.html";
const TEST_CACHE_PAGE = TEST_CACHE_SITE + TEST_DIRECTORY + "file_favicon.html";
const FAVICON_URI = TEST_SITE + TEST_DIRECTORY + "file_favicon.png";
const FAVICON_CACHE_URI = TEST_CACHE_SITE + TEST_DIRECTORY + "file_favicon.png";

let systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal();

function clearAllImageCaches() {
  let tools = SpecialPowers.Cc["@mozilla.org/image/tools;1"].getService(
    SpecialPowers.Ci.imgITools
  );
  let imageCache = tools.getImgCacheForDocument(window.document);
  imageCache.clearCache(true); // true=chrome
  imageCache.clearCache(false); // false=content
}

function clearAllPlacesFavicons() {
  let faviconService = Cc["@mozilla.org/browser/favicon-service;1"].getService(
    Ci.nsIFaviconService
  );

  return new Promise(resolve => {
    let observer = {
      observe(aSubject, aTopic, aData) {
        if (aTopic === "places-favicons-expired") {
          resolve();
          Services.obs.removeObserver(observer, "places-favicons-expired");
        }
      },
    };

    Services.obs.addObserver(observer, "places-favicons-expired");
    faviconService.expireAllFavicons();
  });
}

function observeFavicon(aIsPrivate, aExpectedCookie, aPageURI) {
  let attr = {};

  if (aIsPrivate) {
    attr.privateBrowsingId = 1;
  }

  let expectedPrincipal = Services.scriptSecurityManager.createContentPrincipal(
    aPageURI,
    attr
  );

  return new Promise(resolve => {
    let observer = {
      observe(aSubject, aTopic, aData) {
        // Make sure that the topic is 'http-on-modify-request'.
        if (aTopic === "http-on-modify-request") {
          // We check the privateBrowsingId for the originAttributes of the loading
          // channel. All requests for the favicon should contain the correct
          // privateBrowsingId. There are two requests for a favicon loading, one
          // from the Places library and one from the XUL image. The difference
          // of them is the loading principal. The Places will use the content
          // principal and the XUL image will use the system principal.

          let httpChannel = aSubject.QueryInterface(Ci.nsIHttpChannel);
          let reqLoadInfo = httpChannel.loadInfo;
          let loadingPrincipal = reqLoadInfo.loadingPrincipal;

          // Make sure this is a favicon request.
          if (httpChannel.URI.spec !== FAVICON_URI) {
            return;
          }

          // Check the privateBrowsingId.
          if (aIsPrivate) {
            is(
              reqLoadInfo.originAttributes.privateBrowsingId,
              1,
              "The loadInfo has correct privateBrowsingId"
            );
          } else {
            is(
              reqLoadInfo.originAttributes.privateBrowsingId,
              0,
              "The loadInfo has correct privateBrowsingId"
            );
          }

          ok(
            loadingPrincipal.equals(expectedPrincipal),
            "The loadingPrincipal of favicon loading from Places should be the content prinicpal"
          );

          let faviconCookie = httpChannel.getRequestHeader("cookie");

          is(
            faviconCookie,
            aExpectedCookie,
            "The cookie of the favicon loading is correct."
          );
        } else {
          ok(false, "Received unexpected topic: ", aTopic);
        }

        resolve();
        Services.obs.removeObserver(observer, "http-on-modify-request");
      },
    };

    Services.obs.addObserver(observer, "http-on-modify-request");
  });
}

function waitOnFaviconResponse(aFaviconURL) {
  return new Promise(resolve => {
    let observer = {
      observe(aSubject, aTopic, aData) {
        if (
          aTopic === "http-on-examine-response" ||
          aTopic === "http-on-examine-cached-response"
        ) {
          let httpChannel = aSubject.QueryInterface(Ci.nsIHttpChannel);
          let loadInfo = httpChannel.loadInfo;

          if (httpChannel.URI.spec !== aFaviconURL) {
            return;
          }

          let result = {
            topic: aTopic,
            privateBrowsingId: loadInfo.originAttributes.privateBrowsingId,
          };

          resolve(result);
          Services.obs.removeObserver(observer, "http-on-examine-response");
          Services.obs.removeObserver(
            observer,
            "http-on-examine-cached-response"
          );
        }
      },
    };

    Services.obs.addObserver(observer, "http-on-examine-response");
    Services.obs.addObserver(observer, "http-on-examine-cached-response");
  });
}

function waitOnFaviconLoaded(aFaviconURL) {
  return PlacesTestUtils.waitForNotification("favicon-changed", events =>
    events.some(e => e.faviconUrl == aFaviconURL)
  );
}

async function assignCookies(aBrowser, aURL, aCookieValue) {
  let tabInfo = await openTab(aBrowser, aURL);

  await SpecialPowers.spawn(
    tabInfo.browser,
    [aCookieValue],
    async function (value) {
      content.document.cookie = value;
    }
  );

  BrowserTestUtils.removeTab(tabInfo.tab);
}

async function openTab(aBrowser, aURL) {
  let tab = BrowserTestUtils.addTab(aBrowser, aURL);

  // Select tab and make sure its browser is focused.
  aBrowser.selectedTab = tab;
  tab.ownerGlobal.focus();

  let browser = aBrowser.getBrowserForTab(tab);
  await BrowserTestUtils.browserLoaded(browser);
  return { tab, browser };
}

registerCleanupFunction(async () => {
  Services.cookies.removeAll();
  clearAllImageCaches();
  Services.cache2.clear();
  await PlacesUtils.history.clear();
  await PlacesUtils.bookmarks.eraseEverything();
});

add_task(async function test_favicon_privateBrowsing() {
  // Clear all image caches before running the test.
  clearAllImageCaches();
  // Clear all favicons in Places.
  await clearAllPlacesFavicons();

  // Create a private browsing window.
  let privateWindow = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });
  let pageURI = Services.io.newURI(TEST_PAGE);

  // Generate two random cookies for non-private window and private window
  // respectively.
  let cookies = [];
  cookies.push(Math.random().toString());
  cookies.push(Math.random().toString());

  // Open a tab in private window and add a cookie into it.
  await assignCookies(privateWindow.gBrowser, TEST_SITE, cookies[0]);

  // Open a tab in non-private window and add a cookie into it.
  await assignCookies(gBrowser, TEST_SITE, cookies[1]);

  // Add the observer earlier in case we don't capture events in time.
  let promiseObserveFavicon = observeFavicon(true, cookies[0], pageURI);

  // The page must be bookmarked for favicon requests to go through in PB mode.
  await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: TEST_PAGE,
  });

  // Open a tab for the private window.
  let tabInfo = await openTab(privateWindow.gBrowser, TEST_PAGE);

  info("Waiting until favicon requests are all made in private window.");
  await promiseObserveFavicon;

  // Close the tab.
  BrowserTestUtils.removeTab(tabInfo.tab);
  // FIXME: We need to wait for the next event tick here to avoid observing
  //        the previous tab info in the next step (bug 1446725).
  await new Promise(executeSoon);

  // Add the observer earlier in case we don't capture events in time.
  promiseObserveFavicon = observeFavicon(false, cookies[1], pageURI);

  // Open a tab for the non-private window.
  tabInfo = await openTab(gBrowser, TEST_PAGE);

  info("Waiting until favicon requests are all made in non-private window.");
  await promiseObserveFavicon;

  // Close the tab.
  BrowserTestUtils.removeTab(tabInfo.tab);
  await BrowserTestUtils.closeWindow(privateWindow);
});

add_task(async function test_favicon_cache_privateBrowsing() {
  // Clear all image caches and network cache before running the test.
  clearAllImageCaches();

  Services.cache2.clear();

  // Clear all favicons in Places.
  await clearAllPlacesFavicons();

  // Add an observer for making sure the favicon has been loaded and cached.
  let promiseFaviconLoaded = waitOnFaviconLoaded(FAVICON_CACHE_URI);
  let promiseFaviconResponse = waitOnFaviconResponse(FAVICON_CACHE_URI);

  // Open a tab for the non-private window.
  let tabInfoNonPrivate = await openTab(gBrowser, TEST_CACHE_PAGE);

  let response = await promiseFaviconResponse;

  await promiseFaviconLoaded;

  // Check that the favicon response has come from the network and it has the
  // correct privateBrowsingId.
  is(
    response.topic,
    "http-on-examine-response",
    "The favicon image should be loaded through network."
  );
  is(
    response.privateBrowsingId,
    0,
    "We should observe the network response for the non-private tab."
  );

  // Create a private browsing window.
  let privateWindow = await BrowserTestUtils.openNewBrowserWindow({
    private: true,
  });

  // The page must be bookmarked for favicon requests to go through in PB mode.
  await PlacesUtils.bookmarks.insert({
    parentGuid: PlacesUtils.bookmarks.unfiledGuid,
    url: TEST_CACHE_PAGE,
  });

  promiseFaviconResponse = waitOnFaviconResponse(FAVICON_CACHE_URI);

  // Open a tab for the private window.
  let tabInfoPrivate = await openTab(privateWindow.gBrowser, TEST_CACHE_PAGE);

  // Wait for the favicon response of the private tab.
  response = await promiseFaviconResponse;

  // Make sure the favicon is loaded through the network and its privateBrowsingId is correct.
  is(
    response.topic,
    "http-on-examine-response",
    "The favicon image should be loaded through the network again."
  );
  is(
    response.privateBrowsingId,
    1,
    "We should observe the network response for the private tab."
  );

  BrowserTestUtils.removeTab(tabInfoPrivate.tab);
  BrowserTestUtils.removeTab(tabInfoNonPrivate.tab);
  await BrowserTestUtils.closeWindow(privateWindow);
});