From b73190dfcbb9dc1110b58e425b11ba202891078e Mon Sep 17 00:00:00 2001 From: Erik Vold Date: Tue, 9 Jul 2013 13:21:23 -0700 Subject: [PATCH] Bug 885054 - Sidebars should not migrate across private / non-private window. r=gavin --- browser/base/content/browser.js | 3 +- .../privatebrowsing/test/browser/Makefile.in | 1 + .../browser_privatebrowsing_sidebar.js | 92 +++++++++++++++++++ .../privatebrowsing/test/browser/head.js | 15 ++- 4 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 browser/components/privatebrowsing/test/browser/browser_privatebrowsing_sidebar.js diff --git a/browser/base/content/browser.js b/browser/base/content/browser.js index 64f13ae3813f..9ea443cdf27c 100644 --- a/browser/base/content/browser.js +++ b/browser/base/content/browser.js @@ -823,7 +823,8 @@ var gBrowserInit = { // setup history swipe animation gHistorySwipeAnimation.init(); - if (window.opener && !window.opener.closed) { + if (window.opener && !window.opener.closed && + PrivateBrowsingUtils.isWindowPrivate(window) == PrivateBrowsingUtils.isWindowPrivate(window.opener)) { let openerSidebarBox = window.opener.document.getElementById("sidebar-box"); // If the opener had a sidebar, open the same sidebar in our window. // The opener can be the hidden window too, if we're coming from the state diff --git a/browser/components/privatebrowsing/test/browser/Makefile.in b/browser/components/privatebrowsing/test/browser/Makefile.in index 5d31c3f71816..03be7c9562d0 100644 --- a/browser/components/privatebrowsing/test/browser/Makefile.in +++ b/browser/components/privatebrowsing/test/browser/Makefile.in @@ -45,6 +45,7 @@ MOCHITEST_BROWSER_FILES = \ browser_privatebrowsing_popupblocker.js \ browser_privatebrowsing_protocolhandler.js \ browser_privatebrowsing_protocolhandler_page.html \ + browser_privatebrowsing_sidebar.js \ browser_privatebrowsing_theming.js \ browser_privatebrowsing_ui.js \ browser_privatebrowsing_urlbarfocus.js \ diff --git a/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_sidebar.js b/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_sidebar.js new file mode 100644 index 000000000000..7883f5d55df3 --- /dev/null +++ b/browser/components/privatebrowsing/test/browser/browser_privatebrowsing_sidebar.js @@ -0,0 +1,92 @@ +/* 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 makes sure that Sidebars do not migrate across windows with +// different privacy states + +// See Bug 885054: https://bugzilla.mozilla.org/show_bug.cgi?id=885054 + +function test() { + waitForExplicitFinish(); + + let { utils: Cu } = Components; + + let { Promise: { defer } } = Cu.import("resource://gre/modules/Promise.jsm", {}); + + // opens a sidebar + function openSidebar(win) { + let { promise, resolve } = defer(); + let doc = win.document; + + let sidebarID = 'viewBookmarksSidebar'; + + let sidebar = doc.getElementById('sidebar'); + + let sidebarurl = doc.getElementById(sidebarID).getAttribute('sidebarurl'); + + sidebar.addEventListener('load', function onSidebarLoad() { + if (sidebar.contentWindow.location.href != sidebarurl) + return; + sidebar.removeEventListener('load', onSidebarLoad, true); + + resolve(win); + }, true); + + win.toggleSidebar(sidebarID, true); + + return promise; + } + + let windowCache = []; + function cacheWindow(w) { + windowCache.push(w); + return w; + } + function closeCachedWindows () { + windowCache.forEach(function(w) w.close()); + } + + // Part 1: NON PRIVATE WINDOW -> PRIVATE WINDOW + openWindow(window, {}, 1). + then(cacheWindow). + then(openSidebar). + then(function(win) openWindow(win, { private: true })). + then(cacheWindow). + then(function({ document }) { + let sidebarBox = document.getElementById("sidebar-box"); + is(sidebarBox.hidden, true, 'Opening a private window from reg window does not open the sidebar'); + }). + // Part 2: NON PRIVATE WINDOW -> NON PRIVATE WINDOW + then(function() openWindow(window)). + then(cacheWindow). + then(openSidebar). + then(function(win) openWindow(win)). + then(cacheWindow). + then(function({ document }) { + let sidebarBox = document.getElementById("sidebar-box"); + is(sidebarBox.hidden, false, 'Opening a reg window from reg window does open the sidebar'); + }). + // Part 3: PRIVATE WINDOW -> NON PRIVATE WINDOW + then(function() openWindow(window, { private: true })). + then(cacheWindow). + then(openSidebar). + then(function(win) openWindow(win)). + then(cacheWindow). + then(function({ document }) { + let sidebarBox = document.getElementById("sidebar-box"); + is(sidebarBox.hidden, true, 'Opening a reg window from a private window does not open the sidebar'); + }). + // Part 4: PRIVATE WINDOW -> PRIVATE WINDOW + then(function() openWindow(window, { private: true })). + then(cacheWindow). + then(openSidebar). + then(function(win) openWindow(win, { private: true })). + then(cacheWindow). + then(function({ document }) { + let sidebarBox = document.getElementById("sidebar-box"); + is(sidebarBox.hidden, false, 'Opening a private window from private window does open the sidebar'); + }). + then(closeCachedWindows). + then(finish); +} diff --git a/browser/components/privatebrowsing/test/browser/head.js b/browser/components/privatebrowsing/test/browser/head.js index 9569a901f1bc..a34ec8bbfc8f 100644 --- a/browser/components/privatebrowsing/test/browser/head.js +++ b/browser/components/privatebrowsing/test/browser/head.js @@ -31,6 +31,20 @@ function whenNewWindowLoaded(aOptions, aCallback) { return win; } +function openWindow(aParent, aOptions, a3) { + let { Promise: { defer } } = Components.utils.import("resource://gre/modules/Promise.jsm", {}); + let { promise, resolve } = defer(); + + let win = aParent.OpenBrowserWindow(aOptions); + + win.addEventListener("load", function onLoad() { + win.removeEventListener("load", onLoad, false); + resolve(win); + }, false); + + return promise; +} + function newDirectory() { let FileUtils = Cu.import("resource://gre/modules/FileUtils.jsm", {}).FileUtils; @@ -62,4 +76,3 @@ function _initTest() { } _initTest(); -