2006-03-17 10:16:00 +03:00
|
|
|
# ***** BEGIN LICENSE BLOCK *****
|
|
|
|
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
#
|
|
|
|
# The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
# 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
# the License. You may obtain a copy of the License at
|
|
|
|
# http://www.mozilla.org/MPL/
|
|
|
|
#
|
|
|
|
# Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
# for the specific language governing rights and limitations under the
|
|
|
|
# License.
|
|
|
|
#
|
|
|
|
# The Original Code is the Browser Search Service.
|
|
|
|
#
|
|
|
|
# The Initial Developer of the Original Code is
|
|
|
|
# Google Inc.
|
2006-06-21 01:41:47 +04:00
|
|
|
# Portions created by the Initial Developer are Copyright (C) 2005-2006
|
2006-03-17 10:16:00 +03:00
|
|
|
# the Initial Developer. All Rights Reserved.
|
|
|
|
#
|
|
|
|
# Contributor(s):
|
|
|
|
# Ben Goodger <beng@google.com> (Original author)
|
|
|
|
# Gavin Sharp <gavin@gavinsharp.com>
|
2006-07-05 23:09:38 +04:00
|
|
|
# Joe Hughes <joe@retrovirus.com>
|
2006-06-21 01:41:47 +04:00
|
|
|
# Pamela Greene <pamg.bugs@gmail.com>
|
2006-03-17 10:16:00 +03:00
|
|
|
#
|
|
|
|
# Alternatively, the contents of this file may be used under the terms of
|
|
|
|
# either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
# in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
# of those above. If you wish to allow use of your version of this file only
|
|
|
|
# under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
# use your version of this file under the terms of the MPL, indicate your
|
|
|
|
# decision by deleting the provisions above and replace them with the notice
|
|
|
|
# and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
# the provisions above, a recipient may use your version of this file under
|
|
|
|
# the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
#
|
2006-03-19 01:23:02 +03:00
|
|
|
# ***** END LICENSE BLOCK *****
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Cr = Components.results;
|
|
|
|
|
|
|
|
const PERMS_FILE = 0644;
|
|
|
|
const PERMS_DIRECTORY = 0755;
|
|
|
|
|
|
|
|
const MODE_RDONLY = 0x01;
|
|
|
|
const MODE_WRONLY = 0x02;
|
|
|
|
const MODE_CREATE = 0x08;
|
|
|
|
const MODE_APPEND = 0x10;
|
|
|
|
const MODE_TRUNCATE = 0x20;
|
|
|
|
|
2006-04-28 06:18:58 +04:00
|
|
|
// Directory service keys
|
|
|
|
const NS_APP_SEARCH_DIR_LIST = "SrchPluginsDL";
|
|
|
|
const NS_APP_USER_SEARCH_DIR = "UsrSrchPlugns";
|
2006-06-21 04:18:29 +04:00
|
|
|
const NS_APP_SEARCH_DIR = "SrchPlugns";
|
2006-06-21 04:19:55 +04:00
|
|
|
const NS_APP_USER_PROFILE_50_DIR = "ProfD";
|
2006-03-17 10:16:00 +03:00
|
|
|
|
2007-05-04 19:48:08 +04:00
|
|
|
// Search engine "locations". If this list is changed, be sure to update
|
|
|
|
// the engine's _isDefault function accordingly.
|
|
|
|
const SEARCH_APP_DIR = 1;
|
|
|
|
const SEARCH_PROFILE_DIR = 2;
|
|
|
|
const SEARCH_IN_EXTENSION = 3;
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
// See documentation in nsIBrowserSearchService.idl.
|
|
|
|
const SEARCH_ENGINE_TOPIC = "browser-search-engine-modified";
|
2006-08-04 17:59:20 +04:00
|
|
|
const QUIT_APPLICATION_TOPIC = "quit-application";
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
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";
|
|
|
|
|
2006-04-14 03:28:21 +04:00
|
|
|
const SEARCH_TYPE_MOZSEARCH = Ci.nsISearchEngine.TYPE_MOZSEARCH;
|
|
|
|
const SEARCH_TYPE_OPENSEARCH = Ci.nsISearchEngine.TYPE_OPENSEARCH;
|
|
|
|
const SEARCH_TYPE_SHERLOCK = Ci.nsISearchEngine.TYPE_SHERLOCK;
|
|
|
|
|
|
|
|
const SEARCH_DATA_XML = Ci.nsISearchEngine.DATA_XML;
|
|
|
|
const SEARCH_DATA_TEXT = Ci.nsISearchEngine.DATA_TEXT;
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
// File extensions for search plugin description files
|
|
|
|
const XML_FILE_EXT = "xml";
|
|
|
|
const SHERLOCK_FILE_EXT = "src";
|
|
|
|
|
2006-12-11 23:54:35 +03:00
|
|
|
// Delay for lazy serialization (ms)
|
|
|
|
const LAZY_SERIALIZE_DELAY = 100;
|
|
|
|
|
2008-10-07 00:57:18 +04:00
|
|
|
// 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.
|
2008-10-18 06:24:47 +04:00
|
|
|
const CACHE_VERSION = 2;
|
2008-10-07 00:57:18 +04:00
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
const ICON_DATAURL_PREFIX = "data:image/x-icon;base64,";
|
|
|
|
|
|
|
|
// Supported extensions for Sherlock plugin icons
|
2006-04-28 06:18:58 +04:00
|
|
|
const SHERLOCK_ICON_EXTENSIONS = [".gif", ".png", ".jpg", ".jpeg"];
|
2006-03-17 10:16:00 +03:00
|
|
|
|
2006-05-17 05:37:49 +04:00
|
|
|
const NEW_LINES = /(\r\n|\r|\n)/;
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
// 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 = 10000;
|
|
|
|
|
2006-06-28 20:09:26 +04:00
|
|
|
// Default charset to use for sending search parameters. ISO-8859-1 is used to
|
|
|
|
// match previous nsInternetSearchService behavior.
|
2006-03-17 10:16:00 +03:00
|
|
|
const DEFAULT_QUERY_CHARSET = "ISO-8859-1";
|
|
|
|
|
2008-09-14 01:01:14 +04:00
|
|
|
const SEARCH_BUNDLE = "chrome://global/locale/search/search.properties";
|
2006-03-17 10:16:00 +03:00
|
|
|
const BRAND_BUNDLE = "chrome://branding/locale/brand.properties";
|
|
|
|
|
2006-05-12 00:31:55 +04:00
|
|
|
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
|
2006-06-21 01:53:09 +04:00
|
|
|
// are using the following versions. We therefore allow either.
|
2006-06-28 20:09:26 +04:00
|
|
|
const OPENSEARCH_NAMESPACES = [
|
|
|
|
OPENSEARCH_NS_11, OPENSEARCH_NS_10,
|
|
|
|
"http://a9.com/-/spec/opensearchdescription/1.1/",
|
|
|
|
"http://a9.com/-/spec/opensearchdescription/1.0/"
|
|
|
|
];
|
|
|
|
|
2006-05-12 00:31:55 +04:00
|
|
|
const OPENSEARCH_LOCALNAME = "OpenSearchDescription";
|
2006-03-17 10:16:00 +03:00
|
|
|
|
2006-05-12 00:31:55 +04:00
|
|
|
const MOZSEARCH_NS_10 = "http://www.mozilla.org/2006/browser/search/";
|
|
|
|
const MOZSEARCH_LOCALNAME = "SearchPlugin";
|
2006-03-17 10:16:00 +03:00
|
|
|
|
2006-06-16 23:31:40 +04:00
|
|
|
const URLTYPE_SUGGEST_JSON = "application/x-suggestions+json";
|
|
|
|
const URLTYPE_SEARCH_HTML = "text/html";
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
// Empty base document used to serialize engines to file.
|
|
|
|
const EMPTY_DOC = "<?xml version=\"1.0\"?>\n" +
|
2006-05-12 00:31:55 +04:00
|
|
|
"<" + MOZSEARCH_LOCALNAME +
|
|
|
|
" xmlns=\"" + MOZSEARCH_NS_10 + "\"" +
|
|
|
|
" xmlns:os=\"" + OPENSEARCH_NS_11 + "\"" +
|
2006-03-17 10:16:00 +03:00
|
|
|
"/>";
|
|
|
|
|
|
|
|
const BROWSER_SEARCH_PREF = "browser.search.";
|
|
|
|
|
2006-06-28 20:09:26 +04:00
|
|
|
const USER_DEFINED = "{searchTerms}";
|
|
|
|
|
2006-07-05 22:06:47 +04:00
|
|
|
// Custom search parameters
|
|
|
|
#ifdef OFFICIAL_BUILD
|
|
|
|
const MOZ_OFFICIAL = "official";
|
|
|
|
#else
|
|
|
|
const MOZ_OFFICIAL = "unofficial";
|
|
|
|
#endif
|
|
|
|
#expand const MOZ_DISTRIBUTION_ID = __MOZ_DISTRIBUTION_ID__;
|
|
|
|
|
|
|
|
const MOZ_PARAM_LOCALE = /\{moz:locale\}/g;
|
|
|
|
const MOZ_PARAM_DIST_ID = /\{moz:distributionID\}/g;
|
|
|
|
const MOZ_PARAM_OFFICIAL = /\{moz:official\}/g;
|
|
|
|
|
2006-06-28 20:09:26 +04:00
|
|
|
// 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";
|
2006-11-11 08:26:26 +03:00
|
|
|
const OS_PARAM_INPUT_ENCODING_DEF = "UTF-8";
|
2006-06-28 20:09:26 +04:00
|
|
|
|
|
|
|
// "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
|
2006-12-08 00:31:15 +03:00
|
|
|
const OS_PARAM_OPTIONAL = /\{(?:\w+:)?\w+\?\}/g;
|
2006-06-28 20:09:26 +04:00
|
|
|
|
|
|
|
// 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],
|
|
|
|
];
|
2006-03-17 10:16:00 +03:00
|
|
|
|
2006-07-21 03:00:39 +04:00
|
|
|
// 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;
|
|
|
|
|
2006-04-20 01:16:45 +04:00
|
|
|
// Returns false for whitespace-only or commented out lines in a
|
|
|
|
// Sherlock file, true otherwise.
|
|
|
|
function isUsefulLine(aLine) {
|
|
|
|
return !(/^\s*($|#)/i.test(aLine));
|
|
|
|
}
|
2006-03-17 10:16:00 +03:00
|
|
|
|
2008-10-07 00:57:18 +04:00
|
|
|
__defineGetter__("gObsSvc", function() {
|
|
|
|
delete this.gObsSvc;
|
|
|
|
return this.gObsSvc = Cc["@mozilla.org/observer-service;1"].
|
|
|
|
getService(Ci.nsIObserverService);
|
|
|
|
});
|
|
|
|
|
|
|
|
__defineGetter__("gIoSvc", function() {
|
|
|
|
delete this.gIoSvc;
|
|
|
|
return this.gIoSvc = Cc["@mozilla.org/network/io-service;1"].
|
|
|
|
getService(Ci.nsIIOService);
|
|
|
|
});
|
|
|
|
|
|
|
|
__defineGetter__("gPrefSvc", function() {
|
|
|
|
delete this.gPrefSvc;
|
|
|
|
return this.gPrefSvc = Cc["@mozilla.org/preferences-service;1"].
|
|
|
|
getService(Ci.nsIPrefBranch);
|
|
|
|
});
|
|
|
|
|
2006-04-25 20:26:58 +04:00
|
|
|
/**
|
|
|
|
* Prefixed to all search debug output.
|
|
|
|
*/
|
|
|
|
const SEARCH_LOG_PREFIX = "*** Search: ";
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
/**
|
2008-01-29 22:39:20 +03:00
|
|
|
* Outputs aText to the JavaScript console as well as to stdout.
|
2008-01-29 22:39:20 +03:00
|
|
|
*/
|
2008-01-29 22:39:20 +03:00
|
|
|
function DO_LOG(aText) {
|
|
|
|
dump(SEARCH_LOG_PREFIX + aText + "\n");
|
|
|
|
var consoleService = Cc["@mozilla.org/consoleservice;1"].
|
|
|
|
getService(Ci.nsIConsoleService);
|
|
|
|
consoleService.logStringMessage(aText);
|
|
|
|
}
|
|
|
|
|
2007-09-06 02:18:50 +04:00
|
|
|
#ifdef DEBUG
|
2008-01-29 22:39:20 +03:00
|
|
|
/**
|
|
|
|
* In debug builds, use a live, pref-based (browser.search.log) LOG function
|
|
|
|
* to allow enabling/disabling without a restart.
|
|
|
|
*/
|
|
|
|
function PREF_LOG(aText) {
|
2008-10-07 00:57:18 +04:00
|
|
|
if (getBoolPref(BROWSER_SEARCH_PREF + "log", false))
|
2008-01-29 22:39:20 +03:00
|
|
|
DO_LOG(aText);
|
2006-03-17 10:16:00 +03:00
|
|
|
}
|
2008-01-29 22:39:20 +03:00
|
|
|
var LOG = PREF_LOG;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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(){};
|
|
|
|
|
|
|
|
#endif
|
2006-03-17 10:16:00 +03:00
|
|
|
|
2008-10-18 06:24:47 +04:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2006-03-17 10:16:00 +03:00
|
|
|
function ERROR(message, resultCode) {
|
2006-04-25 20:26:58 +04:00
|
|
|
NS_ASSERT(false, SEARCH_LOG_PREFIX + message);
|
2008-10-18 06:24:47 +04:00
|
|
|
throw Components.Exception(message, resultCode);
|
2006-03-17 10:16:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-10-18 06:24:47 +04:00
|
|
|
* Logs the failure message (if browser.search.log is enabled) and throws.
|
2006-03-17 10:16:00 +03:00
|
|
|
* @param message
|
2008-10-18 06:24:47 +04:00
|
|
|
* A message to display
|
2006-03-17 10:16:00 +03:00
|
|
|
* @param resultCode
|
2008-10-18 06:24:47 +04:00
|
|
|
* The NS_ERROR_* value to throw.
|
|
|
|
* @throws resultCode or NS_ERROR_INVALID_ARG if resultCode isn't specified.
|
2006-03-17 10:16:00 +03:00
|
|
|
*/
|
2008-10-18 06:24:47 +04:00
|
|
|
function FAIL(message, resultCode) {
|
|
|
|
LOG(message);
|
|
|
|
throw Components.Exception(message, resultCode || Cr.NS_ERROR_INVALID_ARG);
|
2006-03-17 10:16:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2008-10-18 06:24:47 +04:00
|
|
|
* Ensures an assertion is met before continuing. Should be used to indicate
|
|
|
|
* fatal errors.
|
2006-03-17 10:16:00 +03:00
|
|
|
* @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
|
|
|
|
*/
|
2008-10-18 06:24:47 +04:00
|
|
|
function ENSURE_WARN(assertion, message, resultCode) {
|
|
|
|
NS_ASSERT(assertion, SEARCH_LOG_PREFIX + message);
|
|
|
|
if (!assertion)
|
|
|
|
throw Components.Exception(message, resultCode);
|
2006-03-17 10:16:00 +03:00
|
|
|
}
|
|
|
|
|
2006-05-17 05:37:49 +04:00
|
|
|
function loadListener(aChannel, aEngine, aCallback) {
|
2006-03-17 10:16:00 +03:00
|
|
|
this._channel = aChannel;
|
2006-07-21 03:00:39 +04:00
|
|
|
this._bytes = [];
|
2006-04-28 06:18:58 +04:00
|
|
|
this._engine = aEngine;
|
2006-05-17 05:37:49 +04:00
|
|
|
this._callback = aCallback;
|
2006-03-17 10:16:00 +03:00
|
|
|
}
|
2006-05-17 05:37:49 +04:00
|
|
|
loadListener.prototype = {
|
|
|
|
_callback: null,
|
2006-03-17 10:16:00 +03:00
|
|
|
_channel: null,
|
|
|
|
_countRead: 0,
|
|
|
|
_engine: null,
|
|
|
|
_stream: null,
|
|
|
|
|
2007-11-24 05:45:42 +03:00
|
|
|
QueryInterface: function SRCH_loadQI(aIID) {
|
|
|
|
if (aIID.equals(Ci.nsISupports) ||
|
|
|
|
aIID.equals(Ci.nsIRequestObserver) ||
|
|
|
|
aIID.equals(Ci.nsIStreamListener) ||
|
|
|
|
aIID.equals(Ci.nsIChannelEventSink) ||
|
|
|
|
aIID.equals(Ci.nsIInterfaceRequestor) ||
|
2008-01-15 18:06:34 +03:00
|
|
|
aIID.equals(Ci.nsIBadCertListener2) ||
|
|
|
|
aIID.equals(Ci.nsISSLErrorListener) ||
|
2007-11-24 05:45:42 +03:00
|
|
|
// See FIXME comment below
|
|
|
|
aIID.equals(Ci.nsIHttpEventSink) ||
|
|
|
|
aIID.equals(Ci.nsIProgressEventSink) ||
|
|
|
|
false)
|
|
|
|
return this;
|
|
|
|
|
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
|
|
},
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
// nsIRequestObserver
|
2006-05-17 05:37:49 +04:00
|
|
|
onStartRequest: function SRCH_loadStartR(aRequest, aContext) {
|
|
|
|
LOG("loadListener: Starting request: " + aRequest.name);
|
2006-03-17 10:16:00 +03:00
|
|
|
this._stream = Cc["@mozilla.org/binaryinputstream;1"].
|
|
|
|
createInstance(Ci.nsIBinaryInputStream);
|
|
|
|
},
|
|
|
|
|
2006-05-17 05:37:49 +04:00
|
|
|
onStopRequest: function SRCH_loadStopR(aRequest, aContext, aStatusCode) {
|
|
|
|
LOG("loadListener: Stopping request: " + aRequest.name);
|
2006-05-29 23:00:56 +04:00
|
|
|
|
2006-07-21 03:00:39 +04:00
|
|
|
var requestFailed = !Components.isSuccessCode(aStatusCode);
|
|
|
|
if (!requestFailed && (aRequest instanceof Ci.nsIHttpChannel))
|
|
|
|
requestFailed = !aRequest.requestSucceeded;
|
|
|
|
|
|
|
|
if (requestFailed || this._countRead == 0) {
|
2006-05-17 05:37:49 +04:00
|
|
|
LOG("loadListener: request failed!");
|
|
|
|
// send null so the callback can deal with the failure
|
|
|
|
this._callback(null, this._engine);
|
2006-05-29 23:00:56 +04:00
|
|
|
} else
|
|
|
|
this._callback(this._bytes, this._engine);
|
2006-03-17 10:16:00 +03:00
|
|
|
this._channel = null;
|
|
|
|
this._engine = null;
|
|
|
|
},
|
|
|
|
|
|
|
|
// nsIStreamListener
|
2006-05-17 05:37:49 +04:00
|
|
|
onDataAvailable: function SRCH_loadDAvailable(aRequest, aContext,
|
|
|
|
aInputStream, aOffset,
|
|
|
|
aCount) {
|
2006-03-17 10:16:00 +03:00
|
|
|
this._stream.setInputStream(aInputStream);
|
|
|
|
|
|
|
|
// Get a byte array of the data
|
|
|
|
this._bytes = this._bytes.concat(this._stream.readByteArray(aCount));
|
|
|
|
this._countRead += aCount;
|
|
|
|
},
|
|
|
|
|
|
|
|
// nsIChannelEventSink
|
2006-05-17 05:37:49 +04:00
|
|
|
onChannelRedirect: function SRCH_loadCRedirect(aOldChannel, aNewChannel,
|
|
|
|
aFlags) {
|
2006-03-17 10:16:00 +03:00
|
|
|
this._channel = aNewChannel;
|
|
|
|
},
|
|
|
|
|
|
|
|
// nsIInterfaceRequestor
|
2006-05-17 05:37:49 +04:00
|
|
|
getInterface: function SRCH_load_GI(aIID) {
|
2006-03-17 10:16:00 +03:00
|
|
|
return this.QueryInterface(aIID);
|
|
|
|
},
|
|
|
|
|
2008-01-15 18:06:34 +03:00
|
|
|
// nsIBadCertListener2
|
|
|
|
notifyCertProblem: function SRCH_certProblem(socketInfo, status, targetSite) {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
|
|
|
// nsISSLErrorListener
|
|
|
|
notifySSLError: function SRCH_SSLError(socketInfo, error, targetSite) {
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
|
2006-07-21 03:00:39 +04:00
|
|
|
// FIXME: bug 253127
|
2006-03-17 10:16:00 +03:00
|
|
|
// nsIHttpEventSink
|
|
|
|
onRedirect: function (aChannel, aNewChannel) {},
|
|
|
|
// nsIProgressEventSink
|
|
|
|
onProgress: function (aRequest, aContext, aProgress, aProgressMax) {},
|
|
|
|
onStatus: function (aRequest, aContext, aStatus, aStatusArg) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to verify a given DOM node's localName and namespaceURI.
|
|
|
|
* @param aElement
|
|
|
|
* The element to verify.
|
|
|
|
* @param aLocalNameArray
|
|
|
|
* An array of strings to compare against aElement's localName.
|
|
|
|
* @param aNameSpaceArray
|
|
|
|
* An array of strings to compare against aElement's namespaceURI.
|
|
|
|
*
|
2006-04-28 06:18:58 +04:00
|
|
|
* @returns false if aElement is null, or if its localName or namespaceURI
|
2006-03-17 10:16:00 +03:00
|
|
|
* does not match one of the elements in the aLocalNameArray or
|
|
|
|
* aNameSpaceArray arrays, respectively.
|
|
|
|
* @throws NS_ERROR_INVALID_ARG if aLocalNameArray or aNameSpaceArray are null.
|
|
|
|
*/
|
|
|
|
function checkNameSpace(aElement, aLocalNameArray, aNameSpaceArray) {
|
2008-10-18 06:24:47 +04:00
|
|
|
if (!aLocalNameArray || !aNameSpaceArray)
|
|
|
|
FAIL("missing aLocalNameArray or aNameSpaceArray for checkNameSpace");
|
2006-03-17 10:16:00 +03:00
|
|
|
return (aElement &&
|
|
|
|
(aLocalNameArray.indexOf(aElement.localName) != -1) &&
|
|
|
|
(aNameSpaceArray.indexOf(aElement.namespaceURI) != -1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2006-05-15 18:31:21 +04:00
|
|
|
function makeURI(aURLSpec, aCharset) {
|
2006-03-17 10:16:00 +03:00
|
|
|
try {
|
2008-10-07 00:57:18 +04:00
|
|
|
return gIoSvc.newURI(aURLSpec, aCharset, null);
|
2006-03-17 10:16:00 +03:00
|
|
|
} catch (ex) { }
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets a directory from the directory service.
|
|
|
|
* @param aKey
|
|
|
|
* The directory service key indicating the directory to get.
|
|
|
|
*/
|
2008-10-07 00:57:18 +04:00
|
|
|
let _dirSvc = null;
|
|
|
|
function getDir(aKey, aIFace) {
|
2008-10-18 06:24:47 +04:00
|
|
|
if (!aKey)
|
|
|
|
FAIL("getDir requires a directory key!");
|
2006-03-17 10:16:00 +03:00
|
|
|
|
2008-10-07 00:57:18 +04:00
|
|
|
if (!_dirSvc)
|
|
|
|
_dirSvc = Cc["@mozilla.org/file/directory_service;1"].
|
|
|
|
getService(Ci.nsIProperties);
|
|
|
|
return _dirSvc.get(aKey, aIFace || Ci.nsIFile);
|
2006-03-17 10:16:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-05-17 05:37:49 +04:00
|
|
|
* The following two functions are essentially copied from
|
|
|
|
* nsInternetSearchService. They are required for backwards compatibility.
|
2006-03-17 10:16:00 +03:00
|
|
|
*/
|
2006-05-17 05:37:49 +04:00
|
|
|
function queryCharsetFromCode(aCode) {
|
|
|
|
const codes = [];
|
|
|
|
codes[0] = "x-mac-roman";
|
|
|
|
codes[6] = "x-mac-greek";
|
|
|
|
codes[35] = "x-mac-turkish";
|
|
|
|
codes[513] = "ISO-8859-1";
|
|
|
|
codes[514] = "ISO-8859-2";
|
|
|
|
codes[517] = "ISO-8859-5";
|
|
|
|
codes[518] = "ISO-8859-6";
|
|
|
|
codes[519] = "ISO-8859-7";
|
|
|
|
codes[520] = "ISO-8859-8";
|
|
|
|
codes[521] = "ISO-8859-9";
|
|
|
|
codes[1049] = "IBM864";
|
|
|
|
codes[1280] = "windows-1252";
|
|
|
|
codes[1281] = "windows-1250";
|
|
|
|
codes[1282] = "windows-1251";
|
|
|
|
codes[1283] = "windows-1253";
|
|
|
|
codes[1284] = "windows-1254";
|
|
|
|
codes[1285] = "windows-1255";
|
|
|
|
codes[1286] = "windows-1256";
|
|
|
|
codes[1536] = "us-ascii";
|
|
|
|
codes[1584] = "GB2312";
|
|
|
|
codes[1585] = "x-gbk";
|
|
|
|
codes[1600] = "EUC-KR";
|
|
|
|
codes[2080] = "ISO-2022-JP";
|
|
|
|
codes[2096] = "ISO-2022-CN";
|
|
|
|
codes[2112] = "ISO-2022-KR";
|
|
|
|
codes[2336] = "EUC-JP";
|
|
|
|
codes[2352] = "GB2312";
|
|
|
|
codes[2353] = "x-euc-tw";
|
|
|
|
codes[2368] = "EUC-KR";
|
|
|
|
codes[2561] = "Shift_JIS";
|
|
|
|
codes[2562] = "KOI8-R";
|
|
|
|
codes[2563] = "Big5";
|
|
|
|
codes[2565] = "HZ-GB-2312";
|
2006-07-21 03:00:39 +04:00
|
|
|
|
2006-05-17 05:37:49 +04:00
|
|
|
if (codes[aCode])
|
|
|
|
return codes[aCode];
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
return getLocalizedPref("intl.charset.default", DEFAULT_QUERY_CHARSET);
|
|
|
|
}
|
2006-05-17 05:37:49 +04:00
|
|
|
function fileCharsetFromCode(aCode) {
|
|
|
|
const codes = [
|
|
|
|
"x-mac-roman", // 0
|
|
|
|
"Shift_JIS", // 1
|
|
|
|
"Big5", // 2
|
|
|
|
"EUC-KR", // 3
|
|
|
|
"X-MAC-ARABIC", // 4
|
|
|
|
"X-MAC-HEBREW", // 5
|
|
|
|
"X-MAC-GREEK", // 6
|
|
|
|
"X-MAC-CYRILLIC", // 7
|
|
|
|
"X-MAC-DEVANAGARI" , // 9
|
|
|
|
"X-MAC-GURMUKHI", // 10
|
|
|
|
"X-MAC-GUJARATI", // 11
|
|
|
|
"X-MAC-ORIYA", // 12
|
|
|
|
"X-MAC-BENGALI", // 13
|
|
|
|
"X-MAC-TAMIL", // 14
|
|
|
|
"X-MAC-TELUGU", // 15
|
|
|
|
"X-MAC-KANNADA", // 16
|
|
|
|
"X-MAC-MALAYALAM", // 17
|
|
|
|
"X-MAC-SINHALESE", // 18
|
|
|
|
"X-MAC-BURMESE", // 19
|
|
|
|
"X-MAC-KHMER", // 20
|
|
|
|
"X-MAC-THAI", // 21
|
|
|
|
"X-MAC-LAOTIAN", // 22
|
|
|
|
"X-MAC-GEORGIAN", // 23
|
|
|
|
"X-MAC-ARMENIAN", // 24
|
|
|
|
"GB2312", // 25
|
|
|
|
"X-MAC-TIBETAN", // 26
|
|
|
|
"X-MAC-MONGOLIAN", // 27
|
|
|
|
"X-MAC-ETHIOPIC", // 28
|
|
|
|
"X-MAC-CENTRALEURROMAN", // 29
|
|
|
|
"X-MAC-VIETNAMESE", // 30
|
|
|
|
"X-MAC-EXTARABIC" // 31
|
|
|
|
];
|
|
|
|
// Sherlock files have always defaulted to x-mac-roman, so do that here too
|
|
|
|
return codes[aCode] || codes[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a string interpretation of aBytes using aCharset, or null on
|
|
|
|
* failure.
|
|
|
|
*/
|
|
|
|
function bytesToString(aBytes, aCharset) {
|
|
|
|
var converter = Cc["@mozilla.org/intl/scriptableunicodeconverter"].
|
|
|
|
createInstance(Ci.nsIScriptableUnicodeConverter);
|
|
|
|
LOG("bytesToString: converting using charset: " + aCharset);
|
|
|
|
|
|
|
|
try {
|
|
|
|
converter.charset = aCharset;
|
|
|
|
return converter.convertFromByteArray(aBytes, aBytes.length);
|
|
|
|
} catch (ex) {}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts an array of bytes representing a Sherlock file into an array of
|
|
|
|
* lines representing the useful data from the file.
|
2006-05-29 22:48:42 +04:00
|
|
|
*
|
|
|
|
* @param aBytes
|
|
|
|
* The array of bytes representing the Sherlock file.
|
|
|
|
* @param aCharsetCode
|
|
|
|
* An integer value representing a character set code to be passed to
|
|
|
|
* fileCharsetFromCode, or null for the default Sherlock encoding.
|
2006-05-17 05:37:49 +04:00
|
|
|
*/
|
2006-05-29 22:48:42 +04:00
|
|
|
function sherlockBytesToLines(aBytes, aCharsetCode) {
|
|
|
|
// fileCharsetFromCode returns the default encoding if aCharsetCode is null
|
|
|
|
var charset = fileCharsetFromCode(aCharsetCode);
|
2006-05-17 05:37:49 +04:00
|
|
|
|
|
|
|
var dataString = bytesToString(aBytes, charset);
|
2008-10-18 06:24:47 +04:00
|
|
|
if (!dataString)
|
|
|
|
FAIL("sherlockBytesToLines: Couldn't convert byte array!", Cr.NS_ERROR_FAILURE);
|
2006-05-17 05:37:49 +04:00
|
|
|
|
|
|
|
// Split the string into lines, and filter out comments and
|
|
|
|
// whitespace-only lines
|
|
|
|
return dataString.split(NEW_LINES).filter(isUsefulLine);
|
|
|
|
}
|
2006-03-17 10:16:00 +03:00
|
|
|
|
2006-06-28 20:09:26 +04:00
|
|
|
/**
|
|
|
|
* 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() {
|
|
|
|
const localePref = "general.useragent.locale";
|
|
|
|
var locale = getLocalizedPref(localePref);
|
|
|
|
if (locale)
|
|
|
|
return locale;
|
|
|
|
|
|
|
|
// Not localized
|
2008-10-07 00:57:18 +04:00
|
|
|
return gPrefSvc.getCharPref(localePref);
|
2006-06-28 20:09:26 +04:00
|
|
|
}
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
/**
|
|
|
|
* 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 {
|
2008-10-07 00:57:18 +04:00
|
|
|
return gPrefSvc.getComplexValue(aPrefName, nsIPLS).data;
|
2006-03-17 10:16:00 +03:00
|
|
|
} catch (ex) {}
|
|
|
|
|
|
|
|
return aDefault;
|
|
|
|
}
|
|
|
|
|
2006-10-31 18:32:43 +03:00
|
|
|
/**
|
|
|
|
* Wrapper for nsIPrefBranch::setComplexValue.
|
|
|
|
* @param aPrefName
|
|
|
|
* The name of the pref to set.
|
|
|
|
*/
|
|
|
|
function setLocalizedPref(aPrefName, aValue) {
|
|
|
|
const nsIPLS = Ci.nsIPrefLocalizedString;
|
|
|
|
try {
|
|
|
|
var pls = Components.classes["@mozilla.org/pref-localizedstring;1"]
|
|
|
|
.createInstance(Ci.nsIPrefLocalizedString);
|
|
|
|
pls.data = aValue;
|
2008-10-07 00:57:18 +04:00
|
|
|
gPrefSvc.setComplexValue(aPrefName, nsIPLS, pls);
|
2006-10-31 18:32:43 +03:00
|
|
|
} catch (ex) {}
|
|
|
|
}
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
/**
|
|
|
|
* Wrapper for nsIPrefBranch::getBoolPref.
|
|
|
|
* @param aPrefName
|
|
|
|
* The name of the pref to get.
|
|
|
|
* @returns aDefault if the requested pref doesn't exist.
|
|
|
|
*/
|
|
|
|
function getBoolPref(aName, aDefault) {
|
|
|
|
try {
|
2008-10-07 00:57:18 +04:00
|
|
|
return gPrefSvc.getBoolPref(aName);
|
2006-03-17 10:16:00 +03:00
|
|
|
} catch (ex) {
|
|
|
|
return aDefault;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a unique nsIFile object with a sanitized name, based on the engine name.
|
|
|
|
* @param aName
|
|
|
|
* A name to "sanitize". Can be an empty string, in which case a random
|
|
|
|
* 8 character filename will be produced.
|
|
|
|
* @returns A nsIFile object in the user's search engines directory with a
|
|
|
|
* unique sanitized name.
|
|
|
|
*/
|
|
|
|
function getSanitizedFile(aName) {
|
|
|
|
var fileName = sanitizeName(aName) + "." + XML_FILE_EXT;
|
|
|
|
var file = getDir(NS_APP_USER_SEARCH_DIR);
|
|
|
|
file.append(fileName);
|
|
|
|
file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, PERMS_FILE);
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all characters not in the "chars" string from aName.
|
|
|
|
*
|
2006-05-06 02:45:37 +04:00
|
|
|
* @returns 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).
|
2006-03-17 10:16:00 +03:00
|
|
|
*/
|
|
|
|
function sanitizeName(aName) {
|
2006-05-06 02:45:37 +04:00
|
|
|
const chars = "-abcdefghijklmnopqrstuvwxyz0123456789";
|
2006-07-21 16:52:57 +04:00
|
|
|
const maxLength = 60;
|
2006-05-06 02:45:37 +04:00
|
|
|
|
|
|
|
var name = aName.toLowerCase();
|
|
|
|
name = name.replace(/ /g, "-");
|
|
|
|
name = name.split("").filter(function (el) {
|
|
|
|
return chars.indexOf(el) != -1;
|
|
|
|
}).join("");
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
if (!name) {
|
|
|
|
// Our input had no valid characters - use a random name
|
2006-05-17 05:37:49 +04:00
|
|
|
var cl = chars.length - 1;
|
2006-03-17 10:16:00 +03:00
|
|
|
for (var i = 0; i < 8; ++i)
|
2006-05-17 05:37:49 +04:00
|
|
|
name += chars.charAt(Math.round(Math.random() * cl));
|
2006-03-17 10:16:00 +03:00
|
|
|
}
|
2006-05-06 02:45:37 +04:00
|
|
|
|
2006-07-21 16:52:57 +04:00
|
|
|
if (name.length > maxLength)
|
|
|
|
name = name.substring(0, maxLength);
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
2008-10-07 00:57:18 +04:00
|
|
|
/**
|
|
|
|
* Retrieve a pref from the search param branch.
|
|
|
|
*
|
|
|
|
* @param prefName
|
|
|
|
* The name of the pref.
|
|
|
|
**/
|
|
|
|
function getMozParamPref(prefName)
|
|
|
|
gPrefSvc.getCharPref(BROWSER_SEARCH_PREF + "param." + prefName);
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
function notifyAction(aEngine, aVerb) {
|
|
|
|
LOG("NOTIFY: Engine: \"" + aEngine.name + "\"; Verb: \"" + aVerb + "\"");
|
2008-10-07 00:57:18 +04:00
|
|
|
gObsSvc.notifyObservers(aEngine, SEARCH_ENGINE_TOPIC, aVerb);
|
2006-03-17 10:16:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Simple object representing a name/value pair.
|
|
|
|
*/
|
|
|
|
function QueryParameter(aName, aValue) {
|
2008-10-18 06:24:47 +04:00
|
|
|
if (!aName || (aValue == null))
|
|
|
|
FAIL("missing name or value for QueryParameter!");
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
this.name = aName;
|
|
|
|
this.value = aValue;
|
|
|
|
}
|
|
|
|
|
2006-06-28 20:09:26 +04:00
|
|
|
/**
|
|
|
|
* Perform OpenSearch parameter substitution on aParamValue.
|
2006-07-21 03:00:39 +04:00
|
|
|
*
|
2006-06-28 20:09:26 +04:00
|
|
|
* @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 aQueryEncoding
|
|
|
|
* The value to use for the OS_PARAM_INPUT_ENCODING parameter. See
|
|
|
|
* definition in the OpenSearch spec.
|
|
|
|
*
|
|
|
|
* @see http://opensearch.a9.com/spec/1.1/querysyntax/#core
|
|
|
|
*/
|
2006-07-05 22:06:47 +04:00
|
|
|
function ParamSubstitution(aParamValue, aSearchTerms, aEngine) {
|
2006-06-28 20:09:26 +04:00
|
|
|
var value = aParamValue;
|
|
|
|
|
2006-09-14 04:49:19 +04:00
|
|
|
var distributionID = MOZ_DISTRIBUTION_ID;
|
|
|
|
try {
|
2008-10-07 00:57:18 +04:00
|
|
|
distributionID = gPrefSvc.getCharPref(BROWSER_SEARCH_PREF + "distributionID");
|
2006-09-14 04:49:19 +04:00
|
|
|
}
|
|
|
|
catch (ex) { }
|
|
|
|
|
2007-05-04 19:48:08 +04:00
|
|
|
// Custom search parameters. These are only available to default search
|
2006-07-05 22:06:47 +04:00
|
|
|
// engines.
|
2007-05-04 19:48:08 +04:00
|
|
|
if (aEngine._isDefault) {
|
2006-07-05 22:06:47 +04:00
|
|
|
value = value.replace(MOZ_PARAM_LOCALE, getLocale());
|
2006-09-14 04:49:19 +04:00
|
|
|
value = value.replace(MOZ_PARAM_DIST_ID, distributionID);
|
2006-07-05 22:06:47 +04:00
|
|
|
value = value.replace(MOZ_PARAM_OFFICIAL, MOZ_OFFICIAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert the OpenSearch parameters we're confident about
|
2006-06-28 20:09:26 +04:00
|
|
|
value = value.replace(OS_PARAM_USER_DEFINED, aSearchTerms);
|
2006-07-05 22:06:47 +04:00
|
|
|
value = value.replace(OS_PARAM_INPUT_ENCODING, aEngine.queryCharset);
|
2006-06-28 20:09:26 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-06-17 04:58:09 +04:00
|
|
|
/**
|
|
|
|
* Creates a mozStorage statement that can be used to access the database we
|
|
|
|
* use to hold metadata.
|
|
|
|
*
|
|
|
|
* @param dbconn the database that the statement applies to
|
|
|
|
* @param sql a string specifying the sql statement that should be created
|
|
|
|
*/
|
|
|
|
function createStatement (dbconn, sql) {
|
|
|
|
var stmt = dbconn.createStatement(sql);
|
2006-06-28 20:09:26 +04:00
|
|
|
var wrapper = Cc["@mozilla.org/storage/statement-wrapper;1"].
|
|
|
|
createInstance(Ci.mozIStorageStatementWrapper);
|
2006-06-17 04:58:09 +04:00
|
|
|
|
|
|
|
wrapper.initialize(stmt);
|
|
|
|
return wrapper;
|
|
|
|
}
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* @see http://opensearch.a9.com/spec/1.1/querysyntax/#urltag
|
|
|
|
*
|
2006-06-28 20:09:26 +04:00
|
|
|
* @throws NS_ERROR_NOT_IMPLEMENTED if aType is unsupported.
|
2006-03-17 10:16:00 +03:00
|
|
|
*/
|
|
|
|
function EngineURL(aType, aMethod, aTemplate) {
|
2008-10-18 06:24:47 +04:00
|
|
|
if (!aType || !aMethod || !aTemplate)
|
|
|
|
FAIL("missing type, method or template for EngineURL!");
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
var method = aMethod.toUpperCase();
|
|
|
|
var type = aType.toLowerCase();
|
|
|
|
|
2008-10-18 06:24:47 +04:00
|
|
|
if (method != "GET" && method != "POST")
|
|
|
|
FAIL("method passed to EngineURL must be \"GET\" or \"POST\"");
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
this.type = type;
|
|
|
|
this.method = method;
|
|
|
|
this.params = [];
|
2008-10-07 00:57:18 +04:00
|
|
|
// Don't serialize expanded mozparams
|
|
|
|
this.mozparams = {};
|
2006-06-20 09:07:00 +04:00
|
|
|
|
|
|
|
var templateURI = makeURI(aTemplate);
|
2008-10-18 06:24:47 +04:00
|
|
|
if (!templateURI)
|
|
|
|
FAIL("new EngineURL: template is not a valid URI!", Cr.NS_ERROR_FAILURE);
|
2006-06-20 09:07:00 +04:00
|
|
|
|
|
|
|
switch (templateURI.scheme) {
|
|
|
|
case "http":
|
|
|
|
case "https":
|
|
|
|
// Disable these for now, see bug 295018
|
|
|
|
// case "file":
|
|
|
|
// case "resource":
|
2006-06-28 20:16:12 +04:00
|
|
|
this.template = aTemplate;
|
2006-06-20 09:07:00 +04:00
|
|
|
break;
|
|
|
|
default:
|
2008-10-18 06:24:47 +04:00
|
|
|
FAIL("new EngineURL: template uses invalid scheme!", Cr.NS_ERROR_FAILURE);
|
2006-06-20 09:07:00 +04:00
|
|
|
}
|
2006-03-17 10:16:00 +03:00
|
|
|
}
|
|
|
|
EngineURL.prototype = {
|
|
|
|
|
|
|
|
addParam: function SRCH_EURL_addParam(aName, aValue) {
|
|
|
|
this.params.push(new QueryParameter(aName, aValue));
|
|
|
|
},
|
|
|
|
|
2008-10-07 00:57:18 +04:00
|
|
|
_addMozParam: function SRCH_EURL__addMozParam(aObj) {
|
|
|
|
aObj.mozparam = true;
|
|
|
|
this.mozparams[aObj.name] = aObj;
|
|
|
|
},
|
|
|
|
|
2006-07-05 22:06:47 +04:00
|
|
|
getSubmission: function SRCH_EURL_getSubmission(aSearchTerms, aEngine) {
|
|
|
|
var url = ParamSubstitution(this.template, aSearchTerms, aEngine);
|
2006-06-28 20:09:26 +04:00
|
|
|
|
|
|
|
// 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];
|
2006-07-05 22:06:47 +04:00
|
|
|
var value = ParamSubstitution(param.value, aSearchTerms, aEngine);
|
2006-06-28 20:09:26 +04:00
|
|
|
|
|
|
|
dataString += (i > 0 ? "&" : "") + param.name + "=" + value;
|
2006-03-17 10:16:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
var postData = null;
|
|
|
|
if (this.method == "GET") {
|
|
|
|
// GET method requests have no post data, and append the encoded
|
2006-06-28 20:09:26 +04:00
|
|
|
// query string to the url...
|
2006-03-17 10:16:00 +03:00
|
|
|
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);
|
|
|
|
},
|
|
|
|
|
2008-10-18 06:24:47 +04:00
|
|
|
_initWithJSON: function SRC_EURL__initWithJSON(aJson, aEngine) {
|
2008-10-07 00:57:18 +04:00
|
|
|
if (!aJson.params)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (let i = 0; i < aJson.params.length; ++i) {
|
|
|
|
let param = aJson.params[i];
|
|
|
|
if (param.mozparam) {
|
|
|
|
if (param.condition == "defaultEngine") {
|
2008-10-18 06:24:47 +04:00
|
|
|
if (aEngine._isDefaultEngine())
|
2008-10-07 00:57:18 +04:00
|
|
|
this.addParam(param.name, param.trueValue);
|
|
|
|
else
|
|
|
|
this.addParam(param.name, param.falseValue);
|
|
|
|
} else if (param.condition == "pref") {
|
|
|
|
let value = getMozParamPref(param.pref);
|
|
|
|
this.addParam(param.name, value);
|
|
|
|
}
|
|
|
|
this._addMozParam(param);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
this.addParam(param.name, param.value);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a JavaScript object that represents this URL.
|
|
|
|
* @returns An object suitable for serialization as JSON.
|
|
|
|
**/
|
|
|
|
_serializeToJSON: function SRCH_EURL__serializeToJSON() {
|
|
|
|
var json = {
|
|
|
|
template: this.template,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this.type != URLTYPE_SEARCH_HTML)
|
|
|
|
json.type = this.type;
|
|
|
|
if (this.method != "GET")
|
|
|
|
json.method = this.method;
|
|
|
|
|
|
|
|
function collapseMozParams(aParam)
|
|
|
|
this.mozparams[aParam.name] || aParam;
|
|
|
|
json.params = this.params.map(collapseMozParams, this);
|
|
|
|
|
|
|
|
return json;
|
|
|
|
},
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
/**
|
|
|
|
* Serializes the engine object to a OpenSearch Url element.
|
|
|
|
* @param aDoc
|
|
|
|
* The document to use to create the Url element.
|
|
|
|
* @param aElement
|
|
|
|
* The element to which the created Url element is appended.
|
|
|
|
*
|
|
|
|
* @see http://opensearch.a9.com/spec/1.1/querysyntax/#urltag
|
|
|
|
*/
|
|
|
|
_serializeToElement: function SRCH_EURL_serializeToEl(aDoc, aElement) {
|
2006-05-12 00:31:55 +04:00
|
|
|
var url = aDoc.createElementNS(OPENSEARCH_NS_11, "Url");
|
2006-03-17 10:16:00 +03:00
|
|
|
url.setAttribute("type", this.type);
|
|
|
|
url.setAttribute("method", this.method);
|
|
|
|
url.setAttribute("template", this.template);
|
|
|
|
|
|
|
|
for (var i = 0; i < this.params.length; ++i) {
|
2006-05-12 00:31:55 +04:00
|
|
|
var param = aDoc.createElementNS(OPENSEARCH_NS_11, "Param");
|
2006-03-17 10:16:00 +03:00
|
|
|
param.setAttribute("name", this.params[i].name);
|
|
|
|
param.setAttribute("value", this.params[i].value);
|
|
|
|
url.appendChild(aDoc.createTextNode("\n "));
|
|
|
|
url.appendChild(param);
|
|
|
|
}
|
|
|
|
url.appendChild(aDoc.createTextNode("\n"));
|
|
|
|
aElement.appendChild(url);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nsISearchEngine constructor.
|
|
|
|
* @param aLocation
|
|
|
|
* A nsILocalFile or nsIURI object representing the location of the
|
|
|
|
* search engine data file.
|
|
|
|
* @param aSourceDataType
|
|
|
|
* The data type of the file used to describe the engine. Must be either
|
|
|
|
* DATA_XML or DATA_TEXT.
|
|
|
|
* @param aIsReadOnly
|
|
|
|
* Boolean indicating whether the engine should be treated as read-only.
|
|
|
|
* Read only engines cannot be serialized to file.
|
|
|
|
*/
|
|
|
|
function Engine(aLocation, aSourceDataType, aIsReadOnly) {
|
|
|
|
this._dataType = aSourceDataType;
|
|
|
|
this._readOnly = aIsReadOnly;
|
|
|
|
this._urls = [];
|
|
|
|
|
2008-10-18 06:24:47 +04:00
|
|
|
if (aLocation.cached) {
|
|
|
|
this._file = aLocation.value;
|
|
|
|
} else if (aLocation instanceof Ci.nsILocalFile) {
|
2006-03-17 10:16:00 +03:00
|
|
|
// we already have a file (e.g. loading engines from disk)
|
|
|
|
this._file = aLocation;
|
|
|
|
} else if (aLocation instanceof Ci.nsIURI) {
|
|
|
|
this._uri = aLocation;
|
|
|
|
switch (aLocation.scheme) {
|
|
|
|
case "https":
|
|
|
|
case "http":
|
2006-06-21 01:53:09 +04:00
|
|
|
case "ftp":
|
2006-03-17 10:16:00 +03:00
|
|
|
case "data":
|
|
|
|
case "file":
|
|
|
|
case "resource":
|
2006-04-14 03:28:21 +04:00
|
|
|
this._uri = aLocation;
|
2006-03-17 10:16:00 +03:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
Engine.prototype = {
|
|
|
|
// The engine's alias.
|
|
|
|
_alias: null,
|
2006-05-29 22:48:42 +04:00
|
|
|
// The data describing the engine. Is either an array of bytes, for Sherlock
|
2006-03-17 10:16:00 +03:00
|
|
|
// files, or an XML document element, for XML plugins.
|
|
|
|
_data: null,
|
|
|
|
// The engine's data type. See data types (DATA_) defined above.
|
|
|
|
_dataType: null,
|
|
|
|
// Whether or not the engine is readonly.
|
|
|
|
_readOnly: true,
|
|
|
|
// The engine's description
|
|
|
|
_description: "",
|
2006-07-21 03:00:39 +04:00
|
|
|
// Used to store the engine to replace, if we're an update to an existing
|
|
|
|
// engine.
|
|
|
|
_engineToUpdate: null,
|
2006-03-17 10:16:00 +03:00
|
|
|
// The file from which the plugin was loaded.
|
2008-10-18 06:24:47 +04:00
|
|
|
__file: null,
|
|
|
|
get _file() {
|
|
|
|
if (this.__file && !(this.__file instanceof Ci.nsILocalFile)) {
|
|
|
|
let file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
|
|
|
|
file.persistentDescriptor = this.__file;
|
|
|
|
return this.__file = file;
|
|
|
|
}
|
|
|
|
return this.__file;
|
|
|
|
},
|
|
|
|
set _file(aValue) {
|
|
|
|
this.__file = aValue;
|
|
|
|
},
|
2006-05-29 23:00:56 +04:00
|
|
|
// Set to true if the engine has a preferred icon (an icon that should not be
|
|
|
|
// overridden by a non-preferred icon).
|
|
|
|
_hasPreferredIcon: null,
|
2006-03-17 10:16:00 +03:00
|
|
|
// Whether the engine is hidden from the user.
|
|
|
|
_hidden: null,
|
|
|
|
// The engine's name.
|
|
|
|
_name: null,
|
|
|
|
// The engine type. See engine types (TYPE_) defined above.
|
|
|
|
_type: null,
|
|
|
|
// The name of the charset used to submit the search terms.
|
|
|
|
_queryCharset: null,
|
2006-05-31 05:10:05 +04:00
|
|
|
// A URL string pointing to the engine's search form.
|
|
|
|
_searchForm: null,
|
2006-03-17 10:16:00 +03:00
|
|
|
// The URI object from which the engine was retrieved.
|
2006-07-14 20:31:27 +04:00
|
|
|
// This is null for local plugins, and is used for error messages and logging.
|
2006-03-17 10:16:00 +03:00
|
|
|
_uri: null,
|
2006-07-14 20:31:27 +04:00
|
|
|
// 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.
|
2006-08-11 01:16:13 +04:00
|
|
|
_useNow: true,
|
2007-05-04 19:48:08 +04:00
|
|
|
// Where the engine was loaded from. Can be one of: SEARCH_APP_DIR,
|
|
|
|
// SEARCH_PROFILE_DIR, SEARCH_IN_EXTENSION.
|
|
|
|
__installLocation: null,
|
2006-07-21 03:00:39 +04:00
|
|
|
// 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,
|
2006-12-11 23:54:35 +03:00
|
|
|
// A reference to the timer used for lazily serializing the engine to file
|
|
|
|
_serializeTimer: null,
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the data from the engine's file. If the engine's dataType is
|
|
|
|
* XML, the document element is placed in the engine's data field. For text
|
|
|
|
* engines, the data is just read directly from file and placed as an array
|
|
|
|
* of lines in the engine's data field.
|
|
|
|
*/
|
2006-04-14 03:28:21 +04:00
|
|
|
_initFromFile: function SRCH_ENG_initFromFile() {
|
2008-10-18 06:24:47 +04:00
|
|
|
if (!this._file || !this._file.exists())
|
|
|
|
FAIL("File must exist before calling initFromFile!", Cr.NS_ERROR_UNEXPECTED);
|
2006-07-21 03:00:39 +04:00
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
var fileInStream = Cc["@mozilla.org/network/file-input-stream;1"].
|
|
|
|
createInstance(Ci.nsIFileInputStream);
|
|
|
|
|
|
|
|
fileInStream.init(this._file, MODE_RDONLY, PERMS_FILE, false);
|
|
|
|
|
|
|
|
switch (this._dataType) {
|
2006-04-14 03:28:21 +04:00
|
|
|
case SEARCH_DATA_XML:
|
2006-03-17 10:16:00 +03:00
|
|
|
var domParser = Cc["@mozilla.org/xmlextras/domparser;1"].
|
|
|
|
createInstance(Ci.nsIDOMParser);
|
|
|
|
var doc = domParser.parseFromStream(fileInStream, "UTF-8",
|
|
|
|
this._file.fileSize,
|
|
|
|
"text/xml");
|
|
|
|
|
|
|
|
this._data = doc.documentElement;
|
|
|
|
break;
|
2006-04-14 03:28:21 +04:00
|
|
|
case SEARCH_DATA_TEXT:
|
2006-05-17 05:37:49 +04:00
|
|
|
var binaryInStream = Cc["@mozilla.org/binaryinputstream;1"].
|
|
|
|
createInstance(Ci.nsIBinaryInputStream);
|
|
|
|
binaryInStream.setInputStream(fileInStream);
|
2006-03-17 10:16:00 +03:00
|
|
|
|
2006-05-17 05:37:49 +04:00
|
|
|
var bytes = binaryInStream.readByteArray(binaryInStream.available());
|
2006-05-29 22:48:42 +04:00
|
|
|
this._data = bytes;
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ERROR("Bogus engine _dataType: \"" + this._dataType + "\"",
|
|
|
|
Cr.NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
|
|
|
fileInStream.close();
|
2006-04-14 03:28:21 +04:00
|
|
|
|
|
|
|
// Now that the data is loaded, initialize the engine object
|
2006-08-30 10:46:36 +04:00
|
|
|
this._initFromData();
|
2006-03-17 10:16:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the engine data from a URI.
|
|
|
|
*/
|
2006-04-14 03:28:21 +04:00
|
|
|
_initFromURI: function SRCH_ENG_initFromURI() {
|
|
|
|
ENSURE_WARN(this._uri instanceof Ci.nsIURI,
|
|
|
|
"Must have URI when calling _initFromURI!",
|
|
|
|
Cr.NS_ERROR_UNEXPECTED);
|
2006-03-17 10:16:00 +03:00
|
|
|
|
2006-04-14 03:28:21 +04:00
|
|
|
LOG("_initFromURI: Downloading engine from: \"" + this._uri.spec + "\".");
|
2006-03-17 10:16:00 +03:00
|
|
|
|
2008-10-07 00:57:18 +04:00
|
|
|
var chan = gIoSvc.newChannelFromURI(this._uri);
|
2006-03-17 10:16:00 +03:00
|
|
|
|
2006-07-21 03:00:39 +04:00
|
|
|
if (this._engineToUpdate && (chan instanceof Ci.nsIHttpChannel)) {
|
|
|
|
var lastModified = engineMetadataService.getAttr(this._engineToUpdate,
|
|
|
|
"updatelastmodified");
|
|
|
|
if (lastModified)
|
|
|
|
chan.setRequestHeader("If-Modified-Since", lastModified, false);
|
|
|
|
}
|
2006-05-17 05:37:49 +04:00
|
|
|
var listener = new loadListener(chan, this, this._onLoad);
|
|
|
|
chan.notificationCallbacks = listener;
|
|
|
|
chan.asyncOpen(listener, null);
|
2006-03-17 10:16:00 +03:00
|
|
|
},
|
|
|
|
|
2006-06-16 23:31:40 +04:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
_getURLOfType: function SRCH_ENG__getURLOfType(aType) {
|
|
|
|
for (var i = 0; i < this._urls.length; ++i) {
|
|
|
|
if (this._urls[i].type == aType)
|
|
|
|
return this._urls[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
|
|
|
|
2006-07-14 20:31:27 +04:00
|
|
|
_confirmAddEngine: function SRCH_SVC_confirmAddEngine() {
|
|
|
|
var sbs = Cc["@mozilla.org/intl/stringbundle;1"].
|
|
|
|
getService(Ci.nsIStringBundleService);
|
2006-07-20 03:55:13 +04:00
|
|
|
var stringBundle = sbs.createBundle(SEARCH_BUNDLE);
|
2006-07-14 20:31:27 +04:00
|
|
|
var titleMessage = stringBundle.GetStringFromName("addEngineConfirmTitle");
|
|
|
|
|
|
|
|
// Display only the hostname portion of the URL.
|
|
|
|
var dialogMessage =
|
2006-07-20 03:55:13 +04:00
|
|
|
stringBundle.formatStringFromName("addEngineConfirmation",
|
2006-07-14 20:31:27 +04:00
|
|
|
[this._name, this._uri.host], 2);
|
|
|
|
var checkboxMessage = stringBundle.GetStringFromName("addEngineUseNowText");
|
|
|
|
var addButtonLabel =
|
|
|
|
stringBundle.GetStringFromName("addEngineAddButtonLabel");
|
|
|
|
|
|
|
|
var ps = Cc["@mozilla.org/embedcomp/prompt-service;1"].
|
|
|
|
getService(Ci.nsIPromptService);
|
|
|
|
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};
|
|
|
|
},
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2006-05-17 05:37:49 +04:00
|
|
|
_onLoad: function SRCH_ENG_onLoad(aBytes, aEngine) {
|
|
|
|
/**
|
|
|
|
* Handle an error during the load of an engine by prompting the user to
|
|
|
|
* notify him that the load failed.
|
|
|
|
*/
|
2006-07-24 22:41:25 +04:00
|
|
|
function onError(aErrorString, aTitleString) {
|
2006-07-21 03:00:39 +04:00
|
|
|
if (aEngine._engineToUpdate) {
|
|
|
|
// We're in an update, so just fail quietly
|
|
|
|
LOG("updating " + aEngine._engineToUpdate.name + " failed");
|
|
|
|
return;
|
|
|
|
}
|
2006-05-17 05:37:49 +04:00
|
|
|
var sbs = Cc["@mozilla.org/intl/stringbundle;1"].
|
|
|
|
getService(Ci.nsIStringBundleService);
|
2006-07-24 22:41:25 +04:00
|
|
|
|
2006-05-17 05:37:49 +04:00
|
|
|
var brandBundle = sbs.createBundle(BRAND_BUNDLE);
|
|
|
|
var brandName = brandBundle.GetStringFromName("brandShortName");
|
2006-07-24 22:41:25 +04:00
|
|
|
|
|
|
|
var searchBundle = sbs.createBundle(SEARCH_BUNDLE);
|
|
|
|
var msgStringName = aErrorString || "error_loading_engine_msg2";
|
|
|
|
var titleStringName = aTitleString || "error_loading_engine_title";
|
|
|
|
var title = searchBundle.GetStringFromName(titleStringName);
|
|
|
|
var text = searchBundle.formatStringFromName(msgStringName,
|
2006-05-17 05:37:49 +04:00
|
|
|
[brandName, aEngine._location],
|
|
|
|
2);
|
|
|
|
|
|
|
|
var ww = Cc["@mozilla.org/embedcomp/window-watcher;1"].
|
|
|
|
getService(Ci.nsIWindowWatcher);
|
|
|
|
ww.getNewPrompter(null).alert(title, text);
|
|
|
|
}
|
2006-03-17 10:16:00 +03:00
|
|
|
|
2006-05-17 05:37:49 +04:00
|
|
|
if (!aBytes) {
|
|
|
|
onError();
|
2006-03-17 10:16:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-07-21 03:00:39 +04:00
|
|
|
var engineToUpdate = null;
|
|
|
|
if (aEngine._engineToUpdate) {
|
|
|
|
engineToUpdate = aEngine._engineToUpdate.wrappedJSObject;
|
|
|
|
|
|
|
|
// Make this new engine use the old engine's file.
|
|
|
|
aEngine._file = engineToUpdate._file;
|
|
|
|
}
|
|
|
|
|
2006-05-17 05:37:49 +04:00
|
|
|
switch (aEngine._dataType) {
|
2006-04-14 03:28:21 +04:00
|
|
|
case SEARCH_DATA_XML:
|
2006-05-17 05:37:49 +04:00
|
|
|
var parser = Cc["@mozilla.org/xmlextras/domparser;1"].
|
|
|
|
createInstance(Ci.nsIDOMParser);
|
2007-05-03 00:06:43 +04:00
|
|
|
var doc = parser.parseFromBuffer(aBytes, aBytes.length, "text/xml");
|
2006-05-17 05:37:49 +04:00
|
|
|
aEngine._data = doc.documentElement;
|
2006-03-17 10:16:00 +03:00
|
|
|
break;
|
2006-04-14 03:28:21 +04:00
|
|
|
case SEARCH_DATA_TEXT:
|
2006-05-29 22:48:42 +04:00
|
|
|
aEngine._data = aBytes;
|
2006-03-17 10:16:00 +03:00
|
|
|
break;
|
|
|
|
default:
|
2006-05-17 05:37:49 +04:00
|
|
|
onError();
|
2006-03-17 10:16:00 +03:00
|
|
|
LOG("_onLoad: Bogus engine _dataType: \"" + this._dataType + "\"");
|
|
|
|
return;
|
|
|
|
}
|
2006-07-21 03:00:39 +04:00
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
try {
|
|
|
|
// Initialize the engine from the obtained data
|
2006-05-17 05:37:49 +04:00
|
|
|
aEngine._initFromData();
|
2006-03-17 10:16:00 +03:00
|
|
|
} catch (ex) {
|
|
|
|
LOG("_onLoad: Failed to init engine!\n" + ex);
|
2006-05-17 05:37:49 +04:00
|
|
|
// Report an error to the user
|
|
|
|
onError();
|
2006-03-17 10:16:00 +03:00
|
|
|
return;
|
2006-07-24 22:41:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check to see if this is a duplicate engine. If we're confirming the
|
|
|
|
// engine load, then we display a "this is a duplicate engine" prompt,
|
|
|
|
// otherwise we fail silently.
|
2006-08-11 00:21:26 +04:00
|
|
|
if (!engineToUpdate) {
|
|
|
|
var ss = Cc["@mozilla.org/browser/search-service;1"].
|
|
|
|
getService(Ci.nsIBrowserSearchService);
|
|
|
|
if (ss.getEngineByName(aEngine.name)) {
|
|
|
|
if (aEngine._confirm)
|
|
|
|
onError("error_duplicate_engine_msg", "error_invalid_engine_title");
|
|
|
|
|
|
|
|
LOG("_onLoad: duplicate engine found, bailing");
|
|
|
|
return;
|
|
|
|
}
|
2006-03-17 10:16:00 +03:00
|
|
|
}
|
|
|
|
|
2006-07-14 20:31:27 +04:00
|
|
|
// If requested, confirm the addition now that we have the title.
|
2006-09-18 04:43:32 +04:00
|
|
|
// This property is only ever true for engines added via
|
|
|
|
// nsIBrowserSearchService::addEngine.
|
2006-07-14 20:31:27 +04:00
|
|
|
if (aEngine._confirm) {
|
|
|
|
var confirmation = aEngine._confirmAddEngine();
|
|
|
|
LOG("_onLoad: confirm is " + confirmation.confirmed +
|
|
|
|
"; useNow is " + confirmation.useNow);
|
|
|
|
if (!confirmation.confirmed)
|
|
|
|
return;
|
|
|
|
aEngine._useNow = confirmation.useNow;
|
|
|
|
}
|
|
|
|
|
2006-07-21 03:00:39 +04:00
|
|
|
// If we don't yet have a file, get one now. The only case where we would
|
2006-09-18 04:43:32 +04:00
|
|
|
// already have a file is if this is an update and _file was set above.
|
2006-07-21 03:00:39 +04:00
|
|
|
if (!aEngine._file)
|
|
|
|
aEngine._file = getSanitizedFile(aEngine.name);
|
|
|
|
|
|
|
|
if (engineToUpdate) {
|
|
|
|
// Keep track of the last modified date, so that we can make conditional
|
|
|
|
// requests for future updates.
|
|
|
|
engineMetadataService.setAttr(aEngine, "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;
|
2006-09-18 04:43:32 +04:00
|
|
|
|
|
|
|
// Clear the "use now" flag since we don't want to be changing the
|
|
|
|
// current engine for an update.
|
|
|
|
aEngine._useNow = false;
|
2006-07-21 03:00:39 +04:00
|
|
|
}
|
2006-07-14 20:31:27 +04:00
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
// Write the engine to file
|
2006-05-17 05:37:49 +04:00
|
|
|
aEngine._serializeToFile();
|
2006-03-17 10:16:00 +03:00
|
|
|
|
2006-07-21 03:00:39 +04:00
|
|
|
// Notify the search service of the sucessful load. It will deal with
|
|
|
|
// updates by checking aEngine._engineToUpdate.
|
2006-05-17 05:37:49 +04:00
|
|
|
notifyAction(aEngine, SEARCH_ENGINE_LOADED);
|
2006-03-17 10:16:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the .iconURI property of the engine.
|
|
|
|
*
|
|
|
|
* @param aIconURL
|
2006-06-21 01:53:09 +04:00
|
|
|
* 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.
|
2006-05-29 23:00:56 +04:00
|
|
|
* @param aIsPreferred
|
|
|
|
* Whether or not this icon is to be preferred. Preferred icons can
|
|
|
|
* override non-preferred icons.
|
2006-03-17 10:16:00 +03:00
|
|
|
*/
|
2006-05-29 23:00:56 +04:00
|
|
|
_setIcon: function SRCH_ENG_setIcon(aIconURL, aIsPreferred) {
|
|
|
|
// If we already have a preferred icon, and this isn't a preferred icon,
|
|
|
|
// just ignore it.
|
|
|
|
if (this._hasPreferredIcon && !aIsPreferred)
|
|
|
|
return;
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
var uri = makeURI(aIconURL);
|
|
|
|
|
|
|
|
// Ignore bad URIs
|
|
|
|
if (!uri)
|
|
|
|
return;
|
|
|
|
|
|
|
|
LOG("_setIcon: Setting icon url \"" + uri.spec + "\" for engine \""
|
|
|
|
+ this.name + "\".");
|
2006-06-21 01:53:09 +04:00
|
|
|
// Only accept remote icons from http[s] or ftp
|
2006-03-17 10:16:00 +03:00
|
|
|
switch (uri.scheme) {
|
|
|
|
case "data":
|
|
|
|
this._iconURI = uri;
|
2006-05-29 23:00:56 +04:00
|
|
|
notifyAction(this, SEARCH_ENGINE_CHANGED);
|
2006-05-29 23:04:14 +04:00
|
|
|
this._hasPreferredIcon = aIsPreferred;
|
2006-03-17 10:16:00 +03:00
|
|
|
break;
|
|
|
|
case "http":
|
|
|
|
case "https":
|
2006-06-21 01:53:09 +04:00
|
|
|
case "ftp":
|
2006-03-17 10:16:00 +03:00
|
|
|
// No use downloading the icon if the engine file is read-only
|
|
|
|
if (!this._readOnly) {
|
|
|
|
LOG("_setIcon: Downloading icon: \"" + uri.spec +
|
|
|
|
"\" for engine: \"" + this.name + "\"");
|
2008-10-07 00:57:18 +04:00
|
|
|
var chan = gIoSvc.newChannelFromURI(uri);
|
2006-05-17 05:37:49 +04:00
|
|
|
|
|
|
|
function iconLoadCallback(aByteArray, aEngine) {
|
2006-05-29 23:00:56 +04:00
|
|
|
// This callback may run after we've already set a preferred icon,
|
|
|
|
// so check again.
|
|
|
|
if (aEngine._hasPreferredIcon && !aIsPreferred)
|
|
|
|
return;
|
|
|
|
|
2006-05-17 05:37:49 +04:00
|
|
|
if (!aByteArray || aByteArray.length > MAX_ICON_SIZE) {
|
2006-07-21 03:00:39 +04:00
|
|
|
LOG("iconLoadCallback: load failed, or the icon was too large!");
|
2006-05-17 05:37:49 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-12-21 01:41:20 +03:00
|
|
|
var str = btoa(String.fromCharCode.apply(null, aByteArray));
|
2006-05-17 05:37:49 +04:00
|
|
|
aEngine._iconURI = makeURI(ICON_DATAURL_PREFIX + str);
|
|
|
|
|
|
|
|
// The engine might not have a file yet, if it's being downloaded,
|
|
|
|
// because the request for the engine file itself (_onLoad) may not
|
|
|
|
// yet be complete. In that case, this change will be written to
|
|
|
|
// file when _onLoad is called.
|
|
|
|
if (aEngine._file)
|
|
|
|
aEngine._serializeToFile();
|
|
|
|
|
|
|
|
notifyAction(aEngine, SEARCH_ENGINE_CHANGED);
|
2006-05-29 23:04:14 +04:00
|
|
|
aEngine._hasPreferredIcon = aIsPreferred;
|
2006-05-17 05:37:49 +04:00
|
|
|
}
|
|
|
|
|
2006-07-21 03:00:39 +04:00
|
|
|
// 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);
|
2006-03-17 10:16:00 +03:00
|
|
|
chan.notificationCallbacks = listener;
|
|
|
|
chan.asyncOpen(listener, null);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize this Engine object from the collected data.
|
|
|
|
*/
|
2006-04-14 03:28:21 +04:00
|
|
|
_initFromData: function SRCH_ENG_initFromData() {
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
ENSURE_WARN(this._data, "Can't init an engine with no data!",
|
|
|
|
Cr.NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
// Find out what type of engine we are
|
|
|
|
switch (this._dataType) {
|
2006-04-14 03:28:21 +04:00
|
|
|
case SEARCH_DATA_XML:
|
2006-05-12 00:31:55 +04:00
|
|
|
if (checkNameSpace(this._data, [MOZSEARCH_LOCALNAME],
|
|
|
|
[MOZSEARCH_NS_10])) {
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
LOG("_init: Initing MozSearch plugin from " + this._location);
|
|
|
|
|
2006-04-14 03:28:21 +04:00
|
|
|
this._type = SEARCH_TYPE_MOZSEARCH;
|
2006-03-17 10:16:00 +03:00
|
|
|
this._parseAsMozSearch();
|
|
|
|
|
2006-05-12 00:31:55 +04:00
|
|
|
} else if (checkNameSpace(this._data, [OPENSEARCH_LOCALNAME],
|
|
|
|
OPENSEARCH_NAMESPACES)) {
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
LOG("_init: Initing OpenSearch plugin from " + this._location);
|
|
|
|
|
2006-04-14 03:28:21 +04:00
|
|
|
this._type = SEARCH_TYPE_OPENSEARCH;
|
2006-03-17 10:16:00 +03:00
|
|
|
this._parseAsOpenSearch();
|
|
|
|
|
|
|
|
} else
|
2008-10-18 06:24:47 +04:00
|
|
|
FAIL(this._location + " is not a valid search plugin.", Cr.NS_ERROR_FAILURE);
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
break;
|
2006-04-14 03:28:21 +04:00
|
|
|
case SEARCH_DATA_TEXT:
|
2006-03-17 10:16:00 +03:00
|
|
|
LOG("_init: Initing Sherlock plugin from " + this._location);
|
|
|
|
|
|
|
|
// the only text-based format we support is Sherlock
|
2006-04-14 03:28:21 +04:00
|
|
|
this._type = SEARCH_TYPE_SHERLOCK;
|
2006-03-17 10:16:00 +03:00
|
|
|
this._parseAsSherlock();
|
|
|
|
}
|
2007-07-09 03:45:35 +04:00
|
|
|
|
|
|
|
// No need to keep a ref to our data (which in some cases can be a document
|
|
|
|
// element) past this point
|
|
|
|
this._data = null;
|
2006-03-17 10:16:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize this Engine object from a collection of metadata.
|
|
|
|
*/
|
|
|
|
_initFromMetadata: function SRCH_ENG_initMetaData(aName, aIconURL, aAlias,
|
|
|
|
aDescription, aMethod,
|
|
|
|
aTemplate) {
|
|
|
|
ENSURE_WARN(!this._readOnly,
|
|
|
|
"Can't call _initFromMetaData on a readonly engine!",
|
|
|
|
Cr.NS_ERROR_FAILURE);
|
|
|
|
|
2006-05-29 23:19:05 +04:00
|
|
|
this._urls.push(new EngineURL("text/html", aMethod, aTemplate));
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
this._name = aName;
|
2008-02-26 01:13:30 +03:00
|
|
|
this.alias = aAlias;
|
2006-03-17 10:16:00 +03:00
|
|
|
this._description = aDescription;
|
2006-05-29 23:00:56 +04:00
|
|
|
this._setIcon(aIconURL, true);
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
this._serializeToFile();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts data from an OpenSearch URL element and creates an EngineURL
|
|
|
|
* object which is then added to the engine's list of URLs.
|
|
|
|
*
|
2006-05-29 23:19:05 +04:00
|
|
|
* @throws NS_ERROR_FAILURE if a URL object could not be created.
|
|
|
|
*
|
2006-03-17 10:16:00 +03:00
|
|
|
* @see http://opensearch.a9.com/spec/1.1/querysyntax/#urltag.
|
|
|
|
* @see EngineURL()
|
|
|
|
*/
|
|
|
|
_parseURL: function SRCH_ENG_parseURL(aElement) {
|
|
|
|
var type = aElement.getAttribute("type");
|
2006-05-12 00:31:55 +04:00
|
|
|
// According to the spec, method is optional, defaulting to "GET" if not
|
|
|
|
// specified
|
|
|
|
var method = aElement.getAttribute("method") || "GET";
|
2006-03-17 10:16:00 +03:00
|
|
|
var template = aElement.getAttribute("template");
|
|
|
|
|
2006-05-29 23:19:05 +04:00
|
|
|
try {
|
|
|
|
var url = new EngineURL(type, method, template);
|
|
|
|
} catch (ex) {
|
2008-10-18 06:24:47 +04:00
|
|
|
FAIL("_parseURL: failed to add " + template + " as a URL",
|
|
|
|
Cr.NS_ERROR_FAILURE);
|
2006-05-29 23:19:05 +04:00
|
|
|
}
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
for (var i = 0; i < aElement.childNodes.length; ++i) {
|
|
|
|
var param = aElement.childNodes[i];
|
2006-05-29 23:19:05 +04:00
|
|
|
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");
|
|
|
|
}
|
2006-07-05 22:06:47 +04:00
|
|
|
} else if (param.localName == "MozParam" &&
|
2007-05-04 19:48:08 +04:00
|
|
|
// We only support MozParams for default search engines
|
|
|
|
this._isDefault) {
|
2006-07-05 22:06:47 +04:00
|
|
|
var value;
|
2006-09-08 23:27:29 +04:00
|
|
|
switch (param.getAttribute("condition")) {
|
|
|
|
case "defaultEngine":
|
|
|
|
// If this engine was the default search engine, use the true value
|
2008-10-07 00:57:18 +04:00
|
|
|
if (this._isDefaultEngine())
|
2006-09-08 23:27:29 +04:00
|
|
|
value = param.getAttribute("trueValue");
|
|
|
|
else
|
|
|
|
value = param.getAttribute("falseValue");
|
|
|
|
url.addParam(param.getAttribute("name"), value);
|
2008-10-07 00:57:18 +04:00
|
|
|
url._addMozParam({"name": param.getAttribute("name"),
|
|
|
|
"falseValue": param.getAttribute("falseValue"),
|
|
|
|
"trueValue": param.getAttribute("trueValue"),
|
|
|
|
"condition": "defaultEngine"});
|
2006-09-08 23:27:29 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "pref":
|
|
|
|
try {
|
2008-10-07 00:57:18 +04:00
|
|
|
value = getMozParamPref(param.getAttribute("pref"), value);
|
2006-09-08 23:27:29 +04:00
|
|
|
url.addParam(param.getAttribute("name"), value);
|
2008-10-07 00:57:18 +04:00
|
|
|
url._addMozParam({"pref": param.getAttribute("pref"),
|
|
|
|
"name": param.getAttribute("name"),
|
|
|
|
"condition": "pref"});
|
2006-09-08 23:27:29 +04:00
|
|
|
} catch (e) { }
|
|
|
|
break;
|
2006-07-05 22:06:47 +04:00
|
|
|
}
|
2006-05-29 23:19:05 +04:00
|
|
|
}
|
2006-03-17 10:16:00 +03:00
|
|
|
}
|
2006-05-29 23:19:05 +04:00
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
this._urls.push(url);
|
|
|
|
},
|
|
|
|
|
2008-10-07 00:57:18 +04:00
|
|
|
_isDefaultEngine: function SRCH_ENG__isDefaultEngine() {
|
|
|
|
let defaultPrefB = gPrefSvc.QueryInterface(Ci.nsIPrefService)
|
|
|
|
.getDefaultBranch(BROWSER_SEARCH_PREF);
|
|
|
|
let nsIPLS = Ci.nsIPrefLocalizedString;
|
|
|
|
let defaultEngine;
|
|
|
|
try {
|
|
|
|
defaultEngine = defaultPrefB.getComplexValue("defaultenginename", nsIPLS).data;
|
|
|
|
} catch (ex) {}
|
|
|
|
return this.name == defaultEngine;
|
|
|
|
},
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
/**
|
|
|
|
* 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: \"" + aElement.textContent + "\"");
|
|
|
|
if (aElement.getAttribute("width") == "16" &&
|
|
|
|
aElement.getAttribute("height") == "16") {
|
2006-05-29 23:00:56 +04:00
|
|
|
this._setIcon(aElement.textContent, true);
|
2006-03-17 10:16:00 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_parseAsMozSearch: function SRCH_ENG_parseAsMoz() {
|
2006-05-31 05:10:05 +04:00
|
|
|
//forward to the OpenSearch parser
|
2006-03-17 10:16:00 +03:00
|
|
|
this._parseAsOpenSearch();
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract search engine information from the collected data to initialize
|
|
|
|
* the engine object.
|
|
|
|
*/
|
|
|
|
_parseAsOpenSearch: function SRCH_ENG_parseAsOS() {
|
|
|
|
var doc = this._data;
|
2006-04-26 09:13:07 +04:00
|
|
|
|
2006-11-11 08:26:26 +03:00
|
|
|
// The OpenSearch spec sets a default value for the input encoding.
|
|
|
|
this._queryCharset = OS_PARAM_INPUT_ENCODING_DEF;
|
|
|
|
|
2006-04-26 09:13:07 +04:00
|
|
|
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":
|
2006-05-29 23:19:05 +04:00
|
|
|
try {
|
|
|
|
this._parseURL(child);
|
|
|
|
} catch (ex) {
|
|
|
|
// Parsing of the element failed, just skip it.
|
|
|
|
}
|
2006-04-26 09:13:07 +04:00
|
|
|
break;
|
|
|
|
case "Image":
|
|
|
|
this._parseImage(child);
|
|
|
|
break;
|
|
|
|
case "InputEncoding":
|
|
|
|
this._queryCharset = child.textContent.toUpperCase();
|
|
|
|
break;
|
2006-07-21 03:00:39 +04:00
|
|
|
|
2006-05-31 05:10:05 +04:00
|
|
|
// Non-OpenSearch elements
|
|
|
|
case "SearchForm":
|
|
|
|
this._searchForm = child.textContent;
|
|
|
|
break;
|
2006-07-21 03:00:39 +04:00
|
|
|
case "UpdateUrl":
|
|
|
|
this._updateURL = child.textContent;
|
|
|
|
break;
|
|
|
|
case "UpdateInterval":
|
2006-07-21 05:30:48 +04:00
|
|
|
this._updateInterval = parseInt(child.textContent);
|
2006-07-21 03:00:39 +04:00
|
|
|
break;
|
2006-09-01 22:50:31 +04:00
|
|
|
case "IconUpdateUrl":
|
|
|
|
this._iconUpdateURL = child.textContent;
|
|
|
|
break;
|
2006-04-26 09:13:07 +04:00
|
|
|
}
|
|
|
|
}
|
2008-10-18 06:24:47 +04:00
|
|
|
if (!this.name || (this._urls.length == 0))
|
|
|
|
FAIL("_parseAsOpenSearch: No name, or missing URL!", Cr.NS_ERROR_FAILURE);
|
|
|
|
if (!this.supportsResponseType(URLTYPE_SEARCH_HTML))
|
|
|
|
FAIL("_parseAsOpenSearch: No text/html result type!", Cr.NS_ERROR_FAILURE);
|
2006-03-17 10:16:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extract search engine information from the collected data to initialize
|
|
|
|
* the engine object.
|
|
|
|
*/
|
|
|
|
_parseAsSherlock: function SRCH_ENG_parseAsSherlock() {
|
|
|
|
/**
|
|
|
|
* Extracts one Sherlock "section" from aSource. A section is essentially
|
|
|
|
* an HTML element with attributes, but each attribute must be on a new
|
|
|
|
* line, by definition.
|
|
|
|
*
|
|
|
|
* @param aLines
|
|
|
|
* An array of lines from the sherlock file.
|
|
|
|
* @param aSection
|
|
|
|
* The name of the section (e.g. "search" or "browser"). This value
|
|
|
|
* is not case sensitive.
|
|
|
|
* @returns an object whose properties correspond to the section's
|
|
|
|
* attributes.
|
|
|
|
*/
|
|
|
|
function getSection(aLines, aSection) {
|
|
|
|
LOG("_parseAsSherlock::getSection: Sherlock lines:\n" +
|
|
|
|
aLines.join("\n"));
|
|
|
|
var lines = aLines;
|
|
|
|
var startMark = new RegExp("^\\s*<" + aSection.toLowerCase() + "\\s*",
|
|
|
|
"gi");
|
|
|
|
var endMark = /\s*>\s*$/gi;
|
|
|
|
|
|
|
|
var foundStart = false;
|
|
|
|
var startLine, numberOfLines;
|
|
|
|
// Find the beginning and end of the section
|
2006-04-28 06:18:58 +04:00
|
|
|
for (var i = 0; i < lines.length; i++) {
|
2006-03-17 10:16:00 +03:00
|
|
|
if (foundStart) {
|
|
|
|
if (endMark.test(lines[i])) {
|
|
|
|
numberOfLines = i - startLine;
|
|
|
|
// Remove the end marker
|
|
|
|
lines[i] = lines[i].replace(endMark, "");
|
|
|
|
// If the endmarker was not the only thing on the line, include
|
|
|
|
// this line in the results
|
|
|
|
if (lines[i])
|
|
|
|
numberOfLines++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (startMark.test(lines[i])) {
|
|
|
|
foundStart = true;
|
|
|
|
// Remove the start marker
|
|
|
|
lines[i] = lines[i].replace(startMark, "");
|
|
|
|
startLine = i;
|
|
|
|
// If the line is empty, don't include it in the result
|
|
|
|
if (!lines[i])
|
|
|
|
startLine++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG("_parseAsSherlock::getSection: Start index: " + startLine +
|
|
|
|
"\nNumber of lines: " + numberOfLines);
|
|
|
|
lines = lines.splice(startLine, numberOfLines);
|
|
|
|
LOG("_parseAsSherlock::getSection: Section lines:\n" +
|
|
|
|
lines.join("\n"));
|
|
|
|
|
|
|
|
var section = {};
|
2006-04-28 06:18:58 +04:00
|
|
|
for (var i = 0; i < lines.length; i++) {
|
2008-09-09 08:48:07 +04:00
|
|
|
var line = lines[i].trim();
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
var els = line.split("=");
|
2008-09-09 08:48:07 +04:00
|
|
|
var name = els.shift().trim().toLowerCase();
|
|
|
|
var value = els.join("=").trim();
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
if (!name || !value)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Strip leading and trailing whitespace, remove quotes from the
|
|
|
|
// value, and remove any trailing slashes or ">" characters
|
|
|
|
value = value.replace(/^["']/, "")
|
|
|
|
.replace(/["']\s*[\\\/]?>?\s*$/, "") || "";
|
2008-09-09 08:48:07 +04:00
|
|
|
value = value.trim();
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
// Don't clobber existing attributes
|
|
|
|
if (!(name in section))
|
|
|
|
section[name] = value;
|
|
|
|
}
|
|
|
|
return section;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of name-value pair arrays representing the Sherlock
|
2006-06-28 20:09:26 +04:00
|
|
|
* file's input elements. User defined inputs return USER_DEFINED
|
|
|
|
* as the value. Elements are returned in the order they appear in the
|
|
|
|
* source file.
|
2006-03-17 10:16:00 +03:00
|
|
|
*
|
|
|
|
* Example:
|
|
|
|
* <input name="foo" value="bar">
|
|
|
|
* <input name="foopy" user>
|
|
|
|
* Returns:
|
|
|
|
* [["foo", "bar"], ["foopy", "{searchTerms}"]]
|
|
|
|
*
|
|
|
|
* @param aLines
|
|
|
|
* An array of lines from the source file.
|
|
|
|
*/
|
|
|
|
function getInputs(aLines) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts an attribute value from a given a line of text.
|
|
|
|
* Example: <input value="foo" name="bar">
|
|
|
|
* Extracts the string |foo| or |bar| given an input aAttr of
|
|
|
|
* |value| or |name|.
|
|
|
|
* Attributes may be quoted or unquoted. If unquoted, any whitespace
|
|
|
|
* indicates the end of the attribute value.
|
|
|
|
* Example: < value=22 33 name=44\334 >
|
|
|
|
* Returns |22| for "value" and |44\334| for "name".
|
|
|
|
*
|
|
|
|
* @param aAttr
|
|
|
|
* The name of the attribute for which to obtain the value. This
|
|
|
|
* value is not case sensitive.
|
|
|
|
* @param aLine
|
|
|
|
* The line containing the attribute.
|
|
|
|
*
|
|
|
|
* @returns the attribute value, or an empty string if the attribute
|
|
|
|
* doesn't exist.
|
|
|
|
*/
|
|
|
|
function getAttr(aAttr, aLine) {
|
2006-06-28 20:09:26 +04:00
|
|
|
// Used to determine whether an "input" line from a Sherlock file is a
|
|
|
|
// "user defined" input.
|
|
|
|
const userInput = /(\s|["'=])user(\s|[>="'\/\\+]|$)/i;
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
LOG("_parseAsSherlock::getAttr: Getting attr: \"" +
|
|
|
|
aAttr + "\" for line: \"" + aLine + "\"");
|
|
|
|
// We're not case sensitive, but we want to return the attribute value
|
2006-04-28 06:18:58 +04:00
|
|
|
// in its original case, so create a copy of the source
|
2006-03-17 10:16:00 +03:00
|
|
|
var lLine = aLine.toLowerCase();
|
|
|
|
var attr = aAttr.toLowerCase();
|
|
|
|
|
|
|
|
var attrStart = lLine.search(new RegExp("\\s" + attr, "i"));
|
|
|
|
if (attrStart == -1) {
|
2006-07-21 03:00:39 +04:00
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
// If this is the "user defined input" (i.e. contains the empty
|
|
|
|
// "user" attribute), return our special keyword
|
2006-06-28 20:09:26 +04:00
|
|
|
if (userInput.test(lLine) && attr == "value") {
|
2006-03-17 10:16:00 +03:00
|
|
|
LOG("_parseAsSherlock::getAttr: Found user input!\nLine:\"" + lLine
|
|
|
|
+ "\"");
|
2006-06-28 20:09:26 +04:00
|
|
|
return USER_DEFINED;
|
2006-03-17 10:16:00 +03:00
|
|
|
}
|
|
|
|
// The attribute doesn't exist - ignore
|
|
|
|
LOG("_parseAsSherlock::getAttr: Failed to find attribute:\nLine:\""
|
|
|
|
+ lLine + "\"\nAttr:\"" + attr + "\"");
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
var valueStart = lLine.indexOf("=", attrStart) + "=".length;
|
|
|
|
if (valueStart == -1)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
var quoteStart = lLine.indexOf("\"", valueStart);
|
|
|
|
if (quoteStart == -1) {
|
|
|
|
|
|
|
|
// Unquoted attribute, get the rest of the line, trimmed at the first
|
|
|
|
// sign of whitespace. If the rest of the line is only whitespace,
|
|
|
|
// returns a blank string.
|
|
|
|
return lLine.substr(valueStart).replace(/\s.*$/, "");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// Make sure that there's only whitespace between the start of the
|
|
|
|
// value and the first quote. If there is, end the attribute value at
|
|
|
|
// the first sign of whitespace. This prevents us from falling into
|
|
|
|
// the next attribute if this is an unquoted attribute followed by a
|
|
|
|
// quoted attribute.
|
|
|
|
var betweenEqualAndQuote = lLine.substring(valueStart, quoteStart);
|
|
|
|
if (/\S/.test(betweenEqualAndQuote))
|
|
|
|
return lLine.substr(valueStart).replace(/\s.*$/, "");
|
|
|
|
|
|
|
|
// Adjust the start index to account for the opening quote
|
|
|
|
valueStart = quoteStart + "\"".length;
|
|
|
|
// Find the closing quote
|
|
|
|
valueEnd = lLine.indexOf("\"", valueStart);
|
|
|
|
// If there is no closing quote, just go to the end of the line
|
|
|
|
if (valueEnd == -1)
|
|
|
|
valueEnd = aLine.length;
|
|
|
|
}
|
|
|
|
return aLine.substring(valueStart, valueEnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
var inputs = [];
|
|
|
|
|
|
|
|
LOG("_parseAsSherlock::getInputs: Lines:\n" + aLines);
|
|
|
|
// Filter out everything but non-inputs
|
|
|
|
lines = aLines.filter(function (line) {
|
|
|
|
return /^\s*<input/i.test(line);
|
|
|
|
});
|
|
|
|
LOG("_parseAsSherlock::getInputs: Filtered lines:\n" + lines);
|
|
|
|
|
|
|
|
lines.forEach(function (line) {
|
|
|
|
// Strip leading/trailing whitespace and remove the surrounding markup
|
|
|
|
// ("<input" and ">")
|
2008-09-09 08:48:07 +04:00
|
|
|
line = line.trim().replace(/^<input/i, "").replace(/>$/, "");
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
// If this is one of the "directional" inputs (<inputnext>/<inputprev>)
|
|
|
|
const directionalInput = /^(prev|next)/i;
|
|
|
|
if (directionalInput.test(line)) {
|
|
|
|
|
|
|
|
// Make it look like a normal input by removing "prev" or "next"
|
|
|
|
line = line.replace(directionalInput, "");
|
|
|
|
|
|
|
|
// If it has a name, give it a dummy value to match previous
|
|
|
|
// nsInternetSearchService behavior
|
|
|
|
if (/name\s*=/i.test(line)) {
|
|
|
|
line += " value=\"0\"";
|
|
|
|
} else
|
|
|
|
return; // Line has no name, skip it
|
|
|
|
}
|
|
|
|
|
|
|
|
var attrName = getAttr("name", line);
|
|
|
|
var attrValue = getAttr("value", line);
|
|
|
|
LOG("_parseAsSherlock::getInputs: Got input:\nName:\"" + attrName +
|
|
|
|
"\"\nValue:\"" + attrValue + "\"");
|
|
|
|
if (attrValue)
|
|
|
|
inputs.push([attrName, attrValue]);
|
|
|
|
});
|
|
|
|
return inputs;
|
|
|
|
}
|
|
|
|
|
|
|
|
function err(aErr) {
|
2008-10-18 06:24:47 +04:00
|
|
|
FAIL("_parseAsSherlock::err: Sherlock param error:\n" + aErr,
|
|
|
|
Cr.NS_ERROR_FAILURE);
|
2006-03-17 10:16:00 +03:00
|
|
|
}
|
|
|
|
|
2006-05-29 22:48:42 +04:00
|
|
|
// First try converting our byte array using the default Sherlock encoding.
|
|
|
|
// If this fails, or if we find a sourceTextEncoding attribute, we need to
|
|
|
|
// reconvert the byte array using the specified encoding.
|
2006-07-21 03:00:39 +04:00
|
|
|
var sherlockLines, searchSection, sourceTextEncoding, browserSection;
|
2006-05-29 22:48:42 +04:00
|
|
|
try {
|
|
|
|
sherlockLines = sherlockBytesToLines(this._data);
|
|
|
|
searchSection = getSection(sherlockLines, "search");
|
2006-07-21 03:00:39 +04:00
|
|
|
browserSection = getSection(sherlockLines, "browser");
|
2006-05-29 22:48:42 +04:00
|
|
|
sourceTextEncoding = parseInt(searchSection["sourcetextencoding"]);
|
|
|
|
if (sourceTextEncoding) {
|
|
|
|
// Re-convert the bytes using the found sourceTextEncoding
|
|
|
|
sherlockLines = sherlockBytesToLines(this._data, sourceTextEncoding);
|
|
|
|
searchSection = getSection(sherlockLines, "search");
|
2006-07-21 03:00:39 +04:00
|
|
|
browserSection = getSection(sherlockLines, "browser");
|
2006-05-29 22:48:42 +04:00
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
// The conversion using the default charset failed. Remove any non-ascii
|
|
|
|
// bytes and try to find a sourceTextEncoding.
|
|
|
|
var asciiBytes = this._data.filter(function (n) {return !(0x80 & n);});
|
|
|
|
var asciiString = String.fromCharCode.apply(null, asciiBytes);
|
|
|
|
sherlockLines = asciiString.split(NEW_LINES).filter(isUsefulLine);
|
|
|
|
searchSection = getSection(sherlockLines, "search");
|
|
|
|
sourceTextEncoding = parseInt(searchSection["sourcetextencoding"]);
|
|
|
|
if (sourceTextEncoding) {
|
|
|
|
sherlockLines = sherlockBytesToLines(this._data, sourceTextEncoding);
|
|
|
|
searchSection = getSection(sherlockLines, "search");
|
2006-07-21 03:00:39 +04:00
|
|
|
browserSection = getSection(sherlockLines, "browser");
|
2006-05-29 22:48:42 +04:00
|
|
|
} else
|
|
|
|
ERROR("Couldn't find a working charset", Cr.NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
LOG("_parseAsSherlock: Search section:\n" + searchSection.toSource());
|
|
|
|
|
|
|
|
this._name = searchSection["name"] || err("Missing name!");
|
|
|
|
this._description = searchSection["description"] || "";
|
|
|
|
this._queryCharset = searchSection["querycharset"] ||
|
2006-05-17 05:37:49 +04:00
|
|
|
queryCharsetFromCode(searchSection["queryencoding"]);
|
2006-09-15 06:37:45 +04:00
|
|
|
this._searchForm = searchSection["searchform"];
|
2006-03-17 10:16:00 +03:00
|
|
|
|
2006-07-21 05:30:48 +04:00
|
|
|
this._updateInterval = parseInt(browserSection["updatecheckdays"]);
|
2006-07-21 03:00:39 +04:00
|
|
|
|
|
|
|
this._updateURL = browserSection["update"];
|
|
|
|
this._iconUpdateURL = browserSection["updateicon"];
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
var method = (searchSection["method"] || "GET").toUpperCase();
|
|
|
|
var template = searchSection["action"] || err("Missing action!");
|
|
|
|
|
2006-05-29 22:48:42 +04:00
|
|
|
var inputs = getInputs(sherlockLines);
|
2006-03-17 10:16:00 +03:00
|
|
|
LOG("_parseAsSherlock: Inputs:\n" + inputs.toSource());
|
|
|
|
|
|
|
|
var url = null;
|
|
|
|
|
|
|
|
if (method == "GET") {
|
|
|
|
// Here's how we construct the input string:
|
|
|
|
// <input> is first: Name Attr: Prefix Data Example:
|
|
|
|
// YES EMPTY None <value> TEMPLATE<value>
|
|
|
|
// YES NON-EMPTY ? <name>=<value> TEMPLATE?<name>=<value>
|
|
|
|
// NO EMPTY ------------- <ignored> --------------
|
|
|
|
// NO NON-EMPTY & <name>=<value> TEMPLATE?<n1>=<v1>&<n2>=<v2>
|
2006-04-28 06:18:58 +04:00
|
|
|
for (var i = 0; i < inputs.length; i++) {
|
2006-03-17 10:16:00 +03:00
|
|
|
var name = inputs[i][0];
|
|
|
|
var value = inputs[i][1];
|
|
|
|
if (i==0) {
|
|
|
|
if (name == "")
|
2006-06-28 20:09:26 +04:00
|
|
|
template += USER_DEFINED;
|
2006-03-17 10:16:00 +03:00
|
|
|
else
|
|
|
|
template += "?" + name + "=" + value;
|
|
|
|
} else if (name != "")
|
|
|
|
template += "&" + name + "=" + value;
|
|
|
|
}
|
|
|
|
url = new EngineURL("text/html", method, template);
|
|
|
|
|
|
|
|
} else if (method == "POST") {
|
|
|
|
// Create the URL object and just add the parameters directly
|
|
|
|
url = new EngineURL("text/html", method, template);
|
2006-04-28 06:18:58 +04:00
|
|
|
for (var i = 0; i < inputs.length; i++) {
|
2006-03-17 10:16:00 +03:00
|
|
|
var name = inputs[i][0];
|
|
|
|
var value = inputs[i][1];
|
|
|
|
if (name)
|
|
|
|
url.addParam(name, value);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
err("Invalid method!");
|
|
|
|
|
|
|
|
this._urls.push(url);
|
|
|
|
},
|
|
|
|
|
2008-10-07 00:57:18 +04:00
|
|
|
/**
|
|
|
|
* Init from a JSON record.
|
|
|
|
**/
|
|
|
|
_initWithJSON: function SRCH_ENG__initWithJSON(aJson) {
|
2008-10-18 06:24:47 +04:00
|
|
|
this.__id = aJson._id;
|
2008-10-07 00:57:18 +04:00
|
|
|
this._name = aJson._name;
|
|
|
|
this._description = aJson.description;
|
|
|
|
if (aJson._hasPreferredIcon == undefined)
|
|
|
|
this._hasPreferredIcon = true;
|
|
|
|
else
|
|
|
|
this._hasPreferredIcon = false;
|
|
|
|
this._hidden = aJson.hidden || null;
|
|
|
|
this._type = aJson.type || SEARCH_TYPE_MOZSEARCH;
|
|
|
|
this._queryCharset = aJson.queryCharset || DEFAULT_QUERY_CHARSET;
|
|
|
|
this._searchForm = aJson.searchForm;
|
|
|
|
this.__installLocation = aJson._installLocation || SEARCH_APP_DIR;
|
|
|
|
this._updateInterval = aJson._updateInterval || null;
|
|
|
|
this._updateURL = aJson._updateURL || null;
|
|
|
|
this._iconUpdateURL = aJson._iconUpdateURL || null;
|
|
|
|
if (aJson._readOnly == undefined)
|
|
|
|
this._readOnly = true;
|
|
|
|
else
|
|
|
|
this._readOnly = false;
|
|
|
|
this._iconURI = makeURI(aJson._iconURL);
|
|
|
|
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);
|
2008-10-18 06:24:47 +04:00
|
|
|
engineURL._initWithJSON(url, this);
|
2008-10-07 00:57:18 +04:00
|
|
|
this._urls.push(engineURL);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a JavaScript object that represents this engine.
|
|
|
|
* @param aFilter
|
|
|
|
* Whether or not to filter out common default values. Recommended for
|
|
|
|
* use with _initWithJSON().
|
|
|
|
* @returns An object suitable for serialization as JSON.
|
|
|
|
**/
|
|
|
|
_serializeToJSON: function SRCH_ENG__serializeToJSON(aFilter) {
|
|
|
|
var json = {
|
2008-10-18 06:24:47 +04:00
|
|
|
_id: this._id,
|
2008-10-07 00:57:18 +04:00
|
|
|
_name: this._name,
|
|
|
|
description: this.description,
|
|
|
|
filePath: this._file.QueryInterface(Ci.nsILocalFile).persistentDescriptor,
|
|
|
|
searchForm: this.searchForm,
|
|
|
|
_iconURL: this._iconURL,
|
|
|
|
_urls: [url._serializeToJSON() for each(url in this._urls)]
|
|
|
|
};
|
|
|
|
|
|
|
|
if (this._installLocation != SEARCH_APP_DIR || !aFilter)
|
|
|
|
json._installLocation = this._installLocation;
|
|
|
|
if (this._updateInterval || !aFilter)
|
|
|
|
json.updateInterval = this._updateInterval;
|
|
|
|
if (this._updateURL || !aFilter)
|
|
|
|
json._updateURL = this._updateURL;
|
|
|
|
if (this._iconUpdateURL || !aFilter)
|
|
|
|
json._iconUpdateURL = this._iconUpdateURL;
|
|
|
|
if (!this._hasPreferredIcon || !aFilter)
|
|
|
|
json._hasPreferredIcon = this._hasPreferredIcon;
|
|
|
|
if (this.hidden || !aFilter)
|
|
|
|
json.hidden = this.hidden;
|
|
|
|
if (this.type != SEARCH_TYPE_MOZSEARCH || !aFilter)
|
|
|
|
json.type = this.type;
|
|
|
|
if (this.queryCharset != DEFAULT_QUERY_CHARSET || !aFilter)
|
|
|
|
json.queryCharset = this.queryCharset;
|
|
|
|
if (this._dataType != SEARCH_DATA_XML || !aFilter)
|
|
|
|
json._dataType = this._dataType;
|
|
|
|
if (!this._readOnly || !aFilter)
|
|
|
|
json._readOnly = this._readOnly;
|
|
|
|
|
|
|
|
return json;
|
|
|
|
},
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
/**
|
|
|
|
* Returns an XML document object containing the search plugin information,
|
|
|
|
* which can later be used to reload the engine.
|
|
|
|
*/
|
|
|
|
_serializeToElement: function SRCH_ENG_serializeToEl() {
|
2006-07-21 03:00:39 +04:00
|
|
|
function appendTextNode(aNameSpace, aLocalName, aValue) {
|
|
|
|
if (!aValue)
|
|
|
|
return null;
|
|
|
|
var node = doc.createElementNS(aNameSpace, aLocalName);
|
|
|
|
node.appendChild(doc.createTextNode(aValue));
|
|
|
|
docElem.appendChild(node);
|
|
|
|
docElem.appendChild(doc.createTextNode("\n"));
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
var parser = Cc["@mozilla.org/xmlextras/domparser;1"].
|
|
|
|
createInstance(Ci.nsIDOMParser);
|
|
|
|
|
|
|
|
var doc = parser.parseFromString(EMPTY_DOC, "text/xml");
|
2008-09-10 18:26:54 +04:00
|
|
|
var docElem = doc.documentElement;
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
docElem.appendChild(doc.createTextNode("\n"));
|
|
|
|
|
2006-07-21 03:00:39 +04:00
|
|
|
appendTextNode(OPENSEARCH_NS_11, "ShortName", this.name);
|
|
|
|
appendTextNode(OPENSEARCH_NS_11, "Description", this._description);
|
|
|
|
appendTextNode(OPENSEARCH_NS_11, "InputEncoding", this._queryCharset);
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
if (this._iconURI) {
|
2006-07-21 03:00:39 +04:00
|
|
|
var imageNode = appendTextNode(OPENSEARCH_NS_11, "Image",
|
|
|
|
this._iconURI.spec);
|
|
|
|
if (imageNode) {
|
|
|
|
imageNode.setAttribute("width", "16");
|
|
|
|
imageNode.setAttribute("height", "16");
|
|
|
|
}
|
2006-03-17 10:16:00 +03:00
|
|
|
}
|
|
|
|
|
2006-07-21 03:00:39 +04:00
|
|
|
appendTextNode(MOZSEARCH_NS_10, "UpdateInterval", this._updateInterval);
|
|
|
|
appendTextNode(MOZSEARCH_NS_10, "UpdateUrl", this._updateURL);
|
2006-09-01 22:50:31 +04:00
|
|
|
appendTextNode(MOZSEARCH_NS_10, "IconUpdateUrl", this._iconUpdateURL);
|
|
|
|
appendTextNode(MOZSEARCH_NS_10, "SearchForm", this._searchForm);
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
for (var i = 0; i < this._urls.length; ++i)
|
|
|
|
this._urls[i]._serializeToElement(doc, docElem);
|
|
|
|
docElem.appendChild(doc.createTextNode("\n"));
|
|
|
|
|
|
|
|
return doc;
|
|
|
|
},
|
|
|
|
|
2006-12-11 23:54:35 +03:00
|
|
|
_lazySerializeToFile: function SRCH_ENG_serializeToFile() {
|
|
|
|
if (this._serializeTimer) {
|
|
|
|
// Reset the timer
|
|
|
|
this._serializeTimer.delay = LAZY_SERIALIZE_DELAY;
|
|
|
|
} else {
|
|
|
|
this._serializeTimer = Cc["@mozilla.org/timer;1"].
|
|
|
|
createInstance(Ci.nsITimer);
|
|
|
|
var timerCallback = {
|
|
|
|
self: this,
|
|
|
|
notify: function SRCH_ENG_notify(aTimer) {
|
|
|
|
try {
|
|
|
|
this.self._serializeToFile();
|
|
|
|
} catch (ex) {
|
|
|
|
LOG("Serialization from timer callback failed:\n" + ex);
|
|
|
|
}
|
|
|
|
this.self._serializeTimer = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this._serializeTimer.initWithCallback(timerCallback,
|
|
|
|
LAZY_SERIALIZE_DELAY,
|
|
|
|
Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
/**
|
|
|
|
* Serializes the engine object to file.
|
|
|
|
*/
|
|
|
|
_serializeToFile: function SRCH_ENG_serializeToFile() {
|
|
|
|
var file = this._file;
|
|
|
|
ENSURE_WARN(!this._readOnly, "Can't serialize a read only engine!",
|
|
|
|
Cr.NS_ERROR_FAILURE);
|
2006-07-21 03:00:39 +04:00
|
|
|
ENSURE_WARN(file && file.exists(), "Can't serialize: file doesn't exist!",
|
2006-03-17 10:16:00 +03:00
|
|
|
Cr.NS_ERROR_UNEXPECTED);
|
|
|
|
|
|
|
|
var fos = Cc["@mozilla.org/network/safe-file-output-stream;1"].
|
|
|
|
createInstance(Ci.nsIFileOutputStream);
|
|
|
|
|
|
|
|
// Serialize the engine first - we don't want to overwrite a good file
|
|
|
|
// if this somehow fails.
|
2008-09-10 18:26:54 +04:00
|
|
|
var doc = this._serializeToElement();
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
fos.init(file, (MODE_WRONLY | MODE_TRUNCATE), PERMS_FILE, 0);
|
|
|
|
|
|
|
|
try {
|
|
|
|
var serializer = Cc["@mozilla.org/xmlextras/xmlserializer;1"].
|
|
|
|
createInstance(Ci.nsIDOMSerializer);
|
|
|
|
serializer.serializeToStream(doc.documentElement, fos, null);
|
|
|
|
} catch (e) {
|
|
|
|
LOG("_serializeToFile: Error serializing engine:\n" + e);
|
|
|
|
}
|
|
|
|
|
|
|
|
closeSafeOutputStream(fos);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the engine's file from disk. The search service calls this once it
|
2006-04-28 06:18:58 +04:00
|
|
|
* removes the engine from its internal store. This function will throw if
|
2006-03-17 10:16:00 +03:00
|
|
|
* the file cannot be removed.
|
|
|
|
*/
|
|
|
|
_remove: function SRCH_ENG_remove() {
|
2008-10-18 06:24:47 +04:00
|
|
|
if (this._readOnly)
|
|
|
|
FAIL("Can't remove read only engine!", Cr.NS_ERROR_FAILURE);
|
|
|
|
if (!this._file || !this._file.exists())
|
|
|
|
FAIL("Can't remove engine: file doesn't exist!", Cr.NS_ERROR_FILE_NOT_FOUND);
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
this._file.remove(false);
|
|
|
|
},
|
|
|
|
|
|
|
|
// nsISearchEngine
|
|
|
|
get alias() {
|
|
|
|
if (this._alias === null)
|
2006-06-17 04:58:09 +04:00
|
|
|
this._alias = engineMetadataService.getAttr(this, "alias");
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
return this._alias;
|
|
|
|
},
|
|
|
|
set alias(val) {
|
|
|
|
this._alias = val;
|
2006-06-17 04:58:09 +04:00
|
|
|
engineMetadataService.setAttr(this, "alias", val);
|
2006-03-17 10:16:00 +03:00
|
|
|
notifyAction(this, SEARCH_ENGINE_CHANGED);
|
|
|
|
},
|
|
|
|
|
|
|
|
get description() {
|
|
|
|
return this._description;
|
|
|
|
},
|
|
|
|
|
|
|
|
get hidden() {
|
2006-06-17 04:58:09 +04:00
|
|
|
if (this._hidden === null)
|
|
|
|
this._hidden = engineMetadataService.getAttr(this, "hidden");
|
2006-03-17 10:16:00 +03:00
|
|
|
return this._hidden;
|
|
|
|
},
|
|
|
|
set hidden(val) {
|
|
|
|
var value = !!val;
|
|
|
|
if (value != this._hidden) {
|
|
|
|
this._hidden = value;
|
2006-06-17 04:58:09 +04:00
|
|
|
engineMetadataService.setAttr(this, "hidden", value);
|
2006-03-17 10:16:00 +03:00
|
|
|
notifyAction(this, SEARCH_ENGINE_CHANGED);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
get iconURI() {
|
|
|
|
return this._iconURI;
|
|
|
|
},
|
|
|
|
|
2006-03-17 13:39:11 +03:00
|
|
|
get _iconURL() {
|
|
|
|
if (!this._iconURI)
|
|
|
|
return "";
|
|
|
|
return this._iconURI.spec;
|
|
|
|
},
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
// 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.
|
|
|
|
get _location() {
|
|
|
|
if (this._file)
|
|
|
|
return this._file.path;
|
|
|
|
|
|
|
|
if (this._uri)
|
|
|
|
return this._uri.spec;
|
|
|
|
|
|
|
|
return "";
|
|
|
|
},
|
|
|
|
|
2006-06-17 04:58:09 +04:00
|
|
|
// The file that the plugin is loaded from is a unique identifier for it. We
|
|
|
|
// use this as the identifier to store data in the sqlite database
|
2008-10-18 06:24:47 +04:00
|
|
|
__id: null,
|
2006-06-17 04:58:09 +04:00
|
|
|
get _id() {
|
2008-10-18 06:24:47 +04:00
|
|
|
if (!this.__id) {
|
|
|
|
ENSURE_WARN(this._file, "No _file for id!", Cr.NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
if (this._isInProfile)
|
|
|
|
return this.__id = "[profile]/" + this._file.leafName;
|
|
|
|
if (this._isInAppDir)
|
|
|
|
return this.__id = "[app]/" + this._file.leafName;
|
|
|
|
|
|
|
|
// We're not in the profile or appdir, so this must be an extension-shipped
|
|
|
|
// plugin. Use the full path.
|
|
|
|
return this.__id = this._file.path;
|
|
|
|
}
|
|
|
|
return this.__id;
|
2006-06-17 04:58:09 +04:00
|
|
|
},
|
|
|
|
|
2007-05-04 19:48:08 +04:00
|
|
|
get _installLocation() {
|
2006-07-05 22:06:47 +04:00
|
|
|
ENSURE_WARN(this._file && this._file.exists(),
|
2007-05-04 19:48:08 +04:00
|
|
|
"_installLocation: engine has no file!",
|
2006-07-05 22:06:47 +04:00
|
|
|
Cr.NS_ERROR_FAILURE);
|
2007-05-04 19:48:08 +04:00
|
|
|
|
|
|
|
if (this.__installLocation === null) {
|
|
|
|
if (this._file.parent.equals(getDir(NS_APP_SEARCH_DIR)))
|
|
|
|
this.__installLocation = SEARCH_APP_DIR;
|
|
|
|
else if (this._file.parent.equals(getDir(NS_APP_USER_SEARCH_DIR)))
|
|
|
|
this.__installLocation = SEARCH_PROFILE_DIR;
|
|
|
|
else
|
|
|
|
this.__installLocation = SEARCH_IN_EXTENSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.__installLocation;
|
|
|
|
},
|
|
|
|
|
|
|
|
get _isInAppDir() {
|
|
|
|
return this._installLocation == SEARCH_APP_DIR;
|
|
|
|
},
|
|
|
|
get _isInProfile() {
|
|
|
|
return this._installLocation == SEARCH_PROFILE_DIR;
|
|
|
|
},
|
|
|
|
|
|
|
|
get _isDefault() {
|
|
|
|
// For now, our concept of a "default engine" is "one that is not in the
|
|
|
|
// user's profile directory", which is currently equivalent to "is app- or
|
|
|
|
// extension-shipped".
|
|
|
|
return !this._isInProfile;
|
2006-07-05 22:06:47 +04:00
|
|
|
},
|
|
|
|
|
2006-07-21 05:30:48 +04:00
|
|
|
get _hasUpdates() {
|
|
|
|
// Whether or not the engine has an update URL
|
|
|
|
return !!(this._updateURL || this._iconUpdateURL);
|
|
|
|
},
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
get name() {
|
|
|
|
return this._name;
|
|
|
|
},
|
|
|
|
|
|
|
|
get type() {
|
|
|
|
return this._type;
|
|
|
|
},
|
|
|
|
|
|
|
|
get searchForm() {
|
2006-05-31 05:10:05 +04:00
|
|
|
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).
|
2006-06-16 23:31:40 +04:00
|
|
|
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;
|
2006-05-31 05:10:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return this._searchForm;
|
2006-03-17 10:16:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
get queryCharset() {
|
|
|
|
if (this._queryCharset)
|
|
|
|
return this._queryCharset;
|
2006-05-17 05:37:49 +04:00
|
|
|
return this._queryCharset = queryCharsetFromCode(/* get the default */);
|
2006-03-17 10:16:00 +03:00
|
|
|
},
|
|
|
|
|
2006-06-16 23:31:40 +04:00
|
|
|
// from nsISearchEngine
|
|
|
|
addParam: function SRCH_ENG_addParam(aName, aValue, aResponseType) {
|
2008-10-18 06:34:33 +04:00
|
|
|
if (!aName || (aValue == null))
|
2008-10-18 06:24:47 +04:00
|
|
|
FAIL("missing name or value for nsISearchEngine::addParam!");
|
2006-03-17 10:16:00 +03:00
|
|
|
ENSURE_WARN(!this._readOnly,
|
|
|
|
"called nsISearchEngine::addParam on a read-only engine!",
|
|
|
|
Cr.NS_ERROR_FAILURE);
|
2006-06-16 23:31:40 +04:00
|
|
|
if (!aResponseType)
|
|
|
|
aResponseType = URLTYPE_SEARCH_HTML;
|
|
|
|
|
|
|
|
var url = this._getURLOfType(aResponseType);
|
2008-10-18 06:24:47 +04:00
|
|
|
if (!url)
|
|
|
|
FAIL("Engine object has no URL for response type " + aResponseType,
|
2006-06-16 23:31:40 +04:00
|
|
|
Cr.NS_ERROR_FAILURE);
|
2006-03-17 10:16:00 +03:00
|
|
|
|
2006-06-16 23:31:40 +04:00
|
|
|
url.addParam(aName, aValue);
|
2006-12-11 23:54:35 +03:00
|
|
|
|
|
|
|
// Serialize the changes to file lazily
|
|
|
|
this._lazySerializeToFile();
|
2006-03-17 10:16:00 +03:00
|
|
|
},
|
|
|
|
|
2006-06-16 23:31:40 +04:00
|
|
|
// from nsISearchEngine
|
|
|
|
getSubmission: function SRCH_ENG_getSubmission(aData, aResponseType) {
|
|
|
|
if (!aResponseType)
|
|
|
|
aResponseType = URLTYPE_SEARCH_HTML;
|
|
|
|
|
|
|
|
var url = this._getURLOfType(aResponseType);
|
|
|
|
|
|
|
|
if (!url)
|
|
|
|
return null;
|
2006-03-17 10:16:00 +03:00
|
|
|
|
2006-05-31 05:10:05 +04:00
|
|
|
if (!aData) {
|
|
|
|
// Return a dummy submission object with our searchForm attribute
|
|
|
|
return new Submission(makeURI(this.searchForm), null);
|
|
|
|
}
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
LOG("getSubmission: In data: \"" + aData + "\"");
|
|
|
|
var textToSubURI = Cc["@mozilla.org/intl/texttosuburi;1"].
|
|
|
|
getService(Ci.nsITextToSubURI);
|
|
|
|
var data = "";
|
|
|
|
try {
|
|
|
|
data = textToSubURI.ConvertAndEscape(this.queryCharset, aData);
|
|
|
|
} catch (ex) {
|
|
|
|
LOG("getSubmission: Falling back to default queryCharset!");
|
|
|
|
data = textToSubURI.ConvertAndEscape(DEFAULT_QUERY_CHARSET, aData);
|
|
|
|
}
|
|
|
|
LOG("getSubmission: Out data: \"" + data + "\"");
|
2006-07-05 22:06:47 +04:00
|
|
|
return url.getSubmission(data, this);
|
2006-06-16 23:31:40 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
// from nsISearchEngine
|
|
|
|
supportsResponseType: function SRCH_ENG_supportsResponseType(type) {
|
|
|
|
return (this._getURLOfType(type) != null);
|
2006-03-17 10:16:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
// nsISupports
|
2007-11-24 05:45:42 +03:00
|
|
|
QueryInterface: function SRCH_ENG_QI(aIID) {
|
|
|
|
if (aIID.equals(Ci.nsISearchEngine) ||
|
|
|
|
aIID.equals(Ci.nsISupports))
|
|
|
|
return this;
|
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
|
|
},
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
get wrappedJSObject() {
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// nsISearchSubmission
|
|
|
|
function Submission(aURI, aPostData) {
|
|
|
|
this._uri = aURI;
|
|
|
|
this._postData = aPostData;
|
|
|
|
}
|
|
|
|
Submission.prototype = {
|
|
|
|
get uri() {
|
|
|
|
return this._uri;
|
|
|
|
},
|
|
|
|
get postData() {
|
|
|
|
return this._postData;
|
|
|
|
},
|
2007-11-24 05:45:42 +03:00
|
|
|
QueryInterface: function SRCH_SUBM_QI(aIID) {
|
|
|
|
if (aIID.equals(Ci.nsISearchSubmission) ||
|
|
|
|
aIID.equals(Ci.nsISupports))
|
|
|
|
return this;
|
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
2006-03-17 10:16:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// nsIBrowserSearchService
|
|
|
|
function SearchService() {
|
2006-03-19 06:48:48 +03:00
|
|
|
this._init();
|
2006-03-17 10:16:00 +03:00
|
|
|
}
|
|
|
|
SearchService.prototype = {
|
|
|
|
_engines: { },
|
2006-07-21 03:00:39 +04:00
|
|
|
_sortedEngines: null,
|
2006-07-28 01:54:53 +04:00
|
|
|
// Whether or not we need to write the order of engines on shutdown. This
|
|
|
|
// needs to happen anytime _sortedEngines is modified after initial startup.
|
|
|
|
_needToSetOrderPrefs: false,
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
_init: function() {
|
2008-10-07 00:57:18 +04:00
|
|
|
// Replace empty LOG function with the useful one if the log pref is set.
|
|
|
|
if (getBoolPref(BROWSER_SEARCH_PREF + "log", false))
|
2008-01-29 22:39:20 +03:00
|
|
|
LOG = DO_LOG;
|
|
|
|
|
2006-06-17 04:58:09 +04:00
|
|
|
engineMetadataService.init();
|
2006-07-21 03:00:39 +04:00
|
|
|
engineUpdateService.init();
|
|
|
|
|
2008-10-07 00:57:18 +04:00
|
|
|
this._loadEngines();
|
2006-03-17 10:16:00 +03:00
|
|
|
this._addObservers();
|
|
|
|
|
2006-04-28 06:18:58 +04:00
|
|
|
// Now that all engines are loaded, build the sorted engine list
|
|
|
|
this._buildSortedEngineList();
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
selectedEngineName = getLocalizedPref(BROWSER_SEARCH_PREF +
|
|
|
|
"selectedEngine");
|
|
|
|
this._currentEngine = this.getEngineByName(selectedEngineName) ||
|
|
|
|
this.defaultEngine;
|
|
|
|
},
|
|
|
|
|
2008-10-07 00:57:18 +04:00
|
|
|
_buildCache: function SRCH_SVC__buildCache() {
|
|
|
|
if (!getBoolPref(BROWSER_SEARCH_PREF + "cache.enabled", true))
|
|
|
|
return;
|
|
|
|
|
|
|
|
let cache = {};
|
|
|
|
let locale = getLocale();
|
|
|
|
let buildID = Cc["@mozilla.org/xre/app-info;1"].
|
|
|
|
getService(Ci.nsIXULAppInfo).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
|
|
|
|
// runtime (where we refresh for changes through the API) and 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;
|
|
|
|
|
|
|
|
for each (let engine in this._engines) {
|
|
|
|
let parent = engine._file.parent.path;
|
|
|
|
if (!cache[parent]) {
|
|
|
|
let cacheEntry = {};
|
|
|
|
cacheEntry.lastModifiedTime = parent.lastModifiedTime;
|
|
|
|
cacheEntry.engines = [];
|
|
|
|
cache[parent] = cacheEntry;
|
|
|
|
}
|
|
|
|
cache[parent].engines.push(engine._serializeToJSON(true));
|
|
|
|
}
|
|
|
|
|
|
|
|
let json = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
|
|
|
|
let stream = Cc["@mozilla.org/network/file-output-stream;1"].
|
|
|
|
createInstance(Ci.nsIFileOutputStream);
|
|
|
|
let converter = Cc["@mozilla.org/intl/converter-output-stream;1"].
|
|
|
|
createInstance(Ci.nsIConverterOutputStream);
|
|
|
|
let cacheFile = getDir(NS_APP_USER_PROFILE_50_DIR);
|
|
|
|
cacheFile.append("search.json");
|
|
|
|
|
|
|
|
try {
|
|
|
|
LOG("_buildCache: Writing to cache file.");
|
|
|
|
stream.init(cacheFile, (MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE), PERMS_FILE, 0);
|
|
|
|
converter.init(stream, "UTF-8", 0, 0x0000);
|
|
|
|
converter.writeString(json.encode(cache));
|
|
|
|
} catch (ex) {
|
|
|
|
LOG("_buildCache: Could not write to cache file: " + ex);
|
|
|
|
} finally {
|
|
|
|
converter.close();
|
|
|
|
stream.close();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_loadEngines: function SRCH_SVC__loadEngines() {
|
|
|
|
// See if we have a cache file so we don't have to parse a bunch of XML.
|
|
|
|
let cache = {};
|
|
|
|
let cacheEnabled = getBoolPref(BROWSER_SEARCH_PREF + "cache.enabled", true);
|
|
|
|
if (cacheEnabled) {
|
|
|
|
let cacheFile = getDir(NS_APP_USER_PROFILE_50_DIR);
|
|
|
|
cacheFile.append("search.json");
|
|
|
|
if (cacheFile.exists())
|
|
|
|
cache = this._readCacheFile(cacheFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
let locations = getDir(NS_APP_SEARCH_DIR_LIST, Ci.nsISimpleEnumerator);
|
|
|
|
let locale = getLocale();
|
|
|
|
let buildID = Cc["@mozilla.org/xre/app-info;1"].
|
|
|
|
getService(Ci.nsIXULAppInfo).platformBuildID;
|
|
|
|
|
|
|
|
// loop through our directories and check the cache object
|
|
|
|
let rebuildCache = false;
|
|
|
|
while (locations.hasMoreElements()) {
|
|
|
|
let dir = locations.getNext().QueryInterface(Ci.nsIFile);
|
|
|
|
let path = dir.path;
|
|
|
|
if (!cache[path] || cache[path].lastModifiedTime < dir.lastModifiedTime ||
|
|
|
|
cache.locale != locale || cache.buildID != buildID ||
|
|
|
|
cache.version != CACHE_VERSION) {
|
|
|
|
LOG("_loadEngines: Absent or outdated cache. Loading engines from disk.");
|
|
|
|
this._loadEnginesFromDir(dir);
|
|
|
|
rebuildCache = true;
|
|
|
|
} else {
|
|
|
|
this._loadEnginesFromCache(cache[path]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rebuildCache && cacheEnabled)
|
|
|
|
this._buildCache();
|
|
|
|
},
|
|
|
|
|
|
|
|
_readCacheFile: function SRCH_SVC__readCacheFile(aFile) {
|
|
|
|
let stream = Cc["@mozilla.org/network/file-input-stream;1"].
|
|
|
|
createInstance(Ci.nsIFileInputStream);
|
|
|
|
let json = Cc["@mozilla.org/dom/json;1"].createInstance(Ci.nsIJSON);
|
|
|
|
|
|
|
|
try {
|
|
|
|
stream.init(aFile, MODE_RDONLY, PERMS_FILE, 0);
|
|
|
|
return json.decodeFromStream(stream, stream.available());
|
|
|
|
} catch(ex) {
|
|
|
|
LOG("_readCacheFile: Error reading cache file: " + ex);
|
|
|
|
} finally {
|
|
|
|
stream.close();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
_batchTimer: null,
|
|
|
|
_batchCacheInvalidation: function SRCH_SVC__batchCacheInvalidation() {
|
|
|
|
let callback = {
|
|
|
|
self: this,
|
|
|
|
notify: function SRCH_SVC_batchTimerNotify(aTimer) {
|
|
|
|
LOG("_batchCacheInvalidation: Invalidating engine cache");
|
|
|
|
this.self._buildCache();
|
|
|
|
this.self._batchTimer = null;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!this._batchTimer) {
|
|
|
|
this._batchTimer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer);
|
|
|
|
this._batchTimer.initWithCallback(callback, CACHE_INVALIDATION_DELAY,
|
|
|
|
Ci.nsITimer.TYPE_ONE_SHOT);
|
|
|
|
} else {
|
|
|
|
this._batchTimer.delay = CACHE_INVALIDATION_DELAY;
|
|
|
|
LOG("_batchCacheInvalidation: Batch timer reset");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
_addEngineToStore: function SRCH_SVC_addEngineToStore(aEngine) {
|
|
|
|
LOG("_addEngineToStore: Adding engine: \"" + aEngine.name + "\"");
|
2006-07-21 03:00:39 +04:00
|
|
|
|
|
|
|
// 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) {
|
2006-03-17 10:16:00 +03:00
|
|
|
LOG("_addEngineToStore: Duplicate engine found, aborting!");
|
|
|
|
return;
|
|
|
|
}
|
2006-06-16 23:31:40 +04:00
|
|
|
|
2006-07-21 03:00:39 +04:00
|
|
|
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 still loading engines from disk, and will build the
|
|
|
|
// sorted engine list when that initial loading is done.
|
2006-07-28 01:54:53 +04:00
|
|
|
if (this._sortedEngines) {
|
2006-07-21 03:00:39 +04:00
|
|
|
this._sortedEngines.push(aEngine);
|
2006-07-28 01:54:53 +04:00
|
|
|
this._needToSetOrderPrefs = true;
|
|
|
|
}
|
2006-07-21 03:00:39 +04:00
|
|
|
notifyAction(aEngine, SEARCH_ENGINE_ADDED);
|
|
|
|
}
|
|
|
|
|
2006-07-21 05:30:48 +04:00
|
|
|
if (aEngine._hasUpdates) {
|
|
|
|
// Schedule the engine's next update, if it isn't already.
|
|
|
|
if (!engineMetadataService.getAttr(aEngine, "updateexpir"))
|
|
|
|
engineUpdateService.scheduleNextUpdate(aEngine);
|
|
|
|
|
|
|
|
// We need to save the engine's _dataType, if this is the first time the
|
|
|
|
// engine is added to the dataStore, since ._dataType isn't persisted
|
|
|
|
// and will change on the next startup (since the engine will then be
|
|
|
|
// XML). We need this so that we know how to load any future updates from
|
|
|
|
// this engine.
|
|
|
|
if (!engineMetadataService.getAttr(aEngine, "updatedatatype"))
|
|
|
|
engineMetadataService.setAttr(aEngine, "updatedatatype",
|
|
|
|
aEngine._dataType);
|
|
|
|
}
|
2006-03-17 10:16:00 +03:00
|
|
|
},
|
|
|
|
|
2008-10-07 00:57:18 +04:00
|
|
|
_loadEnginesFromCache: function SRCH_SVC__loadEnginesFromCache(aDir) {
|
|
|
|
let engines = aDir.engines;
|
|
|
|
LOG("_loadEnginesFromCache: Loading from cache. " + engines.length + " engines to load.");
|
|
|
|
for (let i = 0; i < engines.length; i++) {
|
|
|
|
let json = engines[i];
|
2008-10-18 06:24:47 +04:00
|
|
|
let engine = new Engine({cached: true, value: json.filePath}, json._dataType,
|
|
|
|
json._readOnly);
|
2008-10-07 00:57:18 +04:00
|
|
|
engine._initWithJSON(json);
|
|
|
|
this._addEngineToStore(engine);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_loadEnginesFromDir: function SRCH_SVC__loadEnginesFromDir(aDir) {
|
|
|
|
LOG("_loadEnginesFromDir: Searching in " + aDir.path + " for search engines.");
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
// Check whether aDir is the user profile dir
|
|
|
|
var isInProfile = aDir.equals(getDir(NS_APP_USER_SEARCH_DIR));
|
|
|
|
|
2006-04-07 03:16:44 +04:00
|
|
|
var files = aDir.directoryEntries
|
|
|
|
.QueryInterface(Ci.nsIDirectoryEnumerator);
|
2006-03-17 10:16:00 +03:00
|
|
|
|
2008-10-07 00:57:18 +04:00
|
|
|
var addedEngines = [];
|
2006-03-17 10:16:00 +03:00
|
|
|
while (files.hasMoreElements()) {
|
2006-04-07 03:16:44 +04:00
|
|
|
var file = files.nextFile;
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
// Ignore hidden and empty files, and directories
|
2006-05-23 18:18:53 +04:00
|
|
|
if (!file.isFile() || file.fileSize == 0 || file.isHidden())
|
2006-03-17 10:16:00 +03:00
|
|
|
continue;
|
|
|
|
|
2008-10-07 00:57:18 +04:00
|
|
|
var fileURL = gIoSvc.newFileURI(file).QueryInterface(Ci.nsIURL);
|
2006-03-17 10:16:00 +03:00
|
|
|
var fileExtension = fileURL.fileExtension.toLowerCase();
|
|
|
|
var isWritable = isInProfile && file.isWritable();
|
|
|
|
|
2006-04-14 03:28:21 +04:00
|
|
|
var dataType;
|
|
|
|
switch (fileExtension) {
|
|
|
|
case XML_FILE_EXT:
|
|
|
|
dataType = SEARCH_DATA_XML;
|
|
|
|
break;
|
|
|
|
case SHERLOCK_FILE_EXT:
|
|
|
|
dataType = SEARCH_DATA_TEXT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// Not an engine
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
var addedEngine = null;
|
2006-03-17 10:16:00 +03:00
|
|
|
try {
|
2006-04-14 03:28:21 +04:00
|
|
|
addedEngine = new Engine(file, dataType, !isWritable);
|
|
|
|
addedEngine._initFromFile();
|
|
|
|
} catch (ex) {
|
2008-10-07 00:57:18 +04:00
|
|
|
LOG("_loadEnginesFromDir: Failed to load " + file.path + "!\n" + ex);
|
2006-04-14 03:28:21 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-04-20 01:10:49 +04:00
|
|
|
if (fileExtension == SHERLOCK_FILE_EXT) {
|
|
|
|
if (isWritable) {
|
|
|
|
try {
|
|
|
|
this._convertSherlockFile(addedEngine, fileURL.fileBaseName);
|
|
|
|
} catch (ex) {
|
2008-10-07 00:57:18 +04:00
|
|
|
LOG("_loadEnginesFromDir: Failed to convert: " + fileURL.path + "\n" + ex);
|
2006-04-20 01:10:49 +04:00
|
|
|
// The engine couldn't be converted, mark it as read-only
|
|
|
|
addedEngine._readOnly = true;
|
|
|
|
}
|
2006-04-14 03:28:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the engine still doesn't have an icon, see if we can find one
|
|
|
|
if (!addedEngine._iconURI) {
|
|
|
|
var icon = this._findSherlockIcon(file, fileURL.fileBaseName);
|
|
|
|
if (icon)
|
2008-10-07 00:57:18 +04:00
|
|
|
addedEngine._iconURI = gIoSvc.newFileURI(icon);
|
2006-03-17 10:16:00 +03:00
|
|
|
}
|
|
|
|
}
|
2006-04-14 03:28:21 +04:00
|
|
|
|
|
|
|
this._addEngineToStore(addedEngine);
|
2008-10-07 00:57:18 +04:00
|
|
|
addedEngines.push(addedEngine);
|
2006-03-17 10:16:00 +03:00
|
|
|
}
|
2008-10-07 00:57:18 +04:00
|
|
|
return addedEngines;
|
2006-03-17 10:16:00 +03:00
|
|
|
},
|
|
|
|
|
2006-04-28 06:18:58 +04:00
|
|
|
_saveSortedEngineList: function SRCH_SVC_saveSortedEngineList() {
|
2006-07-28 01:54:53 +04:00
|
|
|
// We only need to write the prefs. if something has changed.
|
|
|
|
if (!this._needToSetOrderPrefs)
|
|
|
|
return;
|
2006-08-04 17:59:20 +04:00
|
|
|
|
|
|
|
// Set the useDB pref to indicate that from now on we should use the order
|
|
|
|
// information stored in the database.
|
2008-10-07 00:57:18 +04:00
|
|
|
gPrefSvc.setBoolPref(BROWSER_SEARCH_PREF + "useDBForOrder", true);
|
2006-08-04 17:59:20 +04:00
|
|
|
|
2006-07-19 17:35:09 +04:00
|
|
|
var engines = this._getSortedEngines(true);
|
2006-07-21 03:00:39 +04:00
|
|
|
var values = [];
|
2006-07-12 16:34:30 +04:00
|
|
|
var names = [];
|
2006-07-21 03:00:39 +04:00
|
|
|
|
2006-07-12 16:34:30 +04:00
|
|
|
for (var i = 0; i < engines.length; ++i) {
|
|
|
|
names[i] = "order";
|
|
|
|
values[i] = i + 1;
|
|
|
|
}
|
2006-07-21 03:00:39 +04:00
|
|
|
|
2006-07-12 16:34:30 +04:00
|
|
|
engineMetadataService.setAttrs(engines, names, values);
|
2006-04-28 06:18:58 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
_buildSortedEngineList: function SRCH_SVC_buildSortedEngineList() {
|
|
|
|
var addedEngines = { };
|
2006-06-24 17:48:19 +04:00
|
|
|
this._sortedEngines = [];
|
2006-06-17 04:58:09 +04:00
|
|
|
var engine;
|
|
|
|
|
2006-08-04 17:59:20 +04:00
|
|
|
// If the user has specified a custom engine order, read the order
|
|
|
|
// information from the engineMetadataService instead of the default
|
|
|
|
// prefs.
|
|
|
|
if (getBoolPref(BROWSER_SEARCH_PREF + "useDBForOrder", false)) {
|
2006-08-04 18:02:22 +04:00
|
|
|
for each (engine in this._engines) {
|
|
|
|
var orderNumber = engineMetadataService.getAttr(engine, "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. This problem will sort itself out when we call
|
|
|
|
// _saveSortedEngineList at shutdown.
|
|
|
|
if (orderNumber && !this._sortedEngines[orderNumber-1]) {
|
|
|
|
this._sortedEngines[orderNumber-1] = engine;
|
|
|
|
addedEngines[engine.name] = engine;
|
|
|
|
} else {
|
|
|
|
// We need to call _saveSortedEngines so this gets sorted out.
|
|
|
|
this._needToSetOrderPrefs = true;
|
|
|
|
}
|
2006-06-17 04:58:09 +04:00
|
|
|
}
|
2006-04-28 06:18:58 +04:00
|
|
|
|
2006-08-04 18:02:22 +04:00
|
|
|
// 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)
|
|
|
|
this._needToSetOrderPrefs = true;
|
|
|
|
this._sortedEngines = filteredEngines;
|
2006-07-06 03:42:12 +04:00
|
|
|
|
2006-08-04 17:59:20 +04:00
|
|
|
} else {
|
2006-08-04 18:02:22 +04:00
|
|
|
// The DB isn't being used, so just read the engine order from the prefs
|
2006-08-04 17:59:20 +04:00
|
|
|
var i = 0;
|
|
|
|
var engineName;
|
2006-08-07 19:22:55 +04:00
|
|
|
var prefName;
|
|
|
|
|
|
|
|
try {
|
|
|
|
var extras =
|
2008-10-07 00:57:18 +04:00
|
|
|
gPrefSvc.getChildList(BROWSER_SEARCH_PREF + "order.extra.", { });
|
2006-08-07 19:22:55 +04:00
|
|
|
|
|
|
|
for each (prefName in extras) {
|
2008-10-07 00:57:18 +04:00
|
|
|
engineName = gPrefSvc.getCharPref(prefName);
|
2006-08-07 19:22:55 +04:00
|
|
|
|
|
|
|
engine = this._engines[engineName];
|
|
|
|
if (!engine || engine.name in addedEngines)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
this._sortedEngines.push(engine);
|
|
|
|
addedEngines[engine.name] = engine;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (e) { }
|
|
|
|
|
2006-08-04 17:59:20 +04:00
|
|
|
while (true) {
|
|
|
|
engineName = getLocalizedPref(BROWSER_SEARCH_PREF + "order." + (++i));
|
|
|
|
if (!engineName)
|
|
|
|
break;
|
|
|
|
|
|
|
|
engine = this._engines[engineName];
|
2006-08-07 19:22:55 +04:00
|
|
|
if (!engine || engine.name in addedEngines)
|
2006-08-04 17:59:20 +04:00
|
|
|
continue;
|
|
|
|
|
|
|
|
this._sortedEngines.push(engine);
|
|
|
|
addedEngines[engine.name] = engine;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-28 06:18:58 +04:00
|
|
|
// Array for the remaining engines, alphabetically sorted
|
|
|
|
var alphaEngines = [];
|
|
|
|
|
2006-06-17 04:58:09 +04:00
|
|
|
for each (engine in this._engines) {
|
|
|
|
if (!(engine.name in addedEngines))
|
|
|
|
alphaEngines.push(this._engines[engine.name]);
|
2006-04-28 06:18:58 +04:00
|
|
|
}
|
|
|
|
alphaEngines = alphaEngines.sort(function (a, b) {
|
2006-08-31 03:19:18 +04:00
|
|
|
return a.name.localeCompare(b.name);
|
2006-04-28 06:18:58 +04:00
|
|
|
});
|
|
|
|
this._sortedEngines = this._sortedEngines.concat(alphaEngines);
|
|
|
|
},
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
/**
|
2006-04-28 06:18:58 +04:00
|
|
|
* Converts a Sherlock file and its icon into the custom XML format used by
|
2006-03-17 10:16:00 +03:00
|
|
|
* the Search Service. Saves the engine's icon (if present) into the XML as a
|
|
|
|
* data: URI and changes the extension of the source file from ".src" to
|
|
|
|
* ".xml". The engine data is then written to the file as XML.
|
|
|
|
* @param aEngine
|
|
|
|
* The Engine object that needs to be converted.
|
|
|
|
* @param aBaseName
|
|
|
|
* The basename of the Sherlock file.
|
|
|
|
* Example: "foo" for file "foo.src".
|
2006-04-20 01:16:45 +04:00
|
|
|
*
|
|
|
|
* @throws NS_ERROR_FAILURE if the file could not be converted.
|
|
|
|
*
|
2006-03-17 10:16:00 +03:00
|
|
|
* @see nsIURL::fileBaseName
|
|
|
|
*/
|
|
|
|
_convertSherlockFile: function SRCH_SVC_convertSherlock(aEngine, aBaseName) {
|
2006-04-07 03:16:44 +04:00
|
|
|
var oldSherlockFile = aEngine._file;
|
2006-04-07 03:15:47 +04:00
|
|
|
|
|
|
|
// Back up the old file
|
|
|
|
try {
|
2006-04-07 03:16:44 +04:00
|
|
|
var backupDir = oldSherlockFile.parent;
|
2006-04-07 03:15:47 +04:00
|
|
|
backupDir.append("searchplugins-backup");
|
2006-04-07 03:16:44 +04:00
|
|
|
|
2006-04-07 03:15:47 +04:00
|
|
|
if (!backupDir.exists())
|
|
|
|
backupDir.create(Ci.nsIFile.DIRECTORY_TYPE, PERMS_DIRECTORY);
|
|
|
|
|
2006-04-07 03:16:44 +04:00
|
|
|
oldSherlockFile.copyTo(backupDir, null);
|
2006-04-07 03:15:47 +04:00
|
|
|
} catch (ex) {
|
|
|
|
// Just bail. Engines that can't be backed up won't be converted, but
|
|
|
|
// engines that aren't converted are loaded as readonly.
|
2008-10-18 06:24:47 +04:00
|
|
|
FAIL("_convertSherlockFile: Couldn't back up " + oldSherlockFile.path +
|
|
|
|
":\n" + ex, Cr.NS_ERROR_FAILURE);
|
2006-04-07 03:15:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rename the file, but don't clobber existing files
|
2006-04-07 03:16:44 +04:00
|
|
|
var newXMLFile = oldSherlockFile.parent.clone();
|
|
|
|
newXMLFile.append(aBaseName + "." + XML_FILE_EXT);
|
|
|
|
|
|
|
|
if (newXMLFile.exists()) {
|
|
|
|
// There is an existing file with this name, create a unique file
|
|
|
|
newXMLFile.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, PERMS_FILE);
|
2006-04-07 03:15:47 +04:00
|
|
|
}
|
|
|
|
|
2006-04-07 03:16:44 +04:00
|
|
|
// Rename the .src file to .xml
|
|
|
|
oldSherlockFile.moveTo(null, newXMLFile.leafName);
|
|
|
|
|
|
|
|
aEngine._file = newXMLFile;
|
|
|
|
|
2006-04-07 03:15:47 +04:00
|
|
|
// Write the converted engine to disk
|
|
|
|
aEngine._serializeToFile();
|
2006-04-20 01:16:45 +04:00
|
|
|
|
2006-06-20 06:53:47 +04:00
|
|
|
// Update the engine's _type.
|
|
|
|
aEngine._type = SEARCH_TYPE_MOZSEARCH;
|
|
|
|
|
2006-04-20 01:16:45 +04:00
|
|
|
// See if it has a corresponding icon
|
|
|
|
try {
|
|
|
|
var icon = this._findSherlockIcon(aEngine._file, aBaseName);
|
|
|
|
if (icon && icon.fileSize < MAX_ICON_SIZE) {
|
|
|
|
// Use this as the engine's icon
|
|
|
|
var bStream = Cc["@mozilla.org/binaryinputstream;1"].
|
|
|
|
createInstance(Ci.nsIBinaryInputStream);
|
|
|
|
var fileInStream = Cc["@mozilla.org/network/file-input-stream;1"].
|
|
|
|
createInstance(Ci.nsIFileInputStream);
|
|
|
|
|
|
|
|
fileInStream.init(icon, MODE_RDONLY, PERMS_FILE, 0);
|
|
|
|
bStream.setInputStream(fileInStream);
|
|
|
|
|
|
|
|
var bytes = [];
|
|
|
|
while (bStream.available() != 0)
|
|
|
|
bytes = bytes.concat(bStream.readByteArray(bStream.available()));
|
|
|
|
bStream.close();
|
|
|
|
|
|
|
|
// Convert the byte array to a base64-encoded string
|
2008-01-29 22:35:31 +03:00
|
|
|
var str = btoa(String.fromCharCode.apply(null, bytes));
|
2006-04-20 01:16:45 +04:00
|
|
|
|
|
|
|
aEngine._iconURI = makeURI(ICON_DATAURL_PREFIX + str);
|
|
|
|
LOG("_importSherlockEngine: Set sherlock iconURI to: \"" +
|
|
|
|
aEngine._iconURL + "\"");
|
|
|
|
|
|
|
|
// Write the engine to disk to save changes
|
|
|
|
aEngine._serializeToFile();
|
|
|
|
|
|
|
|
// Delete the icon now that we're sure everything's been saved
|
|
|
|
icon.remove(false);
|
|
|
|
}
|
|
|
|
} catch (ex) { LOG("_convertSherlockFile: Error setting icon:\n" + ex); }
|
2006-03-17 10:16:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds an icon associated to a given Sherlock file. Searches the provided
|
|
|
|
* file's parent directory looking for files with the same base name and one
|
|
|
|
* of the file extensions in SHERLOCK_ICON_EXTENSIONS.
|
|
|
|
* @param aEngineFile
|
|
|
|
* The Sherlock plugin file.
|
|
|
|
* @param aBaseName
|
|
|
|
* The basename of the Sherlock file.
|
|
|
|
* Example: "foo" for file "foo.src".
|
|
|
|
* @see nsIURL::fileBaseName
|
|
|
|
*/
|
|
|
|
_findSherlockIcon: function SRCH_SVC_findSherlock(aEngineFile, aBaseName) {
|
2006-04-28 06:18:58 +04:00
|
|
|
for (var i = 0; i < SHERLOCK_ICON_EXTENSIONS.length; i++) {
|
2006-03-17 10:16:00 +03:00
|
|
|
var icon = aEngineFile.parent.clone();
|
|
|
|
icon.append(aBaseName + SHERLOCK_ICON_EXTENSIONS[i]);
|
|
|
|
if (icon.exists() && icon.isFile())
|
|
|
|
return icon;
|
|
|
|
}
|
2006-03-17 13:39:11 +03:00
|
|
|
return null;
|
2006-03-17 10:16:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a sorted array of engines.
|
|
|
|
* @param aWithHidden
|
|
|
|
* True if hidden plugins should be included in the result.
|
|
|
|
*/
|
|
|
|
_getSortedEngines: function SRCH_SVC_getSorted(aWithHidden) {
|
2006-04-28 06:18:58 +04:00
|
|
|
if (aWithHidden)
|
|
|
|
return this._sortedEngines;
|
2006-03-17 10:16:00 +03:00
|
|
|
|
2006-04-28 06:18:58 +04:00
|
|
|
return this._sortedEngines.filter(function (engine) {
|
|
|
|
return !engine.hidden;
|
|
|
|
});
|
2006-03-17 10:16:00 +03:00
|
|
|
},
|
|
|
|
|
2006-04-28 06:18:58 +04:00
|
|
|
// nsIBrowserSearchService
|
2006-03-17 10:16:00 +03:00
|
|
|
getEngines: function SRCH_SVC_getEngines(aCount) {
|
|
|
|
LOG("getEngines: getting all engines");
|
|
|
|
var engines = this._getSortedEngines(true);
|
|
|
|
aCount.value = engines.length;
|
|
|
|
return engines;
|
|
|
|
},
|
|
|
|
|
|
|
|
getVisibleEngines: function SRCH_SVC_getVisible(aCount) {
|
|
|
|
LOG("getVisibleEngines: getting all visible engines");
|
|
|
|
var engines = this._getSortedEngines(false);
|
|
|
|
aCount.value = engines.length;
|
|
|
|
return engines;
|
|
|
|
},
|
|
|
|
|
2006-08-08 03:26:27 +04:00
|
|
|
getDefaultEngines: function SRCH_SVC_getDefault(aCount) {
|
2007-05-04 19:48:08 +04:00
|
|
|
function isDefault(engine) {
|
|
|
|
return engine._isDefault;
|
2006-08-08 03:26:27 +04:00
|
|
|
};
|
|
|
|
var engines = this._sortedEngines.filter(isDefault);
|
|
|
|
var engineOrder = {};
|
2007-05-04 19:48:08 +04:00
|
|
|
var engineName;
|
2006-08-08 03:26:27 +04:00
|
|
|
var i = 1;
|
2006-08-27 21:18:41 +04:00
|
|
|
|
2007-05-04 19:48:08 +04:00
|
|
|
// 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 {
|
2008-10-07 00:57:18 +04:00
|
|
|
var extras = gPrefSvc.getChildList(BROWSER_SEARCH_PREF + "order.extra.",
|
|
|
|
{});
|
2007-05-04 19:48:08 +04:00
|
|
|
|
|
|
|
for each (var prefName in extras) {
|
2008-10-07 00:57:18 +04:00
|
|
|
engineName = gPrefSvc.getCharPref(prefName);
|
2007-05-04 19:48:08 +04:00
|
|
|
|
|
|
|
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)
|
2006-08-08 03:26:27 +04:00
|
|
|
break;
|
2006-08-27 21:18:41 +04:00
|
|
|
|
2007-05-04 19:48:08 +04:00
|
|
|
if (!(engineName in engineOrder))
|
|
|
|
engineOrder[engineName] = i++;
|
2006-08-08 03:26:27 +04:00
|
|
|
}
|
|
|
|
|
2007-05-04 19:48:08 +04:00
|
|
|
LOG("getDefaultEngines: engineOrder: " + engineOrder.toSource());
|
|
|
|
|
2006-08-08 03:26:27 +04:00
|
|
|
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;
|
|
|
|
|
2006-08-31 03:19:18 +04:00
|
|
|
return a.name.localeCompare(b.name);
|
2006-08-08 03:26:27 +04:00
|
|
|
}
|
|
|
|
engines.sort(compareEngines);
|
|
|
|
|
|
|
|
aCount.value = engines.length;
|
|
|
|
return engines;
|
|
|
|
},
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
getEngineByName: function SRCH_SVC_getEngineByName(aEngineName) {
|
|
|
|
return this._engines[aEngineName] || null;
|
|
|
|
},
|
|
|
|
|
|
|
|
getEngineByAlias: function SRCH_SVC_getEngineByAlias(aAlias) {
|
|
|
|
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) {
|
2008-10-18 06:24:47 +04:00
|
|
|
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);
|
2006-03-17 10:16:00 +03:00
|
|
|
|
2006-04-14 03:28:21 +04:00
|
|
|
var engine = new Engine(getSanitizedFile(aName), SEARCH_DATA_XML, false);
|
2006-03-17 10:16:00 +03:00
|
|
|
engine._initFromMetadata(aName, aIconURL, aAlias, aDescription,
|
|
|
|
aMethod, aTemplate);
|
|
|
|
this._addEngineToStore(engine);
|
2008-10-07 00:57:18 +04:00
|
|
|
this._batchCacheInvalidation();
|
2006-03-17 10:16:00 +03:00
|
|
|
},
|
|
|
|
|
2006-07-21 03:00:39 +04:00
|
|
|
addEngine: function SRCH_SVC_addEngine(aEngineURL, aDataType, aIconURL,
|
2006-07-14 20:31:27 +04:00
|
|
|
aConfirm) {
|
2006-03-17 10:16:00 +03:00
|
|
|
LOG("addEngine: Adding \"" + aEngineURL + "\".");
|
|
|
|
try {
|
2006-06-21 01:41:47 +04:00
|
|
|
var uri = makeURI(aEngineURL);
|
2006-07-21 03:00:39 +04:00
|
|
|
var engine = new Engine(uri, aDataType, false);
|
2006-04-14 03:28:21 +04:00
|
|
|
engine._initFromURI();
|
2006-03-17 10:16:00 +03:00
|
|
|
} catch (ex) {
|
2008-10-18 06:24:47 +04:00
|
|
|
FAIL("addEngine: Error adding engine:\n" + ex, Cr.NS_ERROR_FAILURE);
|
2006-03-17 10:16:00 +03:00
|
|
|
}
|
2006-05-29 23:00:56 +04:00
|
|
|
engine._setIcon(aIconURL, false);
|
2006-07-14 20:31:27 +04:00
|
|
|
engine._confirm = aConfirm;
|
2006-03-17 10:16:00 +03:00
|
|
|
},
|
|
|
|
|
2006-04-28 06:18:58 +04:00
|
|
|
removeEngine: function SRCH_SVC_removeEngine(aEngine) {
|
2008-10-18 06:24:47 +04:00
|
|
|
if (!aEngine)
|
|
|
|
FAIL("no engine passed to removeEngine!");
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
var engineToRemove = null;
|
|
|
|
for (var e in this._engines)
|
|
|
|
if (aEngine.wrappedJSObject == this._engines[e])
|
|
|
|
engineToRemove = this._engines[e];
|
|
|
|
|
2008-10-18 06:24:47 +04:00
|
|
|
if (!engineToRemove)
|
|
|
|
FAIL("removeEngine: Can't find engine to remove!", Cr.NS_ERROR_FILE_NOT_FOUND);
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
if (engineToRemove == this.currentEngine)
|
2006-04-28 06:18:58 +04:00
|
|
|
this._currentEngine = null;
|
2006-03-17 10:16:00 +03:00
|
|
|
|
|
|
|
if (engineToRemove._readOnly) {
|
2007-06-27 23:25:14 +04:00
|
|
|
// Just hide it (the "hidden" setter will notify) and remove its alias to
|
|
|
|
// avoid future conflicts with other engines.
|
2006-03-17 10:16:00 +03:00
|
|
|
engineToRemove.hidden = true;
|
2007-06-27 23:25:14 +04:00
|
|
|
engineToRemove.alias = null;
|
2006-03-17 10:16:00 +03:00
|
|
|
} else {
|
2008-03-06 01:13:41 +03:00
|
|
|
// Cancel the lazy serialization timer if it's running
|
|
|
|
if (engineToRemove._serializeTimer) {
|
|
|
|
engineToRemove._serializeTimer.cancel();
|
|
|
|
engineToRemove._serializeTimer = null;
|
|
|
|
}
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
// Remove the engine file from disk (this might throw)
|
|
|
|
engineToRemove._remove();
|
2006-07-21 03:00:39 +04:00
|
|
|
engineToRemove._file = null;
|
2006-04-28 06:18:58 +04:00
|
|
|
|
|
|
|
// Remove the engine from _sortedEngines
|
|
|
|
var index = this._sortedEngines.indexOf(engineToRemove);
|
2008-10-18 06:24:47 +04:00
|
|
|
if (index == -1)
|
|
|
|
FAIL("Can't find engine to remove in _sortedEngines!", Cr.NS_ERROR_FAILURE);
|
2006-04-28 06:18:58 +04:00
|
|
|
this._sortedEngines.splice(index, 1);
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
// Remove the engine from the internal store
|
|
|
|
delete this._engines[engineToRemove.name];
|
2006-04-28 06:18:58 +04:00
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
notifyAction(engineToRemove, SEARCH_ENGINE_REMOVED);
|
2006-07-28 01:54:53 +04:00
|
|
|
|
|
|
|
// Since we removed an engine, we need to update the preferences.
|
|
|
|
this._needToSetOrderPrefs = true;
|
2006-03-17 10:16:00 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2006-04-28 06:18:58 +04:00
|
|
|
moveEngine: function SRCH_SVC_moveEngine(aEngine, aNewIndex) {
|
2008-10-18 06:24:47 +04:00
|
|
|
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);
|
2006-04-28 06:18:58 +04:00
|
|
|
|
|
|
|
var engine = aEngine.wrappedJSObject;
|
|
|
|
|
|
|
|
var currentIndex = this._sortedEngines.indexOf(engine);
|
2008-10-18 06:24:47 +04:00
|
|
|
if (currentIndex == -1)
|
|
|
|
FAIL("moveEngine: Can't find engine to move!", Cr.NS_ERROR_UNEXPECTED);
|
2006-04-28 06:18:58 +04:00
|
|
|
|
2006-09-13 11:03:34 +04:00
|
|
|
// 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];
|
2008-10-18 06:24:47 +04:00
|
|
|
if (!newIndexEngine)
|
|
|
|
FAIL("moveEngine: Can't find engine to replace!", Cr.NS_ERROR_UNEXPECTED);
|
2006-09-13 11:03:34 +04:00
|
|
|
|
|
|
|
for (var i = 0; i < this._sortedEngines.length; ++i) {
|
|
|
|
if (newIndexEngine == this._sortedEngines[i])
|
|
|
|
break;
|
|
|
|
if (this._sortedEngines[i].hidden)
|
|
|
|
aNewIndex++;
|
|
|
|
}
|
|
|
|
|
2006-08-27 21:18:41 +04:00
|
|
|
if (currentIndex == aNewIndex)
|
|
|
|
return; // nothing to do!
|
|
|
|
|
2006-06-09 21:13:52 +04:00
|
|
|
// Move the engine
|
|
|
|
var movedEngine = this._sortedEngines.splice(currentIndex, 1)[0];
|
|
|
|
this._sortedEngines.splice(aNewIndex, 0, movedEngine);
|
2006-04-28 06:18:58 +04:00
|
|
|
|
|
|
|
notifyAction(engine, SEARCH_ENGINE_CHANGED);
|
2006-07-28 01:54:53 +04:00
|
|
|
|
|
|
|
// Since we moved an engine, we need to update the preferences.
|
|
|
|
this._needToSetOrderPrefs = true;
|
2006-04-28 06:18:58 +04:00
|
|
|
},
|
|
|
|
|
2006-07-05 23:09:38 +04:00
|
|
|
restoreDefaultEngines: function SRCH_SVC_resetDefaultEngines() {
|
|
|
|
for each (var e in this._engines) {
|
2007-05-04 19:48:08 +04:00
|
|
|
// Unhide all default engines
|
|
|
|
if (e.hidden && e._isDefault)
|
2006-07-05 23:09:38 +04:00
|
|
|
e.hidden = false;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
get defaultEngine() {
|
|
|
|
const defPref = BROWSER_SEARCH_PREF + "defaultenginename";
|
|
|
|
// Get the default engine - this pref should always exist, but the engine
|
|
|
|
// might be hidden
|
|
|
|
this._defaultEngine = this.getEngineByName(getLocalizedPref(defPref, ""));
|
|
|
|
if (!this._defaultEngine || this._defaultEngine.hidden)
|
2006-04-28 06:18:58 +04:00
|
|
|
this._defaultEngine = this._getSortedEngines(false)[0] || null;
|
2006-03-17 10:16:00 +03:00
|
|
|
return this._defaultEngine;
|
|
|
|
},
|
|
|
|
|
|
|
|
get currentEngine() {
|
2006-04-28 06:18:58 +04:00
|
|
|
if (!this._currentEngine || this._currentEngine.hidden)
|
2006-03-17 10:16:00 +03:00
|
|
|
this._currentEngine = this.defaultEngine;
|
|
|
|
return this._currentEngine;
|
|
|
|
},
|
|
|
|
set currentEngine(val) {
|
2008-10-18 06:24:47 +04:00
|
|
|
if (!(val instanceof Ci.nsISearchEngine))
|
|
|
|
FAIL("Invalid argument passed to currentEngine setter");
|
2006-03-17 10:16:00 +03:00
|
|
|
|
2006-04-28 06:18:58 +04:00
|
|
|
var newCurrentEngine = this.getEngineByName(val.name);
|
2008-10-18 06:24:47 +04:00
|
|
|
if (!newCurrentEngine)
|
|
|
|
FAIL("Can't find engine in store!", Cr.NS_ERROR_UNEXPECTED);
|
2006-04-28 06:18:58 +04:00
|
|
|
|
|
|
|
this._currentEngine = newCurrentEngine;
|
2006-08-02 18:08:12 +04:00
|
|
|
|
|
|
|
var currentEnginePref = BROWSER_SEARCH_PREF + "selectedEngine";
|
|
|
|
|
|
|
|
if (this._currentEngine == this.defaultEngine) {
|
2008-10-07 00:57:18 +04:00
|
|
|
if (gPrefSvc.prefHasUserValue(currentEnginePref))
|
|
|
|
gPrefSvc.clearUserPref(currentEnginePref);
|
2006-08-02 18:08:12 +04:00
|
|
|
}
|
|
|
|
else {
|
2006-10-31 18:32:43 +03:00
|
|
|
setLocalizedPref(currentEnginePref, this._currentEngine.name);
|
2006-08-02 18:08:12 +04:00
|
|
|
}
|
|
|
|
|
2006-03-17 10:16:00 +03:00
|
|
|
notifyAction(this._currentEngine, SEARCH_ENGINE_CURRENT);
|
|
|
|
},
|
|
|
|
|
|
|
|
// nsIObserver
|
|
|
|
observe: function SRCH_SVC_observe(aEngine, aTopic, aVerb) {
|
|
|
|
switch (aTopic) {
|
|
|
|
case SEARCH_ENGINE_TOPIC:
|
2008-10-07 00:57:18 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
this._batchCacheInvalidation();
|
|
|
|
break;
|
|
|
|
case SEARCH_ENGINE_CHANGED:
|
|
|
|
case SEARCH_ENGINE_REMOVED:
|
|
|
|
this._batchCacheInvalidation();
|
|
|
|
break;
|
2006-03-17 10:16:00 +03:00
|
|
|
}
|
|
|
|
break;
|
2008-10-07 00:57:18 +04:00
|
|
|
|
2006-08-04 17:59:20 +04:00
|
|
|
case QUIT_APPLICATION_TOPIC:
|
2006-03-17 10:16:00 +03:00
|
|
|
this._removeObservers();
|
2006-08-04 17:59:20 +04:00
|
|
|
this._saveSortedEngineList();
|
2008-10-07 00:57:18 +04:00
|
|
|
if (this._batchTimer) {
|
|
|
|
// Flush to disk immediately
|
|
|
|
this._batchTimer.cancel();
|
|
|
|
this._buildCache();
|
|
|
|
}
|
2006-03-17 10:16:00 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
_addObservers: function SRCH_SVC_addObservers() {
|
2008-10-07 00:57:18 +04:00
|
|
|
gObsSvc.addObserver(this, SEARCH_ENGINE_TOPIC, false);
|
|
|
|
gObsSvc.addObserver(this, QUIT_APPLICATION_TOPIC, false);
|
2006-03-17 10:16:00 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
_removeObservers: function SRCH_SVC_removeObservers() {
|
2008-10-07 00:57:18 +04:00
|
|
|
gObsSvc.removeObserver(this, SEARCH_ENGINE_TOPIC);
|
|
|
|
gObsSvc.removeObserver(this, QUIT_APPLICATION_TOPIC);
|
2006-03-17 10:16:00 +03:00
|
|
|
},
|
|
|
|
|
2007-11-24 05:45:42 +03:00
|
|
|
QueryInterface: function SRCH_SVC_QI(aIID) {
|
|
|
|
if (aIID.equals(Ci.nsIBrowserSearchService) ||
|
|
|
|
aIID.equals(Ci.nsIObserver) ||
|
|
|
|
aIID.equals(Ci.nsISupports))
|
|
|
|
return this;
|
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
2006-03-17 10:16:00 +03:00
|
|
|
};
|
|
|
|
|
2006-06-17 04:58:09 +04:00
|
|
|
var engineMetadataService = {
|
|
|
|
init: function epsInit() {
|
|
|
|
var engineDataTable = "id INTEGER PRIMARY KEY, engineid STRING, name STRING, value STRING";
|
2006-06-21 04:19:55 +04:00
|
|
|
var file = getDir(NS_APP_USER_PROFILE_50_DIR);
|
2006-06-28 20:09:26 +04:00
|
|
|
file.append("search.sqlite");
|
2006-06-17 04:58:09 +04:00
|
|
|
var dbService = Cc["@mozilla.org/storage/service;1"].
|
2006-06-21 04:19:55 +04:00
|
|
|
getService(Ci.mozIStorageService);
|
2006-07-26 22:54:12 +04:00
|
|
|
try {
|
|
|
|
this.mDB = dbService.openDatabase(file);
|
|
|
|
} catch (ex) {
|
|
|
|
if (ex.result == 0x8052000b) { /* NS_ERROR_FILE_CORRUPTED */
|
|
|
|
// delete and try again
|
|
|
|
file.remove(false);
|
|
|
|
this.mDB = dbService.openDatabase(file);
|
|
|
|
} else {
|
|
|
|
throw ex;
|
|
|
|
}
|
|
|
|
}
|
2006-06-17 04:58:09 +04:00
|
|
|
|
|
|
|
try {
|
|
|
|
this.mDB.createTable("engine_data", engineDataTable);
|
|
|
|
} catch (ex) {
|
|
|
|
// Fails if the table already exists, which is fine
|
|
|
|
}
|
|
|
|
|
|
|
|
this.mGetData = createStatement (
|
|
|
|
this.mDB,
|
|
|
|
"SELECT value FROM engine_data WHERE engineid = :engineid AND name = :name");
|
|
|
|
this.mDeleteData = createStatement (
|
|
|
|
this.mDB,
|
|
|
|
"DELETE FROM engine_data WHERE engineid = :engineid AND name = :name");
|
|
|
|
this.mInsertData = createStatement (
|
|
|
|
this.mDB,
|
|
|
|
"INSERT INTO engine_data (engineid, name, value) " +
|
|
|
|
"VALUES (:engineid, :name, :value)");
|
|
|
|
},
|
|
|
|
getAttr: function epsGetAttr(engine, name) {
|
|
|
|
// attr names must be lower case
|
|
|
|
name = name.toLowerCase();
|
|
|
|
|
|
|
|
var stmt = this.mGetData;
|
|
|
|
stmt.reset();
|
|
|
|
var pp = stmt.params;
|
|
|
|
pp.engineid = engine._id;
|
|
|
|
pp.name = name;
|
|
|
|
|
|
|
|
var value = null;
|
|
|
|
if (stmt.step())
|
|
|
|
value = stmt.row.value;
|
|
|
|
stmt.reset();
|
|
|
|
return value;
|
|
|
|
},
|
|
|
|
|
|
|
|
setAttr: function epsSetAttr(engine, name, value) {
|
|
|
|
// attr names must be lower case
|
|
|
|
name = name.toLowerCase();
|
|
|
|
|
|
|
|
this.mDB.beginTransaction();
|
|
|
|
|
|
|
|
var pp = this.mDeleteData.params;
|
|
|
|
pp.engineid = engine._id;
|
|
|
|
pp.name = name;
|
|
|
|
this.mDeleteData.step();
|
|
|
|
this.mDeleteData.reset();
|
|
|
|
|
|
|
|
pp = this.mInsertData.params;
|
|
|
|
pp.engineid = engine._id;
|
|
|
|
pp.name = name;
|
|
|
|
pp.value = value;
|
|
|
|
this.mInsertData.step();
|
|
|
|
this.mInsertData.reset();
|
|
|
|
|
|
|
|
this.mDB.commitTransaction();
|
|
|
|
},
|
2006-07-21 03:00:39 +04:00
|
|
|
|
2006-07-12 16:34:30 +04:00
|
|
|
setAttrs: function epsSetAttrs(engines, names, values) {
|
|
|
|
this.mDB.beginTransaction();
|
2006-07-21 03:00:39 +04:00
|
|
|
|
2006-07-12 16:34:30 +04:00
|
|
|
for (var i = 0; i < engines.length; i++) {
|
|
|
|
// attr names must be lower case
|
|
|
|
var name = names[i].toLowerCase();
|
|
|
|
|
|
|
|
var pp = this.mDeleteData.params;
|
|
|
|
pp.engineid = engines[i]._id;
|
|
|
|
pp.name = names[i];
|
|
|
|
this.mDeleteData.step();
|
|
|
|
this.mDeleteData.reset();
|
|
|
|
|
|
|
|
pp = this.mInsertData.params;
|
|
|
|
pp.engineid = engines[i]._id;
|
|
|
|
pp.name = names[i];
|
|
|
|
pp.value = values[i];
|
|
|
|
this.mInsertData.step();
|
|
|
|
this.mInsertData.reset();
|
|
|
|
}
|
2006-07-21 03:00:39 +04:00
|
|
|
|
2006-07-12 16:34:30 +04:00
|
|
|
this.mDB.commitTransaction();
|
|
|
|
},
|
2006-06-17 04:58:09 +04:00
|
|
|
|
|
|
|
deleteEngineData: function epsDelData(engine, name) {
|
|
|
|
// attr names must be lower case
|
|
|
|
name = name.toLowerCase();
|
|
|
|
|
|
|
|
var pp = this.mDeleteData.params;
|
|
|
|
pp.engineid = engine._id;
|
|
|
|
pp.name = name;
|
|
|
|
this.mDeleteData.step();
|
|
|
|
this.mDeleteData.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-21 03:00:39 +04:00
|
|
|
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) {
|
2008-10-07 00:57:18 +04:00
|
|
|
if (getBoolPref(BROWSER_SEARCH_PREF + "update.log", false)) {
|
2006-07-21 03:00:39 +04:00
|
|
|
dump(SEARCH_UPDATE_LOG_PREFIX + aText + "\n");
|
|
|
|
var consoleService = Cc["@mozilla.org/consoleservice;1"].
|
|
|
|
getService(Ci.nsIConsoleService);
|
|
|
|
consoleService.logStringMessage(aText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var engineUpdateService = {
|
|
|
|
init: function eus_init() {
|
|
|
|
var tm = Cc["@mozilla.org/updates/timer-manager;1"].
|
|
|
|
getService(Ci.nsIUpdateTimerManager);
|
|
|
|
// figure out how often to check for any expired engines
|
2008-10-07 00:57:18 +04:00
|
|
|
var interval = gPrefSvc.getIntPref(BROWSER_SEARCH_PREF + "updateinterval");
|
2006-07-21 03:00:39 +04:00
|
|
|
|
|
|
|
// Interval is stored in hours
|
|
|
|
var seconds = interval * 3600;
|
|
|
|
tm.registerTimer("search-engine-update-timer", engineUpdateService,
|
|
|
|
seconds);
|
|
|
|
},
|
|
|
|
|
|
|
|
scheduleNextUpdate: function eus_scheduleNextUpdate(aEngine) {
|
2006-07-21 05:30:48 +04:00
|
|
|
var interval = aEngine._updateInterval || SEARCH_DEFAULT_UPDATE_INTERVAL;
|
2006-07-21 03:00:39 +04:00
|
|
|
var milliseconds = interval * 86400000; // |interval| is in days
|
|
|
|
engineMetadataService.setAttr(aEngine, "updateexpir",
|
|
|
|
Date.now() + milliseconds);
|
|
|
|
},
|
|
|
|
|
|
|
|
notify: function eus_Notify(aTimer) {
|
|
|
|
ULOG("notify called");
|
|
|
|
|
|
|
|
if (!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 searchService = Cc["@mozilla.org/browser/search-service;1"].
|
|
|
|
getService(Ci.nsIBrowserSearchService);
|
|
|
|
var currentTime = Date.now();
|
|
|
|
ULOG("currentTime: " + currentTime);
|
|
|
|
for each (engine in searchService.getEngines({})) {
|
|
|
|
engine = engine.wrappedJSObject;
|
2006-08-07 00:30:37 +04:00
|
|
|
if (!engine._hasUpdates || engine._readOnly)
|
2006-07-21 05:30:48 +04:00
|
|
|
continue;
|
|
|
|
|
2006-07-21 03:00:39 +04:00
|
|
|
ULOG("checking " + engine.name);
|
|
|
|
|
|
|
|
var expirTime = engineMetadataService.getAttr(engine, "updateexpir");
|
|
|
|
var updateURL = engine._updateURL;
|
|
|
|
var iconUpdateURL = engine._iconUpdateURL;
|
|
|
|
ULOG("expirTime: " + expirTime + "\nupdateURL: " + updateURL +
|
|
|
|
"\niconUpdateURL: " + iconUpdateURL);
|
|
|
|
|
|
|
|
var engineExpired = expirTime <= currentTime;
|
|
|
|
|
2006-07-21 05:30:48 +04:00
|
|
|
if (!expirTime || !engineExpired) {
|
2006-07-21 03:00:39 +04:00
|
|
|
ULOG("skipping engine");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ULOG(engine.name + " has expired");
|
|
|
|
|
|
|
|
var testEngine = null;
|
2006-10-18 20:52:35 +04:00
|
|
|
|
|
|
|
var updateURI = makeURI(updateURL);
|
|
|
|
if (updateURI) {
|
2006-07-21 03:00:39 +04:00
|
|
|
var dataType = engineMetadataService.getAttr(engine, "updatedatatype")
|
|
|
|
if (!dataType) {
|
|
|
|
ULOG("No loadtype to update engine!");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-10-18 20:52:35 +04:00
|
|
|
testEngine = new Engine(updateURI, dataType, false);
|
2006-07-21 03:00:39 +04:00
|
|
|
testEngine._engineToUpdate = engine;
|
|
|
|
testEngine._initFromURI();
|
2006-10-18 20:52:35 +04:00
|
|
|
} else
|
|
|
|
ULOG("invalid updateURI");
|
2006-07-21 03:00:39 +04:00
|
|
|
|
|
|
|
if (iconUpdateURL) {
|
|
|
|
// If we're updating the engine too, use the new engine object,
|
|
|
|
// otherwise use the existing engine object.
|
|
|
|
(testEngine || engine)._setIcon(iconUpdateURL, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Schedule the next update
|
|
|
|
this.scheduleNextUpdate(engine);
|
2006-10-18 20:52:35 +04:00
|
|
|
|
2006-07-21 03:00:39 +04:00
|
|
|
} // end engine iteration
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-11-24 05:45:42 +03:00
|
|
|
const kClassID = Components.ID("{7319788a-fe93-4db3-9f39-818cf08f4256}");
|
|
|
|
const kClassName = "Browser Search Service";
|
|
|
|
const kContractID = "@mozilla.org/browser/search-service;1";
|
|
|
|
|
|
|
|
// nsIFactory
|
|
|
|
const kFactory = {
|
|
|
|
createInstance: function (outer, iid) {
|
|
|
|
if (outer != null)
|
|
|
|
throw Cr.NS_ERROR_NO_AGGREGATION;
|
|
|
|
return (new SearchService()).QueryInterface(iid);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// nsIModule
|
|
|
|
const gModule = {
|
|
|
|
registerSelf: function (componentManager, fileSpec, location, type) {
|
|
|
|
componentManager.QueryInterface(Ci.nsIComponentRegistrar);
|
|
|
|
componentManager.registerFactoryLocation(kClassID,
|
|
|
|
kClassName,
|
|
|
|
kContractID,
|
|
|
|
fileSpec, location, type);
|
|
|
|
},
|
|
|
|
|
|
|
|
unregisterSelf: function(componentManager, fileSpec, location) {
|
|
|
|
componentManager.QueryInterface(Ci.nsIComponentRegistrar);
|
|
|
|
componentManager.unregisterFactoryLocation(kClassID, fileSpec);
|
|
|
|
},
|
|
|
|
|
|
|
|
getClassObject: function (componentManager, cid, iid) {
|
|
|
|
if (!cid.equals(kClassID))
|
|
|
|
throw Cr.NS_ERROR_NO_INTERFACE;
|
|
|
|
if (!iid.equals(Ci.nsIFactory))
|
|
|
|
throw Cr.NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
return kFactory;
|
|
|
|
},
|
|
|
|
|
|
|
|
canUnload: function (componentManager) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function NSGetModule(componentManager, fileSpec) {
|
|
|
|
return gModule;
|
|
|
|
}
|
2006-04-25 20:26:58 +04:00
|
|
|
|
|
|
|
#include ../../../toolkit/content/debug.js
|