From 6d805dd6c8c09f22d20aae7cbb63eacd1c11b0c4 Mon Sep 17 00:00:00 2001 From: Mike de Boer Date: Fri, 30 Aug 2013 18:20:22 +0200 Subject: [PATCH] Bug 900865: Make about:home call nsISearchEngine.getSubmission for all searches. r=gavin --- browser/base/content/abouthome/aboutHome.js | 55 ++++--------------- browser/base/content/content.js | 14 ++--- .../base/content/test/browser_aboutHome.js | 7 ++- browser/modules/AboutHome.jsm | 30 ++++------ .../base/public/nsIBrowserSearchService.idl | 8 +-- toolkit/components/search/nsSearchService.js | 15 ++--- 6 files changed, 36 insertions(+), 93 deletions(-) diff --git a/browser/base/content/abouthome/aboutHome.js b/browser/base/content/abouthome/aboutHome.js index fa0691d16ba..738fe27ab04 100644 --- a/browser/base/content/abouthome/aboutHome.js +++ b/browser/base/content/abouthome/aboutHome.js @@ -153,7 +153,7 @@ const SNIPPETS_OBJECTSTORE_NAME = "snippets"; let gInitialized = false; let gObserver = new MutationObserver(function (mutations) { for (let mutation of mutations) { - if (mutation.attributeName == "searchEngineURL") { + if (mutation.attributeName == "searchEngineName") { setupSearchEngine(); if (!gInitialized) { ensureSnippetsMapThen(loadSnippets); @@ -295,52 +295,17 @@ function ensureSnippetsMapThen(aCallback) function onSearchSubmit(aEvent) { let searchTerms = document.getElementById("searchText").value; - let searchURL = document.documentElement.getAttribute("searchEngineURL"); + let engineName = document.documentElement.getAttribute("searchEngineName"); - if (searchURL && searchTerms.length > 0) { - // Send an event that a search was performed. This was originally - // added so Firefox Health Report could record that a search from - // about:home had occurred. - let engineName = document.documentElement.getAttribute("searchEngineName"); - let event = new CustomEvent("AboutHomeSearchEvent", {detail: engineName}); + if (engineName && searchTerms.length > 0) { + // Send an event that will perform a search and Firefox Health Report will + // record that a search from about:home has occurred. + let eventData = JSON.stringify({ + engineName: engineName, + searchTerms: searchTerms + }); + let event = new CustomEvent("AboutHomeSearchEvent", {detail: eventData}); document.dispatchEvent(event); - - const SEARCH_TOKEN = "_searchTerms_"; - let searchPostData = document.documentElement.getAttribute("searchEnginePostData"); - if (searchPostData) { - // Check if a post form already exists. If so, remove it. - const POST_FORM_NAME = "searchFormPost"; - let form = document.forms[POST_FORM_NAME]; - if (form) { - form.parentNode.removeChild(form); - } - - // Create a new post form. - form = document.body.appendChild(document.createElement("form")); - form.setAttribute("name", POST_FORM_NAME); - // Set the URL to submit the form to. - form.setAttribute("action", searchURL.replace(SEARCH_TOKEN, searchTerms)); - form.setAttribute("method", "post"); - - // Create new elements for search param. - searchPostData = searchPostData.split("&"); - for (let postVar of searchPostData) { - let [name, value] = postVar.split("="); - if (value == SEARCH_TOKEN) { - value = searchTerms; - } - let input = document.createElement("input"); - input.setAttribute("type", "hidden"); - input.setAttribute("name", name); - input.setAttribute("value", value); - form.appendChild(input); - } - // Submit the form. - form.submit(); - } else { - searchURL = searchURL.replace(SEARCH_TOKEN, encodeURIComponent(searchTerms)); - window.location.href = searchURL; - } } aEvent.preventDefault(); diff --git a/browser/base/content/content.js b/browser/base/content/content.js index 824b29a2ac4..e734f4b1867 100644 --- a/browser/base/content/content.js +++ b/browser/base/content/content.js @@ -90,19 +90,13 @@ let AboutHomeListener = { // Inject search engine and snippets URL. let docElt = doc.documentElement; - // set the following attributes BEFORE searchEngineURL, which triggers to + // set the following attributes BEFORE searchEngineName, which triggers to // show the snippets when it's set. docElt.setAttribute("snippetsURL", aData.snippetsURL); if (aData.showKnowYourRights) docElt.setAttribute("showKnowYourRights", "true"); docElt.setAttribute("snippetsVersion", aData.snippetsVersion); - - let engine = aData.defaultSearchEngine; - docElt.setAttribute("searchEngineName", engine.name); - docElt.setAttribute("searchEnginePostData", engine.postDataString || ""); - // Again, keep the searchEngineURL as the last attribute, because the - // mutation observer in aboutHome.js is counting on that. - docElt.setAttribute("searchEngineURL", engine.searchURL); + docElt.setAttribute("searchEngineName", Services.search.defaultEngine.name); }, onPageLoad: function() { @@ -135,7 +129,7 @@ let AboutHomeListener = { sendAsyncMessage("AboutHome:RequestUpdate"); doc.addEventListener("AboutHomeSearchEvent", function onSearch(e) { - sendAsyncMessage("AboutHome:Search", { engineName: e.detail }); + sendAsyncMessage("AboutHome:Search", { searchData: e.detail }); }, true, true); }, @@ -280,4 +274,4 @@ let ClickEventHandler = { return [href ? makeURLAbsolute(baseURI, href) : null, null]; } }; -ClickEventHandler.init(); \ No newline at end of file +ClickEventHandler.init(); diff --git a/browser/base/content/test/browser_aboutHome.js b/browser/base/content/test/browser_aboutHome.js index 7afe2bae532..430b137b8ce 100644 --- a/browser/base/content/test/browser_aboutHome.js +++ b/browser/base/content/test/browser_aboutHome.js @@ -107,7 +107,8 @@ let gTests = [ let engineName = doc.documentElement.getAttribute("searchEngineName"); doc.addEventListener("AboutHomeSearchEvent", function onSearch(e) { - is(e.detail, engineName, "Detail is search engine name"); + let data = JSON.parse(e.detail); + is(data.engineName, engineName, "Detail is search engine name"); // We use executeSoon() to ensure that this code runs after the // count has been updated in browser.js, since it uses the same @@ -287,7 +288,7 @@ let gTests = [ // propagated to the about:home content, we want to perform a search. let mutationObserver = new MutationObserver(function (mutations) { for (let mutation of mutations) { - if (mutation.attributeName == "searchEngineURL") { + if (mutation.attributeName == "searchEngineName") { searchText.value = needle; searchText.focus(); EventUtils.synthesizeKey("VK_RETURN", {}); @@ -445,7 +446,7 @@ function promiseBrowserAttributes(aTab) } // Now we just have to wait for the last attribute. - if (mutation.attributeName == "searchEngineURL") { + if (mutation.attributeName == "searchEngineName") { info("Remove attributes observer"); observer.disconnect(); // Must be sure to continue after the page mutation observer. diff --git a/browser/modules/AboutHome.jsm b/browser/modules/AboutHome.jsm index bad0ad6f671..bdf61bf90bb 100644 --- a/browser/modules/AboutHome.jsm +++ b/browser/modules/AboutHome.jsm @@ -25,21 +25,6 @@ const STARTPAGE_VERSION = 4; this.AboutHomeUtils = { get snippetsVersion() STARTPAGE_VERSION, - /** - * Returns an object containing the name and searchURL of the original default - * search engine. - */ - get defaultSearchEngine() { - let defaultEngine = Services.search.defaultEngine; - let submission = defaultEngine.getSubmission("_searchTerms_", null, "homepage"); - - return Object.freeze({ - name: defaultEngine.name, - searchURL: submission.uri.spec, - postDataString: submission.postDataString - }); - }, - /* * showKnowYourRights - Determines if the user should be shown the * about:rights notification. The notification should *not* be shown if @@ -173,9 +158,19 @@ let AboutHome = { break; case "AboutHome:Search": + let data; + try { + data = JSON.parse(aMessage.data.searchData); + } catch(ex) { + Cu.reportError(ex); + break; + } #ifdef MOZ_SERVICES_HEALTHREPORT - window.BrowserSearch.recordSearchInHealthReport(aMessage.data.engineName, "abouthome"); + window.BrowserSearch.recordSearchInHealthReport(data.engineName, "abouthome"); #endif + // Trigger a search through nsISearchEngine.getSubmission() + let submission = Services.search.currentEngine.getSubmission(data.searchTerms); + window.loadURI(submission.uri.spec, null, submission.postData); break; } }, @@ -189,8 +184,7 @@ let AboutHome = { showRestoreLastSession: ss.canRestoreLastSession, snippetsURL: AboutHomeUtils.snippetsURL, showKnowYourRights: AboutHomeUtils.showKnowYourRights, - snippetsVersion: AboutHomeUtils.snippetsVersion, - defaultSearchEngine: AboutHomeUtils.defaultSearchEngine + snippetsVersion: AboutHomeUtils.snippetsVersion }; if (AboutHomeUtils.showKnowYourRights) { diff --git a/netwerk/base/public/nsIBrowserSearchService.idl b/netwerk/base/public/nsIBrowserSearchService.idl index d8182fd81f8..c15e1298bfd 100644 --- a/netwerk/base/public/nsIBrowserSearchService.idl +++ b/netwerk/base/public/nsIBrowserSearchService.idl @@ -7,7 +7,7 @@ interface nsIURI; interface nsIInputStream; -[scriptable, uuid(82ec6ee8-68b5-49ef-87b7-0d5240f8a183)] +[scriptable, uuid(5799251f-5b55-4df7-a9e7-0c27812c469a)] interface nsISearchSubmission : nsISupports { /** @@ -16,12 +16,6 @@ interface nsISearchSubmission : nsISupports */ readonly attribute nsIInputStream postData; - /** - * The POST data associated with a search submission as an - * application/x-www-form-urlencoded string. May be null. - */ - readonly attribute AString postDataString; - /** * The URI to submit a search to. */ diff --git a/toolkit/components/search/nsSearchService.js b/toolkit/components/search/nsSearchService.js index 4bbf533eb31..15f96eb0970 100644 --- a/toolkit/components/search/nsSearchService.js +++ b/toolkit/components/search/nsSearchService.js @@ -931,7 +931,6 @@ EngineURL.prototype = { } var postData = null; - let postDataString = null; if (this.method == "GET") { // GET method requests have no post data, and append the encoded // query string to the url... @@ -939,8 +938,8 @@ EngineURL.prototype = { url += "?"; url += dataString; } else if (this.method == "POST") { - // For POST requests, specify the data as a MIME stream as well as a string. - postDataString = dataString; + // POST method requests must wrap the encoded text in a MIME + // stream and supply that as POSTDATA. var stringStream = Cc["@mozilla.org/io/string-input-stream;1"]. createInstance(Ci.nsIStringInputStream); stringStream.data = dataString; @@ -952,7 +951,7 @@ EngineURL.prototype = { postData.setData(stringStream); } - return new Submission(makeURI(url), postData, postDataString); + return new Submission(makeURI(url), postData); }, _hasRelation: function SRC_EURL__hasRelation(aRel) @@ -2544,7 +2543,7 @@ Engine.prototype = { if (!aData) { // Return a dummy submission object with our searchForm attribute - return new Submission(makeURI(this.searchForm)); + return new Submission(makeURI(this.searchForm), null); } LOG("getSubmission: In data: \"" + aData + "\"; Purpose: \"" + aPurpose + "\""); @@ -2581,10 +2580,9 @@ Engine.prototype = { }; // nsISearchSubmission -function Submission(aURI, aPostData = null, aPostDataString = null) { +function Submission(aURI, aPostData = null) { this._uri = aURI; this._postData = aPostData; - this._postDataString = aPostDataString; } Submission.prototype = { get uri() { @@ -2593,9 +2591,6 @@ Submission.prototype = { get postData() { return this._postData; }, - get postDataString() { - return this._postDataString; - }, QueryInterface: function SRCH_SUBM_QI(aIID) { if (aIID.equals(Ci.nsISearchSubmission) || aIID.equals(Ci.nsISupports))