Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 35 additions & 6 deletions bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,57 @@ Cu.import("resource://gre/modules/AddonManager.jsm");
function startup(aData, aReason) {
// Helper function that backs up and then clears a pref, if it has a user-set
// value.

function resetPref(prefName) {
let status = "";
if (Services.prefs.prefIsLocked(prefName) && DEBUG) status = "\n(LOCKED)";

if (Services.prefs.prefHasUserValue(prefName)) {
var existingValue = Services.prefs.getCharPref(prefName);
Services.prefs.setCharPref("searchreset.backup." + prefName, existingValue);

Services.prefs.clearUserPref(prefName);

if (DEBUG) status = "\n\nold: " + existingValue;
}

if (DEBUG) Services.prompt.alert(null, "RESET", prefName + status + "\ncur: " + Services.prefs.getCharPref(prefName));
}

// Reset the home page and keyword.URL
resetPref("browser.startup.homepage");
resetPref("browser.startup.homepage"); // complex value
resetPref("keyword.URL");

// Reset the New Tab Page
resetPref("browser.newtab.url");

// Now also reset the default search engine
resetPref("browser.search.defaultenginename");
resetPref("browser.search.defaultenginename"); // complex value

let originalDefaultEngine = Services.search.originalDefaultEngine;
originalDefaultEngine.hidden = false;
Services.search.currentEngine = originalDefaultEngine;
Services.search.moveEngine(originalDefaultEngine, 0);

if (originalDefaultEngine) {
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't need to add this check - after having reset the defaultenginename pref, originalDefaultEngine is garanteed to be present and represent the the build's default engine, unless its default value is corrupt due to e.g. an addon, but I don't think we should try to handle that case.

originalDefaultEngine.hidden = false;
Services.search.currentEngine = originalDefaultEngine;
Services.search.moveEngine(originalDefaultEngine, 0);

// reset the search engine used on the about:home page
// code reused from AboutHomeUtils
// http://mxr.mozilla.org/mozilla-central/source/browser/components/nsBrowserContentHandler.js#838
let submission = originalDefaultEngine.getSubmission("_searchTerms_");
let engine = {name: originalDefaultEngine.name, searchUrl: submission.uri.spec};
let aboutHomeURI = Services.io.newURI("moz-safe-about:home", null, null);

try {
let ssm = Components.classes["@mozilla.org/scriptsecuritymanager;1"].getService(Components.interfaces.nsIScriptSecurityManager);
let principal = (ssm.getCodebasePrincipal || ssm.getNoAppCodebasePrincipal)(aboutHomeURI);

let dsm = Components.classes["@mozilla.org/dom/storagemanager;1"].getService(Components.interfaces.nsIDOMStorageManager);

dsm.getLocalStorageForPrincipal(principal, "").setItem("search-engine", JSON.stringify(engine));
} catch(e) { if (DEBUG) Services.prompt.alert(null, "ERROR resetting about:home" ,e); }
} else if (DEBUG) Services.prompt.alert(null, "ERROR", "NO originalDefaultEngine");

// Flush changes to disk
Services.prefs.savePrefFile(null);

Expand All @@ -46,4 +74,5 @@ function shutdown(aData, aReason) { }

function install(aData, aReason) { }

function uninstall(aData, aReason) { }
function uninstall(aData, aReason) { }