Bug 780179 SeaMonkey should use the asynchronous API from nsIBrowserSearchService. r=Neil
authorPhilip Chee <philip.chee@gmail.com>
Tue, 07 Aug 2012 17:53:30 +0800
changeset 13206 c7dea0a0a0835c67ced8c5e9f311bb4300be7973
parent 13205 2d9e21833e4791fdb9d0de4bbfb0e6defee963a0
child 13207 493c0d246a5ead3bbfda7fee2827223db343e99f
push idunknown
push userunknown
push dateunknown
reviewersNeil
bugs780179
Bug 780179 SeaMonkey should use the asynchronous API from nsIBrowserSearchService. r=Neil
suite/common/search/search-panel.js
suite/common/search/search.xml
suite/common/src/nsSuiteGlue.js
--- a/suite/common/search/search-panel.js
+++ b/suite/common/search/search-panel.js
@@ -7,32 +7,35 @@ Components.utils.import("resource://gre/
 const kXULNS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
 const SEARCH_ENGINE_TOPIC = "browser-search-engine-modified";
 var menulist, textbox;
 
 function Startup() {
   menulist = document.getElementById("sidebar-search-engines");
   textbox = document.getElementById("sidebar-search-text");
 
+  LoadEngineList();
+  Services.obs.addObserver(engineObserver, SEARCH_ENGINE_TOPIC, false);
+}
+
+function LoadEngineList() {
   // Make sure the popup is empty.
   menulist.removeAllItems();
 
   var engines = Services.search.getVisibleEngines();
   for (let i = 0; i < engines.length; i++) {
     let name = engines[i].name;
     let menuitem = menulist.appendItem(name, name);
     menuitem.setAttribute("class", "menuitem-iconic");
     if (engines[i].iconURI)
       menuitem.setAttribute("image", engines[i].iconURI.spec);
     menulist.menupopup.appendChild(menuitem);
     menuitem.engine = engines[i];
   }
   menulist.value = Services.search.currentEngine.name;
-
-  Services.obs.addObserver(engineObserver, SEARCH_ENGINE_TOPIC, false);
 }
 
 function Shutdown() {
   Services.obs.removeObserver(engineObserver, SEARCH_ENGINE_TOPIC, false);
 }
 
 function SelectEngine() {
   if (menulist.selectedItem)
--- a/suite/common/search/search.xml
+++ b/suite/common/search/search.xml
@@ -58,49 +58,37 @@
           <xul:image class="search-go-button"
                      anonid="search-go-button"
                      onclick="handleSearchCommand(event);"
                      tooltiptext="&searchEndCap.label;"/>
         </xul:hbox>
       </xul:textbox>
     </content>
 
-    <implementation implements="nsIObserver">
+    <implementation implements="nsIObserver, nsIBrowserSearchInitObserver">
       <constructor><![CDATA[
         if (this.parentNode.parentNode.localName == "toolbarpaletteitem")
           return;
-        setTimeout(function (a) { a.init(); }, 0, this);
+
+        this.searchService.init(this);
+        var os = Components.classes["@mozilla.org/observer-service;1"]
+                           .getService(Components.interfaces.nsIObserverService);
+        os.addObserver(this, "browser-search-engine-modified", false);
+        this._addedObserver = true;
       ]]></constructor>
 
-      <method name="init">
-        <body><![CDATA[
-          // Make sure we rebuild the popup in onpopupshowing
-          this._needToBuildPopup = true;
-
-          // Refresh the display (updating icon, etc)
-          this.updateDisplay();
-
-          var os =
-               Components.classes["@mozilla.org/observer-service;1"]
-                         .getService(Components.interfaces.nsIObserverService);
-          os.addObserver(this, "browser-search-engine-modified", false);
-
-          this._addedObserver = true;
-        ]]></body>
-      </method>
-
       <destructor><![CDATA[
         if (this._addedObserver) {
           var os = Components.classes["@mozilla.org/observer-service;1"]
                              .getService(Components.interfaces.nsIObserverService);
           os.removeObserver(this, "browser-search-engine-modified");
           this._addedObserver = false;
         }
 
-        // Make sure to break the cycle from _texbox to us. Otherwise we leak
+        // Make sure to break the cycle from _textbox to us. Otherwise we leak
         // the world. But make sure it's actually pointing to us.
         if (this._textbox.mController.input == this)
           this._textbox.mController.input = null;
       ]]></destructor>
 
       <field name="_stringBundle">document.getAnonymousElementByAttribute(this,
           "anonid", "searchbar-stringbundle");</field>
       <field name="_textbox">document.getAnonymousElementByAttribute(this,
@@ -110,16 +98,17 @@
       <field name="searchButton">document.getAnonymousElementByAttribute(this,
           "anonid", "searchbar-engine-button");</field>
       <field name="_searchService">
         Components.classes["@mozilla.org/browser/search-service;1"]
                   .getService(Components.interfaces.nsIBrowserSearchService);
       </field>
       <field name="_addedObserver">false</field>
       <field name="_engines">null</field>
+      <field name="_needToBuildPopup">true</field>
 
       <property name="engines" readonly="true">
         <getter><![CDATA[
           if (!this._engines)
             this._engines = this.searchService.getVisibleEngines();
           return this._engines;
         ]]></getter>
       </property>
@@ -151,16 +140,28 @@
       </method>
 
       <method name="select">
         <body><![CDATA[
           this._textbox.select();
         ]]></body>
       </method>
 
+      <method name="onInitComplete">
+        <parameter name="aStatus"/>
+        <body><![CDATA[
+          if (Components.isSuccessCode(aStatus)) {
+            // Refresh the display (updating icon, etc)
+            this.updateDisplay();
+          } else {
+            Components.utils.reportError("Cannot initialize search service, bailing out: " + aStatus);
+          }
+        ]]></body>
+      </method>
+
       <method name="observe">
         <parameter name="aEngine"/>
         <parameter name="aTopic"/>
         <parameter name="aVerb"/>
         <body><![CDATA[
           if (aTopic == "browser-search-engine-modified") {
             switch (aVerb) {
             case "engine-removed":
@@ -452,23 +453,20 @@
         ]]></body>
       </method>
     </implementation>
 
     <handlers>
       <handler event="command"><![CDATA[
         const target = event.originalTarget;
         if (target.classList.contains("addengine-item")) {
-          var searchService =
-            Components.classes["@mozilla.org/browser/search-service;1"]
-                      .getService(Components.interfaces.nsIBrowserSearchService);
           // We only detect OpenSearch files
           var type = Components.interfaces.nsISearchEngine.DATA_XML;
-          searchService.addEngine(target.getAttribute("uri"), type,
-                                  target.getAttribute("image"), false);
+          this.searchService.addEngine(target.getAttribute("uri"), type,
+                                       target.getAttribute("image"), false);
         }
         else if (target.engine)
           this.currentEngine = target.engine;
         else
           return;
 
         this.focus();
         this.select();
--- a/suite/common/src/nsSuiteGlue.js
+++ b/suite/common/src/nsSuiteGlue.js
@@ -104,16 +104,17 @@ SuiteGlue.prototype = {
         break;
       case "profile-after-change":
         this._onProfileAfterChange();
         break;
       case "final-ui-startup":
         this._onProfileStartup();
         this._promptForMasterPassword();
         this._checkForNewAddons();
+        Services.search.init();
         break;
       case "sessionstore-windows-restored":
         this._onBrowserStartup(subject);
         break;
       case "browser:purge-session-history":
         // reset the console service's error buffer
         Services.console.logStringMessage(null); // clear the console (in case it's open)
         Services.console.reset();