Replace myk's changes that I accidentally horked during the merge.

This commit is contained in:
dmose@mozilla.org 2007-07-20 13:23:44 -07:00
parent 9b40cbecb4
commit 32ec18717d
7 changed files with 935 additions and 0 deletions

View File

@ -128,6 +128,7 @@ XPIDLSRCS = \
nsIExternalProtocolService.idl \
nsIExternalHelperAppService.idl \
nsIHelperAppLauncherDialog.idl \
nsIHandlerService.idl \
$(NULL)
ifneq (,$(filter mac cocoa,$(MOZ_WIDGET_TOOLKIT)))
@ -148,6 +149,8 @@ GARBAGE += nsOSHelperAppService.cpp $(srcdir)/nsOSHelperAppService.cpp \
nsMIMEInfoWin.cpp $(srcdir)/nsMIMEInfoWin.cpp
endif
EXTRA_COMPONENTS = nsHandlerService.js
# we don't want the shared lib, but we want to force the creation of a static lib.
FORCE_STATIC_LIB = 1
SRCS_IN_OBJDIR = 1

View File

@ -0,0 +1,513 @@
/* ***** 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 Mozilla browser.
*
* The Initial Developer of the Original Code is Mozilla.
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Myk Melez <myk@mozilla.org>
*
* 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.
*
* ***** END LICENSE BLOCK ***** */
const Ci = Components.interfaces;
const Cc = Components.classes;
const Cu = Components.utils;
const Cr = Components.results;
// namespace prefix
const NC_NS = "http://home.netscape.com/NC-rdf#";
// type list properties
const NC_MIME_TYPES = NC_NS + "MIME-types";
const NC_PROTOCOL_SCHEMES = NC_NS + "Protocol-Schemes";
// content type ("type") properties
const NC_EDITABLE = NC_NS + "editable";
// nsIMIMEInfo::MIMEType
const NC_VALUE = NC_NS + "value";
// references nsIHandlerInfo record
const NC_HANDLER_INFO = NC_NS + "handlerProp";
// handler info ("info") properties
// nsIHandlerInfo::preferredAction
const NC_SAVE_TO_DISK = NC_NS + "saveToDisk";
const NC_HANDLE_INTERNALLY = NC_NS + "handleInternal";
const NC_USE_SYSTEM_DEFAULT = NC_NS + "useSystemDefault";
// nsIHandlerInfo::alwaysAskBeforeHandling
const NC_ALWAYS_ASK = NC_NS + "alwaysAsk";
// references nsIHandlerApp record
const NC_PREFERRED_APP = NC_NS + "externalApplication";
// handler app ("handler") properties
// nsIHandlerApp::name
const NC_PRETTY_NAME = NC_NS + "prettyName";
// nsILocalHandlerApp::executable
const NC_PATH = NC_NS + "path";
// nsIWebHandlerApp::uriTemplate
const NC_URI_TEMPLATE = NC_NS + "uriTemplate";
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
function HandlerService() {}
HandlerService.prototype = {
//**************************************************************************//
// XPCOM Plumbing
classDescription: "Handler Service",
classID: Components.ID("{32314cc8-22f7-4f7f-a645-1a45453ba6a6}"),
contractID: "@mozilla.org/uriloader/handler-service;1",
QueryInterface: XPCOMUtils.generateQI([Ci.nsIHandlerService]),
//**************************************************************************//
// nsIHandlerService
store: function HS_store(aHandlerInfo) {
// FIXME: when we switch from RDF to something with transactions (like
// SQLite), enclose the following changes in a transaction so they all
// get rolled back if any of them fail and we don't leave the datastore
// in an inconsistent state.
this._ensureRecordsForType(aHandlerInfo);
this._storePreferredAction(aHandlerInfo);
this._storePreferredHandler(aHandlerInfo);
this._storeAlwaysAsk(aHandlerInfo);
},
//**************************************************************************//
// Storage Methods
_storePreferredAction: function HS__storePreferredAction(aHandlerInfo) {
var infoID = this._getInfoID(aHandlerInfo);
switch(aHandlerInfo.preferredAction) {
case Ci.nsIHandlerInfo.saveToDisk:
this._setLiteral(infoID, NC_SAVE_TO_DISK, "true");
this._removeValue(infoID, NC_HANDLE_INTERNALLY);
this._removeValue(infoID, NC_USE_SYSTEM_DEFAULT);
break;
case Ci.nsIHandlerInfo.handleInternally:
this._setLiteral(infoID, NC_HANDLE_INTERNALLY, "true");
this._removeValue(infoID, NC_SAVE_TO_DISK);
this._removeValue(infoID, NC_USE_SYSTEM_DEFAULT);
break;
case Ci.nsIHandlerInfo.useSystemDefault:
this._setLiteral(infoID, NC_USE_SYSTEM_DEFAULT, "true");
this._removeValue(infoID, NC_SAVE_TO_DISK);
this._removeValue(infoID, NC_HANDLE_INTERNALLY);
break;
// This value is indicated in the datastore either by the absence of
// the three properties or by setting them all "false". Of these two
// options, the former seems preferable, because it reduces the size
// of the RDF file and thus the amount of stuff we have to parse.
case Ci.nsIHandlerInfo.useHelperApp:
default:
this._removeValue(infoID, NC_SAVE_TO_DISK);
this._removeValue(infoID, NC_HANDLE_INTERNALLY);
this._removeValue(infoID, NC_USE_SYSTEM_DEFAULT);
break;
}
},
_storePreferredHandler: function HS__storePreferredHandler(aHandlerInfo) {
var infoID = this._getInfoID(aHandlerInfo);
var handlerID = this._getPreferredHandlerID(aHandlerInfo);
var handler = aHandlerInfo.preferredApplicationHandler;
// First add a record for the preferred app to the datasource. In the
// process we also need to remove any vestiges of an existing record, so
// we remove any properties that we aren't overwriting.
this._setLiteral(handlerID, NC_PRETTY_NAME, handler.name);
if (handler instanceof Ci.nsILocalHandlerApp) {
handler.QueryInterface(Ci.nsILocalHandlerApp);
this._setLiteral(handlerID, NC_PATH, handler.executable.path);
this._removeValue(handlerID, NC_URI_TEMPLATE);
}
else {
handler.QueryInterface(Ci.nsIWebHandlerApp);
this._setLiteral(handlerID, NC_URI_TEMPLATE, handler.uriTemplate);
this._removeValue(handlerID, NC_PATH);
}
// Finally, make the handler app be the preferred app for the handler info.
// Note: at least some code completely ignores this setting and assumes
// the preferred app is the one whose URI follows the appropriate pattern.
this._setResource(infoID, NC_PREFERRED_APP, handlerID);
},
_storeAlwaysAsk: function HS__storeAlwaysAsk(aHandlerInfo) {
var infoID = this._getInfoID(aHandlerInfo);
this._setLiteral(infoID,
NC_ALWAYS_ASK,
aHandlerInfo.alwaysAskBeforeHandling ? "true" : "false");
},
//**************************************************************************//
// Storage Utils
// RDF Service
__rdf: null,
get _rdf() {
if (!this.__rdf)
this.__rdf = Cc["@mozilla.org/rdf/rdf-service;1"].
getService(Ci.nsIRDFService);
return this.__rdf;
},
// RDF Container Utils
__containerUtils: null,
get _containerUtils() {
if (!this.__containerUtils)
this.__containerUtils = Cc["@mozilla.org/rdf/container-utils;1"].
getService(Ci.nsIRDFContainerUtils);
return this.__containerUtils;
},
// RDF datasource containing content handling config (i.e. mimeTypes.rdf)
__ds: null,
get _ds() {
if (!this.__ds) {
var fileLocator = Cc["@mozilla.org/file/directory_service;1"].
getService(Ci.nsIProperties);
var file = fileLocator.get("UMimTyp", Ci.nsIFile);
// FIXME: make this a memoizing getter if we use it anywhere else.
var ioService = Cc["@mozilla.org/network/io-service;1"].
getService(Ci.nsIIOService);
var fileHandler = ioService.getProtocolHandler("file").
QueryInterface(Ci.nsIFileProtocolHandler);
this.__ds =
this._rdf.GetDataSourceBlocking(fileHandler.getURLSpecFromFile(file));
}
return this.__ds;
},
/**
* Get the string value identifying the given content type (i.e. the MIME
* type or protocol scheme).
*
* FIXME: this should be a property of nsIHandlerInfo.
*
* @param aHandlerInfo {nsIHandlerInfo} the type for which to get the string
*/
_getType: function HS__getType(aHandlerInfo) {
// FIXME: once nsIHandlerInfo supports retrieving the scheme
// (and differentiating between MIME and protocol content types),
// implement support for protocols.
var mimeInfo = aHandlerInfo.QueryInterface(Ci.nsIMIMEInfo);
try { var type = mimeInfo.MIMEType }
catch(ex) { throw Cr.NS_ERROR_NOT_IMPLEMENTED }
return type;
},
/**
* Return the unique identifier for a content type record, which stores
* the editable and value fields plus a reference to the type's handler.
*
* FIXME: the ID should be a property of nsIHandlerInfo.
*
* |urn:(mimetype|scheme):<type>|
*
* @param aHandlerInfo {nsIHandlerInfo} the type for which to get the ID
*/
_getTypeID: function HS__getTypeID(aHandlerInfo) {
// FIXME: once nsIHandlerInfo supports retrieving the scheme
// (and differentiating between MIME and protocol content types),
// implement support for protocols.
var id = "urn:mimetype:" + this._getType(aHandlerInfo);
return id;
},
/**
* Return the unique identifier for a type info record, which stores
* the preferredAction and alwaysAsk fields plus a reference to the preferred
* handler. Roughly equivalent to the nsIHandlerInfo interface.
*
* FIXME: the type info record should be merged into the type record,
* since there's a one to one relationship between them, and this record
* merely stores additional attributes of a content type.
*
* |urn:(mimetype|scheme):handler:<type>|
*
* @param aHandlerInfo {nsIHandlerInfo} the type for which to get the ID
*/
_getInfoID: function HS__getInfoID(aHandlerInfo) {
// FIXME: once nsIHandlerInfo supports retrieving the scheme
// (and differentiating between MIME and protocol content types),
// implement support for protocols.
var id = "urn:mimetype:handler:" + this._getType(aHandlerInfo);
return id;
},
/**
* Return the unique identifier for a preferred handler record, which stores
* information about the preferred handler for a given content type, including
* its human-readable name and the path to its executable (for a local app)
* or its URI template (for a web app).
*
* |urn:(mimetype|scheme):externalApplication:<type>|
*
* FIXME: this should be a property of nsIHandlerApp.
*
* FIXME: this should be an arbitrary ID, and we should retrieve it from
* the datastore for a given content type via the NC:ExternalApplication
* property rather than looking for a specific ID, so a handler doesn't
* have to change IDs when it goes from being a possible handler to being
* the preferred one.
*
* @param aHandlerInfo {nsIHandlerInfo} the type for which to get the ID
*/
_getPreferredHandlerID: function HS__getPreferredHandlerID(aHandlerInfo) {
// FIXME: once nsIHandlerInfo supports retrieving the scheme
// (and differentiating between MIME and protocol content types),
// implement support for protocols.
var id = "urn:mimetype:externalApplication:" + this._getType(aHandlerInfo);
return id;
},
_ensureAndGetTypeList: function HS__ensureAndGetTypeList(aHandlerInfo) {
// FIXME: once nsIHandlerInfo supports retrieving the scheme
// (and differentiating between MIME and protocol content types),
// implement support for protocols.
var source = this._rdf.GetResource("urn:mimetypes");
var property = this._rdf.GetResource(NC_MIME_TYPES);
var target = this._rdf.GetResource("urn:mimetypes:root");
// Make sure we have an arc from the source to the target.
if (!this._ds.HasAssertion(source, property, target, true))
this._ds.Assert(source, property, target, true);
// Make sure the target is a container.
if (!this._containerUtils.IsContainer(this._ds, target))
this._containerUtils.MakeSeq(this._ds, target);
// Get the type list as an RDF container.
var typeList = Cc["@mozilla.org/rdf/container;1"].
createInstance(Ci.nsIRDFContainer);
typeList.Init(this._ds, target);
return typeList;
},
/**
* Make sure there are records in the datasource for the given content type
* by creating them if they don't already exist. We have to do this before
* storing any specific data, because we can't assume the presence
* of the records (the nsIHandlerInfo object might have been created
* from the OS), and the records have to all be there in order for the helper
* app service to properly construct an nsIHandlerInfo object for the type.
*
* Based on old downloadactions.js::_ensureMIMERegistryEntry.
*
* @param aHandlerInfo {nsIHandlerInfo} the type to make sure has a record
*/
_ensureRecordsForType: function HS__ensureRecordsForType(aHandlerInfo) {
// Get the list of types.
var typeList = this._ensureAndGetTypeList(aHandlerInfo);
// If there's already a record in the datastore for this type, then we
// don't need to do anything more.
var typeID = this._getTypeID(aHandlerInfo);
var type = this._rdf.GetResource(typeID);
if (typeList.IndexOf(type) != -1)
return;
// Create a basic type record for this type.
typeList.AppendElement(type);
this._setLiteral(typeID, NC_EDITABLE, "true");
this._setLiteral(typeID, NC_VALUE, this._getType(aHandlerInfo));
// Create a basic info record for this type.
var infoID = this._getInfoID(aHandlerInfo);
this._setLiteral(infoID, NC_ALWAYS_ASK, "false");
this._setResource(typeID, NC_HANDLER_INFO, infoID);
// XXX Shouldn't we set preferredAction to useSystemDefault?
// That's what it is if there's no record in the datastore; why should it
// change to useHelperApp just because we add a record to the datastore?
// Create a basic preferred handler record for this type.
// XXX Not sure this is necessary, since preferred handlers are optional,
// and nsExternalHelperAppService::FillHandlerInfoForTypeFromDS doesn't seem
// to require the record , but downloadactions.js::_ensureMIMERegistryEntry
// used to create it, so we'll do the same.
var preferredHandlerID = this._getPreferredHandlerID(aHandlerInfo);
this._setLiteral(preferredHandlerID, NC_PATH, "");
this._setResource(infoID, NC_PREFERRED_APP, preferredHandlerID);
},
/**
* Set a property of an RDF source to a literal value.
*
* @param sourceURI {string} the URI of the source
* @param propertyURI {string} the URI of the property
* @param value {string} the literal value
*/
_setLiteral: function HS__setLiteral(sourceURI, propertyURI, value) {
var source = this._rdf.GetResource(sourceURI);
var property = this._rdf.GetResource(propertyURI);
var target = this._rdf.GetLiteral(value);
this._setTarget(source, property, target);
},
/**
* Set a property of an RDF source to a resource.
*
* @param sourceURI {string} the URI of the source
* @param propertyURI {string} the URI of the property
* @param resourceURI {string} the URI of the resource
*/
_setResource: function HS__setResource(sourceURI, propertyURI, resourceURI) {
var source = this._rdf.GetResource(sourceURI);
var property = this._rdf.GetResource(propertyURI);
var target = this._rdf.GetResource(resourceURI);
this._setTarget(source, property, target);
},
/**
* Assert an arc into the RDF datasource if there is no arc with the given
* source and property; otherwise, if there is already an existing arc,
* change it to point to the given target.
*
* @param source {nsIRDFResource} the source
* @param property {nsIRDFResource} the property
* @param value {nsIRDFNode} the target
*/
_setTarget: function HS__setTarget(source, property, target) {
if (this._ds.hasArcOut(source, property)) {
var oldTarget = this._ds.GetTarget(source, property, true);
this._ds.Change(source, property, oldTarget, target);
}
else
this._ds.Assert(source, property, target, true);
},
/**
* Remove a property of an RDF source.
*
* @param sourceURI {string} the URI of the source
* @param propertyURI {string} the URI of the property
*/
_removeValue: function HS__removeValue(sourceURI, propertyURI) {
var source = this._rdf.GetResource(sourceURI);
var property = this._rdf.GetResource(propertyURI);
if (this._ds.hasArcOut(source, property)) {
var target = this._ds.GetTarget(source, property, true);
this._ds.Unassert(source, property, target, true);
}
},
//**************************************************************************//
// Utilities
// FIXME: given that I keep copying them from JS component to JS component,
// these utilities should all be in a JavaScript module or FUEL interface.
/**
* Get an app pref or a default value if the pref doesn't exist.
*
* @param aPrefName
* @param aDefaultValue
* @returns the pref's value or the default (if it is missing)
*/
_getAppPref: function _getAppPref(aPrefName, aDefaultValue) {
try {
var prefBranch = Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch);
switch (prefBranch.getPrefType(aPrefName)) {
case prefBranch.PREF_STRING:
return prefBranch.getCharPref(aPrefName);
case prefBranch.PREF_INT:
return prefBranch.getIntPref(aPrefName);
case prefBranch.PREF_BOOL:
return prefBranch.getBoolPref(aPrefName);
}
}
catch (ex) { /* return the default value */ }
return aDefaultValue;
},
// Console Service
__consoleSvc: null,
get _consoleSvc() {
if (!this.__consoleSvc)
this.__consoleSvc = Cc["@mozilla.org/consoleservice;1"].
getService(Ci.nsIConsoleService);
return this.__consoleSvc;
},
_log: function _log(aMessage) {
if (!this._getAppPref("browser.contentHandling.log", false))
return;
aMessage = "*** HandlerService: " + aMessage;
dump(aMessage + "\n");
this._consoleSvc.logStringMessage(aMessage);
}
};
//****************************************************************************//
// More XPCOM Plumbing
function NSGetModule(compMgr, fileSpec) {
return XPCOMUtils.generateModule([HandlerService]);
}

View File

@ -0,0 +1,57 @@
/* ***** 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 Mozilla browser.
*
* The Initial Developer of the Original Code is Mozilla.
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Myk Melez <myk@mozilla.org>
*
* 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.
*
* ***** END LICENSE BLOCK ***** */
#include "nsISupports.idl"
interface nsIHandlerInfo;
[scriptable, uuid(7c754635-139b-4d80-894e-6c71594ceb44)]
interface nsIHandlerService : nsISupports
{
/**
* Save the preferred action, preferred handler, and always ask properties
* of the given handler info object to the datastore. Updates an existing
* record or creates a new one if necessary.
*
* Note: if preferred action is undefined or invalid, then we assume
* the default value nsIHandlerInfo::useHelperApp.
*
* FIXME: also store any changes to the list of possible handlers.
*
* @param aHandlerInfo {nsIHandlerInfo} the handler info object
*/
void store(in nsIHandlerInfo aHandlerInfo);
};

View File

@ -0,0 +1,51 @@
#
# ***** 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 Mozilla browser.
#
# The Initial Developer of the Original Code is Mozilla.
# Portions created by the Initial Developer are Copyright (C) 2007
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Myk Melez <myk@mozilla.org>
#
# Alternatively, the contents of this file may be used under the terms of
# either of 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.
#
# ***** END LICENSE BLOCK *****
DEPTH = ../../..
topsrcdir = @top_srcdir@
srcdir = @srcdir@
VPATH = @srcdir@
include $(DEPTH)/config/autoconf.mk
MODULE = test_uriloader_exthandler
ifdef MOZ_PHOENIX
XPCSHELL_TESTS = unit
endif
include $(topsrcdir)/config/rules.mk

View File

@ -0,0 +1,153 @@
/* ***** 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 Mozilla browser.
*
* The Initial Developer of the Original Code is Mozilla.
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Myk Melez <myk@mozilla.org>
*
* 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.
*
* ***** END LICENSE BLOCK ***** */
// Inspired by the Places infrastructure in head_bookmarks.js
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
const Cu = Components.utils;
var HandlerServiceTest = {
//**************************************************************************//
// Convenience Getters
__dirSvc: null,
get _dirSvc() {
if (!this.__dirSvc)
this.__dirSvc = Cc["@mozilla.org/file/directory_service;1"].
getService(Ci.nsIProperties);
return this.__dirSvc;
},
__consoleSvc: null,
get _consoleSvc() {
if (!this.__consoleSvc)
this.__consoleSvc = Cc["@mozilla.org/consoleservice;1"].
getService(Ci.nsIConsoleService);
return this.__consoleSvc;
},
//**************************************************************************//
// nsISupports
interfaces: [Ci.nsIDirectoryServiceProvider, Ci.nsISupports],
QueryInterface: function HandlerServiceTest_QueryInterface(iid) {
if (!this.interfaces.some( function(v) { return iid.equals(v) } ))
throw Cr.NS_ERROR_NO_INTERFACE;
return this;
},
//**************************************************************************//
// nsIDirectoryServiceProvider
getFile: function HandlerServiceTest_getFile(property, persistent) {
this.log("getFile: requesting " + property);
persistent.value = true;
if (property == "UMimTyp") {
var datasourceFile = this._dirSvc.get("CurProcD", Ci.nsIFile);
datasourceFile.append("mimeTypes.rdf");
return datasourceFile;
}
// This causes extraneous errors to show up in the log when the directory
// service asks us first for CurProcD and MozBinD. I wish there was a way
// to suppress those errors.
this.log("the following NS_ERROR_FAILURE exception in " +
"nsIDirectoryServiceProvider::getFile is expected, " +
"as we don't provide the '" + property + "' file");
throw Cr.NS_ERROR_FAILURE;
},
//**************************************************************************//
// Utilities
/**
* Get the datasource file, registering ourselves as a provider
* of that directory if necessary.
*/
getDatasourceFile: function HandlerServiceTest_getDatasourceFile() {
var datasourceFile;
try {
datasourceFile = this._dirSvc.get("UMimTyp", Ci.nsIFile);
}
catch (e) {}
if (!datasourceFile) {
this._dirSvc.QueryInterface(Ci.nsIDirectoryService).registerProvider(this);
datasourceFile = this._dirSvc.get("UMimTyp", Ci.nsIFile);
}
return datasourceFile;
},
deleteDatasourceFile: function HandlerServiceTest_deleteDatasourceFile() {
try {
var file = this.getDatasourceFile();
if (file.exists())
file.remove(false);
}
catch(ex) {
Cu.reportError(ex);
}
},
/**
* Log a message to the console and the test log.
*/
log: function HandlerServiceTest_log(message) {
message = "*** HandlerServiceTest: " + message;
this._consoleSvc.logStringMessage(message);
print(message);
}
};
HandlerServiceTest.getDatasourceFile();
//HandlerServiceTest.deleteDatasourceFile();
//HandlerServiceTest.createDatasourceFile();
// Turn on logging so we can troubleshoot problems with the tests.
var prefBranch = Cc["@mozilla.org/preferences-service;1"].
getService(Ci.nsIPrefBranch);
prefBranch.setBoolPref("browser.contentHandling.log", true);

View File

@ -0,0 +1,35 @@
/* ***** 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 Mozilla browser.
*
* The Initial Developer of the Original Code is Mozilla.
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Myk Melez <myk@mozilla.org>
*
* 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.
*
* ***** END LICENSE BLOCK ***** */

View File

@ -0,0 +1,123 @@
/* ***** 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 Content Preferences (cpref).
*
* The Initial Developer of the Original Code is Mozilla.
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Myk Melez <myk@mozilla.org>
*
* 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.
*
* ***** END LICENSE BLOCK ***** */
function run_test() {
// It doesn't matter whether or not this executable exists or is executable,
// only that it'll QI to nsIFile and has a path attribute, which the service
// expects.
var executable = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
executable.initWithPath("/usr/bin/test");
var localHandler = {
name: "Local Handler",
executable: executable,
interfaces: [Ci.nsIHandlerApp, Ci.nsILocalHandlerApp, Ci.nsISupports],
QueryInterface: function(iid) {
if (!this.interfaces.some( function(v) { return iid.equals(v) } ))
throw Cr.NS_ERROR_NO_INTERFACE;
return this;
}
};
var webHandler = {
name: "Web Handler",
uriTemplate: "http://www.example.com/?%s",
interfaces: [Ci.nsIHandlerApp, Ci.nsIWebHandlerApp, Ci.nsISupports],
QueryInterface: function(iid) {
if (!this.interfaces.some( function(v) { return iid.equals(v) } ))
throw Cr.NS_ERROR_NO_INTERFACE;
return this;
}
};
var handlerSvc = Cc["@mozilla.org/uriloader/handler-service;1"].
getService(Ci.nsIHandlerService);
var mimeSvc = Cc["@mozilla.org/uriloader/external-helper-app-service;1"].
getService(Ci.nsIMIMEService);
//**************************************************************************//
// Default Properties
// Get a handler info for a MIME type that neither the application nor
// the OS knows about and make sure its properties are set to the proper
// default values.
var handlerInfo = mimeSvc.getFromTypeAndExtension("nonexistent/type", null);
do_check_eq(handlerInfo.MIMEType, "nonexistent/type");
// These three properties are the ones the handler service knows how to store.
do_check_eq(handlerInfo.preferredAction, Ci.nsIHandlerInfo.saveToDisk);
do_check_eq(handlerInfo.preferredApplicationHandler, null);
do_check_true(handlerInfo.alwaysAskBeforeHandling);
do_check_eq(handlerInfo.description, "");
do_check_eq(handlerInfo.hasDefaultHandler, false);
do_check_eq(handlerInfo.defaultDescription, "");
//**************************************************************************//
// Round-Trip Data Integrity
// Test round-trip data integrity by setting the properties of the handler
// info object to different values, telling the handler service to store the
// object, and then retrieving a new info object for the same type and making
// sure its properties are identical.
handlerInfo.preferredAction = Ci.nsIHandlerInfo.useHelperApp;
handlerInfo.preferredApplicationHandler = localHandler;
handlerInfo.alwaysAskBeforeHandling = false;
handlerSvc.store(handlerInfo);
handlerInfo = mimeSvc.getFromTypeAndExtension("nonexistent/type", null);
do_check_eq(handlerInfo.preferredAction, Ci.nsIHandlerInfo.useHelperApp);
do_check_neq(handlerInfo.preferredApplicationHandler, null);
var preferredHandler = handlerInfo.preferredApplicationHandler;
do_check_eq(typeof preferredHandler, "object");
do_check_eq(preferredHandler.name, "Local Handler");
var localHandler = preferredHandler.QueryInterface(Ci.nsILocalHandlerApp);
do_check_eq(localHandler.executable.path, "/usr/bin/test");
do_check_false(handlerInfo.alwaysAskBeforeHandling);
// FIXME: test round trip integrity for a protocol.
// FIXME: test round trip integrity for a handler info with a web handler.
}