2000-06-20 22:22:47 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
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-20 22:22:47 +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-20 22:22:47 +04:00
|
|
|
* Contributor(s):
|
|
|
|
* Scott MacGregor <mscott@netscape.com>
|
2001-08-16 02:17:32 +04:00
|
|
|
* Boris Zbarsky <bzbarsky@mit.edu> (Added mailcap and mime.types support)
|
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-20 22:22:47 +04:00
|
|
|
|
2004-03-06 22:37:22 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2011-02-03 09:31:36 +03:00
|
|
|
#if defined(MOZ_ENABLE_CONTENTACTION)
|
2010-07-30 23:46:20 +04:00
|
|
|
#include <contentaction/contentaction.h>
|
|
|
|
#include <QString>
|
|
|
|
#endif
|
|
|
|
|
2000-06-20 22:22:47 +04:00
|
|
|
#include "nsOSHelperAppService.h"
|
2007-07-26 09:27:43 +04:00
|
|
|
#include "nsMIMEInfoUnix.h"
|
2003-09-30 02:04:05 +04:00
|
|
|
#ifdef MOZ_WIDGET_GTK2
|
|
|
|
#include "nsGNOMERegistry.h"
|
|
|
|
#endif
|
2000-06-20 22:22:47 +04:00
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsString.h"
|
2001-08-16 02:17:32 +04:00
|
|
|
#include "nsReadableUtils.h"
|
2001-10-30 02:14:05 +03:00
|
|
|
#include "nsUnicharUtils.h"
|
2000-06-20 22:22:47 +04:00
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "nsIURL.h"
|
2001-08-16 02:17:32 +04:00
|
|
|
#include "nsIFileStreams.h"
|
|
|
|
#include "nsILineInputStream.h"
|
2000-06-20 22:22:47 +04:00
|
|
|
#include "nsILocalFile.h"
|
2001-12-04 03:27:43 +03:00
|
|
|
#include "nsIProcess.h"
|
2003-01-18 05:15:14 +03:00
|
|
|
#include "nsNetCID.h"
|
2002-09-04 03:36:13 +04:00
|
|
|
#include "nsXPCOM.h"
|
2002-06-18 03:20:46 +04:00
|
|
|
#include "nsISupportsPrimitives.h"
|
2001-08-16 02:17:32 +04:00
|
|
|
#include "nsHashtable.h"
|
|
|
|
#include "nsCRT.h"
|
2003-09-17 16:47:56 +04:00
|
|
|
#include "nsDirectoryServiceDefs.h"
|
|
|
|
#include "nsDirectoryServiceUtils.h"
|
2001-08-16 02:17:32 +04:00
|
|
|
#include "prenv.h" // for PR_GetEnv()
|
2004-02-24 01:49:28 +03:00
|
|
|
#include "nsAutoPtr.h"
|
2011-07-01 12:35:28 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
2000-06-20 22:22:47 +04:00
|
|
|
|
2003-07-25 20:48:00 +04:00
|
|
|
#define LOG(args) PR_LOG(mLog, PR_LOG_DEBUG, args)
|
|
|
|
#define LOG_ENABLED() PR_LOG_TEST(mLog, PR_LOG_DEBUG)
|
2002-06-18 03:20:46 +04:00
|
|
|
|
|
|
|
static nsresult
|
2001-08-16 02:17:32 +04:00
|
|
|
FindSemicolon(nsAString::const_iterator& aSemicolon_iter,
|
|
|
|
const nsAString::const_iterator& aEnd_iter);
|
2002-06-18 03:20:46 +04:00
|
|
|
static nsresult
|
2001-08-16 02:17:32 +04:00
|
|
|
ParseMIMEType(const nsAString::const_iterator& aStart_iter,
|
|
|
|
nsAString::const_iterator& aMajorTypeStart,
|
|
|
|
nsAString::const_iterator& aMajorTypeEnd,
|
|
|
|
nsAString::const_iterator& aMinorTypeStart,
|
|
|
|
nsAString::const_iterator& aMinorTypeEnd,
|
|
|
|
const nsAString::const_iterator& aEnd_iter);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
inline bool
|
2004-04-22 19:32:54 +04:00
|
|
|
IsNetscapeFormat(const nsACString& aBuffer);
|
2001-08-16 02:17:32 +04:00
|
|
|
|
2000-06-20 22:22:47 +04:00
|
|
|
nsOSHelperAppService::nsOSHelperAppService() : nsExternalHelperAppService()
|
|
|
|
{
|
2004-03-06 22:37:22 +03:00
|
|
|
mode_t mask = umask(0777);
|
|
|
|
umask(mask);
|
|
|
|
mPermissions = 0666 & ~mask;
|
2000-06-20 22:22:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsOSHelperAppService::~nsOSHelperAppService()
|
|
|
|
{}
|
|
|
|
|
2001-08-16 02:17:32 +04:00
|
|
|
/*
|
|
|
|
* Take a command with all the mailcap escapes in it and unescape it
|
|
|
|
* Ideally this needs the mime type, mime type options, and location of the
|
|
|
|
* temporary file, but this last can't be got from here
|
|
|
|
*/
|
2003-07-25 20:48:00 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOSHelperAppService::UnescapeCommand(const nsAString& aEscapedCommand,
|
|
|
|
const nsAString& aMajorType,
|
|
|
|
const nsAString& aMinorType,
|
|
|
|
nsHashtable& aTypeOptions,
|
|
|
|
nsACString& aUnEscapedCommand) {
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("-- UnescapeCommand"));
|
|
|
|
LOG(("Command to escape: '%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(aEscapedCommand).get()));
|
2001-08-16 02:17:32 +04:00
|
|
|
// XXX This function will need to get the mime type and various stuff like that being passed in to work properly
|
|
|
|
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("UnescapeCommand really needs some work -- it should actually do some unescaping\n"));
|
|
|
|
|
2003-12-23 19:48:40 +03:00
|
|
|
CopyUTF16toUTF8(aEscapedCommand, aUnEscapedCommand);
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("Escaped command: '%s'\n",
|
|
|
|
PromiseFlatCString(aUnEscapedCommand).get()));
|
2001-08-16 02:17:32 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Put aSemicolon_iter at the first non-escaped semicolon after
|
|
|
|
* aStart_iter but before aEnd_iter
|
|
|
|
*/
|
|
|
|
|
2002-06-18 03:20:46 +04:00
|
|
|
static nsresult
|
2001-08-16 02:17:32 +04:00
|
|
|
FindSemicolon(nsAString::const_iterator& aSemicolon_iter,
|
|
|
|
const nsAString::const_iterator& aEnd_iter) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool semicolonFound = false;
|
2001-08-16 02:17:32 +04:00
|
|
|
while (aSemicolon_iter != aEnd_iter && !semicolonFound) {
|
|
|
|
switch(*aSemicolon_iter) {
|
|
|
|
case '\\':
|
|
|
|
aSemicolon_iter.advance(2);
|
|
|
|
break;
|
|
|
|
case ';':
|
2011-10-17 18:59:28 +04:00
|
|
|
semicolonFound = true;
|
2001-08-16 02:17:32 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
++aSemicolon_iter;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-06-18 03:20:46 +04:00
|
|
|
static nsresult
|
2001-08-16 02:17:32 +04:00
|
|
|
ParseMIMEType(const nsAString::const_iterator& aStart_iter,
|
|
|
|
nsAString::const_iterator& aMajorTypeStart,
|
|
|
|
nsAString::const_iterator& aMajorTypeEnd,
|
|
|
|
nsAString::const_iterator& aMinorTypeStart,
|
|
|
|
nsAString::const_iterator& aMinorTypeEnd,
|
|
|
|
const nsAString::const_iterator& aEnd_iter) {
|
|
|
|
nsAString::const_iterator iter(aStart_iter);
|
|
|
|
|
|
|
|
// skip leading whitespace
|
|
|
|
while (iter != aEnd_iter && nsCRT::IsAsciiSpace(*iter)) {
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iter == aEnd_iter) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
aMajorTypeStart = iter;
|
|
|
|
|
|
|
|
// find major/minor separator ('/')
|
|
|
|
while (iter != aEnd_iter && *iter != '/') {
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iter == aEnd_iter) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
aMajorTypeEnd = iter;
|
|
|
|
|
|
|
|
// skip '/'
|
|
|
|
++iter;
|
|
|
|
|
|
|
|
if (iter == aEnd_iter) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
aMinorTypeStart = iter;
|
|
|
|
|
|
|
|
// find end of minor type, delimited by whitespace or ';'
|
|
|
|
while (iter != aEnd_iter && !nsCRT::IsAsciiSpace(*iter) && *iter != ';') {
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
aMinorTypeEnd = iter;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-07-25 20:48:00 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOSHelperAppService::GetFileLocation(const char* aPrefName,
|
|
|
|
const char* aEnvVarName,
|
2011-07-01 12:35:28 +04:00
|
|
|
nsAString& aFileLocation) {
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("-- GetFileLocation. Pref: '%s' EnvVar: '%s'\n",
|
|
|
|
aPrefName,
|
|
|
|
aEnvVarName));
|
|
|
|
NS_PRECONDITION(aPrefName, "Null pref name passed; don't do that!");
|
|
|
|
|
2011-07-01 12:35:28 +04:00
|
|
|
aFileLocation.Truncate();
|
2002-06-18 03:20:46 +04:00
|
|
|
/* The lookup order is:
|
|
|
|
1) user pref
|
|
|
|
2) env var
|
|
|
|
3) pref
|
|
|
|
*/
|
2011-07-01 12:35:28 +04:00
|
|
|
NS_ENSURE_TRUE(Preferences::GetRootBranch(), NS_ERROR_FAILURE);
|
2002-06-18 03:20:46 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
If we have an env var we should check whether the pref is a user
|
|
|
|
pref. If we do not, we don't care.
|
|
|
|
*/
|
2011-07-01 12:35:28 +04:00
|
|
|
if (Preferences::HasUserValue(aPrefName) &&
|
|
|
|
NS_SUCCEEDED(Preferences::GetString(aPrefName, &aFileLocation))) {
|
|
|
|
return NS_OK;
|
2002-06-18 03:20:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aEnvVarName && *aEnvVarName) {
|
|
|
|
char* prefValue = PR_GetEnv(aEnvVarName);
|
|
|
|
if (prefValue && *prefValue) {
|
|
|
|
// the pref is in the system charset and it's a filepath... The
|
|
|
|
// natural way to do the charset conversion is by just initing
|
|
|
|
// an nsIFile with the native path and asking it for the Unicode
|
|
|
|
// version.
|
2011-07-01 12:35:28 +04:00
|
|
|
nsresult rv;
|
2002-06-18 03:20:46 +04:00
|
|
|
nsCOMPtr<nsILocalFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = file->InitWithNativePath(nsDependentCString(prefValue));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-07-01 12:35:28 +04:00
|
|
|
rv = file->GetPath(aFileLocation);
|
2002-06-18 03:20:46 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-07-01 12:35:28 +04:00
|
|
|
|
2002-06-18 03:20:46 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
2011-07-01 12:35:28 +04:00
|
|
|
|
|
|
|
return Preferences::GetString(aPrefName, &aFileLocation);
|
2002-06-18 03:20:46 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-16 02:17:32 +04:00
|
|
|
/* Get the mime.types file names from prefs and look up info in them
|
|
|
|
based on extension */
|
2003-07-25 20:48:00 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOSHelperAppService::LookUpTypeAndDescription(const nsAString& aFileExtension,
|
2003-10-10 20:18:16 +04:00
|
|
|
nsAString& aMajorType,
|
|
|
|
nsAString& aMinorType,
|
|
|
|
nsAString& aDescription,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aUserData) {
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("-- LookUpTypeAndDescription for extension '%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(aFileExtension).get()));
|
2001-08-16 02:17:32 +04:00
|
|
|
nsresult rv = NS_OK;
|
2011-07-01 12:35:28 +04:00
|
|
|
nsAutoString mimeFileName;
|
2001-08-16 02:17:32 +04:00
|
|
|
|
2003-10-10 20:18:16 +04:00
|
|
|
const char* filenamePref = aUserData ?
|
|
|
|
"helpers.private_mime_types_file" : "helpers.global_mime_types_file";
|
|
|
|
|
2011-07-01 12:35:28 +04:00
|
|
|
rv = GetFileLocation(filenamePref, nsnull, mimeFileName);
|
2002-06-18 03:20:46 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && !mimeFileName.IsEmpty()) {
|
|
|
|
rv = GetTypeAndDescriptionFromMimetypesFile(mimeFileName,
|
|
|
|
aFileExtension,
|
|
|
|
aMajorType,
|
|
|
|
aMinorType,
|
|
|
|
aDescription);
|
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2003-10-10 20:18:16 +04:00
|
|
|
|
2001-08-16 02:17:32 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
inline bool
|
2004-04-22 19:32:54 +04:00
|
|
|
IsNetscapeFormat(const nsACString& aBuffer) {
|
|
|
|
return StringBeginsWith(aBuffer, NS_LITERAL_CSTRING("#--Netscape Communications Corporation MIME Information")) ||
|
|
|
|
StringBeginsWith(aBuffer, NS_LITERAL_CSTRING("#--MCOM MIME Information"));
|
2001-08-16 02:17:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a file stream and line input stream for the filename.
|
|
|
|
* Leaves the first line of the file in aBuffer and sets the format to
|
2011-10-17 18:59:28 +04:00
|
|
|
* true for netscape files and false for normail ones
|
2001-08-16 02:17:32 +04:00
|
|
|
*/
|
2003-07-25 20:48:00 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOSHelperAppService::CreateInputStream(const nsAString& aFilename,
|
|
|
|
nsIFileInputStream ** aFileInputStream,
|
|
|
|
nsILineInputStream ** aLineInputStream,
|
2004-04-22 19:32:54 +04:00
|
|
|
nsACString& aBuffer,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool * aNetscapeFormat,
|
|
|
|
bool * aMore) {
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("-- CreateInputStream"));
|
2001-08-16 02:17:32 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsILocalFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2002-05-08 03:07:19 +04:00
|
|
|
rv = file->InitWithPath(aFilename);
|
2001-08-16 02:17:32 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFileInputStream> fileStream(do_CreateInstance(NS_LOCALFILEINPUTSTREAM_CONTRACTID, &rv));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = fileStream->Init(file, -1, -1, false);
|
2001-08-16 02:17:32 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsILineInputStream> lineStream(do_QueryInterface(fileStream, &rv));
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("Interface trouble in stream land!"));
|
2001-08-16 02:17:32 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-09-18 07:53:25 +04:00
|
|
|
rv = lineStream->ReadLine(aBuffer, aMore);
|
|
|
|
if (NS_FAILED(rv)) {
|
2001-08-16 02:17:32 +04:00
|
|
|
fileStream->Close();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aNetscapeFormat = IsNetscapeFormat(aBuffer);
|
|
|
|
|
|
|
|
*aFileInputStream = fileStream;
|
|
|
|
NS_ADDREF(*aFileInputStream);
|
|
|
|
*aLineInputStream = lineStream;
|
|
|
|
NS_ADDREF(*aLineInputStream);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the file, read the first line, decide what type of file it is,
|
|
|
|
then get info based on extension */
|
2003-07-25 20:48:00 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOSHelperAppService::GetTypeAndDescriptionFromMimetypesFile(const nsAString& aFilename,
|
|
|
|
const nsAString& aFileExtension,
|
|
|
|
nsAString& aMajorType,
|
|
|
|
nsAString& aMinorType,
|
|
|
|
nsAString& aDescription) {
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("-- GetTypeAndDescriptionFromMimetypesFile\n"));
|
|
|
|
LOG(("Getting type and description from types file '%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(aFilename).get()));
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("Using extension '%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(aFileExtension).get()));
|
2001-08-16 02:17:32 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsIFileInputStream> mimeFile;
|
|
|
|
nsCOMPtr<nsILineInputStream> mimeTypes;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool netscapeFormat;
|
2001-08-16 02:17:32 +04:00
|
|
|
nsAutoString buf;
|
2004-04-22 19:32:54 +04:00
|
|
|
nsCAutoString cBuf;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool more = false;
|
2001-08-16 02:17:32 +04:00
|
|
|
rv = CreateInputStream(aFilename, getter_AddRefs(mimeFile), getter_AddRefs(mimeTypes),
|
2004-04-22 19:32:54 +04:00
|
|
|
cBuf, &netscapeFormat, &more);
|
2001-08-16 02:17:32 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString extensions;
|
|
|
|
nsString entry;
|
|
|
|
entry.SetCapacity(100);
|
|
|
|
nsAString::const_iterator majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd,
|
|
|
|
descriptionStart, descriptionEnd;
|
|
|
|
|
|
|
|
do {
|
2004-05-04 05:05:13 +04:00
|
|
|
CopyASCIItoUTF16(cBuf, buf);
|
2001-08-16 02:17:32 +04:00
|
|
|
// read through, building up an entry. If we finish an entry, check for
|
|
|
|
// a match and return out of the loop if we match
|
|
|
|
|
|
|
|
// skip comments and empty lines
|
|
|
|
if (!buf.IsEmpty() && buf.First() != '#') {
|
|
|
|
entry.Append(buf);
|
|
|
|
if (entry.Last() == '\\') {
|
|
|
|
entry.Truncate(entry.Length() - 1);
|
|
|
|
entry.Append(PRUnichar(' ')); // in case there is no trailing whitespace on this line
|
|
|
|
} else { // we have a full entry
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("Current entry: '%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(entry).get()));
|
2001-08-16 02:17:32 +04:00
|
|
|
if (netscapeFormat) {
|
|
|
|
rv = ParseNetscapeMIMETypesEntry(entry,
|
|
|
|
majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd,
|
|
|
|
extensions,
|
|
|
|
descriptionStart, descriptionEnd);
|
2001-10-25 18:26:07 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2002-06-18 03:20:46 +04:00
|
|
|
// We sometimes get things like RealPlayer appending
|
|
|
|
// "normal" entries to "Netscape" .mime.types files. Try
|
|
|
|
// to handle that. Bug 106381.
|
|
|
|
LOG(("Bogus entry; trying 'normal' mode\n"));
|
2001-10-25 18:26:07 +04:00
|
|
|
rv = ParseNormalMIMETypesEntry(entry,
|
|
|
|
majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd,
|
|
|
|
extensions,
|
|
|
|
descriptionStart, descriptionEnd);
|
|
|
|
}
|
2001-08-16 02:17:32 +04:00
|
|
|
} else {
|
|
|
|
rv = ParseNormalMIMETypesEntry(entry,
|
|
|
|
majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd,
|
|
|
|
extensions,
|
|
|
|
descriptionStart, descriptionEnd);
|
2002-06-18 03:20:46 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// We sometimes get things like StarOffice prepending
|
|
|
|
// "normal" entries to "Netscape" .mime.types files. Try
|
|
|
|
// to handle that. Bug 136670.
|
|
|
|
LOG(("Bogus entry; trying 'Netscape' mode\n"));
|
|
|
|
rv = ParseNetscapeMIMETypesEntry(entry,
|
|
|
|
majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd,
|
|
|
|
extensions,
|
|
|
|
descriptionStart, descriptionEnd);
|
|
|
|
}
|
2001-08-16 02:17:32 +04:00
|
|
|
}
|
2002-06-18 03:20:46 +04:00
|
|
|
|
2001-08-16 02:17:32 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) { // entry parses
|
2001-10-18 07:38:52 +04:00
|
|
|
nsAString::const_iterator start, end;
|
|
|
|
extensions.BeginReading(start);
|
|
|
|
extensions.EndReading(end);
|
|
|
|
nsAString::const_iterator iter(start);
|
|
|
|
|
|
|
|
while (start != end) {
|
|
|
|
FindCharInReadable(',', iter, end);
|
2002-02-10 18:49:46 +03:00
|
|
|
if (Substring(start, iter).Equals(aFileExtension,
|
|
|
|
nsCaseInsensitiveStringComparator())) {
|
2001-08-16 02:17:32 +04:00
|
|
|
// it's a match. Assign the type and description and run
|
|
|
|
aMajorType.Assign(Substring(majorTypeStart, majorTypeEnd));
|
|
|
|
aMinorType.Assign(Substring(minorTypeStart, minorTypeEnd));
|
|
|
|
aDescription.Assign(Substring(descriptionStart, descriptionEnd));
|
|
|
|
mimeFile->Close();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-10-18 07:38:52 +04:00
|
|
|
if (iter != end) {
|
|
|
|
++iter;
|
2001-08-16 02:17:32 +04:00
|
|
|
}
|
2001-10-18 07:38:52 +04:00
|
|
|
start = iter;
|
2001-08-16 02:17:32 +04:00
|
|
|
}
|
2002-06-18 03:20:46 +04:00
|
|
|
} else {
|
2006-02-03 17:18:39 +03:00
|
|
|
LOG(("Failed to parse entry: %s\n", NS_LossyConvertUTF16toASCII(entry).get()));
|
2001-08-16 02:17:32 +04:00
|
|
|
}
|
|
|
|
// truncate the entry for the next iteration
|
|
|
|
entry.Truncate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!more) {
|
2002-06-18 03:20:46 +04:00
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
2001-08-16 02:17:32 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// read the next line
|
2004-04-22 19:32:54 +04:00
|
|
|
rv = mimeTypes->ReadLine(cBuf, &more);
|
2001-08-16 02:17:32 +04:00
|
|
|
} while (NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
mimeFile->Close();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the mime.types file names from prefs and look up info in them
|
|
|
|
based on mimetype */
|
2003-07-25 20:48:00 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOSHelperAppService::LookUpExtensionsAndDescription(const nsAString& aMajorType,
|
|
|
|
const nsAString& aMinorType,
|
|
|
|
nsAString& aFileExtensions,
|
|
|
|
nsAString& aDescription) {
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("-- LookUpExtensionsAndDescription for type '%s/%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(aMajorType).get(),
|
|
|
|
NS_LossyConvertUTF16toASCII(aMinorType).get()));
|
2001-08-16 02:17:32 +04:00
|
|
|
nsresult rv = NS_OK;
|
2011-07-01 12:35:28 +04:00
|
|
|
nsAutoString mimeFileName;
|
2001-08-16 02:17:32 +04:00
|
|
|
|
2011-07-01 12:35:28 +04:00
|
|
|
rv = GetFileLocation("helpers.private_mime_types_file", nsnull, mimeFileName);
|
2002-06-18 03:20:46 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && !mimeFileName.IsEmpty()) {
|
|
|
|
rv = GetExtensionsAndDescriptionFromMimetypesFile(mimeFileName,
|
|
|
|
aMajorType,
|
|
|
|
aMinorType,
|
|
|
|
aFileExtensions,
|
|
|
|
aDescription);
|
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv) || aFileExtensions.IsEmpty()) {
|
|
|
|
rv = GetFileLocation("helpers.global_mime_types_file",
|
2011-07-01 12:35:28 +04:00
|
|
|
nsnull, mimeFileName);
|
2001-08-16 02:17:32 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && !mimeFileName.IsEmpty()) {
|
|
|
|
rv = GetExtensionsAndDescriptionFromMimetypesFile(mimeFileName,
|
|
|
|
aMajorType,
|
|
|
|
aMinorType,
|
|
|
|
aFileExtensions,
|
|
|
|
aDescription);
|
2002-06-18 03:20:46 +04:00
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
2001-08-16 02:17:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the file, read the first line, decide what type of file it is,
|
|
|
|
then get info based on extension */
|
2003-07-25 20:48:00 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOSHelperAppService::GetExtensionsAndDescriptionFromMimetypesFile(const nsAString& aFilename,
|
|
|
|
const nsAString& aMajorType,
|
|
|
|
const nsAString& aMinorType,
|
|
|
|
nsAString& aFileExtensions,
|
|
|
|
nsAString& aDescription) {
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("-- GetExtensionsAndDescriptionFromMimetypesFile\n"));
|
|
|
|
LOG(("Getting extensions and description from types file '%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(aFilename).get()));
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("Using type '%s/%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(aMajorType).get(),
|
|
|
|
NS_LossyConvertUTF16toASCII(aMinorType).get()));
|
2002-06-18 03:20:46 +04:00
|
|
|
|
2001-08-16 02:17:32 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsIFileInputStream> mimeFile;
|
|
|
|
nsCOMPtr<nsILineInputStream> mimeTypes;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool netscapeFormat;
|
2004-04-22 19:32:54 +04:00
|
|
|
nsCAutoString cBuf;
|
2001-08-16 02:17:32 +04:00
|
|
|
nsAutoString buf;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool more = false;
|
2001-08-16 02:17:32 +04:00
|
|
|
rv = CreateInputStream(aFilename, getter_AddRefs(mimeFile), getter_AddRefs(mimeTypes),
|
2004-04-22 19:32:54 +04:00
|
|
|
cBuf, &netscapeFormat, &more);
|
2001-08-16 02:17:32 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString extensions;
|
|
|
|
nsString entry;
|
|
|
|
entry.SetCapacity(100);
|
|
|
|
nsAString::const_iterator majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd,
|
|
|
|
descriptionStart, descriptionEnd;
|
|
|
|
|
|
|
|
do {
|
2004-04-22 19:32:54 +04:00
|
|
|
CopyASCIItoUTF16(cBuf, buf);
|
2001-08-16 02:17:32 +04:00
|
|
|
// read through, building up an entry. If we finish an entry, check for
|
|
|
|
// a match and return out of the loop if we match
|
|
|
|
|
|
|
|
// skip comments and empty lines
|
|
|
|
if (!buf.IsEmpty() && buf.First() != '#') {
|
|
|
|
entry.Append(buf);
|
|
|
|
if (entry.Last() == '\\') {
|
|
|
|
entry.Truncate(entry.Length() - 1);
|
|
|
|
entry.Append(PRUnichar(' ')); // in case there is no trailing whitespace on this line
|
|
|
|
} else { // we have a full entry
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("Current entry: '%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(entry).get()));
|
2001-08-16 02:17:32 +04:00
|
|
|
if (netscapeFormat) {
|
|
|
|
rv = ParseNetscapeMIMETypesEntry(entry,
|
|
|
|
majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd,
|
|
|
|
extensions,
|
|
|
|
descriptionStart, descriptionEnd);
|
|
|
|
|
2002-06-18 03:20:46 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// We sometimes get things like RealPlayer appending
|
|
|
|
// "normal" entries to "Netscape" .mime.types files. Try
|
|
|
|
// to handle that. Bug 106381.
|
|
|
|
LOG(("Bogus entry; trying 'normal' mode\n"));
|
|
|
|
rv = ParseNormalMIMETypesEntry(entry,
|
|
|
|
majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd,
|
|
|
|
extensions,
|
|
|
|
descriptionStart, descriptionEnd);
|
|
|
|
}
|
2001-08-16 02:17:32 +04:00
|
|
|
} else {
|
|
|
|
rv = ParseNormalMIMETypesEntry(entry,
|
|
|
|
majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart,
|
|
|
|
minorTypeEnd, extensions,
|
|
|
|
descriptionStart, descriptionEnd);
|
|
|
|
|
2002-06-18 03:20:46 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// We sometimes get things like StarOffice prepending
|
|
|
|
// "normal" entries to "Netscape" .mime.types files. Try
|
|
|
|
// to handle that. Bug 136670.
|
|
|
|
LOG(("Bogus entry; trying 'Netscape' mode\n"));
|
|
|
|
rv = ParseNetscapeMIMETypesEntry(entry,
|
|
|
|
majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd,
|
|
|
|
extensions,
|
|
|
|
descriptionStart, descriptionEnd);
|
|
|
|
}
|
2001-08-16 02:17:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) &&
|
2002-02-10 18:49:46 +03:00
|
|
|
Substring(majorTypeStart,
|
|
|
|
majorTypeEnd).Equals(aMajorType,
|
|
|
|
nsCaseInsensitiveStringComparator())&&
|
|
|
|
Substring(minorTypeStart,
|
|
|
|
minorTypeEnd).Equals(aMinorType,
|
|
|
|
nsCaseInsensitiveStringComparator())) {
|
2001-08-16 02:17:32 +04:00
|
|
|
// it's a match
|
|
|
|
aFileExtensions.Assign(extensions);
|
|
|
|
aDescription.Assign(Substring(descriptionStart, descriptionEnd));
|
|
|
|
mimeFile->Close();
|
|
|
|
return NS_OK;
|
2002-06-18 03:20:46 +04:00
|
|
|
} else if (NS_FAILED(rv)) {
|
2006-02-03 17:18:39 +03:00
|
|
|
LOG(("Failed to parse entry: %s\n", NS_LossyConvertUTF16toASCII(entry).get()));
|
2001-08-16 02:17:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
entry.Truncate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!more) {
|
2002-06-18 03:20:46 +04:00
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
2001-08-16 02:17:32 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// read the next line
|
2004-04-22 19:32:54 +04:00
|
|
|
rv = mimeTypes->ReadLine(cBuf, &more);
|
2001-08-16 02:17:32 +04:00
|
|
|
} while (NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
mimeFile->Close();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This parses a Netscape format mime.types entry. There are two
|
|
|
|
* possible formats:
|
|
|
|
*
|
|
|
|
* type=foo/bar; options exts="baz" description="Some type"
|
|
|
|
*
|
|
|
|
* and
|
|
|
|
*
|
|
|
|
* type=foo/bar; options description="Some type" exts="baz"
|
|
|
|
*/
|
2003-07-25 20:48:00 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOSHelperAppService::ParseNetscapeMIMETypesEntry(const nsAString& aEntry,
|
|
|
|
nsAString::const_iterator& aMajorTypeStart,
|
|
|
|
nsAString::const_iterator& aMajorTypeEnd,
|
|
|
|
nsAString::const_iterator& aMinorTypeStart,
|
|
|
|
nsAString::const_iterator& aMinorTypeEnd,
|
|
|
|
nsAString& aExtensions,
|
|
|
|
nsAString::const_iterator& aDescriptionStart,
|
|
|
|
nsAString::const_iterator& aDescriptionEnd) {
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("-- ParseNetscapeMIMETypesEntry\n"));
|
2001-08-16 02:17:32 +04:00
|
|
|
NS_ASSERTION(!aEntry.IsEmpty(), "Empty Netscape MIME types entry being parsed.");
|
|
|
|
|
|
|
|
nsAString::const_iterator start_iter, end_iter, match_start, match_end;
|
|
|
|
|
|
|
|
aEntry.BeginReading(start_iter);
|
|
|
|
aEntry.EndReading(end_iter);
|
|
|
|
|
|
|
|
// skip trailing whitespace
|
|
|
|
do {
|
|
|
|
--end_iter;
|
|
|
|
} while (end_iter != start_iter &&
|
|
|
|
nsCRT::IsAsciiSpace(*end_iter));
|
|
|
|
// if we're pointing to a quote, don't advance -- we don't want to
|
|
|
|
// include the quote....
|
|
|
|
if (*end_iter != '"')
|
|
|
|
++end_iter;
|
|
|
|
match_start = start_iter;
|
|
|
|
match_end = end_iter;
|
|
|
|
|
|
|
|
// Get the major and minor types
|
|
|
|
// First the major type
|
|
|
|
if (! FindInReadable(NS_LITERAL_STRING("type="), match_start, match_end)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
match_start = match_end;
|
|
|
|
|
|
|
|
while (match_end != end_iter &&
|
|
|
|
*match_end != '/') {
|
|
|
|
++match_end;
|
|
|
|
}
|
|
|
|
if (match_end == end_iter) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
aMajorTypeStart = match_start;
|
|
|
|
aMajorTypeEnd = match_end;
|
|
|
|
|
|
|
|
// now the minor type
|
|
|
|
if (++match_end == end_iter) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
match_start = match_end;
|
|
|
|
|
|
|
|
while (match_end != end_iter &&
|
|
|
|
!nsCRT::IsAsciiSpace(*match_end) &&
|
|
|
|
*match_end != ';') {
|
|
|
|
++match_end;
|
|
|
|
}
|
|
|
|
if (match_end == end_iter) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
aMinorTypeStart = match_start;
|
|
|
|
aMinorTypeEnd = match_end;
|
|
|
|
|
|
|
|
// ignore everything up to the end of the mime type from here on
|
|
|
|
start_iter = match_end;
|
|
|
|
|
|
|
|
// get the extensions
|
|
|
|
match_start = match_end;
|
|
|
|
match_end = end_iter;
|
|
|
|
if (FindInReadable(NS_LITERAL_STRING("exts="), match_start, match_end)) {
|
|
|
|
nsAString::const_iterator extStart, extEnd;
|
|
|
|
|
|
|
|
if (match_end == end_iter ||
|
|
|
|
(*match_end == '"' && ++match_end == end_iter)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
extStart = match_end;
|
|
|
|
match_start = extStart;
|
|
|
|
match_end = end_iter;
|
|
|
|
if (FindInReadable(NS_LITERAL_STRING("desc=\""), match_start, match_end)) {
|
|
|
|
// exts= before desc=, so we have to find the actual end of the extensions
|
|
|
|
extEnd = match_start;
|
|
|
|
if (extEnd == extStart) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
--extEnd;
|
|
|
|
} while (extEnd != extStart &&
|
|
|
|
nsCRT::IsAsciiSpace(*extEnd));
|
|
|
|
|
|
|
|
if (extEnd != extStart && *extEnd == '"') {
|
|
|
|
--extEnd;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// desc= before exts=, so we can use end_iter as the end of the extensions
|
|
|
|
extEnd = end_iter;
|
|
|
|
}
|
|
|
|
aExtensions = Substring(extStart, extEnd);
|
|
|
|
} else {
|
|
|
|
// no extensions
|
|
|
|
aExtensions.Truncate();
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the description
|
|
|
|
match_start = start_iter;
|
|
|
|
match_end = end_iter;
|
|
|
|
if (FindInReadable(NS_LITERAL_STRING("desc=\""), match_start, match_end)) {
|
|
|
|
aDescriptionStart = match_end;
|
|
|
|
match_start = aDescriptionStart;
|
|
|
|
match_end = end_iter;
|
|
|
|
if (FindInReadable(NS_LITERAL_STRING("exts="), match_start, match_end)) {
|
|
|
|
// exts= after desc=, so have to find actual end of description
|
|
|
|
aDescriptionEnd = match_start;
|
|
|
|
if (aDescriptionEnd == aDescriptionStart) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
--aDescriptionEnd;
|
|
|
|
} while (aDescriptionEnd != aDescriptionStart &&
|
|
|
|
nsCRT::IsAsciiSpace(*aDescriptionEnd));
|
|
|
|
|
|
|
|
if (aDescriptionStart != aDescriptionStart && *aDescriptionEnd == '"') {
|
|
|
|
--aDescriptionEnd;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// desc= after exts=, so use end_iter for the description end
|
|
|
|
aDescriptionEnd = end_iter;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// no description
|
|
|
|
aDescriptionStart = start_iter;
|
|
|
|
aDescriptionEnd = start_iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This parses a normal format mime.types entry. The format is:
|
|
|
|
*
|
|
|
|
* major/minor ext1 ext2 ext3
|
|
|
|
*/
|
2003-07-25 20:48:00 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOSHelperAppService::ParseNormalMIMETypesEntry(const nsAString& aEntry,
|
|
|
|
nsAString::const_iterator& aMajorTypeStart,
|
|
|
|
nsAString::const_iterator& aMajorTypeEnd,
|
|
|
|
nsAString::const_iterator& aMinorTypeStart,
|
|
|
|
nsAString::const_iterator& aMinorTypeEnd,
|
|
|
|
nsAString& aExtensions,
|
|
|
|
nsAString::const_iterator& aDescriptionStart,
|
|
|
|
nsAString::const_iterator& aDescriptionEnd) {
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("-- ParseNormalMIMETypesEntry\n"));
|
2001-08-16 02:17:32 +04:00
|
|
|
NS_ASSERTION(!aEntry.IsEmpty(), "Empty Normal MIME types entry being parsed.");
|
|
|
|
|
|
|
|
nsAString::const_iterator start_iter, end_iter, iter;
|
|
|
|
|
|
|
|
aEntry.BeginReading(start_iter);
|
|
|
|
aEntry.EndReading(end_iter);
|
|
|
|
|
|
|
|
// no description
|
|
|
|
aDescriptionStart = start_iter;
|
|
|
|
aDescriptionEnd = start_iter;
|
|
|
|
|
|
|
|
// skip leading whitespace
|
|
|
|
while (start_iter != end_iter && nsCRT::IsAsciiSpace(*start_iter)) {
|
|
|
|
++start_iter;
|
|
|
|
}
|
|
|
|
if (start_iter == end_iter) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
// skip trailing whitespace
|
|
|
|
do {
|
|
|
|
--end_iter;
|
|
|
|
} while (end_iter != start_iter && nsCRT::IsAsciiSpace(*end_iter));
|
|
|
|
|
|
|
|
++end_iter; // point to first whitespace char (or to end of string)
|
|
|
|
iter = start_iter;
|
|
|
|
|
|
|
|
// get the major type
|
2002-06-18 03:20:46 +04:00
|
|
|
if (! FindCharInReadable('/', iter, end_iter))
|
2001-08-16 02:17:32 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2002-06-18 03:20:46 +04:00
|
|
|
|
|
|
|
nsAString::const_iterator equals_sign_iter(start_iter);
|
|
|
|
if (FindCharInReadable('=', equals_sign_iter, iter))
|
|
|
|
return NS_ERROR_FAILURE; // see bug 136670
|
|
|
|
|
2001-08-16 02:17:32 +04:00
|
|
|
aMajorTypeStart = start_iter;
|
|
|
|
aMajorTypeEnd = iter;
|
|
|
|
|
|
|
|
// get the minor type
|
|
|
|
if (++iter == end_iter) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
start_iter = iter;
|
|
|
|
|
|
|
|
while (iter != end_iter && !nsCRT::IsAsciiSpace(*iter)) {
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
aMinorTypeStart = start_iter;
|
|
|
|
aMinorTypeEnd = iter;
|
|
|
|
|
|
|
|
// get the extensions
|
|
|
|
aExtensions.Truncate();
|
|
|
|
while (iter != end_iter) {
|
|
|
|
while (iter != end_iter && nsCRT::IsAsciiSpace(*iter)) {
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
start_iter = iter;
|
|
|
|
while (iter != end_iter && !nsCRT::IsAsciiSpace(*iter)) {
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
aExtensions.Append(Substring(start_iter, iter));
|
|
|
|
if (iter != end_iter) { // not the last extension
|
2002-06-18 03:20:46 +04:00
|
|
|
aExtensions.Append(PRUnichar(','));
|
2001-08-16 02:17:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-07-25 20:48:00 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOSHelperAppService::LookUpHandlerAndDescription(const nsAString& aMajorType,
|
|
|
|
const nsAString& aMinorType,
|
|
|
|
nsHashtable& aTypeOptions,
|
|
|
|
nsAString& aHandler,
|
|
|
|
nsAString& aDescription,
|
|
|
|
nsAString& aMozillaFlags) {
|
2003-07-27 20:27:15 +04:00
|
|
|
|
2004-01-05 00:50:19 +03:00
|
|
|
// The mailcap lookup is two-pass to handle the case of mailcap files
|
|
|
|
// that have something like:
|
|
|
|
//
|
|
|
|
// text/*; emacs %s
|
|
|
|
// text/rtf; soffice %s
|
|
|
|
//
|
|
|
|
// in that order. We want to pick up "soffice" for text/rtf in such cases
|
2003-07-27 20:27:15 +04:00
|
|
|
nsresult rv = DoLookUpHandlerAndDescription(aMajorType,
|
|
|
|
aMinorType,
|
|
|
|
aTypeOptions,
|
|
|
|
aHandler,
|
|
|
|
aDescription,
|
2003-10-10 20:18:16 +04:00
|
|
|
aMozillaFlags,
|
2011-10-17 18:59:28 +04:00
|
|
|
true);
|
2003-10-10 20:18:16 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
rv = DoLookUpHandlerAndDescription(aMajorType,
|
|
|
|
aMinorType,
|
|
|
|
aTypeOptions,
|
|
|
|
aHandler,
|
|
|
|
aDescription,
|
|
|
|
aMozillaFlags,
|
2011-10-17 18:59:28 +04:00
|
|
|
false);
|
2003-10-10 20:18:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// maybe we have an entry for "aMajorType/*"?
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
rv = DoLookUpHandlerAndDescription(aMajorType,
|
|
|
|
NS_LITERAL_STRING("*"),
|
|
|
|
aTypeOptions,
|
|
|
|
aHandler,
|
|
|
|
aDescription,
|
|
|
|
aMozillaFlags,
|
2011-10-17 18:59:28 +04:00
|
|
|
true);
|
2003-10-10 20:18:16 +04:00
|
|
|
}
|
|
|
|
|
2003-07-27 20:27:15 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
rv = DoLookUpHandlerAndDescription(aMajorType,
|
|
|
|
NS_LITERAL_STRING("*"),
|
|
|
|
aTypeOptions,
|
|
|
|
aHandler,
|
|
|
|
aDescription,
|
2003-10-10 20:18:16 +04:00
|
|
|
aMozillaFlags,
|
2011-10-17 18:59:28 +04:00
|
|
|
false);
|
2003-07-27 20:27:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOSHelperAppService::DoLookUpHandlerAndDescription(const nsAString& aMajorType,
|
|
|
|
const nsAString& aMinorType,
|
|
|
|
nsHashtable& aTypeOptions,
|
|
|
|
nsAString& aHandler,
|
|
|
|
nsAString& aDescription,
|
2003-10-10 20:18:16 +04:00
|
|
|
nsAString& aMozillaFlags,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aUserData) {
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("-- LookUpHandlerAndDescription for type '%s/%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(aMajorType).get(),
|
|
|
|
NS_LossyConvertUTF16toASCII(aMinorType).get()));
|
2001-08-16 02:17:32 +04:00
|
|
|
nsresult rv = NS_OK;
|
2011-07-01 12:35:28 +04:00
|
|
|
nsAutoString mailcapFileName;
|
2001-08-16 02:17:32 +04:00
|
|
|
|
2003-10-10 20:18:16 +04:00
|
|
|
const char * filenamePref = aUserData ?
|
|
|
|
"helpers.private_mailcap_file" : "helpers.global_mailcap_file";
|
|
|
|
const char * filenameEnvVar = aUserData ?
|
|
|
|
"PERSONAL_MAILCAP" : "MAILCAP";
|
|
|
|
|
2011-07-01 12:35:28 +04:00
|
|
|
rv = GetFileLocation(filenamePref, filenameEnvVar, mailcapFileName);
|
2002-06-18 03:20:46 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && !mailcapFileName.IsEmpty()) {
|
|
|
|
rv = GetHandlerAndDescriptionFromMailcapFile(mailcapFileName,
|
|
|
|
aMajorType,
|
|
|
|
aMinorType,
|
|
|
|
aTypeOptions,
|
|
|
|
aHandler,
|
|
|
|
aDescription,
|
|
|
|
aMozillaFlags);
|
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
2003-10-10 20:18:16 +04:00
|
|
|
|
2001-08-16 02:17:32 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2003-07-25 20:48:00 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOSHelperAppService::GetHandlerAndDescriptionFromMailcapFile(const nsAString& aFilename,
|
|
|
|
const nsAString& aMajorType,
|
|
|
|
const nsAString& aMinorType,
|
|
|
|
nsHashtable& aTypeOptions,
|
|
|
|
nsAString& aHandler,
|
|
|
|
nsAString& aDescription,
|
|
|
|
nsAString& aMozillaFlags) {
|
2001-08-16 02:17:32 +04:00
|
|
|
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("-- GetHandlerAndDescriptionFromMailcapFile\n"));
|
|
|
|
LOG(("Getting handler and description from mailcap file '%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(aFilename).get()));
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("Using type '%s/%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(aMajorType).get(),
|
|
|
|
NS_LossyConvertUTF16toASCII(aMinorType).get()));
|
2002-06-18 03:20:46 +04:00
|
|
|
|
2001-08-16 02:17:32 +04:00
|
|
|
nsresult rv = NS_OK;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool more = false;
|
2001-08-16 02:17:32 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsILocalFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2002-05-08 03:07:19 +04:00
|
|
|
rv = file->InitWithPath(aFilename);
|
2001-08-16 02:17:32 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFileInputStream> mailcapFile(do_CreateInstance(NS_LOCALFILEINPUTSTREAM_CONTRACTID, &rv));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = mailcapFile->Init(file, -1, -1, false);
|
2001-08-16 02:17:32 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsILineInputStream> mailcap (do_QueryInterface(mailcapFile, &rv));
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("Interface trouble in stream land!"));
|
2001-08-16 02:17:32 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString entry, buffer;
|
2004-04-22 19:32:54 +04:00
|
|
|
nsCAutoString cBuffer;
|
2001-08-16 02:17:32 +04:00
|
|
|
entry.SetCapacity(128);
|
2004-04-22 19:32:54 +04:00
|
|
|
cBuffer.SetCapacity(80);
|
|
|
|
rv = mailcap->ReadLine(cBuffer, &more);
|
2001-08-16 02:17:32 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mailcapFile->Close();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
do { // return on end-of-file in the loop
|
|
|
|
|
2004-04-22 19:32:54 +04:00
|
|
|
CopyASCIItoUTF16(cBuffer, buffer);
|
2001-08-16 02:17:32 +04:00
|
|
|
if (!buffer.IsEmpty() && buffer.First() != '#') {
|
|
|
|
entry.Append(buffer);
|
|
|
|
if (entry.Last() == '\\') { // entry continues on next line
|
|
|
|
entry.Truncate(entry.Length()-1);
|
|
|
|
entry.Append(PRUnichar(' ')); // in case there is no trailing whitespace on this line
|
|
|
|
} else { // we have a full entry in entry. Check it for the type
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("Current entry: '%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(entry).get()));
|
2002-06-18 03:20:46 +04:00
|
|
|
|
2001-08-16 02:17:32 +04:00
|
|
|
nsAString::const_iterator semicolon_iter,
|
|
|
|
start_iter, end_iter,
|
|
|
|
majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd;
|
|
|
|
entry.BeginReading(start_iter);
|
|
|
|
entry.EndReading(end_iter);
|
|
|
|
semicolon_iter = start_iter;
|
|
|
|
FindSemicolon(semicolon_iter, end_iter);
|
|
|
|
if (semicolon_iter != end_iter) { // we have something resembling a valid entry
|
|
|
|
rv = ParseMIMEType(start_iter, majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd, semicolon_iter);
|
|
|
|
if (NS_SUCCEEDED(rv) &&
|
2002-02-10 18:49:46 +03:00
|
|
|
Substring(majorTypeStart,
|
|
|
|
majorTypeEnd).Equals(aMajorType,
|
|
|
|
nsCaseInsensitiveStringComparator()) &&
|
2002-06-18 03:20:46 +04:00
|
|
|
Substring(minorTypeStart,
|
|
|
|
minorTypeEnd).Equals(aMinorType,
|
|
|
|
nsCaseInsensitiveStringComparator())) { // we have a match
|
2011-09-29 10:19:26 +04:00
|
|
|
bool match = true;
|
2001-08-16 02:17:32 +04:00
|
|
|
++semicolon_iter; // point at the first char past the semicolon
|
|
|
|
start_iter = semicolon_iter; // handler string starts here
|
|
|
|
FindSemicolon(semicolon_iter, end_iter);
|
|
|
|
while (start_iter != semicolon_iter &&
|
|
|
|
nsCRT::IsAsciiSpace(*start_iter)) {
|
|
|
|
++start_iter;
|
|
|
|
}
|
|
|
|
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("The real handler is: '%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(Substring(start_iter,
|
2002-06-18 03:20:46 +04:00
|
|
|
semicolon_iter)).get()));
|
2001-08-16 02:17:32 +04:00
|
|
|
|
|
|
|
// XXX ugly hack. Just grab the executable name
|
|
|
|
nsAString::const_iterator end_handler_iter = semicolon_iter;
|
|
|
|
nsAString::const_iterator end_executable_iter = start_iter;
|
|
|
|
while (end_executable_iter != end_handler_iter &&
|
|
|
|
!nsCRT::IsAsciiSpace(*end_executable_iter)) {
|
|
|
|
++end_executable_iter;
|
|
|
|
}
|
|
|
|
// XXX End ugly hack
|
|
|
|
|
|
|
|
aHandler = Substring(start_iter, end_executable_iter);
|
|
|
|
|
|
|
|
nsAString::const_iterator start_option_iter, end_optionname_iter, equal_sign_iter;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool equalSignFound;
|
2001-08-16 02:17:32 +04:00
|
|
|
while (match &&
|
|
|
|
semicolon_iter != end_iter &&
|
|
|
|
++semicolon_iter != end_iter) { // there are options left and we still match
|
|
|
|
start_option_iter = semicolon_iter;
|
|
|
|
// skip over leading whitespace
|
|
|
|
while (start_option_iter != end_iter &&
|
|
|
|
nsCRT::IsAsciiSpace(*start_option_iter)) {
|
|
|
|
++start_option_iter;
|
|
|
|
}
|
|
|
|
if (start_option_iter == end_iter) { // nothing actually here
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
semicolon_iter = start_option_iter;
|
|
|
|
FindSemicolon(semicolon_iter, end_iter);
|
|
|
|
equal_sign_iter = start_option_iter;
|
2011-10-17 18:59:28 +04:00
|
|
|
equalSignFound = false;
|
2001-08-16 02:17:32 +04:00
|
|
|
while (equal_sign_iter != semicolon_iter && !equalSignFound) {
|
|
|
|
switch(*equal_sign_iter) {
|
|
|
|
case '\\':
|
|
|
|
equal_sign_iter.advance(2);
|
|
|
|
break;
|
|
|
|
case '=':
|
2011-10-17 18:59:28 +04:00
|
|
|
equalSignFound = true;
|
2001-08-16 02:17:32 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
++equal_sign_iter;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end_optionname_iter = start_option_iter;
|
|
|
|
// find end of option name
|
|
|
|
while (end_optionname_iter != equal_sign_iter &&
|
|
|
|
!nsCRT::IsAsciiSpace(*end_optionname_iter)) {
|
|
|
|
++end_optionname_iter;
|
|
|
|
}
|
|
|
|
nsDependentSubstring optionName(start_option_iter, end_optionname_iter);
|
|
|
|
if (equalSignFound) {
|
|
|
|
// This is an option that has a name and value
|
2004-05-23 02:15:22 +04:00
|
|
|
if (optionName.EqualsLiteral("description")) {
|
2001-08-16 02:17:32 +04:00
|
|
|
aDescription = Substring(++equal_sign_iter, semicolon_iter);
|
2004-05-23 02:15:22 +04:00
|
|
|
} else if (optionName.EqualsLiteral("x-mozilla-flags")) {
|
2001-08-16 02:17:32 +04:00
|
|
|
aMozillaFlags = Substring(++equal_sign_iter, semicolon_iter);
|
2004-05-23 02:15:22 +04:00
|
|
|
} else if (optionName.EqualsLiteral("test")) {
|
2001-08-16 02:17:32 +04:00
|
|
|
nsCAutoString testCommand;
|
|
|
|
rv = UnescapeCommand(Substring(++equal_sign_iter, semicolon_iter),
|
|
|
|
aMajorType,
|
|
|
|
aMinorType,
|
|
|
|
aTypeOptions,
|
|
|
|
testCommand);
|
2008-07-12 12:46:11 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
continue;
|
|
|
|
nsCOMPtr<nsIProcess> process = do_CreateInstance(NS_PROCESS_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
continue;
|
|
|
|
nsCOMPtr<nsILocalFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
continue;
|
|
|
|
rv = file->InitWithNativePath(NS_LITERAL_CSTRING("/bin/sh"));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
continue;
|
|
|
|
rv = process->Init(file);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
continue;
|
|
|
|
const char *args[] = { "-c", testCommand.get() };
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("Running Test: %s\n", testCommand.get()));
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = process->Run(true, args, 2);
|
2008-07-12 12:46:11 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
continue;
|
|
|
|
PRInt32 exitValue;
|
|
|
|
rv = process->GetExitValue(&exitValue);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
continue;
|
|
|
|
LOG(("Exit code: %d\n", exitValue));
|
|
|
|
if (exitValue) {
|
2011-10-17 18:59:28 +04:00
|
|
|
match = false;
|
2001-08-16 02:17:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// This is an option that just has a name but no value (eg "copiousoutput")
|
2008-07-12 12:45:22 +04:00
|
|
|
if (optionName.EqualsLiteral("needsterminal")) {
|
2011-10-17 18:59:28 +04:00
|
|
|
match = false;
|
2008-07-12 12:45:22 +04:00
|
|
|
}
|
2001-08-16 02:17:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (match) { // we did not fail any test clauses; all is good
|
|
|
|
// get out of here
|
|
|
|
mailcapFile->Close();
|
|
|
|
return NS_OK;
|
|
|
|
} else { // pretend that this match never happened
|
|
|
|
aDescription.Truncate();
|
|
|
|
aMozillaFlags.Truncate();
|
|
|
|
aHandler.Truncate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// zero out the entry for the next cycle
|
|
|
|
entry.Truncate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!more) {
|
2002-06-18 03:20:46 +04:00
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
2001-08-16 02:17:32 +04:00
|
|
|
break;
|
|
|
|
}
|
2004-04-22 19:32:54 +04:00
|
|
|
rv = mailcap->ReadLine(cBuffer, &more);
|
2001-08-16 02:17:32 +04:00
|
|
|
} while (NS_SUCCEEDED(rv));
|
|
|
|
mailcapFile->Close();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
nsresult nsOSHelperAppService::OSProtocolHandlerExists(const char * aProtocolScheme, bool * aHandlerExists)
|
2000-06-20 22:22:47 +04:00
|
|
|
{
|
2007-05-25 19:17:44 +04:00
|
|
|
LOG(("-- nsOSHelperAppService::OSProtocolHandlerExists for '%s'\n",
|
2002-06-18 03:20:46 +04:00
|
|
|
aProtocolScheme));
|
2011-10-17 18:59:28 +04:00
|
|
|
*aHandlerExists = false;
|
2003-09-17 16:47:56 +04:00
|
|
|
|
2011-02-03 09:31:36 +03:00
|
|
|
#if defined(MOZ_ENABLE_CONTENTACTION)
|
2010-07-30 23:46:20 +04:00
|
|
|
// libcontentaction requires character ':' after scheme
|
|
|
|
ContentAction::Action action =
|
|
|
|
ContentAction::Action::defaultActionForScheme(QString(aProtocolScheme) + ':');
|
|
|
|
|
|
|
|
if (action.isValid())
|
2011-10-17 18:59:28 +04:00
|
|
|
*aHandlerExists = true;
|
2010-07-30 23:46:20 +04:00
|
|
|
#endif
|
|
|
|
|
2003-09-30 02:04:05 +04:00
|
|
|
#ifdef MOZ_WIDGET_GTK2
|
|
|
|
// Check the GConf registry for a protocol handler
|
2009-08-19 23:29:42 +04:00
|
|
|
*aHandlerExists = nsGNOMERegistry::HandlerExists(aProtocolScheme);
|
2010-02-19 01:12:33 +03:00
|
|
|
#if (MOZ_PLATFORM_MAEMO == 5) && defined (MOZ_ENABLE_GNOMEVFS)
|
2009-08-19 23:29:42 +04:00
|
|
|
*aHandlerExists = nsMIMEInfoUnix::HandlerExists(aProtocolScheme);
|
2009-02-18 16:41:37 +03:00
|
|
|
#endif
|
2003-09-30 02:04:05 +04:00
|
|
|
#endif
|
|
|
|
|
2000-06-20 22:22:47 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2004-10-16 17:46:17 +04:00
|
|
|
NS_IMETHODIMP nsOSHelperAppService::GetApplicationDescription(const nsACString& aScheme, nsAString& _retval)
|
|
|
|
{
|
|
|
|
#ifdef MOZ_WIDGET_GTK2
|
|
|
|
nsGNOMERegistry::GetAppDescForScheme(aScheme, _retval);
|
|
|
|
return _retval.IsEmpty() ? NS_ERROR_NOT_AVAILABLE : NS_OK;
|
|
|
|
#else
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2000-06-21 12:34:58 +04:00
|
|
|
nsresult nsOSHelperAppService::GetFileTokenForPath(const PRUnichar * platformAppPath, nsIFile ** aFile)
|
|
|
|
{
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("-- nsOSHelperAppService::GetFileTokenForPath: '%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(platformAppPath).get()));
|
2001-09-18 07:53:25 +04:00
|
|
|
if (! *platformAppPath) { // empty filename--return error
|
|
|
|
NS_WARNING("Empty filename passed in.");
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2004-03-08 23:15:09 +03:00
|
|
|
|
|
|
|
// first check if the base class implementation finds anything
|
|
|
|
nsresult rv = nsExternalHelperAppService::GetFileTokenForPath(platformAppPath, aFile);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
return rv;
|
2004-03-31 23:32:26 +04:00
|
|
|
// If the reason for failure was that the file doesn't exist, return too
|
|
|
|
// (because it means the path was absolute, and so that we shouldn't search in
|
|
|
|
// the path)
|
|
|
|
if (rv == NS_ERROR_FILE_NOT_FOUND)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
// If we get here, we really should have a relative path.
|
|
|
|
NS_ASSERTION(*platformAppPath != PRUnichar('/'), "Unexpected absolute path");
|
2004-03-08 23:15:09 +03:00
|
|
|
|
2000-09-14 03:57:52 +04:00
|
|
|
nsCOMPtr<nsILocalFile> localFile (do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
|
2000-06-21 12:34:58 +04:00
|
|
|
|
2001-08-16 02:17:32 +04:00
|
|
|
if (!localFile) return NS_ERROR_NOT_INITIALIZED;
|
2004-03-08 23:15:09 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool exists = false;
|
2004-03-08 23:15:09 +03:00
|
|
|
// ugly hack. Walk the PATH variable...
|
|
|
|
char* unixpath = PR_GetEnv("PATH");
|
|
|
|
nsCAutoString path(unixpath);
|
2001-08-16 02:17:32 +04:00
|
|
|
|
2004-03-08 23:15:09 +03:00
|
|
|
const char* start_iter = path.BeginReading(start_iter);
|
|
|
|
const char* colon_iter = start_iter;
|
|
|
|
const char* end_iter = path.EndReading(end_iter);
|
2001-08-16 02:17:32 +04:00
|
|
|
|
2004-03-08 23:15:09 +03:00
|
|
|
while (start_iter != end_iter && !exists) {
|
|
|
|
while (colon_iter != end_iter && *colon_iter != ':') {
|
|
|
|
++colon_iter;
|
|
|
|
}
|
|
|
|
localFile->InitWithNativePath(Substring(start_iter, colon_iter));
|
|
|
|
rv = localFile->AppendRelativePath(nsDependentString(platformAppPath));
|
2004-03-31 23:32:26 +04:00
|
|
|
// Failing AppendRelativePath is a bad thing - it should basically always
|
|
|
|
// succeed given a relative path. Show a warning if it does fail.
|
|
|
|
// To prevent infinite loops when it does fail, return at this point.
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
localFile->Exists(&exists);
|
|
|
|
if (!exists) {
|
|
|
|
if (colon_iter == end_iter) {
|
|
|
|
break;
|
2001-08-16 02:17:32 +04:00
|
|
|
}
|
2004-03-31 23:32:26 +04:00
|
|
|
++colon_iter;
|
|
|
|
start_iter = colon_iter;
|
2001-08-16 02:17:32 +04:00
|
|
|
}
|
2000-06-21 12:34:58 +04:00
|
|
|
}
|
2001-08-16 02:17:32 +04:00
|
|
|
|
|
|
|
if (exists) {
|
|
|
|
rv = NS_OK;
|
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aFile = localFile;
|
|
|
|
NS_IF_ADDREF(*aFile);
|
2000-06-21 12:34:58 +04:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
2001-08-16 02:17:32 +04:00
|
|
|
|
2004-02-24 01:49:28 +03:00
|
|
|
already_AddRefed<nsMIMEInfoBase>
|
2004-04-16 00:56:37 +04:00
|
|
|
nsOSHelperAppService::GetFromExtension(const nsCString& aFileExt) {
|
|
|
|
// if the extension is empty, return immediately
|
|
|
|
if (aFileExt.IsEmpty())
|
2003-07-25 20:48:00 +04:00
|
|
|
return nsnull;
|
2001-08-16 02:17:32 +04:00
|
|
|
|
2004-04-16 00:56:37 +04:00
|
|
|
LOG(("Here we do an extension lookup for '%s'\n", aFileExt.get()));
|
2001-08-16 02:17:32 +04:00
|
|
|
|
2004-02-24 01:49:28 +03:00
|
|
|
nsAutoString majorType, minorType,
|
2001-08-16 02:17:32 +04:00
|
|
|
mime_types_description, mailcap_description,
|
|
|
|
handler, mozillaFlags;
|
|
|
|
|
2006-02-03 17:18:39 +03:00
|
|
|
nsresult rv = LookUpTypeAndDescription(NS_ConvertUTF8toUTF16(aFileExt),
|
2003-07-27 20:27:15 +04:00
|
|
|
majorType,
|
|
|
|
minorType,
|
2003-10-10 20:18:16 +04:00
|
|
|
mime_types_description,
|
2011-10-17 18:59:28 +04:00
|
|
|
true);
|
2003-10-10 20:18:16 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv) || majorType.IsEmpty()) {
|
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_GTK2
|
|
|
|
LOG(("Looking in GNOME registry\n"));
|
2008-01-30 07:36:11 +03:00
|
|
|
nsMIMEInfoBase *gnomeInfo = nsGNOMERegistry::GetFromExtension(aFileExt).get();
|
2003-10-10 20:18:16 +04:00
|
|
|
if (gnomeInfo) {
|
|
|
|
LOG(("Got MIMEInfo from GNOME registry\n"));
|
|
|
|
return gnomeInfo;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-02-03 17:18:39 +03:00
|
|
|
rv = LookUpTypeAndDescription(NS_ConvertUTF8toUTF16(aFileExt),
|
2003-10-10 20:18:16 +04:00
|
|
|
majorType,
|
|
|
|
minorType,
|
|
|
|
mime_types_description,
|
2011-10-17 18:59:28 +04:00
|
|
|
false);
|
2003-10-10 20:18:16 +04:00
|
|
|
}
|
|
|
|
|
2001-08-16 02:17:32 +04:00
|
|
|
if (NS_FAILED(rv))
|
2003-07-25 20:48:00 +04:00
|
|
|
return nsnull;
|
2001-08-16 02:17:32 +04:00
|
|
|
|
2004-02-24 01:49:28 +03:00
|
|
|
NS_LossyConvertUTF16toASCII asciiMajorType(majorType);
|
|
|
|
NS_LossyConvertUTF16toASCII asciiMinorType(minorType);
|
|
|
|
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("Type/Description results: majorType='%s', minorType='%s', description='%s'\n",
|
2004-02-24 01:49:28 +03:00
|
|
|
asciiMajorType.get(),
|
|
|
|
asciiMinorType.get(),
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(mime_types_description).get()));
|
2002-06-18 03:20:46 +04:00
|
|
|
|
2001-08-16 02:17:32 +04:00
|
|
|
if (majorType.IsEmpty() && minorType.IsEmpty()) {
|
|
|
|
// we didn't get a type mapping, so we can't do anything useful
|
2003-07-25 20:48:00 +04:00
|
|
|
return nsnull;
|
2001-08-16 02:17:32 +04:00
|
|
|
}
|
|
|
|
|
2004-02-24 01:49:28 +03:00
|
|
|
nsCAutoString mimeType(asciiMajorType + NS_LITERAL_CSTRING("/") + asciiMinorType);
|
2007-07-26 08:38:56 +04:00
|
|
|
nsMIMEInfoUnix* mimeInfo = new nsMIMEInfoUnix(mimeType);
|
2004-01-28 17:48:22 +03:00
|
|
|
if (!mimeInfo)
|
2003-07-25 20:48:00 +04:00
|
|
|
return nsnull;
|
2004-01-28 17:48:22 +03:00
|
|
|
NS_ADDREF(mimeInfo);
|
2001-08-16 02:17:32 +04:00
|
|
|
|
|
|
|
mimeInfo->AppendExtension(aFileExt);
|
|
|
|
nsHashtable typeOptions; // empty hash table
|
|
|
|
rv = LookUpHandlerAndDescription(majorType, minorType, typeOptions,
|
|
|
|
handler, mailcap_description,
|
|
|
|
mozillaFlags);
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("Handler/Description results: handler='%s', description='%s', mozillaFlags='%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(handler).get(),
|
|
|
|
NS_LossyConvertUTF16toASCII(mailcap_description).get(),
|
|
|
|
NS_LossyConvertUTF16toASCII(mozillaFlags).get()));
|
2001-08-16 02:17:32 +04:00
|
|
|
mailcap_description.Trim(" \t\"");
|
|
|
|
mozillaFlags.Trim(" \t");
|
|
|
|
if (!mime_types_description.IsEmpty()) {
|
2004-04-16 00:56:37 +04:00
|
|
|
mimeInfo->SetDescription(mime_types_description);
|
2001-08-16 02:17:32 +04:00
|
|
|
} else {
|
2004-04-16 00:56:37 +04:00
|
|
|
mimeInfo->SetDescription(mailcap_description);
|
2001-08-16 02:17:32 +04:00
|
|
|
}
|
2003-07-27 20:27:15 +04:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) && handler.IsEmpty()) {
|
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2001-08-16 02:17:32 +04:00
|
|
|
nsCOMPtr<nsIFile> handlerFile;
|
|
|
|
rv = GetFileTokenForPath(handler.get(), getter_AddRefs(handlerFile));
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2004-02-24 01:49:28 +03:00
|
|
|
mimeInfo->SetDefaultApplication(handlerFile);
|
2003-01-12 04:14:35 +03:00
|
|
|
mimeInfo->SetPreferredAction(nsIMIMEInfo::useSystemDefault);
|
2004-04-16 00:56:37 +04:00
|
|
|
mimeInfo->SetDefaultDescription(handler);
|
2001-08-16 02:17:32 +04:00
|
|
|
}
|
2003-07-27 20:27:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2001-08-16 02:17:32 +04:00
|
|
|
mimeInfo->SetPreferredAction(nsIMIMEInfo::saveToDisk);
|
|
|
|
}
|
2003-07-25 20:48:00 +04:00
|
|
|
|
|
|
|
return mimeInfo;
|
2001-08-16 02:17:32 +04:00
|
|
|
}
|
|
|
|
|
2004-02-24 01:49:28 +03:00
|
|
|
already_AddRefed<nsMIMEInfoBase>
|
2004-04-16 00:56:37 +04:00
|
|
|
nsOSHelperAppService::GetFromType(const nsCString& aMIMEType) {
|
|
|
|
// if the type is empty, return immediately
|
|
|
|
if (aMIMEType.IsEmpty())
|
2003-07-25 20:48:00 +04:00
|
|
|
return nsnull;
|
2001-08-16 02:17:32 +04:00
|
|
|
|
2004-04-16 00:56:37 +04:00
|
|
|
LOG(("Here we do a mimetype lookup for '%s'\n", aMIMEType.get()));
|
2003-09-30 02:04:05 +04:00
|
|
|
|
2001-08-16 02:17:32 +04:00
|
|
|
// extract the major and minor types
|
2003-07-27 20:27:15 +04:00
|
|
|
NS_ConvertASCIItoUTF16 mimeType(aMIMEType);
|
2001-08-16 02:17:32 +04:00
|
|
|
nsAString::const_iterator start_iter, end_iter,
|
|
|
|
majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd;
|
|
|
|
|
|
|
|
mimeType.BeginReading(start_iter);
|
|
|
|
mimeType.EndReading(end_iter);
|
|
|
|
|
|
|
|
// XXX FIXME: add typeOptions parsing in here
|
2003-07-27 20:27:15 +04:00
|
|
|
nsHashtable typeOptions;
|
|
|
|
nsresult rv = ParseMIMEType(start_iter, majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd, end_iter);
|
2001-08-16 02:17:32 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2003-07-25 20:48:00 +04:00
|
|
|
return nsnull;
|
2001-08-16 02:17:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsDependentSubstring majorType(majorTypeStart, majorTypeEnd);
|
|
|
|
nsDependentSubstring minorType(minorTypeStart, minorTypeEnd);
|
2003-10-10 20:18:16 +04:00
|
|
|
|
2007-04-16 00:43:49 +04:00
|
|
|
// First check the user's private mailcap file
|
2003-10-10 20:18:16 +04:00
|
|
|
nsAutoString mailcap_description, handler, mozillaFlags;
|
|
|
|
DoLookUpHandlerAndDescription(majorType,
|
|
|
|
minorType,
|
|
|
|
typeOptions,
|
|
|
|
handler,
|
|
|
|
mailcap_description,
|
|
|
|
mozillaFlags,
|
2011-10-17 18:59:28 +04:00
|
|
|
true);
|
2007-03-23 02:01:14 +03:00
|
|
|
|
2007-04-16 00:43:49 +04:00
|
|
|
LOG(("Private Handler/Description results: handler='%s', description='%s'\n",
|
|
|
|
NS_LossyConvertUTF16toASCII(handler).get(),
|
|
|
|
NS_LossyConvertUTF16toASCII(mailcap_description).get()));
|
|
|
|
|
2003-10-10 20:18:16 +04:00
|
|
|
#ifdef MOZ_WIDGET_GTK2
|
2007-04-16 00:43:49 +04:00
|
|
|
nsMIMEInfoBase *gnomeInfo = nsnull;
|
|
|
|
if (handler.IsEmpty()) {
|
|
|
|
// No useful data yet. Check the GNOME registry. Unfortunately, newer
|
|
|
|
// GNOME versions no longer have type-to-extension mappings, so we might
|
|
|
|
// get back a MIMEInfo without any extensions set. In that case we'll have
|
|
|
|
// to look in our mime.types files for the extensions.
|
2003-10-10 20:18:16 +04:00
|
|
|
LOG(("Looking in GNOME registry\n"));
|
2008-01-30 07:36:11 +03:00
|
|
|
gnomeInfo = nsGNOMERegistry::GetFromType(aMIMEType).get();
|
2007-04-16 00:43:49 +04:00
|
|
|
if (gnomeInfo && gnomeInfo->HasExtensions()) {
|
|
|
|
LOG(("Got MIMEInfo from GNOME registry, and it has extensions set\n"));
|
2003-10-10 20:18:16 +04:00
|
|
|
return gnomeInfo;
|
|
|
|
}
|
2007-04-16 00:43:49 +04:00
|
|
|
}
|
2003-10-10 20:18:16 +04:00
|
|
|
#endif
|
2007-04-16 00:43:49 +04:00
|
|
|
|
|
|
|
// Now look up our extensions
|
|
|
|
nsAutoString extensions, mime_types_description;
|
|
|
|
LookUpExtensionsAndDescription(majorType,
|
|
|
|
minorType,
|
|
|
|
extensions,
|
|
|
|
mime_types_description);
|
|
|
|
|
|
|
|
#ifdef MOZ_WIDGET_GTK2
|
|
|
|
if (gnomeInfo) {
|
|
|
|
LOG(("Got MIMEInfo from GNOME registry without extensions; setting them "
|
|
|
|
"to %s\n", NS_LossyConvertUTF16toASCII(extensions).get()));
|
|
|
|
|
|
|
|
NS_ASSERTION(!gnomeInfo->HasExtensions(), "How'd that happen?");
|
|
|
|
gnomeInfo->SetFileExtensions(NS_ConvertUTF16toUTF8(extensions));
|
|
|
|
return gnomeInfo;
|
2003-10-10 20:18:16 +04:00
|
|
|
}
|
2007-04-16 00:43:49 +04:00
|
|
|
#endif
|
2003-10-10 20:18:16 +04:00
|
|
|
|
2007-04-16 00:43:49 +04:00
|
|
|
if (handler.IsEmpty()) {
|
2003-10-10 20:18:16 +04:00
|
|
|
DoLookUpHandlerAndDescription(majorType,
|
|
|
|
minorType,
|
|
|
|
typeOptions,
|
|
|
|
handler,
|
|
|
|
mailcap_description,
|
|
|
|
mozillaFlags,
|
2011-10-17 18:59:28 +04:00
|
|
|
false);
|
2003-10-10 20:18:16 +04:00
|
|
|
}
|
|
|
|
|
2007-04-16 00:43:49 +04:00
|
|
|
if (handler.IsEmpty()) {
|
2003-10-10 20:18:16 +04:00
|
|
|
DoLookUpHandlerAndDescription(majorType,
|
|
|
|
NS_LITERAL_STRING("*"),
|
|
|
|
typeOptions,
|
|
|
|
handler,
|
|
|
|
mailcap_description,
|
|
|
|
mozillaFlags,
|
2011-10-17 18:59:28 +04:00
|
|
|
true);
|
2003-10-10 20:18:16 +04:00
|
|
|
}
|
|
|
|
|
2007-04-16 00:43:49 +04:00
|
|
|
if (handler.IsEmpty()) {
|
2003-10-10 20:18:16 +04:00
|
|
|
DoLookUpHandlerAndDescription(majorType,
|
|
|
|
NS_LITERAL_STRING("*"),
|
|
|
|
typeOptions,
|
|
|
|
handler,
|
|
|
|
mailcap_description,
|
|
|
|
mozillaFlags,
|
2011-10-17 18:59:28 +04:00
|
|
|
false);
|
2003-10-10 20:18:16 +04:00
|
|
|
}
|
|
|
|
|
2002-06-18 03:20:46 +04:00
|
|
|
LOG(("Handler/Description results: handler='%s', description='%s', mozillaFlags='%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(handler).get(),
|
|
|
|
NS_LossyConvertUTF16toASCII(mailcap_description).get(),
|
|
|
|
NS_LossyConvertUTF16toASCII(mozillaFlags).get()));
|
2001-11-28 00:24:32 +03:00
|
|
|
|
|
|
|
mailcap_description.Trim(" \t\"");
|
|
|
|
mozillaFlags.Trim(" \t");
|
2003-07-27 20:27:15 +04:00
|
|
|
|
|
|
|
if (handler.IsEmpty() && extensions.IsEmpty() &&
|
|
|
|
mailcap_description.IsEmpty() && mime_types_description.IsEmpty()) {
|
|
|
|
// No real useful info
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
2007-07-26 08:38:56 +04:00
|
|
|
nsMIMEInfoUnix* mimeInfo = new nsMIMEInfoUnix(aMIMEType);
|
2004-01-28 17:48:22 +03:00
|
|
|
if (!mimeInfo)
|
2003-07-25 20:48:00 +04:00
|
|
|
return nsnull;
|
2004-01-28 17:48:22 +03:00
|
|
|
NS_ADDREF(mimeInfo);
|
2003-07-25 20:48:00 +04:00
|
|
|
|
2006-02-03 17:18:39 +03:00
|
|
|
mimeInfo->SetFileExtensions(NS_ConvertUTF16toUTF8(extensions));
|
2001-08-16 02:17:32 +04:00
|
|
|
if (! mime_types_description.IsEmpty()) {
|
2004-04-16 00:56:37 +04:00
|
|
|
mimeInfo->SetDescription(mime_types_description);
|
2001-08-16 02:17:32 +04:00
|
|
|
} else {
|
2004-04-16 00:56:37 +04:00
|
|
|
mimeInfo->SetDescription(mailcap_description);
|
2001-08-16 02:17:32 +04:00
|
|
|
}
|
2003-07-25 20:48:00 +04:00
|
|
|
|
2003-07-27 20:27:15 +04:00
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
2001-08-16 02:17:32 +04:00
|
|
|
nsCOMPtr<nsIFile> handlerFile;
|
2003-07-27 20:27:15 +04:00
|
|
|
if (!handler.IsEmpty()) {
|
|
|
|
rv = GetFileTokenForPath(handler.get(), getter_AddRefs(handlerFile));
|
|
|
|
}
|
2001-11-28 00:24:32 +03:00
|
|
|
|
2001-08-16 02:17:32 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2004-02-24 01:49:28 +03:00
|
|
|
mimeInfo->SetDefaultApplication(handlerFile);
|
2003-01-12 04:14:35 +03:00
|
|
|
mimeInfo->SetPreferredAction(nsIMIMEInfo::useSystemDefault);
|
2004-04-16 00:56:37 +04:00
|
|
|
mimeInfo->SetDefaultDescription(handler);
|
2001-08-16 02:17:32 +04:00
|
|
|
} else {
|
|
|
|
mimeInfo->SetPreferredAction(nsIMIMEInfo::saveToDisk);
|
|
|
|
}
|
2003-07-25 20:48:00 +04:00
|
|
|
|
|
|
|
return mimeInfo;
|
2001-08-16 02:17:32 +04:00
|
|
|
}
|
2003-07-25 20:48:00 +04:00
|
|
|
|
|
|
|
|
|
|
|
already_AddRefed<nsIMIMEInfo>
|
2004-04-16 00:56:37 +04:00
|
|
|
nsOSHelperAppService::GetMIMEInfoFromOS(const nsACString& aType,
|
|
|
|
const nsACString& aFileExt,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool *aFound) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*aFound = true;
|
2004-04-16 00:56:37 +04:00
|
|
|
nsMIMEInfoBase* retval = GetFromType(PromiseFlatCString(aType)).get();
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasDefault = false;
|
2003-07-25 20:48:00 +04:00
|
|
|
if (retval)
|
|
|
|
retval->GetHasDefaultHandler(&hasDefault);
|
|
|
|
if (!retval || !hasDefault) {
|
2004-04-16 00:56:37 +04:00
|
|
|
nsRefPtr<nsMIMEInfoBase> miByExt = GetFromExtension(PromiseFlatCString(aFileExt));
|
2003-11-15 18:01:39 +03:00
|
|
|
// If we had no extension match, but a type match, use that
|
|
|
|
if (!miByExt && retval)
|
2003-07-25 20:48:00 +04:00
|
|
|
return retval;
|
2003-11-15 18:01:39 +03:00
|
|
|
// If we had an extension match but no type match, set the mimetype and use
|
|
|
|
// it
|
|
|
|
if (!retval && miByExt) {
|
2004-04-16 00:56:37 +04:00
|
|
|
if (!aType.IsEmpty())
|
2003-07-25 20:48:00 +04:00
|
|
|
miByExt->SetMIMEType(aType);
|
|
|
|
miByExt.swap(retval);
|
2003-11-15 18:01:39 +03:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
// If we got nothing, make a new mimeinfo
|
|
|
|
if (!retval) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*aFound = false;
|
2007-07-26 08:38:56 +04:00
|
|
|
retval = new nsMIMEInfoUnix(aType);
|
2003-11-15 18:01:39 +03:00
|
|
|
if (retval) {
|
2004-01-28 17:48:22 +03:00
|
|
|
NS_ADDREF(retval);
|
2004-04-16 00:56:37 +04:00
|
|
|
if (!aFileExt.IsEmpty())
|
2003-11-15 18:01:39 +03:00
|
|
|
retval->AppendExtension(aFileExt);
|
|
|
|
}
|
|
|
|
|
2003-07-25 20:48:00 +04:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2009-10-17 11:53:12 +04:00
|
|
|
// Copy the attributes of retval (mimeinfo from type) onto miByExt, to
|
|
|
|
// return it
|
|
|
|
// but reset to just collected mDefaultAppDescription (from ext)
|
|
|
|
nsAutoString byExtDefault;
|
|
|
|
miByExt->GetDefaultDescription(byExtDefault);
|
|
|
|
retval->SetDefaultDescription(byExtDefault);
|
2004-02-24 01:49:28 +03:00
|
|
|
retval->CopyBasicDataTo(miByExt);
|
2004-04-16 00:56:37 +04:00
|
|
|
|
2004-02-24 01:49:28 +03:00
|
|
|
miByExt.swap(retval);
|
2003-07-25 20:48:00 +04:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2008-02-20 07:46:35 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOSHelperAppService::GetProtocolHandlerInfoFromOS(const nsACString &aScheme,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool *found,
|
2008-02-20 07:46:35 +03:00
|
|
|
nsIHandlerInfo **_retval)
|
2007-07-22 22:40:24 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!aScheme.IsEmpty(), "No scheme was specified!");
|
|
|
|
|
2007-07-27 22:31:13 +04:00
|
|
|
// We must check that a registered handler exists so that gnome_url_show
|
|
|
|
// doesn't fallback to gnomevfs.
|
|
|
|
// See nsGNOMERegistry::LoadURL and bug 389632.
|
2007-07-22 22:40:24 +04:00
|
|
|
nsresult rv = OSProtocolHandlerExists(nsPromiseFlatCString(aScheme).get(),
|
2007-07-31 03:33:16 +04:00
|
|
|
found);
|
|
|
|
if (NS_FAILED(rv))
|
2008-02-20 07:46:35 +03:00
|
|
|
return rv;
|
2007-07-22 22:40:24 +04:00
|
|
|
|
2007-07-26 08:38:56 +04:00
|
|
|
nsMIMEInfoUnix *handlerInfo =
|
|
|
|
new nsMIMEInfoUnix(aScheme, nsMIMEInfoBase::eProtocolInfo);
|
2008-02-20 09:10:33 +03:00
|
|
|
NS_ENSURE_TRUE(handlerInfo, NS_ERROR_OUT_OF_MEMORY);
|
2008-02-20 07:46:35 +03:00
|
|
|
NS_ADDREF(*_retval = handlerInfo);
|
2007-07-22 22:40:24 +04:00
|
|
|
|
2007-07-31 03:33:16 +04:00
|
|
|
if (!*found) {
|
|
|
|
// Code that calls this requires an object regardless if the OS has
|
|
|
|
// something for us, so we return the empty object.
|
2008-02-20 07:46:35 +03:00
|
|
|
return NS_OK;
|
2007-07-31 03:33:16 +04:00
|
|
|
}
|
|
|
|
|
2007-07-22 22:40:24 +04:00
|
|
|
nsAutoString desc;
|
|
|
|
GetApplicationDescription(aScheme, desc);
|
|
|
|
handlerInfo->SetDefaultDescription(desc);
|
|
|
|
|
2008-02-20 07:46:35 +03:00
|
|
|
return NS_OK;
|
2007-07-22 22:40:24 +04:00
|
|
|
}
|
|
|
|
|
2004-03-06 22:37:22 +03:00
|
|
|
void
|
|
|
|
nsOSHelperAppService::FixFilePermissions(nsILocalFile* aFile)
|
|
|
|
{
|
|
|
|
aFile->SetPermissions(mPermissions);
|
|
|
|
}
|
|
|
|
|