home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Minami 80
/
MINAMI80.iso
/
Extra
/
DivXInstaller.exe
/
$PLUGINSDIR
/
GoogleToolbarFirefox.msi
/
xpi
/
components
/
gtbComponent.js
< prev
Wrap
Text File
|
2006-05-15
|
15KB
|
497 lines
const Cc = Components.classes;
const Ci = Components.interfaces;
var nsIComponentRegistrar = Ci.nsIComponentRegistrar;
var UNINSTALLER_CID = Components.ID("{abf0defb-ad5c-4673-a8fe-edd64c22a441}");
var UNINSTALLER_NAME = "Google Toolbar Uninstall Handler";
var UNINSTALLER_CONTRACTID = "@google.com/uninstaller;1";
function GTB_UninstallExtension(guid) {
try {
var manager = Cc["@mozilla.org/extensions/manager;1"]
.getService(Ci.nsIExtensionManager);
var dummy = manager.datasource; // HACK: force datasource initialization
if ("uninstallExtension" in manager) {
manager.uninstallExtension('{' + guid + '}');
} else if ("uninstallItem" in manager) {
manager.uninstallItem('{' + guid + '}');
} else {
return false;
}
dummy = null; // REHACK: don't leak any resources
}
catch(e) {
}
return true;
}
function insertCommas(number) {
var s = number.toString();
var result = "";
while(s.length > 3) {
result = "," + s.substring(s.length-3, s.length);
s = s.substring(0, s.length-3);
}
result = s + result;
return result;
}
var SEARCH_SUGGEST_CONTRACTID = "@mozilla.org/autocomplete/search;1?name=google-search-suggest";
var SEARCH_SUGGEST_CLASSNAME = "Google Search Suggest";
var SEARCH_SUGGEST_CLASSID = Components.ID("{71631a52-b676-4628-8831-f777932b82ec}");
var SEARCH_SUGGEST_SERVICEURL = "google.suggest.serviceURL.search";
var USERAGENT_LOCALE_PREF = "general.useragent.locale";
function SuggestAutoCompleteResult(searchString,
searchResult,
defaultIndex,
errorDescription,
suggestResults,
suggestComments,
historyResults,
historyComments) {
this._searchString = searchString;
this._searchResult = searchResult;
this._defaultIndex = defaultIndex;
this._errorDescription = errorDescription;
for (var i in historyResults) {
for (var j in suggestResults) {
if (historyResults[i] == suggestResults[j]) {
suggestResults.splice(j, 1);
suggestComments.splice(j, 1);
break;
}
}
}
var results = [];
results = results.concat(historyResults);
results = results.concat(suggestResults);
this._results = results;
var comments = [];
comments = comments.concat(historyComments);
comments = comments.concat(suggestComments);
this._comments = comments;
}
SuggestAutoCompleteResult.prototype = {
_resultString: "",
_resultsString: "",
_didYouMeanString: "",
_historyString: "",
_searchString: "",
_searchResult: 0,
_defaultIndex: 0,
_errorDescription: "",
_results: [],
_comments: [],
getValueAt: function(index) {
return this._results[index];
},
getCommentAt: function(index) {
return this._comments[index];
},
getStyleAt: function(index) {
return null;
},
removeValueAt: function(index, removeFromDatabase) {
this._results.splice(index, 1);
this._comments.splice(index, 1);
},
QueryInterface: function(iid) {
if (!iid.equals(Ci.nsIAutoCompleteResult) &&
!iid.equals(Ci.nsISupports)) {
throw Components.results.NS_ERROR_NO_INTERFACE;
}
return this;
}
};
SuggestAutoCompleteResult.prototype.__defineGetter__("searchString", function() {
return this._searchString;
});
SuggestAutoCompleteResult.prototype.__defineGetter__("searchResult", function() {
return this._searchResult;
});
SuggestAutoCompleteResult.prototype.__defineGetter__("defaultIndex", function() {
return this._defaultIndex;
});
SuggestAutoCompleteResult.prototype.__defineGetter__("errorDescription", function() {
return this._errorDescription;
});
SuggestAutoCompleteResult.prototype.__defineGetter__("matchCount", function() {
return this._results.length;
});
function SuggestAutoComplete() { }
SuggestAutoComplete.prototype = {
_requestSuggest: null,
_listener: null,
onSuggestReadyStateChange: function() {
if (this._requestSuggest && this._requestSuggest.readyState == 4) {
try {
var status = this._requestSuggest.status;
}
catch (e) {
return;
}
var suggestResults = [];
var suggestComments = [];
var spellingResults = [];
var spellingComments = [];
var responseText = this._requestSuggest.responseText;
if (status == 200 && responseText != "") {
var parser = Cc["@mozilla.org/xmlextras/domparser;1"]
.createInstance(Ci.nsIDOMParser);
var xmlDoc = parser.parseFromString(responseText, "text/xml");
if (xmlDoc != null) {
var topLevelNode = xmlDoc.documentElement;
if (topLevelNode.hasChildNodes()) {
var children = topLevelNode.childNodes;
for(var i=0 ; i < children.length ; i++) {
var child = children[i];
if (child.nodeName == "CompleteSuggestion" &&
child.hasChildNodes()) {
var suggestionText = null;
var resultCount = null;
var suggestion = child.childNodes;
for(var j=0 ; j < suggestion.length ; j++) {
if (suggestion[j].nodeName == "suggestion") {
suggestionText = suggestion[j].getAttribute("data");
} else if (suggestion[j].nodeName == "num_queries") {
resultCount = parseInt(suggestion[j].getAttribute("int"));
}
}
if (suggestionText != null && resultCount != null) {
suggestResults[suggestResults.length] = suggestionText;
suggestComments[suggestComments.length] =
insertCommas(resultCount) + " " +
(resultCount == 1 ?
this._resultString :
this._resultsString);
}
} else if (child.nodeName == "spelling_suggestion") {
var spellingText = child.getAttribute("data");
spellingText = spellingText.replace(/<\/{0,1}[a-z]+\/{0,1}>/gi,"");
spellingResults[spellingResults.length] = spellingText;
spellingComments[spellingComments.length] = this._didYouMeanString;
}
}
}
}
}
var results = [];
var comments = [];
results = results.concat(spellingResults);
results = results.concat(suggestResults);
comments = comments.concat(spellingComments);
comments = comments.concat(suggestComments);
this.onSuggestResultsReady(this._searchString, results, comments);
this._requestSuggest = null;
}
},
onSuggestResultsReady: function(searchString, results, comments) {
if (this._listener) {
this._suggestResults = results;
this._suggestComments = comments;
if (this._suggestResults != null && this._suggestComments != null &&
this._historyResults != null && this._historyComments != null) {
var result = new SuggestAutoCompleteResult(searchString,
Ci.nsIAutoCompleteResult.RESULT_SUCCESS,
0, "",
this._suggestResults, this._suggestComments,
this._historyResults, this._historyComments);
this._listener.onSearchResult(this, result);
}
}
},
onSuggestError: function() {
if (this._listener) {
this._suggestResults = [];
this._suggestComments = [];
if (this._suggestResults != null && this._suggestComments != null &&
this._historyResults != null && this._historyComments != null) {
var result = new SuggestAutoCompleteResult("",
Ci.nsIAutoCompleteResult.RESULT_FAILURE,
0, "",
this._suggestResults, this._suggestComments,
this._historyResults, this._historyComments);
this._listener.onSearchResult(this, result);
}
}
},
startSearch: function(searchString, searchParam, previousResult, listener) {
if (this._requestSuggest) {
this.stopSuggestSearch();
}
this._historyResults = null;
this._historyComments = null;
this._suggestResults = null;
this._suggestComments = null;
this._listener = listener;
this._searchString = searchString;
this._requestSuggest = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"]
.createInstance(Ci.nsIXMLHttpRequest);
this._requestSuggest.open("GET", this.serviceURL + searchString, true);
var self = this;
function onSuggestReadyStateChange() {
self.onSuggestReadyStateChange();
}
function onSuggestError() {
self.onSuggestError();
}
this._requestSuggest.onreadystatechange = onSuggestReadyStateChange;
this._requestSuggest.onerror = onSuggestError;
this._requestSuggest.send(null);
var history = getMatchingHistory(this._searchString);
this._historyResults = [];
this._historyComments = [];
for(var j=0 ; j < history.length ; j++) {
this._historyResults[this._historyResults.length] = history[j];
this._historyComments[this._historyComments.length] = this._historyString;
}
},
stopSuggestSearch: function() {
if (this._requestSuggest) {
this._requestSuggest.abort();
this._requestSuggest = null;
}
},
stopSearch: function() {
this.stopSuggestSearch();
this._listener = null;
},
QueryInterface: function(iid) {
if (!iid.equals(Ci.nsIAutoCompleteSearch) &&
!iid.equals(Ci.nsIAutoCompleteObserver) &&
!iid.equals(Ci.nsISupports)) {
throw Components.results.NS_ERROR_NO_INTERFACE;
}
return this;
}
};
function getMatchingHistory(query) {
var psvc = Cc["@mozilla.org/preferences;1"]
.getService(Ci.nsIPrefBranch);
var result = [];
if (psvc != null) {
var pref = psvc.getComplexValue("google.toolbar.search_box_history",
Ci.nsISupportsString).data;
if (pref != null) {
var temp = pref.split("\n");
for(var i=0 ; i < temp.length ; i++) {
if(temp[i].indexOf(query) == 0) {
result[result.length] = temp[i];
}
}
}
}
return result;
}
function getServiceURL(serviceURLPref) {
var psvc = Cc["@mozilla.org/preferences;1"]
.getService(Ci.nsIPrefBranch);
var locale = psvc.getCharPref(USERAGENT_LOCALE_PREF);
var langCode = locale.substr(0, locale.indexOf("-"));
var serviceURL = "http://toolbarqueries.google.com/complete/search?hl=%LANG%&client=firetools&output=toolbar&q=";
return serviceURL.replace(/%LANG%/, langCode);
}
function SearchSuggestAutoComplete() {
this.serviceURL = getServiceURL(SEARCH_SUGGEST_SERVICEURL);
var bundle_service = Cc["@mozilla.org/intl/stringbundle;1"].getService(Ci.nsIStringBundleService);
var stringBundle = bundle_service.createBundle("chrome://google-toolbar/locale/google-toolbar.properties");
if(stringBundle){
this._resultString = stringBundle.GetStringFromName("google-suggest.result");
this._resultsString = stringBundle.GetStringFromName("google-suggest.results");
this._didYouMeanString = stringBundle.GetStringFromName("google-suggest.didyoumean");
this._historyString = stringBundle.GetStringFromName("google-suggest.history");
}
}
SearchSuggestAutoComplete.prototype = {
__proto__: SuggestAutoComplete.prototype,
serviceURL: ""
};
function GetProperty(datasource, item, prop) {
var service = Cc["@mozilla.org/rdf/rdf-service;1"]
.getService(Ci.nsIRDFService);
var resource = service.GetResource("http://www.mozilla.org/2004/em-rdf#" + prop);
var valueNode = datasource.GetTarget(item, resource, true);
if (valueNode) {
if (valueNode instanceof Ci.nsIRDFLiteral)
return valueNode.Value;
else if (valueNode instanceof Ci.nsIRDFResource)
return valueNode.Value
}
return null;
}
var gModule = {
_firstTime: true,
getClassObject: function(compMgr, cid, iid) {
if (!iid.equals(Ci.nsIFactory))
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
for (var key in this.objects) {
if (cid.equals(this.objects[key].CID)){
return this.objects[key].factory;
}
}
if (!cid.equals(UNINSTALLER_CID)) {
throw Components.results.NS_ERROR_NO_INTERFACE;
}
return this.uninstallerFactory;
},
registerSelf: function(compMgr, location, loaderStr, type) {
compMgr.QueryInterface(nsIComponentRegistrar);
if (this._firstTime) {
compMgr.registerFactoryLocation(
UNINSTALLER_CID,
UNINSTALLER_NAME,
UNINSTALLER_CONTRACTID,
location, loaderStr, type);
this._firstTime = false;
throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
}
for (var key in this.objects) {
var obj = this.objects[key];
compMgr.registerFactoryLocation(obj.CID, obj.className, obj.contractID,
location, loaderStr, type);
}
},
_makeFactory: function(constructor) {
function createInstance(outer, iid) {
if (outer != null)
throw Components.results.NS_ERROR_NO_AGGREGATION;
return (new constructor()).QueryInterface(iid);
}
return { createInstance: createInstance };
},
canUnload: function(compMgr) {
return true;
},
QueryInterface: function(iid) {
if (iid.equals(Ci.nsIModule) ||
iid.equals(Ci.nsISupports))
return this;
throw Components.results.NS_ERROR_NO_INTERFACE;
},
uninstallerFactory: {
QueryInterface: function(iid) {
if (iid.equals(Ci.nsIFactory) ||
iid.equals(Ci.nsISupports))
return this;
throw Components.results.NS_ERROR_NO_INTERFACE;
},
createInstance: function(outer, iid) {
if (outer != null) {
throw Components.results.NS_ERROR_NO_AGGREGATION;
}
if (!this.uninstaller) {
this.uninstaller = {
handleUninstall: function() {
var prefService = Cc["@mozilla.org/preferences;1"]
.getService(Ci.nsIPrefService);
var prefBranch = prefService.getBranch('google.toolbar.');
prefBranch.deleteBranch('');
try {
var regClass = Cc["@google.com/regutil;1"];
if (regClass) {
var regUtil = regClass.createInstance(Ci.GTBIRegUtil);
if (regUtil) {
var result = regUtil.deleteLocalMachineString("SOFTWARE\\Google\\Toolbar for Firefox", "id");
}
}
} catch (e) {
}
try {
prefService.savePrefFile(null);
} catch (e) {
dump('Error saving pref file:' + e);
}
},
observe: function(subject, topic, data) {
var GUID = '3112ca9c-de6d-4884-a869-9855de68056c';
if (topic == "em-action-requested" && data == "item-uninstalled") {
var item = subject.QueryInterface(Components.interfaces.nsIUpdateItem);
var id = item.id.toLowerCase();
if (id == "{" + GUID + "}") {
var prefService = Cc["@mozilla.org/preferences;1"]
.getService(Ci.nsIPrefService);
var prefBranch = prefService.getBranch('google.toolbar.');
prefBranch.setBoolPref("uninstall_pending", true);
}
}
if (topic == "xpcom-shutdown") {
var prefService = Cc["@mozilla.org/preferences;1"]
.getService(Ci.nsIPrefService);
var prefBranch = prefService.getBranch('google.toolbar.');
if (prefBranch.prefHasUserValue("uninstall_pending")) {
this.handleUninstall();
return;
}
var service = Cc["@mozilla.org/rdf/rdf-service;1"]
.getService(Ci.nsIRDFService);
var container = Cc["@mozilla.org/rdf/container;1"]
.createInstance(Ci.nsIRDFContainer);
var datasource= Cc["@mozilla.org/extensions/manager;1"]
.getService(Ci.nsIExtensionManager).datasource;
var rootName = "urn:mozilla:extension:"
var root = service.GetResource(rootName + "root");
var nameResource = service
.GetResource("http://www.mozilla.org/2004/em-rdf#name");
var uninstallResource = service
.GetResource("http://www.mozilla.org/2004/em-rdf#toBeUninstalled");
var disableResource = service
.GetResource("http://www.mozilla.org/2004/em-rdf#toBeDisabled");
try {
container.Init(datasource, root);
} catch (e) {
rootName = "urn:mozilla:item:";
root = service.GetResource(rootName + "root");
container.Init(datasource, root);
}
var elements = container.GetElements();
while (elements.hasMoreElements()) {
var elt = elements.getNext()
.QueryInterface(Ci.nsIRDFResource);
if (elt.Value != rootName + "{" + GUID + "}") {
continue;
}
var found = false;
var values = ["toBeUninstalled", "toBeDisabled"]; //, "userDisabled"];
for (var i = 0, value = null; value = values[i]; ++i) {
var rdfValue = GetProperty(datasource, elt, value);
if ("true" == rdfValue) {
found = true;
}
}
var rdfValue = GetProperty(datasource, elt, "opType");
if ("needs-uninstall" == rdfValue) {
found = true;
}
if (found) {
this.handleUninstall();
GTB_UninstallExtension(GUID);
}
}
}
},
QueryInterface: function(iid) {
if (iid.equals(Ci.nsISupports) ||
iid.equals(Ci.nsIObserver)) {
return this;
}
throw Components.results.NS_ERROR_NO_INTERFACE;
}
};
var obs = Cc["@mozilla.org/observer-service;1"]
.getService(Ci.nsIObserverService);
obs.addObserver(this.uninstaller, "xpcom-shutdown", false);
obs.addObserver(this.uninstaller, "em-action-requested", false);
}
return this.uninstaller.QueryInterface(iid);
},
uninstaller: null
}
};
function NSGetModule(compMgr, location) {
gModule.objects = {
search: {
CID: SEARCH_SUGGEST_CLASSID,
contractID: SEARCH_SUGGEST_CONTRACTID,
className: SEARCH_SUGGEST_CLASSNAME,
factory: gModule._makeFactory(SearchSuggestAutoComplete)
}
};
return gModule;
}