Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reset the search engine on the about:home page #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 2 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
35 changes: 32 additions & 3 deletions bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

var Cu = Components.utils;
var DEBUG = 0;
Copy link
Owner

Choose a reason for hiding this comment

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

I think we should avoid adding this - it's easy enough to re-add during development, don't need to have it checked in.

Copy link
Author

Choose a reason for hiding this comment

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

I just added some code for testing and left it in case you want to verify.

So leaving out the var DEBUG = 0; is OK and leaving the alerts in is
also OK for you?

I did some testing and locked the affected prefs to check its affect.

If I lock the browser.search.defaultenginename pref then
originalDefaultEngine (and defaultEngine) seems to be null or
undefined and the script aborts leaving the extension installed
(about:addons).
At least now the script finishes properly in the cases that I've
tested and it is only two extra lines.

lockPref("browser.search.defaultenginename", "Wikipedia (en)");

Locking prefs doesn't effect the reset operation (prefHasUserValue
returns false), but of course the reset to the unlocked default will
fail.
I know that this is a very rare case and might never expose, but I
consider code that work in as much cases as possible important.

"You can just re-use originalDefaultEngine from above."
I know . I left this because that make it easier to maintain
(compare) the code should any change be necessary and it makes that
code independent from the rest of the code in the extension.

If you still want to to make your proposed changes in addition to
removing DEBUG=0 then I will do that.

Here is some code for the web console or scratchpad that you can use
to test the reset of the about:home search engine.
https://support.mozilla.org//en-US/questions/933343

Should it be added to the README file that a reload of the about:home
page is required if that page is currently open?

Maybe also mention the prefix name (searchreset.backup.*) of the
backup prefs explicitly to make it easier to spot those backup prefs.

On 8/15/12, Gavin Sharp [email protected] wrote:

@@ -3,6 +3,7 @@

I think we should avoid adding this - it's easy enough to re-add during
development, don't need to have it checked in.


Reply to this email directly or view it on GitHub:
https://github.com/gavinsharp/SearchReset/pull/2/files#r1384719

Copy link
Author

Choose a reason for hiding this comment

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

I've edited the previous patch with some of the proposed changes and
some others.
Placed both under one if (originalDefaultEngine) {} as I still prefer
to keep it.


Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/AddonManager.jsm");
Expand All @@ -11,6 +12,10 @@ function startup(aData, aReason) {
// Helper function that backs up and then clears a pref, if it has a user-set
// value.
function resetPref(prefName) {
if (Services.prefs.prefIsLocked(prefName)){
Copy link
Owner

Choose a reason for hiding this comment

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

Did you see this occurring in practice? I don't quite recall how this would behave if the prefs are locked, but I don't think we need to handle that case, since that would only occur in heavily customized Firefox builds.

if (DEBUG) Services.prompt.alert(null, "RESET", "LOCKED: "+prefName);
}

if (Services.prefs.prefHasUserValue(prefName)) {
var existingValue = Services.prefs.getCharPref(prefName);
Services.prefs.setCharPref("searchreset.backup." + prefName, existingValue);
Expand All @@ -28,14 +33,38 @@ function startup(aData, aReason) {

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

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

if (originalDefaultEngine) {
Copy link
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);
} else if (DEBUG) Services.prompt.alert(null, "ERROR", "NO originalDefaultEngine");

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

// 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 defaultEngine = Services.search.originalDefaultEngine;
Copy link
Owner

Choose a reason for hiding this comment

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

You can just re-use originalDefaultEngine from above.


if (defaultEngine) {
let submission = defaultEngine.getSubmission("_searchTerms_");
let engine = {name: defaultEngine.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 defaultEngine");

// auto-uninstall after running
AddonManager.getAddonByID(aData.id, function(addon) {
addon.uninstall();
Expand Down