Bug 729162 - Add per-docshell privacy mode transition observers. r=bz

This commit is contained in:
Josh Matthews 2012-04-19 23:19:54 -04:00
parent fb81af3718
commit 03e6832624
7 changed files with 72 additions and 3 deletions

View File

@ -83,6 +83,7 @@ XPIDLSRCS = \
nsIRefreshURI.idl \
nsIContentViewerContainer.idl \
nsIDocumentLoaderFactory.idl \
nsIPrivacyTransitionObserver.idl \
$(NULL)
EXPORTS = \

View File

@ -107,6 +107,7 @@
#include "nsIScriptChannel.h"
#include "nsIOfflineCacheUpdate.h"
#include "nsITimedChannel.h"
#include "nsIPrivacyTransitionObserver.h"
#include "nsCPrefetchService.h"
#include "nsJSON.h"
#include "IHistory.h"
@ -2041,7 +2042,8 @@ nsDocShell::GetUsePrivateBrowsing(bool* aUsePrivateBrowsing)
NS_IMETHODIMP
nsDocShell::SetUsePrivateBrowsing(bool aUsePrivateBrowsing)
{
if (aUsePrivateBrowsing != mInPrivateBrowsing) {
bool changed = aUsePrivateBrowsing != mInPrivateBrowsing;
if (changed) {
mInPrivateBrowsing = aUsePrivateBrowsing;
if (aUsePrivateBrowsing) {
IncreasePrivateDocShellCount();
@ -2057,9 +2059,32 @@ nsDocShell::SetUsePrivateBrowsing(bool aUsePrivateBrowsing)
shell->SetUsePrivateBrowsing(aUsePrivateBrowsing);
}
}
if (changed) {
nsTObserverArray<nsWeakPtr>::ForwardIterator iter(mPrivacyObservers);
while (iter.HasMore()) {
nsWeakPtr ref = iter.GetNext();
nsCOMPtr<nsIPrivacyTransitionObserver> obs = do_QueryReferent(ref);
if (!obs) {
mPrivacyObservers.RemoveElement(ref);
} else {
obs->PrivateModeChanged(aUsePrivateBrowsing);
}
}
}
return NS_OK;
}
NS_IMETHODIMP
nsDocShell::AddWeakPrivacyTransitionObserver(nsIPrivacyTransitionObserver* aObserver)
{
nsWeakPtr weakObs = do_GetWeakReference(aObserver);
if (!weakObs) {
return NS_ERROR_NOT_AVAILABLE;
}
return mPrivacyObservers.AppendElement(weakObs) ? NS_OK : NS_ERROR_FAILURE;
}
NS_IMETHODIMP nsDocShell::GetAllowMetaRedirects(bool * aReturn)
{
NS_ENSURE_ARG_POINTER(aReturn);

View File

@ -847,6 +847,7 @@ protected:
private:
nsCOMPtr<nsIAtom> mForcedCharset;
nsCOMPtr<nsIAtom> mParentCharset;
nsTObserverArray<nsWeakPtr> mPrivacyObservers;
PRInt32 mParentCharsetSource;
#ifdef DEBUG

View File

@ -71,8 +71,9 @@ interface nsIDOMStorage;
interface nsIPrincipal;
interface nsIWebBrowserPrint;
interface nsIVariant;
interface nsIPrivacyTransitionObserver;
[scriptable, uuid(c7325422-817e-4321-957a-c0bdd764941d)]
[scriptable, uuid(6f60ac96-fa2c-41a5-92b4-29aaadbd7a7b)]
interface nsIDocShell : nsISupports
{
/**
@ -603,6 +604,12 @@ interface nsIDocShell : nsISupports
*/
attribute PRInt32 parentCharsetSource;
/**
* Add an observer to the list of parties to be notified when this docshell's
* private browsing status is changed. |obs| must support weak references.
*/
void addWeakPrivacyTransitionObserver(in nsIPrivacyTransitionObserver obs);
/*
* Is this docshell a browser frame (i.e., does it correspond to an <iframe
* mozbrowser>)? The frameloader is responsible for setting this property

View File

@ -0,0 +1,11 @@
/* 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/. */
#include "nsISupports.idl"
[scriptable, function, uuid(b4b1449d-0ef0-47f5-b62e-adc57fd49702)]
interface nsIPrivacyTransitionObserver : nsISupports
{
void privateModeChanged(in bool enabled);
};

View File

@ -0,0 +1,23 @@
var gNotifications = 0;
var observer = {
QueryInterface: function(iid) {
if (Ci.nsIPrivacyTransitionObserver.equals(iid) ||
Ci.nsISupportsWeakReference.equals(iid) ||
Ci.nsISupports.equals(iid))
return this;
throw Cr.NS_ERROR_NO_INTERFACE;
},
privateModeChanged: function(enabled) {
gNotifications++;
}
}
function run_test() {
var docshell = Cc["@mozilla.org/docshell;1"].createInstance(Ci.nsIDocShell);
docshell.addWeakPrivacyTransitionObserver(observer);
docshell.QueryInterface(Ci.nsILoadContext).usePrivateBrowsing = true;
docshell.QueryInterface(Ci.nsILoadContext).usePrivateBrowsing = false;
do_check_eq(gNotifications, 2);
}

View File

@ -5,4 +5,5 @@ tail =
[test_bug414201_jfif.js]
[test_bug442584.js]
[test_nsIDownloadHistory.js]
[test_pb_notification.js]
[test_pb_notification.js]
[test_privacy_transition.js]