2003-10-10 17:43:15 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2004-07-15 22:56:42 +04:00
|
|
|
* vim:expandtab:shiftwidth=2:tabstop=2:cin:
|
2004-04-17 20:52:41 +04:00
|
|
|
* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
2000-06-17 01:35:13 +04:00
|
|
|
* The Original Code is the Mozilla browser.
|
2004-04-17 20:52:41 +04:00
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications, Inc.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1999
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
2000-06-17 01:35:13 +04:00
|
|
|
* Contributor(s):
|
|
|
|
* Scott MacGregor <mscott@netscape.com>
|
2001-05-04 01:24:36 +04:00
|
|
|
* Bill Law <law@netscape.com>
|
2003-09-12 00:44:25 +04:00
|
|
|
* Christian Biesinger <cbiesinger@web.de>
|
2004-04-17 20:52:41 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
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"
|
2002-03-06 10:48:55 +03:00
|
|
|
#include "nsIFileURL.h"
|
2003-03-15 04:04:32 +03:00
|
|
|
#include "nsIChannel.h"
|
2000-08-22 04:53:37 +04:00
|
|
|
#include "nsIDirectoryService.h"
|
|
|
|
#include "nsAppDirectoryServiceDefs.h"
|
2004-02-06 21:43:27 +03:00
|
|
|
#include "nsICategoryManager.h"
|
2000-06-17 04:06:26 +04:00
|
|
|
#include "nsXPIDLString.h"
|
2001-11-30 07:37:21 +03:00
|
|
|
#include "nsUnicharUtils.h"
|
2003-06-17 21:00:40 +04:00
|
|
|
#include "nsIStringEnumerator.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 "nsIWebProgressListener.h"
|
2005-01-04 16:40:51 +03:00
|
|
|
#include "nsITransfer.h"
|
2002-01-26 01:05:16 +03:00
|
|
|
#include "nsReadableUtils.h"
|
2003-07-01 18:16:06 +04:00
|
|
|
#include "nsIRequest.h"
|
2004-03-08 23:15:09 +03:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
2004-04-17 01:06:07 +04:00
|
|
|
#include "nsIInterfaceRequestor.h"
|
2004-09-21 23:52:45 +04:00
|
|
|
#include "nsAutoPtr.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 "nsHelperAppRDF.h"
|
|
|
|
#include "nsIMIMEInfo.h"
|
2000-08-12 00:31:57 +04:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
2004-08-10 01:27:24 +04:00
|
|
|
#include "nsIRefreshURI.h" // XXX needed to redirect according to Refresh: URI
|
|
|
|
#include "nsIDocumentLoader.h" // XXX needed to get orig. channel and assoc. refresh uri
|
2000-07-12 10:09:02 +04:00
|
|
|
#include "nsIHelperAppLauncherDialog.h"
|
2003-01-18 05:15:14 +03:00
|
|
|
#include "nsNetUtil.h"
|
2001-07-21 06:54:32 +04:00
|
|
|
#include "nsIIOService.h"
|
|
|
|
#include "nsNetCID.h"
|
2005-04-06 03:24:23 +04:00
|
|
|
#include "nsChannelProperties.h"
|
2000-06-17 01:35:13 +04:00
|
|
|
|
2000-09-15 10:07:58 +04:00
|
|
|
#include "nsMimeTypes.h"
|
2002-02-06 06:39:04 +03:00
|
|
|
// used for header disposition information.
|
2001-05-12 01:05:08 +04:00
|
|
|
#include "nsIHttpChannel.h"
|
2002-09-27 23:23:21 +04:00
|
|
|
#include "nsIEncodedChannel.h"
|
2002-02-06 06:39:04 +03:00
|
|
|
#include "nsIMultiPartChannel.h"
|
2003-01-22 09:57:15 +03:00
|
|
|
#include "nsIObserverService.h" // so we can be a profile change observer
|
2005-04-06 03:24:23 +04:00
|
|
|
#include "nsIPropertyBag2.h" // for the 64-bit content length
|
2000-09-15 10:07:58 +04:00
|
|
|
|
2002-10-09 05:16:38 +04:00
|
|
|
#if defined(XP_MAC) || defined (XP_MACOSX)
|
2000-09-07 01:12:11 +04:00
|
|
|
#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"
|
2003-04-26 01:18:10 +04:00
|
|
|
#endif // defined(XP_MAC) || defined (XP_MACOSX)
|
2000-09-07 01:12:11 +04:00
|
|
|
|
2004-08-10 01:27:24 +04:00
|
|
|
#include "nsIPluginHost.h" // XXX needed for ext->type mapping (bug 233289)
|
2001-09-08 16:47:05 +04:00
|
|
|
#include "nsEscape.h"
|
2001-07-25 03:12:29 +04:00
|
|
|
|
2004-08-10 01:27:24 +04:00
|
|
|
#include "nsIStringBundle.h" // XXX needed to localize error msgs
|
2004-10-14 21:06:32 +04:00
|
|
|
#include "nsIPrompt.h"
|
2002-02-20 11:00:29 +03:00
|
|
|
|
2004-08-10 01:27:24 +04:00
|
|
|
#include "nsITextToSubURI.h" // to unescape the filename
|
2003-06-13 01:57:49 +04:00
|
|
|
#include "nsIMIMEHeaderParam.h"
|
2002-08-12 23:23:22 +04:00
|
|
|
|
2003-01-12 04:14:35 +03:00
|
|
|
#include "nsIPrefService.h"
|
2004-10-25 11:46:01 +04:00
|
|
|
#include "nsIWindowWatcher.h"
|
2003-01-12 04:14:35 +03:00
|
|
|
|
2004-08-10 01:27:24 +04:00
|
|
|
#include "nsIGlobalHistory.h" // to mark downloads as visited
|
2003-06-30 23:00:46 +04:00
|
|
|
|
2003-03-15 04:04:32 +03:00
|
|
|
#include "nsCRT.h"
|
|
|
|
|
2003-07-25 20:48:00 +04:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
PRLogModuleInfo* nsExternalHelperAppService::mLog = nsnull;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Using level 3 here because the OSHelperAppServices use a log level
|
|
|
|
// of PR_LOG_DEBUG (4), and we want less detailed output here
|
|
|
|
// Using 3 instead of PR_LOG_WARN because we don't output warnings
|
|
|
|
#define LOG(args) PR_LOG(mLog, 3, args)
|
2003-11-15 18:01:39 +03:00
|
|
|
#define LOG_ENABLED() PR_LOG_TEST(mLog, 3)
|
2003-07-25 20:48:00 +04:00
|
|
|
|
2003-07-15 01:09:11 +04:00
|
|
|
static const char NEVER_ASK_PREF_BRANCH[] = "browser.helperApps.neverAsk.";
|
|
|
|
static const char NEVER_ASK_FOR_SAVE_TO_DISK_PREF[] = "saveToDisk";
|
|
|
|
static const char NEVER_ASK_FOR_OPEN_FILE_PREF[] = "openFile";
|
2001-06-06 12:17:39 +04:00
|
|
|
|
2000-06-21 12:29:13 +04:00
|
|
|
static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
|
2001-07-25 03:12:29 +04:00
|
|
|
static NS_DEFINE_CID(kPluginManagerCID, NS_PLUGINMANAGER_CID);
|
|
|
|
|
2004-03-06 22:37:22 +03:00
|
|
|
/**
|
|
|
|
* Contains a pointer to the helper app service, set in its constructor
|
|
|
|
*/
|
|
|
|
static nsExternalHelperAppService* sSrv;
|
|
|
|
|
2003-09-11 17:27:06 +04:00
|
|
|
// Helper functions for Content-Disposition headers
|
|
|
|
|
2004-08-10 01:27:24 +04:00
|
|
|
/**
|
|
|
|
* Given a URI fragment, unescape it
|
|
|
|
* @param aFragment The string to unescape
|
|
|
|
* @param aURI The URI from which this fragment is taken. Only its character set
|
|
|
|
* will be used.
|
|
|
|
* @param aResult [out] Unescaped string.
|
|
|
|
*/
|
|
|
|
static nsresult UnescapeFragment(const nsACString& aFragment, nsIURI* aURI,
|
|
|
|
nsAString& aResult)
|
|
|
|
{
|
|
|
|
// First, we need a charset
|
|
|
|
nsCAutoString originCharset;
|
|
|
|
nsresult rv = aURI->GetOriginCharset(originCharset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Now, we need the unescaper
|
|
|
|
nsCOMPtr<nsITextToSubURI> textToSubURI = do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return textToSubURI->UnEscapeURIForUI(originCharset, aFragment, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* UTF-8 version of UnescapeFragment.
|
|
|
|
* @param aFragment The string to unescape
|
|
|
|
* @param aURI The URI from which this fragment is taken. Only its character set
|
|
|
|
* will be used.
|
|
|
|
* @param aResult [out] Unescaped string, UTF-8 encoded.
|
|
|
|
* @note It is safe to pass the same string for aFragment and aResult.
|
|
|
|
* @note When this function fails, aResult will not be modified.
|
|
|
|
*/
|
|
|
|
static nsresult UnescapeFragment(const nsACString& aFragment, nsIURI* aURI,
|
|
|
|
nsACString& aResult)
|
|
|
|
{
|
|
|
|
nsAutoString result;
|
|
|
|
nsresult rv = UnescapeFragment(aFragment, aURI, result);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
CopyUTF16toUTF8(result, aResult);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2003-09-11 17:27:06 +04:00
|
|
|
/** Gets the content-disposition header from a channel, using nsIHttpChannel
|
|
|
|
* or nsIMultipartChannel if available
|
|
|
|
* @param aChannel The channel to extract the disposition header from
|
|
|
|
* @param aDisposition Reference to a string where the header is to be stored
|
|
|
|
*/
|
|
|
|
static void ExtractDisposition(nsIChannel* aChannel, nsACString& aDisposition)
|
|
|
|
{
|
|
|
|
aDisposition.Truncate();
|
|
|
|
// First see whether this is an http channel
|
|
|
|
nsCOMPtr<nsIHttpChannel> httpChannel(do_QueryInterface(aChannel));
|
|
|
|
if (httpChannel)
|
|
|
|
{
|
|
|
|
httpChannel->GetResponseHeader(NS_LITERAL_CSTRING("content-disposition"), aDisposition);
|
|
|
|
}
|
|
|
|
if (aDisposition.IsEmpty())
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIMultiPartChannel> multipartChannel(do_QueryInterface(aChannel));
|
|
|
|
if (multipartChannel)
|
|
|
|
{
|
|
|
|
multipartChannel->GetContentDisposition(aDisposition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Extracts the filename out of a content-disposition header
|
|
|
|
* @param aFilename [out] The filename. Can be empty on error.
|
|
|
|
* @param aDisposition Value of a Content-Disposition header
|
|
|
|
* @param aURI Optional. Will be used to get a fallback charset for the
|
|
|
|
* filename, if it is QI'able to nsIURL
|
|
|
|
* @param aMIMEHeaderParam Optional. Pointer to a nsIMIMEHeaderParam class, so
|
|
|
|
* that it doesn't need to be fetched by this function.
|
|
|
|
*/
|
|
|
|
static void GetFilenameFromDisposition(nsAString& aFilename,
|
|
|
|
const nsACString& aDisposition,
|
|
|
|
nsIURI* aURI = nsnull,
|
|
|
|
nsIMIMEHeaderParam* aMIMEHeaderParam = nsnull)
|
|
|
|
{
|
|
|
|
aFilename.Truncate();
|
|
|
|
nsCOMPtr<nsIMIMEHeaderParam> mimehdrpar(aMIMEHeaderParam);
|
|
|
|
if (!mimehdrpar) {
|
|
|
|
mimehdrpar = do_GetService(NS_MIMEHEADERPARAM_CONTRACTID);
|
|
|
|
if (!mimehdrpar)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURL> url = do_QueryInterface(aURI);
|
|
|
|
|
|
|
|
nsCAutoString fallbackCharset;
|
|
|
|
if (url)
|
|
|
|
url->GetOriginCharset(fallbackCharset);
|
|
|
|
// Get the value of 'filename' parameter
|
|
|
|
nsresult rv = mimehdrpar->GetParameter(aDisposition, "filename", fallbackCharset,
|
|
|
|
PR_TRUE, nsnull, aFilename);
|
|
|
|
if (NS_FAILED(rv) || aFilename.IsEmpty())
|
|
|
|
// Try 'name' parameter, instead.
|
|
|
|
rv = mimehdrpar->GetParameter(aDisposition, "name", fallbackCharset, PR_TRUE,
|
|
|
|
nsnull, aFilename);
|
|
|
|
}
|
|
|
|
|
2003-10-22 10:46:52 +04:00
|
|
|
/**
|
|
|
|
* Given a channel, returns the filename and extension the channel has.
|
|
|
|
* This uses the URL and other sources (nsIMultiPartChannel).
|
|
|
|
* Also gives back whether the channel requested external handling (i.e.
|
|
|
|
* whether Content-Disposition: attachment was sent)
|
|
|
|
* @param aChannel The channel to extract the filename/extension from
|
|
|
|
* @param aFileName [out] Reference to the string where the filename should be
|
|
|
|
* stored. Empty if it could not be retrieved.
|
|
|
|
* WARNING - this filename may contain characters which the OS does not
|
|
|
|
* allow as part of filenames!
|
|
|
|
* @param aExtension [out] Reference to the string where the extension should
|
2004-08-10 01:27:24 +04:00
|
|
|
* be stored. Empty if it could not be retrieved. Stored in UTF-8.
|
2003-10-22 10:46:52 +04:00
|
|
|
* @param aAllowURLExtension (optional) Get the extension from the URL if no
|
|
|
|
* Content-Disposition header is present. Default is true.
|
|
|
|
* @retval true The server sent Content-Disposition:attachment or equivalent
|
|
|
|
* @retval false Content-Disposition: inline or no content-disposition header
|
|
|
|
* was sent.
|
|
|
|
*/
|
|
|
|
static PRBool GetFilenameAndExtensionFromChannel(nsIChannel* aChannel,
|
2004-07-07 03:15:30 +04:00
|
|
|
nsString& aFileName,
|
|
|
|
nsCString& aExtension,
|
2003-10-22 10:46:52 +04:00
|
|
|
PRBool aAllowURLExtension = PR_TRUE)
|
|
|
|
{
|
2004-07-07 03:15:30 +04:00
|
|
|
aExtension.Truncate();
|
2003-10-22 10:46:52 +04:00
|
|
|
/*
|
|
|
|
* If the channel is an http or part of a multipart 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... otherwise just use our temp file
|
|
|
|
*/
|
|
|
|
nsCAutoString disp;
|
|
|
|
ExtractDisposition(aChannel, disp);
|
|
|
|
PRBool handleExternally = PR_FALSE;
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsresult rv;
|
|
|
|
aChannel->GetURI(getter_AddRefs(uri));
|
|
|
|
// content-disposition: has format:
|
|
|
|
// disposition-type < ; name=value >* < ; filename=value > < ; name=value >*
|
|
|
|
if (!disp.IsEmpty())
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIMIMEHeaderParam> mimehdrpar = do_GetService(NS_MIMEHEADERPARAM_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return PR_FALSE;
|
2004-11-07 05:22:55 +03:00
|
|
|
|
|
|
|
nsCAutoString fallbackCharset;
|
|
|
|
uri->GetOriginCharset(fallbackCharset);
|
2003-10-22 10:46:52 +04:00
|
|
|
// Get the disposition type
|
2004-11-07 05:22:55 +03:00
|
|
|
nsAutoString dispToken;
|
|
|
|
rv = mimehdrpar->GetParameter(disp, "", fallbackCharset, PR_TRUE,
|
2003-10-22 10:46:52 +04:00
|
|
|
nsnull, dispToken);
|
|
|
|
// RFC 2183, section 2.8 says that an unknown disposition
|
|
|
|
// value should be treated as "attachment"
|
2004-12-04 20:46:49 +03:00
|
|
|
// XXXbz this code is duplicated in nsDocumentOpenInfo::DispatchContent.
|
|
|
|
// Factor it out! Maybe store it in the nsDocumentOpenInfo?
|
2003-10-22 10:46:52 +04:00
|
|
|
if (NS_FAILED(rv) ||
|
2004-12-04 20:46:49 +03:00
|
|
|
(// Some broken sites just send
|
|
|
|
// Content-Disposition: ; filename="file"
|
|
|
|
// screen those out here.
|
|
|
|
!dispToken.IsEmpty() &&
|
|
|
|
!dispToken.LowerCaseEqualsLiteral("inline") &&
|
2003-10-22 10:46:52 +04:00
|
|
|
// Broken sites just send
|
|
|
|
// Content-Disposition: filename="file"
|
|
|
|
// without a disposition token... screen those out.
|
2004-01-07 23:23:46 +03:00
|
|
|
!dispToken.EqualsIgnoreCase("filename", 8)) &&
|
|
|
|
// Also in use is Content-Disposition: name="file"
|
|
|
|
!dispToken.EqualsIgnoreCase("name", 4))
|
2003-10-22 10:46:52 +04:00
|
|
|
{
|
|
|
|
// We have a content-disposition of "attachment" or unknown
|
|
|
|
handleExternally = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We may not have a disposition type listed; some servers suck.
|
|
|
|
// But they could have listed a filename anyway.
|
|
|
|
GetFilenameFromDisposition(aFileName, disp, uri, mimehdrpar);
|
|
|
|
|
|
|
|
} // we had a disp header
|
|
|
|
|
|
|
|
// If the disposition header didn't work, try the filename from nsIURL
|
|
|
|
nsCOMPtr<nsIURL> url(do_QueryInterface(uri));
|
|
|
|
if (url && aFileName.IsEmpty())
|
|
|
|
{
|
2004-07-07 03:15:30 +04:00
|
|
|
if (aAllowURLExtension) {
|
2003-10-22 10:46:52 +04:00
|
|
|
url->GetFileExtension(aExtension);
|
2004-08-10 01:27:24 +04:00
|
|
|
UnescapeFragment(aExtension, url, aExtension);
|
|
|
|
|
2004-07-07 03:15:30 +04:00
|
|
|
// Windows ignores terminating dots. So we have to as well, so
|
|
|
|
// that our security checks do "the right thing"
|
|
|
|
// In case the aExtension consisted only of the dot, the code below will
|
|
|
|
// extract an aExtension from the filename
|
|
|
|
aExtension.Trim(".", PR_FALSE);
|
|
|
|
}
|
2003-10-22 10:46:52 +04:00
|
|
|
|
|
|
|
// try to extract the file name from the url and use that as a first pass as the
|
|
|
|
// leaf name of our temp file...
|
2004-08-10 01:27:24 +04:00
|
|
|
nsCAutoString leafName;
|
2003-10-22 10:46:52 +04:00
|
|
|
url->GetFileName(leafName);
|
|
|
|
if (!leafName.IsEmpty())
|
|
|
|
{
|
2004-08-10 01:27:24 +04:00
|
|
|
rv = UnescapeFragment(leafName, url, aFileName);
|
2003-10-22 10:46:52 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
CopyUTF8toUTF16(leafName, aFileName); // use escaped name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-07-07 03:15:30 +04:00
|
|
|
// Extract Extension, if we have a filename; otherwise,
|
|
|
|
// truncate the string
|
|
|
|
if (aExtension.IsEmpty()) {
|
|
|
|
if (!aFileName.IsEmpty())
|
|
|
|
{
|
|
|
|
// Windows ignores terminating dots. So we have to as well, so
|
|
|
|
// that our security checks do "the right thing"
|
|
|
|
aFileName.Trim(".", PR_FALSE);
|
|
|
|
|
|
|
|
// XXX RFindCharInReadable!!
|
|
|
|
nsAutoString fileNameStr(aFileName);
|
|
|
|
PRInt32 idx = fileNameStr.RFindChar(PRUnichar('.'));
|
|
|
|
if (idx != kNotFound)
|
|
|
|
CopyUTF16toUTF8(StringTail(fileNameStr, fileNameStr.Length() - idx - 1), aExtension);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-10-22 10:46:52 +04:00
|
|
|
return handleExternally;
|
|
|
|
}
|
2003-09-11 17:27:06 +04:00
|
|
|
|
2003-09-26 23:49:23 +04:00
|
|
|
/**
|
|
|
|
* Structure for storing extension->type mappings.
|
|
|
|
* @see defaultMimeEntries
|
|
|
|
*/
|
2003-07-15 02:30:52 +04:00
|
|
|
struct nsDefaultMimeTypeEntry {
|
|
|
|
const char* mMimeType;
|
|
|
|
const char* mFileExtension;
|
|
|
|
};
|
|
|
|
|
2003-09-26 23:49:23 +04:00
|
|
|
/**
|
|
|
|
* Default extension->mimetype mappings. These are not overridable.
|
2004-04-16 00:56:37 +04:00
|
|
|
* If you add types here, make sure they are lowercase, or you'll regret it.
|
2003-09-26 23:49:23 +04:00
|
|
|
*/
|
2000-09-15 10:07:58 +04:00
|
|
|
static nsDefaultMimeTypeEntry defaultMimeEntries [] =
|
|
|
|
{
|
2003-07-15 02:30:52 +04:00
|
|
|
// The following are those extensions that we're asked about during startup,
|
|
|
|
// sorted by order used
|
|
|
|
{ IMAGE_GIF, "gif" },
|
|
|
|
{ TEXT_XML, "xml" },
|
2004-08-08 05:31:02 +04:00
|
|
|
{ APPLICATION_RDF, "rdf" },
|
2003-07-15 02:30:52 +04:00
|
|
|
{ TEXT_XUL, "xul" },
|
2003-10-06 22:48:17 +04:00
|
|
|
{ IMAGE_PNG, "png" },
|
2003-07-15 02:30:52 +04:00
|
|
|
// -- end extensions used during startup
|
|
|
|
{ TEXT_CSS, "css" },
|
|
|
|
{ IMAGE_JPG, "jpeg" },
|
|
|
|
{ IMAGE_JPG, "jpg" },
|
|
|
|
{ TEXT_HTML, "html" },
|
|
|
|
{ TEXT_HTML, "htm" },
|
2003-09-14 15:41:22 +04:00
|
|
|
{ APPLICATION_XPINSTALL, "xpi" },
|
|
|
|
{ "application/xhtml+xml", "xhtml" },
|
2003-09-14 19:54:57 +04:00
|
|
|
{ "application/xhtml+xml", "xht" }
|
2003-07-15 02:30:52 +04:00
|
|
|
};
|
|
|
|
|
2003-09-26 23:49:23 +04:00
|
|
|
/**
|
|
|
|
* This is a small private struct used to help us initialize some
|
|
|
|
* default mime types.
|
|
|
|
*/
|
2003-07-15 02:30:52 +04:00
|
|
|
struct nsExtraMimeTypeEntry {
|
|
|
|
const char* mMimeType;
|
|
|
|
const char* mFileExtensions;
|
|
|
|
const char* mDescription;
|
|
|
|
PRUint32 mMactype;
|
|
|
|
PRUint32 mMacCreator;
|
2001-05-04 01:24:36 +04:00
|
|
|
};
|
|
|
|
|
2003-09-12 00:44:25 +04:00
|
|
|
#if defined(XP_MACOSX) || defined(XP_MAC)
|
|
|
|
#define MAC_TYPE(x) x
|
|
|
|
#else
|
|
|
|
#define MAC_TYPE(x) 0
|
|
|
|
#endif
|
|
|
|
|
2003-09-26 23:49:23 +04:00
|
|
|
/**
|
2004-08-10 01:27:24 +04:00
|
|
|
* This table lists all of the 'extra' content types that we can deduce from particular
|
2003-09-26 23:49:23 +04:00
|
|
|
* 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.
|
2004-04-16 00:56:37 +04:00
|
|
|
* If you add types here, make sure they are lowercase, or you'll regret it.
|
2003-09-26 23:49:23 +04:00
|
|
|
*/
|
2003-07-15 02:30:52 +04:00
|
|
|
static nsExtraMimeTypeEntry extraMimeEntries [] =
|
2001-05-04 01:24:36 +04:00
|
|
|
{
|
|
|
|
#if defined(VMS)
|
2003-12-30 13:45:12 +03:00
|
|
|
{ APPLICATION_OCTET_STREAM, "exe,com,bin,sav,bck,pcsi,dcx_axpexe,dcx_vaxexe,sfx_axpexe,sfx_vaxexe", "Binary File", 0, 0 },
|
2002-10-09 05:16:38 +04:00
|
|
|
#elif defined(XP_MAC) || defined (XP_MACOSX)// don't define .bin on the mac...use internet config to look that up...
|
2003-12-30 13:45:12 +03:00
|
|
|
{ APPLICATION_OCTET_STREAM, "exe,com", "Binary File", 0, 0 },
|
2001-05-04 01:24:36 +04:00
|
|
|
#else
|
2003-12-30 13:45:12 +03:00
|
|
|
{ APPLICATION_OCTET_STREAM, "exe,com,bin", "Binary File", 0, 0 },
|
2001-05-04 01:24:36 +04:00
|
|
|
#endif
|
|
|
|
{ APPLICATION_GZIP2, "gz", "gzip", 0, 0 },
|
2003-07-15 02:30:52 +04:00
|
|
|
{ "application/x-arj", "arj", "ARJ file", 0,0 },
|
2003-09-12 00:44:25 +04:00
|
|
|
{ APPLICATION_XPINSTALL, "xpi", "XPInstall Install", MAC_TYPE('xpi*'), MAC_TYPE('MOSS') },
|
|
|
|
{ APPLICATION_POSTSCRIPT, "ps,eps,ai", "Postscript File", 0, 0 },
|
|
|
|
{ APPLICATION_JAVASCRIPT, "js", "Javascript Source File", MAC_TYPE('TEXT'), MAC_TYPE('ttxt') },
|
2001-04-13 04:44:55 +04:00
|
|
|
{ IMAGE_ART, "art", "ART Image", 0, 0 },
|
2003-07-15 02:30:52 +04:00
|
|
|
{ IMAGE_BMP, "bmp", "BMP Image", 0, 0 },
|
|
|
|
{ IMAGE_GIF, "gif", "GIF Image", 0,0 },
|
|
|
|
{ IMAGE_ICO, "ico,cur", "ICO Image", 0, 0 },
|
|
|
|
{ IMAGE_JPG, "jpeg,jpg,jfif,pjpeg,pjp", "JPEG Image", 0, 0 },
|
|
|
|
{ IMAGE_PNG, "png", "PNG Image", 0, 0 },
|
2001-04-13 04:44:55 +04:00
|
|
|
{ IMAGE_TIFF, "tiff,tif", "TIFF Image", 0, 0 },
|
2003-07-15 02:30:52 +04:00
|
|
|
{ IMAGE_XBM, "xbm", "XBM Image", 0, 0 },
|
2003-09-12 00:44:25 +04:00
|
|
|
{ "image/svg+xml", "svg", "Scalable Vector Graphics", MAC_TYPE('svg '), MAC_TYPE('ttxt') },
|
|
|
|
{ MESSAGE_RFC822, "eml", "RFC-822 data", MAC_TYPE('TEXT'), MAC_TYPE('MOSS') },
|
|
|
|
{ TEXT_PLAIN, "txt,text", "Text File", MAC_TYPE('TEXT'), MAC_TYPE('ttxt') },
|
|
|
|
{ TEXT_HTML, "html,htm,shtml,ehtml", "HyperText Markup Language", MAC_TYPE('TEXT'), MAC_TYPE('MOSS') },
|
|
|
|
{ "application/xhtml+xml", "xhtml,xht", "Extensible HyperText Markup Language", MAC_TYPE('TEXT'), MAC_TYPE('ttxt') },
|
2004-08-08 05:31:02 +04:00
|
|
|
{ APPLICATION_RDF, "rdf", "Resource Description Framework", MAC_TYPE('TEXT'), MAC_TYPE('ttxt') },
|
2003-09-12 00:44:25 +04:00
|
|
|
{ TEXT_XUL, "xul", "XML-Based User Interface Language", MAC_TYPE('TEXT'), MAC_TYPE('ttxt') },
|
|
|
|
{ TEXT_XML, "xml,xsl,xbl", "Extensible Markup Language", MAC_TYPE('TEXT'), MAC_TYPE('ttxt') },
|
|
|
|
{ TEXT_CSS, "css", "Style Sheet", MAC_TYPE('TEXT'), MAC_TYPE('ttxt') },
|
2000-09-15 10:07:58 +04:00
|
|
|
};
|
|
|
|
|
2003-09-12 00:44:25 +04:00
|
|
|
#undef MAC_TYPE
|
|
|
|
|
2003-09-26 23:49:23 +04:00
|
|
|
/**
|
|
|
|
* File extensions for which decoding should be disabled.
|
2004-08-10 01:27:24 +04:00
|
|
|
* NOTE: These MUST be lower-case and ASCII.
|
2003-09-26 23:49:23 +04:00
|
|
|
*/
|
2003-10-09 06:10:25 +04:00
|
|
|
static nsDefaultMimeTypeEntry nonDecodableExtensions [] = {
|
|
|
|
{ APPLICATION_GZIP, "gz" },
|
|
|
|
{ APPLICATION_GZIP, "tgz" },
|
|
|
|
{ APPLICATION_ZIP, "zip" },
|
2005-02-26 00:13:36 +03:00
|
|
|
{ APPLICATION_COMPRESS, "z" },
|
|
|
|
{ APPLICATION_GZIP, "svgz" }
|
2002-03-14 03:51:28 +03:00
|
|
|
};
|
|
|
|
|
2004-01-16 21:22:55 +03:00
|
|
|
NS_IMPL_ISUPPORTS6(
|
2003-01-22 09:57:15 +03:00
|
|
|
nsExternalHelperAppService,
|
|
|
|
nsIExternalHelperAppService,
|
|
|
|
nsPIExternalAppLauncher,
|
|
|
|
nsIExternalProtocolService,
|
|
|
|
nsIMIMEService,
|
|
|
|
nsIObserver,
|
|
|
|
nsISupportsWeakReference)
|
|
|
|
|
|
|
|
nsExternalHelperAppService::nsExternalHelperAppService()
|
|
|
|
: mDataSourceInitialized(PR_FALSE)
|
|
|
|
{
|
2004-03-06 22:37:22 +03:00
|
|
|
sSrv = this;
|
2003-01-22 09:57:15 +03:00
|
|
|
}
|
|
|
|
nsresult nsExternalHelperAppService::Init()
|
2000-06-17 01:35:13 +04:00
|
|
|
{
|
2003-07-15 01:09:11 +04:00
|
|
|
// Add an observer for profile change
|
2001-07-11 07:23:24 +04:00
|
|
|
nsresult rv = NS_OK;
|
2001-10-23 02:01:27 +04:00
|
|
|
nsCOMPtr<nsIObserverService> obs = do_GetService("@mozilla.org/observer-service;1", &rv);
|
2003-01-22 09:57:15 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-07-11 07:23:24 +04:00
|
|
|
|
2003-07-25 20:48:00 +04:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (!mLog) {
|
|
|
|
mLog = PR_NewLogModule("HelperAppService");
|
|
|
|
if (!mLog)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-01-22 09:57:15 +03:00
|
|
|
return obs->AddObserver(this, "profile-before-change", PR_TRUE);
|
2000-06-17 01:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsExternalHelperAppService::~nsExternalHelperAppService()
|
|
|
|
{
|
2004-03-06 22:37:22 +03:00
|
|
|
sSrv = nsnull;
|
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);
|
|
|
|
|
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.
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCAutoString urlSpec;
|
|
|
|
rv = NS_GetURLSpecFromFile(mimeTypesFile, urlSpec);
|
2000-06-23 02:17:25 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2000-06-21 12:29:13 +04:00
|
|
|
|
2001-12-06 07:07:26 +03:00
|
|
|
// Get the data source; if it is going to be created, then load is synchronous.
|
2002-03-06 10:48:55 +03:00
|
|
|
rv = rdf->GetDataSourceBlocking( urlSpec.get(), getter_AddRefs( mOverRideDataSource ) );
|
2000-06-21 12:29:13 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// initialize our resources if we haven't done so already...
|
|
|
|
if (!kNC_Description)
|
|
|
|
{
|
2003-02-25 22:02:00 +03:00
|
|
|
rdf->GetResource(NS_LITERAL_CSTRING(NC_RDF_DESCRIPTION),
|
|
|
|
getter_AddRefs(kNC_Description));
|
|
|
|
rdf->GetResource(NS_LITERAL_CSTRING(NC_RDF_VALUE),
|
|
|
|
getter_AddRefs(kNC_Value));
|
|
|
|
rdf->GetResource(NS_LITERAL_CSTRING(NC_RDF_FILEEXTENSIONS),
|
|
|
|
getter_AddRefs(kNC_FileExtensions));
|
|
|
|
rdf->GetResource(NS_LITERAL_CSTRING(NC_RDF_PATH),
|
|
|
|
getter_AddRefs(kNC_Path));
|
|
|
|
rdf->GetResource(NS_LITERAL_CSTRING(NC_RDF_SAVETODISK),
|
|
|
|
getter_AddRefs(kNC_SaveToDisk));
|
|
|
|
rdf->GetResource(NS_LITERAL_CSTRING(NC_RDF_USESYSTEMDEFAULT),
|
|
|
|
getter_AddRefs(kNC_UseSystemDefault));
|
|
|
|
rdf->GetResource(NS_LITERAL_CSTRING(NC_RDF_HANDLEINTERNAL),
|
|
|
|
getter_AddRefs(kNC_HandleInternal));
|
|
|
|
rdf->GetResource(NS_LITERAL_CSTRING(NC_RDF_ALWAYSASK),
|
|
|
|
getter_AddRefs(kNC_AlwaysAsk));
|
|
|
|
rdf->GetResource(NS_LITERAL_CSTRING(NC_RDF_PRETTYNAME),
|
|
|
|
getter_AddRefs(kNC_PrettyName));
|
2000-06-21 12:29:13 +04:00
|
|
|
}
|
2000-09-08 04:12:51 +04:00
|
|
|
|
|
|
|
mDataSourceInitialized = PR_TRUE;
|
2003-01-22 09:57:15 +03:00
|
|
|
|
2000-07-07 05:48:05 +04:00
|
|
|
return rv;
|
2000-06-21 12:29:13 +04:00
|
|
|
}
|
|
|
|
|
2004-04-16 00:56:37 +04:00
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::DoContent(const nsACString& aMimeContentType,
|
2003-07-01 18:16:06 +04:00
|
|
|
nsIRequest *aRequest,
|
2004-04-17 01:06:07 +04:00
|
|
|
nsIInterfaceRequestor *aWindowContext,
|
2003-07-01 18:16:06 +04:00
|
|
|
nsIStreamListener ** aStreamListener)
|
2000-06-17 01:35:13 +04:00
|
|
|
{
|
2003-10-22 10:46:52 +04:00
|
|
|
nsAutoString fileName;
|
2003-07-25 20:48:00 +04:00
|
|
|
nsCAutoString fileExtension;
|
2005-03-13 20:03:44 +03:00
|
|
|
PRUint32 reason = nsIHelperAppLauncherDialog::REASON_CANTHANDLE;
|
2003-09-25 00:21:09 +04:00
|
|
|
nsresult rv;
|
2000-09-08 04:12:51 +04:00
|
|
|
|
2003-10-22 10:46:52 +04:00
|
|
|
// Get the file extension and name that we will need later
|
2003-07-01 18:16:06 +04:00
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
|
|
|
|
if (channel) {
|
2003-10-22 10:46:52 +04:00
|
|
|
// Check if we have a POST request, in which case we don't want to use
|
|
|
|
// the url's extension
|
|
|
|
PRBool allowURLExt = PR_TRUE;
|
2003-07-01 18:16:06 +04:00
|
|
|
nsCOMPtr<nsIHttpChannel> httpChan = do_QueryInterface(channel);
|
|
|
|
if (httpChan) {
|
|
|
|
nsCAutoString requestMethod;
|
|
|
|
httpChan->GetRequestMethod(requestMethod);
|
2003-10-22 10:46:52 +04:00
|
|
|
allowURLExt = !requestMethod.Equals("POST");
|
2003-07-01 18:16:06 +04:00
|
|
|
}
|
2000-06-21 12:29:13 +04:00
|
|
|
|
2003-07-25 20:48:00 +04:00
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
channel->GetURI(getter_AddRefs(uri));
|
|
|
|
|
2003-10-22 10:46:52 +04:00
|
|
|
// Check if we had a query string - we don't want to check the URL
|
|
|
|
// extension if a query is present in the URI
|
|
|
|
// If we already know we don't want to check the URL extension, don't
|
|
|
|
// bother checking the query
|
|
|
|
if (uri && allowURLExt) {
|
2003-09-12 00:44:25 +04:00
|
|
|
nsCOMPtr<nsIURL> url = do_QueryInterface(uri);
|
2003-07-25 20:48:00 +04:00
|
|
|
|
|
|
|
if (url) {
|
|
|
|
nsCAutoString query;
|
2003-09-25 00:21:09 +04:00
|
|
|
|
|
|
|
// We only care about the query for HTTP and HTTPS URLs
|
|
|
|
PRBool isHTTP, isHTTPS;
|
|
|
|
rv = uri->SchemeIs("http", &isHTTP);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
isHTTP = PR_FALSE;
|
|
|
|
rv = uri->SchemeIs("https", &isHTTPS);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
isHTTPS = PR_FALSE;
|
|
|
|
|
|
|
|
if (isHTTP || isHTTPS)
|
|
|
|
url->GetQuery(query);
|
|
|
|
|
2003-07-25 20:48:00 +04:00
|
|
|
// Only get the extension if the query is empty; if it isn't, then the
|
|
|
|
// extension likely belongs to a cgi script and isn't helpful
|
2003-10-22 10:46:52 +04:00
|
|
|
allowURLExt = query.IsEmpty();
|
2003-01-12 04:14:35 +03:00
|
|
|
}
|
|
|
|
}
|
2003-10-22 10:46:52 +04:00
|
|
|
// Extract name & extension
|
2005-03-13 20:03:44 +03:00
|
|
|
PRBool isAttachment = GetFilenameAndExtensionFromChannel(channel, fileName,
|
|
|
|
fileExtension,
|
|
|
|
allowURLExt);
|
2004-01-29 01:05:25 +03:00
|
|
|
LOG(("Found extension '%s' (filename is '%s', handling attachment: %i)",
|
2003-10-22 10:46:52 +04:00
|
|
|
fileExtension.get(), NS_ConvertUTF16toUTF8(fileName).get(),
|
2004-01-29 01:05:25 +03:00
|
|
|
isAttachment));
|
2005-03-13 20:03:44 +03:00
|
|
|
if (isAttachment)
|
|
|
|
reason = nsIHelperAppLauncherDialog::REASON_SERVERREQUEST;
|
2003-01-12 04:14:35 +03:00
|
|
|
}
|
2000-06-21 12:29:13 +04:00
|
|
|
|
2003-07-25 20:48:00 +04:00
|
|
|
LOG(("HelperAppService::DoContent: mime '%s', extension '%s'\n",
|
2004-04-16 00:56:37 +04:00
|
|
|
PromiseFlatCString(aMimeContentType).get(), fileExtension.get()));
|
2003-07-25 20:48:00 +04:00
|
|
|
|
2003-11-15 18:01:39 +03:00
|
|
|
// Try to find a mime object by looking at the mime type/extension
|
2003-09-12 00:44:25 +04:00
|
|
|
nsCOMPtr<nsIMIMEInfo> mimeInfo;
|
2004-04-16 00:56:37 +04:00
|
|
|
if (aMimeContentType.Equals(APPLICATION_GUESS_FROM_EXT, nsCaseInsensitiveCStringComparator())) {
|
|
|
|
nsCAutoString mimeType;
|
2004-01-08 06:56:02 +03:00
|
|
|
if (!fileExtension.IsEmpty()) {
|
2004-04-16 00:56:37 +04:00
|
|
|
GetFromTypeAndExtension(EmptyCString(), fileExtension, getter_AddRefs(mimeInfo));
|
2004-01-08 06:56:02 +03:00
|
|
|
if (mimeInfo) {
|
2004-04-16 00:56:37 +04:00
|
|
|
mimeInfo->GetMIMEType(mimeType);
|
2004-01-08 06:56:02 +03:00
|
|
|
|
|
|
|
LOG(("OS-Provided mime type '%s' for extension '%s'\n",
|
|
|
|
mimeType.get(), fileExtension.get()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileExtension.IsEmpty() || mimeType.IsEmpty()) {
|
|
|
|
// Extension lookup gave us no useful match
|
2004-04-16 00:56:37 +04:00
|
|
|
GetFromTypeAndExtension(NS_LITERAL_CSTRING(APPLICATION_OCTET_STREAM), fileExtension,
|
2004-01-08 06:56:02 +03:00
|
|
|
getter_AddRefs(mimeInfo));
|
2005-03-07 22:18:12 +03:00
|
|
|
mimeType.AssignLiteral(APPLICATION_OCTET_STREAM);
|
2004-01-08 06:56:02 +03:00
|
|
|
}
|
2005-03-07 22:18:12 +03:00
|
|
|
if (channel)
|
|
|
|
channel->SetContentType(mimeType);
|
2005-03-13 20:03:44 +03:00
|
|
|
// Don't overwrite SERVERREQUEST
|
|
|
|
if (reason == nsIHelperAppLauncherDialog::REASON_CANTHANDLE)
|
|
|
|
reason = nsIHelperAppLauncherDialog::REASON_TYPESNIFFED;
|
2004-01-08 06:56:02 +03:00
|
|
|
}
|
|
|
|
else {
|
2004-04-16 00:56:37 +04:00
|
|
|
GetFromTypeAndExtension(aMimeContentType, fileExtension,
|
2004-01-08 06:56:02 +03:00
|
|
|
getter_AddRefs(mimeInfo));
|
|
|
|
}
|
2003-07-25 20:48:00 +04:00
|
|
|
LOG(("Type/Ext lookup found 0x%p\n", mimeInfo.get()));
|
|
|
|
|
2003-11-15 18:01:39 +03:00
|
|
|
// No mimeinfo -> we can't continue. probably OOM.
|
2001-02-07 05:43:18 +03:00
|
|
|
if (!mimeInfo)
|
2003-11-15 18:01:39 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2000-06-21 12:29:13 +04:00
|
|
|
|
2001-02-07 05:43:18 +03:00
|
|
|
*aStreamListener = nsnull;
|
2003-07-27 01:01:08 +04:00
|
|
|
// We want the mimeInfo's primary extension to pass it to
|
2005-03-13 20:03:44 +03:00
|
|
|
// nsExternalAppHandler
|
2004-04-16 00:56:37 +04:00
|
|
|
nsCAutoString buf;
|
|
|
|
mimeInfo->GetPrimaryExtension(buf);
|
2003-07-25 20:48:00 +04:00
|
|
|
|
2005-03-13 20:03:44 +03:00
|
|
|
nsExternalAppHandler * handler = new nsExternalAppHandler(mimeInfo,
|
2004-04-16 00:56:37 +04:00
|
|
|
buf,
|
2005-03-13 20:03:44 +03:00
|
|
|
aWindowContext,
|
2003-10-22 10:46:52 +04:00
|
|
|
fileName,
|
2005-03-13 20:03:44 +03:00
|
|
|
reason);
|
2003-07-15 01:09:11 +04:00
|
|
|
if (!handler)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(*aStreamListener = handler);
|
|
|
|
|
2001-02-07 05:43:18 +03:00
|
|
|
return NS_OK;
|
2000-06-17 01:35:13 +04:00
|
|
|
}
|
|
|
|
|
2004-04-16 00:56:37 +04:00
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::ApplyDecodingForExtension(const nsACString& aExtension,
|
|
|
|
const nsACString& aEncodingType,
|
|
|
|
PRBool *aApplyDecoding)
|
2002-03-14 03:51:28 +03:00
|
|
|
{
|
|
|
|
*aApplyDecoding = PR_TRUE;
|
2003-10-09 06:10:25 +04:00
|
|
|
PRUint32 i;
|
|
|
|
for(i = 0; i < NS_ARRAY_LENGTH(nonDecodableExtensions); ++i) {
|
2004-08-10 01:27:24 +04:00
|
|
|
if (aExtension.LowerCaseEqualsASCII(nonDecodableExtensions[i].mFileExtension) &&
|
|
|
|
aEncodingType.LowerCaseEqualsASCII(nonDecodableExtensions[i].mMimeType)) {
|
2002-03-14 03:51:28 +03:00
|
|
|
*aApplyDecoding = PR_FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-06-11 19:47:57 +04:00
|
|
|
nsresult nsExternalHelperAppService::FillTopLevelProperties(nsIRDFResource * aContentTypeNodeResource,
|
2000-06-21 12:29:13 +04:00
|
|
|
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;
|
|
|
|
|
2003-11-15 18:01:39 +03:00
|
|
|
// set the pretty name description, if nonempty
|
2000-06-21 12:29:13 +04:00
|
|
|
FillLiteralValueFromTarget(aContentTypeNodeResource,kNC_Description, &stringValue);
|
2003-11-15 18:01:39 +03:00
|
|
|
if (stringValue && *stringValue)
|
2004-04-16 00:56:37 +04:00
|
|
|
aMIMEInfo->SetDescription(nsDependentString(stringValue));
|
2000-06-21 12:29:13 +04:00
|
|
|
|
|
|
|
// 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);
|
2004-08-10 01:27:24 +04:00
|
|
|
CopyUTF16toUTF8(stringValue, fileExtension);
|
2000-06-21 12:29:13 +04:00
|
|
|
if (!fileExtension.IsEmpty())
|
2004-04-16 00:56:37 +04:00
|
|
|
aMIMEInfo->AppendExtension(fileExtension);
|
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;
|
|
|
|
|
2003-01-12 04:14:35 +03:00
|
|
|
nsCAutoString contentTypeHandlerNodeName(NC_CONTENT_NODE_HANDLER_PREFIX);
|
2000-06-21 12:29:13 +04:00
|
|
|
contentTypeHandlerNodeName.Append(aContentType);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRDFResource> contentTypeHandlerNodeResource;
|
2003-02-25 22:02:00 +03:00
|
|
|
aRDFService->GetResource(contentTypeHandlerNodeName, 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);
|
2003-07-30 21:47:26 +04:00
|
|
|
NS_NAMED_LITERAL_STRING(trueString, "true");
|
|
|
|
NS_NAMED_LITERAL_STRING(falseString, "false");
|
2001-11-30 07:37:21 +03:00
|
|
|
if (stringValue && trueString.Equals(stringValue))
|
2000-06-21 12:29:13 +04:00
|
|
|
aMIMEInfo->SetPreferredAction(nsIMIMEInfo::saveToDisk);
|
|
|
|
|
2003-01-12 04:14:35 +03:00
|
|
|
// use system default
|
|
|
|
FillLiteralValueFromTarget(contentTypeHandlerNodeResource,kNC_UseSystemDefault, &stringValue);
|
|
|
|
if (stringValue && trueString.Equals(stringValue))
|
|
|
|
aMIMEInfo->SetPreferredAction(nsIMIMEInfo::useSystemDefault);
|
|
|
|
|
2000-06-21 12:29:13 +04:00
|
|
|
// handle internal
|
|
|
|
FillLiteralValueFromTarget(contentTypeHandlerNodeResource,kNC_HandleInternal, &stringValue);
|
2001-11-30 07:37:21 +03:00
|
|
|
if (stringValue && trueString.Equals(stringValue))
|
2000-06-21 12:29:13 +04:00
|
|
|
aMIMEInfo->SetPreferredAction(nsIMIMEInfo::handleInternally);
|
|
|
|
|
2003-01-12 04:14:35 +03:00
|
|
|
// always ask
|
|
|
|
FillLiteralValueFromTarget(contentTypeHandlerNodeResource,kNC_AlwaysAsk, &stringValue);
|
2003-07-30 21:47:26 +04:00
|
|
|
// Only skip asking if we are absolutely sure the user does not want
|
|
|
|
// to be asked. Any sort of bofus data should mean we ask.
|
|
|
|
aMIMEInfo->SetAlwaysAskBeforeHandling(!stringValue ||
|
2003-07-30 22:56:12 +04:00
|
|
|
!falseString.Equals(stringValue));
|
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;
|
2003-02-25 22:02:00 +03:00
|
|
|
aRDFService->GetResource(externalAppNodeName, getter_AddRefs(externalAppNodeResource));
|
2000-06-21 12:29:13 +04:00
|
|
|
|
2003-11-15 18:01:39 +03:00
|
|
|
// Clear out any possibly set preferred application, to match the datasource
|
2004-04-16 00:56:37 +04:00
|
|
|
aMIMEInfo->SetApplicationDescription(EmptyString());
|
2003-11-15 18:01:39 +03:00
|
|
|
aMIMEInfo->SetPreferredApplicationHandler(nsnull);
|
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)
|
2004-04-16 00:56:37 +04:00
|
|
|
aMIMEInfo->SetApplicationDescription(nsDependentString(stringValue));
|
2000-06-21 12:29:13 +04:00
|
|
|
|
|
|
|
FillLiteralValueFromTarget(externalAppNodeResource, kNC_Path, &stringValue);
|
2003-01-12 04:14:35 +03:00
|
|
|
if (stringValue && stringValue[0])
|
2000-06-21 12:29:13 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIFile> application;
|
|
|
|
GetFileTokenForPath(stringValue, getter_AddRefs(application));
|
|
|
|
if (application)
|
|
|
|
aMIMEInfo->SetPreferredApplicationHandler(application);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2003-01-12 04:14:35 +03:00
|
|
|
PRBool nsExternalHelperAppService::MIMETypeIsInDataSource(const char * aContentType)
|
|
|
|
{
|
|
|
|
nsresult rv = InitDataSource();
|
|
|
|
if (NS_FAILED(rv)) return PR_FALSE;
|
|
|
|
|
|
|
|
if (mOverRideDataSource)
|
|
|
|
{
|
|
|
|
// Get the RDF service.
|
|
|
|
nsCOMPtr<nsIRDFService> rdf = do_GetService(kRDFServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return PR_FALSE;
|
|
|
|
|
|
|
|
// Build uri for the mimetype resource.
|
|
|
|
nsCAutoString contentTypeNodeName(NC_CONTENT_NODE_PREFIX);
|
|
|
|
nsCAutoString contentType(aContentType);
|
|
|
|
ToLowerCase(contentType);
|
|
|
|
contentTypeNodeName.Append(contentType);
|
|
|
|
|
|
|
|
// Get the mime type resource.
|
|
|
|
nsCOMPtr<nsIRDFResource> contentTypeNodeResource;
|
2003-02-25 22:02:00 +03:00
|
|
|
rv = rdf->GetResource(contentTypeNodeName, getter_AddRefs(contentTypeNodeResource));
|
2003-01-12 04:14:35 +03:00
|
|
|
if (NS_FAILED(rv)) return PR_FALSE;
|
|
|
|
|
|
|
|
// Test that there's a #value arc from the mimetype resource to the mimetype literal string.
|
|
|
|
nsCOMPtr<nsIRDFLiteral> mimeLiteral;
|
2004-06-11 19:47:57 +04:00
|
|
|
NS_ConvertUTF8toUTF16 mimeType(contentType);
|
2003-01-12 04:14:35 +03:00
|
|
|
rv = rdf->GetLiteral( mimeType.get(), getter_AddRefs( mimeLiteral ) );
|
|
|
|
if (NS_FAILED(rv)) return PR_FALSE;
|
|
|
|
|
|
|
|
PRBool exists = PR_FALSE;
|
|
|
|
rv = mOverRideDataSource->HasAssertion(contentTypeNodeResource, kNC_Value, mimeLiteral, PR_TRUE, &exists );
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) && exists) return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2004-04-16 00:56:37 +04:00
|
|
|
nsresult nsExternalHelperAppService::GetMIMEInfoForMimeTypeFromDS(const nsACString& aContentType, nsIMIMEInfo * aMIMEInfo)
|
2000-06-21 12:29:13 +04:00
|
|
|
{
|
2003-11-15 18:01:39 +03:00
|
|
|
NS_ENSURE_ARG_POINTER(aMIMEInfo);
|
2003-07-15 01:09:11 +04:00
|
|
|
nsresult rv = InitDataSource();
|
2000-09-08 04:12:51 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2003-07-15 01:09:11 +04:00
|
|
|
// can't do anything if we have no datasource...
|
|
|
|
if (!mOverRideDataSource)
|
|
|
|
return NS_ERROR_FAILURE;
|
2001-05-04 01:24:36 +04:00
|
|
|
|
2003-07-15 01:09:11 +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
|
|
|
|
2003-07-15 01:09:11 +04:00
|
|
|
|
|
|
|
// Build uri for the mimetype resource.
|
|
|
|
nsCAutoString contentTypeNodeName(NC_CONTENT_NODE_PREFIX);
|
|
|
|
nsCAutoString contentType(aContentType);
|
|
|
|
ToLowerCase(contentType);
|
|
|
|
contentTypeNodeName.Append(contentType);
|
|
|
|
|
|
|
|
// Get the mime type resource.
|
|
|
|
nsCOMPtr<nsIRDFResource> contentTypeNodeResource;
|
|
|
|
rv = rdf->GetResource(contentTypeNodeName, getter_AddRefs(contentTypeNodeResource));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2000-06-21 12:29:13 +04:00
|
|
|
|
2003-07-15 01:09:11 +04:00
|
|
|
// we need a way to determine if this content type resource is really in the graph or not...
|
|
|
|
// ...Test that there's a #value arc from the mimetype resource to the mimetype literal string.
|
|
|
|
nsCOMPtr<nsIRDFLiteral> mimeLiteral;
|
2004-06-11 19:47:57 +04:00
|
|
|
NS_ConvertUTF8toUTF16 mimeType(contentType);
|
2003-07-15 01:09:11 +04:00
|
|
|
rv = rdf->GetLiteral( mimeType.get(), getter_AddRefs( mimeLiteral ) );
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
PRBool exists = PR_FALSE;
|
|
|
|
rv = mOverRideDataSource->HasAssertion(contentTypeNodeResource, kNC_Value, mimeLiteral, PR_TRUE, &exists );
|
2000-06-21 12:29:13 +04:00
|
|
|
|
2003-07-15 01:09:11 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && exists)
|
|
|
|
{
|
2003-11-15 18:01:39 +03:00
|
|
|
// fill the mimeinfo in based on the values from the data source
|
2004-06-11 19:47:57 +04:00
|
|
|
rv = FillTopLevelProperties(contentTypeNodeResource, rdf, aMIMEInfo);
|
2003-07-15 01:09:11 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-11-15 18:01:39 +03:00
|
|
|
rv = FillContentHandlerProperties(contentType.get(), contentTypeNodeResource, rdf, aMIMEInfo);
|
2003-07-15 01:09:11 +04:00
|
|
|
|
|
|
|
} // if we have a node in the graph for this content type
|
2003-11-15 18:01:39 +03:00
|
|
|
// If we had success, but entry doesn't exist, we don't want to return success
|
|
|
|
else if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2003-07-15 01:09:11 +04:00
|
|
|
|
2000-06-21 12:29:13 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2004-04-16 00:56:37 +04:00
|
|
|
nsresult nsExternalHelperAppService::GetMIMEInfoForExtensionFromDS(const nsACString& aFileExtension, nsIMIMEInfo * aMIMEInfo)
|
2001-11-30 07:37:21 +03:00
|
|
|
{
|
2004-06-11 19:47:57 +04:00
|
|
|
nsCAutoString type;
|
|
|
|
PRBool found = GetTypeFromDS(aFileExtension, type);
|
|
|
|
if (!found)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2001-11-30 07:37:21 +03:00
|
|
|
|
2004-06-11 19:47:57 +04:00
|
|
|
return GetMIMEInfoForMimeTypeFromDS(type, aMIMEInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool nsExternalHelperAppService::GetTypeFromDS(const nsACString& aExtension,
|
|
|
|
nsACString& aType)
|
|
|
|
{
|
2003-11-15 18:01:39 +03:00
|
|
|
nsresult rv = InitDataSource();
|
2004-06-11 19:47:57 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return PR_FALSE;
|
2001-11-30 07:37:21 +03:00
|
|
|
|
2003-07-15 01:09:11 +04:00
|
|
|
// Can't do anything without a datasource
|
|
|
|
if (!mOverRideDataSource)
|
2004-06-11 19:47:57 +04:00
|
|
|
return PR_FALSE;
|
2001-11-30 07:37:21 +03:00
|
|
|
|
2003-07-15 01:09:11 +04:00
|
|
|
// Get the RDF service.
|
|
|
|
nsCOMPtr<nsIRDFService> rdf = do_GetService(kRDFServiceCID, &rv);
|
2004-06-11 19:47:57 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, PR_FALSE);
|
2001-11-30 07:37:21 +03:00
|
|
|
|
2004-06-11 19:47:57 +04:00
|
|
|
NS_ConvertUTF8toUTF16 extension(aExtension);
|
2003-07-15 01:09:11 +04:00
|
|
|
ToLowerCase(extension);
|
|
|
|
nsCOMPtr<nsIRDFLiteral> extensionLiteral;
|
2004-06-11 19:47:57 +04:00
|
|
|
rv = rdf->GetLiteral(extension.get(), getter_AddRefs(extensionLiteral));
|
|
|
|
NS_ENSURE_SUCCESS(rv, PR_FALSE);
|
2003-07-15 01:09:11 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIRDFResource> contentTypeNodeResource;
|
|
|
|
rv = mOverRideDataSource->GetSource(kNC_FileExtensions,
|
|
|
|
extensionLiteral,
|
|
|
|
PR_TRUE,
|
|
|
|
getter_AddRefs(contentTypeNodeResource));
|
|
|
|
nsCAutoString contentTypeStr;
|
|
|
|
if (NS_SUCCEEDED(rv) && contentTypeNodeResource)
|
|
|
|
{
|
|
|
|
const PRUnichar* contentType = nsnull;
|
|
|
|
rv = FillLiteralValueFromTarget(contentTypeNodeResource, kNC_Value, &contentType);
|
2004-06-11 19:47:57 +04:00
|
|
|
if (contentType) {
|
|
|
|
LossyCopyUTF16toASCII(contentType, aType);
|
|
|
|
return PR_TRUE;
|
2003-07-15 01:09:11 +04:00
|
|
|
}
|
|
|
|
} // if we have a node in the graph for this extension
|
2004-06-11 19:47:57 +04:00
|
|
|
return PR_FALSE;
|
2001-11-30 07:37:21 +03:00
|
|
|
}
|
|
|
|
|
2004-03-08 23:15:09 +03:00
|
|
|
nsresult nsExternalHelperAppService::GetFileTokenForPath(const PRUnichar * aPlatformAppPath,
|
|
|
|
nsIFile ** aFile)
|
2003-01-12 04:14:35 +03:00
|
|
|
{
|
2004-03-08 23:15:09 +03:00
|
|
|
nsDependentString platformAppPath(aPlatformAppPath);
|
|
|
|
// First, check if we have an absolute path
|
|
|
|
nsILocalFile* localFile = nsnull;
|
|
|
|
nsresult rv = NS_NewLocalFile(platformAppPath, PR_TRUE, &localFile);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
*aFile = localFile;
|
|
|
|
PRBool exists;
|
|
|
|
if (NS_FAILED((*aFile)->Exists(&exists)) || !exists) {
|
|
|
|
NS_RELEASE(*aFile);
|
|
|
|
return NS_ERROR_FILE_NOT_FOUND;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Second, check if file exists in mozilla program directory
|
|
|
|
rv = NS_GetSpecialDirectory(NS_XPCOM_CURRENT_PROCESS_DIR, aFile);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = (*aFile)->Append(platformAppPath);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
PRBool exists = PR_FALSE;
|
|
|
|
rv = (*aFile)->Exists(&exists);
|
|
|
|
if (NS_SUCCEEDED(rv) && exists)
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
NS_RELEASE(*aFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-31 23:32:26 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2003-01-12 04:14:35 +03:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2003-11-21 03:09:50 +03:00
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::IsExposedProtocol(const char * aProtocolScheme, PRBool * aResult)
|
|
|
|
{
|
|
|
|
// by default, no protocol is exposed. i.e., by default all link clicks must
|
|
|
|
// go through the external protocol service. most applications override this
|
|
|
|
// default behavior.
|
|
|
|
*aResult = PR_FALSE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrefBranch> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID);
|
|
|
|
if (prefs)
|
|
|
|
{
|
|
|
|
PRBool val;
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// check the per protocol setting first. it always takes precidence.
|
|
|
|
// if not set, then use the global setting.
|
|
|
|
|
|
|
|
nsCAutoString name;
|
|
|
|
name = NS_LITERAL_CSTRING("network.protocol-handler.expose.")
|
|
|
|
+ nsDependentCString(aProtocolScheme);
|
|
|
|
rv = prefs->GetBoolPref(name.get(), &val);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
*aResult = val;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rv = prefs->GetBoolPref("network.protocol-handler.expose-all", &val);
|
|
|
|
if (NS_SUCCEEDED(rv) && val)
|
|
|
|
*aResult = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-06-19 02:07:54 +04:00
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::LoadUrl(nsIURI * aURL)
|
|
|
|
{
|
2004-10-25 11:46:01 +04:00
|
|
|
return LoadURI(aURL, nsnull);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// nsExternalHelperAppService::LoadURI() may now pose a confirm dialog
|
|
|
|
// that existing callers aren't expecting. We must do it on an event
|
|
|
|
// callback to make sure we don't hang someone up.
|
|
|
|
|
|
|
|
struct extLoadRequest : PLEvent {
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
nsCOMPtr<nsIPrompt> prompt;
|
|
|
|
};
|
|
|
|
|
|
|
|
void *PR_CALLBACK
|
|
|
|
nsExternalHelperAppService::handleExternalLoadEvent(PLEvent *event)
|
|
|
|
{
|
|
|
|
extLoadRequest* req = NS_STATIC_CAST(extLoadRequest*, event);
|
|
|
|
if (req && sSrv && sSrv->isExternalLoadOK(req->uri, req->prompt))
|
|
|
|
sSrv->LoadUriInternal(req->uri);
|
|
|
|
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void PR_CALLBACK destroyExternalLoadEvent(PLEvent *event)
|
|
|
|
{
|
|
|
|
delete NS_STATIC_CAST(extLoadRequest*, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::LoadURI(nsIURI * aURL, nsIPrompt * aPrompt)
|
|
|
|
{
|
|
|
|
// post external load event
|
|
|
|
nsCOMPtr<nsIEventQueue> eventQ;
|
|
|
|
nsresult rv = NS_GetCurrentEventQ(getter_AddRefs(eventQ));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
extLoadRequest *event = new extLoadRequest;
|
|
|
|
if (!event)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
event->uri = aURL;
|
|
|
|
event->prompt = aPrompt;
|
|
|
|
PL_InitEvent(event, nsnull, handleExternalLoadEvent, destroyExternalLoadEvent);
|
|
|
|
|
|
|
|
rv = eventQ->PostEvent(event);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
PL_DestroyEvent(event);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper routines used by LoadURI to check whether we're allowed
|
|
|
|
// to load external schemes and whether or not to warn the user
|
|
|
|
|
|
|
|
static const char kExternalProtocolPrefPrefix[] = "network.protocol-handler.external.";
|
|
|
|
static const char kExternalProtocolDefaultPref[] = "network.protocol-handler.external-default";
|
|
|
|
static const char kExternalWarningPrefPrefix[] = "network.protocol-handler.warn-external.";
|
|
|
|
static const char kExternalWarningDefaultPref[] = "network.protocol-handler.warn-external-default";
|
|
|
|
|
|
|
|
|
|
|
|
PRBool nsExternalHelperAppService::isExternalLoadOK(nsIURI* aURL, nsIPrompt* aPrompt)
|
|
|
|
{
|
|
|
|
if (!aURL)
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
nsCAutoString scheme;
|
|
|
|
aURL->GetScheme(scheme);
|
|
|
|
if (scheme.IsEmpty())
|
|
|
|
return PR_FALSE; // must have a scheme
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrefBranch> prefs(do_GetService(NS_PREFSERVICE_CONTRACTID));
|
|
|
|
if (!prefs)
|
|
|
|
return PR_FALSE; // deny if we can't check prefs
|
|
|
|
|
|
|
|
|
|
|
|
// Deny load if the prefs say to do so
|
|
|
|
nsCAutoString externalPref(kExternalProtocolPrefPrefix);
|
|
|
|
externalPref += scheme;
|
|
|
|
PRBool allowLoad = PR_FALSE;
|
|
|
|
nsresult rv = prefs->GetBoolPref(externalPref.get(), &allowLoad);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
// no scheme-specific value, check the default
|
|
|
|
rv = prefs->GetBoolPref(kExternalProtocolDefaultPref, &allowLoad);
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv) || !allowLoad)
|
|
|
|
return PR_FALSE; // explicitly denied or missing default pref
|
|
|
|
|
|
|
|
|
|
|
|
// allowLoad is now true. See whether we have to ask the user
|
|
|
|
nsCAutoString warningPref(kExternalWarningPrefPrefix);
|
|
|
|
warningPref += scheme;
|
|
|
|
PRBool warn = PR_TRUE;
|
|
|
|
rv = prefs->GetBoolPref(warningPref.get(), &warn);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
// no scheme-specific value, check the default
|
|
|
|
rv = prefs->GetBoolPref(kExternalWarningDefaultPref, &warn);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (NS_FAILED(rv) || warn)
|
|
|
|
{
|
|
|
|
// explicit "warn" setting or missing default pref:
|
|
|
|
// we must ask the user before loading this type externally
|
|
|
|
PRBool remember = PR_FALSE;
|
|
|
|
allowLoad = promptForScheme(aURL, aPrompt, &remember);
|
|
|
|
|
|
|
|
if (remember)
|
|
|
|
{
|
|
|
|
if (allowLoad)
|
|
|
|
// suppress future warnings for this scheme
|
|
|
|
prefs->SetBoolPref(warningPref.get(), PR_FALSE);
|
|
|
|
else
|
|
|
|
// prevent externally loading this scheme in the future
|
|
|
|
prefs->SetBoolPref(externalPref.get(), PR_FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return allowLoad;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool nsExternalHelperAppService::promptForScheme(nsIURI* aURI,
|
|
|
|
nsIPrompt* aPrompt,
|
|
|
|
PRBool *aRemember)
|
|
|
|
{
|
|
|
|
// if no prompt passed in get one from the windowwatcher
|
|
|
|
nsCOMPtr<nsIPrompt> prompt(aPrompt);
|
|
|
|
if (!prompt)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIWindowWatcher> wwatch(do_GetService(NS_WINDOWWATCHER_CONTRACTID));
|
|
|
|
if (wwatch)
|
|
|
|
wwatch->GetNewPrompter(0, getter_AddRefs(prompt));
|
|
|
|
}
|
|
|
|
if (!prompt) {
|
|
|
|
NS_ERROR("No prompt to warn user about external load, denying");
|
|
|
|
return PR_FALSE; // told to warn but no prompt: deny
|
|
|
|
}
|
|
|
|
|
|
|
|
// load the strings we need
|
|
|
|
nsCOMPtr<nsIStringBundleService> sbSvc(do_GetService(NS_STRINGBUNDLE_CONTRACTID));
|
|
|
|
if (!sbSvc) {
|
|
|
|
NS_ERROR("Couldn't load StringBundleService");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStringBundle> appstrings;
|
|
|
|
nsresult rv = sbSvc->CreateBundle("chrome://global/locale/appstrings.properties",
|
|
|
|
getter_AddRefs(appstrings));
|
|
|
|
if (NS_FAILED(rv) || !appstrings) {
|
|
|
|
NS_ERROR("Failed to create appstrings.properties bundle");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCAutoString spec;
|
|
|
|
aURI->GetSpec(spec);
|
|
|
|
NS_ConvertUTF8toUTF16 uri(spec);
|
|
|
|
|
|
|
|
nsCAutoString asciischeme;
|
|
|
|
aURI->GetScheme(asciischeme);
|
|
|
|
NS_ConvertUTF8toUTF16 scheme(asciischeme);
|
|
|
|
|
|
|
|
nsXPIDLString desc;
|
|
|
|
GetApplicationDescription(asciischeme, desc);
|
|
|
|
|
|
|
|
nsXPIDLString title;
|
|
|
|
appstrings->GetStringFromName(NS_LITERAL_STRING("externalProtocolTitle").get(),
|
|
|
|
getter_Copies(title));
|
|
|
|
nsXPIDLString checkMsg;
|
|
|
|
appstrings->GetStringFromName(NS_LITERAL_STRING("externalProtocolChkMsg").get(),
|
|
|
|
getter_Copies(checkMsg));
|
|
|
|
nsXPIDLString launchBtn;
|
|
|
|
appstrings->GetStringFromName(NS_LITERAL_STRING("externalProtocolLaunchBtn").get(),
|
|
|
|
getter_Copies(launchBtn));
|
|
|
|
|
|
|
|
if (desc.IsEmpty())
|
|
|
|
appstrings->GetStringFromName(NS_LITERAL_STRING("externalProtocolUnknown").get(),
|
|
|
|
getter_Copies(desc));
|
|
|
|
|
|
|
|
nsXPIDLString message;
|
|
|
|
const PRUnichar* msgArgs[] = { scheme.get(), uri.get(), desc.get() };
|
|
|
|
appstrings->FormatStringFromName(NS_LITERAL_STRING("externalProtocolPrompt").get(),
|
|
|
|
msgArgs,
|
|
|
|
NS_ARRAY_LENGTH(msgArgs),
|
|
|
|
getter_Copies(message));
|
|
|
|
|
|
|
|
if (scheme.IsEmpty() || uri.IsEmpty() || title.IsEmpty() ||
|
|
|
|
checkMsg.IsEmpty() || launchBtn.IsEmpty() || message.IsEmpty() ||
|
|
|
|
desc.IsEmpty())
|
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
// all pieces assembled, now we can pose the dialog
|
|
|
|
PRInt32 choice = 1; // assume "cancel" in case of failure
|
|
|
|
rv = prompt->ConfirmEx(title.get(), message.get(),
|
|
|
|
nsIPrompt::BUTTON_DELAY_ENABLE +
|
|
|
|
nsIPrompt::BUTTON_POS_1_DEFAULT +
|
|
|
|
(nsIPrompt::BUTTON_TITLE_IS_STRING * nsIPrompt::BUTTON_POS_0) +
|
|
|
|
(nsIPrompt::BUTTON_TITLE_CANCEL * nsIPrompt::BUTTON_POS_1),
|
|
|
|
launchBtn.get(), 0, 0, checkMsg.get(),
|
|
|
|
aRemember, &choice);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) && choice == 0)
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
return PR_FALSE;
|
2000-06-19 02:07:54 +04:00
|
|
|
}
|
|
|
|
|
2004-10-16 17:46:17 +04:00
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::GetApplicationDescription(const nsACString& aScheme, nsAString& _retval)
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
|
2003-07-02 04:29:26 +04:00
|
|
|
mTemporaryFilesList.AppendObject(localFile);
|
2001-07-11 07:23:24 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-03-06 22:37:22 +03:00
|
|
|
void nsExternalHelperAppService::FixFilePermissions(nsILocalFile* aFile)
|
|
|
|
{
|
|
|
|
// This space intentionally left blank
|
|
|
|
}
|
|
|
|
|
2001-07-11 07:23:24 +04:00
|
|
|
nsresult nsExternalHelperAppService::ExpungeTemporaryFiles()
|
|
|
|
{
|
2003-07-02 04:29:26 +04:00
|
|
|
PRInt32 numEntries = mTemporaryFilesList.Count();
|
|
|
|
nsILocalFile* localFile;
|
|
|
|
for (PRInt32 index = 0; index < numEntries; index++)
|
2001-07-11 07:23:24 +04:00
|
|
|
{
|
2003-07-02 04:29:26 +04:00
|
|
|
localFile = mTemporaryFilesList[index];
|
|
|
|
if (localFile)
|
|
|
|
localFile->Remove(PR_FALSE);
|
2001-07-11 07:23:24 +04:00
|
|
|
}
|
|
|
|
|
2003-07-02 04:29:26 +04:00
|
|
|
mTemporaryFilesList.Clear();
|
2001-07-11 07:23:24 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-08-10 01:27:24 +04:00
|
|
|
// XPCOM profile change observer
|
2001-07-11 07:23:24 +04:00
|
|
|
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
|
|
|
{
|
2003-01-22 09:57:15 +03:00
|
|
|
if (!strcmp(aTopic, "profile-before-change")) {
|
|
|
|
ExpungeTemporaryFiles();
|
|
|
|
nsCOMPtr <nsIRDFRemoteDataSource> flushableDataSource = do_QueryInterface(mOverRideDataSource);
|
|
|
|
if (flushableDataSource)
|
|
|
|
flushableDataSource->Flush();
|
|
|
|
mOverRideDataSource = nsnull;
|
|
|
|
mDataSourceInitialized = PR_FALSE;
|
|
|
|
}
|
2003-07-15 01:09:11 +04:00
|
|
|
return NS_OK;
|
2001-07-11 07:23:24 +04:00
|
|
|
}
|
|
|
|
|
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)
|
2005-04-15 23:41:07 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsICancelable)
|
2000-06-17 01:35:13 +04:00
|
|
|
NS_INTERFACE_MAP_END_THREADSAFE
|
|
|
|
|
2005-03-13 20:03:44 +03:00
|
|
|
nsExternalAppHandler::nsExternalAppHandler(nsIMIMEInfo * aMIMEInfo,
|
|
|
|
const nsCSubstring& aTempFileExtension,
|
|
|
|
nsIInterfaceRequestor* aWindowContext,
|
|
|
|
const nsAString& aSuggestedFilename,
|
|
|
|
PRUint32 aReason)
|
|
|
|
: mMimeInfo(aMIMEInfo)
|
|
|
|
, mWindowContext(aWindowContext)
|
|
|
|
, mSuggestedFileName(aSuggestedFilename)
|
|
|
|
, mCanceled(PR_FALSE)
|
|
|
|
, mReceivedDispositionInfo(PR_FALSE)
|
|
|
|
, mStopRequestIssued(PR_FALSE)
|
|
|
|
, mProgressListenerInitialized(PR_FALSE)
|
|
|
|
, mReason(aReason)
|
|
|
|
, mContentLength(-1)
|
2005-04-06 03:24:23 +04:00
|
|
|
, mProgress(0)
|
2005-03-13 20:03:44 +03:00
|
|
|
, mRequest(nsnull)
|
2000-06-17 01:35:13 +04:00
|
|
|
{
|
2005-03-13 20:03:44 +03:00
|
|
|
|
|
|
|
// make sure the extention includes the '.'
|
|
|
|
if (!aTempFileExtension.IsEmpty() && aTempFileExtension.First() != '.')
|
|
|
|
mTempFileExtension = PRUnichar('.');
|
|
|
|
AppendUTF8toUTF16(aTempFileExtension, mTempFileExtension);
|
|
|
|
|
|
|
|
// replace platform specific path separator and illegal characters to avoid any confusion
|
|
|
|
mSuggestedFileName.ReplaceChar(FILE_PATH_SEPARATOR FILE_ILLEGAL_CHARACTERS, '-');
|
|
|
|
mTempFileExtension.ReplaceChar(FILE_PATH_SEPARATOR FILE_ILLEGAL_CHARACTERS, '-');
|
|
|
|
|
|
|
|
// Make sure extension is correct.
|
|
|
|
EnsureSuggestedFileName();
|
2004-10-17 18:47:35 +04:00
|
|
|
|
|
|
|
sSrv->AddRef();
|
2000-06-17 01:35:13 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsExternalAppHandler::~nsExternalAppHandler()
|
|
|
|
{
|
2004-10-17 18:47:35 +04:00
|
|
|
// Not using NS_RELEASE, since we don't want to set sSrv to NULL
|
|
|
|
sSrv->Release();
|
2000-06-17 01:35:13 +04:00
|
|
|
}
|
|
|
|
|
2005-04-06 03:24:23 +04:00
|
|
|
NS_IMETHODIMP nsExternalAppHandler::SetWebProgressListener(nsIWebProgressListener2 * aWebProgressListener)
|
2001-01-25 09:48:16 +03:00
|
|
|
{
|
2002-06-04 00:52:27 +04:00
|
|
|
// this call back means we've succesfully brought up the
|
|
|
|
// progress window so set the appropriate flag, even though
|
|
|
|
// aWebProgressListener might be null
|
|
|
|
|
2002-06-07 02:12:18 +04:00
|
|
|
if (mReceivedDispositionInfo)
|
|
|
|
mProgressListenerInitialized = PR_TRUE;
|
2000-10-25 02:18:56 +04:00
|
|
|
|
2002-05-17 04:31:53 +04:00
|
|
|
// Go ahead and register the progress listener....
|
2004-02-07 22:52:16 +03:00
|
|
|
mWebProgressListener = aWebProgressListener;
|
2002-05-17 04:31:53 +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. We need to execute the
|
|
|
|
// operation since we are actually done now.
|
|
|
|
if (mStopRequestIssued && aWebProgressListener)
|
|
|
|
{
|
|
|
|
return ExecuteDesiredAction();
|
|
|
|
}
|
|
|
|
|
2000-10-04 10:54:47 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-09-25 17:07:24 +04:00
|
|
|
NS_IMETHODIMP nsExternalAppHandler::GetTargetFile(nsIFile** aTarget)
|
2000-10-04 10:54:47 +04:00
|
|
|
{
|
|
|
|
if (mFinalFileDestination)
|
|
|
|
*aTarget = mFinalFileDestination;
|
|
|
|
else
|
|
|
|
*aTarget = mTempFile;
|
|
|
|
|
|
|
|
NS_IF_ADDREF(*aTarget);
|
2003-09-25 17:07:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-10-04 10:54:47 +04:00
|
|
|
|
2003-09-25 17:07:24 +04:00
|
|
|
NS_IMETHODIMP nsExternalAppHandler::GetTimeDownloadStarted(PRTime* aTime)
|
|
|
|
{
|
|
|
|
*aTime = mTimeDownloadStarted;
|
2000-10-04 10:54:47 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsExternalAppHandler::CloseProgressWindow()
|
|
|
|
{
|
|
|
|
// release extra state...
|
|
|
|
mWebProgressListener = nsnull;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-02-07 22:52:16 +03:00
|
|
|
void 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...
|
2001-02-21 23:38:08 +03:00
|
|
|
nsCOMPtr<nsIChannel> aChannel = do_QueryInterface(request);
|
2004-02-07 22:52:16 +03:00
|
|
|
if (!aChannel)
|
|
|
|
return;
|
2001-02-21 23:38:08 +03:00
|
|
|
|
2000-10-04 10:54:47 +04:00
|
|
|
nsCOMPtr<nsILoadGroup> oldLoadGroup;
|
|
|
|
aChannel->GetLoadGroup(getter_AddRefs(oldLoadGroup));
|
|
|
|
|
2004-02-07 22:52:16 +03:00
|
|
|
if(oldLoadGroup)
|
2004-09-09 10:54:52 +04:00
|
|
|
oldLoadGroup->RemoveRequest(request, nsnull, NS_BINDING_RETARGETED);
|
2004-02-07 22:52:16 +03:00
|
|
|
|
|
|
|
aChannel->SetLoadGroup(nsnull);
|
|
|
|
aChannel->SetNotificationCallbacks(nsnull);
|
|
|
|
|
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....
|
2004-12-08 20:32:06 +03:00
|
|
|
nsCOMPtr<nsIDocumentLoader> origContextLoader =
|
|
|
|
do_GetInterface(mWindowContext);
|
2000-11-01 11:06:19 +03:00
|
|
|
if (origContextLoader)
|
|
|
|
origContextLoader->GetDocumentChannel(getter_AddRefs(mOriginalChannel));
|
2000-10-04 10:54:47 +04:00
|
|
|
}
|
|
|
|
|
2000-11-02 04:27:12 +03:00
|
|
|
#define SALT_SIZE 8
|
|
|
|
#define TABLE_SIZE 36
|
2002-05-08 03:07:19 +04:00
|
|
|
const PRUnichar table[] =
|
2000-11-02 04:27:12 +03:00
|
|
|
{ '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'};
|
|
|
|
|
2003-10-22 10:46:52 +04:00
|
|
|
/**
|
|
|
|
* Make mTempFileExtension contain an extension exactly when its previous value
|
|
|
|
* is different from mSuggestedFileName's extension, so that it can be appended
|
|
|
|
* to mSuggestedFileName and form a valid, useful leaf name.
|
|
|
|
* This is required so that the (renamed) temporary file has the correct extension
|
|
|
|
* after downloading to make sure the OS will launch the application corresponding
|
|
|
|
* to the MIME type (which was used to calculate mTempFileExtension). This prevents
|
|
|
|
* a cgi-script named foobar.exe that returns application/zip from being named
|
|
|
|
* foobar.exe and executed as an executable file. It also blocks content that
|
|
|
|
* a web site might provide with a content-disposition header indicating
|
|
|
|
* filename="foobar.exe" from being downloaded to a file with extension .exe
|
|
|
|
* and executed.
|
|
|
|
*/
|
2002-04-24 04:09:52 +04:00
|
|
|
void nsExternalAppHandler::EnsureSuggestedFileName()
|
|
|
|
{
|
|
|
|
// Make sure there is a mTempFileExtension (not "" or ".").
|
|
|
|
// Remember that mTempFileExtension will always have the leading "."
|
|
|
|
// (the check for empty is just to be safe).
|
|
|
|
if (mTempFileExtension.Length() > 1)
|
|
|
|
{
|
|
|
|
// Get mSuggestedFileName's current extension.
|
2002-05-08 03:07:19 +04:00
|
|
|
nsAutoString fileExt;
|
2002-04-27 09:33:09 +04:00
|
|
|
PRInt32 pos = mSuggestedFileName.RFindChar('.');
|
2002-04-24 04:09:52 +04:00
|
|
|
if (pos != kNotFound)
|
|
|
|
mSuggestedFileName.Right(fileExt, mSuggestedFileName.Length() - pos);
|
|
|
|
|
|
|
|
// Now, compare fileExt to mTempFileExtension.
|
2003-10-22 10:46:52 +04:00
|
|
|
if (fileExt.Equals(mTempFileExtension, nsCaseInsensitiveStringComparator()))
|
2002-04-24 04:09:52 +04:00
|
|
|
{
|
2003-10-22 10:46:52 +04:00
|
|
|
// Matches -> mTempFileExtension can be empty
|
|
|
|
mTempFileExtension.Truncate();
|
2002-04-24 04:09:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-11-02 04:27:12 +03:00
|
|
|
|
2000-07-07 05:48:05 +04:00
|
|
|
nsresult nsExternalAppHandler::SetUpTempFile(nsIChannel * aChannel)
|
2000-06-17 01:35:13 +04:00
|
|
|
{
|
2005-03-02 00:34:57 +03:00
|
|
|
nsresult rv;
|
2000-06-17 04:06:26 +04:00
|
|
|
|
2002-10-09 05:16:38 +04:00
|
|
|
#if defined(XP_MAC) || defined (XP_MACOSX)
|
2000-06-23 08:43:13 +04:00
|
|
|
// 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
|
2005-03-02 00:34:57 +03:00
|
|
|
rv = NS_GetSpecialDirectory(NS_MAC_DEFAULT_DOWNLOAD_DIR,
|
|
|
|
getter_AddRefs(mTempFile));
|
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.
|
2005-03-02 00:34:57 +03:00
|
|
|
rv = NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(mTempFile));
|
2000-06-23 08:43:13 +04:00
|
|
|
#endif
|
2005-03-02 00:34:57 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2000-06-17 04:06:26 +04:00
|
|
|
|
2003-10-22 10:46:52 +04:00
|
|
|
// We need to generate a name for the temp file that we are going to be streaming data to.
|
2000-11-02 04:27:12 +03:00
|
|
|
// We don't want this name to be predictable for security reasons so we are going to generate a
|
|
|
|
// "salted" name.....
|
2002-05-08 03:07:19 +04:00
|
|
|
nsAutoString saltedTempLeafName;
|
2000-11-02 04:27:12 +03:00
|
|
|
// this salting code was ripped directly from the profile manager.
|
2003-04-22 06:44:21 +04:00
|
|
|
// turn PR_Now() into milliseconds since epoch
|
|
|
|
// and salt rand with that.
|
2000-11-02 04:27:12 +03:00
|
|
|
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.
|
2004-04-16 00:56:37 +04:00
|
|
|
nsCAutoString ext;
|
|
|
|
mMimeInfo->GetPrimaryExtension(ext);
|
2003-10-22 10:46:52 +04:00
|
|
|
if (!ext.IsEmpty()) {
|
|
|
|
if (ext.First() != '.')
|
|
|
|
saltedTempLeafName.Append(PRUnichar('.'));
|
|
|
|
AppendUTF8toUTF16(ext, saltedTempLeafName);
|
|
|
|
}
|
2000-06-18 05:13:04 +04:00
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
mTempFile->Append(saltedTempLeafName); // make this file unique!!!
|
2002-02-02 03:06:12 +03:00
|
|
|
mTempFile->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600);
|
2000-06-17 04:06:26 +04:00
|
|
|
|
2002-10-09 05:16:38 +04:00
|
|
|
#if defined(XP_MAC) || defined (XP_MACOSX)
|
|
|
|
// Now that the file exists 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->SetFileType(type);
|
|
|
|
macfile->SetFileCreator(creator);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
rv = NS_NewLocalFileOutputStream(getter_AddRefs(mOutStream), mTempFile,
|
|
|
|
PR_WRONLY | PR_CREATE_FILE, 0600);
|
2004-02-05 13:53:11 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mTempFile->Remove(PR_FALSE);
|
|
|
|
return rv;
|
|
|
|
}
|
2001-02-21 23:38:08 +03:00
|
|
|
|
2003-04-26 01:18:10 +04:00
|
|
|
#if defined(XP_MAC) || defined (XP_MACOSX)
|
2004-04-16 01:32:11 +04:00
|
|
|
nsCAutoString contentType;
|
|
|
|
mMimeInfo->GetMIMEType(contentType);
|
2004-08-10 01:27:24 +04:00
|
|
|
if (contentType.LowerCaseEqualsLiteral(APPLICATION_APPLEFILE) ||
|
|
|
|
contentType.LowerCaseEqualsLiteral(MULTIPART_APPLEDOUBLE))
|
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);
|
2005-03-02 00:34:57 +03:00
|
|
|
if (NS_SUCCEEDED(rv))
|
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
|
|
|
{
|
2005-04-06 03:24:23 +04:00
|
|
|
NS_PRECONDITION(request, "OnStartRequest without request?");
|
2000-07-07 05:48:05 +04:00
|
|
|
|
2004-01-29 01:05:25 +03:00
|
|
|
mRequest = request;
|
|
|
|
|
2001-02-21 23:38:08 +03:00
|
|
|
nsCOMPtr<nsIChannel> aChannel = do_QueryInterface(request);
|
2000-08-10 05:49:06 +04:00
|
|
|
|
2005-04-06 03:24:23 +04:00
|
|
|
// Get content length
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIPropertyBag2> props(do_QueryInterface(request, &rv));
|
|
|
|
if (props) {
|
|
|
|
rv = props->GetPropertyAsInt64(NS_CHANNEL_PROP_CONTENT_LENGTH,
|
|
|
|
&mContentLength.mValue);
|
|
|
|
}
|
|
|
|
// If that failed, ask the channel
|
|
|
|
if (NS_FAILED(rv) && aChannel) {
|
|
|
|
PRInt32 len;
|
|
|
|
aChannel->GetContentLength(&len);
|
|
|
|
mContentLength = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now get the URI
|
2003-10-22 10:46:52 +04:00
|
|
|
if (aChannel)
|
2002-02-20 11:00:29 +03:00
|
|
|
{
|
2003-10-22 10:46:52 +04:00
|
|
|
aChannel->GetURI(getter_AddRefs(mSourceUrl));
|
2002-02-20 11:00:29 +03:00
|
|
|
}
|
2003-01-12 04:14:35 +03:00
|
|
|
|
2005-04-06 03:24:23 +04:00
|
|
|
rv = SetUpTempFile(aChannel);
|
2004-02-05 13:53:11 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mCanceled = PR_TRUE;
|
|
|
|
request->Cancel(rv);
|
|
|
|
nsAutoString path;
|
|
|
|
if (mTempFile)
|
|
|
|
mTempFile->GetPath(path);
|
|
|
|
SendStatusChange(kWriteError, rv, request, path);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-10-22 10:46:52 +04:00
|
|
|
|
2003-01-12 04:14:35 +03:00
|
|
|
// Extract mime type for later use below.
|
2004-04-16 00:56:37 +04:00
|
|
|
nsCAutoString MIMEType;
|
|
|
|
mMimeInfo->GetMIMEType(MIMEType);
|
2003-01-12 04:14:35 +03:00
|
|
|
|
2003-09-25 16:51:45 +04:00
|
|
|
// retarget all load notifications to our docloader instead of the original window's docloader...
|
2001-02-21 23:38:08 +03:00
|
|
|
RetargetLoadNotifications(request);
|
2003-09-25 16:51:45 +04:00
|
|
|
nsCOMPtr<nsIEncodedChannel> encChannel = do_QueryInterface( aChannel );
|
|
|
|
if (encChannel)
|
2000-10-23 08:31:07 +04:00
|
|
|
{
|
2002-02-27 03:43:46 +03:00
|
|
|
// Turn off content encoding conversions if needed
|
|
|
|
PRBool applyConversion = PR_TRUE;
|
2003-01-12 04:14:35 +03:00
|
|
|
|
2003-10-09 06:10:25 +04:00
|
|
|
nsCOMPtr<nsIURL> sourceURL(do_QueryInterface(mSourceUrl));
|
|
|
|
if (sourceURL)
|
2003-07-01 02:33:36 +04:00
|
|
|
{
|
2003-10-09 06:10:25 +04:00
|
|
|
nsCAutoString extension;
|
|
|
|
sourceURL->GetFileExtension(extension);
|
|
|
|
if (!extension.IsEmpty())
|
2003-07-01 02:33:36 +04:00
|
|
|
{
|
2003-10-09 06:10:25 +04:00
|
|
|
nsCOMPtr<nsIUTF8StringEnumerator> encEnum;
|
|
|
|
encChannel->GetContentEncodings(getter_AddRefs(encEnum));
|
|
|
|
if (encEnum)
|
|
|
|
{
|
|
|
|
PRBool hasMore;
|
|
|
|
rv = encEnum->HasMore(&hasMore);
|
|
|
|
if (NS_SUCCEEDED(rv) && hasMore)
|
|
|
|
{
|
|
|
|
nsCAutoString encType;
|
|
|
|
rv = encEnum->GetNext(encType);
|
|
|
|
if (NS_SUCCEEDED(rv) && !encType.IsEmpty())
|
|
|
|
{
|
2004-03-06 22:37:22 +03:00
|
|
|
NS_ASSERTION(sSrv, "Where did the service go?");
|
2004-04-16 00:56:37 +04:00
|
|
|
sSrv->ApplyDecodingForExtension(extension,
|
|
|
|
encType,
|
2004-03-06 22:37:22 +03:00
|
|
|
&applyConversion);
|
2003-10-09 06:10:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-03-14 03:51:28 +03:00
|
|
|
}
|
2003-09-25 16:51:45 +04:00
|
|
|
|
|
|
|
encChannel->SetApplyConversion( applyConversion );
|
2000-10-23 08:31:07 +04:00
|
|
|
}
|
|
|
|
|
2002-02-20 11:00:29 +03:00
|
|
|
mTimeDownloadStarted = PR_Now();
|
|
|
|
|
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;
|
2002-09-04 02:00:22 +04:00
|
|
|
// If we're handling an attachment we want to default to saving but
|
|
|
|
// always ask just in case
|
2005-03-13 20:03:44 +03:00
|
|
|
if (mReason == nsIHelperAppLauncherDialog::REASON_CANTHANDLE)
|
2003-07-03 17:55:17 +04:00
|
|
|
{
|
2002-09-04 02:00:22 +04:00
|
|
|
mMimeInfo->GetAlwaysAskBeforeHandling(&alwaysAsk);
|
|
|
|
}
|
2003-01-12 04:14:35 +03:00
|
|
|
if (alwaysAsk)
|
|
|
|
{
|
|
|
|
// But we *don't* ask if this mimeInfo didn't come from
|
|
|
|
// our mimeTypes.rdf data source and the user has said
|
|
|
|
// at some point in the distant past that they don't
|
|
|
|
// want to be asked. The latter fact would have been
|
|
|
|
// stored in pref strings back in the old days.
|
2004-03-06 22:37:22 +03:00
|
|
|
NS_ASSERTION(sSrv, "Service gone away!?");
|
|
|
|
if (!sSrv->MIMETypeIsInDataSource(MIMEType.get()))
|
2003-01-12 04:14:35 +03:00
|
|
|
{
|
|
|
|
if (!GetNeverAskFlagFromPref(NEVER_ASK_FOR_SAVE_TO_DISK_PREF, MIMEType.get()))
|
|
|
|
{
|
|
|
|
// Don't need to ask after all.
|
|
|
|
alwaysAsk = PR_FALSE;
|
|
|
|
// Make sure action matches pref (save to disk).
|
|
|
|
mMimeInfo->SetPreferredAction(nsIMIMEInfo::saveToDisk);
|
|
|
|
}
|
|
|
|
else if (!GetNeverAskFlagFromPref(NEVER_ASK_FOR_OPEN_FILE_PREF, MIMEType.get()))
|
|
|
|
{
|
|
|
|
// Don't need to ask after all.
|
|
|
|
alwaysAsk = PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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...
|
2002-06-07 02:12:18 +04:00
|
|
|
mReceivedDispositionInfo = PR_FALSE;
|
2000-07-07 05:48:05 +04:00
|
|
|
|
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);
|
|
|
|
|
2004-01-29 01:05:25 +03:00
|
|
|
// this will create a reference cycle (the dialog holds a reference to us as
|
|
|
|
// nsIHelperAppLauncher), which will be broken in Cancel or
|
|
|
|
// CreateProgressListener.
|
2005-03-13 20:03:44 +03:00
|
|
|
rv = mDialog->Show( this, mWindowContext, mReason );
|
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
|
|
|
{
|
2002-06-07 02:12:18 +04:00
|
|
|
mReceivedDispositionInfo = 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 );
|
2002-06-29 05:04:59 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
/* We need to see whether the file we've got here could be
|
|
|
|
* executable. If it could, we had better not try to open it!
|
2004-02-07 01:32:29 +03:00
|
|
|
* We can skip this check, though, if we have a setting to open in a
|
|
|
|
* helper app.
|
2002-06-29 05:04:59 +04:00
|
|
|
* This code mirrors the code in
|
|
|
|
* nsExternalAppHandler::LaunchWithApplication so that what we
|
|
|
|
* test here is as close as possible to what will really be
|
|
|
|
* happening if we decide to execute
|
|
|
|
*/
|
2004-02-07 01:32:29 +03:00
|
|
|
nsCOMPtr<nsIFile> prefApp;
|
2004-02-07 02:05:40 +03:00
|
|
|
mMimeInfo->GetPreferredApplicationHandler(getter_AddRefs(prefApp));
|
2004-02-07 01:32:29 +03:00
|
|
|
if (action != nsIMIMEInfo::useHelperApp || !prefApp) {
|
|
|
|
nsCOMPtr<nsIFile> fileToTest;
|
|
|
|
GetTargetFile(getter_AddRefs(fileToTest));
|
|
|
|
if (fileToTest) {
|
|
|
|
PRBool isExecutable;
|
|
|
|
rv = fileToTest->IsExecutable(&isExecutable);
|
|
|
|
if (NS_FAILED(rv) || isExecutable) { // checking NS_FAILED, because paranoia is good
|
|
|
|
action = nsIMIMEInfo::saveToDisk;
|
|
|
|
}
|
|
|
|
} else { // Paranoia is good here too, though this really should not happen
|
|
|
|
NS_WARNING("GetDownloadInfo returned a null file after the temp file has been set up! ");
|
2002-07-02 03:14:07 +04:00
|
|
|
action = nsIMIMEInfo::saveToDisk;
|
2002-06-29 05:04:59 +04:00
|
|
|
}
|
|
|
|
}
|
2002-07-02 03:14:07 +04:00
|
|
|
|
2002-06-29 05:04:59 +04:00
|
|
|
#endif
|
2003-07-30 21:47:26 +04:00
|
|
|
if (action == nsIMIMEInfo::useHelperApp ||
|
|
|
|
action == nsIMIMEInfo::useSystemDefault)
|
2001-05-04 01:24:36 +04:00
|
|
|
{
|
2003-07-30 21:47:26 +04:00
|
|
|
rv = LaunchWithApplication(nsnull, PR_FALSE);
|
2001-05-04 01:24:36 +04:00
|
|
|
}
|
2003-07-30 21:47:26 +04:00
|
|
|
else // Various unknown actions go here too
|
2001-05-04 01:24:36 +04:00
|
|
|
{
|
2003-07-30 21:47:26 +04:00
|
|
|
rv = SaveToDisk(nsnull, PR_FALSE);
|
2001-05-04 01:24:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-30 23:00:46 +04:00
|
|
|
// Now let's mark the downloaded URL as visited
|
|
|
|
nsCOMPtr<nsIGlobalHistory> history(do_GetService(NS_GLOBALHISTORY_CONTRACTID));
|
|
|
|
nsCAutoString spec;
|
|
|
|
mSourceUrl->GetSpec(spec);
|
|
|
|
if (history && !spec.IsEmpty())
|
|
|
|
{
|
|
|
|
history->AddPage(spec.get());
|
|
|
|
}
|
|
|
|
|
2000-06-17 01:35:13 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-02-20 11:00:29 +03:00
|
|
|
// Convert error info into proper message text and send OnStatusChange notification
|
|
|
|
// to the web progress listener.
|
2002-05-17 04:31:53 +04:00
|
|
|
void nsExternalAppHandler::SendStatusChange(ErrorType type, nsresult rv, nsIRequest *aRequest, const nsAFlatString &path)
|
2002-02-20 11:00:29 +03:00
|
|
|
{
|
|
|
|
nsAutoString msgId;
|
|
|
|
switch(rv)
|
|
|
|
{
|
2003-10-04 00:56:52 +04:00
|
|
|
case NS_ERROR_OUT_OF_MEMORY:
|
|
|
|
// No memory
|
2004-06-17 04:13:25 +04:00
|
|
|
msgId.AssignLiteral("noMemory");
|
2003-10-04 00:56:52 +04:00
|
|
|
break;
|
|
|
|
|
2002-02-20 11:00:29 +03:00
|
|
|
case NS_ERROR_FILE_DISK_FULL:
|
|
|
|
case NS_ERROR_FILE_NO_DEVICE_SPACE:
|
|
|
|
// Out of space on target volume.
|
2004-06-17 04:13:25 +04:00
|
|
|
msgId.AssignLiteral("diskFull");
|
2002-02-20 11:00:29 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case NS_ERROR_FILE_READ_ONLY:
|
|
|
|
// Attempt to write to read/only file.
|
2004-06-17 04:13:25 +04:00
|
|
|
msgId.AssignLiteral("readOnly");
|
2002-02-20 11:00:29 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case NS_ERROR_FILE_ACCESS_DENIED:
|
2005-04-05 22:32:35 +04:00
|
|
|
if (type == kWriteError) {
|
|
|
|
// Attempt to write without sufficient permissions.
|
|
|
|
msgId.AssignLiteral("accessError");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
msgId.AssignLiteral("launchError");
|
|
|
|
}
|
2002-02-20 11:00:29 +03:00
|
|
|
break;
|
|
|
|
|
2003-07-22 21:25:02 +04:00
|
|
|
case NS_ERROR_FILE_NOT_FOUND:
|
2003-12-04 01:21:39 +03:00
|
|
|
case NS_ERROR_FILE_TARGET_DOES_NOT_EXIST:
|
2003-10-04 00:56:52 +04:00
|
|
|
case NS_ERROR_FILE_UNRECOGNIZED_PATH:
|
2003-07-22 21:25:02 +04:00
|
|
|
// Helper app not found, let's verify this happened on launch
|
|
|
|
if (type == kLaunchError) {
|
2004-06-17 04:13:25 +04:00
|
|
|
msgId.AssignLiteral("helperAppNotFound");
|
2003-07-22 21:25:02 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// fall through
|
|
|
|
|
2002-02-20 11:00:29 +03:00
|
|
|
default:
|
|
|
|
// Generic read/write/launch error message.
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case kReadError:
|
2004-06-17 04:13:25 +04:00
|
|
|
msgId.AssignLiteral("readError");
|
2002-02-20 11:00:29 +03:00
|
|
|
break;
|
|
|
|
case kWriteError:
|
2004-06-17 04:13:25 +04:00
|
|
|
msgId.AssignLiteral("writeError");
|
2002-02-20 11:00:29 +03:00
|
|
|
break;
|
|
|
|
case kLaunchError:
|
2004-06-17 04:13:25 +04:00
|
|
|
msgId.AssignLiteral("launchError");
|
2002-02-20 11:00:29 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2003-09-23 23:27:07 +04:00
|
|
|
PR_LOG(nsExternalHelperAppService::mLog, PR_LOG_ERROR,
|
2005-02-18 21:36:33 +03:00
|
|
|
("Error: %s, type=%i, listener=0x%p, rv=0x%08X\n",
|
|
|
|
NS_LossyConvertUTF16toASCII(msgId).get(), type, mWebProgressListener.get(), rv));
|
|
|
|
PR_LOG(nsExternalHelperAppService::mLog, PR_LOG_ERROR,
|
|
|
|
(" path='%s'\n", NS_ConvertUTF16toUTF8(path).get()));
|
|
|
|
|
2002-02-20 11:00:29 +03:00
|
|
|
// Get properties file bundle and extract status string.
|
|
|
|
nsCOMPtr<nsIStringBundleService> s = do_GetService(NS_STRINGBUNDLE_CONTRACTID);
|
|
|
|
if (s)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIStringBundle> bundle;
|
|
|
|
if (NS_SUCCEEDED(s->CreateBundle("chrome://global/locale/nsWebBrowserPersist.properties", getter_AddRefs(bundle))))
|
|
|
|
{
|
|
|
|
nsXPIDLString msgText;
|
2002-05-08 03:07:19 +04:00
|
|
|
const PRUnichar *strings[] = { path.get() };
|
2002-02-20 11:00:29 +03:00
|
|
|
if(NS_SUCCEEDED(bundle->FormatStringFromName(msgId.get(), strings, 1, getter_Copies(msgText))))
|
|
|
|
{
|
2002-05-17 04:31:53 +04:00
|
|
|
if (mWebProgressListener)
|
|
|
|
{
|
|
|
|
// We have a listener, let it handle the error.
|
|
|
|
mWebProgressListener->OnStatusChange(nsnull, (type == kReadError) ? aRequest : nsnull, rv, msgText);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We don't have a listener. Simply show the alert ourselves.
|
2004-10-14 21:06:32 +04:00
|
|
|
nsCOMPtr<nsIPrompt> prompter(do_GetInterface(mWindowContext));
|
2002-05-17 04:31:53 +04:00
|
|
|
nsXPIDLString title;
|
|
|
|
bundle->FormatStringFromName(NS_LITERAL_STRING("title").get(),
|
|
|
|
strings,
|
|
|
|
1,
|
|
|
|
getter_Copies(title));
|
|
|
|
if (prompter)
|
|
|
|
{
|
2004-10-14 21:06:32 +04:00
|
|
|
prompter->Alert(title, msgText);
|
2002-05-17 04:31:53 +04:00
|
|
|
}
|
|
|
|
}
|
2002-02-20 11:00:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2002-02-20 11:00:29 +03:00
|
|
|
nsresult rv = NS_OK;
|
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.
|
2003-12-19 18:18:59 +03:00
|
|
|
if (mOutStream && count > 0)
|
2000-06-17 23:53:44 +04:00
|
|
|
{
|
|
|
|
PRUint32 numBytesRead = 0;
|
|
|
|
PRUint32 numBytesWritten = 0;
|
2002-02-20 11:00:29 +03:00
|
|
|
mProgress += count;
|
2002-03-06 01:33:54 +03:00
|
|
|
PRBool readError = PR_TRUE;
|
2002-02-20 11:00:29 +03:00
|
|
|
while (NS_SUCCEEDED(rv) && count > 0) // while we still have bytes to copy...
|
|
|
|
{
|
|
|
|
readError = PR_TRUE;
|
|
|
|
rv = inStr->Read(mDataBuffer, PR_MIN(count, DATA_BUFFER_SIZE - 1), &numBytesRead);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
if (count >= numBytesRead)
|
|
|
|
count -= numBytesRead; // subtract off the number of bytes we just read
|
|
|
|
else
|
|
|
|
count = 0;
|
|
|
|
readError = PR_FALSE;
|
|
|
|
// Write out the data until something goes wrong, or, it is
|
|
|
|
// all written. We loop because for some errors (e.g., disk
|
|
|
|
// full), we get NS_OK with some bytes written, then an error.
|
|
|
|
// So, we want to write again in that case to get the actual
|
|
|
|
// error code.
|
|
|
|
const char *bufPtr = mDataBuffer; // Where to write from.
|
|
|
|
while (NS_SUCCEEDED(rv) && numBytesRead)
|
|
|
|
{
|
|
|
|
numBytesWritten = 0;
|
|
|
|
rv = mOutStream->Write(bufPtr, numBytesRead, &numBytesWritten);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
numBytesRead -= numBytesWritten;
|
|
|
|
bufPtr += numBytesWritten;
|
|
|
|
// Force an error if (for some reason) we get NS_OK but
|
|
|
|
// no bytes written.
|
|
|
|
if (!numBytesWritten)
|
|
|
|
{
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
// Send progress notification.
|
|
|
|
if (mWebProgressListener)
|
|
|
|
{
|
2005-04-06 03:24:23 +04:00
|
|
|
mWebProgressListener->OnProgressChange64(nsnull, request, mProgress, mContentLength, mProgress, mContentLength);
|
2002-02-20 11:00:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2000-06-17 23:53:44 +04:00
|
|
|
{
|
2002-02-20 11:00:29 +03:00
|
|
|
// An error occurred, notify listener.
|
2002-05-17 04:31:53 +04:00
|
|
|
nsAutoString tempFilePath;
|
|
|
|
if (mTempFile)
|
|
|
|
mTempFile->GetPath(tempFilePath);
|
|
|
|
SendStatusChange(readError ? kReadError : kWriteError, rv, request, tempFilePath);
|
|
|
|
|
|
|
|
// Cancel the download.
|
2005-04-15 23:41:07 +04:00
|
|
|
Cancel(rv);
|
2000-06-17 23:53:44 +04:00
|
|
|
}
|
|
|
|
}
|
2002-02-20 11:00:29 +03:00
|
|
|
return rv;
|
2000-06-17 01:35:13 +04:00
|
|
|
}
|
|
|
|
|
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;
|
2004-01-29 01:05:25 +03:00
|
|
|
mRequest = nsnull;
|
2002-05-17 04:31:53 +04:00
|
|
|
// Cancel if the request did not complete successfully.
|
|
|
|
if (!mCanceled && NS_FAILED(aStatus))
|
|
|
|
{
|
|
|
|
// Send error notification.
|
|
|
|
nsAutoString tempFilePath;
|
|
|
|
if (mTempFile)
|
|
|
|
mTempFile->GetPath(tempFilePath);
|
|
|
|
SendStatusChange( kReadError, aStatus, request, tempFilePath );
|
|
|
|
|
2005-04-15 23:41:07 +04:00
|
|
|
Cancel(aStatus);
|
2002-05-17 04:31:53 +04:00
|
|
|
}
|
|
|
|
|
2000-08-10 05:49:06 +04:00
|
|
|
// first, check to see if we've been canceled....
|
2004-01-29 01:05:25 +03:00
|
|
|
if (mCanceled)
|
|
|
|
return NS_OK;
|
2000-08-10 05:49:06 +04:00
|
|
|
|
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
|
|
|
|
2003-07-15 01:09:11 +04:00
|
|
|
// Do what the user asked for
|
2004-01-29 01:05:25 +03:00
|
|
|
ExecuteDesiredAction();
|
|
|
|
|
|
|
|
// At this point, the channel should still own us. So releasing the reference
|
2005-01-04 16:40:51 +03:00
|
|
|
// to us in the nsITransfer should be ok.
|
|
|
|
// This nsITransfer object holds a reference to us (we are its observer), so
|
2004-01-29 01:05:25 +03:00
|
|
|
// we need to release the reference to break a reference cycle (and therefore
|
|
|
|
// to prevent leaking)
|
|
|
|
mWebProgressListener = nsnull;
|
|
|
|
|
|
|
|
return NS_OK;
|
2001-01-25 09:48:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsExternalAppHandler::ExecuteDesiredAction()
|
|
|
|
{
|
2001-07-11 07:23:24 +04:00
|
|
|
nsresult rv = NS_OK;
|
2002-06-04 00:52:27 +04:00
|
|
|
if (mProgressListenerInitialized && !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);
|
2003-07-30 21:47:26 +04:00
|
|
|
if (action == nsIMIMEInfo::useHelperApp ||
|
|
|
|
action == nsIMIMEInfo::useSystemDefault)
|
2002-01-11 22:51:57 +03:00
|
|
|
{
|
|
|
|
// Make sure the suggested name is unique since in this case we don't
|
|
|
|
// have a file name that was guaranteed to be unique by going through
|
|
|
|
// the File Save dialog
|
2002-02-02 03:06:12 +03:00
|
|
|
rv = mFinalFileDestination->CreateUnique(nsIFile::NORMAL_FILE_TYPE, 0600);
|
2002-01-11 22:51:57 +03:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
// Source and dest dirs should be == so this should just do a rename
|
|
|
|
rv = MoveFile(mFinalFileDestination);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2004-02-24 01:49:28 +03:00
|
|
|
rv = OpenWithApplication();
|
2002-01-11 22:51:57 +03:00
|
|
|
}
|
|
|
|
}
|
2003-07-30 21:47:26 +04:00
|
|
|
else // Various unknown actions go here too
|
|
|
|
{
|
|
|
|
// XXX Put progress dialog in barber-pole mode
|
|
|
|
// and change text to say "Copying from:".
|
|
|
|
rv = MoveFile(mFinalFileDestination);
|
2004-10-25 11:46:01 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && action == nsIMIMEInfo::saveToDisk)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsILocalFile> destfile(do_QueryInterface(mFinalFileDestination));
|
|
|
|
sSrv->FixFilePermissions(destfile);
|
|
|
|
}
|
2003-07-30 21:47:26 +04:00
|
|
|
}
|
|
|
|
|
2002-05-17 04:31:53 +04:00
|
|
|
// Notify dialog that download is complete.
|
|
|
|
// By waiting till this point, it ensures that the progress dialog doesn't indicate
|
|
|
|
// success until we're really done.
|
|
|
|
if(mWebProgressListener)
|
|
|
|
{
|
|
|
|
if (!mCanceled)
|
|
|
|
{
|
2005-04-06 03:24:23 +04:00
|
|
|
mWebProgressListener->OnProgressChange64(nsnull, nsnull, mContentLength, mContentLength, mContentLength, mContentLength);
|
2002-05-17 04:31:53 +04:00
|
|
|
}
|
|
|
|
mWebProgressListener->OnStateChange(nsnull, nsnull, nsIWebProgressListener::STATE_STOP, NS_OK);
|
|
|
|
}
|
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-07 05:48:05 +04:00
|
|
|
NS_IMETHODIMP nsExternalAppHandler::GetMIMEInfo(nsIMIMEInfo ** aMIMEInfo)
|
|
|
|
{
|
|
|
|
*aMIMEInfo = mMimeInfo;
|
2005-04-06 03:24:23 +04:00
|
|
|
NS_ADDREF(*aMIMEInfo);
|
2000-07-07 05:48:05 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-04-16 00:56:37 +04:00
|
|
|
NS_IMETHODIMP nsExternalAppHandler::GetSuggestedFileName(nsAString& aSuggestedFileName)
|
2002-01-26 01:05:16 +03:00
|
|
|
{
|
2004-04-16 00:56:37 +04:00
|
|
|
aSuggestedFileName = mSuggestedFileName;
|
2002-01-26 01:05:16 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-01-04 16:40:51 +03:00
|
|
|
nsresult nsExternalAppHandler::InitializeDownload(nsITransfer* aTransfer)
|
2000-10-04 10:54:47 +04:00
|
|
|
{
|
2002-03-18 00:41:01 +03:00
|
|
|
nsresult rv;
|
2002-06-04 00:52:27 +04:00
|
|
|
|
2004-04-17 04:58:23 +04:00
|
|
|
nsCOMPtr<nsIURI> target;
|
|
|
|
rv = NS_NewFileURI(getter_AddRefs(target), mFinalFileDestination);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2002-06-04 00:52:27 +04:00
|
|
|
|
2005-04-25 01:16:28 +04:00
|
|
|
rv = aTransfer->Init(mSourceUrl, target, EmptyString(),
|
|
|
|
mMimeInfo, mTimeDownloadStarted, this);
|
2002-03-18 00:41:01 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2002-06-04 00:52:27 +04:00
|
|
|
|
2004-05-26 19:07:52 +04:00
|
|
|
// Tell the listener about the location of the target file
|
2005-01-04 16:40:51 +03:00
|
|
|
nsCOMPtr<nsIObserver> obs(do_QueryInterface(aTransfer));
|
2004-05-26 19:07:52 +04:00
|
|
|
if (obs)
|
|
|
|
obs->Observe(mTempFile, "temp-file", nsnull);
|
|
|
|
|
2002-06-04 00:52:27 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsExternalAppHandler::CreateProgressListener()
|
|
|
|
{
|
|
|
|
// 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...
|
2004-01-29 01:05:25 +03:00
|
|
|
// Also, release our reference to mDialog. We don't need it anymore, and we
|
|
|
|
// need to break the reference cycle.
|
|
|
|
mDialog = nsnull;
|
2002-06-04 00:52:27 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
2005-02-07 17:35:46 +03:00
|
|
|
nsCOMPtr<nsITransfer> tr = do_CreateInstance(NS_TRANSFER_CONTRACTID, &rv);
|
2005-01-04 16:40:51 +03:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
InitializeDownload(tr);
|
2002-02-20 11:00:29 +03:00
|
|
|
|
2002-06-04 00:52:27 +04:00
|
|
|
// note we might not have a listener here if the QI() failed, or if
|
2005-01-04 16:40:51 +03:00
|
|
|
// there is no nsITransfer object, but we still call
|
2002-06-04 00:52:27 +04:00
|
|
|
// SetWebProgressListener() to make sure our progress state is sane
|
2005-01-04 16:40:51 +03:00
|
|
|
// NOTE: This will set up a reference cycle (this nsITransfer has us set up as
|
2004-01-29 01:05:25 +03:00
|
|
|
// its observer). This cycle will be broken in Cancel, CloseProgressWindow or
|
|
|
|
// OnStopRequest.
|
2005-01-04 16:40:51 +03:00
|
|
|
SetWebProgressListener(tr);
|
2000-10-04 10:54:47 +04:00
|
|
|
|
2005-01-04 16:40:51 +03:00
|
|
|
if (tr)
|
|
|
|
tr->OnStateChange(nsnull, mRequest, nsIWebProgressListener::STATE_START, NS_OK);
|
2004-01-29 01:05:25 +03:00
|
|
|
|
2000-10-04 10:54:47 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2002-05-08 03:07:19 +04:00
|
|
|
nsresult nsExternalAppHandler::PromptForSaveToFile(nsILocalFile ** aNewFile, const nsAFlatString &aDefaultFile, const nsAFlatString &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
|
2004-09-21 23:52:45 +04:00
|
|
|
// Convert to use file picker? No, then embeddors could not do any sort of
|
|
|
|
// "AutoDownload" w/o showing a prompt
|
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...
|
2004-09-21 23:52:45 +04:00
|
|
|
|
|
|
|
// Now, be sure to keep |this| alive, and the dialog
|
|
|
|
// If we don't do this, users that close the helper app dialog while the file
|
|
|
|
// picker is up would cause Cancel() to be called, and the dialog would be
|
|
|
|
// released, which would release this object too, which would crash.
|
|
|
|
// See Bug 249143
|
|
|
|
nsRefPtr<nsExternalAppHandler> kungFuDeathGrip(this);
|
|
|
|
nsCOMPtr<nsIHelperAppLauncherDialog> dlg(mDialog);
|
2003-08-05 02:31:14 +04:00
|
|
|
rv = mDialog->PromptForSaveToFile(this,
|
|
|
|
mWindowContext,
|
2002-05-08 03:07:19 +04:00
|
|
|
aDefaultFile.get(),
|
|
|
|
aFileExtension.get(),
|
2002-04-27 09:33:09 +04:00
|
|
|
aNewFile);
|
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
|
2002-04-27 09:33:09 +04:00
|
|
|
nsCAutoString fileName;
|
2002-05-08 03:07:19 +04:00
|
|
|
fileToUse->GetNativeLeafName(fileName);
|
2001-01-25 09:48:16 +03:00
|
|
|
nsCOMPtr<nsIFile> directoryLocation;
|
2005-03-02 00:34:57 +03:00
|
|
|
rv = fileToUse->GetParent(getter_AddRefs(directoryLocation));
|
2001-01-25 09:48:16 +03:00
|
|
|
if (directoryLocation)
|
|
|
|
{
|
2002-05-08 03:07:19 +04:00
|
|
|
rv = mTempFile->MoveToNative(directoryLocation, fileName);
|
2001-01-25 09:48:16 +03:00
|
|
|
}
|
2002-05-17 04:31:53 +04:00
|
|
|
if (NS_FAILED(rv))
|
2002-02-20 11:00:29 +03:00
|
|
|
{
|
|
|
|
// Send error notification.
|
2002-05-08 03:07:19 +04:00
|
|
|
nsAutoString path;
|
2002-04-27 09:33:09 +04:00
|
|
|
fileToUse->GetPath(path);
|
2002-05-17 04:31:53 +04:00
|
|
|
SendStatusChange(kWriteError, rv, nsnull, path);
|
2005-04-15 23:41:07 +04:00
|
|
|
Cancel(rv); // Cancel (and clean up temp file).
|
2002-02-20 11:00:29 +03:00
|
|
|
}
|
2001-01-25 09:48:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2002-05-17 04:31:53 +04:00
|
|
|
// The helper app dialog has told us what to do.
|
2002-06-07 02:12:18 +04:00
|
|
|
mReceivedDispositionInfo = PR_TRUE;
|
2002-05-17 04:31:53 +04:00
|
|
|
|
2003-03-18 02:39:13 +03:00
|
|
|
nsCOMPtr<nsILocalFile> fileToUse = do_QueryInterface(aNewFileLocation);
|
|
|
|
if (!fileToUse)
|
2000-07-07 05:48:05 +04:00
|
|
|
{
|
2002-05-08 03:07:19 +04:00
|
|
|
nsAutoString leafName;
|
2002-04-27 09:33:09 +04:00
|
|
|
mTempFile->GetLeafName(leafName);
|
2000-10-04 10:54:47 +04:00
|
|
|
if (mSuggestedFileName.IsEmpty())
|
2002-04-27 09:33:09 +04:00
|
|
|
rv = PromptForSaveToFile(getter_AddRefs(fileToUse), leafName, mTempFileExtension);
|
2000-09-15 10:07:58 +04:00
|
|
|
else
|
2001-06-28 06:18:54 +04:00
|
|
|
{
|
2002-05-08 03:07:19 +04:00
|
|
|
nsAutoString fileExt;
|
2002-04-27 09:33:09 +04:00
|
|
|
PRInt32 pos = mSuggestedFileName.RFindChar('.');
|
2001-06-28 06:18:54 +04:00
|
|
|
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())
|
2002-04-27 09:33:09 +04:00
|
|
|
fileExt = mTempFileExtension;
|
2001-06-28 06:18:54 +04:00
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
rv = PromptForSaveToFile(getter_AddRefs(fileToUse), mSuggestedFileName, fileExt);
|
2001-06-28 06:18:54 +04:00
|
|
|
}
|
2000-09-15 10:07:58 +04:00
|
|
|
|
2003-02-03 23:36:12 +03:00
|
|
|
if (NS_FAILED(rv) || !fileToUse) {
|
2005-04-15 23:41:07 +04:00
|
|
|
Cancel(NS_BINDING_ABORTED);
|
2003-02-03 23:36:12 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2003-03-18 02:39:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mFinalFileDestination = do_QueryInterface(fileToUse);
|
2000-11-01 11:06:19 +03:00
|
|
|
|
2004-01-17 16:18:12 +03:00
|
|
|
// Move what we have in the final directory, but append .part
|
|
|
|
// to it, to indicate that it's unfinished.
|
|
|
|
// do not do that if we're already done
|
|
|
|
if (mFinalFileDestination && !mStopRequestIssued)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIFile> movedFile;
|
|
|
|
mFinalFileDestination->Clone(getter_AddRefs(movedFile));
|
|
|
|
if (movedFile) {
|
|
|
|
// Get the old leaf name and append .part to it
|
|
|
|
nsCAutoString name;
|
|
|
|
mFinalFileDestination->GetNativeLeafName(name);
|
2004-06-17 04:13:25 +04:00
|
|
|
name.AppendLiteral(".part");
|
2004-01-17 16:18:12 +03:00
|
|
|
movedFile->SetNativeLeafName(name);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> dir;
|
|
|
|
movedFile->GetParent(getter_AddRefs(dir));
|
|
|
|
|
|
|
|
mOutStream->Close();
|
|
|
|
|
2004-01-17 16:41:23 +03:00
|
|
|
rv = mTempFile->MoveToNative(dir, name);
|
2004-01-17 16:18:12 +03:00
|
|
|
if (NS_SUCCEEDED(rv)) // if it failed, we just continue with $TEMP
|
|
|
|
mTempFile = movedFile;
|
|
|
|
rv = NS_NewLocalFileOutputStream(getter_AddRefs(mOutStream), mTempFile,
|
|
|
|
PR_WRONLY | PR_APPEND, 0600);
|
|
|
|
if (NS_FAILED(rv)) { // (Re-)opening the output stream failed. bad luck.
|
|
|
|
nsAutoString path;
|
|
|
|
mTempFile->GetPath(path);
|
|
|
|
SendStatusChange(kWriteError, rv, nsnull, path);
|
2005-04-15 23:41:07 +04:00
|
|
|
Cancel(rv);
|
2004-01-17 16:18:12 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-18 02:39:13 +03:00
|
|
|
if (!mProgressListenerInitialized)
|
|
|
|
CreateProgressListener();
|
2001-01-25 09:48:16 +03:00
|
|
|
|
2003-03-18 02:39:13 +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-08-10 05:49:06 +04:00
|
|
|
|
2003-03-18 02:39:13 +03:00
|
|
|
return NS_OK;
|
2001-01-25 09:48:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-24 01:49:28 +03:00
|
|
|
nsresult nsExternalAppHandler::OpenWithApplication()
|
2001-01-25 09:48:16 +03:00
|
|
|
{
|
|
|
|
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
|
|
|
{
|
2004-02-24 01:49:28 +03:00
|
|
|
rv = mMimeInfo->LaunchWithFile(mFinalFileDestination);
|
2003-07-01 02:33:36 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
{
|
|
|
|
// Send error notification.
|
|
|
|
nsAutoString path;
|
|
|
|
mFinalFileDestination->GetPath(path);
|
|
|
|
SendStatusChange(kLaunchError, rv, nsnull, path);
|
2005-04-15 23:41:07 +04:00
|
|
|
Cancel(rv); // Cancel, and clean up temp file.
|
2003-07-01 02:33:36 +04:00
|
|
|
}
|
|
|
|
else
|
2001-01-25 09:48:16 +03:00
|
|
|
{
|
2002-10-09 05:16:38 +04:00
|
|
|
#if !defined(XP_MAC) && !defined (XP_MACOSX)
|
2004-03-06 22:37:22 +03:00
|
|
|
NS_ASSERTION(sSrv, "Service gone away!?");
|
2002-01-11 23:51:09 +03:00
|
|
|
// Mac users have been very verbal about temp files being deleted on app exit - they
|
|
|
|
// don't like it - but we'll continue to do this on other platforms for now
|
2004-03-06 22:43:40 +03:00
|
|
|
sSrv->DeleteTemporaryFileOnExit(mFinalFileDestination);
|
2002-01-11 23:51:09 +03:00
|
|
|
#endif
|
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-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
|
|
|
|
2002-06-07 02:12:18 +04:00
|
|
|
mReceivedDispositionInfo = PR_TRUE;
|
2000-07-11 01:21:10 +04:00
|
|
|
if (mMimeInfo && aApplication)
|
|
|
|
mMimeInfo->SetPreferredApplicationHandler(aApplication);
|
2000-07-07 05:48:05 +04:00
|
|
|
|
2003-07-01 02:33:36 +04:00
|
|
|
// Now check if the file is local, in which case we won't bother with saving
|
|
|
|
// it to a temporary directory and just launch it from where it is
|
|
|
|
nsCOMPtr<nsIFileURL> fileUrl(do_QueryInterface(mSourceUrl));
|
|
|
|
if (fileUrl)
|
|
|
|
{
|
2005-04-15 23:41:07 +04:00
|
|
|
Cancel(NS_BINDING_ABORTED);
|
2003-07-01 02:33:36 +04:00
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
nsresult rv = fileUrl->GetFile(getter_AddRefs(file));
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
2004-02-24 01:49:28 +03:00
|
|
|
rv = mMimeInfo->LaunchWithFile(file);
|
2003-07-01 02:33:36 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
nsAutoString path;
|
|
|
|
if (file)
|
|
|
|
file->GetPath(path);
|
|
|
|
// If we get here, an error happened
|
|
|
|
SendStatusChange(kLaunchError, rv, nsnull, path);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2002-01-11 22:51:57 +03:00
|
|
|
// Now that the user has elected to launch the downloaded file with a helper app, we're justified in
|
|
|
|
// removing the 'salted' name. We'll rename to what was specified in mSuggestedFileName after the
|
|
|
|
// download is done prior to launching the helper app. So that any existing file of that name won't
|
|
|
|
// be overwritten we call CreateUnique() before calling MoveFile(). Also note that we use the same
|
|
|
|
// directory as originally downloaded to so that MoveFile() just does an in place rename.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> fileToUse;
|
|
|
|
|
|
|
|
// The directories specified here must match those specified in SetUpTempFile(). This avoids
|
|
|
|
// having to do a copy of the file when it finishes downloading and the potential for errors
|
|
|
|
// that would introduce
|
2002-10-09 05:16:38 +04:00
|
|
|
#if defined(XP_MAC) || defined (XP_MACOSX)
|
2002-01-11 22:51:57 +03:00
|
|
|
NS_GetSpecialDirectory(NS_MAC_DEFAULT_DOWNLOAD_DIR, getter_AddRefs(fileToUse));
|
|
|
|
#else
|
|
|
|
NS_GetSpecialDirectory(NS_OS_TEMP_DIR, getter_AddRefs(fileToUse));
|
|
|
|
#endif
|
2002-02-06 06:35:31 +03:00
|
|
|
|
|
|
|
if (mSuggestedFileName.IsEmpty())
|
|
|
|
{
|
|
|
|
// Keep using the leafname of the temp file, since we're just starting a helper
|
2002-04-27 09:33:09 +04:00
|
|
|
mTempFile->GetLeafName(mSuggestedFileName);
|
2002-02-06 06:35:31 +03:00
|
|
|
}
|
|
|
|
|
2003-10-22 10:46:52 +04:00
|
|
|
#ifdef XP_WIN
|
|
|
|
fileToUse->Append(mSuggestedFileName + mTempFileExtension);
|
|
|
|
#else
|
|
|
|
fileToUse->Append(mSuggestedFileName);
|
|
|
|
#endif
|
|
|
|
|
2002-01-11 22:51:57 +03:00
|
|
|
// We'll make sure this results in a unique name later
|
|
|
|
|
|
|
|
mFinalFileDestination = do_QueryInterface(fileToUse);
|
|
|
|
|
2002-02-20 11:00:29 +03:00
|
|
|
// launch the progress window now that the user has picked the desired action.
|
2002-06-04 00:52:27 +04:00
|
|
|
if (!mProgressListenerInitialized)
|
|
|
|
CreateProgressListener();
|
2002-02-20 11:00:29 +03:00
|
|
|
|
2002-01-11 22:51:57 +03:00
|
|
|
return NS_OK;
|
2000-07-07 05:48:05 +04:00
|
|
|
}
|
|
|
|
|
2005-04-15 23:41:07 +04:00
|
|
|
NS_IMETHODIMP nsExternalAppHandler::Cancel(nsresult aReason)
|
2000-07-07 05:48:05 +04:00
|
|
|
{
|
2005-04-15 23:41:07 +04:00
|
|
|
NS_ENSURE_ARG(NS_FAILED(aReason));
|
|
|
|
// XXX should not ignore the reason
|
|
|
|
|
2000-07-07 05:48:05 +04:00
|
|
|
mCanceled = PR_TRUE;
|
2004-01-29 01:05:25 +03:00
|
|
|
// Break our reference cycle with the helper app dialog (set up in
|
|
|
|
// OnStartRequest)
|
|
|
|
mDialog = nsnull;
|
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...
|
2004-05-26 19:07:52 +04:00
|
|
|
// but only if we got asked to open the file. when saving,
|
|
|
|
// we leave the file there - the partial file might be useful
|
2004-07-15 22:56:42 +04:00
|
|
|
// But if we haven't received disposition info yet, then we're
|
|
|
|
// here because the user cancelled the helper app dialog.
|
|
|
|
// Delete the file in this case.
|
2004-05-26 19:07:52 +04:00
|
|
|
nsMIMEInfoHandleAction action = nsIMIMEInfo::saveToDisk;
|
|
|
|
mMimeInfo->GetPreferredAction(&action);
|
2004-10-14 21:06:32 +04:00
|
|
|
if (mTempFile &&
|
|
|
|
(!mReceivedDispositionInfo || action != nsIMIMEInfo::saveToDisk))
|
2000-08-10 05:49:06 +04:00
|
|
|
{
|
2004-09-17 23:13:00 +04:00
|
|
|
mTempFile->Remove(PR_FALSE);
|
2000-08-10 05:49:06 +04:00
|
|
|
mTempFile = nsnull;
|
|
|
|
}
|
2000-10-04 10:54:47 +04:00
|
|
|
|
2004-01-29 01:05:25 +03:00
|
|
|
// Release the listener, to break the reference cycle with it (we are the
|
|
|
|
// observer of the listener).
|
|
|
|
mWebProgressListener = 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
|
|
|
|
2003-01-12 04:14:35 +03:00
|
|
|
PRBool nsExternalAppHandler::GetNeverAskFlagFromPref(const char * prefName, const char * aContentType)
|
|
|
|
{
|
|
|
|
// Search the obsolete pref strings.
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIPrefService> prefs = do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
|
|
|
|
nsCOMPtr<nsIPrefBranch> prefBranch;
|
|
|
|
if (prefs)
|
|
|
|
rv = prefs->GetBranch(NEVER_ASK_PREF_BRANCH, getter_AddRefs(prefBranch));
|
|
|
|
if (NS_SUCCEEDED(rv) && prefBranch)
|
|
|
|
{
|
|
|
|
nsXPIDLCString prefCString;
|
|
|
|
nsCAutoString prefValue;
|
|
|
|
rv = prefBranch->GetCharPref(prefName, getter_Copies(prefCString));
|
|
|
|
if (NS_SUCCEEDED(rv) && !prefCString.IsEmpty())
|
|
|
|
{
|
|
|
|
NS_UnescapeURL(prefCString);
|
|
|
|
nsACString::const_iterator start, end;
|
|
|
|
prefCString.BeginReading(start);
|
|
|
|
prefCString.EndReading(end);
|
|
|
|
if (CaseInsensitiveFindInReadable(nsDependentCString(aContentType), start, end))
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Default is true, if not found in the pref string.
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
2000-09-08 04:12:51 +04:00
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// The following section contains our nsIMIMEService implementation and related methods.
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2003-07-25 20:48:00 +04:00
|
|
|
// nsIMIMEService methods
|
2004-04-16 00:56:37 +04:00
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::GetFromTypeAndExtension(const nsACString& aMIMEType, const nsACString& aFileExt, nsIMIMEInfo **_retval)
|
2003-07-25 20:48:00 +04:00
|
|
|
{
|
2004-04-16 00:56:37 +04:00
|
|
|
NS_PRECONDITION(!aMIMEType.IsEmpty() ||
|
|
|
|
!aFileExt.IsEmpty(),
|
2003-07-25 20:48:00 +04:00
|
|
|
"Give me something to work with");
|
2004-04-16 00:56:37 +04:00
|
|
|
LOG(("Getting mimeinfo from type '%s' ext '%s'\n",
|
|
|
|
PromiseFlatCString(aMIMEType).get(), PromiseFlatCString(aFileExt).get()));
|
2003-07-25 20:48:00 +04:00
|
|
|
|
|
|
|
*_retval = nsnull;
|
2003-07-27 01:01:08 +04:00
|
|
|
|
2004-06-11 19:47:57 +04:00
|
|
|
// OK... we need a type. Get one.
|
|
|
|
nsCAutoString typeToUse(aMIMEType);
|
|
|
|
if (typeToUse.IsEmpty()) {
|
|
|
|
nsresult rv = GetTypeFromExtension(aFileExt, typeToUse);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2003-07-27 01:01:08 +04:00
|
|
|
// (1) Ask the OS for a mime info
|
2003-11-15 18:01:39 +03:00
|
|
|
PRBool found;
|
2004-06-11 19:47:57 +04:00
|
|
|
*_retval = GetMIMEInfoFromOS(typeToUse, aFileExt, &found).get();
|
2004-01-29 01:05:25 +03:00
|
|
|
LOG(("OS gave back 0x%p - found: %i\n", *_retval, found));
|
2003-11-15 18:01:39 +03:00
|
|
|
// If we got no mimeinfo, something went wrong. Probably lack of memory.
|
|
|
|
if (!*_retval)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2003-07-25 20:48:00 +04:00
|
|
|
|
2003-07-27 01:01:08 +04:00
|
|
|
// (2) Now, let's see if we can find something in our datasource
|
2004-06-11 19:47:57 +04:00
|
|
|
// This will not overwrite the OS information that interests us
|
|
|
|
// (i.e. default application, default app. description)
|
|
|
|
nsresult rv = GetMIMEInfoForMimeTypeFromDS(typeToUse, *_retval);
|
|
|
|
found = found || NS_SUCCEEDED(rv);
|
2003-07-25 20:48:00 +04:00
|
|
|
|
2003-11-15 18:01:39 +03:00
|
|
|
LOG(("Data source: Via type: retval 0x%08x\n", rv));
|
2003-07-25 20:48:00 +04:00
|
|
|
|
2003-11-15 18:01:39 +03:00
|
|
|
if (!found || NS_FAILED(rv)) {
|
2003-07-25 20:48:00 +04:00
|
|
|
// No type match, try extension match
|
2004-04-16 00:56:37 +04:00
|
|
|
if (!aFileExt.IsEmpty()) {
|
2003-11-15 18:01:39 +03:00
|
|
|
rv = GetMIMEInfoForExtensionFromDS(aFileExt, *_retval);
|
|
|
|
LOG(("Data source: Via ext: retval 0x%08x\n", rv));
|
2003-12-30 13:45:12 +03:00
|
|
|
found = found || NS_SUCCEEDED(rv);
|
2003-01-12 04:14:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-07-27 01:01:08 +04:00
|
|
|
// (3) No match yet. Ask extras.
|
2003-11-15 18:01:39 +03:00
|
|
|
if (!found) {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
2003-07-27 01:01:08 +04:00
|
|
|
#ifdef XP_WIN
|
2004-06-11 19:47:57 +04:00
|
|
|
/* XXX Gross hack to wallpaper over the most common Win32
|
|
|
|
* extension issues caused by the fix for bug 116938. See bug
|
|
|
|
* 120327, comment 271 for why this is needed. Not even sure we
|
|
|
|
* want to remove this once we have fixed all this stuff to work
|
|
|
|
* right; any info we get from extras on this type is pretty much
|
|
|
|
* useless....
|
|
|
|
*/
|
|
|
|
if (!typeToUse.Equals(APPLICATION_OCTET_STREAM, nsCaseInsensitiveCStringComparator()))
|
2003-07-27 01:01:08 +04:00
|
|
|
#endif
|
2004-06-11 19:47:57 +04:00
|
|
|
rv = GetMIMEInfoForMimeTypeFromExtras(typeToUse, *_retval);
|
|
|
|
LOG(("Searched extras (by type), rv 0x%08X\n", rv));
|
2003-07-27 01:01:08 +04:00
|
|
|
// If that didn't work out, try file extension from extras
|
2004-04-16 00:56:37 +04:00
|
|
|
if (NS_FAILED(rv) && !aFileExt.IsEmpty()) {
|
2003-11-15 18:01:39 +03:00
|
|
|
rv = GetMIMEInfoForExtensionFromExtras(aFileExt, *_retval);
|
|
|
|
LOG(("Searched extras (by ext), rv 0x%08X\n", rv));
|
2003-07-27 01:01:08 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, check if we got a file extension and if yes, if it is an
|
|
|
|
// extension on the mimeinfo, in which case we want it to be the primary one
|
2004-04-16 00:56:37 +04:00
|
|
|
if (!aFileExt.IsEmpty()) {
|
2003-07-27 01:01:08 +04:00
|
|
|
PRBool matches = PR_FALSE;
|
|
|
|
(*_retval)->ExtensionExists(aFileExt, &matches);
|
2004-04-16 00:56:37 +04:00
|
|
|
LOG(("Extension '%s' matches mime info: %i\n", PromiseFlatCString(aFileExt).get(), matches));
|
2003-07-27 01:01:08 +04:00
|
|
|
if (matches)
|
|
|
|
(*_retval)->SetPrimaryExtension(aFileExt);
|
|
|
|
}
|
|
|
|
|
2003-11-15 18:01:39 +03:00
|
|
|
#ifdef PR_LOGGING
|
|
|
|
if (LOG_ENABLED()) {
|
2004-06-11 19:47:57 +04:00
|
|
|
nsCAutoString type;
|
|
|
|
(*_retval)->GetMIMEType(type);
|
|
|
|
|
2004-04-16 00:56:37 +04:00
|
|
|
nsCAutoString ext;
|
|
|
|
(*_retval)->GetPrimaryExtension(ext);
|
2003-11-15 18:01:39 +03:00
|
|
|
LOG(("MIME Info Summary: Type '%s', Primary Ext '%s'\n", type.get(), ext.get()));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-07-25 20:48:00 +04:00
|
|
|
return NS_OK;
|
2000-09-08 04:12:51 +04:00
|
|
|
}
|
|
|
|
|
2004-04-16 00:56:37 +04:00
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::GetTypeFromExtension(const nsACString& aFileExt, nsACString& aContentType)
|
2000-09-08 04:12:51 +04:00
|
|
|
{
|
2004-06-11 19:47:57 +04:00
|
|
|
// OK. We want to try the following sources of mimetype information, in this order:
|
|
|
|
// 1. defaultMimeEntries array
|
|
|
|
// 2. User-set preferences (mimeTypes.rdf)
|
|
|
|
// 3. OS-provided information
|
|
|
|
// 4. our "extras" array
|
|
|
|
// 5. Information from plugins
|
|
|
|
// 6. The "ext-to-type-mapping" category
|
|
|
|
|
2004-02-06 21:43:27 +03:00
|
|
|
nsresult rv = NS_OK;
|
2003-07-15 02:30:52 +04:00
|
|
|
// First of all, check our default entries
|
2003-09-23 23:24:26 +04:00
|
|
|
for (size_t i = 0; i < NS_ARRAY_LENGTH(defaultMimeEntries); i++)
|
2003-07-15 02:30:52 +04:00
|
|
|
{
|
2004-08-10 01:27:24 +04:00
|
|
|
if (aFileExt.LowerCaseEqualsASCII(defaultMimeEntries[i].mFileExtension)) {
|
2004-04-16 00:56:37 +04:00
|
|
|
aContentType = defaultMimeEntries[i].mMimeType;
|
2004-02-06 21:43:27 +03:00
|
|
|
return rv;
|
2003-07-15 02:30:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-11 19:47:57 +04:00
|
|
|
// Check RDF DS
|
|
|
|
PRBool found = GetTypeFromDS(aFileExt, aContentType);
|
|
|
|
if (found)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// Ask OS.
|
|
|
|
nsCOMPtr<nsIMIMEInfo> mi = GetMIMEInfoFromOS(EmptyCString(), aFileExt, &found);
|
|
|
|
if (mi && found)
|
|
|
|
return mi->GetMIMEType(aContentType);
|
|
|
|
|
|
|
|
// Check extras array.
|
|
|
|
found = GetTypeFromExtras(aFileExt, aContentType);
|
|
|
|
if (found)
|
|
|
|
return NS_OK;
|
|
|
|
|
2004-04-16 00:56:37 +04:00
|
|
|
const nsCString& flatExt = PromiseFlatCString(aFileExt);
|
2004-06-11 19:47:57 +04:00
|
|
|
// Try the plugins
|
|
|
|
const char* mimeType;
|
|
|
|
nsCOMPtr<nsIPluginHost> pluginHost (do_GetService(kPluginManagerCID, &rv));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
if (NS_SUCCEEDED(pluginHost->IsPluginEnabledForExtension(flatExt.get(), mimeType)))
|
|
|
|
{
|
|
|
|
aContentType = mimeType;
|
|
|
|
return NS_OK;
|
2001-07-25 03:12:29 +04:00
|
|
|
}
|
|
|
|
}
|
2004-06-11 19:47:57 +04:00
|
|
|
|
|
|
|
rv = NS_OK;
|
|
|
|
// Let's see if an extension added something
|
|
|
|
nsCOMPtr<nsICategoryManager> catMan(do_GetService("@mozilla.org/categorymanager;1"));
|
|
|
|
if (catMan) {
|
|
|
|
nsXPIDLCString type;
|
|
|
|
rv = catMan->GetCategoryEntry("ext-to-type-mapping", flatExt.get(), getter_Copies(type));
|
|
|
|
aContentType = type;
|
2004-02-06 21:43:27 +03:00
|
|
|
}
|
|
|
|
else {
|
2004-06-11 19:47:57 +04:00
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
2004-02-06 21:43:27 +03:00
|
|
|
}
|
2004-06-11 19:47:57 +04:00
|
|
|
|
2004-02-06 21:43:27 +03:00
|
|
|
return rv;
|
2000-09-08 04:12:51 +04:00
|
|
|
}
|
|
|
|
|
2004-04-16 00:56:37 +04:00
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::GetPrimaryExtension(const nsACString& aMIMEType, const nsACString& aFileExt, nsACString& _retval)
|
2003-11-14 02:00:55 +03:00
|
|
|
{
|
2004-04-16 00:56:37 +04:00
|
|
|
NS_ENSURE_ARG(!aMIMEType.IsEmpty());
|
2003-11-14 02:00:55 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIMIMEInfo> mi;
|
|
|
|
nsresult rv = GetFromTypeAndExtension(aMIMEType, aFileExt, getter_AddRefs(mi));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
return mi->GetPrimaryExtension(_retval);
|
|
|
|
}
|
|
|
|
|
2004-04-16 00:56:37 +04:00
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::GetTypeFromURI(nsIURI *aURI, nsACString& aContentType)
|
2000-09-08 04:12:51 +04:00
|
|
|
{
|
2002-11-07 01:29:17 +03:00
|
|
|
nsresult rv = NS_ERROR_NOT_AVAILABLE;
|
2004-04-16 00:56:37 +04:00
|
|
|
aContentType.Truncate();
|
2002-11-07 01:29:17 +03:00
|
|
|
|
|
|
|
// First look for a file to use. If we have one, we just use that.
|
|
|
|
nsCOMPtr<nsIFileURL> fileUrl = do_QueryInterface(aURI);
|
|
|
|
if (fileUrl) {
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
rv = fileUrl->GetFile(getter_AddRefs(file));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = GetTypeFromFile(file, aContentType);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// we got something!
|
|
|
|
return rv;
|
2002-04-22 17:39:16 +04:00
|
|
|
}
|
2000-09-15 10:07:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-07 01:29:17 +03:00
|
|
|
// Now try to get an nsIURL so we don't have to do our own parsing
|
|
|
|
nsCOMPtr<nsIURL> url = do_QueryInterface(aURI);
|
|
|
|
if (url) {
|
|
|
|
nsCAutoString ext;
|
|
|
|
rv = url->GetFileExtension(ext);
|
2004-08-10 01:27:24 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
if (ext.IsEmpty())
|
2002-11-07 01:29:17 +03:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2004-08-10 01:27:24 +04:00
|
|
|
|
|
|
|
UnescapeFragment(ext, url, ext);
|
|
|
|
|
2004-04-16 00:56:37 +04:00
|
|
|
return GetTypeFromExtension(ext, aContentType);
|
2002-11-07 01:29:17 +03:00
|
|
|
}
|
|
|
|
|
2000-09-15 10:07:58 +04:00
|
|
|
// no url, let's give the raw spec a shot
|
2002-11-07 01:29:17 +03:00
|
|
|
nsCAutoString specStr;
|
2002-03-06 10:48:55 +03:00
|
|
|
rv = aURI->GetSpec(specStr);
|
2004-08-10 01:27:24 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
UnescapeFragment(specStr, aURI, specStr);
|
2000-09-15 10:07:58 +04:00
|
|
|
|
|
|
|
// find the file extension (if any)
|
|
|
|
PRInt32 extLoc = specStr.RFindChar('.');
|
2003-03-18 02:39:13 +03:00
|
|
|
PRInt32 specLength = specStr.Length();
|
2002-11-07 01:29:17 +03:00
|
|
|
if (-1 != extLoc &&
|
2003-03-18 02:39:13 +03:00
|
|
|
extLoc != specLength - 1 &&
|
2002-11-07 01:29:17 +03:00
|
|
|
// nothing over 20 chars long can be sanely considered an
|
|
|
|
// extension.... Dat dere would be just data.
|
2003-03-18 02:39:13 +03:00
|
|
|
specLength - extLoc < 20)
|
2000-09-15 10:07:58 +04:00
|
|
|
{
|
2004-04-16 00:56:37 +04:00
|
|
|
return GetTypeFromExtension(Substring(specStr, extLoc + 1), aContentType);
|
2000-09-15 10:07:58 +04:00
|
|
|
}
|
2002-11-07 01:29:17 +03:00
|
|
|
|
|
|
|
// We found no information; say so.
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2000-09-08 04:12:51 +04:00
|
|
|
}
|
|
|
|
|
2004-04-16 00:56:37 +04:00
|
|
|
NS_IMETHODIMP nsExternalHelperAppService::GetTypeFromFile(nsIFile* aFile, nsACString& aContentType)
|
2000-09-08 04:12:51 +04:00
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIMIMEInfo> info;
|
2003-07-15 01:09:11 +04:00
|
|
|
|
2002-04-27 09:33:09 +04:00
|
|
|
// Get the Extension
|
2004-08-10 01:27:24 +04:00
|
|
|
nsAutoString fileName;
|
|
|
|
rv = aFile->GetLeafName(fileName);
|
2000-09-15 10:07:58 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2004-08-10 01:27:24 +04:00
|
|
|
nsCAutoString fileExt;
|
2002-04-27 09:33:09 +04:00
|
|
|
if (!fileName.IsEmpty())
|
2000-09-15 10:07:58 +04:00
|
|
|
{
|
2002-04-27 09:33:09 +04:00
|
|
|
PRInt32 len = fileName.Length();
|
2000-09-15 10:07:58 +04:00
|
|
|
for (PRInt32 i = len; i >= 0; i--)
|
|
|
|
{
|
2004-08-10 01:27:24 +04:00
|
|
|
if (fileName[i] == PRUnichar('.'))
|
2000-09-15 10:07:58 +04:00
|
|
|
{
|
2004-08-10 01:27:24 +04:00
|
|
|
CopyUTF16toUTF8(fileName.get() + i + 1, fileExt);
|
2000-09-15 10:07:58 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-09-08 04:12:51 +04:00
|
|
|
|
2000-09-15 10:07:58 +04:00
|
|
|
// Handle the mac case
|
2002-10-09 05:16:38 +04:00
|
|
|
#if defined(XP_MAC) || defined (XP_MACOSX)
|
2000-09-15 10:07:58 +04:00
|
|
|
nsCOMPtr<nsILocalFileMac> macFile;
|
|
|
|
macFile = do_QueryInterface( aFile, &rv );
|
|
|
|
if ( NS_SUCCEEDED( rv ) && fileExt.IsEmpty())
|
|
|
|
{
|
|
|
|
PRUint32 type, creator;
|
2002-01-07 17:35:04 +03:00
|
|
|
macFile->GetFileType( (OSType*)&type );
|
|
|
|
macFile->GetFileCreator( (OSType*)&creator );
|
2000-09-15 10:07:58 +04:00
|
|
|
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));
|
2004-08-10 01:27:24 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2000-09-15 10:07:58 +04:00
|
|
|
return info->GetMIMEType(aContentType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// Windows, unix and mac when no type match occured.
|
|
|
|
if (fileExt.IsEmpty())
|
|
|
|
return NS_ERROR_FAILURE;
|
2004-08-10 01:27:24 +04:00
|
|
|
return GetTypeFromExtension(fileExt, aContentType);
|
2000-09-08 04:12:51 +04:00
|
|
|
}
|
2000-09-15 10:07:58 +04:00
|
|
|
|
2004-04-16 00:56:37 +04:00
|
|
|
nsresult nsExternalHelperAppService::GetMIMEInfoForMimeTypeFromExtras(const nsACString& aContentType, nsIMIMEInfo * aMIMEInfo )
|
2001-05-04 01:24:36 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG( aMIMEInfo );
|
|
|
|
|
2004-04-16 00:56:37 +04:00
|
|
|
NS_ENSURE_ARG( !aContentType.IsEmpty() );
|
2001-05-04 01:24:36 +04:00
|
|
|
|
|
|
|
// Look for default entry with matching mime type.
|
|
|
|
nsCAutoString MIMEType(aContentType);
|
2001-12-17 10:14:49 +03:00
|
|
|
ToLowerCase(MIMEType);
|
2003-07-15 01:09:11 +04:00
|
|
|
PRInt32 numEntries = NS_ARRAY_LENGTH(extraMimeEntries);
|
2003-11-15 18:01:39 +03:00
|
|
|
for (PRInt32 index = 0; index < numEntries; index++)
|
2001-05-04 01:24:36 +04:00
|
|
|
{
|
2003-11-15 18:01:39 +03:00
|
|
|
if ( MIMEType.Equals(extraMimeEntries[index].mMimeType) )
|
|
|
|
{
|
|
|
|
// This is the one. Set attributes appropriately.
|
2004-04-16 00:56:37 +04:00
|
|
|
aMIMEInfo->SetFileExtensions(nsDependentCString(extraMimeEntries[index].mFileExtensions));
|
|
|
|
aMIMEInfo->SetDescription(NS_ConvertASCIItoUCS2(extraMimeEntries[index].mDescription));
|
2003-11-15 18:01:39 +03:00
|
|
|
aMIMEInfo->SetMacType(extraMimeEntries[index].mMactype);
|
|
|
|
aMIMEInfo->SetMacCreator(extraMimeEntries[index].mMacCreator);
|
|
|
|
|
|
|
|
return NS_OK;
|
2001-05-04 01:24:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-11-15 18:01:39 +03:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2001-11-17 05:13:33 +03:00
|
|
|
}
|
|
|
|
|
2004-06-11 19:47:57 +04:00
|
|
|
nsresult nsExternalHelperAppService::GetMIMEInfoForExtensionFromExtras(const nsACString& aExtension, nsIMIMEInfo * aMIMEInfo)
|
2001-11-17 05:13:33 +03:00
|
|
|
{
|
2004-06-11 19:47:57 +04:00
|
|
|
nsCAutoString type;
|
|
|
|
PRBool found = GetTypeFromExtras(aExtension, type);
|
|
|
|
if (!found)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
return GetMIMEInfoForMimeTypeFromExtras(type, aMIMEInfo);
|
|
|
|
}
|
2003-11-15 18:01:39 +03:00
|
|
|
|
2004-06-11 19:47:57 +04:00
|
|
|
PRBool nsExternalHelperAppService::GetTypeFromExtras(const nsACString& aExtension, nsACString& aMIMEType)
|
|
|
|
{
|
2004-04-16 00:56:37 +04:00
|
|
|
NS_ASSERTION(!aExtension.IsEmpty(), "Empty aExtension parameter!");
|
2001-11-17 05:13:33 +03:00
|
|
|
|
|
|
|
// Look for default entry with matching extension.
|
|
|
|
nsDependentCString::const_iterator start, end, iter;
|
2003-07-15 01:09:11 +04:00
|
|
|
PRInt32 numEntries = NS_ARRAY_LENGTH(extraMimeEntries);
|
2003-11-15 18:01:39 +03:00
|
|
|
for (PRInt32 index = 0; index < numEntries; index++)
|
2001-11-17 05:13:33 +03:00
|
|
|
{
|
|
|
|
nsDependentCString extList(extraMimeEntries[index].mFileExtensions);
|
|
|
|
extList.BeginReading(start);
|
|
|
|
extList.EndReading(end);
|
|
|
|
iter = start;
|
|
|
|
while (start != end)
|
|
|
|
{
|
|
|
|
FindCharInReadable(',', iter, end);
|
2004-04-16 00:56:37 +04:00
|
|
|
if (Substring(start, iter).Equals(aExtension,
|
2002-02-10 18:49:46 +03:00
|
|
|
nsCaseInsensitiveCStringComparator()))
|
2001-11-17 05:13:33 +03:00
|
|
|
{
|
2004-06-11 19:47:57 +04:00
|
|
|
aMIMEType = extraMimeEntries[index].mMimeType;
|
|
|
|
return PR_TRUE;
|
2001-11-17 05:13:33 +03:00
|
|
|
}
|
|
|
|
if (iter != end) {
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
start = iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-06-11 19:47:57 +04:00
|
|
|
return PR_FALSE;
|
2001-05-04 01:24:36 +04:00
|
|
|
}
|
|
|
|
|