Bug 850210 - Part 1. Update ForgetAboutSite.jsm to use the nsIContentPrefService2 API. r=adw

This commit is contained in:
Felipe Gomes 2013-03-29 23:08:46 -03:00
parent 155415dd90
commit 8eb577433f
5 changed files with 153 additions and 117 deletions

View File

@ -274,29 +274,30 @@ var tests = [
function test_forget_site() {
// click "Forget About This Site" button
gBrowser.contentDocument.getElementById("forget-site-button").doCommand();
waitForClearHistory(function() {
is(gSiteLabel.value, "", "site label cleared");
is(gSiteLabel.value, "", "site label cleared");
let allSitesItem = gBrowser.contentDocument.getElementById("all-sites-item");
is(gSitesList.selectedItem, allSitesItem,
"all sites item selected after forgetting selected site");
let allSitesItem = gBrowser.contentDocument.getElementById("all-sites-item");
is(gSitesList.selectedItem, allSitesItem,
"all sites item selected after forgetting selected site");
// check to make sure site is gone from sites list
let testSiteItem = getSiteItem(TEST_URI_2.host);
ok(!testSiteItem, "site removed from sites list");
// check to make sure site is gone from sites list
let testSiteItem = getSiteItem(TEST_URI_2.host);
ok(!testSiteItem, "site removed from sites list");
// check to make sure we forgot all permissions corresponding to site
for (let type in TEST_PERMS) {
if (type == "password") {
ok(Services.logins.getLoginSavingEnabled(TEST_URI_2.prePath),
"password saving should be enabled by default");
} else {
is(Services.perms.testPermissionFromPrincipal(TEST_PRINCIPAL_2, type), PERM_UNKNOWN,
type + " permission should not be set for test site 2");
// check to make sure we forgot all permissions corresponding to site
for (let type in TEST_PERMS) {
if (type == "password") {
ok(Services.logins.getLoginSavingEnabled(TEST_URI_2.prePath),
"password saving should be enabled by default");
} else {
is(Services.perms.testPermissionFromPrincipal(TEST_PRINCIPAL_2, type), PERM_UNKNOWN,
type + " permission should not be set for test site 2");
}
}
}
runNextTest();
runNextTest();
});
}
];

View File

@ -4,7 +4,18 @@
Components.utils.import("resource://gre/modules/ForgetAboutSite.jsm");
function waitForClearHistory(aCallback) {
let observer = {
observe: function(aSubject, aTopic, aData) {
Services.obs.removeObserver(this, "browser:purge-domain-data");
setTimeout(aCallback, 0);
}
};
Services.obs.addObserver(observer, "browser:purge-domain-data", false);
}
function test() {
waitForExplicitFinish();
// utility functions
function countClosedTabsByTitle(aClosedTabList, aTitle)
aClosedTabList.filter(function (aData) aData.title == aTitle).length;
@ -80,36 +91,38 @@ function test() {
// purge domain & check that we purged correctly for closed windows
ForgetAboutSite.removeDataFromDomain("mozilla.org");
waitForClearHistory(function() {
let closedWindowData = JSON.parse(ss.getClosedWindowData());
let closedWindowData = JSON.parse(ss.getClosedWindowData());
// First set of tests for _closedWindows[0] - tests basics
let win = closedWindowData[0];
is(win.tabs.length, 1, "1 tab was removed");
is(countOpenTabsByTitle(win.tabs, FORGET), 0,
"The correct tab was removed");
is(countOpenTabsByTitle(win.tabs, REMEMBER), 1,
"The correct tab was remembered");
is(win.selected, 1, "Selected tab has changed");
is(win.title, REMEMBER, "The window title was correctly updated");
// First set of tests for _closedWindows[0] - tests basics
let win = closedWindowData[0];
is(win.tabs.length, 1, "1 tab was removed");
is(countOpenTabsByTitle(win.tabs, FORGET), 0,
"The correct tab was removed");
is(countOpenTabsByTitle(win.tabs, REMEMBER), 1,
"The correct tab was remembered");
is(win.selected, 1, "Selected tab has changed");
is(win.title, REMEMBER, "The window title was correctly updated");
// Test more complicated case
win = closedWindowData[1];
is(win.tabs.length, 3, "2 tabs were removed");
is(countOpenTabsByTitle(win.tabs, FORGET), 0,
"The correct tabs were removed");
is(countOpenTabsByTitle(win.tabs, REMEMBER), 3,
"The correct tabs were remembered");
is(win.selected, 3, "Selected tab has changed");
is(win.title, REMEMBER, "The window title was correctly updated");
// Test more complicated case
win = closedWindowData[1];
is(win.tabs.length, 3, "2 tabs were removed");
is(countOpenTabsByTitle(win.tabs, FORGET), 0,
"The correct tabs were removed");
is(countOpenTabsByTitle(win.tabs, REMEMBER), 3,
"The correct tabs were remembered");
is(win.selected, 3, "Selected tab has changed");
is(win.title, REMEMBER, "The window title was correctly updated");
// Tests handling of _closedTabs
win = closedWindowData[2];
is(countClosedTabsByTitle(win._closedTabs, REMEMBER), 1,
"The correct number of tabs were removed, and the correct ones");
is(countClosedTabsByTitle(win._closedTabs, FORGET), 0,
"All tabs to be forgotten were indeed removed");
// Tests handling of _closedTabs
win = closedWindowData[2];
is(countClosedTabsByTitle(win._closedTabs, REMEMBER), 1,
"The correct number of tabs were removed, and the correct ones");
is(countClosedTabsByTitle(win._closedTabs, FORGET), 0,
"All tabs to be forgotten were indeed removed");
// restore pre-test state
ss.setBrowserState(oldState);
// restore pre-test state
ss.setBrowserState(oldState);
finish();
});
}

View File

@ -4,6 +4,16 @@
Components.utils.import("resource://gre/modules/ForgetAboutSite.jsm");
function waitForClearHistory(aCallback) {
let observer = {
observe: function(aSubject, aTopic, aData) {
Services.obs.removeObserver(this, "browser:purge-domain-data");
setTimeout(aCallback, 0);
}
};
Services.obs.addObserver(observer, "browser:purge-domain-data", false);
}
function test() {
/** Test for Bug 464199 **/
@ -59,18 +69,19 @@ function test() {
"Everything is set up.");
ForgetAboutSite.removeDataFromDomain("example.net");
waitForClearHistory(function() {
closedTabs = JSON.parse(ss.getClosedTabData(newWin));
is(closedTabs.length, remember_count,
"The correct amout of tabs was removed");
is(countByTitle(closedTabs, FORGET), 0,
"All tabs to be forgotten were indeed removed");
is(countByTitle(closedTabs, REMEMBER), remember_count,
"... and tabs to be remembered weren't.");
closedTabs = JSON.parse(ss.getClosedTabData(newWin));
is(closedTabs.length, remember_count,
"The correct amout of tabs was removed");
is(countByTitle(closedTabs, FORGET), 0,
"All tabs to be forgotten were indeed removed");
is(countByTitle(closedTabs, REMEMBER), remember_count,
"... and tabs to be remembered weren't.");
// clean up
newWin.close();
gPrefService.clearUserPref("browser.sessionstore.max_tabs_undo");
finish();
// clean up
newWin.close();
gPrefService.clearUserPref("browser.sessionstore.max_tabs_undo");
finish();
});
}, false);
}

View File

@ -176,39 +176,6 @@ this.ForgetAboutSite = {
}
}
// Content Preferences
let (cp = Cc["@mozilla.org/content-pref/service;1"].
getService(Ci.nsIContentPrefService)) {
let db = cp.DBConnection;
// First we need to get the list of "groups" which are really just domains
let names = [];
let stmt = db.createStatement(
"SELECT name " +
"FROM groups " +
"WHERE name LIKE ?1 ESCAPE '/'"
);
let pattern = stmt.escapeStringForLIKE(aDomain, "/");
stmt.bindByIndex(0, "%" + pattern);
try {
while (stmt.executeStep())
if (hasRootDomain(stmt.getString(0), aDomain))
names.push(stmt.getString(0));
}
finally {
stmt.finalize();
}
// Now, for each name we got back, remove all of its prefs.
for (let i = 0; i < names.length; i++) {
let uri = names[i];
let enumerator = cp.getPrefs(uri, null).enumerator;
while (enumerator.hasMoreElements()) {
let pref = enumerator.getNext().QueryInterface(Ci.nsIProperty);
cp.removePref(uri, pref.name, null);
}
}
}
// Offline Storages
let (qm = Cc["@mozilla.org/dom/quota/manager;1"].
getService(Ci.nsIQuotaManager)) {
@ -224,7 +191,17 @@ this.ForgetAboutSite = {
qm.clearStoragesForURI(httpsURI);
}
// Everybody else (including extensions)
Services.obs.notifyObservers(null, "browser:purge-domain-data", aDomain);
function onContentPrefsRemovalFinished() {
// Everybody else (including extensions)
Services.obs.notifyObservers(null, "browser:purge-domain-data", aDomain);
}
// Content Preferences
let cps2 = Cc["@mozilla.org/content-pref/service;1"].
getService(Ci.nsIContentPrefService2);
cps2.removeBySubdomain(aDomain, null, {
handleCompletion: function() onContentPrefsRemovalFinished(),
handleError: function() {}
});
}
};

View File

@ -352,27 +352,32 @@ function check_permission_exists(aURI, aExists)
*/
function add_preference(aURI)
{
check_preference_exists(aURI, false);
let deferred = Promise.defer();
let cp = Cc["@mozilla.org/content-pref/service;1"].
getService(Ci.nsIContentPrefService);
cp.setPref(aURI, PREFERENCE_NAME, "foo", null);
check_preference_exists(aURI, true);
getService(Ci.nsIContentPrefService2);
cp.set(aURI.spec, PREFERENCE_NAME, "foo", null, {
handleCompletion: function() deferred.resolve()
});
return deferred.promise;
}
/**
* Checks to see if a preference exists for the given URI.
* Checks to see if a content preference exists for the given URI.
*
* @param aURI
* The URI to check if a preference exists.
* @param aExists
* True if the permission should exist, false otherwise.
*/
function check_preference_exists(aURI, aExists)
function preference_exists(aURI)
{
let deferred = Promise.defer();
let cp = Cc["@mozilla.org/content-pref/service;1"].
getService(Ci.nsIContentPrefService);
let checker = aExists ? do_check_true : do_check_false;
checker(cp.hasPref(aURI, PREFERENCE_NAME, null));
getService(Ci.nsIContentPrefService2);
let exists = false;
cp.getByDomainAndName(aURI.spec, PREFERENCE_NAME, null, {
handleResult: function() exists = true,
handleCompletion: function() deferred.resolve(exists)
});
return deferred.promise;
}
////////////////////////////////////////////////////////////////////////////////
@ -561,42 +566,70 @@ function test_permission_manager_not_cleared_with_uri_contains_domain()
check_permission_exists(TEST_URI, false);
}
function waitForPurgeNotification() {
let deferred = Promise.defer();
let observer = {
observe: function(aSubject, aTopic, aData)
{
Services.obs.removeObserver(observer, "browser:purge-domain-data");
// test_storage_cleared needs this extra executeSoon because
// the DOMStorage clean-up is also listening to this same observer
// which is run synchronously.
Services.tm.mainThread.dispatch(function() {
deferred.resolve();
}, Components.interfaces.nsIThread.DISPATCH_NORMAL);
}
};
Services.obs.addObserver(observer, "browser:purge-domain-data", false);
return deferred.promise;
}
// Content Preferences
function test_content_preferences_cleared_with_direct_match()
{
const TEST_URI = uri("http://mozilla.org");
add_preference(TEST_URI);
do_check_false(yield preference_exists(TEST_URI));
yield add_preference(TEST_URI);
do_check_true(yield preference_exists(TEST_URI));
ForgetAboutSite.removeDataFromDomain("mozilla.org");
check_preference_exists(TEST_URI, false);
yield waitForPurgeNotification();
do_check_false(yield preference_exists(TEST_URI));
}
function test_content_preferences_cleared_with_subdomain()
{
const TEST_URI = uri("http://www.mozilla.org");
add_preference(TEST_URI);
do_check_false(yield preference_exists(TEST_URI));
yield add_preference(TEST_URI);
do_check_true(yield preference_exists(TEST_URI));
ForgetAboutSite.removeDataFromDomain("mozilla.org");
check_preference_exists(TEST_URI, false);
yield waitForPurgeNotification();
do_check_false(yield preference_exists(TEST_URI));
}
function test_content_preferecnes_not_cleared_with_uri_contains_domain()
function test_content_preferences_not_cleared_with_uri_contains_domain()
{
const TEST_URI = uri("http://ilovemozilla.org");
add_preference(TEST_URI);
do_check_false(yield preference_exists(TEST_URI));
yield add_preference(TEST_URI);
do_check_true(yield preference_exists(TEST_URI));
ForgetAboutSite.removeDataFromDomain("mozilla.org");
check_preference_exists(TEST_URI, true);
yield waitForPurgeNotification();
do_check_true(yield preference_exists(TEST_URI));
// Reset state
let cp = Cc["@mozilla.org/content-pref/service;1"].
getService(Ci.nsIContentPrefService);
cp.removePref(TEST_URI, PREFERENCE_NAME, null);
check_preference_exists(TEST_URI, false);
ForgetAboutSite.removeDataFromDomain("ilovemozilla.org");
yield waitForPurgeNotification();
do_check_false(yield preference_exists(TEST_URI));
}
// Cache
function test_cache_cleared()
{
// Because this test is asynchronous, it should be the last test
do_check_eq(tests[tests.length - 1], arguments.callee);
do_check_true(tests[tests.length - 1] == arguments.callee);
// NOTE: We could be more extensive with this test and actually add an entry
// to the cache, and then make sure it is gone. However, we trust that
@ -646,6 +679,7 @@ function test_storage_cleared()
}
ForgetAboutSite.removeDataFromDomain("mozilla.org");
yield waitForPurgeNotification();
do_check_eq(s[0].getItem("test"), null);
do_check_eq(s[0].length, 0);
@ -688,7 +722,7 @@ let tests = [
// Content Preferences
test_content_preferences_cleared_with_direct_match,
test_content_preferences_cleared_with_subdomain,
test_content_preferecnes_not_cleared_with_uri_contains_domain,
test_content_preferences_not_cleared_with_uri_contains_domain,
// Storage
test_storage_cleared,