diff options
author | Matt A. Tobin <email@mattatobin.com> | 2019-12-16 13:57:01 -0500 |
---|---|---|
committer | Matt A. Tobin <email@mattatobin.com> | 2019-12-16 13:57:01 -0500 |
commit | 06494f307850c576868831bd28a61464eab1f359 (patch) | |
tree | f281f5c46c3e0b73c7eabe22f02622dc013b0c35 /application/basilisk/components/search | |
parent | e7d4713e0765c79feddf2384d343d10595fa5cb3 (diff) | |
download | UXP-06494f307850c576868831bd28a61464eab1f359.tar UXP-06494f307850c576868831bd28a61464eab1f359.tar.gz UXP-06494f307850c576868831bd28a61464eab1f359.tar.lz UXP-06494f307850c576868831bd28a61464eab1f359.tar.xz UXP-06494f307850c576868831bd28a61464eab1f359.zip |
Remove Basilisk from the Unified XUL Platform repository
Development will proceed at https://github.com/MoonchildProductions/Basilisk
Diffstat (limited to 'application/basilisk/components/search')
13 files changed, 0 insertions, 7338 deletions
diff --git a/application/basilisk/components/search/content/search.xml b/application/basilisk/components/search/content/search.xml deleted file mode 100644 index 41a5256d5..000000000 --- a/application/basilisk/components/search/content/search.xml +++ /dev/null @@ -1,2089 +0,0 @@ -<?xml version="1.0"?> -<!-- This Source Code Form is subject to the terms of the Mozilla Public - - License, v. 2.0. If a copy of the MPL was not distributed with this - - file, You can obtain one at http://mozilla.org/MPL/2.0/. --> - -<!DOCTYPE bindings [ -<!ENTITY % searchBarDTD SYSTEM "chrome://browser/locale/searchbar.dtd" > -%searchBarDTD; -<!ENTITY % browserDTD SYSTEM "chrome://browser/locale/browser.dtd"> -%browserDTD; -]> - -<bindings id="SearchBindings" - xmlns="http://www.mozilla.org/xbl" - xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" - xmlns:xbl="http://www.mozilla.org/xbl"> - - <binding id="searchbar"> - <resources> - <stylesheet src="chrome://browser/content/search/searchbarBindings.css"/> - <stylesheet src="chrome://browser/skin/searchbar.css"/> - </resources> - <content> - <xul:stringbundle src="chrome://browser/locale/search.properties" - anonid="searchbar-stringbundle"/> - <!-- - There is a dependency between "maxrows" attribute and - "SuggestAutoComplete._historyLimit" (nsSearchSuggestions.js). Changing - one of them requires changing the other one. - --> - <xul:textbox class="searchbar-textbox" - anonid="searchbar-textbox" - type="autocomplete" - inputtype="search" - flex="1" - autocompletepopup="PopupSearchAutoComplete" - autocompletesearch="search-autocomplete" - autocompletesearchparam="searchbar-history" - maxrows="10" - completeselectedindex="true" - minresultsforpopup="0" - xbl:inherits="disabled,disableautocomplete,searchengine,src,newlines"> - <!-- - Empty <box> to properly position the icon within the autocomplete - binding's anonymous children (the autocomplete binding positions <box> - children differently) - --> - <xul:box> - <xul:hbox class="searchbar-search-button-container"> - <xul:image class="searchbar-search-button" - anonid="searchbar-search-button" - xbl:inherits="addengines" - tooltiptext="&searchEndCap.label;"/> - </xul:hbox> - </xul:box> - <xul:hbox class="search-go-container"> - <xul:image class="search-go-button" hidden="true" - anonid="search-go-button" - onclick="handleSearchCommand(event);" - tooltiptext="&searchEndCap.label;"/> - </xul:hbox> - </xul:textbox> - </content> - - <implementation implements="nsIObserver"> - <constructor><![CDATA[ - if (this.parentNode.parentNode.localName == "toolbarpaletteitem") - return; - // Make sure we rebuild the popup in onpopupshowing - this._needToBuildPopup = true; - - Services.obs.addObserver(this, "browser-search-engine-modified", false); - - this._initialized = true; - - Services.search.init((function search_init_cb(aStatus) { - // Bail out if the binding's been destroyed - if (!this._initialized) - return; - - if (Components.isSuccessCode(aStatus)) { - // Refresh the display (updating icon, etc) - this.updateDisplay(); - BrowserSearch.updateOpenSearchBadge(); - } else { - Components.utils.reportError("Cannot initialize search service, bailing out: " + aStatus); - } - }).bind(this)); - ]]></constructor> - - <destructor><![CDATA[ - this.destroy(); - ]]></destructor> - - <method name="destroy"> - <body><![CDATA[ - if (this._initialized) { - this._initialized = false; - - Services.obs.removeObserver(this, "browser-search-engine-modified"); - } - - // Make sure to break the cycle from _textbox to us. Otherwise we leak - // the world. But make sure it's actually pointing to us. - // Also make sure the textbox has ever been constructed, otherwise the - // _textbox getter will cause the textbox constructor to run, add an - // observer, and leak the world too. - if (this._textboxInitialized && this._textbox.mController.input == this) - this._textbox.mController.input = null; - ]]></body> - </method> - - <field name="_ignoreFocus">false</field> - <field name="_clickClosedPopup">false</field> - <field name="_stringBundle">document.getAnonymousElementByAttribute(this, - "anonid", "searchbar-stringbundle");</field> - <field name="_textboxInitialized">false</field> - <field name="_textbox">document.getAnonymousElementByAttribute(this, - "anonid", "searchbar-textbox");</field> - <field name="_engines">null</field> - <field name="FormHistory" readonly="true"> - (Components.utils.import("resource://gre/modules/FormHistory.jsm", {})).FormHistory; - </field> - - <property name="engines" readonly="true"> - <getter><![CDATA[ - if (!this._engines) - this._engines = Services.search.getVisibleEngines(); - return this._engines; - ]]></getter> - </property> - - <property name="currentEngine"> - <setter><![CDATA[ - Services.search.currentEngine = val; - return val; - ]]></setter> - <getter><![CDATA[ - var currentEngine = Services.search.currentEngine; - // Return a dummy engine if there is no currentEngine - return currentEngine || {name: "", uri: null}; - ]]></getter> - </property> - - <!-- textbox is used by sanitize.js to clear the undo history when - clearing form information. --> - <property name="textbox" readonly="true" - onget="return this._textbox;"/> - - <property name="value" onget="return this._textbox.value;" - onset="return this._textbox.value = val;"/> - - <method name="focus"> - <body><![CDATA[ - this._textbox.focus(); - ]]></body> - </method> - - <method name="select"> - <body><![CDATA[ - this._textbox.select(); - ]]></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": - this.offerNewEngine(aEngine); - break; - case "engine-added": - this.hideNewEngine(aEngine); - break; - case "engine-changed": - // An engine was removed (or hidden) or added, or an icon was - // changed. Do nothing special. - } - - // Make sure the engine list is refetched next time it's needed - this._engines = null; - - // Update the popup header and update the display after any modification. - this._textbox.popup.updateHeader(); - this.updateDisplay(); - } - ]]></body> - </method> - - <!-- There are two seaprate lists of search engines, whose uses intersect - in this file. The search service (nsIBrowserSearchService and - nsSearchService.js) maintains a list of Engine objects which is used to - populate the searchbox list of available engines and to perform queries. - That list is accessed here via this.SearchService, and it's that sort of - Engine that is passed to this binding's observer as aEngine. - - In addition, browser.js fills two lists of autodetected search engines - (browser.engines and browser.hiddenEngines) as properties of - mCurrentBrowser. Those lists contain unnamed JS objects of the form - { uri:, title:, icon: }, and that's what the searchbar uses to determine - whether to show any "Add <EngineName>" menu items in the drop-down. - - The two types of engines are currently related by their identifying - titles (the Engine object's 'name'), although that may change; see bug - 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[ - for (let browser of gBrowser.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 = []; - browser.engines.push(browser.hiddenEngines[i]); - browser.hiddenEngines.splice(i, 1); - break; - } - } - } - } - BrowserSearch.updateOpenSearchBadge(); - ]]></body> - </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[ - for (let browser of gBrowser.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 = []; - browser.hiddenEngines.push(browser.engines[i]); - browser.engines.splice(i, 1); - break; - } - } - } - } - BrowserSearch.updateOpenSearchBadge(); - ]]></body> - </method> - - <method name="setIcon"> - <parameter name="element"/> - <parameter name="uri"/> - <body><![CDATA[ - element.setAttribute("src", uri); - ]]></body> - </method> - - <method name="updateDisplay"> - <body><![CDATA[ - var uri = this.currentEngine.iconURI; - this.setIcon(this, uri ? uri.spec : ""); - - var name = this.currentEngine.name; - var text = this._stringBundle.getFormattedString("searchtip", [name]); - - this._textbox.placeholder = this._stringBundle.getString("searchPlaceholder"); - this._textbox.label = text; - this._textbox.tooltipText = text; - ]]></body> - </method> - - <method name="updateGoButtonVisibility"> - <body><![CDATA[ - document.getAnonymousElementByAttribute(this, "anonid", - "search-go-button") - .hidden = !this._textbox.value; - ]]></body> - </method> - - <method name="openSuggestionsPanel"> - <parameter name="aShowOnlySettingsIfEmpty"/> - <body><![CDATA[ - if (this._textbox.open) - return; - - this._textbox.showHistoryPopup(); - - if (this._textbox.value) { - // showHistoryPopup does a startSearch("") call, ensure the - // controller handles the text from the input box instead: - this._textbox.mController.handleText(); - } - else if (aShowOnlySettingsIfEmpty) { - this.setAttribute("showonlysettings", "true"); - } - ]]></body> - </method> - - <method name="selectEngine"> - <parameter name="aEvent"/> - <parameter name="isNextEngine"/> - <body><![CDATA[ - // Find the new index - var newIndex = this.engines.indexOf(this.currentEngine); - newIndex += isNextEngine ? 1 : -1; - - if (newIndex >= 0 && newIndex < this.engines.length) { - this.currentEngine = this.engines[newIndex]; - } - - aEvent.preventDefault(); - aEvent.stopPropagation(); - - this.openSuggestionsPanel(); - ]]></body> - </method> - - <method name="handleSearchCommand"> - <parameter name="aEvent"/> - <parameter name="aEngine"/> - <parameter name="aForceNewTab"/> - <body><![CDATA[ - var where = "current"; - let params; - - // Open ctrl/cmd clicks on one-off buttons in a new background tab. - if (aEvent && aEvent.originalTarget.getAttribute("anonid") == "search-go-button") { - if (aEvent.button == 2) - return; - where = whereToOpenLink(aEvent, false, true); - } - else if (aForceNewTab) { - where = "tab"; - if (Services.prefs.getBoolPref("browser.tabs.loadInBackground")) - where += "-background"; - } - else { - var newTabPref = Services.prefs.getBoolPref("browser.search.openintab"); - if (((aEvent instanceof KeyboardEvent) && aEvent.altKey) ^ newTabPref) - where = "tab"; - if ((aEvent instanceof MouseEvent) && - (aEvent.button == 1 || aEvent.getModifierState("Accel"))) { - where = "tab"; - params = { - inBackground: true, - }; - } - } - - this.handleSearchCommandWhere(aEvent, aEngine, where, params); - ]]></body> - </method> - - <method name="handleSearchCommandWhere"> - <parameter name="aEvent"/> - <parameter name="aEngine"/> - <parameter name="aWhere"/> - <parameter name="aParams"/> - <body><![CDATA[ - var textBox = this._textbox; - var textValue = textBox.value; - - let selection = this.telemetrySearchDetails; - let oneOffRecorded = false; - - if (!selection || (selection.index == -1)) { - oneOffRecorded = this.textbox.popup.oneOffButtons - .maybeRecordTelemetry(aEvent, aWhere, aParams); - if (!oneOffRecorded) { - let source = "unknown"; - let type = "unknown"; - let target = aEvent.originalTarget; - if (aEvent instanceof KeyboardEvent) { - type = "key"; - } else if (aEvent instanceof MouseEvent) { - type = "mouse"; - if (target.classList.contains("search-panel-header") || - target.parentNode.classList.contains("search-panel-header")) { - source = "header"; - } - } else if (aEvent instanceof XULCommandEvent) { - if (target.getAttribute("anonid") == "paste-and-search") { - source = "paste"; - } - } - if (!aEngine) { - aEngine = this.currentEngine; - } - BrowserSearch.recordOneoffSearchInTelemetry(aEngine, source, type, - aWhere); - } - } - - // This is a one-off search only if oneOffRecorded is true. - this.doSearch(textValue, aWhere, aEngine, aParams, oneOffRecorded); - - if (aWhere == "tab" && aParams && aParams.inBackground) - this.focus(); - ]]></body> - </method> - - <method name="doSearch"> - <parameter name="aData"/> - <parameter name="aWhere"/> - <parameter name="aEngine"/> - <parameter name="aParams"/> - <parameter name="aOneOff"/> - <body><![CDATA[ - var textBox = this._textbox; - - // Save the current value in the form history - if (aData && !PrivateBrowsingUtils.isWindowPrivate(window) && this.FormHistory.enabled) { - this.FormHistory.update( - { op : "bump", - fieldname : textBox.getAttribute("autocompletesearchparam"), - value : aData }, - { handleError : function(aError) { - Components.utils.reportError("Saving search to form history failed: " + aError.message); - }}); - } - - let engine = aEngine || this.currentEngine; - var submission = engine.getSubmission(aData, null, "searchbar"); - let telemetrySearchDetails = this.telemetrySearchDetails; - this.telemetrySearchDetails = null; - if (telemetrySearchDetails && telemetrySearchDetails.index == -1) { - telemetrySearchDetails = null; - } - // If we hit here, we come either from a one-off, a plain search or a suggestion. - const details = { - isOneOff: aOneOff, - isSuggestion: (!aOneOff && telemetrySearchDetails), - selection: telemetrySearchDetails - }; - BrowserSearch.recordSearchInTelemetry(engine, "searchbar", details); - // null parameter below specifies HTML response for search - let params = { - postData: submission.postData, - }; - if (aParams) { - for (let key in aParams) { - params[key] = aParams[key]; - } - } - openUILinkIn(submission.uri.spec, aWhere, params); - ]]></body> - </method> - </implementation> - - <handlers> - <handler event="command"><![CDATA[ - const target = event.originalTarget; - if (target.engine) { - this.currentEngine = target.engine; - } else if (target.classList.contains("addengine-item")) { - // Select the installed engine if the installation succeeds - var installCallback = { - onSuccess: engine => this.currentEngine = engine - } - Services.search.addEngine(target.getAttribute("uri"), null, - target.getAttribute("src"), false, - installCallback); - } - else - return; - - this.focus(); - this.select(); - ]]></handler> - - <handler event="DOMMouseScroll" - phase="capturing" - modifiers="accel" - action="this.selectEngine(event, (event.detail > 0));"/> - - <handler event="input" action="this.updateGoButtonVisibility();"/> - <handler event="drop" action="this.updateGoButtonVisibility();"/> - - <handler event="blur"> - <![CDATA[ - // If the input field is still focused then a different window has - // received focus, ignore the next focus event. - this._ignoreFocus = (document.activeElement == this._textbox.inputField); - ]]></handler> - - <handler event="focus"> - <![CDATA[ - // Speculatively connect to the current engine's search URI (and - // suggest URI, if different) to reduce request latency - this.currentEngine.speculativeConnect({window: window}); - - if (this._ignoreFocus) { - // This window has been re-focused, don't show the suggestions - this._ignoreFocus = false; - return; - } - - // Don't open the suggestions if there is no text in the textbox. - if (!this._textbox.value) - return; - - // Don't open the suggestions if the mouse was used to focus the - // textbox, that will be taken care of in the click handler. - if (Services.focus.getLastFocusMethod(window) & Services.focus.FLAG_BYMOUSE) - return; - - this.openSuggestionsPanel(); - ]]></handler> - - <handler event="mousedown" phase="capturing"> - <![CDATA[ - if (event.originalTarget.getAttribute("anonid") == "searchbar-search-button") { - this._clickClosedPopup = this._textbox.popup._isHiding; - } - ]]></handler> - - <handler event="click" button="0"> - <![CDATA[ - // Ignore clicks on the search go button. - if (event.originalTarget.getAttribute("anonid") == "search-go-button") { - return; - } - - let isIconClick = event.originalTarget.getAttribute("anonid") == "searchbar-search-button"; - - // Ignore clicks on the icon if they were made to close the popup - if (isIconClick && this._clickClosedPopup) { - return; - } - - // Open the suggestions whenever clicking on the search icon or if there - // is text in the textbox. - if (isIconClick || this._textbox.value) { - this.openSuggestionsPanel(true); - } - ]]></handler> - - </handlers> - </binding> - - <binding id="searchbar-textbox" - extends="chrome://global/content/bindings/autocomplete.xml#autocomplete"> - <implementation implements="nsIObserver"> - <constructor><![CDATA[ - const kXULNS = - "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; - - if (document.getBindingParent(this).parentNode.parentNode.localName == - "toolbarpaletteitem") - return; - - // Initialize fields - this._stringBundle = document.getBindingParent(this)._stringBundle; - this._suggestEnabled = - Services.prefs.getBoolPref("browser.search.suggest.enabled"); - - if (Services.prefs.getBoolPref("browser.urlbar.clickSelectsAll")) - this.setAttribute("clickSelectsAll", true); - - // Add items to context menu and attach controller to handle them - var textBox = document.getAnonymousElementByAttribute(this, - "anonid", "textbox-input-box"); - var cxmenu = document.getAnonymousElementByAttribute(textBox, - "anonid", "input-box-contextmenu"); - var pasteAndSearch; - cxmenu.addEventListener("popupshowing", function() { - BrowserSearch.searchBar._textbox.closePopup(); - if (!pasteAndSearch) - return; - var controller = document.commandDispatcher.getControllerForCommand("cmd_paste"); - var enabled = controller.isCommandEnabled("cmd_paste"); - if (enabled) - pasteAndSearch.removeAttribute("disabled"); - else - pasteAndSearch.setAttribute("disabled", "true"); - }, false); - - var element, label, akey; - - element = document.createElementNS(kXULNS, "menuseparator"); - cxmenu.appendChild(element); - - this.setAttribute("aria-owns", this.popup.id); - - var insertLocation = cxmenu.firstChild; - while (insertLocation.nextSibling && - insertLocation.getAttribute("cmd") != "cmd_paste") - insertLocation = insertLocation.nextSibling; - if (insertLocation) { - element = document.createElementNS(kXULNS, "menuitem"); - label = this._stringBundle.getString("cmd_pasteAndSearch"); - element.setAttribute("label", label); - element.setAttribute("anonid", "paste-and-search"); - element.setAttribute("oncommand", "BrowserSearch.pasteAndSearch(event)"); - cxmenu.insertBefore(element, insertLocation.nextSibling); - pasteAndSearch = element; - } - - element = document.createElementNS(kXULNS, "menuitem"); - label = this._stringBundle.getString("cmd_clearHistory"); - akey = this._stringBundle.getString("cmd_clearHistory_accesskey"); - element.setAttribute("label", label); - element.setAttribute("accesskey", akey); - element.setAttribute("cmd", "cmd_clearhistory"); - cxmenu.appendChild(element); - - element = document.createElementNS(kXULNS, "menuitem"); - label = this._stringBundle.getString("cmd_showSuggestions"); - akey = this._stringBundle.getString("cmd_showSuggestions_accesskey"); - element.setAttribute("anonid", "toggle-suggest-item"); - element.setAttribute("label", label); - element.setAttribute("accesskey", akey); - element.setAttribute("cmd", "cmd_togglesuggest"); - element.setAttribute("type", "checkbox"); - element.setAttribute("checked", this._suggestEnabled); - element.setAttribute("autocheck", "false"); - this._suggestMenuItem = element; - cxmenu.appendChild(element); - - this.addEventListener("keypress", aEvent => { - if (navigator.platform.startsWith("Mac") && aEvent.keyCode == KeyEvent.VK_F4) - this.openSearch() - }, true); - - this.controllers.appendController(this.searchbarController); - document.getBindingParent(this)._textboxInitialized = true; - - // Add observer for suggest preference - Services.prefs.addObserver("browser.search.suggest.enabled", this, false); - ]]></constructor> - - <destructor><![CDATA[ - Services.prefs.removeObserver("browser.search.suggest.enabled", this); - - // Because XBL and the customize toolbar code interacts poorly, - // there may not be anything to remove here - try { - this.controllers.removeController(this.searchbarController); - } catch (ex) { } - ]]></destructor> - - <field name="_stringBundle"/> - <field name="_suggestMenuItem"/> - <field name="_suggestEnabled"/> - - <!-- - This overrides the searchParam property in autocomplete.xml. We're - hijacking this property as a vehicle for delivering the privacy - information about the window into the guts of nsSearchSuggestions. - - Note that the setter is the same as the parent. We were not sure whether - we can override just the getter. If that proves to be the case, the setter - can be removed. - --> - <property name="searchParam" - onget="return this.getAttribute('autocompletesearchparam') + - (PrivateBrowsingUtils.isWindowPrivate(window) ? '|private' : '');" - onset="this.setAttribute('autocompletesearchparam', val); return val;"/> - - <!-- This is implemented so that when textbox.value is set directly (e.g., - by tests), the one-off query is updated. --> - <method name="onBeforeValueSet"> - <parameter name="aValue"/> - <body><![CDATA[ - this.popup.oneOffButtons.query = aValue; - return aValue; - ]]></body> - </method> - - <!-- - This method overrides the autocomplete binding's openPopup (essentially - duplicating the logic from the autocomplete popup binding's - openAutocompletePopup method), modifying it so that the popup is aligned with - the inner textbox, but sized to not extend beyond the search bar border. - --> - <method name="openPopup"> - <body><![CDATA[ - var popup = this.popup; - if (!popup.mPopupOpen) { - // Initially the panel used for the searchbar (PopupSearchAutoComplete - // in browser.xul) is hidden to avoid impacting startup / new - // window performance. The base binding's openPopup would normally - // call the overriden openAutocompletePopup in urlbarBindings.xml's - // browser-autocomplete-result-popup binding to unhide the popup, - // but since we're overriding openPopup we need to unhide the panel - // ourselves. - popup.hidden = false; - - // Don't roll up on mouse click in the anchor for the search UI. - if (popup.id == "PopupSearchAutoComplete") { - popup.setAttribute("norolluponanchor", "true"); - } - - popup.mInput = this; - popup.view = this.controller.QueryInterface(Ci.nsITreeView); - popup.invalidate(); - - popup.showCommentColumn = this.showCommentColumn; - popup.showImageColumn = this.showImageColumn; - - document.popupNode = null; - - const isRTL = getComputedStyle(this, "").direction == "rtl"; - - var outerRect = this.getBoundingClientRect(); - var innerRect = this.inputField.getBoundingClientRect(); - let width = isRTL ? - innerRect.right - outerRect.left : - outerRect.right - innerRect.left; - popup.setAttribute("width", width > 100 ? width : 100); - - var yOffset = outerRect.bottom - innerRect.bottom; - popup.openPopup(this.inputField, "after_start", 0, yOffset, false, false); - } - ]]></body> - </method> - - <method name="observe"> - <parameter name="aSubject"/> - <parameter name="aTopic"/> - <parameter name="aData"/> - <body><![CDATA[ - if (aTopic == "nsPref:changed") { - this._suggestEnabled = - Services.prefs.getBoolPref("browser.search.suggest.enabled"); - this._suggestMenuItem.setAttribute("checked", this._suggestEnabled); - } - ]]></body> - </method> - - <method name="openSearch"> - <body> - <![CDATA[ - if (!this.popupOpen) { - document.getBindingParent(this).openSuggestionsPanel(); - return false; - } - return true; - ]]> - </body> - </method> - - <!-- override |onTextEntered| in autocomplete.xml --> - <method name="onTextEntered"> - <parameter name="aEvent"/> - <body><![CDATA[ - let engine; - let oneOff = this.selectedButton; - if (oneOff) { - if (!oneOff.engine) { - oneOff.doCommand(); - return; - } - engine = oneOff.engine; - } - if (this._selectionDetails && - this._selectionDetails.currentIndex != -1) { - BrowserSearch.searchBar.telemetrySearchDetails = this._selectionDetails; - this._selectionDetails = null; - } - document.getBindingParent(this).handleSearchCommand(aEvent, engine); - ]]></body> - </method> - - <property name="selectedButton"> - <getter><![CDATA[ - return this.popup.oneOffButtons.selectedButton; - ]]></getter> - <setter><![CDATA[ - return this.popup.oneOffButtons.selectedButton = val; - ]]></setter> - </property> - - <method name="handleKeyboardNavigation"> - <parameter name="aEvent"/> - <body><![CDATA[ - let popup = this.popup; - if (!popup.popupOpen) - return; - - // accel + up/down changes the default engine and shouldn't affect - // the selection on the one-off buttons. - if (aEvent.getModifierState("Accel")) - return; - - let suggestions = - document.getAnonymousElementByAttribute(popup, "anonid", "tree"); - let suggestionsHidden = - suggestions.getAttribute("collapsed") == "true"; - let numItems = suggestionsHidden ? 0 : this.popup.view.rowCount; - this.popup.oneOffButtons.handleKeyPress(aEvent, numItems, true); - ]]></body> - </method> - - <!-- nsIController --> - <field name="searchbarController" readonly="true"><![CDATA[({ - _self: this, - supportsCommand: function(aCommand) { - return aCommand == "cmd_clearhistory" || - aCommand == "cmd_togglesuggest"; - }, - - isCommandEnabled: function(aCommand) { - return true; - }, - - doCommand: function (aCommand) { - switch (aCommand) { - case "cmd_clearhistory": - var param = this._self.getAttribute("autocompletesearchparam"); - - BrowserSearch.searchBar.FormHistory.update({ op : "remove", fieldname : param }, null); - this._self.value = ""; - break; - case "cmd_togglesuggest": - // The pref observer will update _suggestEnabled and the menu - // checkmark. - Services.prefs.setBoolPref("browser.search.suggest.enabled", - !this._self._suggestEnabled); - break; - default: - // do nothing with unrecognized command - } - } - })]]></field> - </implementation> - - <handlers> - <handler event="input"><![CDATA[ - this.popup.removeAttribute("showonlysettings"); - ]]></handler> - - <handler event="keypress" phase="capturing" - action="return this.handleKeyboardNavigation(event);"/> - - <handler event="keypress" keycode="VK_UP" modifiers="accel" - phase="capturing" - action="document.getBindingParent(this).selectEngine(event, false);"/> - - <handler event="keypress" keycode="VK_DOWN" modifiers="accel" - phase="capturing" - action="document.getBindingParent(this).selectEngine(event, true);"/> - - <handler event="keypress" keycode="VK_DOWN" modifiers="alt" - phase="capturing" - action="return this.openSearch();"/> - - <handler event="keypress" keycode="VK_UP" modifiers="alt" - phase="capturing" - action="return this.openSearch();"/> - - <handler event="dragover"> - <![CDATA[ - var types = event.dataTransfer.types; - if (types.includes("text/plain") || types.includes("text/x-moz-text-internal")) - event.preventDefault(); - ]]> - </handler> - - <handler event="drop"> - <![CDATA[ - var dataTransfer = event.dataTransfer; - var data = dataTransfer.getData("text/plain"); - if (!data) - data = dataTransfer.getData("text/x-moz-text-internal"); - if (data) { - event.preventDefault(); - this.value = data; - document.getBindingParent(this).openSuggestionsPanel(); - } - ]]> - </handler> - - </handlers> - </binding> - - <binding id="browser-search-autocomplete-result-popup" extends="chrome://browser/content/urlbarBindings.xml#browser-autocomplete-result-popup"> - <resources> - <stylesheet src="chrome://browser/content/search/searchbarBindings.css"/> - <stylesheet src="chrome://browser/skin/searchbar.css"/> - </resources> - <content ignorekeys="true" level="top" consumeoutsideclicks="never"> - <xul:hbox anonid="searchbar-engine" xbl:inherits="showonlysettings" - class="search-panel-header search-panel-current-engine"> - <xul:image class="searchbar-engine-image" xbl:inherits="src"/> - <xul:label anonid="searchbar-engine-name" flex="1" crop="end" - role="presentation"/> - </xul:hbox> - <xul:tree anonid="tree" flex="1" - class="autocomplete-tree plain search-panel-tree" - hidecolumnpicker="true" seltype="single"> - <xul:treecols anonid="treecols"> - <xul:treecol id="treecolAutoCompleteValue" class="autocomplete-treecol" flex="1" overflow="true"/> - </xul:treecols> - <xul:treechildren class="autocomplete-treebody"/> - </xul:tree> - <xul:vbox anonid="search-one-off-buttons" class="search-one-offs"/> - </content> - <implementation> - <!-- Popup rollup is triggered by native events before the mousedown event - reaches the DOM. The will be set to true by the popuphiding event and - false after the mousedown event has been triggered to detect what - caused rollup. --> - <field name="_isHiding">false</field> - <field name="_bundle">null</field> - <property name="bundle" readonly="true"> - <getter> - <![CDATA[ - if (!this._bundle) { - const kBundleURI = "chrome://browser/locale/search.properties"; - this._bundle = Services.strings.createBundle(kBundleURI); - } - return this._bundle; - ]]> - </getter> - </property> - - <field name="oneOffButtons" readonly="true"> - document.getAnonymousElementByAttribute(this, "anonid", - "search-one-off-buttons"); - </field> - - <method name="updateHeader"> - <body><![CDATA[ - let currentEngine = Services.search.currentEngine; - let uri = currentEngine.iconURI; - if (uri) { - this.setAttribute("src", uri.spec); - } - else { - // If the default has just been changed to a provider without icon, - // avoid showing the icon of the previous default provider. - this.removeAttribute("src"); - } - - let headerText = this.bundle.formatStringFromName("searchHeader", - [currentEngine.name], 1); - document.getAnonymousElementByAttribute(this, "anonid", "searchbar-engine-name") - .setAttribute("value", headerText); - document.getAnonymousElementByAttribute(this, "anonid", "searchbar-engine") - .engine = currentEngine; - ]]></body> - </method> - - <!-- This is called when a one-off is clicked and when "search in new tab" - is selected from a one-off context menu. --> - <method name="handleOneOffSearch"> - <parameter name="event"/> - <parameter name="engine"/> - <parameter name="where"/> - <parameter name="params"/> - <body><![CDATA[ - let searchbar = document.getElementById("searchbar"); - searchbar.handleSearchCommandWhere(event, engine, where, params); - ]]></body> - </method> - </implementation> - - <handlers> - <handler event="popupshowing"><![CDATA[ - if (!this.oneOffButtons.popup) { - // The panel width only spans to the textbox size, but we also want it - // to include the magnifier icon's width. - let ltr = getComputedStyle(this).direction == "ltr"; - let magnifierWidth = parseInt(getComputedStyle(this)[ - ltr ? "marginLeft" : "marginRight" - ]) * -1; - // Ensure the panel is wide enough to fit at least 3 engines. - let minWidth = Math.max( - parseInt(this.width) + magnifierWidth, - this.oneOffButtons.buttonWidth * 3 - ); - this.style.minWidth = minWidth + "px"; - - // Set the origin before assigning the popup, as the assignment does - // a rebuild and would miss the origin. - this.oneOffButtons.telemetryOrigin = "searchbar"; - // Set popup after setting the minWidth since it builds the buttons. - this.oneOffButtons.popup = this; - this.oneOffButtons.textbox = this.input; - } - - // First handle deciding if we are showing the reduced version of the - // popup containing only the preferences button. We do this if the - // glass icon has been clicked if the text field is empty. - let searchbar = document.getElementById("searchbar"); - let tree = document.getAnonymousElementByAttribute(this, "anonid", - "tree") - if (searchbar.hasAttribute("showonlysettings")) { - searchbar.removeAttribute("showonlysettings"); - this.setAttribute("showonlysettings", "true"); - - // Setting this with an xbl-inherited attribute gets overridden the - // second time the user clicks the glass icon for some reason... - tree.collapsed = true; - } - else { - this.removeAttribute("showonlysettings"); - // Uncollapse as long as we have a tree with a view which has >= 1 row. - // The autocomplete binding itself will take care of uncollapsing later, - // if we currently have no rows but end up having some in the future - // when the search string changes - tree.collapsed = !tree.view || !tree.view.rowCount; - } - - // Show the current default engine in the top header of the panel. - this.updateHeader(); - ]]></handler> - - <handler event="popuphiding"><![CDATA[ - this._isHiding = true; - setTimeout(() => { - this._isHiding = false; - }, 0); - ]]></handler> - - <!-- This handles clicks on the topmost "Foo Search" header in the - popup (hbox[anonid="searchbar-engine"]). --> - <handler event="click"><![CDATA[ - if (event.button == 2) { - // Ignore right clicks. - return; - } - let button = event.originalTarget; - let engine = button.parentNode.engine; - if (!engine) { - return; - } - this.oneOffButtons.handleSearchCommand(event, engine); - ]]></handler> - </handlers> - - </binding> - - <!-- Used for additional open search providers in the search panel. --> - <binding id="addengine-icon" extends="xul:box"> - <content> - <xul:image class="addengine-icon" xbl:inherits="src"/> - <xul:image class="addengine-badge"/> - </content> - </binding> - - <binding id="search-one-offs"> - <content context="_child"> - <xul:deck anonid="search-panel-one-offs-header" - selectedIndex="0" - class="search-panel-header search-panel-current-input"> - <xul:label anonid="searchbar-oneoffheader-search" - value="&searchWithHeader.label;"/> - <xul:hbox anonid="search-panel-searchforwith" - class="search-panel-current-input"> - <xul:label anonid="searchbar-oneoffheader-before" - value="&searchFor.label;"/> - <xul:label anonid="searchbar-oneoffheader-searchtext" - class="search-panel-input-value" - flex="1" - crop="end"/> - <xul:label anonid="searchbar-oneoffheader-after" - flex="10000" - value="&searchWith.label;"/> - </xul:hbox> - <xul:hbox anonid="search-panel-searchonengine" - class="search-panel-current-input"> - <xul:label anonid="searchbar-oneoffheader-beforeengine" - value="&search.label;"/> - <xul:label anonid="searchbar-oneoffheader-engine" - class="search-panel-input-value" - flex="1" - crop="end"/> - <xul:label anonid="searchbar-oneoffheader-afterengine" - flex="10000" - value="&searchAfter.label;"/> - </xul:hbox> - </xul:deck> - <xul:description anonid="search-panel-one-offs" - role="group" - class="search-panel-one-offs" - xbl:inherits="compact"> - <xul:button anonid="search-settings-compact" - oncommand="showSettings();" - class="searchbar-engine-one-off-item search-setting-button-compact" - tooltiptext="&changeSearchSettings.tooltip;" - xbl:inherits="compact"/> - </xul:description> - <xul:vbox anonid="add-engines"/> - <xul:button anonid="search-settings" - oncommand="showSettings();" - class="search-setting-button search-panel-header" - label="&changeSearchSettings.button;" - xbl:inherits="compact"/> - <xul:menupopup anonid="search-one-offs-context-menu"> - <xul:menuitem anonid="search-one-offs-context-open-in-new-tab" - label="&searchInNewTab.label;" - accesskey="&searchInNewTab.accesskey;"/> - <xul:menuitem anonid="search-one-offs-context-set-default" - label="&searchSetAsDefault.label;" - accesskey="&searchSetAsDefault.accesskey;"/> - </xul:menupopup> - </content> - - <implementation implements="nsIDOMEventListener"> - - <!-- Width in pixels of the one-off buttons. 49px is the min-width of - each search engine button, adapt this const when changing the css. - It's actually 48px + 1px of right border. --> - <property name="buttonWidth" readonly="true" onget="return 49;"/> - - <field name="_popup">null</field> - - <!-- The popup that contains the one-offs. This is required, so it should - never be null or undefined, except possibly before the one-offs are - used. --> - <property name="popup"> - <getter><![CDATA[ - return this._popup; - ]]></getter> - <setter><![CDATA[ - if (this._popup == val) { - return val; - } - - let events = [ - "popupshowing", - "popuphidden", - ]; - if (this._popup) { - for (let event of events) { - this._popup.removeEventListener(event, this); - } - } - if (val) { - for (let event of events) { - val.addEventListener(event, this); - } - } - this._popup = val; - - // If the popup is already open, rebuild the one-offs now. The - // popup may be opening, so check that the state is not closed - // instead of checking popupOpen. - if (val && val.state != "closed") { - this._rebuild(); - } - return val; - ]]></setter> - </property> - - <field name="_textbox">null</field> - - <!-- The textbox associated with the one-offs. Set this to a textbox to - automatically keep the related one-offs UI up to date. Otherwise you - can leave it null/undefined, and in that case you should update the - query property manually. --> - <property name="textbox"> - <getter><![CDATA[ - return this._textbox; - ]]></getter> - <setter><![CDATA[ - if (this._textbox == val) { - return val; - } - if (this._textbox) { - this._textbox.removeEventListener("input", this); - } - if (val) { - val.addEventListener("input", this); - } - return this._textbox = val; - ]]></setter> - </property> - - <!-- Set this to a string that identifies your one-offs consumer. It'll - be appended to telemetry recorded with maybeRecordTelemetry(). --> - <field name="telemetryOrigin">""</field> - - <field name="_query">""</field> - - <!-- The query string currently shown in the one-offs. If the textbox - property is non-null, then this is automatically updated on - input. --> - <property name="query"> - <getter><![CDATA[ - return this._query; - ]]></getter> - <setter><![CDATA[ - this._query = val; - if (this.popup && this.popup.popupOpen) { - this._updateAfterQueryChanged(); - } - return val; - ]]></setter> - </property> - - <field name="_selectedButton">null</field> - - <!-- The selected one-off, a xul:button, including the add-engine button - and the search-settings button. Null if no one-off is selected. --> - <property name="selectedButton"> - <getter><![CDATA[ - return this._selectedButton; - ]]></getter> - <setter><![CDATA[ - this._changeVisuallySelectedButton(val, true); - return val; - ]]></setter> - </property> - - <!-- The index of the selected one-off, including the add-engine button - and the search-settings button. -1 if no one-off is selected. --> - <property name="selectedButtonIndex"> - <getter><![CDATA[ - let buttons = this.getSelectableButtons(true); - for (let i = 0; i < buttons.length; i++) { - if (buttons[i] == this._selectedButton) { - return i; - } - } - return -1; - ]]></getter> - <setter><![CDATA[ - let buttons = this.getSelectableButtons(true); - this.selectedButton = buttons[val]; - return val; - ]]></setter> - </property> - - <!-- The visually selected one-off is the same as the selected one-off - unless a one-off is moused over. In that case, the visually selected - one-off is the moused-over one-off, which may be different from the - selected one-off. The visually selected one-off is always the one - that is visually highlighted. Includes the add-engine button and the - search-settings button. A xul:button. --> - <property name="visuallySelectedButton" readonly="true"> - <getter><![CDATA[ - return this.getSelectableButtons(true).find(button => { - return button.getAttribute("selected") == "true"; - }); - ]]></getter> - </property> - - <property name="compact" readonly="true"> - <getter><![CDATA[ - return this.getAttribute("compact") == "true"; - ]]></getter> - </property> - - <property name="settingsButton" readonly="true"> - <getter><![CDATA[ - let id = this.compact ? "search-settings-compact" : "search-settings"; - return document.getAnonymousElementByAttribute(this, "anonid", id); - ]]></getter> - </property> - - <field name="_bundle">null</field> - - <property name="bundle" readonly="true"> - <getter><![CDATA[ - if (!this._bundle) { - const kBundleURI = "chrome://browser/locale/search.properties"; - this._bundle = Services.strings.createBundle(kBundleURI); - } - return this._bundle; - ]]></getter> - </property> - - <!-- When a context menu is opened on a one-off button, this is set to the - engine of that button for use with the context menu actions. --> - <field name="_contextEngine">null</field> - - <constructor><![CDATA[ - // Prevent popup events from the context menu from reaching the autocomplete - // binding (or other listeners). - let menu = document.getAnonymousElementByAttribute(this, "anonid", "search-one-offs-context-menu"); - let listener = aEvent => aEvent.stopPropagation(); - menu.addEventListener("popupshowing", listener); - menu.addEventListener("popuphiding", listener); - menu.addEventListener("popupshown", aEvent => { - this._ignoreMouseEvents = true; - aEvent.stopPropagation(); - }); - menu.addEventListener("popuphidden", aEvent => { - this._ignoreMouseEvents = false; - aEvent.stopPropagation(); - }); - ]]></constructor> - - <!-- This handles events outside the one-off buttons, like on the popup - and textbox. --> - <method name="handleEvent"> - <parameter name="event"/> - <body><![CDATA[ - switch (event.type) { - case "input": - // Allow the consumer's input to override its value property with - // a oneOffSearchQuery property. That way if the value is not - // actually what the user typed (e.g., it's autofilled, or it's a - // mozaction URI), the consumer has some way of providing it. - this.query = event.target.oneOffSearchQuery || event.target.value; - break; - case "popupshowing": - this._rebuild(); - break; - case "popuphidden": - Services.tm.mainThread.dispatch(() => { - this.selectedButton = null; - this._contextEngine = null; - }, Ci.nsIThread.DISPATCH_NORMAL); - break; - } - ]]></body> - </method> - - <method name="showSettings"> - <body><![CDATA[ - openPreferences("paneSearch"); - // If the preference tab was already selected, the panel doesn't - // close itself automatically. - this.popup.hidePopup(); - ]]></body> - </method> - - <!-- Updates the parts of the UI that show the query string. --> - <method name="_updateAfterQueryChanged"> - <body><![CDATA[ - let headerSearchText = - document.getAnonymousElementByAttribute(this, "anonid", - "searchbar-oneoffheader-searchtext"); - let headerPanel = - document.getAnonymousElementByAttribute(this, "anonid", - "search-panel-one-offs-header"); - let list = document.getAnonymousElementByAttribute(this, "anonid", - "search-panel-one-offs"); - headerSearchText.setAttribute("value", this.query); - let groupText; - let isOneOffSelected = - this.selectedButton && - this.selectedButton.classList.contains("searchbar-engine-one-off-item"); - // Typing de-selects the settings or opensearch buttons at the bottom - // of the search panel, as typing shows the user intends to search. - if (this.selectedButton && !isOneOffSelected) - this.selectedButton = null; - if (this.query) { - groupText = headerSearchText.previousSibling.value + - '"' + headerSearchText.value + '"' + - headerSearchText.nextSibling.value; - if (!isOneOffSelected) - headerPanel.selectedIndex = 1; - } - else { - let noSearchHeader = - document.getAnonymousElementByAttribute(this, "anonid", - "searchbar-oneoffheader-search"); - groupText = noSearchHeader.value; - if (!isOneOffSelected) - headerPanel.selectedIndex = 0; - } - list.setAttribute("aria-label", groupText); - ]]></body> - </method> - - <!-- Builds all the UI. --> - <method name="_rebuild"> - <body><![CDATA[ - // Update the 'Search for <keywords> with:" header. - this._updateAfterQueryChanged(); - - let list = document.getAnonymousElementByAttribute(this, "anonid", - "search-panel-one-offs"); - - // Handle opensearch items. This needs to be done before building the - // list of one off providers, as that code will return early if all the - // alternative engines are hidden. - let addEngineList = - document.getAnonymousElementByAttribute(this, "anonid", "add-engines"); - while (addEngineList.firstChild) - addEngineList.firstChild.remove(); - - const kXULNS = - "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"; - - // Add a button for each engine that the page in the selected browser - // offers. But not when the one-offs are compact. Compact one-offs - // are shown in the urlbar, and the add-engine buttons span the width - // of the popup, so if we added all the engines that a site offers, it - // could effectively break the urlbar popup by offering a ton of - // engines. We should probably make a smaller version of the buttons - // for compact one-offs. - if (!this.compact) { - for (let engine of gBrowser.selectedBrowser.engines || []) { - let button = document.createElementNS(kXULNS, "button"); - let label = this.bundle.formatStringFromName("cmd_addFoundEngine", - [engine.title], 1); - button.id = this.telemetryOrigin + "-add-engine-" + - engine.title.replace(/ /g, '-'); - button.setAttribute("class", "addengine-item"); - button.setAttribute("label", label); - button.setAttribute("pack", "start"); - - button.setAttribute("crop", "end"); - button.setAttribute("tooltiptext", engine.uri); - button.setAttribute("uri", engine.uri); - if (engine.icon) { - button.setAttribute("image", engine.icon); - } - button.setAttribute("title", engine.title); - addEngineList.appendChild(button); - } - } - - let settingsButton = - document.getAnonymousElementByAttribute(this, "anonid", - "search-settings-compact"); - // Finally, build the list of one-off buttons. - while (list.firstChild != settingsButton) - list.firstChild.remove(); - // Remove the trailing empty text node introduced by the binding's - // content markup above. - if (settingsButton.nextSibling) - settingsButton.nextSibling.remove(); - - let Preferences = - Cu.import("resource://gre/modules/Preferences.jsm", {}).Preferences; - let pref = Preferences.get("browser.search.hiddenOneOffs"); - let hiddenList = pref ? pref.split(",") : []; - - let currentEngineName = Services.search.currentEngine.name; - let includeCurrentEngine = this.getAttribute("includecurrentengine"); - let engines = Services.search.getVisibleEngines().filter(e => { - return (includeCurrentEngine || e.name != currentEngineName) && - !hiddenList.includes(e.name); - }); - - let header = document.getAnonymousElementByAttribute(this, "anonid", - "search-panel-one-offs-header") - // header is a xul:deck so collapsed doesn't work on it, see bug 589569. - header.hidden = list.collapsed = !engines.length; - - if (!engines.length) - return; - - let panelWidth = parseInt(this.popup.clientWidth); - // The + 1 is because the last button doesn't have a right border. - let enginesPerRow = Math.floor((panelWidth + 1) / this.buttonWidth); - let buttonWidth = Math.floor(panelWidth / enginesPerRow); - // There will be an emtpy area of: - // panelWidth - enginesPerRow * buttonWidth px - // at the end of each row. - - // If the <description> tag with the list of search engines doesn't have - // a fixed height, the panel will be sized incorrectly, causing the bottom - // of the suggestion <tree> to be hidden. - let oneOffCount = engines.length; - if (this.compact) - ++oneOffCount; - let rowCount = Math.ceil(oneOffCount / enginesPerRow); - let height = rowCount * 33; // 32px per row, 1px border. - list.setAttribute("height", height + "px"); - - // Ensure we can refer to the settings buttons by ID: - let settingsEl = document.getAnonymousElementByAttribute(this, "anonid", "search-settings"); - settingsEl.id = this.telemetryOrigin + "-anon-search-settings"; - let compactSettingsEl = document.getAnonymousElementByAttribute(this, "anonid", "search-settings-compact"); - compactSettingsEl.id = this.telemetryOrigin + - "-anon-search-settings-compact"; - - let dummyItems = enginesPerRow - (oneOffCount % enginesPerRow || enginesPerRow); - for (let i = 0; i < engines.length; ++i) { - let engine = engines[i]; - let button = document.createElementNS(kXULNS, "button"); - button.id = this._buttonIDForEngine(engine); - let uri = "chrome://browser/skin/search-engine-placeholder.png"; - if (engine.iconURI) { - uri = engine.iconURI.spec; - } - button.setAttribute("image", uri); - button.setAttribute("class", "searchbar-engine-one-off-item"); - button.setAttribute("tooltiptext", engine.name); - button.setAttribute("width", buttonWidth); - button.engine = engine; - - if ((i + 1) % enginesPerRow == 0) - button.classList.add("last-of-row"); - - if (i + 1 == engines.length) - button.classList.add("last-engine"); - - if (i >= oneOffCount + dummyItems - enginesPerRow) - button.classList.add("last-row"); - - list.insertBefore(button, settingsButton); - } - - let hasDummyItems = !!dummyItems; - while (dummyItems) { - let button = document.createElementNS(kXULNS, "button"); - button.setAttribute("class", "searchbar-engine-one-off-item dummy last-row"); - button.setAttribute("width", buttonWidth); - - if (!--dummyItems) - button.classList.add("last-of-row"); - - list.insertBefore(button, settingsButton); - } - - if (this.compact) { - this.settingsButton.setAttribute("width", buttonWidth); - if (rowCount == 1 && hasDummyItems) { - // When there's only one row, make the compact settings button - // hug the right edge of the panel. It may not due to the panel's - // width not being an integral multiple of the button width. (See - // the "There will be an emtpy area" comment above.) Increase the - // width of the last dummy item by the remainder. - // - // There's one weird thing to guard against. When layout pixels - // aren't an integral multiple of device pixels, the calculated - // remainder can end up being ~1px too big, at least on Windows, - // which pushes the settings button to a new row. The remainder - // is integral, not a fraction, so that's not the problem. To - // work around that, unscale the remainder, floor it, scale it - // back, and then floor that. - let scale = window.QueryInterface(Ci.nsIInterfaceRequestor) - .getInterface(Ci.nsIDOMWindowUtils) - .screenPixelsPerCSSPixel; - let remainder = panelWidth - (enginesPerRow * buttonWidth); - remainder = Math.floor(Math.floor(remainder * scale) / scale); - let width = remainder + buttonWidth; - let lastDummyItem = this.settingsButton.previousSibling; - lastDummyItem.setAttribute("width", width); - } - } - ]]></body> - </method> - - <method name="_buttonIDForEngine"> - <parameter name="engine"/> - <body><![CDATA[ - return this.telemetryOrigin + "-engine-one-off-item-" + - engine.name.replace(/ /g, '-'); - ]]></body> - </method> - - <method name="_buttonForEngine"> - <parameter name="engine"/> - <body><![CDATA[ - return document.getElementById(this._buttonIDForEngine(engine)); - ]]></body> - </method> - - <method name="_changeVisuallySelectedButton"> - <parameter name="val"/> - <parameter name="aUpdateLogicallySelectedButton"/> - <body><![CDATA[ - let visuallySelectedButton = this.visuallySelectedButton; - if (visuallySelectedButton) - visuallySelectedButton.removeAttribute("selected"); - - let header = - document.getAnonymousElementByAttribute(this, "anonid", - "search-panel-one-offs-header"); - // Avoid selecting dummy buttons. - if (val && !val.classList.contains("dummy")) { - val.setAttribute("selected", "true"); - if (val.classList.contains("searchbar-engine-one-off-item") && - val.engine) { - let headerEngineText = - document.getAnonymousElementByAttribute(this, "anonid", - "searchbar-oneoffheader-engine"); - header.selectedIndex = 2; - headerEngineText.value = val.engine.name; - } - else { - header.selectedIndex = this.query ? 1 : 0; - } - if (this.textbox) { - this.textbox.setAttribute("aria-activedescendant", val.id); - } - } else { - val = null; - header.selectedIndex = this.query ? 1 : 0; - if (this.textbox) { - this.textbox.removeAttribute("aria-activedescendant"); - } - } - - if (aUpdateLogicallySelectedButton) { - this._selectedButton = val; - if (val && !val.engine) { - // If the button doesn't have an engine, then clear the popup's - // selection to indicate that pressing Return while the button is - // selected will do the button's command, not search. - this.popup.selectedIndex = -1; - } - let event = document.createEvent("Events"); - event.initEvent("SelectedOneOffButtonChanged", true, false); - this.dispatchEvent(event); - } - ]]></body> - </method> - - <method name="getSelectableButtons"> - <parameter name="aIncludeNonEngineButtons"/> - <body><![CDATA[ - let buttons = []; - let oneOff = document.getAnonymousElementByAttribute(this, "anonid", - "search-panel-one-offs"); - for (oneOff = oneOff.firstChild; oneOff; oneOff = oneOff.nextSibling) { - // oneOff may be a text node since the list xul:description contains - // whitespace and the compact settings button. See the markup - // above. _rebuild removes text nodes, but it may not have been - // called yet (because e.g. the popup hasn't been opened yet). - if (oneOff.nodeType == Node.ELEMENT_NODE) { - if (oneOff.classList.contains("dummy") || - oneOff.classList.contains("search-setting-button-compact")) - break; - buttons.push(oneOff); - } - } - - if (!aIncludeNonEngineButtons) - return buttons; - - let addEngine = - document.getAnonymousElementByAttribute(this, "anonid", "add-engines"); - for (addEngine = addEngine.firstChild; addEngine; addEngine = addEngine.nextSibling) - buttons.push(addEngine); - - buttons.push(this.settingsButton); - return buttons; - ]]></body> - </method> - - <method name="handleSearchCommand"> - <parameter name="aEvent"/> - <parameter name="aEngine"/> - <parameter name="aForceNewTab"/> - <body><![CDATA[ - let where = "current"; - let params; - - // Open ctrl/cmd clicks on one-off buttons in a new background tab. - if (aForceNewTab) { - where = "tab"; - if (Services.prefs.getBoolPref("browser.tabs.loadInBackground")) { - params = { - inBackground: true, - }; - } - } - else { - var newTabPref = Services.prefs.getBoolPref("browser.search.openintab"); - if (((aEvent instanceof KeyboardEvent) && aEvent.altKey) ^ newTabPref) - where = "tab"; - if ((aEvent instanceof MouseEvent) && - (aEvent.button == 1 || aEvent.getModifierState("Accel"))) { - where = "tab"; - params = { - inBackground: true, - }; - } - } - - this.popup.handleOneOffSearch(aEvent, aEngine, where, params); - ]]></body> - </method> - - <!-- - Increments or decrements the index of the currently selected one-off. - - @param aForward - If true, the index is incremented, and if false, the index is - decremented. - @param aWrapAround - This has a couple of effects, depending on whether there is - currently a selection. - (1) If true and the last one-off is currently selected, - incrementing the index will cause the selection to be cleared and - this method to return true. Calling advanceSelection again after - that (again with aForward=true) will select the first one-off. - Likewise if decrementing the index when the first one-off is - selected, except in the opposite direction of course. - (2) If true and there currently is no selection, decrementing the - index will cause the last one-off to become selected and this - method to return true. Only the aForward=false case is affected - because it is always the case that if aForward=true and there - currently is no selection, the first one-off becomes selected and - this method returns true. - @param aCycleEngines - If true, only engine buttons are included. - @return True if the selection can continue to advance after this method - returns and false if not. - --> - <method name="advanceSelection"> - <parameter name="aForward"/> - <parameter name="aWrapAround"/> - <parameter name="aCycleEngines"/> - <body><![CDATA[ - let selectedButton = this.selectedButton; - let buttons = this.getSelectableButtons(aCycleEngines); - - if (selectedButton) { - // cycle through one-off buttons. - let index = buttons.indexOf(selectedButton); - if (aForward) - ++index; - else - --index; - - if (index >= 0 && index < buttons.length) - this.selectedButton = buttons[index]; - else - this.selectedButton = null; - - if (this.selectedButton || aWrapAround) - return true; - - return false; - } - - // If no selection, select the first button or ... - if (aForward) { - this.selectedButton = buttons[0]; - return true; - } - - if (!aForward && aWrapAround) { - // the last button. - this.selectedButton = buttons[buttons.length - 1]; - return true; - } - - return false; - ]]></body> - </method> - - <!-- - This handles key presses specific to the one-off buttons like Tab and - Alt-Up/Down, and Up/Down keys within the buttons. Since one-off buttons - are always used in conjunction with a list of some sort (in this.popup), - it also handles Up/Down keys that cross the boundaries between list - items and the one-off buttons. - - @param event - The key event. - @param numListItems - The number of items in the list. The reason that this is a - parameter at all is that the list may contain items at the end - that should be ignored, depending on the consumer. That's true - for the urlbar for example. - @param allowEmptySelection - Pass true if it's OK that neither the list nor the one-off - buttons contains a selection. Pass false if either the list or - the one-off buttons (or both) should always contain a selection. - @param textboxUserValue - When the last list item is selected and the user presses Down, - the first one-off becomes selected and the textbox value is - restored to the value that the user typed. Pass that value here. - However, if you pass true for allowEmptySelection, you don't need - to pass anything for this parameter. (Pass undefined or null.) - @return True if this method handled the keypress and false if not. If - false, then you should let the autocomplete controller handle - the keypress. The value of event.defaultPrevented will be the - same as this return value. - --> - <method name="handleKeyPress"> - <parameter name="event"/> - <parameter name="numListItems"/> - <parameter name="allowEmptySelection"/> - <parameter name="textboxUserValue"/> - <body><![CDATA[ - if (!this.popup) { - return false; - } - - let stopEvent = false; - - // Tab cycles through the one-offs and moves the focus out at the end. - // But only if non-Shift modifiers aren't also pressed, to avoid - // clobbering other shortcuts. - if (event.keyCode == KeyEvent.DOM_VK_TAB && - !event.altKey && - !event.ctrlKey && - !event.metaKey && - this.getAttribute("disabletab") != "true") { - stopEvent = this.advanceSelection(!event.shiftKey, false, true); - } - - // Alt + up/down is very similar to (shift +) tab but differs in that - // it loops through the list, whereas tab will move the focus out. - else if (event.altKey && - (event.keyCode == KeyEvent.DOM_VK_DOWN || - event.keyCode == KeyEvent.DOM_VK_UP)) { - stopEvent = - this.advanceSelection(event.keyCode == KeyEvent.DOM_VK_DOWN, - true, false); - } - - else if (event.keyCode == Ci.nsIDOMKeyEvent.DOM_VK_UP) { - if (numListItems > 0) { - if (this.popup.selectedIndex > 0) { - // The autocomplete controller should handle this case. - } else if (this.popup.selectedIndex == 0) { - if (!allowEmptySelection) { - // Wrap around the selection to the last one-off. - this.selectedButton = null; - this.popup.selectedIndex = -1; - // Call advanceSelection after setting selectedIndex so that - // screen readers see the newly selected one-off. Both trigger - // accessibility events. - this.advanceSelection(false, true, true); - stopEvent = true; - } - } else { - let firstButtonSelected = - this.selectedButton && - this.selectedButton == this.getSelectableButtons(true)[0]; - if (firstButtonSelected) { - this.selectedButton = null; - } else { - stopEvent = this.advanceSelection(false, true, true); - } - } - } else { - stopEvent = this.advanceSelection(false, true, true); - } - } - - else if (event.keyCode == Ci.nsIDOMKeyEvent.DOM_VK_DOWN) { - if (numListItems > 0) { - if (this.popup.selectedIndex >= 0 && - this.popup.selectedIndex < numListItems - 1) { - // The autocomplete controller should handle this case. - } else if (this.popup.selectedIndex == numListItems - 1) { - this.selectedButton = null; - if (!allowEmptySelection) { - this.popup.selectedIndex = -1; - stopEvent = true; - } - if (this.textbox && typeof(textboxUserValue) == "string") { - this.textbox.value = textboxUserValue; - } - // Call advanceSelection after setting selectedIndex so that - // screen readers see the newly selected one-off. Both trigger - // accessibility events. - this.advanceSelection(true, true, true); - } else { - let buttons = this.getSelectableButtons(true); - let lastButtonSelected = - this.selectedButton && - this.selectedButton == buttons[buttons.length - 1]; - if (lastButtonSelected) { - this.selectedButton = null; - stopEvent = allowEmptySelection; - } else if (this.selectedButton) { - stopEvent = this.advanceSelection(true, true, true); - } else { - // The autocomplete controller should handle this case. - } - } - } else { - stopEvent = this.advanceSelection(true, true, true); - } - } - - if (stopEvent) { - event.preventDefault(); - event.stopPropagation(); - return true; - } - return false; - ]]></body> - </method> - - <!-- - If the given event is related to the one-offs, this method records - one-off telemetry for it. this.telemetryOrigin will be appended to the - computed source, so make sure you set that first. - - @param aEvent - An event, like a click on a one-off button. - @param aOpenUILinkWhere - The "where" passed to openUILink. - @param aOpenUILinkParams - The "params" passed to openUILink. - @return True if telemetry was recorded and false if not. - --> - <method name="maybeRecordTelemetry"> - <parameter name="aEvent"/> - <parameter name="aOpenUILinkWhere"/> - <parameter name="aOpenUILinkParams"/> - <body><![CDATA[ - if (!aEvent) { - return false; - } - - let source = null; - let type = "unknown"; - let engine = null; - let target = aEvent.originalTarget; - - if (aEvent instanceof KeyboardEvent) { - type = "key"; - if (this.selectedButton) { - source = "oneoff"; - engine = this.selectedButton.engine; - } - } else if (aEvent instanceof MouseEvent) { - type = "mouse"; - if (target.classList.contains("searchbar-engine-one-off-item")) { - source = "oneoff"; - engine = target.engine; - } - } else if ((aEvent instanceof XULCommandEvent) && - target.getAttribute("anonid") == - "search-one-offs-context-open-in-new-tab") { - source = "oneoff-context"; - engine = this._contextEngine; - } - - if (!source) { - return false; - } - - if (this.telemetryOrigin) { - source += "-" + this.telemetryOrigin; - } - - let tabBackground = aOpenUILinkWhere == "tab" && - aOpenUILinkParams && - aOpenUILinkParams.inBackground; - let where = tabBackground ? "tab-background" : aOpenUILinkWhere; - BrowserSearch.recordOneoffSearchInTelemetry(engine, source, type, - where); - return true; - ]]></body> - </method> - - </implementation> - - <handlers> - - <handler event="mousedown"><![CDATA[ - // Required to receive click events from the buttons on Linux. - event.preventDefault(); - ]]></handler> - - <handler event="mousemove"><![CDATA[ - let target = event.originalTarget; - if (target.localName != "button") - return; - - // Ignore mouse events when the context menu is open. - if (this._ignoreMouseEvents) - return; - - if ((target.classList.contains("searchbar-engine-one-off-item") && - !target.classList.contains("dummy")) || - target.classList.contains("addengine-item") || - target.classList.contains("search-setting-button")) { - this._changeVisuallySelectedButton(target); - } - ]]></handler> - - <handler event="mouseout"><![CDATA[ - let target = event.originalTarget; - if (target.localName != "button") { - return; - } - - // Don't deselect the current button if the context menu is open. - if (this._ignoreMouseEvents) - return; - - // Unfortunately this will fire before mouseover hits another item. - // If this button is selected, we replace that selection only if - // we're not moving to a different one-off item: - if (target.getAttribute("selected") == "true" && - (!event.relatedTarget || - !event.relatedTarget.classList.contains("searchbar-engine-one-off-item") || - event.relatedTarget.classList.contains("dummy"))) { - this._changeVisuallySelectedButton(this.selectedButton); - } - ]]></handler> - - <handler event="click"><![CDATA[ - if (event.button == 2) - return; // ignore right clicks. - - let button = event.originalTarget; - let engine = button.engine; - - if (!engine) - return; - - // Select the clicked button so that consumers can easily tell which - // button was acted on. - this.selectedButton = button; - this.handleSearchCommand(event, engine); - ]]></handler> - - <handler event="command"><![CDATA[ - let target = event.originalTarget; - if (target.classList.contains("addengine-item")) { - // On success, hide the panel and tell event listeners to reshow it to - // show the new engine. - let installCallback = { - onSuccess: engine => { - this._rebuild(); - }, - onError: function(errorCode) { - if (errorCode != Ci.nsISearchInstallCallback.ERROR_DUPLICATE_ENGINE) { - // Download error is shown by the search service - return; - } - const kSearchBundleURI = "chrome://global/locale/search/search.properties"; - let searchBundle = Services.strings.createBundle(kSearchBundleURI); - let brandBundle = document.getElementById("bundle_brand"); - let brandName = brandBundle.getString("brandShortName"); - let title = searchBundle.GetStringFromName("error_invalid_engine_title"); - let text = searchBundle.formatStringFromName("error_duplicate_engine_msg", - [brandName, target.getAttribute("uri")], 2); - Services.prompt.QueryInterface(Ci.nsIPromptFactory); - let prompt = Services.prompt.getPrompt(gBrowser.contentWindow, Ci.nsIPrompt); - prompt.QueryInterface(Ci.nsIWritablePropertyBag2); - prompt.setPropertyAsBool("allowTabModal", true); - prompt.alert(title, text); - } - } - Services.search.addEngine(target.getAttribute("uri"), null, - target.getAttribute("image"), false, - installCallback); - } - let anonid = target.getAttribute("anonid"); - if (anonid == "search-one-offs-context-open-in-new-tab") { - // Select the context-clicked button so that consumers can easily - // tell which button was acted on. - this.selectedButton = this._buttonForEngine(this._contextEngine); - this.handleSearchCommand(event, this._contextEngine, true); - } - if (anonid == "search-one-offs-context-set-default") { - let currentEngine = Services.search.currentEngine; - - if (!this.getAttribute("includecurrentengine")) { - // Make the target button of the context menu reflect the current - // search engine first. Doing this as opposed to rebuilding all the - // one-off buttons avoids flicker. - let button = this._buttonForEngine(this._contextEngine); - button.id = this._buttonIDForEngine(currentEngine); - let uri = "chrome://browser/skin/search-engine-placeholder.png"; - if (currentEngine.iconURI) - uri = currentEngine.iconURI.spec; - button.setAttribute("image", uri); - button.setAttribute("tooltiptext", currentEngine.name); - button.engine = currentEngine; - } - - Services.search.currentEngine = this._contextEngine; - } - ]]></handler> - - <handler event="contextmenu"><![CDATA[ - let target = event.originalTarget; - // Prevent the context menu from appearing except on the one off buttons. - if (!target.classList.contains("searchbar-engine-one-off-item") || - target.classList.contains("dummy")) { - event.preventDefault(); - return; - } - document.getAnonymousElementByAttribute(this, "anonid", "search-one-offs-context-set-default") - .setAttribute("disabled", target.engine == Services.search.currentEngine); - - this._contextEngine = target.engine; - ]]></handler> - </handlers> - - </binding> - -</bindings> diff --git a/application/basilisk/components/search/content/searchReset.js b/application/basilisk/components/search/content/searchReset.js deleted file mode 100644 index b541d41da..000000000 --- a/application/basilisk/components/search/content/searchReset.js +++ /dev/null @@ -1,90 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -"use strict"; - -var {classes: Cc, interfaces: Ci, utils: Cu} = Components; - -Cu.import("resource://gre/modules/Services.jsm"); - -const TELEMETRY_RESULT_ENUM = { - RESTORED_DEFAULT: 0, - KEPT_CURRENT: 1, - CHANGED_ENGINE: 2, - CLOSED_PAGE: 3, - OPENED_SETTINGS: 4 -}; - -window.onload = function() { - let defaultEngine = document.getElementById("defaultEngine"); - let originalDefault = Services.search.originalDefaultEngine; - defaultEngine.textContent = originalDefault.name; - defaultEngine.style.backgroundImage = - 'url("' + originalDefault.iconURI.spec + '")'; - - document.getElementById("searchResetChangeEngine").focus(); - window.addEventListener("unload", recordPageClosed); - document.getElementById("linkSettingsPage") - .addEventListener("click", openingSettings); -}; - -function doSearch() { - let queryString = ""; - let purpose = ""; - let params = window.location.href.match(/^about:searchreset\?([^#]*)/); - if (params) { - params = params[1].split("&"); - for (let param of params) { - if (param.startsWith("data=")) - queryString = decodeURIComponent(param.slice(5)); - else if (param.startsWith("purpose=")) - purpose = param.slice(8); - } - } - - let engine = Services.search.currentEngine; - let submission = engine.getSubmission(queryString, null, purpose); - - window.removeEventListener("unload", recordPageClosed); - - let win = window.QueryInterface(Ci.nsIInterfaceRequestor) - .getInterface(Ci.nsIWebNavigation) - .QueryInterface(Ci.nsIDocShellTreeItem) - .rootTreeItem - .QueryInterface(Ci.nsIInterfaceRequestor) - .getInterface(Ci.nsIDOMWindow); - win.openUILinkIn(submission.uri.spec, "current", false, submission.postData); -} - -function openingSettings() { - record(TELEMETRY_RESULT_ENUM.OPENED_SETTINGS); - window.removeEventListener("unload", recordPageClosed); -} - -function record(result) { - Services.telemetry.getHistogramById("SEARCH_RESET_RESULT").add(result); -} - -function keepCurrentEngine() { - // Calling the currentEngine setter will force a correct loadPathHash to be - // written for this engine, so that we don't prompt the user again. - Services.search.currentEngine = Services.search.currentEngine; - record(TELEMETRY_RESULT_ENUM.KEPT_CURRENT); - doSearch(); -} - -function changeSearchEngine() { - let engine = Services.search.originalDefaultEngine; - if (engine.hidden) - engine.hidden = false; - Services.search.currentEngine = engine; - - record(TELEMETRY_RESULT_ENUM.RESTORED_DEFAULT); - - doSearch(); -} - -function recordPageClosed() { - record(TELEMETRY_RESULT_ENUM.CLOSED_PAGE); -} diff --git a/application/basilisk/components/search/content/searchReset.xhtml b/application/basilisk/components/search/content/searchReset.xhtml deleted file mode 100644 index b851dd383..000000000 --- a/application/basilisk/components/search/content/searchReset.xhtml +++ /dev/null @@ -1,61 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> - -<!-- This Source Code Form is subject to the terms of the Mozilla Public - - License, v. 2.0. If a copy of the MPL was not distributed with this - - file, You can obtain one at http://mozilla.org/MPL/2.0/. --> - -<!DOCTYPE html [ - <!ENTITY % htmlDTD PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> - %htmlDTD; - <!ENTITY % globalDTD SYSTEM "chrome://global/locale/global.dtd"> - %globalDTD; - <!ENTITY % searchresetDTD SYSTEM "chrome://browser/locale/aboutSearchReset.dtd"> - %searchresetDTD; - <!ENTITY % brandDTD SYSTEM "chrome://branding/locale/brand.dtd"> - %brandDTD; -]> - -<html xmlns="http://www.w3.org/1999/xhtml" - xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> - <head> - <title>&searchreset.tabtitle;</title> - <link rel="stylesheet" type="text/css" media="all" - href="chrome://global/skin/in-content/info-pages.css"/> - <link rel="stylesheet" type="text/css" media="all" - href="chrome://browser/skin/searchReset.css"/> - <link rel="icon" type="image/png" - href="chrome://browser/skin/favicon-search-16.svg"/> - - <script type="application/javascript;version=1.8" - src="chrome://browser/content/search/searchReset.js"/> - </head> - - <body dir="&locale.dir;"> - - <div class="container"> - <div class="title"> - <h1 class="title-text">&searchreset.pageTitle;</h1> - </div> - - <div class="description"> - <p>&searchreset.pageInfo1;</p> - <p>&searchreset.selector.label;<span id="defaultEngine"/></p> - - <p>&searchreset.beforelink.pageInfo2;<a id="linkSettingsPage" href="about:preferences#search">&searchreset.link.pageInfo2;</a>&searchreset.afterlink.pageInfo2;</p> - </div> - - <div class="button-container"> - <xul:button id="searchResetKeepCurrent" - label="&searchreset.noChangeButton;" - accesskey="&searchreset.noChangeButton.access;" - oncommand="keepCurrentEngine();"/> - <xul:button class="primary" - id="searchResetChangeEngine" - label="&searchreset.changeEngineButton;" - accesskey="&searchreset.changeEngineButton.access;" - oncommand="changeSearchEngine();"/> - </div> - </div> - - </body> -</html> diff --git a/application/basilisk/components/search/content/searchbarBindings.css b/application/basilisk/components/search/content/searchbarBindings.css deleted file mode 100644 index 0429e8811..000000000 --- a/application/basilisk/components/search/content/searchbarBindings.css +++ /dev/null @@ -1,18 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -@namespace url("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"); - -.searchbar-textbox { - -moz-binding: url("chrome://browser/content/search/search.xml#searchbar-textbox"); -} - -.search-one-offs { - -moz-binding: url("chrome://browser/content/search/search.xml#search-one-offs"); -} - -.search-setting-button[compact=true], -.search-setting-button-compact:not([compact=true]) { - display: none; -} diff --git a/application/basilisk/components/search/jar.mn b/application/basilisk/components/search/jar.mn deleted file mode 100644 index 089ec4bb9..000000000 --- a/application/basilisk/components/search/jar.mn +++ /dev/null @@ -1,9 +0,0 @@ -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. - -browser.jar: - content/browser/search/search.xml (content/search.xml) - content/browser/search/searchbarBindings.css (content/searchbarBindings.css) - content/browser/search/searchReset.xhtml (content/searchReset.xhtml) - content/browser/search/searchReset.js (content/searchReset.js) diff --git a/application/basilisk/components/search/moz.build b/application/basilisk/components/search/moz.build deleted file mode 100644 index b406d5f16..000000000 --- a/application/basilisk/components/search/moz.build +++ /dev/null @@ -1,9 +0,0 @@ -# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- -# vim: set filetype=python: -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. - -DIRS += ['service'] - -JAR_MANIFESTS += ['jar.mn'] diff --git a/application/basilisk/components/search/service/SearchStaticData.jsm b/application/basilisk/components/search/service/SearchStaticData.jsm deleted file mode 100644 index de2be695c..000000000 --- a/application/basilisk/components/search/service/SearchStaticData.jsm +++ /dev/null @@ -1,43 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -/* - * This module contains additional data about default search engines that is the - * same across all languages. This information is defined outside of the actual - * search engine definition files, so that localizers don't need to update them - * when a change is made. - * - * This separate module is also easily overridable, in case a hotfix is needed. - * No high-level processing logic is applied here. - */ - -"use strict"; - -this.EXPORTED_SYMBOLS = [ - "SearchStaticData", -]; - -const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components; - -// To update this list of known alternate domains, just cut-and-paste from -// https://www.google.com/supported_domains -const gGoogleDomainsSource = ".google.com .google.ad .google.ae .google.com.af .google.com.ag .google.com.ai .google.al .google.am .google.co.ao .google.com.ar .google.as .google.at .google.com.au .google.az .google.ba .google.com.bd .google.be .google.bf .google.bg .google.com.bh .google.bi .google.bj .google.com.bn .google.com.bo .google.com.br .google.bs .google.bt .google.co.bw .google.by .google.com.bz .google.ca .google.cd .google.cf .google.cg .google.ch .google.ci .google.co.ck .google.cl .google.cm .google.cn .google.com.co .google.co.cr .google.com.cu .google.cv .google.com.cy .google.cz .google.de .google.dj .google.dk .google.dm .google.com.do .google.dz .google.com.ec .google.ee .google.com.eg .google.es .google.com.et .google.fi .google.com.fj .google.fm .google.fr .google.ga .google.ge .google.gg .google.com.gh .google.com.gi .google.gl .google.gm .google.gp .google.gr .google.com.gt .google.gy .google.com.hk .google.hn .google.hr .google.ht .google.hu .google.co.id .google.ie .google.co.il .google.im .google.co.in .google.iq .google.is .google.it .google.je .google.com.jm .google.jo .google.co.jp .google.co.ke .google.com.kh .google.ki .google.kg .google.co.kr .google.com.kw .google.kz .google.la .google.com.lb .google.li .google.lk .google.co.ls .google.lt .google.lu .google.lv .google.com.ly .google.co.ma .google.md .google.me .google.mg .google.mk .google.ml .google.com.mm .google.mn .google.ms .google.com.mt .google.mu .google.mv .google.mw .google.com.mx .google.com.my .google.co.mz .google.com.na .google.com.nf .google.com.ng .google.com.ni .google.ne .google.nl .google.no .google.com.np .google.nr .google.nu .google.co.nz .google.com.om .google.com.pa .google.com.pe .google.com.pg .google.com.ph .google.com.pk .google.pl .google.pn .google.com.pr .google.ps .google.pt .google.com.py .google.com.qa .google.ro .google.ru .google.rw .google.com.sa .google.com.sb .google.sc .google.se .google.com.sg .google.sh .google.si .google.sk .google.com.sl .google.sn .google.so .google.sm .google.sr .google.st .google.com.sv .google.td .google.tg .google.co.th .google.com.tj .google.tk .google.tl .google.tm .google.tn .google.to .google.com.tr .google.tt .google.com.tw .google.co.tz .google.com.ua .google.co.ug .google.co.uk .google.com.uy .google.co.uz .google.com.vc .google.co.ve .google.vg .google.co.vi .google.com.vn .google.vu .google.ws .google.rs .google.co.za .google.co.zm .google.co.zw .google.cat"; -const gGoogleDomains = gGoogleDomainsSource.split(" ").map(d => "www" + d); - -this.SearchStaticData = { - /** - * Returns a list of alternate domains for a given search engine domain. - * - * @param aDomain - * Lowercase host name to look up. For example, if this argument is - * "www.google.com" or "www.google.co.uk", the function returns the - * full list of supported Google domains. - * - * @return Array containing one entry for each alternate host name, or empty - * array if none is known. The returned array should not be modified. - */ - getAlternateDomains: function (aDomain) { - return gGoogleDomains.indexOf(aDomain) == -1 ? [] : gGoogleDomains; - }, -}; diff --git a/application/basilisk/components/search/service/SearchSuggestionController.jsm b/application/basilisk/components/search/service/SearchSuggestionController.jsm deleted file mode 100644 index 952838c0c..000000000 --- a/application/basilisk/components/search/service/SearchSuggestionController.jsm +++ /dev/null @@ -1,398 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -"use strict"; - -this.EXPORTED_SYMBOLS = ["SearchSuggestionController"]; - -const { classes: Cc, interfaces: Ci, utils: Cu } = Components; - -Cu.import("resource://gre/modules/XPCOMUtils.jsm"); -Cu.import("resource://gre/modules/Services.jsm"); -Cu.import("resource://gre/modules/Promise.jsm"); -XPCOMUtils.defineLazyModuleGetter(this, "NS_ASSERT", "resource://gre/modules/debug.js"); - -const SEARCH_RESPONSE_SUGGESTION_JSON = "application/x-suggestions+json"; -const DEFAULT_FORM_HISTORY_PARAM = "searchbar-history"; -const HTTP_OK = 200; -const REMOTE_TIMEOUT = 500; // maximum time (ms) to wait before giving up on a remote suggestions -const BROWSER_SUGGEST_PREF = "browser.search.suggest.enabled"; - -/** - * Remote search suggestions will be shown if gRemoteSuggestionsEnabled - * is true. Global because only one pref observer is needed for all instances. - */ -var gRemoteSuggestionsEnabled = Services.prefs.getBoolPref(BROWSER_SUGGEST_PREF); -Services.prefs.addObserver(BROWSER_SUGGEST_PREF, function(aSubject, aTopic, aData) { - gRemoteSuggestionsEnabled = Services.prefs.getBoolPref(BROWSER_SUGGEST_PREF); -}, false); - -/** - * SearchSuggestionController.jsm exists as a helper module to allow multiple consumers to request and display - * search suggestions from a given engine, regardless of the base implementation. Much of this - * code was originally in nsSearchSuggestions.js until it was refactored to separate it from the - * nsIAutoCompleteSearch dependency. - * One instance of SearchSuggestionController should be used per field since form history results are cached. - */ - -/** - * @param {function} [callback] - Callback for search suggestion results. You can use the promise - * returned by the search method instead if you prefer. - * @constructor - */ -this.SearchSuggestionController = function SearchSuggestionController(callback = null) { - this._callback = callback; -}; - -this.SearchSuggestionController.prototype = { - /** - * The maximum number of local form history results to return. This limit is - * only enforced if remote results are also returned. - */ - maxLocalResults: 5, - - /** - * The maximum number of remote search engine results to return. - * We'll actually only display at most - * maxRemoteResults - <displayed local results count> remote results. - */ - maxRemoteResults: 10, - - /** - * The maximum time (ms) to wait before giving up on a remote suggestions. - */ - remoteTimeout: REMOTE_TIMEOUT, - - /** - * The additional parameter used when searching form history. - */ - formHistoryParam: DEFAULT_FORM_HISTORY_PARAM, - - // Private properties - /** - * The last form history result used to improve the performance of subsequent searches. - * This shouldn't be used for any other purpose as it is never cleared and therefore could be stale. - */ - _formHistoryResult: null, - - /** - * The remote server timeout timer, if applicable. The timer starts when form history - * search is completed. - */ - _remoteResultTimer: null, - - /** - * The deferred for the remote results before its promise is resolved. - */ - _deferredRemoteResult: null, - - /** - * The optional result callback registered from the constructor. - */ - _callback: null, - - /** - * The XMLHttpRequest object for remote results. - */ - _request: null, - - // Public methods - - /** - * Fetch search suggestions from all of the providers. Fetches in progress will be stopped and - * results from them will not be provided. - * - * @param {string} searchTerm - the term to provide suggestions for - * @param {bool} privateMode - whether the request is being made in the context of private browsing - * @param {nsISearchEngine} engine - search engine for the suggestions. - * @param {int} userContextId - the userContextId of the selected tab. - * - * @return {Promise} resolving to an object containing results or null. - */ - fetch: function(searchTerm, privateMode, engine, userContextId) { - // There is no smart filtering from previous results here (as there is when looking through - // history/form data) because the result set returned by the server is different for every typed - // value - e.g. "ocean breathes" does not return a subset of the results returned for "ocean". - - this.stop(); - - if (!Services.search.isInitialized) { - throw new Error("Search not initialized yet (how did you get here?)"); - } - if (typeof privateMode === "undefined") { - throw new Error("The privateMode argument is required to avoid unintentional privacy leaks"); - } - if (!(engine instanceof Ci.nsISearchEngine)) { - throw new Error("Invalid search engine"); - } - if (!this.maxLocalResults && !this.maxRemoteResults) { - throw new Error("Zero results expected, what are you trying to do?"); - } - if (this.maxLocalResults < 0 || this.maxRemoteResults < 0) { - throw new Error("Number of requested results must be positive"); - } - - // Array of promises to resolve before returning results. - let promises = []; - this._searchString = searchTerm; - - // Remote results - if (searchTerm && gRemoteSuggestionsEnabled && this.maxRemoteResults && - engine.supportsResponseType(SEARCH_RESPONSE_SUGGESTION_JSON)) { - this._deferredRemoteResult = this._fetchRemote(searchTerm, engine, privateMode, userContextId); - promises.push(this._deferredRemoteResult.promise); - } - - // Local results from form history - if (this.maxLocalResults) { - let deferredHistoryResult = this._fetchFormHistory(searchTerm); - promises.push(deferredHistoryResult.promise); - } - - function handleRejection(reason) { - if (reason == "HTTP request aborted") { - // Do nothing since this is normal. - return null; - } - Cu.reportError("SearchSuggestionController rejection: " + reason); - return null; - } - return Promise.all(promises).then(this._dedupeAndReturnResults.bind(this), handleRejection); - }, - - /** - * Stop pending fetches so no results are returned from them. - * - * Note: If there was no remote results fetched, the fetching cannot be stopped and local results - * will still be returned because stopping relies on aborting the XMLHTTPRequest to reject the - * promise for Promise.all. - */ - stop: function() { - if (this._request) { - this._request.abort(); - } else if (!this.maxRemoteResults) { - Cu.reportError("SearchSuggestionController: Cannot stop fetching if remote results were not "+ - "requested"); - } - this._reset(); - }, - - // Private methods - - _fetchFormHistory: function(searchTerm) { - let deferredFormHistory = Promise.defer(); - - let acSearchObserver = { - // Implements nsIAutoCompleteSearch - onSearchResult: (search, result) => { - this._formHistoryResult = result; - - if (this._request) { - this._remoteResultTimer = Cc["@mozilla.org/timer;1"]. - createInstance(Ci.nsITimer); - this._remoteResultTimer.initWithCallback(this._onRemoteTimeout.bind(this), - this.remoteTimeout || REMOTE_TIMEOUT, - Ci.nsITimer.TYPE_ONE_SHOT); - } - - switch (result.searchResult) { - case Ci.nsIAutoCompleteResult.RESULT_SUCCESS: - case Ci.nsIAutoCompleteResult.RESULT_NOMATCH: - if (result.searchString !== this._searchString) { - deferredFormHistory.resolve("Unexpected response, this._searchString does not match form history response"); - return; - } - let fhEntries = []; - for (let i = 0; i < result.matchCount; ++i) { - fhEntries.push(result.getValueAt(i)); - } - deferredFormHistory.resolve({ - result: fhEntries, - formHistoryResult: result, - }); - break; - case Ci.nsIAutoCompleteResult.RESULT_FAILURE: - case Ci.nsIAutoCompleteResult.RESULT_IGNORED: - deferredFormHistory.resolve("Form History returned RESULT_FAILURE or RESULT_IGNORED"); - break; - } - }, - }; - - let formHistory = Cc["@mozilla.org/autocomplete/search;1?name=form-history"]. - createInstance(Ci.nsIAutoCompleteSearch); - formHistory.startSearch(searchTerm, this.formHistoryParam || DEFAULT_FORM_HISTORY_PARAM, - this._formHistoryResult, - acSearchObserver); - return deferredFormHistory; - }, - - /** - * Fetch suggestions from the search engine over the network. - */ - _fetchRemote: function(searchTerm, engine, privateMode, userContextId) { - let deferredResponse = Promise.defer(); - this._request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]. - createInstance(Ci.nsIXMLHttpRequest); - let submission = engine.getSubmission(searchTerm, - SEARCH_RESPONSE_SUGGESTION_JSON); - let method = (submission.postData ? "POST" : "GET"); - this._request.open(method, submission.uri.spec, true); - - this._request.setOriginAttributes({userContextId, - privateBrowsingId: privateMode ? 1 : 0}); - - this._request.mozBackgroundRequest = true; // suppress dialogs and fail silently - - this._request.addEventListener("load", this._onRemoteLoaded.bind(this, deferredResponse)); - this._request.addEventListener("error", (evt) => deferredResponse.resolve("HTTP error")); - // Reject for an abort assuming it's always from .stop() in which case we shouldn't return local - // or remote results for existing searches. - this._request.addEventListener("abort", (evt) => deferredResponse.reject("HTTP request aborted")); - - this._request.send(submission.postData); - - return deferredResponse; - }, - - /** - * Called when the request completed successfully (thought the HTTP status could be anything) - * so we can handle the response data. - * @private - */ - _onRemoteLoaded: function(deferredResponse) { - if (!this._request) { - deferredResponse.resolve("Got HTTP response after the request was cancelled"); - return; - } - - let status, serverResults; - try { - status = this._request.status; - } catch (e) { - // The XMLHttpRequest can throw NS_ERROR_NOT_AVAILABLE. - deferredResponse.resolve("Unknown HTTP status: " + e); - return; - } - - if (status != HTTP_OK || this._request.responseText == "") { - deferredResponse.resolve("Non-200 status or empty HTTP response: " + status); - return; - } - - try { - serverResults = JSON.parse(this._request.responseText); - } catch (ex) { - deferredResponse.resolve("Failed to parse suggestion JSON: " + ex); - return; - } - - if (!serverResults[0] || - this._searchString.localeCompare(serverResults[0], undefined, - { sensitivity: "base" })) { - // something is wrong here so drop remote results - deferredResponse.resolve("Unexpected response, this._searchString does not match remote response"); - return; - } - let results = serverResults[1] || []; - deferredResponse.resolve({ result: results }); - }, - - /** - * Called when this._remoteResultTimer fires indicating the remote request took too long. - */ - _onRemoteTimeout: function () { - this._request = null; - - // FIXME: bug 387341 - // Need to break the cycle between us and the timer. - this._remoteResultTimer = null; - - // The XMLHTTPRequest for suggest results is taking too long - // so send out the form history results and cancel the request. - if (this._deferredRemoteResult) { - this._deferredRemoteResult.resolve("HTTP Timeout"); - this._deferredRemoteResult = null; - } - }, - - /** - * @param {Array} suggestResults - an array of result objects from different sources (local or remote) - * @return {Object} - */ - _dedupeAndReturnResults: function(suggestResults) { - if (this._searchString === null) { - // _searchString can be null if stop() was called and remote suggestions - // were disabled (stopping if we are fetching remote suggestions will - // cause a promise rejection before we reach _dedupeAndReturnResults). - return null; - } - - let results = { - term: this._searchString, - remote: [], - local: [], - formHistoryResult: null, - }; - - for (let result of suggestResults) { - if (typeof result === "string") { // Failure message - Cu.reportError("SearchSuggestionController: " + result); - } else if (result.formHistoryResult) { // Local results have a formHistoryResult property. - results.formHistoryResult = result.formHistoryResult; - results.local = result.result || []; - } else { // Remote result - results.remote = result.result || []; - } - } - - // If we have remote results, cap the number of local results - if (results.remote.length) { - results.local = results.local.slice(0, this.maxLocalResults); - } - - // We don't want things to appear in both history and suggestions so remove entries from - // remote results that are already in local. - if (results.remote.length && results.local.length) { - for (let i = 0; i < results.local.length; ++i) { - let term = results.local[i]; - let dupIndex = results.remote.indexOf(term); - if (dupIndex != -1) { - results.remote.splice(dupIndex, 1); - } - } - } - - // Trim the number of results to the maximum requested (now that we've pruned dupes). - results.remote = - results.remote.slice(0, this.maxRemoteResults - results.local.length); - - if (this._callback) { - this._callback(results); - } - this._reset(); - - return results; - }, - - _reset: function() { - this._request = null; - if (this._remoteResultTimer) { - this._remoteResultTimer.cancel(); - this._remoteResultTimer = null; - } - this._deferredRemoteResult = null; - this._searchString = null; - }, -}; - -/** - * Determines whether the given engine offers search suggestions. - * - * @param {nsISearchEngine} engine - The search engine - * @return {boolean} True if the engine offers suggestions and false otherwise. - */ -this.SearchSuggestionController.engineOffersSuggestions = function(engine) { - return engine.supportsResponseType(SEARCH_RESPONSE_SUGGESTION_JSON); -}; diff --git a/application/basilisk/components/search/service/moz.build b/application/basilisk/components/search/service/moz.build deleted file mode 100644 index 423faeffd..000000000 --- a/application/basilisk/components/search/service/moz.build +++ /dev/null @@ -1,24 +0,0 @@ -# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- -# vim: set filetype=python: -# This Source Code Form is subject to the terms of the Mozilla Public -# License, v. 2.0. If a copy of the MPL was not distributed with this -# file, You can obtain one at http://mozilla.org/MPL/2.0/. - -DIST_SUBDIR = '' - -DEFINES['HAVE_SIDEBAR'] = True - -EXTRA_COMPONENTS += [ - 'nsSearchSuggestions.js', - 'nsSidebar.js', -] - -EXTRA_PP_COMPONENTS += [ - 'nsSearchService.js', - 'toolkitsearch.manifest', -] - -EXTRA_JS_MODULES += [ - 'SearchStaticData.jsm', - 'SearchSuggestionController.jsm', -] diff --git a/application/basilisk/components/search/service/nsSearchService.js b/application/basilisk/components/search/service/nsSearchService.js deleted file mode 100644 index b4db31dee..000000000 --- a/application/basilisk/components/search/service/nsSearchService.js +++ /dev/null @@ -1,4324 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -const Ci = Components.interfaces; -const Cc = Components.classes; -const Cr = Components.results; -const Cu = Components.utils; - -Cu.import("resource://gre/modules/XPCOMUtils.jsm"); -Cu.import("resource://gre/modules/Services.jsm"); -Cu.import("resource://gre/modules/Promise.jsm"); -Cu.import("resource://gre/modules/debug.js"); -Cu.import("resource://gre/modules/AppConstants.jsm"); - -XPCOMUtils.defineLazyModuleGetter(this, "AsyncShutdown", - "resource://gre/modules/AsyncShutdown.jsm"); -XPCOMUtils.defineLazyModuleGetter(this, "DeferredTask", - "resource://gre/modules/DeferredTask.jsm"); -XPCOMUtils.defineLazyModuleGetter(this, "OS", - "resource://gre/modules/osfile.jsm"); -XPCOMUtils.defineLazyModuleGetter(this, "Task", - "resource://gre/modules/Task.jsm"); -XPCOMUtils.defineLazyModuleGetter(this, "Deprecated", - "resource://gre/modules/Deprecated.jsm"); -XPCOMUtils.defineLazyModuleGetter(this, "SearchStaticData", - "resource://gre/modules/SearchStaticData.jsm"); -XPCOMUtils.defineLazyModuleGetter(this, "setTimeout", - "resource://gre/modules/Timer.jsm"); -XPCOMUtils.defineLazyModuleGetter(this, "clearTimeout", - "resource://gre/modules/Timer.jsm"); -XPCOMUtils.defineLazyModuleGetter(this, "Lz4", - "resource://gre/modules/lz4.js"); - -XPCOMUtils.defineLazyServiceGetter(this, "gTextToSubURI", - "@mozilla.org/intl/texttosuburi;1", - "nsITextToSubURI"); -XPCOMUtils.defineLazyServiceGetter(this, "gEnvironment", - "@mozilla.org/process/environment;1", - "nsIEnvironment"); - -Cu.importGlobalProperties(["XMLHttpRequest"]); - -// A text encoder to UTF8, used whenever we commit the cache to disk. -XPCOMUtils.defineLazyGetter(this, "gEncoder", - function() { - return new TextEncoder(); - }); - -const MODE_RDONLY = 0x01; -const MODE_WRONLY = 0x02; -const MODE_CREATE = 0x08; -const MODE_APPEND = 0x10; -const MODE_TRUNCATE = 0x20; - -// Directory service keys -const NS_APP_SEARCH_DIR_LIST = "SrchPluginsDL"; -const NS_APP_DISTRIBUTION_SEARCH_DIR_LIST = "SrchPluginsDistDL"; -const NS_APP_USER_SEARCH_DIR = "UsrSrchPlugns"; -const NS_APP_SEARCH_DIR = "SrchPlugns"; -const NS_APP_USER_PROFILE_50_DIR = "ProfD"; - -// Loading plugins from NS_APP_SEARCH_DIR is no longer supported. -// Instead, we now load plugins from APP_SEARCH_PREFIX, where a -// list.txt file needs to exist to list available engines. -const APP_SEARCH_PREFIX = "resource://search-plugins/"; - -// See documentation in nsIBrowserSearchService.idl. -const SEARCH_ENGINE_TOPIC = "browser-search-engine-modified"; -const QUIT_APPLICATION_TOPIC = "quit-application"; - -const SEARCH_ENGINE_REMOVED = "engine-removed"; -const SEARCH_ENGINE_ADDED = "engine-added"; -const SEARCH_ENGINE_CHANGED = "engine-changed"; -const SEARCH_ENGINE_LOADED = "engine-loaded"; -const SEARCH_ENGINE_CURRENT = "engine-current"; -const SEARCH_ENGINE_DEFAULT = "engine-default"; - -// The following constants are left undocumented in nsIBrowserSearchService.idl -// For the moment, they are meant for testing/debugging purposes only. - -/** - * Topic used for events involving the service itself. - */ -const SEARCH_SERVICE_TOPIC = "browser-search-service"; - -/** - * Sent whenever the cache is fully written to disk. - */ -const SEARCH_SERVICE_CACHE_WRITTEN = "write-cache-to-disk-complete"; - -// Delay for lazy serialization (ms) -const LAZY_SERIALIZE_DELAY = 100; - -// Delay for batching invalidation of the JSON cache (ms) -const CACHE_INVALIDATION_DELAY = 1000; - -// Current cache version. This should be incremented if the format of the cache -// file is modified. -const CACHE_VERSION = 1; - -const CACHE_FILENAME = "search.json.mozlz4"; - -const NEW_LINES = /(\r\n|\r|\n)/; - -// Set an arbitrary cap on the maximum icon size. Without this, large icons can -// cause big delays when loading them at startup. -const MAX_ICON_SIZE = 32768; - -// Default charset to use for sending search parameters. ISO-8859-1 is used to -// match previous nsInternetSearchService behavior. -const DEFAULT_QUERY_CHARSET = "ISO-8859-1"; - -const SEARCH_BUNDLE = "chrome://global/locale/search/search.properties"; -const BRAND_BUNDLE = "chrome://branding/locale/brand.properties"; - -const OPENSEARCH_NS_10 = "http://a9.com/-/spec/opensearch/1.0/"; -const OPENSEARCH_NS_11 = "http://a9.com/-/spec/opensearch/1.1/"; - -// Although the specification at http://opensearch.a9.com/spec/1.1/description/ -// gives the namespace names defined above, many existing OpenSearch engines -// are using the following versions. We therefore allow either. -const OPENSEARCH_NAMESPACES = [ - OPENSEARCH_NS_11, OPENSEARCH_NS_10, - "http://a9.com/-/spec/opensearchdescription/1.1/", - "http://a9.com/-/spec/opensearchdescription/1.0/" -]; - -const OPENSEARCH_LOCALNAME = "OpenSearchDescription"; - -const MOZSEARCH_NS_10 = "http://www.mozilla.org/2006/browser/search/"; -const MOZSEARCH_LOCALNAME = "SearchPlugin"; - -const URLTYPE_SUGGEST_JSON = "application/x-suggestions+json"; -const URLTYPE_SEARCH_HTML = "text/html"; -const URLTYPE_OPENSEARCH = "application/opensearchdescription+xml"; - -const BROWSER_SEARCH_PREF = "browser.search."; -const LOCALE_PREF = "general.useragent.locale"; - -const USER_DEFINED = "{searchTerms}"; - -// Custom search parameters -const MOZ_OFFICIAL = AppConstants.MOZ_OFFICIAL_BRANDING ? "official" : "unofficial"; - -const MOZ_PARAM_LOCALE = /\{moz:locale\}/g; -const MOZ_PARAM_DIST_ID = /\{moz:distributionID\}/g; -const MOZ_PARAM_OFFICIAL = /\{moz:official\}/g; - -// Supported OpenSearch parameters -// See http://opensearch.a9.com/spec/1.1/querysyntax/#core -const OS_PARAM_USER_DEFINED = /\{searchTerms\??\}/g; -const OS_PARAM_INPUT_ENCODING = /\{inputEncoding\??\}/g; -const OS_PARAM_LANGUAGE = /\{language\??\}/g; -const OS_PARAM_OUTPUT_ENCODING = /\{outputEncoding\??\}/g; - -// Default values -const OS_PARAM_LANGUAGE_DEF = "*"; -const OS_PARAM_OUTPUT_ENCODING_DEF = "UTF-8"; -const OS_PARAM_INPUT_ENCODING_DEF = "UTF-8"; - -// "Unsupported" OpenSearch parameters. For example, we don't support -// page-based results, so if the engine requires that we send the "page index" -// parameter, we'll always send "1". -const OS_PARAM_COUNT = /\{count\??\}/g; -const OS_PARAM_START_INDEX = /\{startIndex\??\}/g; -const OS_PARAM_START_PAGE = /\{startPage\??\}/g; - -// Default values -const OS_PARAM_COUNT_DEF = "20"; // 20 results -const OS_PARAM_START_INDEX_DEF = "1"; // start at 1st result -const OS_PARAM_START_PAGE_DEF = "1"; // 1st page - -// Optional parameter -const OS_PARAM_OPTIONAL = /\{(?:\w+:)?\w+\?\}/g; - -// A array of arrays containing parameters that we don't fully support, and -// their default values. We will only send values for these parameters if -// required, since our values are just really arbitrary "guesses" that should -// give us the output we want. -var OS_UNSUPPORTED_PARAMS = [ - [OS_PARAM_COUNT, OS_PARAM_COUNT_DEF], - [OS_PARAM_START_INDEX, OS_PARAM_START_INDEX_DEF], - [OS_PARAM_START_PAGE, OS_PARAM_START_PAGE_DEF], -]; - -// The default engine update interval, in days. This is only used if an engine -// specifies an updateURL, but not an updateInterval. -const SEARCH_DEFAULT_UPDATE_INTERVAL = 7; - -// The default interval before checking again for the name of the -// default engine for the region, in seconds. Only used if the response -// from the server doesn't specify an interval. -const SEARCH_GEO_DEFAULT_UPDATE_INTERVAL = 2592000; // 30 days. - -this.__defineGetter__("FileUtils", function() { - delete this.FileUtils; - Components.utils.import("resource://gre/modules/FileUtils.jsm"); - return FileUtils; -}); - -this.__defineGetter__("NetUtil", function() { - delete this.NetUtil; - Components.utils.import("resource://gre/modules/NetUtil.jsm"); - return NetUtil; -}); - -this.__defineGetter__("gChromeReg", function() { - delete this.gChromeReg; - return this.gChromeReg = Cc["@mozilla.org/chrome/chrome-registry;1"]. - getService(Ci.nsIChromeRegistry); -}); - -/** - * Prefixed to all search debug output. - */ -const SEARCH_LOG_PREFIX = "*** Search: "; - -/** - * Outputs aText to the JavaScript console as well as to stdout. - */ -function DO_LOG(aText) { - dump(SEARCH_LOG_PREFIX + aText + "\n"); - Services.console.logStringMessage(aText); -} - -/** - * In debug builds, use a live, pref-based (browser.search.log) LOG function - * to allow enabling/disabling without a restart. Otherwise, don't log at all by - * default. This can be overridden at startup by the pref, see SearchService's - * _init method. - */ -var LOG = function() {}; - -if (AppConstants.DEBUG) { - LOG = function (aText) { - if (Services.prefs.getBoolPref(BROWSER_SEARCH_PREF + "log", false)) { - DO_LOG(aText); - } - }; -} - -/** - * Presents an assertion dialog in non-release builds and throws. - * @param message - * A message to display - * @param resultCode - * The NS_ERROR_* value to throw. - * @throws resultCode - */ -function ERROR(message, resultCode) { - NS_ASSERT(false, SEARCH_LOG_PREFIX + message); - throw Components.Exception(message, resultCode); -} - -/** - * Logs the failure message (if browser.search.log is enabled) and throws. - * @param message - * A message to display - * @param resultCode - * The NS_ERROR_* value to throw. - * @throws resultCode or NS_ERROR_INVALID_ARG if resultCode isn't specified. - */ -function FAIL(message, resultCode) { - LOG(message); - throw Components.Exception(message, resultCode || Cr.NS_ERROR_INVALID_ARG); -} - -/** - * Truncates big blobs of (data-)URIs to console-friendly sizes - * @param str - * String to tone down - * @param len - * Maximum length of the string to return. Defaults to the length of a tweet. - */ -function limitURILength(str, len) { - len = len || 140; - if (str.length > len) - return str.slice(0, len) + "..."; - return str; -} - -/** - * Ensures an assertion is met before continuing. Should be used to indicate - * fatal errors. - * @param assertion - * An assertion that must be met - * @param message - * A message to display if the assertion is not met - * @param resultCode - * The NS_ERROR_* value to throw if the assertion is not met - * @throws resultCode - */ -function ENSURE_WARN(assertion, message, resultCode) { - NS_ASSERT(assertion, SEARCH_LOG_PREFIX + message); - if (!assertion) - throw Components.Exception(message, resultCode); -} - -function loadListener(aChannel, aEngine, aCallback) { - this._channel = aChannel; - this._bytes = []; - this._engine = aEngine; - this._callback = aCallback; -} -loadListener.prototype = { - _callback: null, - _channel: null, - _countRead: 0, - _engine: null, - _stream: null, - - QueryInterface: XPCOMUtils.generateQI([ - Ci.nsIRequestObserver, - Ci.nsIStreamListener, - Ci.nsIChannelEventSink, - Ci.nsIInterfaceRequestor, - // See FIXME comment below. - Ci.nsIHttpEventSink, - Ci.nsIProgressEventSink - ]), - - // nsIRequestObserver - onStartRequest: function SRCH_loadStartR(aRequest, aContext) { - LOG("loadListener: Starting request: " + aRequest.name); - this._stream = Cc["@mozilla.org/binaryinputstream;1"]. - createInstance(Ci.nsIBinaryInputStream); - }, - - onStopRequest: function SRCH_loadStopR(aRequest, aContext, aStatusCode) { - LOG("loadListener: Stopping request: " + aRequest.name); - - var requestFailed = !Components.isSuccessCode(aStatusCode); - if (!requestFailed && (aRequest instanceof Ci.nsIHttpChannel)) - requestFailed = !aRequest.requestSucceeded; - - if (requestFailed || this._countRead == 0) { - LOG("loadListener: request failed!"); - // send null so the callback can deal with the failure - this._callback(null, this._engine); - } else - this._callback(this._bytes, this._engine); - this._channel = null; - this._engine = null; - }, - - // nsIStreamListener - onDataAvailable: function SRCH_loadDAvailable(aRequest, aContext, - aInputStream, aOffset, - aCount) { - this._stream.setInputStream(aInputStream); - - // Get a byte array of the data - this._bytes = this._bytes.concat(this._stream.readByteArray(aCount)); - this._countRead += aCount; - }, - - // nsIChannelEventSink - asyncOnChannelRedirect: function SRCH_loadCRedirect(aOldChannel, aNewChannel, - aFlags, callback) { - this._channel = aNewChannel; - callback.onRedirectVerifyCallback(Components.results.NS_OK); - }, - - // nsIInterfaceRequestor - getInterface: function SRCH_load_GI(aIID) { - return this.QueryInterface(aIID); - }, - - // FIXME: bug 253127 - // nsIHttpEventSink - onRedirect: function (aChannel, aNewChannel) {}, - // nsIProgressEventSink - onProgress: function (aRequest, aContext, aProgress, aProgressMax) {}, - onStatus: function (aRequest, aContext, aStatus, aStatusArg) {} -} - -function isPartnerBuild() { - try { - let distroID = Services.prefs.getCharPref("distribution.id"); - - // Mozilla-provided builds (i.e. funnelcake) are not partner builds - if (distroID && !distroID.startsWith("mozilla")) { - return true; - } - } catch (e) {} - - return false; -} - -function getVerificationHash(aName) { - let disclaimer = "By modifying this file, I agree that I am doing so " + - "only within $appName itself, using official, user-driven search " + - "engine selection processes, and in a way which does not circumvent " + - "user consent. I acknowledge that any attempt to change this file " + - "from outside of $appName is a malicious act, and will be responded " + - "to accordingly." - - let salt = OS.Path.basename(OS.Constants.Path.profileDir) + aName + - disclaimer.replace(/\$appName/g, Services.appinfo.name); - - let converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"] - .createInstance(Ci.nsIScriptableUnicodeConverter); - converter.charset = "UTF-8"; - - // Data is an array of bytes. - let data = converter.convertToByteArray(salt, {}); - let hasher = Cc["@mozilla.org/security/hash;1"] - .createInstance(Ci.nsICryptoHash); - hasher.init(hasher.SHA256); - hasher.update(data, data.length); - - return hasher.finish(true); -} - -/** - * Safely close a nsISafeOutputStream. - * @param aFOS - * The file output stream to close. - */ -function closeSafeOutputStream(aFOS) { - if (aFOS instanceof Ci.nsISafeOutputStream) { - try { - aFOS.finish(); - return; - } catch (e) { } - } - aFOS.close(); -} - -/** - * Wrapper function for nsIIOService::newURI. - * @param aURLSpec - * The URL string from which to create an nsIURI. - * @returns an nsIURI object, or null if the creation of the URI failed. - */ -function makeURI(aURLSpec, aCharset) { - try { - return NetUtil.newURI(aURLSpec, aCharset); - } catch (ex) { } - - return null; -} - -/** - * Wrapper function for nsIIOService::newChannel2. - * @param url - * The URL string from which to create an nsIChannel. - * @returns an nsIChannel object, or null if the url is invalid. - */ -function makeChannel(url) { - try { - return NetUtil.newChannel({ - uri: url, - loadUsingSystemPrincipal: true - }); - } catch (ex) { } - - return null; -} - -/** - * Gets a directory from the directory service. - * @param aKey - * The directory service key indicating the directory to get. - */ -function getDir(aKey, aIFace) { - if (!aKey) - FAIL("getDir requires a directory key!"); - - return Services.dirsvc.get(aKey, aIFace || Ci.nsIFile); -} - -/** - * Gets the current value of the locale. It's possible for this preference to - * be localized, so we have to do a little extra work here. Similar code - * exists in nsHttpHandler.cpp when building the UA string. - */ -function getLocale() { - let locale = getLocalizedPref(LOCALE_PREF); - if (locale) - return locale; - - // Not localized. - return Services.prefs.getCharPref(LOCALE_PREF); -} - -/** - * Wrapper for nsIPrefBranch::getComplexValue. - * @param aPrefName - * The name of the pref to get. - * @returns aDefault if the requested pref doesn't exist. - */ -function getLocalizedPref(aPrefName, aDefault) { - const nsIPLS = Ci.nsIPrefLocalizedString; - try { - return Services.prefs.getComplexValue(aPrefName, nsIPLS).data; - } catch (ex) {} - - return aDefault; -} - -/** - * @return a sanitized name to be used as a filename, or a random name - * if a sanitized name cannot be obtained (if aName contains - * no valid characters). - */ -function sanitizeName(aName) { - const maxLength = 60; - const minLength = 1; - var name = aName.toLowerCase(); - name = name.replace(/\s+/g, "-"); - name = name.replace(/[^-a-z0-9]/g, ""); - - // Use a random name if our input had no valid characters. - if (name.length < minLength) - name = Math.random().toString(36).replace(/^.*\./, ''); - - // Force max length. - return name.substring(0, maxLength); -} - -/** - * Retrieve a pref from the search param branch. - * - * @param prefName - * The name of the pref. - **/ -function getMozParamPref(prefName) { - let branch = Services.prefs.getDefaultBranch(BROWSER_SEARCH_PREF + "param."); - return encodeURIComponent(branch.getCharPref(prefName)); -} - -/** - * Notifies watchers of SEARCH_ENGINE_TOPIC about changes to an engine or to - * the state of the search service. - * - * @param aEngine - * The nsISearchEngine object to which the change applies. - * @param aVerb - * A verb describing the change. - * - * @see nsIBrowserSearchService.idl - */ -var gInitialized = false; -function notifyAction(aEngine, aVerb) { - if (gInitialized) { - LOG("NOTIFY: Engine: \"" + aEngine.name + "\"; Verb: \"" + aVerb + "\""); - Services.obs.notifyObservers(aEngine, SEARCH_ENGINE_TOPIC, aVerb); - } -} - -function parseJsonFromStream(aInputStream) { - const json = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON); - const data = json.decodeFromStream(aInputStream, aInputStream.available()); - return data; -} - -/** - * Simple object representing a name/value pair. - */ -function QueryParameter(aName, aValue, aPurpose) { - if (!aName || (aValue == null)) - FAIL("missing name or value for QueryParameter!"); - - this.name = aName; - this.value = aValue; - this.purpose = aPurpose; -} - -/** - * Perform OpenSearch parameter substitution on aParamValue. - * - * @param aParamValue - * A string containing OpenSearch search parameters. - * @param aSearchTerms - * The user-provided search terms. This string will inserted into - * aParamValue as the value of the OS_PARAM_USER_DEFINED parameter. - * This value must already be escaped appropriately - it is inserted - * as-is. - * @param aEngine - * The engine which owns the string being acted on. - * - * @see http://opensearch.a9.com/spec/1.1/querysyntax/#core - */ -function ParamSubstitution(aParamValue, aSearchTerms, aEngine) { - var value = aParamValue; - - var distributionID = Services.prefs.getCharPref(BROWSER_SEARCH_PREF + "distributionID", - Services.appinfo.distributionID || ""); - var official = MOZ_OFFICIAL; - try { - if (Services.prefs.getBoolPref(BROWSER_SEARCH_PREF + "official")) - official = "official"; - else - official = "unofficial"; - } - catch (ex) { } - - // Custom search parameters. These are only available to default search - // engines. - if (aEngine._isDefault) { - value = value.replace(MOZ_PARAM_LOCALE, getLocale()); - value = value.replace(MOZ_PARAM_DIST_ID, distributionID); - value = value.replace(MOZ_PARAM_OFFICIAL, official); - } - - // Insert the OpenSearch parameters we're confident about - value = value.replace(OS_PARAM_USER_DEFINED, aSearchTerms); - value = value.replace(OS_PARAM_INPUT_ENCODING, aEngine.queryCharset); - value = value.replace(OS_PARAM_LANGUAGE, - getLocale() || OS_PARAM_LANGUAGE_DEF); - value = value.replace(OS_PARAM_OUTPUT_ENCODING, - OS_PARAM_OUTPUT_ENCODING_DEF); - - // Replace any optional parameters - value = value.replace(OS_PARAM_OPTIONAL, ""); - - // Insert any remaining required params with our default values - for (var i = 0; i < OS_UNSUPPORTED_PARAMS.length; ++i) { - value = value.replace(OS_UNSUPPORTED_PARAMS[i][0], - OS_UNSUPPORTED_PARAMS[i][1]); - } - - return value; -} - -/** - * Creates an engineURL object, which holds the query URL and all parameters. - * - * @param aType - * A string containing the name of the MIME type of the search results - * returned by this URL. - * @param aMethod - * The HTTP request method. Must be a case insensitive value of either - * "GET" or "POST". - * @param aTemplate - * The URL to which search queries should be sent. For GET requests, - * must contain the string "{searchTerms}", to indicate where the user - * entered search terms should be inserted. - * @param aResultDomain - * The root domain for this URL. Defaults to the template's host. - * - * @see http://opensearch.a9.com/spec/1.1/querysyntax/#urltag - * - * @throws NS_ERROR_NOT_IMPLEMENTED if aType is unsupported. - */ -function EngineURL(aType, aMethod, aTemplate, aResultDomain) { - if (!aType || !aMethod || !aTemplate) - FAIL("missing type, method or template for EngineURL!"); - - var method = aMethod.toUpperCase(); - var type = aType.toLowerCase(); - - if (method != "GET" && method != "POST") - FAIL("method passed to EngineURL must be \"GET\" or \"POST\""); - - this.type = type; - this.method = method; - this.params = []; - this.rels = []; - // Don't serialize expanded mozparams - this.mozparams = {}; - - var templateURI = makeURI(aTemplate); - if (!templateURI) - FAIL("new EngineURL: template is not a valid URI!", Cr.NS_ERROR_FAILURE); - - switch (templateURI.scheme) { - case "http": - case "https": - // Disable these for now, see bug 295018 - // case "file": - // case "resource": - this.template = aTemplate; - break; - default: - FAIL("new EngineURL: template uses invalid scheme!", Cr.NS_ERROR_FAILURE); - } - - // If no resultDomain was specified in the engine definition file, use the - // host from the template. - this.resultDomain = aResultDomain || templateURI.host; - // We never want to return a "www." prefix, so eventually strip it. - if (this.resultDomain.startsWith("www.")) { - this.resultDomain = this.resultDomain.substr(4); - } -} -EngineURL.prototype = { - - addParam: function SRCH_EURL_addParam(aName, aValue, aPurpose) { - this.params.push(new QueryParameter(aName, aValue, aPurpose)); - }, - - // Note: This method requires that aObj has a unique name or the previous MozParams entry with - // that name will be overwritten. - _addMozParam: function SRCH_EURL__addMozParam(aObj) { - aObj.mozparam = true; - this.mozparams[aObj.name] = aObj; - }, - - getSubmission: function SRCH_EURL_getSubmission(aSearchTerms, aEngine, aPurpose) { - var url = ParamSubstitution(this.template, aSearchTerms, aEngine); - // Default to searchbar if the purpose is not provided - var purpose = aPurpose || "searchbar"; - - // If a particular purpose isn't defined in the plugin, fallback to 'searchbar'. - if (!this.params.some(p => p.purpose !== undefined && p.purpose == purpose)) - 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) - continue; - - var value = ParamSubstitution(param.value, aSearchTerms, aEngine); - - dataString += (i > 0 ? "&" : "") + param.name + "=" + value; - } - - var postData = null; - if (this.method == "GET") { - // GET method requests have no post data, and append the encoded - // query string to the url... - if (url.indexOf("?") == -1 && dataString) - url += "?"; - url += dataString; - } else if (this.method == "POST") { - // 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; - - postData = Cc["@mozilla.org/network/mime-input-stream;1"]. - createInstance(Ci.nsIMIMEInputStream); - postData.addHeader("Content-Type", "application/x-www-form-urlencoded"); - postData.addContentLength = true; - postData.setData(stringStream); - } - - return new Submission(makeURI(url), postData); - }, - - _getTermsParameterName: function SRCH_EURL__getTermsParameterName() { - let queryParam = this.params.find(p => p.value == USER_DEFINED); - return queryParam ? queryParam.name : ""; - }, - - _hasRelation: function SRC_EURL__hasRelation(aRel) { - return this.rels.some(e => e == aRel.toLowerCase()); - }, - - _initWithJSON: function SRC_EURL__initWithJSON(aJson, aEngine) { - if (!aJson.params) - return; - - this.rels = aJson.rels; - - for (let i = 0; i < aJson.params.length; ++i) { - let param = aJson.params[i]; - if (param.mozparam) { - if (param.condition == "pref") { - let value = getMozParamPref(param.pref); - this.addParam(param.name, value); - } - this._addMozParam(param); - } - else - this.addParam(param.name, param.value, param.purpose || undefined); - } - }, - - /** - * Creates a JavaScript object that represents this URL. - * @returns An object suitable for serialization as JSON. - **/ - toJSON: function SRCH_EURL_toJSON() { - var json = { - template: this.template, - rels: this.rels, - resultDomain: this.resultDomain - }; - - if (this.type != URLTYPE_SEARCH_HTML) - json.type = this.type; - if (this.method != "GET") - json.method = this.method; - - function collapseMozParams(aParam) { - return this.mozparams[aParam.name] || aParam; - } - json.params = this.params.map(collapseMozParams, this); - - return json; - } -}; - -/** - * nsISearchEngine constructor. - * @param aLocation - * A nsILocalFile or nsIURI object representing the location of the - * search engine data file. - * @param aIsReadOnly - * Boolean indicating whether the engine should be treated as read-only. - */ -function Engine(aLocation, aIsReadOnly) { - this._readOnly = aIsReadOnly; - this._urls = []; - this._metaData = {}; - - let file, uri; - if (typeof aLocation == "string") { - this._shortName = aLocation; - } else if (aLocation instanceof Ci.nsILocalFile) { - if (!aIsReadOnly) { - // This is an engine that was installed in NS_APP_USER_SEARCH_DIR by a - // previous version. We are converting the file to an engine stored only - // in JSON, but we need to keep the reference to the profile file to - // remove it if the user ever removes the engine. - this._filePath = aLocation.persistentDescriptor; - } - file = aLocation; - } else if (aLocation instanceof Ci.nsIURI) { - switch (aLocation.scheme) { - case "https": - case "http": - case "ftp": - case "data": - case "file": - case "resource": - case "chrome": - uri = aLocation; - break; - default: - ERROR("Invalid URI passed to the nsISearchEngine constructor", - Cr.NS_ERROR_INVALID_ARG); - } - } else - ERROR("Engine location is neither a File nor a URI object", - Cr.NS_ERROR_INVALID_ARG); - - if (!this._shortName) { - // If we don't have a shortName at this point, it's the first time we load - // this engine, so let's generate the shortName, id and loadPath values. - let shortName; - if (file) { - shortName = file.leafName; - } - else if (uri && uri instanceof Ci.nsIURL) { - if (aIsReadOnly || (gEnvironment.get("XPCSHELL_TEST_PROFILE_DIR") && - uri.scheme == "resource")) { - shortName = uri.fileName; - } - } - if (shortName && shortName.endsWith(".xml")) { - this._shortName = shortName.slice(0, -4); - } - this._loadPath = this.getAnonymizedLoadPath(file, uri); - - if (!shortName && !aIsReadOnly) { - // We are in the process of downloading and installing the engine. - // We'll have the shortName and id once we are done parsing it. - return; - } - - // Build the id used for the legacy metadata storage, so that we - // can do a one-time import of data from old profiles. - if (this._isDefault || - (uri && uri.spec.startsWith(APP_SEARCH_PREFIX))) { - // The second part of the check is to catch engines from language packs. - // They aren't default engines (because they aren't app-shipped), but we - // still need to give their id an [app] prefix for backward compat. - this._id = "[app]/" + this._shortName + ".xml"; - } - else if (!aIsReadOnly) { - this._id = "[profile]/" + this._shortName + ".xml"; - } - else { - // If the engine is neither a default one, nor a user-installed one, - // it must be extension-shipped, so use the full path as id. - LOG("Setting _id to full path for engine from " + this._loadPath); - this._id = file ? file.path : uri.spec; - } - } -} - -Engine.prototype = { - // Data set by the user. - _metaData: null, - // The data describing the engine, in the form of an XML document element. - _data: null, - // Whether or not the engine is readonly. - _readOnly: true, - // Anonymized path of where we initially loaded the engine from. - // This will stay null for engines installed in the profile before we moved - // to a JSON storage. - _loadPath: null, - // The engine's description - _description: "", - // Used to store the engine to replace, if we're an update to an existing - // engine. - _engineToUpdate: null, - // Set to true if the engine has a preferred icon (an icon that should not be - // overridden by a non-preferred icon). - _hasPreferredIcon: null, - // The engine's name. - _name: null, - // The name of the charset used to submit the search terms. - _queryCharset: null, - // The engine's raw SearchForm value (URL string pointing to a search form). - __searchForm: null, - get _searchForm() { - return this.__searchForm; - }, - set _searchForm(aValue) { - if (/^https?:/i.test(aValue)) - this.__searchForm = aValue; - else - LOG("_searchForm: Invalid URL dropped for " + this._name || - "the current engine"); - }, - // Whether to obtain user confirmation before adding the engine. This is only - // used when the engine is first added to the list. - _confirm: false, - // Whether to set this as the current engine as soon as it is loaded. This - // is only used when the engine is first added to the list. - _useNow: false, - // A function to be invoked when this engine object's addition completes (or - // fails). Only used for installation via addEngine. - _installCallback: null, - // The number of days between update checks for new versions - _updateInterval: null, - // The url to check at for a new update - _updateURL: null, - // The url to check for a new icon - _iconUpdateURL: null, - /* The extension ID if added by an extension. */ - _extensionID: null, - - /** - * Retrieves the data from the engine's file. - * The document element is placed in the engine's data field. - */ - _initFromFile: function SRCH_ENG_initFromFile(file) { - if (!file || !file.exists()) - FAIL("File must exist before calling initFromFile!", Cr.NS_ERROR_UNEXPECTED); - - var fileInStream = Cc["@mozilla.org/network/file-input-stream;1"]. - createInstance(Ci.nsIFileInputStream); - - fileInStream.init(file, MODE_RDONLY, FileUtils.PERMS_FILE, false); - - var domParser = Cc["@mozilla.org/xmlextras/domparser;1"]. - createInstance(Ci.nsIDOMParser); - var doc = domParser.parseFromStream(fileInStream, "UTF-8", - file.fileSize, - "text/xml"); - - this._data = doc.documentElement; - fileInStream.close(); - - // Now that the data is loaded, initialize the engine object - this._initFromData(); - }, - - /** - * Retrieves the data from the engine's file asynchronously. - * The document element is placed in the engine's data field. - * - * @param file The file to load the search plugin from. - * - * @returns {Promise} A promise, resolved successfully if initializing from - * data succeeds, rejected if it fails. - */ - _asyncInitFromFile: Task.async(function* (file) { - if (!file || !(yield OS.File.exists(file.path))) - FAIL("File must exist before calling initFromFile!", Cr.NS_ERROR_UNEXPECTED); - - let fileURI = NetUtil.ioService.newFileURI(file); - yield this._retrieveSearchXMLData(fileURI.spec); - - // Now that the data is loaded, initialize the engine object - this._initFromData(); - }), - - /** - * Retrieves the engine data from a URI. Initializes the engine, flushes to - * disk, and notifies the search service once initialization is complete. - * - * @param uri The uri to load the search plugin from. - */ - _initFromURIAndLoad: function SRCH_ENG_initFromURIAndLoad(uri) { - ENSURE_WARN(uri instanceof Ci.nsIURI, - "Must have URI when calling _initFromURIAndLoad!", - Cr.NS_ERROR_UNEXPECTED); - - LOG("_initFromURIAndLoad: Downloading engine from: \"" + uri.spec + "\"."); - - var chan = NetUtil.newChannel({ - uri: uri, - loadUsingSystemPrincipal: true - }); - - if (this._engineToUpdate && (chan instanceof Ci.nsIHttpChannel)) { - var lastModified = this._engineToUpdate.getAttr("updatelastmodified"); - if (lastModified) - chan.setRequestHeader("If-Modified-Since", lastModified, false); - } - this._uri = uri; - var listener = new loadListener(chan, this, this._onLoad); - chan.notificationCallbacks = listener; - chan.asyncOpen2(listener); - }, - - /** - * Retrieves the engine data from a URI asynchronously and initializes it. - * - * @param uri The uri to load the search plugin from. - * - * @returns {Promise} A promise, resolved successfully if retrieveing data - * succeeds. - */ - _asyncInitFromURI: Task.async(function* (uri) { - LOG("_asyncInitFromURI: Loading engine from: \"" + uri.spec + "\"."); - yield this._retrieveSearchXMLData(uri.spec); - // Now that the data is loaded, initialize the engine object - this._initFromData(); - }), - - /** - * Retrieves the engine data for a given URI asynchronously. - * - * @returns {Promise} A promise, resolved successfully if retrieveing data - * succeeds. - */ - _retrieveSearchXMLData: function SRCH_ENG__retrieveSearchXMLData(aURL) { - let deferred = Promise.defer(); - let request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]. - createInstance(Ci.nsIXMLHttpRequest); - request.overrideMimeType("text/xml"); - request.onload = (aEvent) => { - let responseXML = aEvent.target.responseXML; - this._data = responseXML.documentElement; - deferred.resolve(); - }; - request.onerror = function(aEvent) { - deferred.resolve(); - }; - request.open("GET", aURL, true); - request.send(); - - return deferred.promise; - }, - - _initFromURISync: function SRCH_ENG_initFromURISync(uri) { - ENSURE_WARN(uri instanceof Ci.nsIURI, - "Must have URI when calling _initFromURISync!", - Cr.NS_ERROR_UNEXPECTED); - - ENSURE_WARN(uri.schemeIs("resource"), "_initFromURISync called for non-resource URI", - Cr.NS_ERROR_FAILURE); - - LOG("_initFromURISync: Loading engine from: \"" + uri.spec + "\"."); - - var chan = NetUtil.newChannel({ - uri: uri, - loadUsingSystemPrincipal: true - }); - - var stream = chan.open2(); - var parser = Cc["@mozilla.org/xmlextras/domparser;1"]. - createInstance(Ci.nsIDOMParser); - var doc = parser.parseFromStream(stream, "UTF-8", stream.available(), "text/xml"); - - this._data = doc.documentElement; - - // Now that the data is loaded, initialize the engine object - this._initFromData(); - }, - - /** - * Attempts to find an EngineURL object in the set of EngineURLs for - * this Engine that has the given type string. (This corresponds to the - * "type" attribute in the "Url" node in the OpenSearch spec.) - * This method will return the first matching URL object found, or null - * if no matching URL is found. - * - * @param aType string to match the EngineURL's type attribute - * @param aRel [optional] only return URLs that with this rel value - */ - _getURLOfType: function SRCH_ENG__getURLOfType(aType, aRel) { - for (var i = 0; i < this._urls.length; ++i) { - if (this._urls[i].type == aType && (!aRel || this._urls[i]._hasRelation(aRel))) - return this._urls[i]; - } - - return null; - }, - - _confirmAddEngine: function SRCH_SVC_confirmAddEngine() { - var stringBundle = Services.strings.createBundle(SEARCH_BUNDLE); - var titleMessage = stringBundle.GetStringFromName("addEngineConfirmTitle"); - - // Display only the hostname portion of the URL. - var dialogMessage = - stringBundle.formatStringFromName("addEngineConfirmation", - [this._name, this._uri.host], 2); - var checkboxMessage = null; - if (!Services.prefs.getBoolPref(BROWSER_SEARCH_PREF + "noCurrentEngine", false)) - checkboxMessage = stringBundle.GetStringFromName("addEngineAsCurrentText"); - - var addButtonLabel = - stringBundle.GetStringFromName("addEngineAddButtonLabel"); - - var ps = Services.prompt; - var buttonFlags = (ps.BUTTON_TITLE_IS_STRING * ps.BUTTON_POS_0) + - (ps.BUTTON_TITLE_CANCEL * ps.BUTTON_POS_1) + - ps.BUTTON_POS_0_DEFAULT; - - var checked = {value: false}; - // confirmEx returns the index of the button that was pressed. Since "Add" - // is button 0, we want to return the negation of that value. - var confirm = !ps.confirmEx(null, - titleMessage, - dialogMessage, - buttonFlags, - addButtonLabel, - null, null, // button 1 & 2 names not used - checkboxMessage, - checked); - - return {confirmed: confirm, useNow: checked.value}; - }, - - /** - * Handle the successful download of an engine. Initializes the engine and - * triggers parsing of the data. The engine is then flushed to disk. Notifies - * the search service once initialization is complete. - */ - _onLoad: function SRCH_ENG_onLoad(aBytes, aEngine) { - /** - * Handle an error during the load of an engine by notifying the engine's - * error callback, if any. - */ - function onError(errorCode = Ci.nsISearchInstallCallback.ERROR_UNKNOWN_FAILURE) { - // Notify the callback of the failure - if (aEngine._installCallback) { - aEngine._installCallback(errorCode); - } - } - - function promptError(strings = {}, error = undefined) { - onError(error); - - if (aEngine._engineToUpdate) { - // We're in an update, so just fail quietly - LOG("updating " + aEngine._engineToUpdate.name + " failed"); - return; - } - var brandBundle = Services.strings.createBundle(BRAND_BUNDLE); - var brandName = brandBundle.GetStringFromName("brandShortName"); - - var searchBundle = Services.strings.createBundle(SEARCH_BUNDLE); - var msgStringName = strings.error || "error_loading_engine_msg2"; - var titleStringName = strings.title || "error_loading_engine_title"; - var title = searchBundle.GetStringFromName(titleStringName); - var text = searchBundle.formatStringFromName(msgStringName, - [brandName, aEngine._location], - 2); - - Services.ww.getNewPrompter(null).alert(title, text); - } - - if (!aBytes) { - promptError(); - return; - } - - var parser = Cc["@mozilla.org/xmlextras/domparser;1"]. - createInstance(Ci.nsIDOMParser); - var doc = parser.parseFromBuffer(aBytes, aBytes.length, "text/xml"); - aEngine._data = doc.documentElement; - - try { - // Initialize the engine from the obtained data - aEngine._initFromData(); - } catch (ex) { - LOG("_onLoad: Failed to init engine!\n" + ex); - // Report an error to the user - promptError(); - return; - } - - if (aEngine._engineToUpdate) { - let engineToUpdate = aEngine._engineToUpdate.wrappedJSObject; - - // Make this new engine use the old engine's shortName, and preserve - // metadata. - aEngine._shortName = engineToUpdate._shortName; - Object.keys(engineToUpdate._metaData).forEach(key => { - aEngine.setAttr(key, engineToUpdate.getAttr(key)); - }); - aEngine._loadPath = engineToUpdate._loadPath; - - // Keep track of the last modified date, so that we can make conditional - // requests for future updates. - aEngine.setAttr("updatelastmodified", (new Date()).toUTCString()); - - // Set the new engine's icon, if it doesn't yet have one. - if (!aEngine._iconURI && engineToUpdate._iconURI) - aEngine._iconURI = engineToUpdate._iconURI; - } else { - // Check that when adding a new engine (e.g., not updating an - // existing one), a duplicate engine does not already exist. - if (Services.search.getEngineByName(aEngine.name)) { - // If we're confirming the engine load, then display a "this is a - // duplicate engine" prompt; otherwise, fail silently. - if (aEngine._confirm) { - promptError({ error: "error_duplicate_engine_msg", - title: "error_invalid_engine_title" - }, Ci.nsISearchInstallCallback.ERROR_DUPLICATE_ENGINE); - } else { - onError(Ci.nsISearchInstallCallback.ERROR_DUPLICATE_ENGINE); - } - LOG("_onLoad: duplicate engine found, bailing"); - return; - } - - // If requested, confirm the addition now that we have the title. - // This property is only ever true for engines added via - // nsIBrowserSearchService::addEngine. - if (aEngine._confirm) { - var confirmation = aEngine._confirmAddEngine(); - LOG("_onLoad: confirm is " + confirmation.confirmed + - "; useNow is " + confirmation.useNow); - if (!confirmation.confirmed) { - onError(); - return; - } - aEngine._useNow = confirmation.useNow; - } - - aEngine._shortName = sanitizeName(aEngine.name); - aEngine._loadPath = aEngine.getAnonymizedLoadPath(null, aEngine._uri); - aEngine.setAttr("loadPathHash", getVerificationHash(aEngine._loadPath)); - } - - // Notify the search service of the successful load. It will deal with - // updates by checking aEngine._engineToUpdate. - notifyAction(aEngine, SEARCH_ENGINE_LOADED); - - // Notify the callback if needed - if (aEngine._installCallback) { - aEngine._installCallback(); - } - }, - - /** - * Creates a key by serializing an object that contains the icon's width - * and height. - * - * @param aWidth - * Width of the icon. - * @param aHeight - * Height of the icon. - * @returns key string - */ - _getIconKey: function SRCH_ENG_getIconKey(aWidth, aHeight) { - let keyObj = { - width: aWidth, - height: aHeight - }; - - return JSON.stringify(keyObj); - }, - - /** - * Add an icon to the icon map used by getIconURIBySize() and getIcons(). - * - * @param aWidth - * Width of the icon. - * @param aHeight - * Height of the icon. - * @param aURISpec - * String with the icon's URI. - */ - _addIconToMap: function SRCH_ENG_addIconToMap(aWidth, aHeight, aURISpec) { - if (aWidth == 16 && aHeight == 16) { - // The 16x16 icon is stored in _iconURL, we don't need to store it twice. - return; - } - - // Use an object instead of a Map() because it needs to be serializable. - this._iconMapObj = this._iconMapObj || {}; - let key = this._getIconKey(aWidth, aHeight); - this._iconMapObj[key] = aURISpec; - }, - - /** - * Sets the .iconURI property of the engine. If both aWidth and aHeight are - * provided an entry will be added to _iconMapObj that will enable accessing - * icon's data through getIcons() and getIconURIBySize() APIs. - * - * @param aIconURL - * A URI string pointing to the engine's icon. Must have a http[s], - * ftp, or data scheme. Icons with HTTP[S] or FTP schemes will be - * downloaded and converted to data URIs for storage in the engine - * XML files, if the engine is not readonly. - * @param aIsPreferred - * Whether or not this icon is to be preferred. Preferred icons can - * override non-preferred icons. - * @param aWidth (optional) - * Width of the icon. - * @param aHeight (optional) - * Height of the icon. - */ - _setIcon: function SRCH_ENG_setIcon(aIconURL, aIsPreferred, aWidth, aHeight) { - var uri = makeURI(aIconURL); - - // Ignore bad URIs - if (!uri) - return; - - LOG("_setIcon: Setting icon url \"" + limitURILength(uri.spec) + "\" for engine \"" - + this.name + "\"."); - // Only accept remote icons from http[s] or ftp - switch (uri.scheme) { - case "resource": - case "chrome": - // We only allow chrome and resource icon URLs for built-in search engines - if (!this._isDefault) { - return; - } - // Fall through to the data case - case "data": - if (!this._hasPreferredIcon || aIsPreferred) { - this._iconURI = uri; - notifyAction(this, SEARCH_ENGINE_CHANGED); - this._hasPreferredIcon = aIsPreferred; - } - - if (aWidth && aHeight) { - this._addIconToMap(aWidth, aHeight, aIconURL) - } - break; - case "http": - case "https": - case "ftp": - LOG("_setIcon: Downloading icon: \"" + uri.spec + - "\" for engine: \"" + this.name + "\""); - var chan = NetUtil.newChannel({ - uri: uri, - loadUsingSystemPrincipal: true - }); - - let iconLoadCallback = function (aByteArray, aEngine) { - // This callback may run after we've already set a preferred icon, - // so check again. - if (aEngine._hasPreferredIcon && !aIsPreferred) - return; - - if (!aByteArray || aByteArray.length > MAX_ICON_SIZE) { - LOG("iconLoadCallback: load failed, or the icon was too large!"); - return; - } - - let type = chan.contentType; - if (!type.startsWith("image/")) - type = "image/x-icon"; - let dataURL = "data:" + type + ";base64," + - btoa(String.fromCharCode.apply(null, aByteArray)); - - aEngine._iconURI = makeURI(dataURL); - - if (aWidth && aHeight) { - aEngine._addIconToMap(aWidth, aHeight, dataURL) - } - - notifyAction(aEngine, SEARCH_ENGINE_CHANGED); - aEngine._hasPreferredIcon = aIsPreferred; - }; - - // If we're currently acting as an "update engine", then the callback - // should set the icon on the engine we're updating and not us, since - // |this| might be gone by the time the callback runs. - var engineToSet = this._engineToUpdate || this; - - var listener = new loadListener(chan, engineToSet, iconLoadCallback); - chan.notificationCallbacks = listener; - chan.asyncOpen2(listener); - break; - } - }, - - /** - * Initialize this Engine object from the collected data. - */ - _initFromData: function SRCH_ENG_initFromData() { - ENSURE_WARN(this._data, "Can't init an engine with no data!", - Cr.NS_ERROR_UNEXPECTED); - - // Ensure we have a supported engine type before attempting to parse it. - let element = this._data; - if ((element.localName == MOZSEARCH_LOCALNAME && - element.namespaceURI == MOZSEARCH_NS_10) || - (element.localName == OPENSEARCH_LOCALNAME && - OPENSEARCH_NAMESPACES.indexOf(element.namespaceURI) != -1)) { - LOG("_init: Initing search plugin from " + this._location); - - this._parse(); - - } else - FAIL(this._location + " is not a valid search plugin.", Cr.NS_ERROR_FAILURE); - - // No need to keep a ref to our data (which in some cases can be a document - // element) past this point - this._data = null; - }, - - /** - * Initialize this Engine object from a collection of metadata. - */ - _initFromMetadata: function SRCH_ENG_initMetaData(aName, aIconURL, aAlias, - aDescription, aMethod, - aTemplate, aExtensionID) { - ENSURE_WARN(!this._readOnly, - "Can't call _initFromMetaData on a readonly engine!", - Cr.NS_ERROR_FAILURE); - - this._urls.push(new EngineURL(URLTYPE_SEARCH_HTML, aMethod, aTemplate)); - - this._name = aName; - this.alias = aAlias; - this._description = aDescription; - this._setIcon(aIconURL, true); - this._extensionID = aExtensionID; - }, - - /** - * Extracts data from an OpenSearch URL element and creates an EngineURL - * object which is then added to the engine's list of URLs. - * - * @throws NS_ERROR_FAILURE if a URL object could not be created. - * - * @see http://opensearch.a9.com/spec/1.1/querysyntax/#urltag. - * @see EngineURL() - */ - _parseURL: function SRCH_ENG_parseURL(aElement) { - var type = aElement.getAttribute("type"); - // According to the spec, method is optional, defaulting to "GET" if not - // specified - var method = aElement.getAttribute("method") || "GET"; - var template = aElement.getAttribute("template"); - var resultDomain = aElement.getAttribute("resultdomain"); - - try { - var url = new EngineURL(type, method, template, resultDomain); - } catch (ex) { - FAIL("_parseURL: failed to add " + template + " as a URL", - Cr.NS_ERROR_FAILURE); - } - - if (aElement.hasAttribute("rel")) - url.rels = aElement.getAttribute("rel").toLowerCase().split(/\s+/); - - for (var i = 0; i < aElement.childNodes.length; ++i) { - var param = aElement.childNodes[i]; - if (param.localName == "Param") { - try { - url.addParam(param.getAttribute("name"), param.getAttribute("value")); - } catch (ex) { - // Ignore failure - LOG("_parseURL: Url element has an invalid param"); - } - } else if (param.localName == "MozParam" && - // We only support MozParams for default search engines - this._isDefault) { - var value; - let condition = param.getAttribute("condition"); - - // MozParams must have a condition to be valid - if (!condition) { - let engineLoc = this._location; - let paramName = param.getAttribute("name"); - LOG("_parseURL: MozParam (" + paramName + ") without a condition attribute found parsing engine: " + engineLoc); - continue; - } - - switch (condition) { - case "purpose": - url.addParam(param.getAttribute("name"), - param.getAttribute("value"), - param.getAttribute("purpose")); - // _addMozParam is not needed here since it can be serialized fine without. _addMozParam - // also requires a unique "name" which is not normally the case when @purpose is used. - break; - case "pref": - try { - value = getMozParamPref(param.getAttribute("pref"), value); - url.addParam(param.getAttribute("name"), value); - url._addMozParam({"pref": param.getAttribute("pref"), - "name": param.getAttribute("name"), - "condition": "pref"}); - } catch (e) { } - break; - default: - let engineLoc = this._location; - let paramName = param.getAttribute("name"); - LOG("_parseURL: MozParam (" + paramName + ") has an unknown condition: " + condition + ". Found parsing engine: " + engineLoc); - break; - } - } - } - - this._urls.push(url); - }, - - /** - * Get the icon from an OpenSearch Image element. - * @see http://opensearch.a9.com/spec/1.1/description/#image - */ - _parseImage: function SRCH_ENG_parseImage(aElement) { - LOG("_parseImage: Image textContent: \"" + limitURILength(aElement.textContent) + "\""); - - let width = parseInt(aElement.getAttribute("width"), 10); - let height = parseInt(aElement.getAttribute("height"), 10); - let isPrefered = width == 16 && height == 16; - - if (isNaN(width) || isNaN(height) || width <= 0 || height <=0) { - LOG("OpenSearch image element must have positive width and height."); - return; - } - - this._setIcon(aElement.textContent, isPrefered, width, height); - }, - - /** - * Extract search engine information from the collected data to initialize - * the engine object. - */ - _parse: function SRCH_ENG_parse() { - var doc = this._data; - - // The OpenSearch spec sets a default value for the input encoding. - this._queryCharset = OS_PARAM_INPUT_ENCODING_DEF; - - for (var i = 0; i < doc.childNodes.length; ++i) { - var child = doc.childNodes[i]; - switch (child.localName) { - case "ShortName": - this._name = child.textContent; - break; - case "Description": - this._description = child.textContent; - break; - case "Url": - try { - this._parseURL(child); - } catch (ex) { - // Parsing of the element failed, just skip it. - LOG("_parse: failed to parse URL child: " + ex); - } - break; - case "Image": - this._parseImage(child); - break; - case "InputEncoding": - this._queryCharset = child.textContent.toUpperCase(); - break; - - // Non-OpenSearch elements - case "SearchForm": - this._searchForm = child.textContent; - break; - case "UpdateUrl": - this._updateURL = child.textContent; - break; - case "UpdateInterval": - this._updateInterval = parseInt(child.textContent); - break; - case "IconUpdateUrl": - this._iconUpdateURL = child.textContent; - break; - case "ExtensionID": - this._extensionID = child.textContent; - break; - } - } - if (!this.name || (this._urls.length == 0)) - FAIL("_parse: No name, or missing URL!", Cr.NS_ERROR_FAILURE); - if (!this.supportsResponseType(URLTYPE_SEARCH_HTML)) - FAIL("_parse: No text/html result type!", Cr.NS_ERROR_FAILURE); - }, - - /** - * Init from a JSON record. - **/ - _initWithJSON: function SRCH_ENG__initWithJSON(aJson) { - this._name = aJson._name; - this._shortName = aJson._shortName; - this._loadPath = aJson._loadPath; - this._description = aJson.description; - this._hasPreferredIcon = aJson._hasPreferredIcon == undefined; - this._queryCharset = aJson.queryCharset || DEFAULT_QUERY_CHARSET; - this.__searchForm = aJson.__searchForm; - this._updateInterval = aJson._updateInterval || null; - this._updateURL = aJson._updateURL || null; - this._iconUpdateURL = aJson._iconUpdateURL || null; - this._readOnly = aJson._readOnly == undefined; - this._iconURI = makeURI(aJson._iconURL); - this._iconMapObj = aJson._iconMapObj; - this._metaData = aJson._metaData || {}; - if (aJson.filePath) { - this._filePath = aJson.filePath; - } - if (aJson.dirPath) { - this._dirPath = aJson.dirPath; - this._dirLastModifiedTime = aJson.dirLastModifiedTime; - } - if (aJson.extensionID) { - this._extensionID = aJson.extensionID; - } - for (let i = 0; i < aJson._urls.length; ++i) { - let url = aJson._urls[i]; - let engineURL = new EngineURL(url.type || URLTYPE_SEARCH_HTML, - url.method || "GET", url.template, - url.resultDomain || undefined); - engineURL._initWithJSON(url, this); - this._urls.push(engineURL); - } - }, - - /** - * Creates a JavaScript object that represents this engine. - * @returns An object suitable for serialization as JSON. - **/ - toJSON: function SRCH_ENG_toJSON() { - var json = { - _name: this._name, - _shortName: this._shortName, - _loadPath: this._loadPath, - description: this.description, - __searchForm: this.__searchForm, - _iconURL: this._iconURL, - _iconMapObj: this._iconMapObj, - _metaData: this._metaData, - _urls: this._urls - }; - - if (this._updateInterval) - json._updateInterval = this._updateInterval; - if (this._updateURL) - json._updateURL = this._updateURL; - if (this._iconUpdateURL) - json._iconUpdateURL = this._iconUpdateURL; - if (!this._hasPreferredIcon) - json._hasPreferredIcon = this._hasPreferredIcon; - if (this.queryCharset != DEFAULT_QUERY_CHARSET) - json.queryCharset = this.queryCharset; - if (!this._readOnly) - json._readOnly = this._readOnly; - if (this._filePath) { - // File path is stored so that we can remove legacy xml files - // from the profile if the user removes the engine. - json.filePath = this._filePath; - } - if (this._dirPath) { - // The directory path is only stored for extension-shipped engines, - // it's used to invalidate the cache. - json.dirPath = this._dirPath; - json.dirLastModifiedTime = this._dirLastModifiedTime; - } - if (this._extensionID) { - json.extensionID = this._extensionID; - } - - return json; - }, - - setAttr(name, val) { - this._metaData[name] = val; - }, - - getAttr(name) { - return this._metaData[name] || undefined; - }, - - // nsISearchEngine - get alias() { - return this.getAttr("alias"); - }, - set alias(val) { - var value = val ? val.trim() : null; - this.setAttr("alias", value); - notifyAction(this, SEARCH_ENGINE_CHANGED); - }, - - /** - * Return the built-in identifier of app-provided engines. - * - * Note that this identifier is substantially similar to _id, with the - * following exceptions: - * - * * There is no trailing file extension. - * * There is no [app] prefix. - * - * @return a string identifier, or null. - */ - get identifier() { - // No identifier if If the engine isn't app-provided - return this._isDefault ? this._shortName : null; - }, - - get description() { - return this._description; - }, - - get hidden() { - return this.getAttr("hidden") || false; - }, - set hidden(val) { - var value = !!val; - if (value != this.hidden) { - this.setAttr("hidden", value); - notifyAction(this, SEARCH_ENGINE_CHANGED); - } - }, - - get iconURI() { - if (this._iconURI) - return this._iconURI; - return null; - }, - - get _iconURL() { - if (!this._iconURI) - return ""; - return this._iconURI.spec; - }, - - // Where the engine is being loaded from: will return the URI's spec if the - // engine is being downloaded and does not yet have a file. This is only used - // for logging and error messages. - get _location() { - if (this._uri) - return this._uri.spec; - - return this._loadPath; - }, - - // This indicates where we found the .xml file to load the engine, - // and attempts to hide user-identifiable data (such as username). - getAnonymizedLoadPath(file, uri) { - /* Examples of expected output: - * jar:[app]/omni.ja!browser/engine.xml - * 'browser' here is the name of the chrome package, not a folder. - * [profile]/searchplugins/engine.xml - * [distribution]/searchplugins/common/engine.xml - * [other]/engine.xml - */ - - const NS_XPCOM_CURRENT_PROCESS_DIR = "XCurProcD"; - const NS_APP_USER_PROFILE_50_DIR = "ProfD"; - const XRE_APP_DISTRIBUTION_DIR = "XREAppDist"; - - const knownDirs = { - app: NS_XPCOM_CURRENT_PROCESS_DIR, - profile: NS_APP_USER_PROFILE_50_DIR, - distribution: XRE_APP_DISTRIBUTION_DIR - }; - - let leafName = this._shortName; - if (!leafName) - return "null"; - leafName += ".xml"; - - let prefix = "", suffix = ""; - if (!file) { - if (uri.schemeIs("resource")) { - uri = makeURI(Services.io.getProtocolHandler("resource") - .QueryInterface(Ci.nsISubstitutingProtocolHandler) - .resolveURI(uri)); - } - let scheme = uri.scheme; - let packageName = ""; - if (scheme == "chrome") { - packageName = uri.hostPort; - uri = gChromeReg.convertChromeURL(uri); - } - - if (AppConstants.platform == "android") { - // On Android the omni.ja file isn't at the same path as the binary - // used to start the process. We tweak the path here so that the code - // shared with Desktop will correctly identify files from the omni.ja - // file as coming from the [app] folder. - let appPath = Services.io.getProtocolHandler("resource") - .QueryInterface(Ci.nsIResProtocolHandler) - .getSubstitution("android"); - if (appPath) { - appPath = appPath.spec; - let spec = uri.spec; - if (spec.includes(appPath)) { - let appURI = Services.io.newFileURI(getDir(knownDirs["app"])); - uri = NetUtil.newURI(spec.replace(appPath, appURI.spec)); - } - } - } - - if (uri instanceof Ci.nsINestedURI) { - prefix = "jar:"; - suffix = "!" + packageName + "/" + leafName; - uri = uri.innermostURI; - } - if (uri instanceof Ci.nsIFileURL) { - file = uri.file; - } else { - let path = "[" + scheme + "]"; - if (/^(?:https?|ftp)$/.test(scheme)) { - path += uri.host; - } - return path + "/" + leafName; - } - } - - let id; - let enginePath = file.path; - - for (let key in knownDirs) { - let path; - try { - path = getDir(knownDirs[key]).path; - } catch (e) { - // Getting XRE_APP_DISTRIBUTION_DIR throws during unit tests. - continue; - } - if (enginePath.startsWith(path)) { - id = "[" + key + "]" + enginePath.slice(path.length).replace(/\\/g, "/"); - break; - } - } - - // If the folder doesn't have a known ancestor, don't record its path to - // avoid leaking user identifiable data. - if (!id) - id = "[other]/" + file.leafName; - - return prefix + id + suffix; - }, - - get _isDefault() { - // If we don't have a shortName, the engine is being parsed from a - // downloaded file, so this can't be a default engine. - if (!this._shortName) - return false; - - // An engine is a default one if we initially loaded it from the application - // or distribution directory. - if (/^(?:jar:)?(?:\[app\]|\[distribution\])/.test(this._loadPath)) - return true; - - // If we are using a non-default locale or in the xpcshell test case, - // we'll accept as a 'default' engine anything that has been registered at - // resource://search-plugins/ even if the file doesn't come from the - // application folder. If not, skip costly additional checks. - if (!Services.prefs.prefHasUserValue(LOCALE_PREF) && - !gEnvironment.get("XPCSHELL_TEST_PROFILE_DIR")) - return false; - - // Some xpcshell tests use the search service without registering - // resource://search-plugins/. - if (!Services.io.getProtocolHandler("resource") - .QueryInterface(Ci.nsIResProtocolHandler) - .hasSubstitution("search-plugins")) - return false; - - let uri = makeURI(APP_SEARCH_PREFIX + this._shortName + ".xml"); - if (this.getAnonymizedLoadPath(null, uri) == this._loadPath) { - // This isn't a real default engine, but it's very close. - LOG("_isDefault, pretending " + this._loadPath + " is a default engine"); - return true; - } - - return false; - }, - - get _hasUpdates() { - // Whether or not the engine has an update URL - let selfURL = this._getURLOfType(URLTYPE_OPENSEARCH, "self"); - return !!(this._updateURL || this._iconUpdateURL || selfURL); - }, - - get name() { - return this._name; - }, - - get searchForm() { - return this._getSearchFormWithPurpose(); - }, - - _getSearchFormWithPurpose(aPurpose = "") { - // First look for a <Url rel="searchform"> - var searchFormURL = this._getURLOfType(URLTYPE_SEARCH_HTML, "searchform"); - if (searchFormURL) { - let submission = searchFormURL.getSubmission("", this, aPurpose); - - // If the rel=searchform URL is not type="get" (i.e. has postData), - // ignore it, since we can only return a URL. - if (!submission.postData) - return submission.uri.spec; - } - - if (!this._searchForm) { - // No SearchForm specified in the engine definition file, use the prePath - // (e.g. https://foo.com for https://foo.com/search.php?q=bar). - var htmlUrl = this._getURLOfType(URLTYPE_SEARCH_HTML); - ENSURE_WARN(htmlUrl, "Engine has no HTML URL!", Cr.NS_ERROR_UNEXPECTED); - this._searchForm = makeURI(htmlUrl.template).prePath; - } - - return ParamSubstitution(this._searchForm, "", this); - }, - - get queryCharset() { - if (this._queryCharset) - return this._queryCharset; - return this._queryCharset = "windows-1252"; // the default - }, - - // from nsISearchEngine - addParam: function SRCH_ENG_addParam(aName, aValue, aResponseType) { - if (!aName || (aValue == null)) - FAIL("missing name or value for nsISearchEngine::addParam!"); - ENSURE_WARN(!this._readOnly, - "called nsISearchEngine::addParam on a read-only engine!", - Cr.NS_ERROR_FAILURE); - if (!aResponseType) - aResponseType = URLTYPE_SEARCH_HTML; - - var url = this._getURLOfType(aResponseType); - if (!url) - FAIL("Engine object has no URL for response type " + aResponseType, - Cr.NS_ERROR_FAILURE); - - url.addParam(aName, aValue); - }, - - get _defaultMobileResponseType() { - let type = URLTYPE_SEARCH_HTML; - - let sysInfo = Cc["@mozilla.org/system-info;1"].getService(Ci.nsIPropertyBag2); - let isTablet = sysInfo.get("tablet"); - if (isTablet && this.supportsResponseType("application/x-moz-tabletsearch")) { - // Check for a tablet-specific search URL override - type = "application/x-moz-tabletsearch"; - } else if (!isTablet && this.supportsResponseType("application/x-moz-phonesearch")) { - // Check for a phone-specific search URL override - type = "application/x-moz-phonesearch"; - } - - delete this._defaultMobileResponseType; - return this._defaultMobileResponseType = type; - }, - - get _isWhiteListed() { - let url = this._getURLOfType(URLTYPE_SEARCH_HTML).template; - let hostname = makeURI(url).host; - let whitelist = Services.prefs.getDefaultBranch(BROWSER_SEARCH_PREF) - .getCharPref("reset.whitelist") - .split(","); - if (whitelist.includes(hostname)) { - LOG("The hostname " + hostname + " is white listed, " + - "we won't show the search reset prompt"); - return true; - } - - return false; - }, - - // from nsISearchEngine - getSubmission: function SRCH_ENG_getSubmission(aData, aResponseType, aPurpose) { - if (!aResponseType) { - aResponseType = AppConstants.platform == "android" ? this._defaultMobileResponseType : - URLTYPE_SEARCH_HTML; - } - - if (aResponseType == URLTYPE_SEARCH_HTML && - Services.prefs.getDefaultBranch(BROWSER_SEARCH_PREF).getBoolPref("reset.enabled") && - this.name == Services.search.currentEngine.name && - !this._isDefault && - this.name != Services.search.originalDefaultEngine.name && - (!this.getAttr("loadPathHash") || - this.getAttr("loadPathHash") != getVerificationHash(this._loadPath)) && - !this._isWhiteListed) { - let url = "about:searchreset"; - let data = []; - if (aData) - data.push("data=" + encodeURIComponent(aData)); - if (aPurpose) - data.push("purpose=" + aPurpose); - if (data.length) - url += "?" + data.join("&"); - return new Submission(makeURI(url)); - } - - var url = this._getURLOfType(aResponseType); - - if (!url) - return null; - - if (!aData) { - // Return a dummy submission object with our searchForm attribute - return new Submission(makeURI(this._getSearchFormWithPurpose(aPurpose))); - } - - LOG("getSubmission: In data: \"" + aData + "\"; Purpose: \"" + aPurpose + "\""); - var data = ""; - try { - data = gTextToSubURI.ConvertAndEscape(this.queryCharset, aData); - } catch (ex) { - LOG("getSubmission: Falling back to default queryCharset!"); - data = gTextToSubURI.ConvertAndEscape(DEFAULT_QUERY_CHARSET, aData); - } - LOG("getSubmission: Out data: \"" + data + "\""); - return url.getSubmission(data, this, aPurpose); - }, - - // from nsISearchEngine - supportsResponseType: function SRCH_ENG_supportsResponseType(type) { - return (this._getURLOfType(type) != null); - }, - - // from nsISearchEngine - getResultDomain: function SRCH_ENG_getResultDomain(aResponseType) { - if (!aResponseType) { - aResponseType = AppConstants.platform == "android" ? this._defaultMobileResponseType : - URLTYPE_SEARCH_HTML; - } - - LOG("getResultDomain: responseType: \"" + aResponseType + "\""); - - let url = this._getURLOfType(aResponseType); - if (url) - return url.resultDomain; - return ""; - }, - - /** - * Returns URL parsing properties used by _buildParseSubmissionMap. - */ - getURLParsingInfo: function () { - let responseType = AppConstants.platform == "android" ? this._defaultMobileResponseType : - URLTYPE_SEARCH_HTML; - - LOG("getURLParsingInfo: responseType: \"" + responseType + "\""); - - let url = this._getURLOfType(responseType); - if (!url || url.method != "GET") { - return null; - } - - let termsParameterName = url._getTermsParameterName(); - if (!termsParameterName) { - return null; - } - - let templateUrl = NetUtil.newURI(url.template).QueryInterface(Ci.nsIURL); - return { - mainDomain: templateUrl.host, - path: templateUrl.filePath.toLowerCase(), - termsParameterName: termsParameterName, - }; - }, - - // nsISupports - QueryInterface: XPCOMUtils.generateQI([Ci.nsISearchEngine]), - - get wrappedJSObject() { - return this; - }, - - /** - * Returns a string with the URL to an engine's icon matching both width and - * height. Returns null if icon with specified dimensions is not found. - * - * @param width - * Width of the requested icon. - * @param height - * Height of the requested icon. - */ - getIconURLBySize: function SRCH_ENG_getIconURLBySize(aWidth, aHeight) { - if (aWidth == 16 && aHeight == 16) - return this._iconURL; - - if (!this._iconMapObj) - return null; - - let key = this._getIconKey(aWidth, aHeight); - if (key in this._iconMapObj) { - return this._iconMapObj[key]; - } - return null; - }, - - /** - * Gets an array of all available icons. Each entry is an object with - * width, height and url properties. width and height are numeric and - * represent the icon's dimensions. url is a string with the URL for - * the icon. - */ - getIcons: function SRCH_ENG_getIcons() { - let result = []; - if (this._iconURL) - result.push({width: 16, height: 16, url: this._iconURL}); - - if (!this._iconMapObj) - return result; - - for (let key of Object.keys(this._iconMapObj)) { - let iconSize = JSON.parse(key); - result.push({ - width: iconSize.width, - height: iconSize.height, - url: this._iconMapObj[key] - }); - } - - return result; - }, - - /** - * Opens a speculative connection to the engine's search URI - * (and suggest URI, if different) to reduce request latency - * - * @param options - * An object that must contain the following fields: - * {window} the content window for the window performing the search - * - * @throws NS_ERROR_INVALID_ARG if options is omitted or lacks required - * elemeents - */ - speculativeConnect: function SRCH_ENG_speculativeConnect(options) { - if (!options || !options.window) { - Cu.reportError("invalid options arg passed to nsISearchEngine.speculativeConnect"); - throw Cr.NS_ERROR_INVALID_ARG; - } - let connector = - Services.io.QueryInterface(Components.interfaces.nsISpeculativeConnect); - - let searchURI = this.getSubmission("dummy").uri; - - let callbacks = options.window.QueryInterface(Components.interfaces.nsIInterfaceRequestor) - .getInterface(Components.interfaces.nsIWebNavigation) - .QueryInterface(Components.interfaces.nsILoadContext); - - connector.speculativeConnect(searchURI, callbacks); - - if (this.supportsResponseType(URLTYPE_SUGGEST_JSON)) { - let suggestURI = this.getSubmission("dummy", URLTYPE_SUGGEST_JSON).uri; - if (suggestURI.prePath != searchURI.prePath) - connector.speculativeConnect(suggestURI, callbacks); - } - }, -}; - -// nsISearchSubmission -function Submission(aURI, aPostData = null) { - this._uri = aURI; - this._postData = aPostData; -} -Submission.prototype = { - get uri() { - return this._uri; - }, - get postData() { - return this._postData; - }, - QueryInterface: XPCOMUtils.generateQI([Ci.nsISearchSubmission]) -} - -// nsISearchParseSubmissionResult -function ParseSubmissionResult(aEngine, aTerms, aTermsOffset, aTermsLength) { - this._engine = aEngine; - this._terms = aTerms; - this._termsOffset = aTermsOffset; - this._termsLength = aTermsLength; -} -ParseSubmissionResult.prototype = { - get engine() { - return this._engine; - }, - get terms() { - return this._terms; - }, - get termsOffset() { - return this._termsOffset; - }, - get termsLength() { - return this._termsLength; - }, - QueryInterface: XPCOMUtils.generateQI([Ci.nsISearchParseSubmissionResult]), -} - -const gEmptyParseSubmissionResult = - Object.freeze(new ParseSubmissionResult(null, "", -1, 0)); - -function executeSoon(func) { - Services.tm.mainThread.dispatch(func, Ci.nsIThread.DISPATCH_NORMAL); -} - -/** - * Check for sync initialization has completed or not. - * - * @param {aPromise} A promise. - * - * @returns the value returned by the invoked method. - * @throws NS_ERROR_ALREADY_INITIALIZED if sync initialization has completed. - */ -function checkForSyncCompletion(aPromise) { - return aPromise.then(function(aValue) { - if (gInitialized) { - throw Components.Exception("Synchronous fallback was called and has " + - "finished so no need to pursue asynchronous " + - "initialization", - Cr.NS_ERROR_ALREADY_INITIALIZED); - } - return aValue; - }); -} - -// nsIBrowserSearchService -function SearchService() { - // Replace empty LOG function with the useful one if the log pref is set. - if (Services.prefs.getBoolPref(BROWSER_SEARCH_PREF + "log", false)) - LOG = DO_LOG; - - this._initObservers = Promise.defer(); -} - -SearchService.prototype = { - classID: Components.ID("{7319788a-fe93-4db3-9f39-818cf08f4256}"), - - // The current status of initialization. Note that it does not determine if - // initialization is complete, only if an error has been encountered so far. - _initRV: Cr.NS_OK, - - // The boolean indicates that the initialization has started or not. - _initStarted: null, - - // Reading the JSON cache file is the first thing done during initialization. - // During the async init, we save it in a field so that if we have to do a - // sync init before the async init finishes, we can avoid reading the cache - // with sync disk I/O and handling lz4 decompression synchronously. - // This is set back to null as soon as the initialization is finished. - _cacheFileJSON: null, - - // If initialization has not been completed yet, perform synchronous - // initialization. - // Throws in case of initialization error. - _ensureInitialized: function SRCH_SVC__ensureInitialized() { - if (gInitialized) { - if (!Components.isSuccessCode(this._initRV)) { - LOG("_ensureInitialized: failure"); - throw this._initRV; - } - return; - } - - let performanceWarning = - "Search service falling back to synchronous initialization. " + - "This is generally the consequence of an add-on using a deprecated " + - "search service API."; - Deprecated.perfWarning(performanceWarning, "https://developer.mozilla.org/en-US/docs/XPCOM_Interface_Reference/nsIBrowserSearchService#async_warning"); - LOG(performanceWarning); - - this._syncInit(); - if (!Components.isSuccessCode(this._initRV)) { - throw this._initRV; - } - }, - - // Synchronous implementation of the initializer. - // Used by |_ensureInitialized| as a fallback if initialization is not - // complete. - _syncInit: function SRCH_SVC__syncInit() { - LOG("_syncInit start"); - this._initStarted = true; - - let cache = this._readCacheFile(); - if (cache.metaData) - this._metaData = cache.metaData; - - try { - this._syncLoadEngines(cache); - } catch (ex) { - this._initRV = Cr.NS_ERROR_FAILURE; - LOG("_syncInit: failure loading engines: " + ex); - } - this._addObservers(); - - gInitialized = true; - this._cacheFileJSON = null; - - this._initObservers.resolve(this._initRV); - - Services.obs.notifyObservers(null, SEARCH_SERVICE_TOPIC, "init-complete"); - Services.telemetry.getHistogramById("SEARCH_SERVICE_INIT_SYNC").add(true); - this._recordEngineTelemetry(); - - LOG("_syncInit end"); - }, - - /** - * Asynchronous implementation of the initializer. - * - * @returns {Promise} A promise, resolved successfully if the initialization - * succeeds. - */ - _asyncInit: Task.async(function* () { - LOG("_asyncInit start"); - - // See if we have a cache file so we don't have to parse a bunch of XML. - let cache = {}; - // Not using checkForSyncCompletion here because we want to ensure we - // fetch the country code and geo specific defaults asynchronously even - // if a sync init has been forced. - cache = yield this._asyncReadCacheFile(); - - if (!gInitialized && cache.metaData) - this._metaData = cache.metaData; - - try { - yield checkForSyncCompletion(this._asyncLoadEngines(cache)); - } catch (ex) { - if (ex.result == Cr.NS_ERROR_ALREADY_INITIALIZED) { - throw ex; - } - this._initRV = Cr.NS_ERROR_FAILURE; - LOG("_asyncInit: failure loading engines: " + ex); - } - this._addObservers(); - gInitialized = true; - this._cacheFileJSON = null; - this._initObservers.resolve(this._initRV); - Services.obs.notifyObservers(null, SEARCH_SERVICE_TOPIC, "init-complete"); - Services.telemetry.getHistogramById("SEARCH_SERVICE_INIT_SYNC").add(false); - this._recordEngineTelemetry(); - - LOG("_asyncInit: Completed _asyncInit"); - }), - - _metaData: { }, - setGlobalAttr(name, val) { - this._metaData[name] = val; - this.batchTask.disarm(); - this.batchTask.arm(); - }, - setVerifiedGlobalAttr(name, val) { - this.setGlobalAttr(name, val); - this.setGlobalAttr(name + "Hash", getVerificationHash(val)); - }, - - getGlobalAttr(name) { - return this._metaData[name] || undefined; - }, - getVerifiedGlobalAttr(name) { - let val = this.getGlobalAttr(name); - if (val && this.getGlobalAttr(name + "Hash") != getVerificationHash(val)) { - LOG("getVerifiedGlobalAttr, invalid hash for " + name); - return ""; - } - return val; - }, - - _engines: { }, - __sortedEngines: null, - _visibleDefaultEngines: [], - get _sortedEngines() { - if (!this.__sortedEngines) - return this._buildSortedEngineList(); - return this.__sortedEngines; - }, - - // Get the original Engine object that is the default for this region, - // ignoring changes the user may have subsequently made. - get originalDefaultEngine() { - let defaultEngine = this.getVerifiedGlobalAttr("searchDefault"); - if (!defaultEngine) { - let defaultPrefB = Services.prefs.getDefaultBranch(BROWSER_SEARCH_PREF); - let nsIPLS = Ci.nsIPrefLocalizedString; - - try { - defaultEngine = defaultPrefB.getComplexValue("defaultenginename", nsIPLS).data; - } catch (ex) { - // If the default pref is invalid (e.g. an add-on set it to a bogus value) - // getEngineByName will just return null, which is the best we can do. - } - } - - return this.getEngineByName(defaultEngine); - }, - - resetToOriginalDefaultEngine: function SRCH_SVC__resetToOriginalDefaultEngine() { - this.currentEngine = this.originalDefaultEngine; - }, - - _buildCache: function SRCH_SVC__buildCache() { - if (this._batchTask) - this._batchTask.disarm(); - - let cache = {}; - let locale = getLocale(); - let buildID = Services.appinfo.platformBuildID; - - // Allows us to force a cache refresh should the cache format change. - cache.version = CACHE_VERSION; - // We don't want to incur the costs of stat()ing each plugin on every - // startup when the only (supported) time they will change is during - // app updates (where the buildID is obviously going to change). - // Extension-shipped plugins are the only exception to this, but their - // directories are blown away during updates, so we'll detect their changes. - cache.buildID = buildID; - cache.locale = locale; - - cache.visibleDefaultEngines = this._visibleDefaultEngines; - cache.metaData = this._metaData; - cache.engines = []; - - for (let name in this._engines) { - cache.engines.push(this._engines[name]); - } - - try { - if (!cache.engines.length) - throw "cannot write without any engine."; - - LOG("_buildCache: Writing to cache file."); - let path = OS.Path.join(OS.Constants.Path.profileDir, CACHE_FILENAME); - let data = gEncoder.encode(JSON.stringify(cache)); - let promise = OS.File.writeAtomic(path, data, {compression: "lz4", - tmpPath: path + ".tmp"}); - - promise.then( - function onSuccess() { - Services.obs.notifyObservers(null, SEARCH_SERVICE_TOPIC, SEARCH_SERVICE_CACHE_WRITTEN); - }, - function onError(e) { - LOG("_buildCache: failure during writeAtomic: " + e); - } - ); - } catch (ex) { - LOG("_buildCache: Could not write to cache file: " + ex); - } - }, - - _syncLoadEngines: function SRCH_SVC__syncLoadEngines(cache) { - LOG("_syncLoadEngines: start"); - // See if we have a cache file so we don't have to parse a bunch of XML. - let chromeURIs = this._findJAREngines(); - - let distDirs = []; - let locations; - try { - locations = getDir(NS_APP_DISTRIBUTION_SEARCH_DIR_LIST, - Ci.nsISimpleEnumerator); - } catch (e) { - // NS_APP_DISTRIBUTION_SEARCH_DIR_LIST is defined by each app - // so this throws during unit tests (but not xpcshell tests). - locations = {hasMoreElements: () => false}; - } - while (locations.hasMoreElements()) { - let dir = locations.getNext().QueryInterface(Ci.nsIFile); - if (dir.directoryEntries.hasMoreElements()) - distDirs.push(dir); - } - - let otherDirs = []; - let userSearchDir = getDir(NS_APP_USER_SEARCH_DIR); - locations = getDir(NS_APP_SEARCH_DIR_LIST, Ci.nsISimpleEnumerator); - while (locations.hasMoreElements()) { - let dir = locations.getNext().QueryInterface(Ci.nsIFile); - if ((!cache.engines || !dir.equals(userSearchDir)) && - dir.directoryEntries.hasMoreElements()) - otherDirs.push(dir); - } - - function modifiedDir(aDir) { - return cacheOtherPaths.get(aDir.path) != aDir.lastModifiedTime; - } - - function notInCacheVisibleEngines(aEngineName) { - return cache.visibleDefaultEngines.indexOf(aEngineName) == -1; - } - - let buildID = Services.appinfo.platformBuildID; - let cacheOtherPaths = new Map(); - if (cache.engines) { - for (let engine of cache.engines) { - if (engine._dirPath) { - cacheOtherPaths.set(engine._dirPath, engine._dirLastModifiedTime); - } - } - } - - let rebuildCache = !cache.engines || - cache.version != CACHE_VERSION || - cache.locale != getLocale() || - cache.buildID != buildID || - cacheOtherPaths.size != otherDirs.length || - otherDirs.some(d => !cacheOtherPaths.has(d.path)) || - cache.visibleDefaultEngines.length != this._visibleDefaultEngines.length || - this._visibleDefaultEngines.some(notInCacheVisibleEngines) || - otherDirs.some(modifiedDir); - - if (rebuildCache) { - LOG("_loadEngines: Absent or outdated cache. Loading engines from disk."); - distDirs.forEach(this._loadEnginesFromDir, this); - - this._loadFromChromeURLs(chromeURIs); - - LOG("_loadEngines: load user-installed engines from the obsolete cache"); - this._loadEnginesFromCache(cache, true); - - otherDirs.forEach(this._loadEnginesFromDir, this); - - this._loadEnginesMetadataFromCache(cache); - this._buildCache(); - return; - } - - LOG("_loadEngines: loading from cache directories"); - this._loadEnginesFromCache(cache); - - LOG("_loadEngines: done"); - }, - - /** - * Loads engines asynchronously. - * - * @returns {Promise} A promise, resolved successfully if loading data - * succeeds. - */ - _asyncLoadEngines: Task.async(function* (cache) { - LOG("_asyncLoadEngines: start"); - Services.obs.notifyObservers(null, SEARCH_SERVICE_TOPIC, "find-jar-engines"); - let chromeURIs = - yield checkForSyncCompletion(this._asyncFindJAREngines()); - - // Get the non-empty distribution directories into distDirs... - let distDirs = []; - let locations; - try { - locations = getDir(NS_APP_DISTRIBUTION_SEARCH_DIR_LIST, - Ci.nsISimpleEnumerator); - } catch (e) { - // NS_APP_DISTRIBUTION_SEARCH_DIR_LIST is defined by each app - // so this throws during unit tests (but not xpcshell tests). - locations = {hasMoreElements: () => false}; - } - while (locations.hasMoreElements()) { - let dir = locations.getNext().QueryInterface(Ci.nsIFile); - let iterator = new OS.File.DirectoryIterator(dir.path, - { winPattern: "*.xml" }); - try { - // Add dir to distDirs if it contains any files. - yield checkForSyncCompletion(iterator.next()); - distDirs.push(dir); - } catch (ex) { - // Catch for StopIteration exception. - if (ex.result == Cr.NS_ERROR_ALREADY_INITIALIZED) { - throw ex; - } - } finally { - iterator.close(); - } - } - - // Add the non-empty directories of NS_APP_SEARCH_DIR_LIST to - // otherDirs... - let otherDirs = []; - let userSearchDir = getDir(NS_APP_USER_SEARCH_DIR); - locations = getDir(NS_APP_SEARCH_DIR_LIST, Ci.nsISimpleEnumerator); - while (locations.hasMoreElements()) { - let dir = locations.getNext().QueryInterface(Ci.nsIFile); - if (cache.engines && dir.equals(userSearchDir)) - continue; - let iterator = new OS.File.DirectoryIterator(dir.path, - { winPattern: "*.xml" }); - try { - // Add dir to otherDirs if it contains any files. - yield checkForSyncCompletion(iterator.next()); - otherDirs.push(dir); - } catch (ex) { - // Catch for StopIteration exception. - if (ex.result == Cr.NS_ERROR_ALREADY_INITIALIZED) { - throw ex; - } - } finally { - iterator.close(); - } - } - - let hasModifiedDir = Task.async(function* (aList) { - let modifiedDir = false; - - for (let dir of aList) { - let lastModifiedTime = cacheOtherPaths.get(dir.path); - if (!lastModifiedTime) { - continue; - } - - let info = yield OS.File.stat(dir.path); - if (lastModifiedTime != info.lastModificationDate.getTime()) { - modifiedDir = true; - break; - } - } - return modifiedDir; - }); - - function notInCacheVisibleEngines(aEngineName) { - return cache.visibleDefaultEngines.indexOf(aEngineName) == -1; - } - - let buildID = Services.appinfo.platformBuildID; - let cacheOtherPaths = new Map(); - if (cache.engines) { - for (let engine of cache.engines) { - if (engine._dirPath) { - cacheOtherPaths.set(engine._dirPath, engine._dirLastModifiedTime); - } - } - } - - let rebuildCache = !cache.engines || - cache.version != CACHE_VERSION || - cache.locale != getLocale() || - cache.buildID != buildID || - cacheOtherPaths.size != otherDirs.length || - otherDirs.some(d => !cacheOtherPaths.has(d.path)) || - cache.visibleDefaultEngines.length != this._visibleDefaultEngines.length || - this._visibleDefaultEngines.some(notInCacheVisibleEngines) || - (yield checkForSyncCompletion(hasModifiedDir(otherDirs))); - - if (rebuildCache) { - LOG("_asyncLoadEngines: Absent or outdated cache. Loading engines from disk."); - for (let loadDir of distDirs) { - let enginesFromDir = - yield checkForSyncCompletion(this._asyncLoadEnginesFromDir(loadDir)); - enginesFromDir.forEach(this._addEngineToStore, this); - } - let enginesFromURLs = - yield checkForSyncCompletion(this._asyncLoadFromChromeURLs(chromeURIs)); - enginesFromURLs.forEach(this._addEngineToStore, this); - - LOG("_asyncLoadEngines: loading user-installed engines from the obsolete cache"); - this._loadEnginesFromCache(cache, true); - - for (let loadDir of otherDirs) { - let enginesFromDir = - yield checkForSyncCompletion(this._asyncLoadEnginesFromDir(loadDir)); - enginesFromDir.forEach(this._addEngineToStore, this); - } - - this._loadEnginesMetadataFromCache(cache); - this._buildCache(); - return; - } - - LOG("_asyncLoadEngines: loading from cache directories"); - this._loadEnginesFromCache(cache); - - LOG("_asyncLoadEngines: done"); - }), - - _asyncReInit: function () { - LOG("_asyncReInit"); - // Start by clearing the initialized state, so we don't abort early. - gInitialized = false; - - Task.spawn(function* () { - try { - if (this._batchTask) { - LOG("finalizing batch task"); - let task = this._batchTask; - this._batchTask = null; - yield task.finalize(); - } - - // Clear the engines, too, so we don't stick with the stale ones. - this._engines = {}; - this.__sortedEngines = null; - this._currentEngine = null; - this._visibleDefaultEngines = []; - this._metaData = {}; - this._cacheFileJSON = null; - - // Tests that want to force a synchronous re-initialization need to - // be notified when we are done uninitializing. - Services.obs.notifyObservers(null, SEARCH_SERVICE_TOPIC, - "uninit-complete"); - - let cache = {}; - cache = yield this._asyncReadCacheFile(); - if (!gInitialized && cache.metaData) - this._metaData = cache.metaData; - - yield this._asyncLoadEngines(cache); - - // Typically we'll re-init as a result of a pref observer, - // so signal to 'callers' that we're done. - Services.obs.notifyObservers(null, SEARCH_SERVICE_TOPIC, "init-complete"); - this._recordEngineTelemetry(); - gInitialized = true; - } catch (err) { - LOG("Reinit failed: " + err); - Services.obs.notifyObservers(null, SEARCH_SERVICE_TOPIC, "reinit-failed"); - } finally { - Services.obs.notifyObservers(null, SEARCH_SERVICE_TOPIC, "reinit-complete"); - } - }.bind(this)); - }, - - /** - * Read the cache file synchronously. This also imports data from the old - * search-metadata.json file if needed. - * - * @returns A JS object containing the cached data. - */ - _readCacheFile: function SRCH_SVC__readCacheFile() { - if (this._cacheFileJSON) { - return this._cacheFileJSON; - } - - let cacheFile = getDir(NS_APP_USER_PROFILE_50_DIR); - cacheFile.append(CACHE_FILENAME); - - let stream; - try { - stream = Cc["@mozilla.org/network/file-input-stream;1"]. - createInstance(Ci.nsIFileInputStream); - stream.init(cacheFile, MODE_RDONLY, FileUtils.PERMS_FILE, 0); - - let bis = Cc["@mozilla.org/binaryinputstream;1"] - .createInstance(Ci.nsIBinaryInputStream); - bis.setInputStream(stream); - - let count = stream.available(); - let array = new Uint8Array(count); - bis.readArrayBuffer(count, array.buffer); - - let bytes = Lz4.decompressFileContent(array); - let json = JSON.parse(new TextDecoder().decode(bytes)); - if (!json.engines || !json.engines.length) - throw "no engine in the file"; - return json; - } catch (ex) { - LOG("_readCacheFile: Error reading cache file: " + ex); - } finally { - stream.close(); - } - - try { - cacheFile.leafName = "search-metadata.json"; - stream = Cc["@mozilla.org/network/file-input-stream;1"]. - createInstance(Ci.nsIFileInputStream); - stream.init(cacheFile, MODE_RDONLY, FileUtils.PERMS_FILE, 0); - let metadata = parseJsonFromStream(stream); - let json = {}; - if ("[global]" in metadata) { - LOG("_readCacheFile: migrating metadata from search-metadata.json"); - let data = metadata["[global]"]; - json.metaData = {}; - let fields = ["searchDefault", "searchDefaultHash", "searchDefaultExpir", - "current", "hash", - "visibleDefaultEngines", "visibleDefaultEnginesHash"]; - for (let field of fields) { - let name = field.toLowerCase(); - if (name in data) - json.metaData[field] = data[name]; - } - } - delete metadata["[global]"]; - json._oldMetadata = metadata; - - return json; - } catch (ex) { - LOG("_readCacheFile: failed to read old metadata: " + ex); - return {}; - } finally { - stream.close(); - } - }, - - /** - * Read the cache file asynchronously. This also imports data from the old - * search-metadata.json file if needed. - * - * @returns {Promise} A promise, resolved successfully if retrieveing data - * succeeds. - */ - _asyncReadCacheFile: Task.async(function* () { - let json; - try { - let cacheFilePath = OS.Path.join(OS.Constants.Path.profileDir, CACHE_FILENAME); - let bytes = yield OS.File.read(cacheFilePath, {compression: "lz4"}); - json = JSON.parse(new TextDecoder().decode(bytes)); - if (!json.engines || !json.engines.length) - throw "no engine in the file"; - this._cacheFileJSON = json; - } catch (ex) { - LOG("_asyncReadCacheFile: Error reading cache file: " + ex); - json = {}; - - let oldMetadata = - OS.Path.join(OS.Constants.Path.profileDir, "search-metadata.json"); - try { - let bytes = yield OS.File.read(oldMetadata); - let metadata = JSON.parse(new TextDecoder().decode(bytes)); - if ("[global]" in metadata) { - LOG("_asyncReadCacheFile: migrating metadata from search-metadata.json"); - let data = metadata["[global]"]; - json.metaData = {}; - let fields = ["searchDefault", "searchDefaultHash", "searchDefaultExpir", - "current", "hash", - "visibleDefaultEngines", "visibleDefaultEnginesHash"]; - for (let field of fields) { - let name = field.toLowerCase(); - if (name in data) - json.metaData[field] = data[name]; - } - } - delete metadata["[global]"]; - json._oldMetadata = metadata; - } catch (ex) {} - } - return json; - }), - - _batchTask: null, - get batchTask() { - if (!this._batchTask) { - let task = function taskCallback() { - LOG("batchTask: Invalidating engine cache"); - this._buildCache(); - }.bind(this); - this._batchTask = new DeferredTask(task, CACHE_INVALIDATION_DELAY); - } - return this._batchTask; - }, - - _addEngineToStore: function SRCH_SVC_addEngineToStore(aEngine) { - LOG("_addEngineToStore: Adding engine: \"" + aEngine.name + "\""); - - // See if there is an existing engine with the same name. However, if this - // engine is updating another engine, it's allowed to have the same name. - var hasSameNameAsUpdate = (aEngine._engineToUpdate && - aEngine.name == aEngine._engineToUpdate.name); - if (aEngine.name in this._engines && !hasSameNameAsUpdate) { - LOG("_addEngineToStore: Duplicate engine found, aborting!"); - return; - } - - if (aEngine._engineToUpdate) { - // We need to replace engineToUpdate with the engine that just loaded. - var oldEngine = aEngine._engineToUpdate; - - // Remove the old engine from the hash, since it's keyed by name, and our - // name might change (the update might have a new name). - delete this._engines[oldEngine.name]; - - // Hack: we want to replace the old engine with the new one, but since - // people may be holding refs to the nsISearchEngine objects themselves, - // we'll just copy over all "private" properties (those without a getter - // or setter) from one object to the other. - for (var p in aEngine) { - if (!(aEngine.__lookupGetter__(p) || aEngine.__lookupSetter__(p))) - oldEngine[p] = aEngine[p]; - } - aEngine = oldEngine; - aEngine._engineToUpdate = null; - - // Add the engine back - this._engines[aEngine.name] = aEngine; - notifyAction(aEngine, SEARCH_ENGINE_CHANGED); - } else { - // Not an update, just add the new engine. - this._engines[aEngine.name] = aEngine; - // Only add the engine to the list of sorted engines if the initial list - // has already been built (i.e. if this.__sortedEngines is non-null). If - // it hasn't, we're loading engines from disk and the sorted engine list - // will be built once we need it. - if (this.__sortedEngines) { - this.__sortedEngines.push(aEngine); - this._saveSortedEngineList(); - } - notifyAction(aEngine, SEARCH_ENGINE_ADDED); - } - - if (aEngine._hasUpdates) { - // Schedule the engine's next update, if it isn't already. - if (!aEngine.getAttr("updateexpir")) - engineUpdateService.scheduleNextUpdate(aEngine); - } - }, - - _loadEnginesMetadataFromCache: function SRCH_SVC__loadEnginesMetadataFromCache(cache) { - if (cache._oldMetadata) { - // If we have old metadata in the cache, we had no valid cache - // file and read data from search-metadata.json. - for (let name in this._engines) { - let engine = this._engines[name]; - if (engine._id && cache._oldMetadata[engine._id]) - engine._metaData = cache._oldMetadata[engine._id]; - } - return; - } - - if (!cache.engines) - return; - - for (let engine of cache.engines) { - let name = engine._name; - if (name in this._engines) { - LOG("_loadEnginesMetadataFromCache, transfering metadata for " + name); - this._engines[name]._metaData = engine._metaData; - } - } - }, - - _loadEnginesFromCache: function SRCH_SVC__loadEnginesFromCache(cache, - skipReadOnly) { - if (!cache.engines) - return; - - LOG("_loadEnginesFromCache: Loading " + - cache.engines.length + " engines from cache"); - - let skippedEngines = 0; - for (let engine of cache.engines) { - if (skipReadOnly && engine._readOnly == undefined) { - ++skippedEngines; - continue; - } - - this._loadEngineFromCache(engine); - } - - if (skippedEngines) { - LOG("_loadEnginesFromCache: skipped " + skippedEngines + " read-only engines."); - } - }, - - _loadEngineFromCache: function SRCH_SVC__loadEngineFromCache(json) { - try { - let engine = new Engine(json._shortName, json._readOnly == undefined); - engine._initWithJSON(json); - this._addEngineToStore(engine); - } catch (ex) { - LOG("Failed to load " + json._name + " from cache: " + ex); - LOG("Engine JSON: " + json.toSource()); - } - }, - - _loadEnginesFromDir: function SRCH_SVC__loadEnginesFromDir(aDir) { - LOG("_loadEnginesFromDir: Searching in " + aDir.path + " for search engines."); - - // Check whether aDir is the user profile dir - var isInProfile = aDir.equals(getDir(NS_APP_USER_SEARCH_DIR)); - - var files = aDir.directoryEntries - .QueryInterface(Ci.nsIDirectoryEnumerator); - - while (files.hasMoreElements()) { - var file = files.nextFile; - - // Ignore hidden and empty files, and directories - if (!file.isFile() || file.fileSize == 0 || file.isHidden()) - continue; - - var fileURL = NetUtil.ioService.newFileURI(file).QueryInterface(Ci.nsIURL); - var fileExtension = fileURL.fileExtension.toLowerCase(); - - if (fileExtension != "xml") { - // Not an engine - continue; - } - - var addedEngine = null; - try { - addedEngine = new Engine(file, !isInProfile); - addedEngine._initFromFile(file); - if (!isInProfile && !addedEngine._isDefault) { - addedEngine._dirPath = aDir.path; - addedEngine._dirLastModifiedTime = aDir.lastModifiedTime; - } - } catch (ex) { - LOG("_loadEnginesFromDir: Failed to load " + file.path + "!\n" + ex); - continue; - } - - this._addEngineToStore(addedEngine); - } - }, - - /** - * Loads engines from a given directory asynchronously. - * - * @param aDir the directory. - * - * @returns {Promise} A promise, resolved successfully if retrieveing data - * succeeds. - */ - _asyncLoadEnginesFromDir: Task.async(function* (aDir) { - LOG("_asyncLoadEnginesFromDir: Searching in " + aDir.path + " for search engines."); - - // Check whether aDir is the user profile dir - let isInProfile = aDir.equals(getDir(NS_APP_USER_SEARCH_DIR)); - let dirPath = aDir.path; - let iterator = new OS.File.DirectoryIterator(dirPath); - - let osfiles = yield iterator.nextBatch(); - iterator.close(); - - let engines = []; - for (let osfile of osfiles) { - if (osfile.isDir || osfile.isSymLink) - continue; - - let fileInfo = yield OS.File.stat(osfile.path); - if (fileInfo.size == 0) - continue; - - let parts = osfile.path.split("."); - if (parts.length <= 1 || (parts.pop()).toLowerCase() != "xml") { - // Not an engine - continue; - } - - let addedEngine = null; - try { - let file = new FileUtils.File(osfile.path); - addedEngine = new Engine(file, !isInProfile); - yield checkForSyncCompletion(addedEngine._asyncInitFromFile(file)); - if (!isInProfile && !addedEngine._isDefault) { - addedEngine._dirPath = dirPath; - let info = yield OS.File.stat(dirPath); - addedEngine._dirLastModifiedTime = - info.lastModificationDate.getTime(); - } - engines.push(addedEngine); - } catch (ex) { - if (ex.result == Cr.NS_ERROR_ALREADY_INITIALIZED) { - throw ex; - } - LOG("_asyncLoadEnginesFromDir: Failed to load " + osfile.path + "!\n" + ex); - } - } - return engines; - }), - - _loadFromChromeURLs: function SRCH_SVC_loadFromChromeURLs(aURLs) { - aURLs.forEach(function (url) { - try { - LOG("_loadFromChromeURLs: loading engine from chrome url: " + url); - - let uri = makeURI(url); - let engine = new Engine(uri, true); - - engine._initFromURISync(uri); - - this._addEngineToStore(engine); - } catch (ex) { - LOG("_loadFromChromeURLs: failed to load engine: " + ex); - } - }, this); - }, - - /** - * Loads engines from Chrome URLs asynchronously. - * - * @param aURLs a list of URLs. - * - * @returns {Promise} A promise, resolved successfully if loading data - * succeeds. - */ - _asyncLoadFromChromeURLs: Task.async(function* (aURLs) { - let engines = []; - for (let url of aURLs) { - try { - LOG("_asyncLoadFromChromeURLs: loading engine from chrome url: " + url); - let uri = NetUtil.newURI(url); - let engine = new Engine(uri, true); - yield checkForSyncCompletion(engine._asyncInitFromURI(uri)); - engines.push(engine); - } catch (ex) { - if (ex.result == Cr.NS_ERROR_ALREADY_INITIALIZED) { - throw ex; - } - LOG("_asyncLoadFromChromeURLs: failed to load engine: " + ex); - } - } - return engines; - }), - - _convertChannelToFile: function(chan) { - let fileURI = chan.URI; - while (fileURI instanceof Ci.nsIJARURI) - fileURI = fileURI.JARFile; - fileURI.QueryInterface(Ci.nsIFileURL); - - return fileURI.file; - }, - - _findJAREngines: function SRCH_SVC_findJAREngines() { - LOG("_findJAREngines: looking for engines in JARs") - - let chan = makeChannel(APP_SEARCH_PREFIX + "list.json"); - if (!chan) { - LOG("_findJAREngines: " + APP_SEARCH_PREFIX + " isn't registered"); - return []; - } - - let uris = []; - - let sis = Cc["@mozilla.org/scriptableinputstream;1"]. - createInstance(Ci.nsIScriptableInputStream); - try { - sis.init(chan.open2()); - this._parseListJSON(sis.read(sis.available()), uris); - // parseListJSON will catch its own errors, so we - // should only go into this catch if list.json - // doesn't exist - } catch (e) { - chan = makeChannel(APP_SEARCH_PREFIX + "list.txt"); - sis.init(chan.open2()); - this._parseListTxt(sis.read(sis.available()), uris); - } - return uris; - }, - - /** - * Loads jar engines asynchronously. - * - * @returns {Promise} A promise, resolved successfully if finding jar engines - * succeeds. - */ - _asyncFindJAREngines: Task.async(function* () { - LOG("_asyncFindJAREngines: looking for engines in JARs") - - let listURL = APP_SEARCH_PREFIX + "list.json"; - let chan = makeChannel(listURL); - if (!chan) { - LOG("_asyncFindJAREngines: " + APP_SEARCH_PREFIX + " isn't registered"); - return []; - } - - let uris = []; - - // Read list.json to find the engines we need to load. - let deferred = Promise.defer(); - let request = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]. - createInstance(Ci.nsIXMLHttpRequest); - request.overrideMimeType("text/plain"); - request.onload = function(aEvent) { - deferred.resolve(aEvent.target.responseText); - }; - request.onerror = function(aEvent) { - LOG("_asyncFindJAREngines: failed to read " + listURL); - // Couldn't find list.json, try list.txt - request.onerror = function(aEvent) { - LOG("_asyncFindJAREngines: failed to read " + APP_SEARCH_PREFIX + "list.txt"); - deferred.resolve(""); - } - request.open("GET", NetUtil.newURI(APP_SEARCH_PREFIX + "list.txt").spec, true); - request.send(); - }; - request.open("GET", NetUtil.newURI(listURL).spec, true); - request.send(); - let list = yield deferred.promise; - - if (request.responseURL.endsWith(".txt")) { - this._parseListTxt(list, uris); - } else { - this._parseListJSON(list, uris); - } - return uris; - }), - - _parseListJSON: function SRCH_SVC_parseListJSON(list, uris) { - let searchSettings; - try { - searchSettings = JSON.parse(list); - } catch (e) { - LOG("failing to parse list.json: " + e); - return; - } - - let jarNames = new Set(); - for (let region in searchSettings) { - // Artifact builds use the full list.json which parses - // slightly differently - if (!("visibleDefaultEngines" in searchSettings[region])) { - continue; - } - for (let engine of searchSettings[region]["visibleDefaultEngines"]) { - jarNames.add(engine); - } - } - - // Check if we have a useable country specific list of visible default engines. - let engineNames; - let visibleDefaultEngines = this.getVerifiedGlobalAttr("visibleDefaultEngines"); - if (visibleDefaultEngines) { - engineNames = visibleDefaultEngines.split(","); - for (let engineName of engineNames) { - // If all engineName values are part of jarNames, - // then we can use the country specific list, otherwise ignore it. - // The visibleDefaultEngines string containing the name of an engine we - // don't ship indicates the server is misconfigured to answer requests - // from the specific Firefox version we are running, so ignoring the - // value altogether is safer. - if (!jarNames.has(engineName)) { - LOG("_parseListJSON: ignoring visibleDefaultEngines value because " + - engineName + " is not in the jar engines we have found"); - engineNames = null; - break; - } - } - } - - // Fallback to building a list based on the regions in the JSON - if (!engineNames || !engineNames.length) { - engineNames = searchSettings["default"]["visibleDefaultEngines"]; - } - - for (let name of engineNames) { - uris.push(APP_SEARCH_PREFIX + name + ".xml"); - } - - // Store this so that it can be used while writing the cache file. - this._visibleDefaultEngines = engineNames; - }, - - _parseListTxt: function SRCH_SVC_parseListTxt(list, uris) { - let names = list.split("\n").filter(n => !!n); - // This maps the names of our built-in engines to a boolean - // indicating whether it should be hidden by default. - let jarNames = new Map(); - for (let name of names) { - if (name.endsWith(":hidden")) { - name = name.split(":")[0]; - jarNames.set(name, true); - } else { - jarNames.set(name, false); - } - } - - // Check if we have a useable country specific list of visible default engines. - let engineNames; - let visibleDefaultEngines = this.getVerifiedGlobalAttr("visibleDefaultEngines"); - if (visibleDefaultEngines) { - engineNames = visibleDefaultEngines.split(","); - - for (let engineName of engineNames) { - // If all engineName values are part of jarNames, - // then we can use the country specific list, otherwise ignore it. - // The visibleDefaultEngines string containing the name of an engine we - // don't ship indicates the server is misconfigured to answer requests - // from the specific Firefox version we are running, so ignoring the - // value altogether is safer. - if (!jarNames.has(engineName)) { - LOG("_parseListTxt: ignoring visibleDefaultEngines value because " + - engineName + " is not in the jar engines we have found"); - engineNames = null; - break; - } - } - } - - // Fallback to building a list based on the :hidden suffixes found in list.txt. - if (!engineNames) { - engineNames = []; - for (let [name, hidden] of jarNames) { - if (!hidden) - engineNames.push(name); - } - } - - for (let name of engineNames) { - uris.push(APP_SEARCH_PREFIX + name + ".xml"); - } - - // Store this so that it can be used while writing the cache file. - this._visibleDefaultEngines = engineNames; - }, - - - _saveSortedEngineList: function SRCH_SVC_saveSortedEngineList() { - LOG("SRCH_SVC_saveSortedEngineList: starting"); - - // Set the useDB pref to indicate that from now on we should use the order - // information stored in the database. - Services.prefs.setBoolPref(BROWSER_SEARCH_PREF + "useDBForOrder", true); - - var engines = this._getSortedEngines(true); - - for (var i = 0; i < engines.length; ++i) { - engines[i].setAttr("order", i + 1); - } - - LOG("SRCH_SVC_saveSortedEngineList: done"); - }, - - _buildSortedEngineList: function SRCH_SVC_buildSortedEngineList() { - LOG("_buildSortedEngineList: building list"); - var addedEngines = { }; - this.__sortedEngines = []; - var engine; - - // If the user has specified a custom engine order, read the order - // information from the metadata instead of the default prefs. - if (Services.prefs.getBoolPref(BROWSER_SEARCH_PREF + "useDBForOrder", false)) { - LOG("_buildSortedEngineList: using db for order"); - - // Flag to keep track of whether or not we need to call _saveSortedEngineList. - let needToSaveEngineList = false; - - for (let name in this._engines) { - let engine = this._engines[name]; - var orderNumber = engine.getAttr("order"); - - // Since the DB isn't regularly cleared, and engine files may disappear - // without us knowing, we may already have an engine in this slot. If - // that happens, we just skip it - it will be added later on as an - // unsorted engine. - if (orderNumber && !this.__sortedEngines[orderNumber-1]) { - this.__sortedEngines[orderNumber-1] = engine; - addedEngines[engine.name] = engine; - } else { - // We need to call _saveSortedEngineList so this gets sorted out. - needToSaveEngineList = true; - } - } - - // Filter out any nulls for engines that may have been removed - var filteredEngines = this.__sortedEngines.filter(function(a) { return !!a; }); - if (this.__sortedEngines.length != filteredEngines.length) - needToSaveEngineList = true; - this.__sortedEngines = filteredEngines; - - if (needToSaveEngineList) - this._saveSortedEngineList(); - } else { - // The DB isn't being used, so just read the engine order from the prefs - var i = 0; - var engineName; - var prefName; - - try { - var extras = - Services.prefs.getChildList(BROWSER_SEARCH_PREF + "order.extra."); - - for (prefName of extras) { - engineName = Services.prefs.getCharPref(prefName); - - engine = this._engines[engineName]; - if (!engine || engine.name in addedEngines) - continue; - - this.__sortedEngines.push(engine); - addedEngines[engine.name] = engine; - } - } - catch (e) { } - - while (true) { - engineName = getLocalizedPref(BROWSER_SEARCH_PREF + "order." + (++i)); - if (!engineName) - break; - - engine = this._engines[engineName]; - if (!engine || engine.name in addedEngines) - continue; - - this.__sortedEngines.push(engine); - addedEngines[engine.name] = engine; - } - } - - // Array for the remaining engines, alphabetically sorted. - let alphaEngines = []; - - for (let name in this._engines) { - let engine = this._engines[name]; - if (!(engine.name in addedEngines)) - alphaEngines.push(this._engines[engine.name]); - } - - let locale = Cc["@mozilla.org/intl/nslocaleservice;1"] - .getService(Ci.nsILocaleService) - .newLocale(getLocale()); - let collation = Cc["@mozilla.org/intl/collation-factory;1"] - .createInstance(Ci.nsICollationFactory) - .CreateCollation(locale); - const strength = Ci.nsICollation.kCollationCaseInsensitiveAscii; - let comparator = (a, b) => collation.compareString(strength, a.name, b.name); - alphaEngines.sort(comparator); - return this.__sortedEngines = this.__sortedEngines.concat(alphaEngines); - }, - - /** - * Get a sorted array of engines. - * @param aWithHidden - * True if hidden plugins should be included in the result. - */ - _getSortedEngines: function SRCH_SVC_getSorted(aWithHidden) { - if (aWithHidden) - return this._sortedEngines; - - return this._sortedEngines.filter(function (engine) { - return !engine.hidden; - }); - }, - - // nsIBrowserSearchService - init: function SRCH_SVC_init(observer) { - LOG("SearchService.init"); - let self = this; - if (!this._initStarted) { - this._initStarted = true; - Task.spawn(function* task() { - try { - // Complete initialization by calling asynchronous initializer. - yield self._asyncInit(); - } catch (ex) { - if (ex.result == Cr.NS_ERROR_ALREADY_INITIALIZED) { - // No need to pursue asynchronous because synchronous fallback was - // called and has finished. - } else { - self._initObservers.reject(ex); - } - } - }); - } - if (observer) { - this._initObservers.promise.then( - function onSuccess() { - try { - observer.onInitComplete(self._initRV); - } catch (e) { - Cu.reportError(e); - } - }, - function onError(aReason) { - Cu.reportError("Internal error while initializing SearchService: " + aReason); - observer.onInitComplete(Components.results.NS_ERROR_UNEXPECTED); - } - ); - } - }, - - get isInitialized() { - return gInitialized; - }, - - getEngines: function SRCH_SVC_getEngines(aCount) { - this._ensureInitialized(); - LOG("getEngines: getting all engines"); - var engines = this._getSortedEngines(true); - aCount.value = engines.length; - return engines; - }, - - getVisibleEngines: function SRCH_SVC_getVisible(aCount) { - this._ensureInitialized(); - LOG("getVisibleEngines: getting all visible engines"); - var engines = this._getSortedEngines(false); - aCount.value = engines.length; - return engines; - }, - - getDefaultEngines: function SRCH_SVC_getDefault(aCount) { - this._ensureInitialized(); - function isDefault(engine) { - return engine._isDefault; - } - var engines = this._sortedEngines.filter(isDefault); - var engineOrder = {}; - var engineName; - var i = 1; - - // Build a list of engines which we have ordering information for. - // We're rebuilding the list here because _sortedEngines contain the - // current order, but we want the original order. - - // First, look at the "browser.search.order.extra" branch. - try { - var extras = Services.prefs.getChildList(BROWSER_SEARCH_PREF + "order.extra."); - - for (var prefName of extras) { - engineName = Services.prefs.getCharPref(prefName); - - if (!(engineName in engineOrder)) - engineOrder[engineName] = i++; - } - } catch (e) { - LOG("Getting extra order prefs failed: " + e); - } - - // Now look through the "browser.search.order" branch. - for (var j = 1; ; j++) { - engineName = getLocalizedPref(BROWSER_SEARCH_PREF + "order." + j); - if (!engineName) - break; - - if (!(engineName in engineOrder)) - engineOrder[engineName] = i++; - } - - LOG("getDefaultEngines: engineOrder: " + engineOrder.toSource()); - - function compareEngines (a, b) { - var aIdx = engineOrder[a.name]; - var bIdx = engineOrder[b.name]; - - if (aIdx && bIdx) - return aIdx - bIdx; - if (aIdx) - return -1; - if (bIdx) - return 1; - - return a.name.localeCompare(b.name); - } - engines.sort(compareEngines); - - aCount.value = engines.length; - return engines; - }, - - getEngineByName: function SRCH_SVC_getEngineByName(aEngineName) { - this._ensureInitialized(); - return this._engines[aEngineName] || null; - }, - - getEngineByAlias: function SRCH_SVC_getEngineByAlias(aAlias) { - this._ensureInitialized(); - for (var engineName in this._engines) { - var engine = this._engines[engineName]; - if (engine && engine.alias == aAlias) - return engine; - } - return null; - }, - - addEngineWithDetails: function SRCH_SVC_addEWD(aName, aIconURL, aAlias, - aDescription, aMethod, - aTemplate, aExtensionID) { - this._ensureInitialized(); - if (!aName) - FAIL("Invalid name passed to addEngineWithDetails!"); - if (!aMethod) - FAIL("Invalid method passed to addEngineWithDetails!"); - if (!aTemplate) - FAIL("Invalid template passed to addEngineWithDetails!"); - if (this._engines[aName]) - FAIL("An engine with that name already exists!", Cr.NS_ERROR_FILE_ALREADY_EXISTS); - - var engine = new Engine(sanitizeName(aName), false); - engine._initFromMetadata(aName, aIconURL, aAlias, aDescription, - aMethod, aTemplate, aExtensionID); - engine._loadPath = "[other]addEngineWithDetails"; - this._addEngineToStore(engine); - }, - - addEngine: function SRCH_SVC_addEngine(aEngineURL, aDataType, aIconURL, - aConfirm, aCallback) { - LOG("addEngine: Adding \"" + aEngineURL + "\"."); - this._ensureInitialized(); - try { - var uri = makeURI(aEngineURL); - var engine = new Engine(uri, false); - if (aCallback) { - engine._installCallback = function (errorCode) { - try { - if (errorCode == null) - aCallback.onSuccess(engine); - else - aCallback.onError(errorCode); - } catch (ex) { - Cu.reportError("Error invoking addEngine install callback: " + ex); - } - // Clear the reference to the callback now that it's been invoked. - engine._installCallback = null; - }; - } - engine._initFromURIAndLoad(uri); - } catch (ex) { - // Drop the reference to the callback, if set - if (engine) - engine._installCallback = null; - FAIL("addEngine: Error adding engine:\n" + ex, Cr.NS_ERROR_FAILURE); - } - engine._setIcon(aIconURL, false); - engine._confirm = aConfirm; - }, - - removeEngine: function SRCH_SVC_removeEngine(aEngine) { - this._ensureInitialized(); - if (!aEngine) - FAIL("no engine passed to removeEngine!"); - - var engineToRemove = null; - for (var e in this._engines) { - if (aEngine.wrappedJSObject == this._engines[e]) - engineToRemove = this._engines[e]; - } - - if (!engineToRemove) - FAIL("removeEngine: Can't find engine to remove!", Cr.NS_ERROR_FILE_NOT_FOUND); - - if (engineToRemove == this.currentEngine) { - this._currentEngine = null; - } - - if (engineToRemove._readOnly) { - // Just hide it (the "hidden" setter will notify) and remove its alias to - // avoid future conflicts with other engines. - engineToRemove.hidden = true; - engineToRemove.alias = null; - } else { - // Remove the engine file from disk if we had a legacy file in the profile. - if (engineToRemove._filePath) { - let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile); - file.persistentDescriptor = engineToRemove._filePath; - if (file.exists()) { - file.remove(false); - } - engineToRemove._filePath = null; - } - - // Remove the engine from _sortedEngines - var index = this._sortedEngines.indexOf(engineToRemove); - if (index == -1) - FAIL("Can't find engine to remove in _sortedEngines!", Cr.NS_ERROR_FAILURE); - this.__sortedEngines.splice(index, 1); - - // Remove the engine from the internal store - delete this._engines[engineToRemove.name]; - - // Since we removed an engine, we need to update the preferences. - this._saveSortedEngineList(); - } - notifyAction(engineToRemove, SEARCH_ENGINE_REMOVED); - }, - - moveEngine: function SRCH_SVC_moveEngine(aEngine, aNewIndex) { - this._ensureInitialized(); - if ((aNewIndex > this._sortedEngines.length) || (aNewIndex < 0)) - FAIL("SRCH_SVC_moveEngine: Index out of bounds!"); - if (!(aEngine instanceof Ci.nsISearchEngine)) - FAIL("SRCH_SVC_moveEngine: Invalid engine passed to moveEngine!"); - if (aEngine.hidden) - FAIL("moveEngine: Can't move a hidden engine!", Cr.NS_ERROR_FAILURE); - - var engine = aEngine.wrappedJSObject; - - var currentIndex = this._sortedEngines.indexOf(engine); - if (currentIndex == -1) - FAIL("moveEngine: Can't find engine to move!", Cr.NS_ERROR_UNEXPECTED); - - // Our callers only take into account non-hidden engines when calculating - // aNewIndex, but we need to move it in the array of all engines, so we - // need to adjust aNewIndex accordingly. To do this, we count the number - // of hidden engines in the list before the engine that we're taking the - // place of. We do this by first finding newIndexEngine (the engine that - // we were supposed to replace) and then iterating through the complete - // engine list until we reach it, increasing aNewIndex for each hidden - // engine we find on our way there. - // - // This could be further simplified by having our caller pass in - // newIndexEngine directly instead of aNewIndex. - var newIndexEngine = this._getSortedEngines(false)[aNewIndex]; - if (!newIndexEngine) - FAIL("moveEngine: Can't find engine to replace!", Cr.NS_ERROR_UNEXPECTED); - - for (var i = 0; i < this._sortedEngines.length; ++i) { - if (newIndexEngine == this._sortedEngines[i]) - break; - if (this._sortedEngines[i].hidden) - aNewIndex++; - } - - if (currentIndex == aNewIndex) - return; // nothing to do! - - // Move the engine - var movedEngine = this.__sortedEngines.splice(currentIndex, 1)[0]; - this.__sortedEngines.splice(aNewIndex, 0, movedEngine); - - notifyAction(engine, SEARCH_ENGINE_CHANGED); - - // Since we moved an engine, we need to update the preferences. - this._saveSortedEngineList(); - }, - - restoreDefaultEngines: function SRCH_SVC_resetDefaultEngines() { - this._ensureInitialized(); - for (let name in this._engines) { - let e = this._engines[name]; - // Unhide all default engines - if (e.hidden && e._isDefault) - e.hidden = false; - } - }, - - get defaultEngine() { return this.currentEngine; }, - - set defaultEngine(val) { - this.currentEngine = val; - }, - - get currentEngine() { - this._ensureInitialized(); - if (!this._currentEngine) { - let name = this.getGlobalAttr("current"); - let engine = this.getEngineByName(name); - if (engine && (this.getGlobalAttr("hash") == getVerificationHash(name) || - engine._isDefault)) { - // If the current engine is a default one, we can relax the - // verification hash check to reduce the annoyance for users who - // backup/sync their profile in custom ways. - this._currentEngine = engine; - } - if (!name) - this._currentEngine = this.originalDefaultEngine; - } - - // If the current engine is not set or hidden, we fallback... - if (!this._currentEngine || this._currentEngine.hidden) { - // first to the original default engine - let originalDefault = this.originalDefaultEngine; - if (!originalDefault || originalDefault.hidden) { - // then to the first visible engine - let firstVisible = this._getSortedEngines(false)[0]; - if (firstVisible && !firstVisible.hidden) { - this.currentEngine = firstVisible; - return firstVisible; - } - // and finally as a last resort we unhide the original default engine. - if (originalDefault) - originalDefault.hidden = false; - } - if (!originalDefault) - return null; - - // If the current engine wasn't set or was hidden, we used a fallback - // to pick a new current engine. As soon as we return it, this new - // current engine will become user-visible, so we should persist it. - // by calling the setter. - this.currentEngine = originalDefault; - } - - return this._currentEngine; - }, - - set currentEngine(val) { - this._ensureInitialized(); - // Sometimes we get wrapped nsISearchEngine objects (external XPCOM callers), - // and sometimes we get raw Engine JS objects (callers in this file), so - // handle both. - if (!(val instanceof Ci.nsISearchEngine) && !(val instanceof Engine)) - FAIL("Invalid argument passed to currentEngine setter"); - - var newCurrentEngine = this.getEngineByName(val.name); - if (!newCurrentEngine) - FAIL("Can't find engine in store!", Cr.NS_ERROR_UNEXPECTED); - - if (!newCurrentEngine._isDefault) { - // If a non default engine is being set as the current engine, ensure - // its loadPath has a verification hash. - if (!newCurrentEngine._loadPath) - newCurrentEngine._loadPath = "[other]unknown"; - let loadPathHash = getVerificationHash(newCurrentEngine._loadPath); - let currentHash = newCurrentEngine.getAttr("loadPathHash"); - if (!currentHash || currentHash != loadPathHash) { - newCurrentEngine.setAttr("loadPathHash", loadPathHash); - notifyAction(newCurrentEngine, SEARCH_ENGINE_CHANGED); - } - } - - if (newCurrentEngine == this._currentEngine) - return; - - this._currentEngine = newCurrentEngine; - - // If we change the default engine in the future, that change should impact - // users who have switched away from and then back to the build's "default" - // engine. So clear the user pref when the currentEngine is set to the - // build's default engine, so that the currentEngine getter falls back to - // whatever the default is. - let newName = this._currentEngine.name; - if (this._currentEngine == this.originalDefaultEngine) { - newName = ""; - } - - this.setGlobalAttr("current", newName); - this.setGlobalAttr("hash", getVerificationHash(newName)); - - notifyAction(this._currentEngine, SEARCH_ENGINE_DEFAULT); - notifyAction(this._currentEngine, SEARCH_ENGINE_CURRENT); - }, - - getDefaultEngineInfo() { - let result = {}; - - let engine; - try { - engine = this.defaultEngine; - } catch (e) { - // The defaultEngine getter will throw if there's no engine at all, - // which shouldn't happen unless an add-on or a test deleted all of them. - // Our preferences UI doesn't let users do that. - Cu.reportError("getDefaultEngineInfo: No default engine"); - } - - if (!engine) { - result.name = "NONE"; - } else { - if (engine.name) - result.name = engine.name; - - result.loadPath = engine._loadPath; - - let origin; - if (engine._isDefault) - origin = "default"; - else { - let currentHash = engine.getAttr("loadPathHash"); - if (!currentHash) - origin = "unverified"; - else { - let loadPathHash = getVerificationHash(engine._loadPath); - origin = currentHash == loadPathHash ? "verified" : "invalid"; - } - } - result.origin = origin; - - // For privacy, we only collect the submission URL for default engines... - let sendSubmissionURL = engine._isDefault; - - // ... or engines sorted by default near the top of the list. - if (!sendSubmissionURL) { - let extras = - Services.prefs.getChildList(BROWSER_SEARCH_PREF + "order.extra."); - - for (let prefName of extras) { - try { - if (result.name == Services.prefs.getCharPref(prefName)) { - sendSubmissionURL = true; - break; - } - } catch (e) {} - } - - let i = 0; - while (!sendSubmissionURL) { - let engineName = getLocalizedPref(BROWSER_SEARCH_PREF + "order." + (++i)); - if (!engineName) - break; - if (result.name == engineName) { - sendSubmissionURL = true; - break; - } - } - } - - if (sendSubmissionURL) { - let uri = engine._getURLOfType("text/html") - .getSubmission("", engine, "searchbar").uri; - uri.userPass = ""; // Avoid reporting a username or password. - result.submissionURL = uri.spec; - } - } - - return result; - }, - - _recordEngineTelemetry: function() { - Services.telemetry.getHistogramById("SEARCH_SERVICE_ENGINE_COUNT") - .add(Object.keys(this._engines).length); - let hasUpdates = false; - let hasIconUpdates = false; - for (let name in this._engines) { - let engine = this._engines[name]; - if (engine._hasUpdates) { - hasUpdates = true; - if (engine._iconUpdateURL) { - hasIconUpdates = true; - break; - } - } - } - Services.telemetry.getHistogramById("SEARCH_SERVICE_HAS_UPDATES").add(hasUpdates); - Services.telemetry.getHistogramById("SEARCH_SERVICE_HAS_ICON_UPDATES").add(hasIconUpdates); - }, - - /** - * This map is built lazily after the available search engines change. It - * allows quick parsing of an URL representing a search submission into the - * search engine name and original terms. - * - * The keys are strings containing the domain name and lowercase path of the - * engine submission, for example "www.google.com/search". - * - * The values are objects with these properties: - * { - * engine: The associated nsISearchEngine. - * termsParameterName: Name of the URL parameter containing the search - * terms, for example "q". - * } - */ - _parseSubmissionMap: null, - - _buildParseSubmissionMap: function SRCH_SVC__buildParseSubmissionMap() { - LOG("_buildParseSubmissionMap"); - this._parseSubmissionMap = new Map(); - - // Used only while building the map, indicates which entries do not refer to - // the main domain of the engine but to an alternate domain, for example - // "www.google.fr" for the "www.google.com" search engine. - let keysOfAlternates = new Set(); - - for (let engine of this._sortedEngines) { - LOG("Processing engine: " + engine.name); - - if (engine.hidden) { - LOG("Engine is hidden."); - continue; - } - - let urlParsingInfo = engine.getURLParsingInfo(); - if (!urlParsingInfo) { - LOG("Engine does not support URL parsing."); - continue; - } - - // Store the same object on each matching map key, as an optimization. - let mapValueForEngine = { - engine: engine, - termsParameterName: urlParsingInfo.termsParameterName, - }; - - let processDomain = (domain, isAlternate) => { - let key = domain + urlParsingInfo.path; - - // Apply the logic for which main domains take priority over alternate - // domains, even if they are found later in the ordered engine list. - let existingEntry = this._parseSubmissionMap.get(key); - if (!existingEntry) { - LOG("Adding new entry: " + key); - if (isAlternate) { - keysOfAlternates.add(key); - } - } else if (!isAlternate && keysOfAlternates.has(key)) { - LOG("Overriding alternate entry: " + key + - " (" + existingEntry.engine.name + ")"); - keysOfAlternates.delete(key); - } else { - LOG("Keeping existing entry: " + key + - " (" + existingEntry.engine.name + ")"); - return; - } - - this._parseSubmissionMap.set(key, mapValueForEngine); - }; - - processDomain(urlParsingInfo.mainDomain, false); - SearchStaticData.getAlternateDomains(urlParsingInfo.mainDomain) - .forEach(d => processDomain(d, true)); - } - }, - - /** - * Checks to see if any engine has an EngineURL of type URLTYPE_SEARCH_HTML - * for this request-method, template URL, and query params. - */ - hasEngineWithURL: function(method, template, formData) { - this._ensureInitialized(); - - // Quick helper method to ensure formData filtered/sorted for compares. - let getSortedFormData = data => { - return data.filter(a => a.name && a.value).sort((a, b) => { - if (a.name > b.name) { - return 1; - } else if (b.name > a.name) { - return -1; - } else if (a.value > b.value) { - return 1; - } - return (b.value > a.value) ? -1 : 0; - }); - }; - - // Sanitize method, ensure formData is pre-sorted. - let methodUpper = method.toUpperCase(); - let sortedFormData = getSortedFormData(formData); - let sortedFormLength = sortedFormData.length; - - return this._getSortedEngines(false).some(engine => { - return engine._urls.some(url => { - // Not an engineURL match if type, method, url, #params don't match. - if (url.type != URLTYPE_SEARCH_HTML || - url.method != methodUpper || - url.template != template || - url.params.length != sortedFormLength) { - return false; - } - - // Ensure engineURL formData is pre-sorted. Then, we're - // not an engineURL match if any queryParam doesn't compare. - let sortedParams = getSortedFormData(url.params); - for (let i = 0; i < sortedFormLength; i++) { - let formData = sortedFormData[i]; - let param = sortedParams[i]; - if (param.name != formData.name || - param.value != formData.value || - param.purpose != formData.purpose) { - return false; - } - } - // Else we're a match. - return true; - }); - }); - }, - - parseSubmissionURL: function SRCH_SVC_parseSubmissionURL(aURL) { - this._ensureInitialized(); - LOG("parseSubmissionURL: Parsing \"" + aURL + "\"."); - - if (!this._parseSubmissionMap) { - this._buildParseSubmissionMap(); - } - - // Extract the elements of the provided URL first. - let soughtKey, soughtQuery; - try { - let soughtUrl = NetUtil.newURI(aURL).QueryInterface(Ci.nsIURL); - - // Exclude any URL that is not HTTP or HTTPS from the beginning. - if (soughtUrl.scheme != "http" && soughtUrl.scheme != "https") { - LOG("The URL scheme is not HTTP or HTTPS."); - return gEmptyParseSubmissionResult; - } - - // Reading these URL properties may fail and raise an exception. - soughtKey = soughtUrl.host + soughtUrl.filePath.toLowerCase(); - soughtQuery = soughtUrl.query; - } catch (ex) { - // Errors while parsing the URL or accessing the properties are not fatal. - LOG("The value does not look like a structured URL."); - return gEmptyParseSubmissionResult; - } - - // Look up the domain and path in the map to identify the search engine. - let mapEntry = this._parseSubmissionMap.get(soughtKey); - if (!mapEntry) { - LOG("No engine associated with domain and path: " + soughtKey); - return gEmptyParseSubmissionResult; - } - - // Extract the search terms from the parameter, for example "caff%C3%A8" - // from the URL "https://www.google.com/search?q=caff%C3%A8&client=firefox". - let encodedTerms = null; - for (let param of soughtQuery.split("&")) { - let equalPos = param.indexOf("="); - if (equalPos != -1 && - param.substr(0, equalPos) == mapEntry.termsParameterName) { - // This is the parameter we are looking for. - encodedTerms = param.substr(equalPos + 1); - break; - } - } - if (encodedTerms === null) { - LOG("Missing terms parameter: " + mapEntry.termsParameterName); - return gEmptyParseSubmissionResult; - } - - let length = 0; - let offset = aURL.indexOf("?") + 1; - let query = aURL.slice(offset); - // Iterate a second time over the original input string to determine the - // correct search term offset and length in the original encoding. - for (let param of query.split("&")) { - let equalPos = param.indexOf("="); - if (equalPos != -1 && - param.substr(0, equalPos) == mapEntry.termsParameterName) { - // This is the parameter we are looking for. - offset += equalPos + 1; - length = param.length - equalPos - 1; - break; - } - offset += param.length + 1; - } - - // Decode the terms using the charset defined in the search engine. - let terms; - try { - terms = gTextToSubURI.UnEscapeAndConvert( - mapEntry.engine.queryCharset, - encodedTerms.replace(/\+/g, " ")); - } catch (ex) { - // Decoding errors will cause this match to be ignored. - LOG("Parameter decoding failed. Charset: " + - mapEntry.engine.queryCharset); - return gEmptyParseSubmissionResult; - } - - LOG("Match found. Terms: " + terms); - return new ParseSubmissionResult(mapEntry.engine, terms, offset, length); - }, - - // nsIObserver - observe: function SRCH_SVC_observe(aEngine, aTopic, aVerb) { - switch (aTopic) { - case SEARCH_ENGINE_TOPIC: - switch (aVerb) { - case SEARCH_ENGINE_LOADED: - var engine = aEngine.QueryInterface(Ci.nsISearchEngine); - LOG("nsSearchService::observe: Done installation of " + engine.name - + "."); - this._addEngineToStore(engine.wrappedJSObject); - if (engine.wrappedJSObject._useNow) { - LOG("nsSearchService::observe: setting current"); - this.currentEngine = aEngine; - } - // The addition of the engine to the store always triggers an ADDED - // or a CHANGED notification, that will trigger the task below. - break; - case SEARCH_ENGINE_ADDED: - case SEARCH_ENGINE_CHANGED: - case SEARCH_ENGINE_REMOVED: - this.batchTask.disarm(); - this.batchTask.arm(); - // Invalidate the map used to parse URLs to search engines. - this._parseSubmissionMap = null; - break; - } - break; - - case QUIT_APPLICATION_TOPIC: - this._removeObservers(); - break; - - case "nsPref:changed": - if (aVerb == LOCALE_PREF) { - // Locale changed. Re-init. We rely on observers, because we can't - // return this promise to anyone. - this._asyncReInit(); - break; - } - } - }, - - // nsITimerCallback - notify: function SRCH_SVC_notify(aTimer) { - LOG("_notify: checking for updates"); - - if (!Services.prefs.getBoolPref(BROWSER_SEARCH_PREF + "update", true)) - return; - - // Our timer has expired, but unfortunately, we can't get any data from it. - // Therefore, we need to walk our engine-list, looking for expired engines - var currentTime = Date.now(); - LOG("currentTime: " + currentTime); - for (let name in this._engines) { - let engine = this._engines[name].wrappedJSObject; - if (!engine._hasUpdates) - continue; - - LOG("checking " + engine.name); - - var expirTime = engine.getAttr("updateexpir"); - LOG("expirTime: " + expirTime + "\nupdateURL: " + engine._updateURL + - "\niconUpdateURL: " + engine._iconUpdateURL); - - var engineExpired = expirTime <= currentTime; - - if (!expirTime || !engineExpired) { - LOG("skipping engine"); - continue; - } - - LOG(engine.name + " has expired"); - - engineUpdateService.update(engine); - - // Schedule the next update - engineUpdateService.scheduleNextUpdate(engine); - - } // end engine iteration - }, - - _addObservers: function SRCH_SVC_addObservers() { - if (this._observersAdded) { - // There might be a race between synchronous and asynchronous - // initialization for which we try to register the observers twice. - return; - } - this._observersAdded = true; - - Services.obs.addObserver(this, SEARCH_ENGINE_TOPIC, false); - Services.obs.addObserver(this, QUIT_APPLICATION_TOPIC, false); - - // The current stage of shutdown. Used to help analyze crash - // signatures in case of shutdown timeout. - let shutdownState = { - step: "Not started", - latestError: { - message: undefined, - stack: undefined - } - }; - OS.File.profileBeforeChange.addBlocker( - "Search service: shutting down", - () => Task.spawn(function* () { - if (this._batchTask) { - shutdownState.step = "Finalizing batched task"; - try { - yield this._batchTask.finalize(); - shutdownState.step = "Batched task finalized"; - } catch (ex) { - shutdownState.step = "Batched task failed to finalize"; - - shutdownState.latestError.message = "" + ex; - if (ex && typeof ex == "object") { - shutdownState.latestError.stack = ex.stack || undefined; - } - - // Ensure that error is reported and that it causes tests - // to fail. - Promise.reject(ex); - } - } - }.bind(this)), - - () => shutdownState - ); - }, - _observersAdded: false, - - _removeObservers: function SRCH_SVC_removeObservers() { - Services.obs.removeObserver(this, SEARCH_ENGINE_TOPIC); - Services.obs.removeObserver(this, QUIT_APPLICATION_TOPIC); - }, - - QueryInterface: XPCOMUtils.generateQI([ - Ci.nsIBrowserSearchService, - Ci.nsIObserver, - Ci.nsITimerCallback - ]) -}; - - -const SEARCH_UPDATE_LOG_PREFIX = "*** Search update: "; - -/** - * Outputs aText to the JavaScript console as well as to stdout, if the search - * logging pref (browser.search.update.log) is set to true. - */ -function ULOG(aText) { - if (Services.prefs.getBoolPref(BROWSER_SEARCH_PREF + "update.log", false)) { - dump(SEARCH_UPDATE_LOG_PREFIX + aText + "\n"); - Services.console.logStringMessage(aText); - } -} - -var engineUpdateService = { - scheduleNextUpdate: function eus_scheduleNextUpdate(aEngine) { - var interval = aEngine._updateInterval || SEARCH_DEFAULT_UPDATE_INTERVAL; - var milliseconds = interval * 86400000; // |interval| is in days - aEngine.setAttr("updateexpir", Date.now() + milliseconds); - }, - - update: function eus_Update(aEngine) { - let engine = aEngine.wrappedJSObject; - ULOG("update called for " + aEngine._name); - if (!Services.prefs.getBoolPref(BROWSER_SEARCH_PREF + "update", true) || !engine._hasUpdates) - return; - - let testEngine = null; - let updateURL = engine._getURLOfType(URLTYPE_OPENSEARCH); - let updateURI = (updateURL && updateURL._hasRelation("self")) ? - updateURL.getSubmission("", engine).uri : - makeURI(engine._updateURL); - if (updateURI) { - if (engine._isDefault && !updateURI.schemeIs("https")) { - ULOG("Invalid scheme for default engine update"); - return; - } - - ULOG("updating " + engine.name + " from " + updateURI.spec); - testEngine = new Engine(updateURI, false); - testEngine._engineToUpdate = engine; - testEngine._initFromURIAndLoad(updateURI); - } else - ULOG("invalid updateURI"); - - if (engine._iconUpdateURL) { - // If we're updating the engine too, use the new engine object, - // otherwise use the existing engine object. - (testEngine || engine)._setIcon(engine._iconUpdateURL, true); - } - } -}; - -this.NSGetFactory = XPCOMUtils.generateNSGetFactory([SearchService]); diff --git a/application/basilisk/components/search/service/nsSearchSuggestions.js b/application/basilisk/components/search/service/nsSearchSuggestions.js deleted file mode 100644 index a05d8b4b4..000000000 --- a/application/basilisk/components/search/service/nsSearchSuggestions.js +++ /dev/null @@ -1,197 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -const { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components; - -Cu.import("resource://gre/modules/XPCOMUtils.jsm"); -Cu.import("resource://gre/modules/nsFormAutoCompleteResult.jsm"); -Cu.import("resource://gre/modules/Services.jsm"); -XPCOMUtils.defineLazyModuleGetter(this, "SearchSuggestionController", - "resource://gre/modules/SearchSuggestionController.jsm"); - -/** - * SuggestAutoComplete is a base class that implements nsIAutoCompleteSearch - * and can collect results for a given search by using this._suggestionController. - * We do it this way since the AutoCompleteController in Mozilla requires a - * unique XPCOM Service for every search provider, even if the logic for two - * providers is identical. - * @constructor - */ -function SuggestAutoComplete() { - this._init(); -} -SuggestAutoComplete.prototype = { - - _init: function() { - this._suggestionController = new SearchSuggestionController(obj => this.onResultsReturned(obj)); - this._suggestionController.maxLocalResults = this._historyLimit; - }, - - get _suggestionLabel() { - let bundle = Services.strings.createBundle("chrome://global/locale/search/search.properties"); - let label = bundle.GetStringFromName("suggestion_label"); - Object.defineProperty(SuggestAutoComplete.prototype, "_suggestionLabel", {value: label}); - return label; - }, - - /** - * The object implementing nsIAutoCompleteObserver that we notify when - * we have found results - * @private - */ - _listener: null, - - /** - * Maximum number of history items displayed. This is capped at 7 - * because the primary consumer (Firefox search bar) displays 10 rows - * by default, and so we want to leave some space for suggestions - * to be visible. - */ - _historyLimit: 7, - - /** - * Callback for handling results from SearchSuggestionController.jsm - * @private - */ - onResultsReturned: function(results) { - let finalResults = []; - let finalComments = []; - - // If form history has results, add them to the list. - for (let i = 0; i < results.local.length; ++i) { - finalResults.push(results.local[i]); - finalComments.push(""); - } - - // If there are remote matches, add them. - if (results.remote.length) { - // "comments" column values for suggestions starts as empty strings - let comments = new Array(results.remote.length).fill("", 1); - comments[0] = this._suggestionLabel; - // now put the history results above the suggestions - finalResults = finalResults.concat(results.remote); - finalComments = finalComments.concat(comments); - } - - // Notify the FE of our new results - this.onResultsReady(results.term, finalResults, finalComments, results.formHistoryResult); - }, - - /** - * Notifies the front end of new results. - * @param searchString the user's query string - * @param results an array of results to the search - * @param comments an array of metadata corresponding to the results - * @private - */ - onResultsReady: function(searchString, results, comments, formHistoryResult) { - if (this._listener) { - // Create a copy of the results array to use as labels, since - // FormAutoCompleteResult doesn't like being passed the same array - // for both. - let labels = results.slice(); - let result = new FormAutoCompleteResult( - searchString, - Ci.nsIAutoCompleteResult.RESULT_SUCCESS, - 0, - "", - results, - labels, - comments, - formHistoryResult); - - this._listener.onSearchResult(this, result); - - // Null out listener to make sure we don't notify it twice - this._listener = null; - } - }, - - /** - * Initiates the search result gathering process. Part of - * nsIAutoCompleteSearch implementation. - * - * @param searchString the user's query string - * @param searchParam unused, "an extra parameter"; even though - * this parameter and the next are unused, pass - * them through in case the form history - * service wants them - * @param previousResult unused, a client-cached store of the previous - * generated resultset for faster searching. - * @param listener object implementing nsIAutoCompleteObserver which - * we notify when results are ready. - */ - startSearch: function(searchString, searchParam, previousResult, listener) { - // Don't reuse a previous form history result when it no longer applies. - if (!previousResult) - this._formHistoryResult = null; - - var formHistorySearchParam = searchParam.split("|")[0]; - - // Receive the information about the privacy mode of the window to which - // this search box belongs. The front-end's search.xml bindings passes this - // information in the searchParam parameter. The alternative would have - // been to modify nsIAutoCompleteSearch to add an argument to startSearch - // and patch all of autocomplete to be aware of this, but the searchParam - // argument is already an opaque argument, so this solution is hopefully - // less hackish (although still gross.) - var privacyMode = (searchParam.split("|")[1] == "private"); - - // Start search immediately if possible, otherwise once the search - // service is initialized - if (Services.search.isInitialized) { - this._triggerSearch(searchString, formHistorySearchParam, listener, privacyMode); - return; - } - - Services.search.init((function startSearch_cb(aResult) { - if (!Components.isSuccessCode(aResult)) { - Cu.reportError("Could not initialize search service, bailing out: " + aResult); - return; - } - this._triggerSearch(searchString, formHistorySearchParam, listener, privacyMode); - }).bind(this)); - }, - - /** - * Actual implementation of search. - */ - _triggerSearch: function(searchString, searchParam, listener, privacyMode) { - this._listener = listener; - this._suggestionController.fetch(searchString, - privacyMode, - Services.search.currentEngine); - }, - - /** - * Ends the search result gathering process. Part of nsIAutoCompleteSearch - * implementation. - */ - stopSearch: function() { - this._suggestionController.stop(); - }, - - // nsISupports - QueryInterface: XPCOMUtils.generateQI([Ci.nsIAutoCompleteSearch, - Ci.nsIAutoCompleteObserver]) -}; - -/** - * SearchSuggestAutoComplete is a service implementation that handles suggest - * results specific to web searches. - * @constructor - */ -function SearchSuggestAutoComplete() { - // This calls _init() in the parent class (SuggestAutoComplete) via the - // prototype, below. - this._init(); -} -SearchSuggestAutoComplete.prototype = { - classID: Components.ID("{aa892eb4-ffbf-477d-9f9a-06c995ae9f27}"), - __proto__: SuggestAutoComplete.prototype, - serviceURL: "" -}; - -var component = [SearchSuggestAutoComplete]; -this.NSGetFactory = XPCOMUtils.generateNSGetFactory(component); diff --git a/application/basilisk/components/search/service/nsSidebar.js b/application/basilisk/components/search/service/nsSidebar.js deleted file mode 100644 index 63976cba7..000000000 --- a/application/basilisk/components/search/service/nsSidebar.js +++ /dev/null @@ -1,66 +0,0 @@ -/* -*- indent-tabs-mode: nil; js-indent-level: 4 -*- */ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - -const { interfaces: Ci, utils: Cu } = Components; - -Cu.import("resource://gre/modules/XPCOMUtils.jsm"); - -// File extension for Sherlock search plugin description files -const SHERLOCK_FILE_EXT_REGEXP = /\.src$/i; - -function nsSidebar() { -} - -nsSidebar.prototype = { - init: function(window) { - this.window = window; - try { - this.mm = window.QueryInterface(Ci.nsIInterfaceRequestor) - .getInterface(Ci.nsIDocShell) - .QueryInterface(Ci.nsIInterfaceRequestor) - .getInterface(Ci.nsIContentFrameMessageManager); - } catch (e) { - Cu.reportError(e); - } - }, - - // Deprecated, only left here to avoid breaking old browser-detection scripts. - addSearchEngine: function(engineURL, iconURL, suggestedTitle, suggestedCategory) { - if (SHERLOCK_FILE_EXT_REGEXP.test(engineURL)) { - Cu.reportError("Installing Sherlock search plugins is no longer supported."); - return; - } - - this.AddSearchProvider(engineURL); - }, - - // This function implements window.external.AddSearchProvider(). - // The capitalization, although nonstandard here, is to match other browsers' - // APIs and is therefore important. - AddSearchProvider: function(engineURL) { - if (!this.mm) { - Cu.reportError(`Installing a search provider from this context is not currently supported: ${Error().stack}.`); - return; - } - - this.mm.sendAsyncMessage("Search:AddEngine", { - pageURL: this.window.document.documentURIObject.spec, - engineURL - }); - }, - - // This function exists to implement window.external.IsSearchProviderInstalled(), - // for compatibility with other browsers. The function has been deprecated - // and so will not be implemented. - IsSearchProviderInstalled: function(engineURL) { - return 0; - }, - - classID: Components.ID("{22117140-9c6e-11d3-aaf1-00805f8a4905}"), - QueryInterface: XPCOMUtils.generateQI([Ci.nsISupports, - Ci.nsIDOMGlobalPropertyInitializer]) -} - -this.NSGetFactory = XPCOMUtils.generateNSGetFactory([nsSidebar]); diff --git a/application/basilisk/components/search/service/toolkitsearch.manifest b/application/basilisk/components/search/service/toolkitsearch.manifest deleted file mode 100644 index b7c55da0e..000000000 --- a/application/basilisk/components/search/service/toolkitsearch.manifest +++ /dev/null @@ -1,10 +0,0 @@ -component {7319788a-fe93-4db3-9f39-818cf08f4256} nsSearchService.js process=main -contract @mozilla.org/browser/search-service;1 {7319788a-fe93-4db3-9f39-818cf08f4256} process=main -# 21600 == 6 hours -category update-timer nsSearchService @mozilla.org/browser/search-service;1,getService,search-engine-update-timer,browser.search.update.interval,21600 -component {aa892eb4-ffbf-477d-9f9a-06c995ae9f27} nsSearchSuggestions.js -contract @mozilla.org/autocomplete/search;1?name=search-autocomplete {aa892eb4-ffbf-477d-9f9a-06c995ae9f27} -#ifdef HAVE_SIDEBAR -component {22117140-9c6e-11d3-aaf1-00805f8a4905} nsSidebar.js -contract @mozilla.org/sidebar;1 {22117140-9c6e-11d3-aaf1-00805f8a4905} -#endif |