factored repeated mime type retrieval implementations into mime mapping service methods
git-svn-id: svn://10.0.0.236/trunk@41758 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
parent
e5ae1ba132
commit
a01b026133
@ -22,6 +22,7 @@
|
||||
|
||||
#include "nsISupports.idl"
|
||||
#include "nsIMIMEInfo.idl"
|
||||
#include "nsIURI.idl"
|
||||
|
||||
|
||||
%{C++
|
||||
@ -41,6 +42,11 @@ interface nsIMIMEService : nsISupports {
|
||||
// return a nsIMIMEInfo for the given file extension.
|
||||
nsIMIMEInfo GetFromExtension(in string aFileExt);
|
||||
|
||||
// return a string representing the content-type
|
||||
void GetTypeFromExtension(in string aFileExt, out string aContentType);
|
||||
|
||||
void GetTypeFromURI(in nsIURI aURI, out string aContentType);
|
||||
|
||||
// return a nsIMIMEInfo for the given MIME type.
|
||||
nsIMIMEInfo GetFromMIMEType(in string aMIMEType);
|
||||
|
||||
|
||||
@ -32,6 +32,7 @@ nsMIMEInfoImpl::nsMIMEInfoImpl(const char *aMIMEType, const char *aFileExtension
|
||||
}
|
||||
|
||||
nsMIMEInfoImpl::~nsMIMEInfoImpl() {
|
||||
NS_RELEASE(mMIMEType);
|
||||
}
|
||||
|
||||
PRBool
|
||||
@ -75,7 +76,7 @@ nsMIMEInfoImpl::GetMIMEType(char * *aMIMEType) {
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsMIMEInfoImpl::SetMIMEType(char * aMIMEType) {
|
||||
delete mMIMEType;
|
||||
NS_RELEASE(mMIMEType);
|
||||
mMIMEType = NS_NewAtom(aMIMEType);
|
||||
if (!mMIMEType) return NS_ERROR_OUT_OF_MEMORY;
|
||||
return NS_OK;
|
||||
|
||||
@ -23,6 +23,7 @@
|
||||
#include "nsMIMEInfoImpl.h"
|
||||
#include "nsIMIMEInfo.h"
|
||||
#include "nsIFileSpec.h"
|
||||
#include "nsIURL.h"
|
||||
|
||||
PRBool DeleteEntry(nsHashKey *aKey, void *aData, void* closure) {
|
||||
nsMIMEInfoImpl *entry = (nsMIMEInfoImpl*)aData;
|
||||
@ -204,6 +205,52 @@ nsMIMEService::GetFromExtension(const char *aFileExt, nsIMIMEInfo **_retval) {
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsMIMEService::GetTypeFromExtension(const char *aFileExt, char **aContentType) {
|
||||
nsresult rv;
|
||||
nsIMIMEInfo *info = nsnull;
|
||||
rv = GetFromExtension(aFileExt, &info);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
|
||||
rv = info->GetMIMEType(aContentType);
|
||||
NS_RELEASE(info);
|
||||
return rv;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsMIMEService::GetTypeFromURI(nsIURI *aURI, char **aContentType) {
|
||||
nsresult rv = NS_ERROR_FAILURE;
|
||||
char *cStrSpec= nsnull;
|
||||
nsString2 specStr;
|
||||
|
||||
// first try to get a url out of the uri so we can skip post
|
||||
// filename stuff (i.e. query string)
|
||||
nsIURL *url = nsnull;
|
||||
rv = aURI->QueryInterface(nsCOMTypeInfo<nsIURL>::GetIID(), (void**)&url);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
rv = url->GetFileName(&cStrSpec);
|
||||
NS_RELEASE(url);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
} else {
|
||||
// no url, let's give the raw spec a shot
|
||||
rv = aURI->GetSpec(&cStrSpec);
|
||||
if (NS_FAILED(rv)) return rv;
|
||||
}
|
||||
|
||||
// find the file extension (if any)
|
||||
specStr.SetString(cStrSpec);
|
||||
nsString2 extStr;
|
||||
PRInt32 extLoc = specStr.RFindChar('.');
|
||||
if (-1 != extLoc) {
|
||||
specStr.Right(extStr, specStr.Length() - extLoc - 1);
|
||||
char *ext = extStr.ToNewCString();
|
||||
if (!ext) return NS_ERROR_OUT_OF_MEMORY;
|
||||
rv = GetTypeFromExtension(ext, aContentType);
|
||||
nsAllocator::Free(ext);
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsMIMEService::GetFromMIMEType(const char *aMIMEType, nsIMIMEInfo **_retval) {
|
||||
|
||||
|
||||
@ -40,6 +40,8 @@ class nsMIMEService : public nsIMIMEService {
|
||||
|
||||
// nsIMIMEService methods
|
||||
NS_IMETHOD GetFromExtension(const char *aFileExt, nsIMIMEInfo **_retval);
|
||||
NS_IMETHOD GetTypeFromExtension(const char *aFileExt, char **aContentType);
|
||||
NS_IMETHOD GetTypeFromURI(nsIURI *aURI, char **aContentType);
|
||||
NS_IMETHOD GetFromMIMEType(const char *aMIMEType, nsIMIMEInfo **_retval);
|
||||
|
||||
NS_IMETHOD AddMIMEInfo(nsIMIMEInfo *aMIMEInfo);
|
||||
|
||||
@ -32,6 +32,7 @@ nsMIMEInfoImpl::nsMIMEInfoImpl(const char *aMIMEType, const char *aFileExtension
|
||||
}
|
||||
|
||||
nsMIMEInfoImpl::~nsMIMEInfoImpl() {
|
||||
NS_RELEASE(mMIMEType);
|
||||
}
|
||||
|
||||
PRBool
|
||||
@ -75,7 +76,7 @@ nsMIMEInfoImpl::GetMIMEType(char * *aMIMEType) {
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsMIMEInfoImpl::SetMIMEType(char * aMIMEType) {
|
||||
delete mMIMEType;
|
||||
NS_RELEASE(mMIMEType);
|
||||
mMIMEType = NS_NewAtom(aMIMEType);
|
||||
if (!mMIMEType) return NS_ERROR_OUT_OF_MEMORY;
|
||||
return NS_OK;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user