Localized Search in Firefox Search Box: Release v.03
From Open Source@Seneca
Contents |
Main Project Page
Localized Search in Firefox Search Box
Release v.03
This release (v.03) dynamically removes the auto-detected search engine when the user navigates to a different web site unless the user selects the "Add <Search Engine>" menu item from the searchbar's list of available engines to install the search engine. For example, the auto-detected search engine is dynamically added and appears as the current engine when the user navigates to a site with an OpenSearch plugin. The engine can be used immediately to search the site without the user having to manually "Add" it. When the user clicks the searchbar's button to display the engine list, the dynamically "Added" search engine is then put on the list of engines as an "Add <Search Engine>" menu item . At that point, the user can choose to manually "Add" the auto-detected search plugin and it will remain on the list of installed engines when the user navigates to another web site.
Patch File - localsearchpatch_v03.txt
? localsearchpatch_v03.txt
? nohup.out
? objdir-ff-debug
Index: browser/base/content/browser.js
===================================================================
RCS file: /cvsroot/mozilla/browser/base/content/browser.js,v
retrieving revision 1.966
diff -u -8 -p -r1.966 browser.js
--- browser/base/content/browser.js 17 Feb 2008 05:26:52 -0000 1.966
+++ browser/base/content/browser.js 21 Feb 2008 21:38:07 -0000
@@ -2788,16 +2788,24 @@ const BrowserSearch = {
// If this engine (identified by title) is already in the list, add it
// to the list of hidden engines rather than to the main list.
// XXX This will need to be changed when engines are identified by URL;
// see bug 335102.
var searchService = Cc["@mozilla.org/browser/search-service;1"].
getService(Ci.nsIBrowserSearchService);
if (searchService.getEngineByName(engine.title))
hidden = true;
+ else {
+ // Dynamically "Add" the web site's search engine plugin.
+ var addedEngine = searchService.addEngine(engine.href, Components.interfaces.nsISearchEngine.DATA_XML, iconURL, false);
+ if (addedEngine) {
+ searchService.dynamicEngine = addedEngine;
+ hidden = true;
+ }
+ }
var engines = (hidden ? browser.hiddenEngines : browser.engines) || [];
engines.push({ uri: engine.href,
title: engine.title,
icon: iconURL });
if (hidden)
@@ -3857,18 +3865,26 @@ nsBrowserStatusHandler.prototype =
this.onProgressChange(gBrowser.webProgress, 0, 0, aTotalProgress, 1);
},
startDocumentLoad : function(aRequest)
{
// clear out feed data
gBrowser.mCurrentBrowser.feeds = null;
+ var searchService = Cc["@mozilla.org/browser/search-service;1"].
+ getService(Ci.nsIBrowserSearchService);
+ var dynamicEngine = searchService.dynamicEngine;
+
+ // Remove the dynamically added search engine if it is on the list.
+ if (dynamicEngine)
+ searchService.removeEngine(dynamicEngine);
+
// clear out search-engine data
- gBrowser.mCurrentBrowser.engines = null;
+ gBrowser.mCurrentBrowser.engines = null;
const nsIChannel = Components.interfaces.nsIChannel;
var urlStr = aRequest.QueryInterface(nsIChannel).URI.spec;
var observerService = Components.classes["@mozilla.org/observer-service;1"]
.getService(Components.interfaces.nsIObserverService);
try {
observerService.notifyObservers(content, "StartDocumentLoad", urlStr);
} catch (e) {
Index: browser/components/search/nsIBrowserSearchService.idl
===================================================================
RCS file: /cvsroot/mozilla/browser/components/search/nsIBrowserSearchService.idl,v
retrieving revision 1.20
diff -u -8 -p -r1.20 nsIBrowserSearchService.idl
--- browser/components/search/nsIBrowserSearchService.idl 11 Sep 2007 16:07:11 -0000 1.20
+++ browser/components/search/nsIBrowserSearchService.idl 21 Feb 2008 21:38:08 -0000
@@ -187,19 +187,21 @@ interface nsIBrowserSearchService : nsIS
* @param confirm
* A boolean value indicating whether the user should be asked for
* confirmation before this engine is added to the list. If this
* value is false, the engine will be added to the list upon successful
* load, but it will not be selected as the current engine.
*
* @throws NS_ERROR_FAILURE if the type is invalid, or if the description
* file cannot be successfully loaded.
+ *
+ * @returns the created engine.
*/
- void addEngine(in AString engineURL, in long dataType, in AString iconURL,
- in boolean confirm);
+ nsISearchEngine addEngine(in AString engineURL, in long dataType, in AString iconURL,
+ in boolean confirm);
/**
* Adds a new search engine, without asking the user for confirmation and
* without starting to use it right away.
*
* @param name
* The search engine's name. Must be unique. Must not be null.
*
@@ -316,16 +318,21 @@ interface nsIBrowserSearchService : nsIS
readonly attribute nsISearchEngine defaultEngine;
/**
* The currently active search engine. May be null if there are no visible
* search engines installed.
*/
attribute nsISearchEngine currentEngine;
+ /** The dynamically added search engine. Set to null when
+ * the dynamically added engine is removed.
+ */
+ attribute nsISearchEngine dynamicEngine;
+
};
%{ C++
/**
* The observer topic to listen to for actions performed on installed
* search engines.
*/
#define SEARCH_ENGINE_TOPIC "browser-search-engine-modified"
Index: browser/components/search/nsSearchService.js
===================================================================
RCS file: /cvsroot/mozilla/browser/components/search/nsSearchService.js,v
retrieving revision 1.108
diff -u -8 -p -r1.108 nsSearchService.js
--- browser/components/search/nsSearchService.js 29 Jan 2008 19:39:20 -0000 1.108
+++ browser/components/search/nsSearchService.js 21 Feb 2008 21:38:09 -0000
@@ -2214,16 +2214,19 @@ function SearchService() {
}
SearchService.prototype = {
_engines: { },
_sortedEngines: null,
// Whether or not we need to write the order of engines on shutdown. This
// needs to happen anytime _sortedEngines is modified after initial startup.
_needToSetOrderPrefs: false,
+ // The dynamically added engine.
+ _dynamicEngine: null,
+
_init: function() {
var prefB = Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch);
var shouldLog = false;
try {
shouldLog = prefB.getBoolPref(BROWSER_SEARCH_PREF + "log");
} catch (ex) {}
@@ -2731,16 +2734,17 @@ SearchService.prototype = {
var engine = new Engine(uri, aDataType, false);
engine._initFromURI();
} catch (ex) {
LOG("addEngine: Error adding engine:\n" + ex);
throw Cr.NS_ERROR_FAILURE;
}
engine._setIcon(aIconURL, false);
engine._confirm = aConfirm;
+ return engine;
},
removeEngine: function SRCH_SVC_removeEngine(aEngine) {
ENSURE_ARG(aEngine, "no engine passed to removeEngine!");
var engineToRemove = null;
for (var e in this._engines)
if (aEngine.wrappedJSObject == this._engines[e])
@@ -2768,16 +2772,18 @@ SearchService.prototype = {
Cr.NS_ERROR_FAILURE);
this._sortedEngines.splice(index, 1);
// Remove the engine from the internal store
delete this._engines[engineToRemove.name];
notifyAction(engineToRemove, SEARCH_ENGINE_REMOVED);
+ this._dynamicEngine = null;
+
// Since we removed an engine, we need to update the preferences.
this._needToSetOrderPrefs = true;
}
},
moveEngine: function SRCH_SVC_moveEngine(aEngine, aNewIndex) {
ENSURE_ARG((aNewIndex < this._sortedEngines.length) && (aNewIndex >= 0),
"SRCH_SVC_moveEngine: Index out of bounds!");
@@ -2839,16 +2845,27 @@ SearchService.prototype = {
const defPref = BROWSER_SEARCH_PREF + "defaultenginename";
// Get the default engine - this pref should always exist, but the engine
// might be hidden
this._defaultEngine = this.getEngineByName(getLocalizedPref(defPref, ""));
if (!this._defaultEngine || this._defaultEngine.hidden)
this._defaultEngine = this._getSortedEngines(false)[0] || null;
return this._defaultEngine;
},
+
+ // Get the dynamically added engine.
+ get dynamicEngine() {
+ return this._dynamicEngine;
+ },
+ // Set the dynamically added engine.
+ set dynamicEngine(val) {
+ ENSURE_ARG(val instanceof Ci.nsISearchEngine,
+ "Invalid argument passed to dynamicEngine setter");
+ this._dynamicEngine = val;
+ },
get currentEngine() {
if (!this._currentEngine || this._currentEngine.hidden)
this._currentEngine = this.defaultEngine;
return this._currentEngine;
},
set currentEngine(val) {
ENSURE_ARG(val instanceof Ci.nsISearchEngine,
Index: browser/components/search/content/search.xml
===================================================================
RCS file: /cvsroot/mozilla/browser/components/search/content/search.xml,v
retrieving revision 1.120
diff -u -8 -p -r1.120 search.xml
--- browser/components/search/content/search.xml 6 Feb 2008 19:00:09 -0000 1.120
+++ browser/components/search/content/search.xml 21 Feb 2008 21:38:10 -0000
@@ -168,16 +168,24 @@
onset="this.searchService.currentEngine = val; return val;">
<getter><![CDATA[
var currentEngine = this.searchService.currentEngine;
// Return a dummy engine if there is no currentEngine
return currentEngine || {name:"", uri:null};
]]></getter>
</property>
+ <!-- Returns the dynamicEngine from the search service. -->
+ <property name="dynamicEngine" readonly="true">
+ <getter><![CDATA[
+ var dynamicEngine = this.searchService.dynamicEngine;
+ return dynamicEngine;
+ ]]></getter>
+ </property>
+
<!-- textbox is used by sanitize.js to clear the undo history when
clearing form information. -->
<property name="textbox" readonly="true"
onget="return this._textbox;"/>
<property name="searchService" readonly="true">
<getter><





