bug 388388: differentiate between MIME and protocol nsIHandlerInfo objects by not allowing protocol objects to be QIed to nsIMIMEInfo, and expose the scheme for a protocol object via the new nsIHandlerInfo::type attribute (which also holds the MIME type for a MIME object and deprecates nsIMIMEInfo::MIMEType); r=dmose, sr=biesi

This commit is contained in:
myk@mozilla.org 2007-07-24 13:29:50 -07:00
parent c89dd52612
commit 388545f1a3
13 changed files with 86 additions and 20 deletions

View File

@ -49,8 +49,16 @@ typedef long nsHandlerInfoAction;
* nsIHandlerInfo gives access to the information about how a given protocol
* scheme or MIME-type is handled.
*/
[scriptable, uuid(2ec1216d-59e7-424c-aa1e-70aa3c897520)]
[scriptable, uuid(51ddc1c5-7077-4c58-a5bd-327d00777b46)]
interface nsIHandlerInfo : nsISupports {
/**
* The type of this handler info. For MIME handlers, this is the MIME type.
* For protocol handlers, it's the scheme.
*
* @return String representing the type.
*/
readonly attribute ACString type;
/**
* A human readable description of the handler type
*/
@ -158,6 +166,8 @@ interface nsIMIMEInfo : nsIHandlerInfo {
* The MIME type of this MIMEInfo.
*
* @return String representing the MIME type.
*
* @deprecated use nsIHandlerInfo::type instead.
*/
readonly attribute ACString MIMEType;

View File

@ -43,6 +43,8 @@ class nsMIMEInfoBeOS : public nsMIMEInfoImpl {
public:
nsMIMEInfoBeOS(const char* aType = "") : nsMIMEInfoImpl(aType) {}
nsMIMEInfoBeOS(const nsACString& aMIMEType) : nsMIMEInfoImpl(aMIMEType) {}
nsMIMEInfoBeOS(const nsACString& aType, HandlerClass aClass) :
nsMIMEInfoImpl(aType, aClass) {}
virtual ~nsMIMEInfoBeOS();
protected:

View File

@ -275,7 +275,8 @@ nsOSHelperAppService::GetProtocolInfoFromOS(const nsACString &aScheme)
&exists);
NS_ENSURE_SUCCESS(rv, nsnull);
nsMIMEInfoBeOS *handlerInfo = new nsMIMEInfoBeOS();
nsMIMEInfoBeOS *handlerInfo =
new nsMIMEInfoBeOS(aScheme, nsMIMEInfoBase::eProtocolInfo);
NS_ENSURE_TRUE(handlerInfo, nsnull);
NS_ADDREF(handlerInfo);

View File

@ -43,6 +43,8 @@ class nsMIMEInfoMac : public nsMIMEInfoImpl {
public:
nsMIMEInfoMac(const char* aMIMEType = "") : nsMIMEInfoImpl(aMIMEType) {}
nsMIMEInfoMac(const nsACString& aMIMEType) : nsMIMEInfoImpl(aMIMEType) {}
nsMIMEInfoMac(const nsACString& aType, HandlerClass aClass) :
nsMIMEInfoImpl(aType, aClass) {}
NS_IMETHOD LaunchWithURI(nsIURI* aURI);

View File

@ -343,7 +343,8 @@ nsOSHelperAppService::GetProtocolInfoFromOS(const nsACString &aScheme)
&exists);
NS_ENSURE_SUCCESS(rv, nsnull);
nsMIMEInfoMac *handlerInfo = new nsMIMEInfoMac();
nsMIMEInfoMac *handlerInfo =
new nsMIMEInfoMac(aScheme, nsMIMEInfoBase::eProtocolInfo);
NS_ENSURE_TRUE(handlerInfo, nsnull);
NS_ADDREF(handlerInfo);

View File

@ -244,9 +244,7 @@ HandlerService.prototype = {
* @returns {string} the ID
*/
_getClass: function HS__getClass(aHandlerInfo) {
if (aHandlerInfo instanceof Ci.nsIMIMEInfo &&
// FIXME: remove this extra condition in the fix for bug 388388.
aHandlerInfo.QueryInterface(Ci.nsIMIMEInfo).MIMEType)
if (aHandlerInfo instanceof Ci.nsIMIMEInfo)
return "mimetype";
else
return "scheme";
@ -265,10 +263,7 @@ HandlerService.prototype = {
* @returns {string} the ID
*/
_getTypeID: function HS__getTypeID(aHandlerInfo) {
return "urn:" + this._getClass(aHandlerInfo) + ":" +
// FIXME: change this to aHandlerInfo.type in the fix for bug 388388.
aHandlerInfo.QueryInterface(Ci.nsIMIMEInfo).MIMEType;
return "urn:" + this._getClass(aHandlerInfo) + ":" + aHandlerInfo.type;
},
/**
@ -288,8 +283,7 @@ HandlerService.prototype = {
*/
_getInfoID: function HS__getInfoID(aHandlerInfo) {
return "urn:" + this._getClass(aHandlerInfo) + ":handler:" +
// FIXME: change this to aHandlerInfo.type in the fix for bug 388388.
aHandlerInfo.QueryInterface(Ci.nsIMIMEInfo).MIMEType;
aHandlerInfo.type;
},
/**
@ -314,8 +308,7 @@ HandlerService.prototype = {
*/
_getPreferredHandlerID: function HS__getPreferredHandlerID(aHandlerInfo) {
return "urn:" + this._getClass(aHandlerInfo) + ":externalApplication:" +
// FIXME: change this to aHandlerInfo.type in the fix for bug 388388.
aHandlerInfo.QueryInterface(Ci.nsIMIMEInfo).MIMEType;
aHandlerInfo.type;
},
/**
@ -383,9 +376,7 @@ HandlerService.prototype = {
// Create a basic type record for this type.
typeList.AppendElement(type);
this._setLiteral(typeID, NC_EDITABLE, "true");
this._setLiteral(typeID, NC_VALUE,
// FIXME: change this to aHandlerInfo.type in the fix for bug 388388.
aHandlerInfo.QueryInterface(Ci.nsIMIMEInfo).MIMEType);
this._setLiteral(typeID, NC_VALUE, aHandlerInfo.type);
// Create a basic info record for this type.
var infoID = this._getInfoID(aHandlerInfo);

View File

@ -45,12 +45,23 @@
#include "nsIFileURL.h"
// nsISupports methods
NS_IMPL_THREADSAFE_ISUPPORTS2(nsMIMEInfoBase, nsIMIMEInfo, nsIHandlerInfo)
NS_IMPL_THREADSAFE_ADDREF(nsMIMEInfoBase)
NS_IMPL_THREADSAFE_RELEASE(nsMIMEInfoBase)
NS_INTERFACE_MAP_BEGIN(nsMIMEInfoBase)
NS_INTERFACE_MAP_ENTRY(nsIHandlerInfo)
// This is only an nsIMIMEInfo if it's a MIME handler with a MIME type.
NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsIMIMEInfo, !mMIMEType.IsEmpty())
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIHandlerInfo)
NS_INTERFACE_MAP_END_THREADSAFE
// nsMIMEInfoImpl methods
// Constructors for a MIME handler.
nsMIMEInfoBase::nsMIMEInfoBase(const char *aMIMEType) :
mMacType(0),
mMacCreator(0),
mType(aMIMEType),
mMIMEType(aMIMEType),
mPreferredAction(nsIMIMEInfo::saveToDisk),
mAlwaysAskBeforeHandling(PR_TRUE)
@ -60,12 +71,29 @@ nsMIMEInfoBase::nsMIMEInfoBase(const char *aMIMEType) :
nsMIMEInfoBase::nsMIMEInfoBase(const nsACString& aMIMEType) :
mMacType(0),
mMacCreator(0),
mType(aMIMEType),
mMIMEType(aMIMEType),
mPreferredAction(nsIMIMEInfo::saveToDisk),
mAlwaysAskBeforeHandling(PR_TRUE)
{
}
// Constructor for a handler that lets the caller specify whether this is a
// MIME handler or a protocol handler. In the long run, these will be distinct
// classes (f.e. nsMIMEInfo and nsProtocolInfo), but for now we reuse this class
// for both and distinguish between the two kinds of handlers via the aClass
// argument to this method, which can be either eMIMEInfo or eProtocolInfo.
nsMIMEInfoBase::nsMIMEInfoBase(const nsACString& aType, HandlerClass aClass) :
mMacType(0),
mMacCreator(0),
mType(aType),
mPreferredAction(nsIMIMEInfo::saveToDisk),
mAlwaysAskBeforeHandling(PR_TRUE)
{
if (aClass == eMIMEInfo)
mMIMEType = aType;
}
nsMIMEInfoBase::~nsMIMEInfoBase()
{
}
@ -136,6 +164,16 @@ nsMIMEInfoBase::AppendExtension(const nsACString& aExtension)
return NS_OK;
}
NS_IMETHODIMP
nsMIMEInfoBase::GetType(nsACString& aType)
{
if (mType.IsEmpty())
return NS_ERROR_NOT_INITIALIZED;
aType = mType;
return NS_OK;
}
NS_IMETHODIMP
nsMIMEInfoBase::GetMIMEType(nsACString& aMIMEType)
{

View File

@ -74,6 +74,7 @@ class nsMIMEInfoBase : public nsIMIMEInfo {
NS_IMETHOD AppendExtension(const nsACString & aExtension);
NS_IMETHOD GetPrimaryExtension(nsACString & aPrimaryExtension);
NS_IMETHOD SetPrimaryExtension(const nsACString & aPrimaryExtension);
NS_IMETHOD GetType(nsACString & aType);
NS_IMETHOD GetMIMEType(nsACString & aMIMEType);
NS_IMETHOD GetDescription(nsAString & aDescription);
NS_IMETHOD SetDescription(const nsAString & aDescription);
@ -91,11 +92,19 @@ class nsMIMEInfoBase : public nsIMIMEInfo {
NS_IMETHOD GetAlwaysAskBeforeHandling(PRBool *aAlwaysAskBeforeHandling);
NS_IMETHOD SetAlwaysAskBeforeHandling(PRBool aAlwaysAskBeforeHandling);
enum HandlerClass {
eMIMEInfo,
eProtocolInfo
};
// nsMIMEInfoBase methods
nsMIMEInfoBase(const char *aMIMEType = "") NS_HIDDEN;
nsMIMEInfoBase(const nsACString& aMIMEType) NS_HIDDEN;
nsMIMEInfoBase(const nsACString& aType, HandlerClass aClass) NS_HIDDEN;
virtual ~nsMIMEInfoBase(); // must be virtual, as the the base class's Release should call the subclass's destructor
void SetType(const nsACString & aType) { mType = aType; }
void SetMIMEType(const nsACString & aMIMEType) { mMIMEType = aMIMEType; }
void SetDefaultDescription(const nsString& aDesc) { mDefaultAppDescription = aDesc; }
@ -158,6 +167,7 @@ class nsMIMEInfoBase : public nsIMIMEInfo {
nsCStringArray mExtensions; ///< array of file extensions associated w/ this MIME obj
nsString mDescription; ///< human readable description
PRUint32 mMacType, mMacCreator; ///< Mac file type and creator
nsCString mType;
nsCString mMIMEType;
nsCOMPtr<nsIHandlerApp> mPreferredApplication;
nsHandlerInfoAction mPreferredAction; ///< preferred action to associate with this type
@ -179,6 +189,8 @@ class nsMIMEInfoImpl : public nsMIMEInfoBase {
public:
nsMIMEInfoImpl(const char *aMIMEType = "") : nsMIMEInfoBase(aMIMEType) {}
nsMIMEInfoImpl(const nsACString& aMIMEType) : nsMIMEInfoBase(aMIMEType) {}
nsMIMEInfoImpl(const nsACString& aType, HandlerClass aClass) :
nsMIMEInfoBase(aType, aClass) {}
virtual ~nsMIMEInfoImpl() {}
// nsIMIMEInfo methods

View File

@ -44,6 +44,8 @@ class nsMIMEInfoOS2 : public nsMIMEInfoImpl
public:
nsMIMEInfoOS2(const char* aType = "") : nsMIMEInfoImpl(aType) {}
nsMIMEInfoOS2(const nsACString& aMIMEType) : nsMIMEInfoImpl(aMIMEType) {}
nsMIMEInfoOS2(const nsACString& aType, HandlerClass aClass) :
nsMIMEInfoImpl(aType, aClass) {}
virtual ~nsMIMEInfoOS2();
NS_IMETHOD LaunchWithURI(nsIURI* aURI);

View File

@ -83,6 +83,9 @@ function run_test() {
// Make sure it's also an nsIHandlerInfo.
do_check_true(handlerInfo instanceof Ci.nsIHandlerInfo);
do_check_eq(handlerInfo.type, "nonexistent/type");
// Deprecated property, but we should still make sure it's set correctly.
do_check_eq(handlerInfo.MIMEType, "nonexistent/type");
// These three properties are the ones the handler service knows how to store.

View File

@ -1683,7 +1683,8 @@ nsOSHelperAppService::GetProtocolInfoFromOS(const nsACString &aScheme)
&exists);
NS_ENSURE_SUCCESS(rv, nsnull);
nsMIMEInfoImpl *handlerInfo = new nsMIMEInfoImpl();
nsMIMEInfoImpl *handlerInfo =
new nsMIMEInfoImpl(aScheme, nsMIMEInfoBase::eProtocolInfo);
NS_ENSURE_TRUE(handlerInfo, nsnull);
NS_ADDREF(handlerInfo);

View File

@ -44,6 +44,8 @@ class nsMIMEInfoWin : public nsMIMEInfoBase, public nsIPropertyBag {
public:
nsMIMEInfoWin(const char* aType = "") : nsMIMEInfoBase(aType) {}
nsMIMEInfoWin(const nsACString& aMIMEType) : nsMIMEInfoBase(aMIMEType) {}
nsMIMEInfoWin(const nsACString& aType, HandlerClass aClass) :
nsMIMEInfoImpl(aType, aClass) {}
virtual ~nsMIMEInfoWin();
NS_IMETHOD GetHasDefaultHandler(PRBool * _retval);

View File

@ -598,7 +598,8 @@ nsOSHelperAppService::GetProtocolInfoFromOS(const nsACString &aScheme)
&exists);
NS_ENSURE_SUCCESS(rv, nsnull);
nsMIMEInfoWin *handlerInfo = new nsMIMEInfoWin();
nsMIMEInfoWin *handlerInfo =
new nsMIMEInfoWin(aScheme, nsMIMEInfoBase::eProtocolInfo);
NS_ENSURE_TRUE(handlerInfo, nsnull);
NS_ADDREF(handlerInfo);