Bug 963132 Part 2 Implement add to searchbar (search-engine autodiscovery) r=Neil SEA_2_26_1_RELBRANCH
authorPhilip Chee <philip.chee@gmail.com>
Thu, 20 Feb 2014 01:40:59 +0800
branchSEA_2_26_1_RELBRANCH
changeset 31066 6ceb04d3717d5e6335b69ce9738fef037d0f1468
parent 31065 38065a20158cb05b99144ccff794247481863590
child 31067 2330f1f99c2af0517f0eb429a7b8f569df00d5ae
push id1
push userclokep@gmail.com
push dateMon, 07 May 2018 22:45:56 +0000
treeherdercomm-esr60@57eacde5ef40 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersNeil
bugs963132
Bug 963132 Part 2 Implement add to searchbar (search-engine autodiscovery) r=Neil
suite/browser/navigator.js
suite/common/search/search.xml
--- a/suite/browser/navigator.js
+++ b/suite/browser/navigator.js
@@ -548,16 +548,18 @@ function Startup()
     document.documentElement.setAttribute("height", defaultHeight);
     // Make sure we're safe at the left/top edge of screen
     document.documentElement.setAttribute("screenX", screen.availLeft);
     document.documentElement.setAttribute("screenY", screen.availTop);
   }
 
   // hook up UI through progress listener
   getBrowser().addProgressListener(window.XULBrowserWindow);
+  // setup the search service DOMLinkAdded listener
+  getBrowser().addEventListener("DOMLinkAdded", BrowserSearch, false);
   // hook up drag'n'drop
   getBrowser().droppedLinkHandler = handleDroppedLink;
 
   var uriToLoad = "";
 
   // Check window.arguments[0]. If not null then use it for uriArray
   // otherwise the new window is being called when another browser
   // window already exists so use the New Window pref for uriArray
@@ -743,16 +745,18 @@ function Shutdown()
 {
   AeroPeek.onCloseWindow(window);
 
   PlacesStarButton.uninit();
 
   // shut down browser access support
   window.browserDOMWindow = null;
 
+  getBrowser().removeEventListener("DOMLinkAdded", BrowserSearch, false);
+
   try {
     getBrowser().removeProgressListener(window.XULBrowserWindow);
   } catch (ex) {
     // Perhaps we didn't get around to adding the progress listener
   }
 
   window.XULBrowserWindow.destroy();
   window.XULBrowserWindow = null;
@@ -1039,16 +1043,28 @@ function BrowserReloadSkipCache()
 function BrowserHome(aEvent)
 {
   var homePage = getHomePage();
   var where = whereToOpenLink(aEvent, false, true);
   openUILinkArrayIn(homePage, where);
 }
 
 const BrowserSearch = {
+  handleEvent: function (event) { // "DOMLinkAdded" event
+    var link = event.originalTarget;
+
+    var isSearch = /(?:^|\s)search(?:\s|$)/i.test(link.rel) && link.title &&
+                   /^(https?|ftp):/i.test(link.href) &&
+                   /(?:^|\s)application\/opensearchdescription\+xml(?:;?.*)$/i.test(link.type);
+
+    if (isSearch) {
+      this.addEngine(link, link.ownerDocument);
+    }
+  },
+
   addEngine: function(engine, targetDoc) {
     if (!this.searchBar)
       return;
 
     var browser = getBrowser().getBrowserForDocument(targetDoc);
     // ignore search engines from subframes (see bug 479408)
     if (!browser)
       return;
--- a/suite/common/search/search.xml
+++ b/suite/common/search/search.xml
@@ -172,16 +172,22 @@
 
       <method name="onSuccess">
         <parameter name="aEngine"/>
         <body><![CDATA[
           this.currentEngine = aEngine;
         ]]></body>
       </method>
 
+      <method name="onError">
+        <parameter name="aErrorCode"/>
+        <body><![CDATA[
+        ]]></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":
@@ -229,19 +235,17 @@
       335102.  -->
 
       <!-- If the engine that was just removed from the searchbox list was
       autodetected on this page, move it to each browser's active list so it
       will be offered to be added again. -->
       <method name="offerNewEngine">
         <parameter name="aEngine"/>
         <body><![CDATA[
-          var allbrowsers = getBrowser().mPanelContainer.childNodes;
-          for (var tab = 0; tab < allbrowsers.length; tab++) {
-            var browser = getBrowser().getBrowserAtIndex(tab);
+          for (var browser of getBrowser().browsers) {
             if (browser.hiddenEngines) {
               // XXX This will need to be changed when engines are identified by
               // URL rather than title; see bug 335102.
               var removeTitle = aEngine.wrappedJSObject.name;
               for (var i = 0; i < browser.hiddenEngines.length; i++) {
                 if (browser.hiddenEngines[i].title == removeTitle) {
                   if (!browser.engines)
                     browser.engines = [];
@@ -256,19 +260,17 @@
       </method>
 
       <!-- If the engine that was just added to the searchbox list was
       autodetected on this page, move it to each browser's hidden list so it is
       no longer offered to be added. -->
       <method name="hideNewEngine">
         <parameter name="aEngine"/>
         <body><![CDATA[
-          var allbrowsers = getBrowser().mPanelContainer.childNodes;
-          for (var tab = 0; tab < allbrowsers.length; tab++) {
-            var browser = getBrowser().getBrowserAtIndex(tab);
+          for (var browser of getBrowser().browsers) {
             if (browser.engines) {
               // XXX This will need to be changed when engines are identified by
               // URL rather than title; see bug 335102.
               var removeTitle = aEngine.wrappedJSObject.name;
               for (var i = 0; i < browser.engines.length; i++) {
                 if (browser.engines[i].title == removeTitle) {
                   if (!browser.hiddenEngines)
                     browser.hiddenEngines = [];