Bug 1089232 - Updates nsContentPrefService to take an extra isPrivate argument. r=adw

This commit is contained in:
Ciro S. Costa 2015-02-20 20:24:35 -02:00
parent 4585df0fdd
commit c6677313e9
7 changed files with 88 additions and 53 deletions

View File

@ -111,8 +111,8 @@ var FullZoom = {
// nsIContentPrefObserver
onContentPrefSet: function FullZoom_onContentPrefSet(aGroup, aName, aValue) {
this._onContentPrefChanged(aGroup, aValue);
onContentPrefSet: function FullZoom_onContentPrefSet(aGroup, aName, aValue, aIsPrivate) {
this._onContentPrefChanged(aGroup, aValue, aIsPrivate);
},
onContentPrefRemoved: function FullZoom_onContentPrefRemoved(aGroup, aName) {
@ -127,7 +127,7 @@ var FullZoom = {
* @param aValue The new value of the changed preference. Pass undefined to
* indicate the preference's removal.
*/
_onContentPrefChanged: function FullZoom__onContentPrefChanged(aGroup, aValue) {
_onContentPrefChanged: function FullZoom__onContentPrefChanged(aGroup, aValue, aIsPrivate) {
if (this._isNextContentPrefChangeInternal) {
// Ignore changes that FullZoom itself makes. This works because the
// content pref service calls callbacks before notifying observers, and it
@ -140,9 +140,10 @@ var FullZoom = {
if (!browser.currentURI)
return;
let ctxt = this._loadContextFromBrowser(browser);
let domain = this._cps2.extractDomain(browser.currentURI.spec);
if (aGroup) {
if (aGroup == domain)
if (aGroup == domain && ctxt.usePrivateBrowsing == aIsPrivate)
this._applyPrefToZoom(aValue, browser);
return;
}
@ -154,7 +155,6 @@ var FullZoom = {
// zoom should be set to the new global preference now that the global
// preference has changed.
let hasPref = false;
let ctxt = this._loadContextFromBrowser(browser);
let token = this._getBrowserToken(browser);
this._cps2.getByDomainAndName(browser.currentURI.spec, this.name, ctxt, {
handleResult: function () { hasPref = true; },

View File

@ -10,22 +10,27 @@ interface nsIContentURIGrouper;
interface nsILoadContext;
interface mozIStorageConnection;
[scriptable, uuid(746c7a02-f6c1-4869-b434-7c8b86e60e61)]
[scriptable, uuid(43635c53-b445-4c4e-8cc5-562697299b55)]
interface nsIContentPrefObserver : nsISupports
{
/**
* Called when a content pref is set to a different value.
*
*
* @param aGroup the group to which the pref belongs, or null
* if it's a global pref (applies to all sites)
* @param aName the name of the pref that was set
* @param aValue the new value of the pref
* @param aIsPrivate an optional flag determining whether the
* original context is private or not
*/
void onContentPrefSet(in AString aGroup, in AString aName, in nsIVariant aValue);
void onContentPrefSet(in AString aGroup,
in AString aName,
in nsIVariant aValue,
[optional] in boolean aIsPrivate);
/**
* Called when a content pref is removed.
*
*
* @param aGroup the group to which the pref belongs, or null
* if it's a global pref (applies to all sites)
* @param aName the name of the pref that was removed
@ -58,7 +63,7 @@ interface nsIContentPrefService : nsISupports
*
* @param aGroup the group for which to get the pref, as an nsIURI
* from which the hostname will be used, a string
* (typically in the format of a hostname), or null
* (typically in the format of a hostname), or null
* to get the global pref (applies to all sites)
* @param aName the name of the pref to get
* @param aPrivacyContext
@ -99,7 +104,7 @@ interface nsIContentPrefService : nsISupports
* @throws NS_ERROR_ILLEGAL_VALUE if aName is null or an empty string
*/
void setPref(in nsIVariant aGroup, in AString aName, in nsIVariant aValue, in nsILoadContext aPrivacyContext);
/**
* Check whether or not a pref exists.
*
@ -142,7 +147,7 @@ interface nsIContentPrefService : nsISupports
* @param aGroup the group for which to remove the pref, as an nsIURI
* from which the hostname will be used, a string
* (typically in the format of a hostname), or null
* to remove the global pref (applies to all sites)
* to remove the global pref (applies to all sites)
* @param aName the name of the pref to remove
* @param aPrivacyContext
* a context from which to determine the privacy status
@ -185,13 +190,13 @@ interface nsIContentPrefService : nsISupports
* @param aGroup the group for which to retrieve prefs, as an nsIURI
* from which the hostname will be used, a string
* (typically in the format of a hostname), or null
* to get the global prefs (apply to all sites)
* to get the global prefs (apply to all sites)
* @param aPrivacyContext
* a context from which to determine the privacy status
* of the pref (ie. whether to search for prefs in memory
* or in permanent storage), obtained from a relevant
* window or channel.
*
*
* @returns a property bag of prefs
* @throws NS_ERROR_ILLEGAL_VALUE if aGroup is not a string, nsIURI, or null
*/
@ -206,21 +211,21 @@ interface nsIContentPrefService : nsISupports
* of the pref (ie. whether to search for prefs in memory
* or in permanent storage), obtained from a relevant
* window or channel.
*
*
* @returns a property bag of prefs
* @throws NS_ERROR_ILLEGAL_VALUE if aName is null or an empty string
*/
nsIPropertyBag2 getPrefsByName(in AString aName, in nsILoadContext aContext);
/**
* Add an observer.
*
*
* @param aName the setting to observe, or null to add
* a generic observer that observes all settings
* @param aObserver the observer to add
*/
void addObserver(in AString aName, in nsIContentPrefObserver aObserver);
/**
* Remove an observer.
*

View File

@ -24,8 +24,9 @@ ContentPrefInstance.prototype = {
return this._contentPrefSvc.getPref(aName, aGroup, this._context, aCallback);
},
setPref: function ContentPrefInstance_setPref(aGroup, aName, aValue) {
return this._contentPrefSvc.setPref(aGroup, aName, aValue, this._context);
setPref: function ContentPrefInstance_setPref(aGroup, aName, aValue, aContext) {
return this._contentPrefSvc.setPref(aGroup, aName, aValue,
aContext ? aContext : this._context);
},
hasPref: function ContentPrefInstance_hasPref(aGroup, aName) {

View File

@ -254,7 +254,7 @@ ContentPrefService2.prototype = {
this._pbStore.set(group, name, value);
this._schedule(function () {
cbHandleCompletion(callback, Ci.nsIContentPrefCallback2.COMPLETE_OK);
this._cps._notifyPrefSet(group, name, value);
this._cps._notifyPrefSet(group, name, value, context.usePrivateBrowsing);
});
return;
}
@ -327,7 +327,7 @@ ContentPrefService2.prototype = {
this._cache.setWithCast(group, name, value);
cbHandleCompletion(callback, reason);
if (ok)
this._cps._notifyPrefSet(group, name, value);
this._cps._notifyPrefSet(group, name, value, context && context.usePrivateBrowsing);
},
onError: function onError(nsresult) {
cbHandleError(callback, nsresult);

View File

@ -256,7 +256,7 @@ ContentPrefService.prototype = {
if (aContext && aContext.usePrivateBrowsing) {
this._privModeStorage.setWithCast(group, aName, aValue);
this._notifyPrefSet(group, aName, aValue);
this._notifyPrefSet(group, aName, aValue, aContext.usePrivateBrowsing);
return;
}
@ -278,7 +278,9 @@ ContentPrefService.prototype = {
this._insertPref(groupID, settingID, aValue);
this._cache.setWithCast(group, aName, aValue);
this._notifyPrefSet(group, aName, aValue);
this._notifyPrefSet(group, aName, aValue,
aContext ? aContext.usePrivateBrowsing : false);
},
hasPref: function ContentPrefService_hasPref(aGroup, aName, aContext) {
@ -382,19 +384,19 @@ ContentPrefService.prototype = {
var settingID = this._selectSettingID(aName);
if (!settingID)
return;
var selectGroupsStmt = this._dbCreateStatement(`
SELECT groups.id AS groupID, groups.name AS groupName
FROM prefs
JOIN groups ON prefs.groupID = groups.id
WHERE prefs.settingID = :setting
`);
var groupNames = [];
var groupIDs = [];
try {
selectGroupsStmt.params.setting = settingID;
while (selectGroupsStmt.executeStep()) {
groupIDs.push(selectGroupsStmt.row["groupID"]);
groupNames.push(selectGroupsStmt.row["groupName"]);
@ -403,7 +405,7 @@ ContentPrefService.prototype = {
finally {
selectGroupsStmt.reset();
}
if (this.hasPref(null, aName)) {
groupNames.push(null);
}
@ -538,10 +540,10 @@ ContentPrefService.prototype = {
/**
* Notify all observers about a preference change.
*/
_notifyPrefSet: function ContentPrefService__notifyPrefSet(aGroup, aName, aValue) {
_notifyPrefSet: function ContentPrefService__notifyPrefSet(aGroup, aName, aValue, aIsPrivate) {
for (var observer of this._getObservers(aName)) {
try {
observer.onContentPrefSet(aGroup, aName, aValue);
observer.onContentPrefSet(aGroup, aName, aValue, aIsPrivate);
}
catch(ex) {
Cu.reportError(ex);
@ -985,7 +987,7 @@ ContentPrefService.prototype = {
finally {
this._stmtSelectPrefsByName.reset();
}
var global = this._selectGlobalPref(aName);
if (typeof global != "undefined") {
prefs.setProperty(null, global);
@ -1261,7 +1263,7 @@ function HostnameGrouper() {}
HostnameGrouper.prototype = {
//**************************************************************************//
// XPCOM Plumbing
classID: Components.ID("{8df290ae-dcaa-4c11-98a5-2429a4dc97bb}"),
QueryInterface: XPCOMUtils.generateQI([Ci.nsIContentURIGrouper]),
@ -1286,7 +1288,7 @@ HostnameGrouper.prototype = {
// reference, and hash, if possible) as the group. This means that URIs
// like about:mozilla and about:blank will be considered separate groups,
// but at least they'll be grouped somehow.
// This also means that each individual file: URL will be considered
// its own group. This seems suboptimal, but so does treating the entire
// file: URL space as a single group (especially if folks start setting

View File

@ -186,7 +186,7 @@ function run_test() {
//**************************************************************************//
// getPrefs
cps.setPref(uri, "test.getPrefs.a", 1);
cps.setPref(uri, "test.getPrefs.b", 2);
cps.setPref(uri, "test.getPrefs.c", 3);
@ -236,7 +236,7 @@ function run_test() {
var specificObserver = {
interfaces: [Ci.nsIContentPrefObserver, Ci.nsISupports],
QueryInterface: function ContentPrefTest_QueryInterface(iid) {
if (!this.interfaces.some( function(v) { return iid.equals(v) } ))
throw Cr.NS_ERROR_NO_INTERFACE;
@ -262,7 +262,7 @@ function run_test() {
var genericObserver = {
interfaces: [Ci.nsIContentPrefObserver, Ci.nsISupports],
QueryInterface: function ContentPrefTest_QueryInterface(iid) {
if (!this.interfaces.some( function(v) { return iid.equals(v) } ))
throw Cr.NS_ERROR_NO_INTERFACE;
@ -270,12 +270,16 @@ function run_test() {
},
numTimesSetCalled: 0,
onContentPrefSet: function genericObserver_onContentPrefSet(group, name, value) {
onContentPrefSet: function genericObserver_onContentPrefSet(group, name, value, isPrivate) {
++this.numTimesSetCalled;
do_check_eq(group, "www.example.com");
if (name != "test.observer.1" && name != "test.observer.2")
if (name == "test.observer.private")
do_check_true(isPrivate);
else if (name == "test.observer.normal")
do_check_false(isPrivate);
else if (name != "test.observer.1" && name != "test.observer.2")
do_throw("genericObserver.onContentPrefSet: " +
"name not in (test.observer.1, test.observer.2)");
"name not in (test.observer.(1|2|normal|private))");
do_check_eq(value, "test value");
},
@ -283,9 +287,11 @@ function run_test() {
onContentPrefRemoved: function genericObserver_onContentPrefRemoved(group, name) {
++this.numTimesRemovedCalled;
do_check_eq(group, "www.example.com");
if (name != "test.observer.1" && name != "test.observer.2")
if (name != "test.observer.1" && name != "test.observer.2" &&
name != "test.observer.normal" && name != "test.observer.private") {
do_throw("genericObserver.onContentPrefSet: " +
"name not in (test.observer.1, test.observer.2)");
"name not in (test.observer.(1|2|normal|private))");
}
}
};
@ -304,6 +310,13 @@ function run_test() {
do_check_eq(specificObserver.numTimesRemovedCalled, 1);
do_check_eq(genericObserver.numTimesRemovedCalled, 2);
// // Make sure information about private context is properly
// // retrieved by the observer.
cps.setPref(uri, "test.observer.private", "test value", {usePrivateBrowsing: true});
cps.setPref(uri, "test.observer.normal", "test value", {usePrivateBrowsing: false});
cps.removePref(uri, "test.observer.private");
cps.removePref(uri, "test.observer.normal");
// Make sure we can remove observers and they don't get notified
// about changes anymore.
cps.removeObserver("test.observer.1", specificObserver);
@ -311,14 +324,14 @@ function run_test() {
cps.setPref(uri, "test.observer.1", "test value");
cps.removePref(uri, "test.observer.1", "test value");
do_check_eq(specificObserver.numTimesSetCalled, 1);
do_check_eq(genericObserver.numTimesSetCalled, 2);
do_check_eq(genericObserver.numTimesSetCalled, 4);
do_check_eq(specificObserver.numTimesRemovedCalled, 1);
do_check_eq(genericObserver.numTimesRemovedCalled, 2);
do_check_eq(genericObserver.numTimesRemovedCalled, 3);
//**************************************************************************//
// Get/Remove Prefs By Name
{
var anObserver = {
interfaces: [Ci.nsIContentPrefObserver, Ci.nsISupports],
@ -356,7 +369,7 @@ function run_test() {
cps.setPref(uri4, "test.byname.1", 16);
cps.setPref(null, "test.byname.1", 32);
cps.setPref(null, "test.byname.2", false);
function enumerateAndCheck(testName, expectedSum, expectedDomains) {
var prefsByName = cps.getPrefsByName(testName);
var enumerator = prefsByName.enumerator;
@ -374,7 +387,7 @@ function run_test() {
// check all domains have been removed from the array
do_check_eq(expectedDomains.length, 0);
}
enumerateAndCheck("test.byname.1", 53,
["foo.domain1.com", null, "www.domain1.com", "www.domain2.com"]);
enumerateAndCheck("test.byname.2", 2, ["www.domain1.com", null]);
@ -394,9 +407,9 @@ function run_test() {
do_check_eq(anObserver.numTimesRemovedCalled, 4);
do_check_eq(anObserver.expectedDomains.length, 0);
cps.removeObserver("test.byname.1", anObserver);
// Clean up after ourselves
cps.removePref(uri1, "test.byname.2");
cps.removePref(uri3, "test.byname.3");

View File

@ -11,14 +11,28 @@ var tests = [
function* observerForName_set() {
yield set("a.com", "foo", 1);
let args = yield on("Set", ["foo", null, "bar"]);
observerArgsOK(args.foo, [["a.com", "foo", 1]]);
observerArgsOK(args.null, [["a.com", "foo", 1]]);
observerArgsOK(args.foo, [["a.com", "foo", 1, false]]);
observerArgsOK(args.null, [["a.com", "foo", 1, false]]);
observerArgsOK(args.bar, []);
yield setGlobal("foo", 2);
args = yield on("Set", ["foo", null, "bar"]);
observerArgsOK(args.foo, [[null, "foo", 2]]);
observerArgsOK(args.null, [[null, "foo", 2]]);
observerArgsOK(args.foo, [[null, "foo", 2, false]]);
observerArgsOK(args.null, [[null, "foo", 2, false]]);
observerArgsOK(args.bar, []);
},
function* observerForName_set_private() {
yield set("a.com", "foo", 1, { usePrivateBrowsing: true });
let args = yield on("Set", ["foo", null, "bar"]);
observerArgsOK(args.foo, [["a.com", "foo", 1, true]]);
observerArgsOK(args.null, [["a.com", "foo", 1, true]]);
observerArgsOK(args.bar, []);
yield setGlobal("foo", 2, { usePrivateBrowsing: true });
args = yield on("Set", ["foo", null, "bar"]);
observerArgsOK(args.foo, [[null, "foo", 2, true]]);
observerArgsOK(args.null, [[null, "foo", 2, true]]);
observerArgsOK(args.bar, []);
},
@ -125,7 +139,7 @@ var tests = [
yield set("a.com", "foo", 1);
yield wait();
observerArgsOK(args.foo, []);
observerArgsOK(args.null, [["a.com", "foo", 1]]);
observerArgsOK(args.null, [["a.com", "foo", 1, false]]);
observerArgsOK(args.bar, []);
args.reset();