From b9acc431b55d9c8144c70bdd5958b67f48bf8b8c Mon Sep 17 00:00:00 2001 From: Ehsan Akhgari Date: Tue, 16 Oct 2012 18:06:38 -0400 Subject: [PATCH] Bug 722983 - Fix the back-end handling of permanent private browsing mode through the per-window private browsing APIs; r=jdm --HG-- extra : rebase_source : 31358b29c5b3c5f6921598ea3246aede62ca78db --- browser/components/preferences/jar.mn | 2 +- browser/components/preferences/privacy.js | 10 +++++--- .../windowwatcher/src/nsWindowWatcher.cpp | 5 +++- toolkit/content/PrivateBrowsingUtils.jsm | 23 +++++++++++++++++- xpfe/appshell/src/nsAppShellService.cpp | 24 ++++++++++++------- 5 files changed, 50 insertions(+), 14 deletions(-) diff --git a/browser/components/preferences/jar.mn b/browser/components/preferences/jar.mn index dc63cace534..071ea3e2420 100644 --- a/browser/components/preferences/jar.mn +++ b/browser/components/preferences/jar.mn @@ -33,7 +33,7 @@ browser.jar: content/browser/preferences/permissions.js * content/browser/preferences/preferences.xul content/browser/preferences/privacy.xul - content/browser/preferences/privacy.js +* content/browser/preferences/privacy.js content/browser/preferences/sanitize.xul content/browser/preferences/security.xul content/browser/preferences/security.js diff --git a/browser/components/preferences/privacy.js b/browser/components/preferences/privacy.js index e0f73e07e23..35e3e3313ce 100644 --- a/browser/components/preferences/privacy.js +++ b/browser/components/preferences/privacy.js @@ -222,18 +222,22 @@ var gPrivacyPane = { observe: function PPP_observe(aSubject, aTopic, aData) { - let privateBrowsingService = Components.classes["@mozilla.org/privatebrowsing;1"]. - getService(Components.interfaces.nsIPrivateBrowsingService); - // Toggle the private browsing mode without switching the session let prefValue = document.getElementById("browser.privatebrowsing.autostart").value; let keepCurrentSession = document.getElementById("browser.privatebrowsing.keep_current_session"); keepCurrentSession.value = true; + +#ifndef MOZ_PER_WINDOW_PRIVATE_BROWSING + let privateBrowsingService = Components.classes["@mozilla.org/privatebrowsing;1"]. + getService(Components.interfaces.nsIPrivateBrowsingService); + // If activating from within the private browsing mode, reset the // private session if (prefValue && privateBrowsingService.privateBrowsingEnabled) privateBrowsingService.privateBrowsingEnabled = false; privateBrowsingService.privateBrowsingEnabled = prefValue; +#endif + keepCurrentSession.reset(); } }, diff --git a/embedding/components/windowwatcher/src/nsWindowWatcher.cpp b/embedding/components/windowwatcher/src/nsWindowWatcher.cpp index 8ab54737200..429747ba395 100644 --- a/embedding/components/windowwatcher/src/nsWindowWatcher.cpp +++ b/embedding/components/windowwatcher/src/nsWindowWatcher.cpp @@ -59,6 +59,7 @@ #include "nsIPrefBranch.h" #include "nsIPrefService.h" #include "nsSandboxFlags.h" +#include "mozilla/Preferences.h" #ifdef USEWEAKREFS #include "nsIWeakReference.h" @@ -898,8 +899,10 @@ nsWindowWatcher::OpenWindowInternal(nsIDOMWindow *aParent, } if (windowIsNew) { - // See if the caller has requested a private browsing window. + // See if the caller has requested a private browsing window, or if all + // windows should be private. bool isPrivateBrowsingWindow = + Preferences::GetBool("browser.privatebrowsing.autostart") || !!(chromeFlags & nsIWebBrowserChrome::CHROME_PRIVATE_WINDOW); // Otherwise, propagate the privacy status of the parent window, if // available, to the child. diff --git a/toolkit/content/PrivateBrowsingUtils.jsm b/toolkit/content/PrivateBrowsingUtils.jsm index 36d5e63f310..a60ee3f2fdd 100644 --- a/toolkit/content/PrivateBrowsingUtils.jsm +++ b/toolkit/content/PrivateBrowsingUtils.jsm @@ -4,6 +4,10 @@ var EXPORTED_SYMBOLS = ["PrivateBrowsingUtils"]; +Components.utils.import("resource://gre/modules/Services.jsm"); + +const kAutoStartPref = "browser.components.autostart"; + const Cc = Components.classes; const Ci = Components.interfaces; @@ -17,7 +21,7 @@ var PrivateBrowsingUtils = { get permanentPrivateBrowsing() { #ifdef MOZ_PER_WINDOW_PRIVATE_BROWSING - return false; // permanent PB is not supported for now + return Services.prefs.getBoolPref(kAutoStart, false); #else try { return Cc["@mozilla.org/privatebrowsing;1"]. @@ -29,3 +33,20 @@ var PrivateBrowsingUtils = { #endif } }; + +#ifdef MOZ_PER_WINDOW_PRIVATE_BROWSING +function autoStartObserver(aSubject, aTopic, aData) { + var newValue = Services.prefs.getBoolPref(kAutoStart); + var windowsEnum = Services.wm.getEnumerator(null); + while (windowsEnum.hasMoreElements()) { + var window = windowsEnum.getNext(); + window.QueryInterface(Ci.nsIInterfaceRequestor) + .getInterface(Ci.nsIWebNavigation) + .QueryInterface(Ci.nsILoadContext) + .usePrivateBrowsing = newValue; + } +} + +Services.prefs.addObserver(kAutoStartPref, autoStartObserver, false); +#endif + diff --git a/xpfe/appshell/src/nsAppShellService.cpp b/xpfe/appshell/src/nsAppShellService.cpp index 1453c8abf53..7625463d5cd 100644 --- a/xpfe/appshell/src/nsAppShellService.cpp +++ b/xpfe/appshell/src/nsAppShellService.cpp @@ -354,18 +354,26 @@ nsAppShellService::JustCreateTopWindow(nsIXULWindow *aParent, NS_ENSURE_SUCCESS(rv, rv); - // Ensure that we propagate any existing private browsing status - // from the parent, even if it will not actually be used - // as a parent value. - nsCOMPtr domWin = do_GetInterface(aParent); - nsCOMPtr webNav = do_GetInterface(domWin); - nsCOMPtr parentContext = do_QueryInterface(webNav); + // Enforce the Private Browsing autoStart pref first. + bool isPrivateBrowsingWindow = + Preferences::GetBool("browser.privatebrowsing.autostart"); + if (!isPrivateBrowsingWindow) { + // Ensure that we propagate any existing private browsing status + // from the parent, even if it will not actually be used + // as a parent value. + nsCOMPtr domWin = do_GetInterface(aParent); + nsCOMPtr webNav = do_GetInterface(domWin); + nsCOMPtr parentContext = do_QueryInterface(webNav); + if (parentContext) { + isPrivateBrowsingWindow = parentContext->UsePrivateBrowsing(); + } + } nsCOMPtr newDomWin = do_GetInterface(NS_ISUPPORTS_CAST(nsIBaseWindow*, window)); nsCOMPtr newWebNav = do_GetInterface(newDomWin); nsCOMPtr thisContext = do_GetInterface(newWebNav); - if (parentContext && thisContext) { - thisContext->SetUsePrivateBrowsing(parentContext->UsePrivateBrowsing()); + if (thisContext) { + thisContext->SetUsePrivateBrowsing(isPrivateBrowsingWindow); } window.swap(*aResult); // transfer reference