Bug 1177443 - Add 'system' purpose for searches coming from outside Firefox. r=MattN, a=ritu
authorFlorian Quèze <florian@queze.net>
Wed, 12 Aug 2015 16:57:43 +0200
changeset 288743 700a1000830032fd8f324b68200d9cb786ddefb0
parent 288742 eb10f5d032197d1cbfef8bd33780d5227026f3a3
child 288744 224c22ef69a4867255372a50885215463491d569
push id5067
push userraliiev@mozilla.com
push dateMon, 21 Sep 2015 14:04:52 +0000
treeherdermozilla-beta@14221ffe5b2f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersMattN, ritu
bugs1177443
milestone42.0a2
Bug 1177443 - Add 'system' purpose for searches coming from outside Firefox. r=MattN, a=ritu
browser/components/nsBrowserContentHandler.js
browser/components/search/test/browser_yahoo.js
browser/locales/en-US/searchplugins/yahoo.xml
toolkit/components/search/nsSearchService.js
toolkit/components/search/tests/xpcshell/data/engine-system-purpose.xml
toolkit/components/search/tests/xpcshell/test_purpose.js
toolkit/components/search/tests/xpcshell/xpcshell.ini
--- a/browser/components/nsBrowserContentHandler.js
+++ b/browser/components/nsBrowserContentHandler.js
@@ -266,17 +266,17 @@ function getMostRecentWindow(aType) {
                      .getService(nsIWindowMediator);
   return wm.getMostRecentWindow(aType);
 }
 
 function doSearch(searchTerm, cmdLine) {
   var ss = Components.classes["@mozilla.org/browser/search-service;1"]
                      .getService(nsIBrowserSearchService);
 
-  var submission = ss.defaultEngine.getSubmission(searchTerm);
+  var submission = ss.defaultEngine.getSubmission(searchTerm, null, "system");
 
   // fill our nsISupportsArray with uri-as-wstring, null, null, postData
   var sa = Components.classes["@mozilla.org/supports-array;1"]
                      .createInstance(Components.interfaces.nsISupportsArray);
 
   var wuri = Components.classes["@mozilla.org/supports-string;1"]
                        .createInstance(Components.interfaces.nsISupportsString);
   wuri.data = submission.uri.spec;
@@ -784,17 +784,17 @@ nsDefaultCommandLineHandler.prototype = 
             var formParam = params.get("form");
             if (!formParam) {
               formParam = params.get("FORM");
             }
             if (formParam == "WNSGPH" || formParam == "WNSBOX" || formParam == "WNSFC2") {
               var term = params.get("q");
               var ss = Components.classes["@mozilla.org/browser/search-service;1"]
                                  .getService(nsIBrowserSearchService);
-              var submission = ss.defaultEngine.getSubmission(term, null, "searchbar");
+              var submission = ss.defaultEngine.getSubmission(term, null, "system");
               uri = submission.uri;
             }
           } catch (e) {
             Components.utils.reportError("Couldn't redirect Windows search: " + e);
           }
         }
 
         urilist.push(uri);
--- a/browser/components/search/test/browser_yahoo.js
+++ b/browser/components/search/test/browser_yahoo.js
@@ -98,16 +98,21 @@ function test() {
               value: "yhs-004",
               purpose: "newtab",
             },
             {
               name: "hsimp",
               value: "yhs-005",
               purpose: "contextmenu",
             },
+            {
+              name: "hsimp",
+              value: "yhs-007",
+              purpose: "system",
+            },
           ],
           mozparams: {},
         },
       ],
     },
   };
 
   isSubObjectOf(EXPECTED_ENGINE, engine, "Yahoo");
--- a/browser/locales/en-US/searchplugins/yahoo.xml
+++ b/browser/locales/en-US/searchplugins/yahoo.xml
@@ -20,10 +20,11 @@
   <Param name="p" value="{searchTerms}"/>
   <Param name="ei" value="UTF-8"/>
   <Param name="hspart" value="mozilla"/>
   <MozParam name="hsimp" condition="purpose" purpose="searchbar"   value="yhs-001"/>
   <MozParam name="hsimp" condition="purpose" purpose="keyword"     value="yhs-002"/>
   <MozParam name="hsimp" condition="purpose" purpose="homepage"    value="yhs-003"/>
   <MozParam name="hsimp" condition="purpose" purpose="newtab"      value="yhs-004"/>
   <MozParam name="hsimp" condition="purpose" purpose="contextmenu" value="yhs-005"/>
+  <MozParam name="hsimp" condition="purpose" purpose="system"      value="yhs-007"/>
 </Url>
 </SearchPlugin>
--- a/toolkit/components/search/nsSearchService.js
+++ b/toolkit/components/search/nsSearchService.js
@@ -1318,16 +1318,20 @@ EngineURL.prototype = {
   },
 
   getSubmission: function SRCH_EURL_getSubmission(aSearchTerms, aEngine, aPurpose) {
     var url = ParamSubstitution(this.template, aSearchTerms, aEngine);
     // Default to an empty string if the purpose is not provided so that default purpose params
     // (purpose="") work consistently rather than having to define "null" and "" purposes.
     var purpose = aPurpose || "";
 
+    // If the 'system' purpose isn't defined in the plugin, fallback to 'searchbar'.
+    if (purpose == "system" && !this.params.some(p => p.purpose == "system"))
+      purpose = "searchbar";
+
     // Create an application/x-www-form-urlencoded representation of our params
     // (name=value&name=value&name=value)
     var dataString = "";
     for (var i = 0; i < this.params.length; ++i) {
       var param = this.params[i];
 
       // If this parameter has a purpose, only add it if the purpose matches
       if (param.purpose !== undefined && param.purpose != purpose)
new file mode 100644
--- /dev/null
+++ b/toolkit/components/search/tests/xpcshell/data/engine-system-purpose.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<SearchPlugin xmlns="http://www.mozilla.org/2006/browser/search/">
+<ShortName>engine-system-purpose</ShortName>
+<Url type="text/html" method="GET" template="http://www.google.com/search">
+  <Param name="q" value="{searchTerms}"/>
+  <!-- Dynamic parameters -->
+  <MozParam name="channel" condition="purpose" purpose="searchbar" value="sb"/>
+  <MozParam name="channel" condition="purpose" purpose="system" value="sys"/>
+</Url>
+</SearchPlugin>
--- a/toolkit/components/search/tests/xpcshell/test_purpose.js
+++ b/toolkit/components/search/tests/xpcshell/test_purpose.js
@@ -49,10 +49,21 @@ add_task(function* test_purpose() {
   // Tests for a purpose on the search form (ie. empty query).
   [engine] = yield addTestEngines([
     { name: "engine-rel-searchform-purpose", xmlFileName: "engine-rel-searchform-purpose.xml" }
   ]);
   base = "http://www.google.com/search?q=";
   check_submission("&channel=sb", "", null,        "searchbar");
   check_submission("&channel=sb", "", "text/html", "searchbar");
 
+  // verify that the 'system' purpose falls back to the 'searchbar' purpose.
+  base = "http://www.google.com/search?q=foo";
+  check_submission("&channel=sb", "foo", "text/html", "system");
+  check_submission("&channel=sb", "foo", "text/html", "searchbar");
+  // Add an engine that actually defines the 'system' purpose...
+  [engine] = yield addTestEngines([
+    { name: "engine-system-purpose", xmlFileName: "engine-system-purpose.xml" }
+  ]);
+  // ... and check that the system purpose is used correctly.
+  check_submission("&channel=sys", "foo", "text/html", "system");
+
   do_test_finished();
 });
--- a/toolkit/components/search/tests/xpcshell/xpcshell.ini
+++ b/toolkit/components/search/tests/xpcshell/xpcshell.ini
@@ -11,16 +11,17 @@ support-files =
   data/engine-addon.xml
   data/engine-override.xml
   data/engine-app.xml
   data/engine-fr.xml
   data/engineMaker.sjs
   data/engine-rel-searchform.xml
   data/engine-rel-searchform-post.xml
   data/engine-rel-searchform-purpose.xml
+  data/engine-system-purpose.xml
   data/engineImages.xml
   data/ico-size-16x16-png.ico
   data/invalid-engine.xml
   data/install.rdf
   data/search-metadata.json
   data/search.json
   data/search.sqlite
   data/searchSuggestions.sjs