-
Notifications
You must be signed in to change notification settings - Fork 11
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
var Cu = Components.utils; | ||
var DEBUG = 0; | ||
|
||
Cu.import("resource://gre/modules/Services.jsm"); | ||
Cu.import("resource://gre/modules/AddonManager.jsm"); | ||
|
@@ -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)){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
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.