2000-06-17 01:35:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* 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 Netscape
|
|
|
|
* Communications, Inc. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1999, Mozilla. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Scott MacGregor <mscott@netscape.com>
|
2001-05-04 01:24:36 +04:00
|
|
|
* Bill Law <law@netscape.com>
|
2000-06-17 01:35:13 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsExternalHelperAppService.h"
|
|
|
|
#include "nsIURI.h"
|
2000-06-17 04:06:26 +04:00
|
|
|
#include "nsIURL.h"
|
2000-06-17 01:35:13 +04:00
|
|
|
#include "nsIFile.h"
|
2000-06-17 04:06:26 +04:00
|
|
|
#include "nsIChannel.h"
|
2001-03-13 01:41:28 +03:00
|
|
|
#include "nsIFileChannel.h"
|
2000-08-22 04:53:37 +04:00
|
|
|
#include "nsIDirectoryService.h"
|
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
2000-06-17 04:06:26 +04:00
|
|
|
#include "nsXPIDLString.h"
|
2001-09-29 12:28:41 +04:00
|
|
|
#include "nsReadableUtils.h"
|
2000-06-17 23:53:44 +04:00
|
|
|
#include "nsMemory.h"
|
2000-06-17 01:35:13 +04:00
|
|
|
#include "nsIStreamListener.h"
|
2000-06-21 12:29:13 +04:00
|
|
|
#include "nsIMIMEService.h"
|
2000-10-04 10:54:47 +04:00
|
|
|
#include "nsILoadGroup.h"
|
|
|
|
#include "nsCURILoader.h"
|
|
|
|
#include "nsIWebProgress.h"
|
|
|
|
#include "nsIWebProgressListener.h"
|
2000-06-21 12:29:13 +04:00
|
|
|
|
|
|
|
// used to manage our in memory data source of helper applications
|
|
|
|
#include "nsRDFCID.h"
|
|
|
|
#include "rdf.h"
|
|
|
|
#include "nsIRDFService.h"
|
|
|
|
#include "nsIRDFRemoteDataSource.h"
|
|
|
|
#include "nsIFileSpec.h"
|
|
|
|
#include "nsHelperAppRDF.h"
|
|
|
|
#include "nsIMIMEInfo.h"
|
2000-08-12 00:31:57 +04:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
2000-11-01 11:06:19 +03:00
|
|
|
#include "nsIRefreshURI.h"
|
|
|
|
#include "nsIDocumentLoader.h"
|
2000-07-12 10:09:02 +04:00
|
|
|
#include "nsIHelperAppLauncherDialog.h"
|
2001-02-21 23:38:08 +03:00
|
|
|
#include "nsITransport.h"
|
|
|
|
#include "nsIFileTransportService.h"
|
2000-09-14 03:57:52 +04:00
|
|
|
#include "nsCExternalHandlerService.h" // contains contractids for the helper app service
|
2001-07-21 06:54:32 +04:00
|
|
|
#include "nsIIOService.h"
|
|
|
|
#include "nsNetCID.h"
|
2000-06-17 01:35:13 +04:00
|
|
|
|
2000-09-15 10:07:58 +04:00
|
|
|
#include "nsMimeTypes.h"
|
|
|
|
// used for http content header disposition information.
|
2001-05-12 01:05:08 +04:00
|
|
|
#include "nsIHttpChannel.h"
|
2000-09-15 10:07:58 +04:00
|
|
|
#include "nsIAtom.h"
|
2001-07-11 07:23:24 +04:00
|
|
|
#include "nsIObserverService.h" // so we can be an xpcom shutdown observer
|
2000-09-15 10:07:58 +04:00
|
|
|
|
2000-09-07 01:12:11 +04:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#include "nsILocalFileMac.h"
|
2000-09-15 10:07:58 +04:00
|
|
|
#include "nsIInternetConfigService.h"
|
2001-06-26 02:11:43 +04:00
|
|
|
#include "nsIAppleFileDecoder.h"
|
2000-09-07 01:12:11 +04:00
|
|
|
#endif // XP_MAC
|
|
|
|
|
2001-07-25 03:12:29 +04:00
|
|
|
#include "nsIPluginHost.h"
|
2001-09-08 16:47:05 +04:00
|
|
|
#include "nsEscape.h"
|
2001-07-25 03:12:29 +04:00
|
|
|
|
2001-06-06 12:17:39 +04:00
|
|
|
const char *FORCE_ALWAYS_ASK_PREF = "browser.helperApps.alwaysAsk.force";
|
|
|
|
|
2000-06-21 12:29:13 +04:00
|
|
|
static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
|
|
|
|
static NS_DEFINE_CID(kRDFXMLDataSourceCID, NS_RDFXMLDATASOURCE_CID);
|
2001-07-21 06:54:32 +04:00
|
|
|
static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
|
2001-07-25 03:12:29 +04:00
|
|
|
static NS_DEFINE_CID(kPluginManagerCID, NS_PLUGINMANAGER_CID);
|
|
|
|
|
2000-06-21 12:29:13 +04:00
|
|
|
|
2000-09-15 10:07:58 +04:00
|
|
|
// forward declaration of a private helper function
|
2001-03-28 02:59:33 +04:00
|
|
|
static PRBool PR_CALLBACK DeleteEntry(nsHashKey *aKey, void *aData, void* closure);
|
2000-09-15 10:07:58 +04:00
|
|
|
|
|
|
|
// The following static table lists all of the "default" content type mappings we are going to use.
|
2001-05-04 01:24:36 +04:00
|
|
|
// These are only the ones handled internally. These must be listed here so that we can do the
|
|
|
|
// proper file-extension to content-type mapping. These cannot be overridden by user helper app
|
|
|
|
// prefs.
|
2000-09-15 10:07:58 +04:00
|
|
|
static nsDefaultMimeTypeEntry defaultMimeEntries [] =
|
|
|
|
{
|
|
|
|
{ TEXT_PLAIN, "txt,text", "Text File", 'TEXT', 'ttxt' },
|
2001-10-29 08:25:03 +03:00
|
|
|
{ TEXT_HTML, "htm,html,shtml,ehtml", "HyperText Markup Language", 'TEXT', 'MOSS' },
|
2000-09-15 10:07:58 +04:00
|
|
|
{ TEXT_RDF, "rdf", "Resource Description Framework", 'TEXT','ttxt' },
|
|
|
|
{ TEXT_XUL, "xul", "XML-Based User Interface Language", 'TEXT', 'ttxt' },
|
2001-04-26 23:33:12 +04:00
|
|
|
{ TEXT_XML, "xml,xsl", "Extensible Markup Language", 'TEXT', 'ttxt' },
|
|
|
|
{ "application/xhtml+xml", "xhtml,xht", "Extensible HyperText Markup Language", 'TEXT', 'ttxt' },
|
2000-09-15 10:07:58 +04:00
|
|
|
{ TEXT_CSS, "css", "Style Sheet", 'TEXT', 'ttxt' },
|
|
|
|
{ APPLICATION_JAVASCRIPT, "js", "Javascript Source File", 'TEXT', 'ttxt' },
|
2001-04-13 04:44:55 +04:00
|
|
|
{ MESSAGE_RFC822, "eml", "RFC-822 data", 'TEXT', 'MOSS' },
|
|
|
|
{ IMAGE_GIF, "gif", "GIF Image", 0,0 },
|
|
|
|
{ IMAGE_JPG, "jpeg,jpg", "JPEG Image", 0, 0 },
|
|
|
|
{ IMAGE_PNG, "png", "PNG Image", 0, 0 },
|
2001-11-03 10:10:51 +03:00
|
|
|
{ IMAGE_BMP, "bmp", "BMP Image", 0, 0 },
|
|
|
|
{ IMAGE_ICO, "ico", "ICO Image", 0, 0 },
|
2001-06-16 05:10:22 +04:00
|
|
|
{ APPLICATION_XPINSTALL, "xpi", "XPInstall Install", 'xpi*','MOSS' },
|
2001-05-04 01:24:36 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// This table lists all of the 'extra" content types that we can deduce from particular
|
|
|
|
// file extensions. These entries also ensure that we provide a good descriptive name
|
|
|
|
// when we encounter files with these content types and/or extensions. These can be
|
|
|
|
// overridden by user helper app prefs.
|
|
|
|
static nsDefaultMimeTypeEntry extraMimeEntries [] =
|
|
|
|
{
|
|
|
|
#if defined(VMS)
|
|
|
|
{ APPLICATION_OCTET_STREAM, "exe,bin,sav,bck,pcsi,dcx_axpexe,dcx_vaxexe,sfx_axpexe,sfx_vaxexe", "Binary Executable", 0, 0 },
|
|
|
|
#elif defined(XP_MAC) // don't define .bin on the mac...use internet config to look that up...
|
|
|
|
{ APPLICATION_OCTET_STREAM, "exe", "Binary Executable", 0, 0 },
|
|
|
|
#else
|
|
|
|
{ APPLICATION_OCTET_STREAM, "exe,bin", "Binary Executable", 0, 0 },
|
|
|
|
#endif
|
|
|
|
{ APPLICATION_GZIP2, "gz", "gzip", 0, 0 },
|
2001-04-13 04:44:55 +04:00
|
|
|
{ IMAGE_ART, "art", "ART Image", 0, 0 },
|
|
|
|
{ IMAGE_TIFF, "tiff,tif", "TIFF Image", 0, 0 },
|
|
|
|
{ APPLICATION_POSTSCRIPT, "ps,eps,ai", "Postscript File",0, 0 },
|
|
|
|
{ "application/x-arj", "arj", "ARJ file", 0,0 },
|
2000-09-15 10:07:58 +04:00
|
|
|
};
|
|
|
|
|
2000-06-17 01:35:13 +04:00
|
|
|
NS_IMPL_THREADSAFE_ADDREF(nsExternalHelperAppService)
|
|
|
|
NS_IMPL_THREADSAFE_RELEASE(nsExternalHelperAppService)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsExternalHelperAppService)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIExternalHelperAppService)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIExternalHelperAppService)
|
2000-06-18 03:04:53 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsPIExternalAppLauncher)
|
2000-06-19 02:07:54 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIExternalProtocolService)
|
2000-09-08 04:12:51 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIMIMEService)
|
2001-07-11 07:23:24 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIObserver)
|
2000-06-17 01:35:13 +04:00
|
|
|
NS_INTERFACE_MAP_END_THREADSAFE
|
|
|
|
|
2000-09-08 04:12:51 +04:00
|
|
|
nsExternalHelperAppService::nsExternalHelperAppService() : mDataSourceInitialized(PR_FALSE)
|
2000-06-17 01:35:13 +04:00
|
|
|
{
|
|
|
|
NS_INIT_ISUPPORTS();
|
2000-09-15 10:07:58 +04:00
|
|
|
// we need a good guess for a size for our hash table...let's try O(n) where n = # of default
|
|
|
|
// entries we'll be adding to the hash table. Of course, we'll be adding more entries as we
|
|
|
|
// discover those content types at run time...
|
|
|
|
PRInt32 hashTableSize = sizeof(defaultMimeEntries) / sizeof(defaultMimeEntries[0]);
|
|
|
|
mMimeInfoCache = new nsHashtable(hashTableSize);
|
|
|
|
AddDefaultMimeTypesToCache();
|
2001-07-11 07:23:24 +04:00
|
|
|
|
|
|
|
/* Add an observer to XPCOM shutdown */
|
|
|
|
nsresult rv = NS_OK;
|
2001-10-23 02:01:27 +04:00
|
|
|
nsCOMPtr<nsIObserverService> obs = do_GetService("@mozilla.org/observer-service;1", &rv);
|
2001-07-11 07:23:24 +04:00
|
|
|
if (obs)
|
2001-10-20 00:52:59 +04:00
|
|
|
rv = obs->AddObserver(NS_STATIC_CAST(nsIObserver*, this), NS_XPCOM_SHUTDOWN_OBSERVER_ID, PR_FALSE);
|
2001-07-11 07:23:24 +04:00
|
|
|
|
2000-06-17 01:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsExternalHelperAppService::~nsExternalHelperAppService()
|
|
|
|
{
|
2000-09-15 10:07:58 +04:00
|
|
|
if (mMimeInfoCache)
|
|
|
|
{
|
2001-03-28 02:59:33 +04:00
|
|
|
mMimeInfoCache->Reset(DeleteEntry, nsnull);
|
2000-09-15 10:07:58 +04:00
|
|
|
delete mMimeInfoCache;
|
|
|
|
}
|
2000-06-17 01:35:13 +04:00
|
|
|
}
|
|
|
|
|
2000-09-08 04:12:51 +04:00
|
|
|
nsresult nsExternalHelperAppService::InitDataSource()
|
2000-06-21 12:29:13 +04:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
2000-09-08 04:12:51 +04:00
|
|
|
|
|
|
|
// don't re-initialize the data source if we've already done so...
|
|
|
|
if (mDataSourceInitialized)
|
|
|
|
return NS_OK;
|
|
|
|
|
2000-06-21 12:29:13 +04:00
|
|
|
nsCOMPtr<nsIRDFService> rdf = do_GetService(kRDFServiceCID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRDFRemoteDataSource> remoteDS = do_CreateInstance(kRDFXMLDataSourceCID, &rv);
|
|
|
|
mOverRideDataSource = do_QueryInterface(remoteDS);
|
|
|
|
NS_ENSURE_SUCCESS(rv,rv);
|
|
|
|
|
2001-05-04 01:24:36 +04:00
|
|
|
// Get URI of the mimeTypes.rdf data source. Do this the same way it's done in
|
|
|
|
// pref-applications-edit.xul, for example, to ensure we get the same data source!
|
|
|
|
// Note that the way it was done previously (using nsIFileSpec) worked better, but it
|
|
|
|
// produced a different uri (it had "file:///C|" instead of "file:///C:". What can you do?
|
2000-08-22 04:53:37 +04:00
|
|
|
nsCOMPtr<nsIFile> mimeTypesFile;
|
|
|
|
rv = NS_GetSpecialDirectory(NS_APP_USER_MIMETYPES_50_FILE, getter_AddRefs(mimeTypesFile));
|
2000-06-21 12:29:13 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2001-05-04 01:24:36 +04:00
|
|
|
// Get file url spec to be used to initialize the DS.
|
|
|
|
nsXPIDLCString urlSpec;
|
2001-06-22 02:02:47 +04:00
|
|
|
rv = mimeTypesFile->GetURL(getter_Copies(urlSpec));
|
2000-06-23 02:17:25 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2000-06-21 12:29:13 +04:00
|
|
|
|
2001-05-04 01:24:36 +04:00
|
|
|
rv = remoteDS->Init(urlSpec);
|
2000-06-21 12:29:13 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// for now load synchronously (async seems to be busted)
|
|
|
|
rv = remoteDS->Refresh(PR_TRUE);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "failed refresh?\n");
|
|
|
|
|
|
|
|
#ifdef DEBUG_mscott
|
|
|
|
PRBool loaded;
|
|
|
|
rv = remoteDS->GetLoaded(&loaded);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "failed getload\n");
|
2000-10-29 02:17:53 +04:00
|
|
|
printf("After refresh: datasource is %s\n", loaded ? "loaded" : "not loaded");
|
2000-06-21 12:29:13 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// initialize our resources if we haven't done so already...
|
|
|
|
if (!kNC_Description)
|
|
|
|
{
|
|
|
|
rdf->GetResource(NC_RDF_DESCRIPTION, getter_AddRefs(kNC_Description));
|
|
|
|
rdf->GetResource(NC_RDF_VALUE, getter_AddRefs(kNC_Value));
|
|
|
|
rdf->GetResource(NC_RDF_FILEEXTENSIONS,getter_AddRefs(kNC_FileExtensions));
|
|
|
|
rdf->GetResource(NC_RDF_PATH, getter_AddRefs(kNC_Path));
|
|
|
|
rdf->GetResource(NC_RDF_SAVETODISK, getter_AddRefs(kNC_SaveToDisk));
|
|
|
|
rdf->GetResource(NC_RDF_HANDLEINTERNAL,getter_AddRefs(kNC_HandleInternal));
|
|
|
|
rdf->GetResource(NC_RDF_ALWAYSASK, getter_AddRefs(kNC_AlwaysAsk));
|
|
|
|
rdf->GetResource(NC_RDF_PRETTYNAME, getter_AddRefs(kNC_PrettyName));
|
|
|
|
}
|
2000-09-08 04:12:51 +04:00
|
|
|
|
|
|
|
mDataSourceInitialized = PR_TRUE;
|
2000-07-07 05:48:05 +04:00
|
|
|
return rv;
|
2000-06-21 12:29:13 +04:00
|
|
|
}
|
|
|
|
|
2000-06-17 01:35:13 +04:00
|
|
|
/* boolean canHandleContent (in string aMimeContentType); */
|
2000-06-17 04:06:26 +04:00
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::CanHandleContent(const char *aMimeContentType, nsIURI * aURI, PRBool *_retval)
|
2000-06-17 01:35:13 +04:00
|
|
|
{
|
2001-02-07 05:43:18 +03:00
|
|
|
*_retval = PR_FALSE;
|
|
|
|
return NS_OK;
|
2000-06-17 01:35:13 +04:00
|
|
|
}
|
|
|
|
|
2000-06-21 12:29:13 +04:00
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::DoContent(const char *aMimeContentType, nsIURI *aURI, nsISupports *aWindowContext,
|
|
|
|
PRBool *aAbortProcess, nsIStreamListener ** aStreamListener)
|
2000-06-17 01:35:13 +04:00
|
|
|
{
|
2001-02-07 05:43:18 +03:00
|
|
|
nsCOMPtr<nsIMIMEInfo> mimeInfo;
|
|
|
|
nsXPIDLCString fileExtension;
|
2000-09-08 04:12:51 +04:00
|
|
|
|
2001-02-07 05:43:18 +03:00
|
|
|
// (1) Try to find a mime object by looking the mime type
|
2001-03-21 08:32:22 +03:00
|
|
|
GetFromMIMEType(aMimeContentType, getter_AddRefs(mimeInfo));
|
2000-06-21 12:29:13 +04:00
|
|
|
|
2001-05-04 01:24:36 +04:00
|
|
|
if (!mimeInfo)
|
2001-02-07 05:43:18 +03:00
|
|
|
{
|
|
|
|
// if we couldn't find one, don't give up yet! Try and see if there is an extension in the
|
|
|
|
// url itself...
|
|
|
|
nsCOMPtr<nsIURL> url = do_QueryInterface(aURI);
|
|
|
|
if (url)
|
|
|
|
{
|
|
|
|
url->GetFileExtension(getter_Copies(fileExtension));
|
|
|
|
nsCOMPtr<nsIMIMEInfo> tempMIMEObject;
|
|
|
|
GetFromExtension(fileExtension, getter_AddRefs(tempMIMEObject));
|
|
|
|
// only over write mimeInfo if we got a non-null temp mime info object.
|
|
|
|
if (tempMIMEObject)
|
2001-05-04 01:24:36 +04:00
|
|
|
{
|
2001-02-07 05:43:18 +03:00
|
|
|
mimeInfo = tempMIMEObject;
|
2001-05-04 01:24:36 +04:00
|
|
|
// The OS might have thought this extension was a different mime type.
|
|
|
|
// We must reset this to match the actual mime type. Otherwise, we
|
|
|
|
// won't use this MIMEInfo when we see the real mime type next time.
|
2001-05-04 14:26:56 +04:00
|
|
|
//mimeInfo->SetMIMEType(aMimeContentType);
|
2001-05-04 01:24:36 +04:00
|
|
|
}
|
2001-02-07 05:43:18 +03:00
|
|
|
}
|
|
|
|
}
|
2000-06-21 12:29:13 +04:00
|
|
|
|
2001-05-04 01:24:36 +04:00
|
|
|
// (2) if we don't have a match yet, see if this type is in our list of extras.
|
|
|
|
if (!mimeInfo)
|
|
|
|
{
|
|
|
|
GetMIMEInfoForMimeTypeFromExtras(aMimeContentType, getter_AddRefs(mimeInfo));
|
|
|
|
}
|
|
|
|
|
2001-02-07 05:43:18 +03:00
|
|
|
// (3) if we STILL don't have a mime object for this content type then give up
|
|
|
|
// and create a new mime info object for it and use it
|
|
|
|
if (!mimeInfo)
|
2000-06-21 12:29:13 +04:00
|
|
|
{
|
2001-02-07 05:43:18 +03:00
|
|
|
mimeInfo = do_CreateInstance(NS_MIMEINFO_CONTRACTID);
|
|
|
|
if (mimeInfo)
|
|
|
|
{
|
|
|
|
// the file extension was conviently already filled in by our call to FindOSMimeInfoForType.
|
|
|
|
mimeInfo->SetFileExtensions(fileExtension);
|
|
|
|
mimeInfo->SetMIMEType(aMimeContentType);
|
|
|
|
// we may need to add a new method to nsIMIMEService so we can add this mime info object to our mime service.
|
|
|
|
}
|
2000-06-21 12:29:13 +04:00
|
|
|
}
|
|
|
|
|
2001-02-07 05:43:18 +03:00
|
|
|
*aStreamListener = nsnull;
|
|
|
|
if (mimeInfo)
|
|
|
|
{
|
|
|
|
// ensure that the file extension field is always filled in
|
|
|
|
mimeInfo->FirstExtension(getter_Copies(fileExtension));
|
|
|
|
|
|
|
|
// this code is incomplete and just here to get things started..
|
|
|
|
nsExternalAppHandler * handler = CreateNewExternalHandler(mimeInfo, fileExtension, aWindowContext);
|
|
|
|
handler->QueryInterface(NS_GET_IID(nsIStreamListener), (void **) aStreamListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2000-06-17 01:35:13 +04:00
|
|
|
}
|
|
|
|
|
2000-07-07 05:48:05 +04:00
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::LaunchAppWithTempFile(nsIMIMEInfo * aMimeInfo, nsIFile * aTempFile)
|
2000-06-18 03:04:53 +04:00
|
|
|
{
|
|
|
|
// this method should only be implemented by each OS specific implementation of this service.
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2000-07-07 05:48:05 +04:00
|
|
|
nsExternalAppHandler * nsExternalHelperAppService::CreateNewExternalHandler(nsIMIMEInfo * aMIMEInfo,
|
2000-07-11 01:21:10 +04:00
|
|
|
const char * aTempFileExtension,
|
|
|
|
nsISupports * aWindowContext)
|
2000-06-17 04:06:26 +04:00
|
|
|
{
|
|
|
|
nsExternalAppHandler* handler = nsnull;
|
|
|
|
NS_NEWXPCOM(handler, nsExternalAppHandler);
|
|
|
|
// add any XP intialization code for an external handler that we may need here...
|
|
|
|
// right now we don't have any but i bet we will before we are done.
|
2000-06-18 03:04:53 +04:00
|
|
|
|
2000-07-11 01:21:10 +04:00
|
|
|
handler->Init(aMIMEInfo, aTempFileExtension, aWindowContext);
|
2000-06-17 04:06:26 +04:00
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
2000-06-21 12:29:13 +04:00
|
|
|
nsresult nsExternalHelperAppService::FillTopLevelProperties(const char * aContentType, nsIRDFResource * aContentTypeNodeResource,
|
|
|
|
nsIRDFService * aRDFService, nsIMIMEInfo * aMIMEInfo)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsIRDFNode> target;
|
|
|
|
nsCOMPtr<nsIRDFLiteral> literal;
|
|
|
|
const PRUnichar * stringValue;
|
|
|
|
|
2000-09-08 04:12:51 +04:00
|
|
|
rv = InitDataSource();
|
|
|
|
if (NS_FAILED(rv)) return NS_OK;
|
|
|
|
|
2000-06-21 12:29:13 +04:00
|
|
|
// set the mime type
|
|
|
|
aMIMEInfo->SetMIMEType(aContentType);
|
|
|
|
|
|
|
|
// set the pretty name description
|
|
|
|
FillLiteralValueFromTarget(aContentTypeNodeResource,kNC_Description, &stringValue);
|
|
|
|
aMIMEInfo->SetDescription(stringValue);
|
|
|
|
|
|
|
|
// now iterate over all the file type extensions...
|
|
|
|
nsCOMPtr<nsISimpleEnumerator> fileExtensions;
|
|
|
|
mOverRideDataSource->GetTargets(aContentTypeNodeResource, kNC_FileExtensions, PR_TRUE, getter_AddRefs(fileExtensions));
|
|
|
|
|
|
|
|
PRBool hasMoreElements = PR_FALSE;
|
|
|
|
nsCAutoString fileExtension;
|
|
|
|
nsCOMPtr<nsISupports> element;
|
|
|
|
|
|
|
|
if (fileExtensions)
|
|
|
|
{
|
|
|
|
fileExtensions->HasMoreElements(&hasMoreElements);
|
|
|
|
while (hasMoreElements)
|
|
|
|
{
|
|
|
|
fileExtensions->GetNext(getter_AddRefs(element));
|
|
|
|
if (element)
|
|
|
|
{
|
|
|
|
literal = do_QueryInterface(element);
|
2000-10-06 04:58:05 +04:00
|
|
|
if (!literal) return NS_ERROR_FAILURE;
|
|
|
|
|
2000-06-21 12:29:13 +04:00
|
|
|
literal->GetValueConst(&stringValue);
|
|
|
|
fileExtension.AssignWithConversion(stringValue);
|
|
|
|
if (!fileExtension.IsEmpty())
|
2001-10-25 07:49:28 +04:00
|
|
|
aMIMEInfo->AppendExtension(fileExtension.get());
|
2000-06-21 12:29:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
fileExtensions->HasMoreElements(&hasMoreElements);
|
|
|
|
} // while we have more extensions to parse....
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsExternalHelperAppService::FillLiteralValueFromTarget(nsIRDFResource * aSource, nsIRDFResource * aProperty, const PRUnichar ** aLiteralValue)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsIRDFLiteral> literal;
|
|
|
|
nsCOMPtr<nsIRDFNode> target;
|
|
|
|
|
|
|
|
*aLiteralValue = nsnull;
|
2000-09-08 04:12:51 +04:00
|
|
|
rv = InitDataSource();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2000-06-21 12:29:13 +04:00
|
|
|
|
|
|
|
mOverRideDataSource->GetTarget(aSource, aProperty, PR_TRUE, getter_AddRefs(target));
|
|
|
|
if (target)
|
|
|
|
{
|
|
|
|
literal = do_QueryInterface(target);
|
|
|
|
if (!literal)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
literal->GetValueConst(aLiteralValue);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsExternalHelperAppService::FillContentHandlerProperties(const char * aContentType,
|
|
|
|
nsIRDFResource * aContentTypeNodeResource,
|
|
|
|
nsIRDFService * aRDFService,
|
|
|
|
nsIMIMEInfo * aMIMEInfo)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRDFNode> target;
|
|
|
|
nsCOMPtr<nsIRDFLiteral> literal;
|
|
|
|
const PRUnichar * stringValue = nsnull;
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2000-09-08 04:12:51 +04:00
|
|
|
rv = InitDataSource();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2000-06-21 12:29:13 +04:00
|
|
|
nsCString contentTypeHandlerNodeName (NC_CONTENT_NODE_HANDLER_PREFIX);
|
|
|
|
contentTypeHandlerNodeName.Append(aContentType);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRDFResource> contentTypeHandlerNodeResource;
|
2001-10-25 07:49:28 +04:00
|
|
|
aRDFService->GetResource(contentTypeHandlerNodeName.get(), getter_AddRefs(contentTypeHandlerNodeResource));
|
2000-06-22 10:04:23 +04:00
|
|
|
NS_ENSURE_TRUE(contentTypeHandlerNodeResource, NS_ERROR_FAILURE); // that's not good! we have an error in the rdf file
|
2000-06-21 12:29:13 +04:00
|
|
|
|
|
|
|
// now process the application handler information
|
|
|
|
aMIMEInfo->SetPreferredAction(nsIMIMEInfo::useHelperApp);
|
|
|
|
|
|
|
|
// save to disk
|
|
|
|
FillLiteralValueFromTarget(contentTypeHandlerNodeResource,kNC_SaveToDisk, &stringValue);
|
2001-05-04 01:24:36 +04:00
|
|
|
NS_NAMED_LITERAL_STRING( trueString, "true" );
|
2001-11-30 04:10:10 +03:00
|
|
|
if (trueString.Equals(stringValue))
|
2000-06-21 12:29:13 +04:00
|
|
|
aMIMEInfo->SetPreferredAction(nsIMIMEInfo::saveToDisk);
|
|
|
|
|
|
|
|
// handle internal
|
|
|
|
FillLiteralValueFromTarget(contentTypeHandlerNodeResource,kNC_HandleInternal, &stringValue);
|
2001-11-30 04:10:10 +03:00
|
|
|
if (trueString.Equals(stringValue))
|
2000-06-21 12:29:13 +04:00
|
|
|
aMIMEInfo->SetPreferredAction(nsIMIMEInfo::handleInternally);
|
|
|
|
|
2001-07-11 07:23:24 +04:00
|
|
|
// always ask --> these fields aren't stored in the data source anymore
|
|
|
|
//FillLiteralValueFromTarget(contentTypeHandlerNodeResource,kNC_AlwaysAsk, &stringValue);
|
|
|
|
//if (trueString.Equals(stringValue))
|
|
|
|
// aMIMEInfo->SetAlwaysAskBeforeHandling(PR_TRUE);
|
|
|
|
// else
|
|
|
|
// aMIMEInfo->SetAlwaysAskBeforeHandling(PR_FALSE);
|
2000-07-12 03:15:49 +04:00
|
|
|
|
2000-06-21 12:29:13 +04:00
|
|
|
|
|
|
|
// now digest the external application information
|
|
|
|
|
|
|
|
nsCAutoString externalAppNodeName (NC_CONTENT_NODE_EXTERNALAPP_PREFIX);
|
|
|
|
externalAppNodeName.Append(aContentType);
|
|
|
|
nsCOMPtr<nsIRDFResource> externalAppNodeResource;
|
2001-10-25 07:49:28 +04:00
|
|
|
aRDFService->GetResource(externalAppNodeName.get(), getter_AddRefs(externalAppNodeResource));
|
2000-06-21 12:29:13 +04:00
|
|
|
|
2000-06-22 10:04:23 +04:00
|
|
|
if (externalAppNodeResource)
|
2000-06-21 12:29:13 +04:00
|
|
|
{
|
|
|
|
FillLiteralValueFromTarget(externalAppNodeResource, kNC_PrettyName, &stringValue);
|
|
|
|
if (stringValue)
|
|
|
|
aMIMEInfo->SetApplicationDescription(stringValue);
|
|
|
|
|
|
|
|
FillLiteralValueFromTarget(externalAppNodeResource, kNC_Path, &stringValue);
|
|
|
|
if (stringValue)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIFile> application;
|
|
|
|
GetFileTokenForPath(stringValue, getter_AddRefs(application));
|
|
|
|
if (application)
|
|
|
|
aMIMEInfo->SetPreferredApplicationHandler(application);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2000-09-15 10:07:58 +04:00
|
|
|
nsresult nsExternalHelperAppService::GetMIMEInfoForMimeTypeFromDS(const char * aContentType, nsIMIMEInfo ** aMIMEInfo)
|
2000-06-21 12:29:13 +04:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2000-09-08 04:12:51 +04:00
|
|
|
rv = InitDataSource();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2000-06-21 12:29:13 +04:00
|
|
|
// if we have a data source then use the information found in that...
|
|
|
|
// if that fails....then try to the old mime service that i'm going to be
|
|
|
|
// obsoleting soon...
|
|
|
|
if (mOverRideDataSource)
|
|
|
|
{
|
2001-05-04 01:24:36 +04:00
|
|
|
// Get the RDF service.
|
|
|
|
nsCOMPtr<nsIRDFService> rdf = do_GetService(kRDFServiceCID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2000-06-21 12:29:13 +04:00
|
|
|
|
2001-05-04 01:24:36 +04:00
|
|
|
// Build uri for the mimetype resource.
|
2000-06-21 12:29:13 +04:00
|
|
|
nsCString contentTypeNodeName (NC_CONTENT_NODE_PREFIX);
|
|
|
|
contentTypeNodeName.Append(aContentType);
|
|
|
|
|
2001-05-04 01:24:36 +04:00
|
|
|
// Get the mime type resource.
|
|
|
|
nsCOMPtr<nsIRDFResource> contentTypeNodeResource;
|
2001-10-25 07:49:28 +04:00
|
|
|
rv = rdf->GetResource(contentTypeNodeName.get(), getter_AddRefs(contentTypeNodeResource));
|
2000-06-21 12:29:13 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// we need a way to determine if this content type resource is really in the graph or not...
|
2001-05-04 01:24:36 +04:00
|
|
|
// ...Test that there's a #value arc from the mimetype resource to the mimetype literal string.
|
|
|
|
nsCOMPtr<nsIRDFLiteral> mimeLiteral;
|
|
|
|
nsAutoString mimeType; mimeType.AssignWithConversion(aContentType);
|
|
|
|
rv = rdf->GetLiteral( mimeType.get(), getter_AddRefs( mimeLiteral ) );
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
PRBool exists = PR_FALSE;
|
2001-05-04 06:45:37 +04:00
|
|
|
rv = mOverRideDataSource->HasAssertion(contentTypeNodeResource, kNC_Value, mimeLiteral, PR_TRUE, &exists );
|
2000-06-21 12:29:13 +04:00
|
|
|
|
2001-05-04 01:24:36 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && exists)
|
2000-06-21 12:29:13 +04:00
|
|
|
{
|
|
|
|
// create a mime info object and we'll fill it in based on the values from the data source
|
2001-11-30 04:10:10 +03:00
|
|
|
nsCOMPtr<nsIMIMEInfo> mimeInfo (do_CreateInstance(NS_MIMEINFO_CONTRACTID));
|
2000-06-21 12:29:13 +04:00
|
|
|
rv = FillTopLevelProperties(aContentType, contentTypeNodeResource, rdf, mimeInfo);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = FillContentHandlerProperties(aContentType, contentTypeNodeResource, rdf, mimeInfo);
|
|
|
|
|
|
|
|
*aMIMEInfo = mimeInfo;
|
|
|
|
NS_IF_ADDREF(*aMIMEInfo);
|
|
|
|
} // if we have a node in the graph for this content type
|
|
|
|
else
|
|
|
|
*aMIMEInfo = nsnull;
|
|
|
|
} // if we have a data source
|
2000-09-15 10:07:58 +04:00
|
|
|
else
|
|
|
|
rv = NS_ERROR_FAILURE;
|
2000-06-21 12:29:13 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2000-06-19 02:07:54 +04:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// begin external protocol service default implementation...
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::ExternalProtocolHandlerExists(const char * aProtocolScheme,
|
|
|
|
PRBool * aHandlerExists)
|
|
|
|
{
|
|
|
|
// this method should only be implemented by each OS specific implementation of this service.
|
|
|
|
*aHandlerExists = PR_FALSE;
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::LoadUrl(nsIURI * aURL)
|
|
|
|
{
|
|
|
|
// this method should only be implemented by each OS specific implementation of this service.
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2001-07-11 07:23:24 +04:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Methods related to deleting temporary files on exit
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::DeleteTemporaryFileOnExit(nsIFile * aTemporaryFile)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
PRBool isFile = PR_FALSE;
|
|
|
|
nsCOMPtr<nsILocalFile> localFile (do_QueryInterface(aTemporaryFile, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// as a safety measure, make sure the nsIFile is really a file and not a directory object.
|
|
|
|
localFile->IsFile(&isFile);
|
|
|
|
if (!isFile) return NS_OK;
|
|
|
|
|
|
|
|
if (!mTemporaryFilesList)
|
|
|
|
rv = NS_NewISupportsArray(getter_AddRefs(mTemporaryFilesList));
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
mTemporaryFilesList->AppendElement(localFile);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsExternalHelperAppService::ExpungeTemporaryFiles()
|
|
|
|
{
|
|
|
|
if (!mTemporaryFilesList) return NS_OK;
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
PRUint32 numEntries = 0;
|
|
|
|
mTemporaryFilesList->Count(&numEntries);
|
|
|
|
nsCOMPtr<nsISupports> element;
|
|
|
|
nsCOMPtr<nsILocalFile> localFile;
|
|
|
|
for (PRUint32 index = 0; index < numEntries; index ++)
|
|
|
|
{
|
|
|
|
element = getter_AddRefs(mTemporaryFilesList->ElementAt(index));
|
|
|
|
if (element)
|
|
|
|
{
|
|
|
|
localFile = do_QueryInterface(element);
|
|
|
|
if (localFile)
|
2001-07-24 22:38:25 +04:00
|
|
|
localFile->Remove(PR_FALSE);
|
2001-07-11 07:23:24 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mTemporaryFilesList->Clear();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* XPCOM Shutdown observer */
|
|
|
|
NS_IMETHODIMP
|
2001-10-20 00:52:59 +04:00
|
|
|
nsExternalHelperAppService::Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *someData )
|
2001-07-11 07:23:24 +04:00
|
|
|
{
|
|
|
|
// we must be shutting down xpcom so remove our temporary files...
|
|
|
|
ExpungeTemporaryFiles();
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2001-10-23 02:01:27 +04:00
|
|
|
nsCOMPtr<nsIObserverService> obs = do_GetService("@mozilla.org/observer-service;1", &rv);
|
2001-07-11 07:23:24 +04:00
|
|
|
if (obs)
|
2001-10-20 00:52:59 +04:00
|
|
|
rv = obs->RemoveObserver(NS_STATIC_CAST(nsIObserver*, this), NS_XPCOM_SHUTDOWN_OBSERVER_ID);
|
2001-07-11 07:23:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-06-17 01:35:13 +04:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// begin external app handler implementation
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
NS_IMPL_THREADSAFE_ADDREF(nsExternalAppHandler)
|
|
|
|
NS_IMPL_THREADSAFE_RELEASE(nsExternalAppHandler)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsExternalAppHandler)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIStreamListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
|
2001-04-10 10:01:08 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIRequestObserver)
|
2000-07-07 05:48:05 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIHelperAppLauncher)
|
2000-10-04 10:54:47 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIURIContentListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIInterfaceRequestor)
|
2000-06-17 01:35:13 +04:00
|
|
|
NS_INTERFACE_MAP_END_THREADSAFE
|
|
|
|
|
|
|
|
nsExternalAppHandler::nsExternalAppHandler()
|
|
|
|
{
|
|
|
|
NS_INIT_ISUPPORTS();
|
2000-07-07 05:48:05 +04:00
|
|
|
mCanceled = PR_FALSE;
|
|
|
|
mReceivedDispostionInfo = PR_FALSE;
|
|
|
|
mStopRequestIssued = PR_FALSE;
|
2000-06-17 23:53:44 +04:00
|
|
|
mDataBuffer = (char *) nsMemory::Alloc((sizeof(char) * DATA_BUFFER_SIZE));
|
2001-01-25 09:48:16 +03:00
|
|
|
mProgressWindowCreated = PR_FALSE;
|
2000-06-17 01:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsExternalAppHandler::~nsExternalAppHandler()
|
|
|
|
{
|
2000-06-17 23:53:44 +04:00
|
|
|
if (mDataBuffer)
|
|
|
|
nsMemory::Free(mDataBuffer);
|
2000-06-17 01:35:13 +04:00
|
|
|
}
|
|
|
|
|
2000-10-04 10:54:47 +04:00
|
|
|
NS_IMETHODIMP nsExternalAppHandler::GetInterface(const nsIID & aIID, void * *aInstancePtr)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aInstancePtr);
|
|
|
|
return QueryInterface(aIID, aInstancePtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsExternalAppHandler::SetWebProgressListener(nsIWebProgressListener * aWebProgressListener)
|
2001-01-25 09:48:16 +03:00
|
|
|
{
|
|
|
|
// this call back means we've succesfully brought up the
|
|
|
|
// progress window so set the appropriate flag...
|
|
|
|
|
|
|
|
mProgressWindowCreated = PR_TRUE;
|
|
|
|
|
2000-10-25 02:18:56 +04:00
|
|
|
// while we were bringing up the progress dialog, we actually finished processing the
|
|
|
|
// url. If that's the case then mStopRequestIssued will be true. Tell the dialog to go away in that
|
2001-01-25 09:48:16 +03:00
|
|
|
// case and we need to execute the operation since we are actually done.
|
2000-10-25 02:18:56 +04:00
|
|
|
if (mStopRequestIssued && aWebProgressListener)
|
|
|
|
{
|
|
|
|
// simulate a notification saying the document is done. This will turn around and cause our
|
|
|
|
// progress dialog to go away....
|
2001-01-25 09:48:16 +03:00
|
|
|
aWebProgressListener->OnStateChange(nsnull, nsnull, nsIWebProgressListener::STATE_STOP, NS_OK);
|
|
|
|
return ExecuteDesiredAction();
|
2000-10-25 02:18:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// o.t. go ahead and register the progress listener....
|
|
|
|
|
2000-10-04 10:54:47 +04:00
|
|
|
if (mLoadCookie)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIWebProgress> webProgress(do_QueryInterface(mLoadCookie));
|
|
|
|
|
|
|
|
if (webProgress)
|
|
|
|
{
|
|
|
|
mWebProgressListener = aWebProgressListener;
|
|
|
|
webProgress->AddProgressListener(mWebProgressListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-01-25 09:48:16 +03:00
|
|
|
NS_IMETHODIMP nsExternalAppHandler::GetDownloadInfo(nsIURI ** aSourceUrl, PRInt64 * aTimeDownloadStarted, nsIFile ** aTarget)
|
2000-10-04 10:54:47 +04:00
|
|
|
{
|
2001-01-25 09:48:16 +03:00
|
|
|
|
|
|
|
*aTimeDownloadStarted = mTimeDownloadStarted;
|
|
|
|
|
2000-10-04 10:54:47 +04:00
|
|
|
if (mFinalFileDestination)
|
|
|
|
{
|
|
|
|
*aTarget = mFinalFileDestination;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*aTarget = mTempFile;
|
|
|
|
|
|
|
|
NS_IF_ADDREF(*aTarget);
|
|
|
|
|
|
|
|
*aSourceUrl = mSourceUrl;
|
|
|
|
NS_IF_ADDREF(*aSourceUrl);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsExternalAppHandler::CloseProgressWindow()
|
|
|
|
{
|
|
|
|
// make our docloader release the progress listener from the progress window...
|
|
|
|
if (mLoadCookie && mWebProgressListener)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIWebProgress> webProgress(do_QueryInterface(mLoadCookie));
|
|
|
|
|
|
|
|
if (webProgress)
|
|
|
|
{
|
|
|
|
webProgress->RemoveProgressListener(mWebProgressListener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// release extra state...
|
|
|
|
mWebProgressListener = nsnull;
|
|
|
|
mLoadCookie = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
void nsExternalAppHandler::ExtractSuggestedFileNameFromChannel(nsIChannel* aChannel)
|
2000-09-15 10:07:58 +04:00
|
|
|
{
|
|
|
|
// if the channel is an http channel and we have a content disposition header set,
|
|
|
|
// then use the file name suggested there as the preferred file name to SUGGEST to the user.
|
|
|
|
// we shouldn't actually use that without their permission...o.t. just use our temp file
|
|
|
|
// Try to get HTTP channel....if we have a content-disposition header then we can
|
2001-05-12 01:05:08 +04:00
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface( aChannel );
|
2000-09-15 10:07:58 +04:00
|
|
|
if ( httpChannel )
|
|
|
|
{
|
|
|
|
// Get content-disposition response header and extract a file name if there is one...
|
|
|
|
// content-disposition: has format: disposition-type < ; filename=value >
|
2001-05-12 01:05:08 +04:00
|
|
|
nsXPIDLCString disp;
|
|
|
|
nsresult rv = httpChannel->GetResponseHeader( "content-disposition", getter_Copies( disp ) );
|
|
|
|
if ( NS_SUCCEEDED( rv ) && disp )
|
2000-09-15 10:07:58 +04:00
|
|
|
{
|
2001-05-12 01:05:08 +04:00
|
|
|
nsCAutoString dispositionValue;
|
|
|
|
dispositionValue = disp;
|
|
|
|
PRInt32 pos = dispositionValue.Find("filename=", PR_TRUE);
|
|
|
|
if (pos > 0)
|
2000-09-15 10:07:58 +04:00
|
|
|
{
|
2001-05-12 01:05:08 +04:00
|
|
|
// extract everything after the filename= part and treat that as the file name...
|
|
|
|
nsCAutoString dispFileName;
|
2001-09-07 07:44:33 +04:00
|
|
|
dispositionValue.Right(dispFileName, dispositionValue.Length() -
|
|
|
|
(pos + nsCRT::strlen("filename=")));
|
2001-05-12 01:05:08 +04:00
|
|
|
if (!dispFileName.IsEmpty()) // if we got a file name back..
|
2000-09-15 10:07:58 +04:00
|
|
|
{
|
2001-05-12 01:05:08 +04:00
|
|
|
pos = dispFileName.FindChar(';', PR_TRUE);
|
|
|
|
if (pos > 0)
|
|
|
|
dispFileName.Truncate(pos);
|
|
|
|
|
|
|
|
// According to RFC 2183, filename can be given as filename=value,
|
|
|
|
// where value is token or quoted-string. See bug 66181.
|
|
|
|
dispFileName.StripChar('"');
|
|
|
|
|
|
|
|
// ONLY if we got here, will we remember the suggested file name...
|
2001-10-25 07:49:28 +04:00
|
|
|
mSuggestedFileName.AssignWithConversion(dispFileName.get());
|
2001-05-12 01:05:08 +04:00
|
|
|
}
|
|
|
|
} // if we found a file name in the header disposition field
|
|
|
|
} // we had a disp header
|
2000-09-15 10:07:58 +04:00
|
|
|
} // if we had an http channel
|
|
|
|
}
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
nsresult nsExternalAppHandler::RetargetLoadNotifications(nsIRequest *request)
|
2000-10-04 10:54:47 +04:00
|
|
|
{
|
|
|
|
// we are going to run the downloading of the helper app in our own little docloader / load group context.
|
|
|
|
// so go ahead and force the creation of a load group and doc loader for us to use...
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
nsCOMPtr<nsIChannel> aChannel = do_QueryInterface(request);
|
|
|
|
NS_ENSURE_TRUE(aChannel, NS_ERROR_FAILURE);
|
|
|
|
|
2000-10-04 10:54:47 +04:00
|
|
|
nsCOMPtr<nsIURILoader> uriLoader(do_GetService(NS_URI_LOADER_CONTRACTID));
|
|
|
|
NS_ENSURE_TRUE(uriLoader, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsCOMPtr<nsILoadGroup> newLoadGroup;
|
|
|
|
nsCOMPtr<nsILoadGroup> oldLoadGroup;
|
|
|
|
uriLoader->GetLoadGroupForContext(NS_STATIC_CAST(nsIURIContentListener*, this), getter_AddRefs(newLoadGroup));
|
|
|
|
aChannel->GetLoadGroup(getter_AddRefs(oldLoadGroup));
|
|
|
|
|
2000-11-01 11:06:19 +03:00
|
|
|
// we need to store off the original (pre redirect!) channel that initiated the load. We do
|
|
|
|
// this so later on, we can pass any refresh urls associated with the original channel back to the
|
|
|
|
// window context which started the whole process. More comments about that are listed below....
|
|
|
|
// HACK ALERT: it's pretty bogus that we are getting the document channel from the doc loader.
|
|
|
|
// ideally we should be able to just use mChannel (the channel we are extracting content from) or
|
|
|
|
// the default load channel associated with the original load group. Unfortunately because
|
|
|
|
// a redirect may have occurred, the doc loader is the only one with a ptr to the original channel
|
|
|
|
// which is what we really want....
|
|
|
|
nsCOMPtr<nsIDocumentLoader> origContextLoader;
|
|
|
|
uriLoader->GetDocumentLoaderForContext(mWindowContext, getter_AddRefs(origContextLoader));
|
|
|
|
if (origContextLoader)
|
|
|
|
origContextLoader->GetDocumentChannel(getter_AddRefs(mOriginalChannel));
|
|
|
|
|
2000-10-04 10:54:47 +04:00
|
|
|
if(oldLoadGroup)
|
2001-04-10 10:01:08 +04:00
|
|
|
oldLoadGroup->RemoveRequest(request, nsnull, NS_OK);
|
2000-10-04 10:54:47 +04:00
|
|
|
|
|
|
|
aChannel->SetLoadGroup(newLoadGroup);
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> req (do_QueryInterface(mLoadCookie));
|
|
|
|
aChannel->SetNotificationCallbacks(req);
|
2001-02-21 23:38:08 +03:00
|
|
|
rv = newLoadGroup->AddRequest(request, nsnull);
|
2000-10-04 10:54:47 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2000-11-02 04:27:12 +03:00
|
|
|
#define SALT_SIZE 8
|
|
|
|
#define TABLE_SIZE 36
|
|
|
|
const char table[] =
|
|
|
|
{ 'a','b','c','d','e','f','g','h','i','j',
|
|
|
|
'k','l','m','n','o','p','q','r','s','t',
|
|
|
|
'u','v','w','x','y','z','0','1','2','3',
|
|
|
|
'4','5','6','7','8','9'};
|
|
|
|
|
|
|
|
|
2000-07-07 05:48:05 +04:00
|
|
|
nsresult nsExternalAppHandler::SetUpTempFile(nsIChannel * aChannel)
|
2000-06-17 01:35:13 +04:00
|
|
|
{
|
2000-07-07 05:48:05 +04:00
|
|
|
nsresult rv = NS_OK;
|
2000-06-17 04:06:26 +04:00
|
|
|
|
2000-06-23 08:43:13 +04:00
|
|
|
#ifdef XP_MAC
|
|
|
|
// create a temp file for the data...and open it for writing.
|
2000-09-07 01:12:11 +04:00
|
|
|
// use NS_MAC_DEFAULT_DOWNLOAD_DIR which gets download folder from InternetConfig
|
|
|
|
// if it can't get download folder pref, then it uses desktop folder
|
|
|
|
NS_GetSpecialDirectory(NS_MAC_DEFAULT_DOWNLOAD_DIR, getter_AddRefs(mTempFile));
|
|
|
|
// while we're here, also set Mac type and creator
|
|
|
|
if (mMimeInfo)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsILocalFileMac> macfile = do_QueryInterface(mTempFile);
|
|
|
|
if (macfile)
|
|
|
|
{
|
|
|
|
PRUint32 type, creator;
|
|
|
|
mMimeInfo->GetMacType(&type);
|
|
|
|
mMimeInfo->GetMacCreator(&creator);
|
|
|
|
macfile->SetFileTypeAndCreator(type, creator);
|
|
|
|
}
|
|
|
|
}
|
2000-06-23 08:43:13 +04:00
|
|
|
#else
|
2000-06-17 01:35:13 +04:00
|
|
|
// create a temp file for the data...and open it for writing.
|
2000-08-12 00:31:57 +04:00
|
|
|
NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(mTempFile));
|
2000-06-23 08:43:13 +04:00
|
|
|
#endif
|
2000-06-17 04:06:26 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2000-10-04 10:54:47 +04:00
|
|
|
aChannel->GetURI(getter_AddRefs(mSourceUrl));
|
|
|
|
nsCOMPtr<nsIURL> url = do_QueryInterface(mSourceUrl);
|
2000-06-17 04:06:26 +04:00
|
|
|
|
2000-11-02 04:27:12 +03:00
|
|
|
// We need to do two things here, (1) extract the file name that's part of the url
|
|
|
|
// and store this is as mSuggestedfileName. This way, when we show the user a file picker,
|
|
|
|
// we can have it pre filled with the suggested file name.
|
|
|
|
// (2) We need to generate a name for the temp file that we are going to be streaming data to.
|
|
|
|
// We don't want this name to be predictable for security reasons so we are going to generate a
|
|
|
|
// "salted" name.....
|
2000-06-18 05:13:04 +04:00
|
|
|
|
2000-06-17 04:06:26 +04:00
|
|
|
if (url)
|
|
|
|
{
|
|
|
|
// try to extract the file name from the url and use that as a first pass as the
|
|
|
|
// leaf name of our temp file...
|
|
|
|
nsXPIDLCString leafName;
|
|
|
|
url->GetFileName(getter_Copies(leafName));
|
2001-07-21 06:54:32 +04:00
|
|
|
if (leafName.get())
|
|
|
|
{
|
2001-09-08 16:47:05 +04:00
|
|
|
nsXPIDLCString unescapedFileName;
|
|
|
|
rv = nsStdUnescape((char*)leafName.get(), getter_Copies(unescapedFileName));
|
2001-07-21 06:54:32 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
mSuggestedFileName.Assign(NS_ConvertUTF8toUCS2(unescapedFileName));
|
|
|
|
else
|
2001-09-08 16:47:05 +04:00
|
|
|
mSuggestedFileName.AssignWithConversion(leafName);
|
2001-07-21 06:54:32 +04:00
|
|
|
}
|
2000-11-02 04:27:12 +03:00
|
|
|
}
|
2000-10-04 10:54:47 +04:00
|
|
|
|
2000-11-02 04:27:12 +03:00
|
|
|
// step (2), generate a salted file name for the temp file....
|
|
|
|
nsCAutoString saltedTempLeafName;
|
|
|
|
// this salting code was ripped directly from the profile manager.
|
|
|
|
// turn PR_Now() into milliseconds since epoch 1058 // and salt rand with that.
|
|
|
|
double fpTime;
|
|
|
|
LL_L2D(fpTime, PR_Now());
|
|
|
|
srand((uint)(fpTime * 1e-6 + 0.5));
|
|
|
|
PRInt32 i;
|
|
|
|
for (i=0;i<SALT_SIZE;i++)
|
|
|
|
{
|
|
|
|
saltedTempLeafName.Append(table[(rand()%TABLE_SIZE)]);
|
2000-06-17 04:06:26 +04:00
|
|
|
}
|
2000-07-12 10:09:02 +04:00
|
|
|
|
|
|
|
// now append our extension.
|
2000-11-02 04:27:12 +03:00
|
|
|
saltedTempLeafName.Append(mTempFileExtension);
|
2000-06-18 05:13:04 +04:00
|
|
|
|
2001-10-25 07:49:28 +04:00
|
|
|
mTempFile->Append(saltedTempLeafName.get()); // make this file unique!!!
|
2000-06-24 05:55:40 +04:00
|
|
|
mTempFile->CreateUnique(nsnull, nsIFile::NORMAL_FILE_TYPE, 0644);
|
2000-06-17 04:06:26 +04:00
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
NS_DEFINE_CID(kFileTransportServiceCID, NS_FILETRANSPORTSERVICE_CID);
|
2001-07-25 11:54:28 +04:00
|
|
|
nsCOMPtr<nsIFileTransportService> fts =
|
|
|
|
do_GetService(kFileTransportServiceCID, &rv);
|
2001-02-21 23:38:08 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsITransport> fileTransport;
|
2001-03-14 05:57:47 +03:00
|
|
|
rv = fts->CreateTransport(mTempFile, PR_WRONLY | PR_CREATE_FILE, 0664, getter_AddRefs(fileTransport));
|
2001-02-21 23:38:08 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = fileTransport->OpenOutputStream(0, -1, 0, getter_AddRefs(mOutStream));
|
|
|
|
|
2001-02-07 07:10:41 +03:00
|
|
|
#ifdef XP_MAC
|
|
|
|
nsXPIDLCString contentType;
|
|
|
|
mMimeInfo->GetMIMEType(getter_Copies(contentType));
|
2001-06-05 08:50:10 +04:00
|
|
|
if (contentType &&
|
|
|
|
(nsCRT::strcasecmp(contentType, APPLICATION_APPLEFILE) == 0) ||
|
|
|
|
(nsCRT::strcasecmp(contentType, MULTIPART_APPLEDOUBLE) == 0))
|
2001-02-07 07:10:41 +03:00
|
|
|
{
|
2001-06-26 02:11:43 +04:00
|
|
|
nsCOMPtr<nsIAppleFileDecoder> appleFileDecoder = do_CreateInstance(NS_IAPPLEFILEDECODER_CONTRACTID, &rv);
|
|
|
|
if (NS_SUCCEEDED(rv) && appleFileDecoder)
|
2001-02-07 07:10:41 +03:00
|
|
|
{
|
2001-06-26 02:11:43 +04:00
|
|
|
rv = appleFileDecoder->Initialize(mOutStream, mTempFile);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
mOutStream = do_QueryInterface(appleFileDecoder, &rv);
|
2001-02-07 07:10:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2000-07-07 05:48:05 +04:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
NS_IMETHODIMP nsExternalAppHandler::OnStartRequest(nsIRequest *request, nsISupports * aCtxt)
|
2000-07-07 05:48:05 +04:00
|
|
|
{
|
2001-02-21 23:38:08 +03:00
|
|
|
NS_ENSURE_ARG(request);
|
2000-07-07 05:48:05 +04:00
|
|
|
|
2000-08-10 05:49:06 +04:00
|
|
|
// first, check to see if we've been canceled....
|
|
|
|
if (mCanceled) // then go cancel our underlying channel too
|
2001-02-21 23:38:08 +03:00
|
|
|
return request->Cancel(NS_BINDING_ABORTED);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> aChannel = do_QueryInterface(request);
|
2000-08-10 05:49:06 +04:00
|
|
|
|
2000-07-07 05:48:05 +04:00
|
|
|
nsresult rv = SetUpTempFile(aChannel);
|
2001-02-07 07:10:41 +03:00
|
|
|
|
|
|
|
|
2000-10-04 10:54:47 +04:00
|
|
|
// retarget all load notifcations to our docloader instead of the original window's docloader...
|
2001-02-21 23:38:08 +03:00
|
|
|
RetargetLoadNotifications(request);
|
2000-10-04 10:54:47 +04:00
|
|
|
// ignore failure...
|
2000-09-15 10:28:44 +04:00
|
|
|
ExtractSuggestedFileNameFromChannel(aChannel);
|
2001-05-12 01:05:08 +04:00
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel = do_QueryInterface( aChannel );
|
2000-10-23 08:31:07 +04:00
|
|
|
if ( httpChannel )
|
|
|
|
{
|
|
|
|
// Turn off content encoding conversions.
|
|
|
|
httpChannel->SetApplyConversion( PR_FALSE );
|
|
|
|
}
|
|
|
|
|
2000-07-07 05:48:05 +04:00
|
|
|
// now that the temp file is set up, find out if we need to invoke a dialog asking the user what
|
|
|
|
// they want us to do with this content...
|
|
|
|
|
2001-06-06 12:17:39 +04:00
|
|
|
PRBool alwaysAsk = PR_TRUE;
|
2001-06-28 06:18:54 +04:00
|
|
|
mMimeInfo->GetAlwaysAskBeforeHandling(&alwaysAsk);
|
2000-07-12 03:15:49 +04:00
|
|
|
if (alwaysAsk)
|
2000-07-07 05:48:05 +04:00
|
|
|
{
|
|
|
|
// do this first! make sure we don't try to take an action until the user tells us what they want to do
|
|
|
|
// with it...
|
|
|
|
mReceivedDispostionInfo = PR_FALSE;
|
|
|
|
|
2001-05-04 01:24:36 +04:00
|
|
|
// invoke the dialog!!!!! use mWindowContext as the window context parameter for the dialog request
|
|
|
|
mDialog = do_CreateInstance( NS_IHELPERAPPLAUNCHERDLG_CONTRACTID, &rv );
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mDialog->Show( this, mWindowContext );
|
2000-07-11 01:21:10 +04:00
|
|
|
|
2000-07-12 10:09:02 +04:00
|
|
|
// what do we do if the dialog failed? I guess we should call Cancel and abort the load....
|
2000-06-17 06:34:44 +04:00
|
|
|
}
|
2000-07-07 05:48:05 +04:00
|
|
|
else
|
2001-05-04 01:24:36 +04:00
|
|
|
{
|
2000-07-07 05:48:05 +04:00
|
|
|
mReceivedDispostionInfo = PR_TRUE; // no need to wait for a response from the user
|
2000-06-17 06:34:44 +04:00
|
|
|
|
2001-05-04 01:24:36 +04:00
|
|
|
// We need to do the save/open immediately, then.
|
|
|
|
PRInt32 action = nsIMIMEInfo::saveToDisk;
|
|
|
|
mMimeInfo->GetPreferredAction( &action );
|
|
|
|
if ( action == nsIMIMEInfo::saveToDisk )
|
|
|
|
{
|
|
|
|
rv = SaveToDisk(nsnull, PR_FALSE);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rv = LaunchWithApplication(nsnull, PR_FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-25 09:48:16 +03:00
|
|
|
mTimeDownloadStarted = PR_Now();
|
|
|
|
|
2000-06-17 01:35:13 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
NS_IMETHODIMP nsExternalAppHandler::OnDataAvailable(nsIRequest *request, nsISupports * aCtxt,
|
2000-06-17 01:35:13 +04:00
|
|
|
nsIInputStream * inStr, PRUint32 sourceOffset, PRUint32 count)
|
|
|
|
{
|
2000-08-10 05:49:06 +04:00
|
|
|
// first, check to see if we've been canceled....
|
|
|
|
if (mCanceled) // then go cancel our underlying channel too
|
2001-02-21 23:38:08 +03:00
|
|
|
return request->Cancel(NS_BINDING_ABORTED);
|
2000-08-10 05:49:06 +04:00
|
|
|
|
2000-06-17 01:35:13 +04:00
|
|
|
// read the data out of the stream and write it to the temp file.
|
2000-06-17 23:53:44 +04:00
|
|
|
if (mOutStream && mDataBuffer && count > 0)
|
|
|
|
{
|
|
|
|
PRUint32 numBytesRead = 0;
|
|
|
|
PRUint32 numBytesWritten = 0;
|
|
|
|
while (count > 0) // while we still have bytes to copy...
|
|
|
|
{
|
|
|
|
inStr->Read(mDataBuffer, PR_MIN(count, DATA_BUFFER_SIZE - 1), &numBytesRead);
|
|
|
|
if (count >= numBytesRead)
|
|
|
|
count -= numBytesRead; // subtract off the number of bytes we just read
|
|
|
|
else
|
|
|
|
count = 0;
|
|
|
|
mOutStream->Write(mDataBuffer, numBytesRead, &numBytesWritten);
|
|
|
|
}
|
|
|
|
}
|
2000-06-17 01:35:13 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
NS_IMETHODIMP nsExternalAppHandler::OnStopRequest(nsIRequest *request, nsISupports *aCtxt,
|
2001-04-10 10:01:08 +04:00
|
|
|
nsresult aStatus)
|
2000-06-17 01:35:13 +04:00
|
|
|
{
|
2000-07-07 05:48:05 +04:00
|
|
|
mStopRequestIssued = PR_TRUE;
|
|
|
|
|
2000-08-10 05:49:06 +04:00
|
|
|
// first, check to see if we've been canceled....
|
|
|
|
if (mCanceled) // then go cancel our underlying channel too
|
2001-02-21 23:38:08 +03:00
|
|
|
return request->Cancel(NS_BINDING_ABORTED);
|
2000-08-10 05:49:06 +04:00
|
|
|
|
2000-06-17 01:35:13 +04:00
|
|
|
// go ahead and execute the application passing in our temp file as an argument
|
2000-06-17 04:06:26 +04:00
|
|
|
// this may involve us calling back into the OS external app service to make the call
|
|
|
|
// for actually launching the helper app. It'd be great if nsIFile::spawn could be made to work
|
|
|
|
// on the mac...right now the mac implementation ignores all arguments passed in.
|
2000-06-17 06:34:44 +04:00
|
|
|
|
|
|
|
// close the stream...
|
2000-06-17 23:53:44 +04:00
|
|
|
if (mOutStream)
|
2000-08-10 05:49:06 +04:00
|
|
|
{
|
2000-06-17 23:53:44 +04:00
|
|
|
mOutStream->Close();
|
2000-08-10 05:49:06 +04:00
|
|
|
mOutStream = nsnull;
|
|
|
|
}
|
2000-06-17 06:34:44 +04:00
|
|
|
|
2001-01-25 09:48:16 +03:00
|
|
|
return ExecuteDesiredAction();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsExternalAppHandler::ExecuteDesiredAction()
|
|
|
|
{
|
2001-07-11 07:23:24 +04:00
|
|
|
nsresult rv = NS_OK;
|
2001-01-25 09:48:16 +03:00
|
|
|
if (mProgressWindowCreated && !mCanceled)
|
2000-06-18 03:04:53 +04:00
|
|
|
{
|
2000-07-12 03:15:49 +04:00
|
|
|
nsMIMEInfoHandleAction action = nsIMIMEInfo::saveToDisk;
|
2000-07-07 05:48:05 +04:00
|
|
|
mMimeInfo->GetPreferredAction(&action);
|
|
|
|
if (action == nsIMIMEInfo::saveToDisk)
|
2001-01-25 09:48:16 +03:00
|
|
|
rv = MoveFile(mFinalFileDestination);
|
2000-07-07 05:48:05 +04:00
|
|
|
else
|
2001-01-25 09:48:16 +03:00
|
|
|
rv = OpenWithApplication(nsnull);
|
2000-06-18 03:04:53 +04:00
|
|
|
}
|
2001-01-25 09:48:16 +03:00
|
|
|
|
2000-06-18 03:04:53 +04:00
|
|
|
return rv;
|
2000-06-17 01:35:13 +04:00
|
|
|
}
|
|
|
|
|
2000-07-11 01:21:10 +04:00
|
|
|
nsresult nsExternalAppHandler::Init(nsIMIMEInfo * aMIMEInfo, const char * aTempFileExtension, nsISupports * aWindowContext)
|
2000-06-17 06:34:44 +04:00
|
|
|
{
|
2000-07-11 01:21:10 +04:00
|
|
|
mWindowContext = aWindowContext;
|
2000-07-07 05:48:05 +04:00
|
|
|
mMimeInfo = aMIMEInfo;
|
2000-06-21 12:29:13 +04:00
|
|
|
|
|
|
|
// make sure the extention includes the '.'
|
|
|
|
if (aTempFileExtension && *aTempFileExtension != '.')
|
|
|
|
mTempFileExtension = ".";
|
|
|
|
mTempFileExtension.Append(aTempFileExtension);
|
2000-10-04 10:54:47 +04:00
|
|
|
|
2000-06-17 06:34:44 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-07-07 05:48:05 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsExternalAppHandler::GetMIMEInfo(nsIMIMEInfo ** aMIMEInfo)
|
|
|
|
{
|
|
|
|
*aMIMEInfo = mMimeInfo;
|
|
|
|
NS_IF_ADDREF(*aMIMEInfo);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-05-04 01:24:36 +04:00
|
|
|
NS_IMETHODIMP nsExternalAppHandler::GetSource(nsIURI ** aSourceURI)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aSourceURI);
|
|
|
|
*aSourceURI = mSourceUrl;
|
|
|
|
NS_IF_ADDREF(*aSourceURI);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-10-04 10:54:47 +04:00
|
|
|
nsresult nsExternalAppHandler::ShowProgressDialog()
|
|
|
|
{
|
|
|
|
// we are back from the helper app dialog (where the user chooses to save or open), but we aren't
|
|
|
|
// done processing the load. in this case, throw up a progress dialog so the user can see what's going on...
|
|
|
|
nsresult rv = NS_OK;
|
2001-05-04 01:24:36 +04:00
|
|
|
|
|
|
|
if ( !mDialog ) {
|
|
|
|
// Get helper app launcher dialog.
|
|
|
|
mDialog = do_CreateInstance( NS_IHELPERAPPLAUNCHERDLG_CONTRACTID, &rv );
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = mDialog->ShowProgressDialog(this, mWindowContext);
|
2000-10-04 10:54:47 +04:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-06-28 06:18:54 +04:00
|
|
|
nsresult nsExternalAppHandler::PromptForSaveToFile(nsILocalFile ** aNewFile, const PRUnichar * aDefaultFile, const PRUnichar * aFileExtension)
|
2000-07-13 10:26:25 +04:00
|
|
|
{
|
2001-05-04 01:24:36 +04:00
|
|
|
// invoke the dialog!!!!! use mWindowContext as the window context parameter for the dialog request
|
|
|
|
// XXX Convert to use file picker?
|
2000-07-13 10:26:25 +04:00
|
|
|
nsresult rv = NS_OK;
|
2001-05-04 01:24:36 +04:00
|
|
|
if (!mDialog)
|
|
|
|
{
|
|
|
|
// Get helper app launcher dialog.
|
|
|
|
mDialog = do_CreateInstance( NS_IHELPERAPPLAUNCHERDLG_CONTRACTID, &rv );
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2001-06-28 06:18:54 +04:00
|
|
|
// we want to explicitly unescape aDefaultFile b4 passing into the dialog. we can't unescape
|
|
|
|
// it because the dialog is implemented by a JS component which doesn't have a window so no unescape routine is defined...
|
|
|
|
|
|
|
|
rv = mDialog->PromptForSaveToFile(mWindowContext, aDefaultFile, aFileExtension, aNewFile);
|
2000-10-04 10:54:47 +04:00
|
|
|
|
2000-07-13 10:26:25 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-01-25 09:48:16 +03:00
|
|
|
nsresult nsExternalAppHandler::MoveFile(nsIFile * aNewFileLocation)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
NS_ASSERTION(mStopRequestIssued, "uhoh, how did we get here if we aren't done getting data?");
|
|
|
|
|
|
|
|
nsCOMPtr<nsILocalFile> fileToUse = do_QueryInterface(aNewFileLocation);
|
|
|
|
|
|
|
|
// if the on stop request was actually issued then it's now time to actually perform the file move....
|
|
|
|
if (mStopRequestIssued && fileToUse)
|
|
|
|
{
|
|
|
|
// Unfortunately, MoveTo will fail if a file already exists at the user specified location....
|
|
|
|
// but the user has told us, this is where they want the file! (when we threw up the save to file dialog,
|
|
|
|
// it told them the file already exists and do they wish to over write it. So it should be okay to delete
|
|
|
|
// fileToUse if it already exists.
|
|
|
|
PRBool equalToTempFile = PR_FALSE;
|
|
|
|
PRBool filetoUseAlreadyExists = PR_FALSE;
|
|
|
|
fileToUse->Equals(mTempFile, &equalToTempFile);
|
|
|
|
fileToUse->Exists(&filetoUseAlreadyExists);
|
|
|
|
if (filetoUseAlreadyExists && !equalToTempFile)
|
2001-07-24 22:38:25 +04:00
|
|
|
fileToUse->Remove(PR_FALSE);
|
2001-01-25 09:48:16 +03:00
|
|
|
|
|
|
|
// extract the new leaf name from the file location
|
|
|
|
nsXPIDLCString fileName;
|
|
|
|
fileToUse->GetLeafName(getter_Copies(fileName));
|
|
|
|
nsCOMPtr<nsIFile> directoryLocation;
|
|
|
|
fileToUse->GetParent(getter_AddRefs(directoryLocation));
|
|
|
|
if (directoryLocation)
|
|
|
|
{
|
|
|
|
rv = mTempFile->MoveTo(directoryLocation, fileName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveToDisk should only be called by the helper app dialog which allows
|
|
|
|
// the user to say launch with application or save to disk. It doesn't actually
|
|
|
|
// perform the save, it just prompts for the destination file name. The actual save
|
|
|
|
// won't happen until we are done downloading the content and are sure we've
|
|
|
|
// shown a progress dialog. This was done to simplify the
|
|
|
|
// logic that was showing up in this method. Internal callers who actually want
|
|
|
|
// to preform the save should call ::MoveFile
|
|
|
|
|
2000-07-07 05:48:05 +04:00
|
|
|
NS_IMETHODIMP nsExternalAppHandler::SaveToDisk(nsIFile * aNewFileLocation, PRBool aRememberThisPreference)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
2000-08-10 05:49:06 +04:00
|
|
|
if (mCanceled)
|
|
|
|
return NS_OK;
|
|
|
|
|
2000-11-22 22:48:49 +03:00
|
|
|
mMimeInfo->SetPreferredAction(nsIMIMEInfo::saveToDisk);
|
|
|
|
|
2000-08-10 05:49:06 +04:00
|
|
|
if (!aNewFileLocation)
|
2000-07-07 05:48:05 +04:00
|
|
|
{
|
2000-08-10 05:49:06 +04:00
|
|
|
nsXPIDLString leafName;
|
2001-01-25 09:48:16 +03:00
|
|
|
nsCOMPtr<nsILocalFile> fileToUse;
|
2000-08-10 05:49:06 +04:00
|
|
|
mTempFile->GetUnicodeLeafName(getter_Copies(leafName));
|
2000-10-04 10:54:47 +04:00
|
|
|
if (mSuggestedFileName.IsEmpty())
|
2001-06-28 06:18:54 +04:00
|
|
|
rv = PromptForSaveToFile(getter_AddRefs(fileToUse), leafName, NS_ConvertASCIItoUCS2(mTempFileExtension).get());
|
2000-09-15 10:07:58 +04:00
|
|
|
else
|
2001-06-28 06:18:54 +04:00
|
|
|
{
|
|
|
|
nsAutoString fileExt;
|
|
|
|
PRInt32 pos = mSuggestedFileName.RFindChar(PRUnichar('.'));
|
|
|
|
if (pos >= 0)
|
2001-09-07 07:44:33 +04:00
|
|
|
mSuggestedFileName.Right(fileExt, mSuggestedFileName.Length() - pos);
|
2001-06-28 06:18:54 +04:00
|
|
|
if (fileExt.IsEmpty())
|
|
|
|
fileExt = NS_ConvertASCIItoUCS2(mTempFileExtension).get();
|
|
|
|
|
2001-06-30 15:02:25 +04:00
|
|
|
rv = PromptForSaveToFile(getter_AddRefs(fileToUse), mSuggestedFileName.get(), fileExt.get());
|
2001-06-28 06:18:54 +04:00
|
|
|
}
|
2000-09-15 10:07:58 +04:00
|
|
|
|
2000-08-10 05:49:06 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return Cancel();
|
2001-01-25 09:48:16 +03:00
|
|
|
|
2000-08-10 05:49:06 +04:00
|
|
|
mFinalFileDestination = do_QueryInterface(fileToUse);
|
2000-11-01 11:06:19 +03:00
|
|
|
|
2001-01-25 09:48:16 +03:00
|
|
|
if (!mProgressWindowCreated)
|
|
|
|
ShowProgressDialog();
|
|
|
|
|
2000-11-01 11:06:19 +03:00
|
|
|
// now that the user has chosen the file location to save to, it's okay to fire the refresh tag
|
|
|
|
// if there is one. We don't want to do this before the save as dialog goes away because this dialog
|
|
|
|
// is modal and we do bad things if you try to load a web page in the underlying window while a modal
|
|
|
|
// dialog is still up.
|
|
|
|
ProcessAnyRefreshTags();
|
2000-07-07 05:48:05 +04:00
|
|
|
}
|
2000-08-10 05:49:06 +04:00
|
|
|
|
|
|
|
mReceivedDispostionInfo = PR_TRUE;
|
2001-01-25 09:48:16 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult nsExternalAppHandler::OpenWithApplication(nsIFile * aApplication)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (mCanceled)
|
|
|
|
return NS_OK;
|
2000-10-04 10:54:47 +04:00
|
|
|
|
2001-01-25 09:48:16 +03:00
|
|
|
// we only should have gotten here if the on stop request had been fired already.
|
2000-10-10 09:20:09 +04:00
|
|
|
|
2001-01-25 09:48:16 +03:00
|
|
|
NS_ASSERTION(mStopRequestIssued, "uhoh, how did we get here if we aren't done getting data?");
|
|
|
|
// if a stop request was already issued then proceed with launching the application.
|
|
|
|
if (mStopRequestIssued)
|
2000-10-04 10:54:47 +04:00
|
|
|
{
|
2001-01-25 09:48:16 +03:00
|
|
|
nsCOMPtr<nsPIExternalAppLauncher> helperAppService (do_GetService(NS_EXTERNALHELPERAPPSERVICE_CONTRACTID));
|
|
|
|
if (helperAppService)
|
|
|
|
{
|
|
|
|
rv = helperAppService->LaunchAppWithTempFile(mMimeInfo, mTempFile);
|
2001-07-11 07:23:24 +04:00
|
|
|
|
|
|
|
// be sure to add this temporary file to our list of files which need deleted on exit...
|
|
|
|
helperAppService->DeleteTemporaryFileOnExit(mTempFile);
|
2001-01-25 09:48:16 +03:00
|
|
|
}
|
2000-10-04 10:54:47 +04:00
|
|
|
}
|
2000-07-07 05:48:05 +04:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-01-25 09:48:16 +03:00
|
|
|
// LaunchWithApplication should only be called by the helper app dialog which allows
|
|
|
|
// the user to say launch with application or save to disk. It doesn't actually
|
|
|
|
// perform launch with application. That won't happen until we are done downloading
|
|
|
|
// the content and are sure we've showna progress dialog. This was done to simplify the
|
|
|
|
// logic that was showing up in this method.
|
|
|
|
|
2000-07-07 05:48:05 +04:00
|
|
|
NS_IMETHODIMP nsExternalAppHandler::LaunchWithApplication(nsIFile * aApplication, PRBool aRememberThisPreference)
|
|
|
|
{
|
2000-08-10 05:49:06 +04:00
|
|
|
if (mCanceled)
|
|
|
|
return NS_OK;
|
|
|
|
|
2000-11-22 22:48:49 +03:00
|
|
|
mMimeInfo->SetPreferredAction(nsIMIMEInfo::useHelperApp);
|
|
|
|
|
2001-01-25 09:48:16 +03:00
|
|
|
// launch the progress window now that the user has picked the desired action.
|
|
|
|
if (!mProgressWindowCreated)
|
|
|
|
ShowProgressDialog();
|
|
|
|
|
2000-11-01 11:06:19 +03:00
|
|
|
// user has chosen to launch using an application, fire any refresh tags now...
|
|
|
|
ProcessAnyRefreshTags();
|
2001-01-25 09:48:16 +03:00
|
|
|
|
2000-07-07 05:48:05 +04:00
|
|
|
mReceivedDispostionInfo = PR_TRUE;
|
2000-07-11 01:21:10 +04:00
|
|
|
if (mMimeInfo && aApplication)
|
|
|
|
mMimeInfo->SetPreferredApplicationHandler(aApplication);
|
2000-07-07 05:48:05 +04:00
|
|
|
|
2001-01-25 09:48:16 +03:00
|
|
|
return NS_OK;
|
2000-07-07 05:48:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsExternalAppHandler::Cancel()
|
|
|
|
{
|
|
|
|
mCanceled = PR_TRUE;
|
2000-08-10 05:49:06 +04:00
|
|
|
// shutdown our stream to the temp file
|
|
|
|
if (mOutStream)
|
|
|
|
{
|
|
|
|
mOutStream->Close();
|
|
|
|
mOutStream = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
// clean up after ourselves and delete the temp file...
|
|
|
|
if (mTempFile)
|
|
|
|
{
|
2001-07-24 22:38:25 +04:00
|
|
|
mTempFile->Remove(PR_TRUE);
|
2000-08-10 05:49:06 +04:00
|
|
|
mTempFile = nsnull;
|
|
|
|
}
|
2000-10-04 10:54:47 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-11-01 11:06:19 +03:00
|
|
|
void nsExternalAppHandler::ProcessAnyRefreshTags()
|
|
|
|
{
|
|
|
|
// one last thing, try to see if the original window context supports a refresh interface...
|
|
|
|
// Sometimes, when you download content that requires an external handler, there is
|
|
|
|
// a refresh header associated with the download. This refresh header points to a page
|
|
|
|
// the content provider wants the user to see after they download the content. How do we
|
|
|
|
// pass this refresh information back to the caller? For now, try to get the refresh URI
|
|
|
|
// interface. If the window context where the request originated came from supports this
|
|
|
|
// then we can force it to process the refresh information (if there is any) from this channel.
|
|
|
|
if (mWindowContext && mOriginalChannel)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRefreshURI> refreshHandler (do_GetInterface(mWindowContext));
|
|
|
|
if (refreshHandler)
|
|
|
|
refreshHandler->SetupRefreshURI(mOriginalChannel);
|
|
|
|
mOriginalChannel = nsnull;
|
|
|
|
}
|
|
|
|
}
|
2000-10-04 10:54:47 +04:00
|
|
|
|
|
|
|
// nsIURIContentListener implementation
|
2001-05-14 07:43:54 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsExternalAppHandler::OnStartURIOpen(nsIURI* aURI, PRBool* aAbortOpen)
|
2000-10-04 10:54:47 +04:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-05-14 07:43:54 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsExternalAppHandler::IsPreferred(const char * aContentType,
|
|
|
|
char ** aDesiredContentType,
|
|
|
|
PRBool * aCanHandleContent)
|
2000-10-04 10:54:47 +04:00
|
|
|
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("IsPreferred");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2001-05-14 07:43:54 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsExternalAppHandler::CanHandleContent(const char * aContentType,
|
2001-10-27 06:52:39 +04:00
|
|
|
PRBool aIsContentPreferred,
|
2001-05-14 07:43:54 +04:00
|
|
|
char ** aDesiredContentType,
|
|
|
|
PRBool * aCanHandleContent)
|
2000-10-04 10:54:47 +04:00
|
|
|
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("CanHandleContent");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2001-05-14 07:43:54 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsExternalAppHandler::DoContent(const char * aContentType,
|
2001-10-27 06:52:39 +04:00
|
|
|
PRBool aIsContentPreferred,
|
2001-05-14 07:43:54 +04:00
|
|
|
nsIRequest * aRequest,
|
|
|
|
nsIStreamListener ** aContentHandler,
|
|
|
|
PRBool * aAbortProcess)
|
2000-10-04 10:54:47 +04:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("DoContent");
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2001-05-14 07:43:54 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsExternalAppHandler::GetParentContentListener(nsIURIContentListener** aParent)
|
2000-10-04 10:54:47 +04:00
|
|
|
{
|
|
|
|
*aParent = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-05-14 07:43:54 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsExternalAppHandler::SetParentContentListener(nsIURIContentListener* aParent)
|
2000-10-04 10:54:47 +04:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-05-14 07:43:54 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsExternalAppHandler::GetLoadCookie(nsISupports ** aLoadCookie)
|
2000-10-04 10:54:47 +04:00
|
|
|
{
|
|
|
|
*aLoadCookie = mLoadCookie;
|
|
|
|
NS_IF_ADDREF(*aLoadCookie);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-05-14 07:43:54 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsExternalAppHandler::SetLoadCookie(nsISupports * aLoadCookie)
|
2000-10-04 10:54:47 +04:00
|
|
|
{
|
|
|
|
mLoadCookie = aLoadCookie;
|
2000-07-07 05:48:05 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-09-08 04:12:51 +04:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// The following section contains our nsIMIMEService implementation and related methods.
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// nsIMIMEService methods
|
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::GetFromExtension(const char *aFileExt, nsIMIMEInfo **_retval)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
2000-09-15 10:07:58 +04:00
|
|
|
nsCAutoString fileExt(aFileExt);
|
|
|
|
if (fileExt.IsEmpty()) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
fileExt.ToLowerCase();
|
|
|
|
// if the file extension contains a '.', our hash key doesn't include the '.'
|
|
|
|
// so skip over it...
|
|
|
|
if (fileExt.First() == '.')
|
|
|
|
fileExt.Cut(0, 1); // cut the '.'
|
|
|
|
|
2001-03-02 12:26:57 +03:00
|
|
|
nsCStringKey key(fileExt.get());
|
2000-09-08 04:12:51 +04:00
|
|
|
|
2000-09-15 10:07:58 +04:00
|
|
|
*_retval = (nsIMIMEInfo *) mMimeInfoCache->Get(&key);
|
|
|
|
NS_IF_ADDREF(*_retval);
|
|
|
|
if (!*_retval) rv = NS_ERROR_FAILURE;
|
|
|
|
return rv;
|
|
|
|
}
|
2000-09-08 04:12:51 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::GetFromMIMEType(const char *aMIMEType, nsIMIMEInfo **_retval)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
2000-09-15 10:07:58 +04:00
|
|
|
nsCAutoString MIMEType(aMIMEType);
|
|
|
|
MIMEType.ToLowerCase();
|
|
|
|
|
2001-03-02 12:26:57 +03:00
|
|
|
nsCStringKey key(MIMEType.get());
|
2000-09-15 10:07:58 +04:00
|
|
|
|
|
|
|
*_retval = (nsIMIMEInfo *) mMimeInfoCache->Get(&key);
|
|
|
|
NS_IF_ADDREF(*_retval);
|
|
|
|
|
|
|
|
// if we don't have a match in our hash table, then query the user provided
|
|
|
|
// data source containing additional content types...
|
|
|
|
if (!*_retval)
|
|
|
|
rv = GetMIMEInfoForMimeTypeFromDS(aMIMEType, _retval);
|
|
|
|
|
|
|
|
// if we still don't have a match, then we give up, we don't know anything about it...
|
|
|
|
// return an error.
|
|
|
|
|
|
|
|
if (!*_retval) rv = NS_ERROR_FAILURE;
|
|
|
|
return rv;
|
2000-09-08 04:12:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::GetTypeFromExtension(const char *aFileExt, char **aContentType)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
2000-09-15 10:07:58 +04:00
|
|
|
nsCOMPtr<nsIMIMEInfo> info;
|
|
|
|
rv = GetFromExtension(aFileExt, getter_AddRefs(info));
|
2001-07-25 03:12:29 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
/* Try the plugins */
|
|
|
|
const char* mimeType;
|
|
|
|
nsCOMPtr<nsIPluginHost> pluginHost (do_GetService(kPluginManagerCID, &rv));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
if (pluginHost->IsPluginEnabledForExtension(aFileExt, mimeType) == NS_OK)
|
|
|
|
{
|
|
|
|
*aContentType = nsCRT::strdup(mimeType);
|
|
|
|
rv = NS_OK;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-11-17 05:13:33 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
rv = GetMIMEInfoForExtensionFromExtras(aFileExt, getter_AddRefs(info));
|
|
|
|
}
|
2001-07-25 03:12:29 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
} /* endif */
|
|
|
|
|
2000-09-15 10:07:58 +04:00
|
|
|
return info->GetMIMEType(aContentType);
|
2000-09-08 04:12:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::GetTypeFromURI(nsIURI *aURI, char **aContentType)
|
|
|
|
{
|
2000-09-15 10:07:58 +04:00
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
// first try to get a url out of the uri so we can skip post
|
|
|
|
// filename stuff (i.e. query string)
|
|
|
|
nsCOMPtr<nsIURL> url = do_QueryInterface(aURI, &rv);
|
|
|
|
|
|
|
|
#ifdef XP_MAC
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
nsXPIDLCString fileExt;
|
|
|
|
url->GetFileExtension(getter_Copies(fileExt));
|
|
|
|
|
|
|
|
nsresult rv2;
|
|
|
|
nsCOMPtr<nsIFileURL> fileurl = do_QueryInterface( url, &rv2 );
|
|
|
|
if ( NS_SUCCEEDED ( rv2 ) )
|
|
|
|
{
|
|
|
|
nsCOMPtr <nsIFile> file;
|
|
|
|
rv2 = fileurl->GetFile( getter_AddRefs( file ) );
|
|
|
|
if ( NS_SUCCEEDED( rv2 ) )
|
|
|
|
{
|
|
|
|
rv2 = GetTypeFromFile( file, aContentType );
|
|
|
|
if( NS_SUCCEEDED ( rv2 ) )
|
|
|
|
return rv2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
nsXPIDLCString ext;
|
|
|
|
rv = url->GetFileExtension(getter_Copies(ext));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
rv = GetTypeFromExtension(ext, aContentType);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsXPIDLCString cStrSpec;
|
|
|
|
// no url, let's give the raw spec a shot
|
|
|
|
rv = aURI->GetSpec(getter_Copies(cStrSpec));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsAutoString specStr; specStr.AssignWithConversion(cStrSpec);
|
|
|
|
|
|
|
|
// find the file extension (if any)
|
|
|
|
nsAutoString extStr;
|
|
|
|
PRInt32 extLoc = specStr.RFindChar('.');
|
|
|
|
if (-1 != extLoc)
|
|
|
|
{
|
|
|
|
specStr.Right(extStr, specStr.Length() - extLoc - 1);
|
2001-09-29 12:28:41 +04:00
|
|
|
char *ext = ToNewCString(extStr);
|
2000-09-15 10:07:58 +04:00
|
|
|
if (!ext) return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
rv = GetTypeFromExtension(ext, aContentType);
|
|
|
|
nsMemory::Free(ext);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
return rv;
|
2000-09-08 04:12:51 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::GetTypeFromFile( nsIFile* aFile, char **aContentType )
|
|
|
|
{
|
2000-09-15 10:07:58 +04:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIMIMEInfo> info;
|
|
|
|
|
|
|
|
// Get the Extension
|
|
|
|
char* fileName;
|
|
|
|
const char* ext = nsnull;
|
|
|
|
rv = aFile->GetLeafName(&fileName);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (fileName != nsnull)
|
|
|
|
{
|
|
|
|
PRInt32 len = nsCRT::strlen(fileName);
|
|
|
|
for (PRInt32 i = len; i >= 0; i--)
|
|
|
|
{
|
|
|
|
if (fileName[i] == '.')
|
|
|
|
{
|
|
|
|
ext = &fileName[i + 1];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-09-08 04:12:51 +04:00
|
|
|
|
2000-09-15 10:07:58 +04:00
|
|
|
nsCString fileExt( ext );
|
|
|
|
nsCRT::free(fileName);
|
|
|
|
// Handle the mac case
|
|
|
|
#ifdef XP_MAC
|
|
|
|
nsCOMPtr<nsILocalFileMac> macFile;
|
|
|
|
macFile = do_QueryInterface( aFile, &rv );
|
|
|
|
if ( NS_SUCCEEDED( rv ) && fileExt.IsEmpty())
|
|
|
|
{
|
|
|
|
PRUint32 type, creator;
|
|
|
|
macFile->GetFileTypeAndCreator( (OSType*)&type,(OSType*) &creator );
|
|
|
|
nsCOMPtr<nsIInternetConfigService> icService (do_GetService(NS_INTERNETCONFIGSERVICE_CONTRACTID));
|
|
|
|
if (icService)
|
|
|
|
{
|
2001-11-02 09:39:49 +03:00
|
|
|
rv = icService->GetMIMEInfoFromTypeCreator(type, creator, fileExt.get(), getter_AddRefs(info));
|
2000-09-15 10:07:58 +04:00
|
|
|
if ( NS_SUCCEEDED( rv) )
|
|
|
|
return info->GetMIMEType(aContentType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// Windows, unix and mac when no type match occured.
|
|
|
|
if (fileExt.IsEmpty())
|
|
|
|
return NS_ERROR_FAILURE;
|
2001-10-25 07:49:28 +04:00
|
|
|
return GetTypeFromExtension( fileExt.get(), aContentType );
|
2000-09-08 04:12:51 +04:00
|
|
|
}
|
2000-09-15 10:07:58 +04:00
|
|
|
|
2001-05-04 01:24:36 +04:00
|
|
|
nsresult nsExternalHelperAppService::GetMIMEInfoForMimeTypeFromExtras(const char * aContentType, nsIMIMEInfo ** aMIMEInfo )
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
NS_ENSURE_ARG( aMIMEInfo );
|
|
|
|
|
|
|
|
*aMIMEInfo = 0;
|
|
|
|
|
|
|
|
// Look for default entry with matching mime type.
|
|
|
|
nsCAutoString MIMEType(aContentType);
|
|
|
|
MIMEType.ToLowerCase();
|
|
|
|
PRInt32 numEntries = sizeof(extraMimeEntries) / sizeof(extraMimeEntries[0]);
|
|
|
|
for (PRInt32 index = 0; !*aMIMEInfo && index < numEntries; index++)
|
|
|
|
{
|
2001-11-02 09:39:49 +03:00
|
|
|
if ( MIMEType.Equals(extraMimeEntries[index].mMimeType) ) {
|
2001-05-04 01:24:36 +04:00
|
|
|
// This is the one. Create MIMEInfo object and set attributes appropriately.
|
|
|
|
nsCOMPtr<nsIMIMEInfo> mimeInfo (do_CreateInstance(NS_MIMEINFO_CONTRACTID,&rv));
|
|
|
|
NS_ENSURE_SUCCESS( rv, rv );
|
|
|
|
|
|
|
|
mimeInfo->SetFileExtensions(extraMimeEntries[index].mFileExtensions);
|
|
|
|
mimeInfo->SetMIMEType(extraMimeEntries[index].mMimeType);
|
|
|
|
mimeInfo->SetDescription(NS_ConvertASCIItoUCS2(extraMimeEntries[index].mDescription).get());
|
|
|
|
mimeInfo->SetMacType(extraMimeEntries[index].mMactype);
|
|
|
|
mimeInfo->SetMacCreator(extraMimeEntries[index].mMacCreator);
|
|
|
|
|
|
|
|
*aMIMEInfo = mimeInfo;
|
2001-11-08 22:05:22 +03:00
|
|
|
NS_ADDREF(*aMIMEInfo);
|
2001-05-04 01:24:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-17 05:13:33 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsExternalHelperAppService::GetMIMEInfoForExtensionFromExtras(const char* aExtension, nsIMIMEInfo ** aMIMEInfo )
|
|
|
|
{
|
|
|
|
nsresult rv = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
NS_ENSURE_ARG( aMIMEInfo );
|
|
|
|
|
|
|
|
*aMIMEInfo = 0;
|
|
|
|
|
|
|
|
// Look for default entry with matching extension.
|
|
|
|
nsDependentCString extension(aExtension);
|
|
|
|
nsDependentCString::const_iterator start, end, iter;
|
|
|
|
PRInt32 numEntries = sizeof(extraMimeEntries) / sizeof(extraMimeEntries[0]);
|
|
|
|
for (PRInt32 index = 0; !*aMIMEInfo && index < numEntries; index++)
|
|
|
|
{
|
|
|
|
nsDependentCString extList(extraMimeEntries[index].mFileExtensions);
|
|
|
|
extList.BeginReading(start);
|
|
|
|
extList.EndReading(end);
|
|
|
|
iter = start;
|
|
|
|
while (start != end)
|
|
|
|
{
|
|
|
|
FindCharInReadable(',', iter, end);
|
|
|
|
if (Compare(Substring(start, iter),
|
|
|
|
extension,
|
|
|
|
nsCaseInsensitiveCStringComparator()) == 0)
|
|
|
|
{
|
|
|
|
// This is the one. Create MIMEInfo object and set
|
|
|
|
// attributes appropriately.
|
|
|
|
nsCOMPtr<nsIMIMEInfo> mimeInfo (do_CreateInstance(NS_MIMEINFO_CONTRACTID,&rv));
|
|
|
|
NS_ENSURE_SUCCESS( rv, rv );
|
|
|
|
|
|
|
|
mimeInfo->SetFileExtensions(extraMimeEntries[index].mFileExtensions);
|
|
|
|
mimeInfo->SetMIMEType(extraMimeEntries[index].mMimeType);
|
|
|
|
mimeInfo->SetDescription(NS_ConvertASCIItoUCS2(extraMimeEntries[index].mDescription).get());
|
|
|
|
mimeInfo->SetMacType(extraMimeEntries[index].mMactype);
|
|
|
|
mimeInfo->SetMacCreator(extraMimeEntries[index].mMacCreator);
|
|
|
|
*aMIMEInfo = mimeInfo;
|
|
|
|
NS_ADDREF(*aMIMEInfo);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (iter != end) {
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
start = iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-05-04 01:24:36 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2000-09-15 10:07:58 +04:00
|
|
|
nsresult nsExternalHelperAppService::AddDefaultMimeTypesToCache()
|
|
|
|
{
|
|
|
|
PRInt32 numEntries = sizeof(defaultMimeEntries) / sizeof(defaultMimeEntries[0]);
|
|
|
|
for (PRInt32 index = 0; index < numEntries; index++)
|
|
|
|
{
|
|
|
|
// create a mime info object for each default mime entry and add it to our cache
|
|
|
|
nsCOMPtr<nsIMIMEInfo> mimeInfo (do_CreateInstance(NS_MIMEINFO_CONTRACTID));
|
|
|
|
mimeInfo->SetFileExtensions(defaultMimeEntries[index].mFileExtensions);
|
|
|
|
mimeInfo->SetMIMEType(defaultMimeEntries[index].mMimeType);
|
2001-03-08 11:05:05 +03:00
|
|
|
mimeInfo->SetDescription(NS_ConvertASCIItoUCS2(defaultMimeEntries[index].mDescription).get());
|
2000-09-15 10:07:58 +04:00
|
|
|
mimeInfo->SetMacType(defaultMimeEntries[index].mMactype);
|
|
|
|
mimeInfo->SetMacCreator(defaultMimeEntries[index].mMacCreator);
|
|
|
|
AddMimeInfoToCache(mimeInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsExternalHelperAppService::AddMimeInfoToCache(nsIMIMEInfo * aMIMEInfo)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG(aMIMEInfo);
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
// Next add the new root MIME mapping.
|
2001-09-25 02:39:17 +04:00
|
|
|
nsXPIDLCString mimeType;
|
|
|
|
nsIMIMEInfo* oldInfo;
|
|
|
|
rv = aMIMEInfo->GetMIMEType(getter_Copies(mimeType));
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCStringKey key(mimeType);
|
|
|
|
oldInfo = (nsIMIMEInfo*)mMimeInfoCache->Put(&key, aMIMEInfo);
|
2000-09-15 10:07:58 +04:00
|
|
|
|
2001-11-28 00:21:34 +03:00
|
|
|
// release the old info to prevent a leak
|
|
|
|
NS_IF_RELEASE(oldInfo);
|
2001-09-25 02:39:17 +04:00
|
|
|
// add a reference for the hash table entry....
|
|
|
|
NS_ADDREF(aMIMEInfo);
|
|
|
|
}
|
2000-09-15 10:07:58 +04:00
|
|
|
|
|
|
|
// now we need to add entries for each file extension
|
|
|
|
char** extensions;
|
|
|
|
PRUint32 count;
|
|
|
|
rv = aMIMEInfo->GetFileExtensions(&count, &extensions );
|
|
|
|
if (NS_FAILED(rv)) return NS_OK;
|
|
|
|
|
|
|
|
for ( PRUint32 i = 0; i < count; i++ )
|
|
|
|
{
|
2001-09-25 02:39:17 +04:00
|
|
|
nsCStringKey key(extensions[i]);
|
2000-09-15 10:07:58 +04:00
|
|
|
oldInfo = (nsIMIMEInfo*) mMimeInfoCache->Put(&key, aMIMEInfo);
|
2001-11-28 00:21:34 +03:00
|
|
|
NS_IF_RELEASE(oldInfo); // release the old info to prevent a leak
|
2000-09-15 10:07:58 +04:00
|
|
|
NS_ADDREF(aMIMEInfo); // addref this new entry in the table
|
|
|
|
nsMemory::Free( extensions[i] );
|
|
|
|
}
|
|
|
|
nsMemory::Free( extensions );
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static helper function to help us release hash table entries...
|
|
|
|
static PRBool DeleteEntry(nsHashKey *aKey, void *aData, void* closure)
|
|
|
|
{
|
|
|
|
nsIMIMEInfo *entry = (nsIMIMEInfo*) aData;
|
|
|
|
NS_RELEASE(entry);
|
|
|
|
return PR_TRUE;
|
|
|
|
};
|
|
|
|
|