Localized Search in Firefox Search Box: Release v.03.2

From CDOT Wiki
Revision as of 22:37, 14 February 2008 by Sinker (talk | contribs)
Jump to: navigation, search

Main Project Page

Localized Search in Firefox Search Box

Release v.03.2

Similarly to release version .03.1, this release (v.03.2) dynamically removes the auto-detected search engine when the user navigates to a different web site unless the user manually adds the "Add <Search Engine>" menu item from the searchbar's list of available engines. This version also dynamically removes the dynamically "Added" search engine from the list of installed engines on the searchbar list and places it on the list of available engines as an "Add <Search Engine>" menu item when the user clicks the searchbar's button to display the engine list. 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_v032.txt

? localsearchpatch_v032.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.962
diff -u -8 -p -r1.962 browser.js
--- browser/base/content/browser.js	13 Feb 2008 10:40:03 -0000	1.962
+++ browser/base/content/browser.js	14 Feb 2008 20:59:15 -0000
@@ -97,16 +97,19 @@ var gContextMenu = null;
 
 var gChromeState = null; // chrome state before we went into print preview
 
 var gSanitizeListener = null;
 
 var gAutoHideTabbarPrefListener = null;
 var gBookmarkAllTabsHandler = null;
 
+// Global variable that holds a 'dynamically added' nsISearchEngine object.
+var gAddedEngine = null;
+
 #ifdef XP_MACOSX
 var gClickAndHoldTimer = null;
 #endif
 
 #ifndef XP_MACOSX
 var gEditUIVisible = true;
 #endif
 
@@ -2780,16 +2783,19 @@ 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.
+      gAddedEngine = searchService.addEngine(engine.href, Components.interfaces.nsISearchEngine.DATA_XML, iconURL, false);
 
     var engines = (hidden ? browser.hiddenEngines : browser.engines) || [];
 
     engines.push({ uri: engine.href,
                    title: engine.title,
                    icon: iconURL });
 
     if (hidden)
@@ -3849,18 +3855,28 @@ 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);
+    // Remove the dynamically added search engine if it is on the list.
+    try {
+	    if (gAddedEngine)
+        searchService.removeEngine(searchService.getEngineByName(gAddedEngine.name));
+        gAddedEngine = null;
+    } catch (e) {
+    }
+
     // 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	14 Feb 2008 20:59:16 -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.
    *
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	14 Feb 2008 20:59:17 -0000
@@ -2731,16 +2731,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])
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	14 Feb 2008 20:59:18 -0000
@@ -326,16 +326,26 @@
       </method>
 
       <!-- Rebuilds the dynamic portion of the popup menu (i.e., the menu items
            for new search engines that can be added to the available list).  This
            is called each time the popup is shown.
       -->
       <method name="rebuildPopupDynamic">
         <body><![CDATA[
+	  // Remove the dynamically added search engine if it is on the list when
+	  // the popup's menu items are first displayed. The user may then choose
+	  // to add the "Add <Search Engine>" item to the list of installed engines. 
+    	  try {
+	    if (gAddedEngine)
+              this.searchService.removeEngine(this.searchService.getEngineByName(gAddedEngine.name));
+              gAddedEngine = null;
+    	  } catch (e) {
+    	  }
+
           // We might not have added the main popup items yet, do that first
           // if needed.
           if (this._needToBuildPopup)
             this.rebuildPopup();
 
           var popup = this._popup;
           // Clear any addengine menuitems, including addengine-item entries and
           // the addengine-separator.  Work backward to avoid invalidating the


Code Modifications

1. search.xml

  • rebuildPopupDynamic() method: When the user first clicks the searchbar's button to display the engine list, remove the dynamically added search engine if it is on the list. Use a try-catch block to first check that the global variable gAddedEngine has a non-null value. Then, use the searchbar's (this) search service getEngineByName() function to "get" the engine if it is on the list. Employ the searchbar's search service removeEngine() function to remove it from the list. Set the global variable gAddedEngine to null.


How to Use the Patch

1. Copy and paste all of the text from the patch file into a text file and name the file localsearchpatch_v031.txt. Save the file to your root mozilla directory. Then, apply the patch and recompile your mozilla source code as described in the release v.01 notes.


2. Start-up Firefox 3 (i.e. Minefield) and go to a web site that offers a search engine plugin.


3. Notice that the search plugin has been dynamically loaded and added to the searchbar's list of installed engines. The newly added search engine appears as the current engine and it can be used to search the web site.


4. Click Firefox's searchbar button to display the list of search engines. Observe that the dynamically loaded search plugin is no longer on the list of installed engines and it is now listed as an "Add <Search Engine>" menu item. If you opt to "Add" the engine back to the list of installed engines, it will remain as an installed engine when you navigate to a different web site.


5. Open the Search Engine Manager's dialog and "Remove" the dynamically "Added" search engine. Notice that it is no longer on the list of installed engines and it is now listed as an "Add <Search Engine>" menu item. If you choose to "Add" the engine back to the list of installed engines, it will remain as an installed engine when you navigate to a different web site.


6. Navigate to a web site that offers a search engine plugin and then to a different site that does NOT offer a search plugin. Observe that the dynamically "Added" search engine is no longer on the list of installed engines.