1999-09-21 00:08:25 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 11:12:37 +00:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
1999-09-21 00:08:25 +00:00
|
|
|
|
1999-12-02 03:39:41 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Part of the reason these routines are all in once place is so that as new
|
|
|
|
// data flavors are added that are known to be one-byte or two-byte strings, or even
|
|
|
|
// raw binary data, then we just have to go to one place to change how the data
|
|
|
|
// moves into/out of the primitives and native line endings.
|
|
|
|
//
|
|
|
|
// If you add new flavors that have special consideration (binary data or one-byte
|
|
|
|
// char* strings), please update all the helper classes in this file.
|
|
|
|
//
|
|
|
|
// For now, this is the assumption that we are making:
|
|
|
|
// - text/plain is always a char*
|
|
|
|
// - anything else is a PRUnichar*
|
|
|
|
//
|
|
|
|
|
|
|
|
|
1999-09-21 00:08:25 +00:00
|
|
|
#include "nsPrimitiveHelpers.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2002-09-03 23:36:13 +00:00
|
|
|
#include "nsXPCOM.h"
|
1999-09-21 00:08:25 +00:00
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsITransferable.h"
|
|
|
|
#include "nsIComponentManager.h"
|
1999-12-02 03:39:41 +00:00
|
|
|
#include "nsLinebreakConverter.h"
|
2002-08-26 21:20:34 +00:00
|
|
|
#include "nsReadableUtils.h"
|
1999-09-21 00:08:25 +00:00
|
|
|
|
2000-01-29 20:24:50 +00:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsICharsetConverterManager.h"
|
|
|
|
// unicode conversion
|
|
|
|
# include "nsIPlatformCharset.h"
|
|
|
|
#include "nsISaveAsCharset.h"
|
2008-08-19 18:28:24 +00:00
|
|
|
#include "nsAutoPtr.h"
|
2012-10-26 13:32:10 +00:00
|
|
|
#include "mozilla/Likely.h"
|
2000-01-29 20:24:50 +00:00
|
|
|
|
1999-09-21 00:08:25 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// CreatePrimitiveForData
|
|
|
|
//
|
|
|
|
// Given some data and the flavor it corresponds to, creates the appropriate
|
|
|
|
// nsISupports* wrapper for passing across IDL boundaries. Right now, everything
|
2002-08-06 00:53:19 +00:00
|
|
|
// creates a two-byte |nsISupportsString|, except for "text/plain" and native
|
2002-05-07 19:45:58 +00:00
|
|
|
// platform HTML (CF_HTML on win32)
|
1999-09-21 00:08:25 +00:00
|
|
|
//
|
|
|
|
void
|
|
|
|
nsPrimitiveHelpers :: CreatePrimitiveForData ( const char* aFlavor, void* aDataBuff,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aDataLen, nsISupports** aPrimitive )
|
1999-09-21 00:08:25 +00:00
|
|
|
{
|
|
|
|
if ( !aPrimitive )
|
|
|
|
return;
|
|
|
|
|
2002-05-07 19:45:58 +00:00
|
|
|
if ( strcmp(aFlavor,kTextMime) == 0 || strcmp(aFlavor,kNativeHTMLMime) == 0 ) {
|
2004-11-01 18:50:36 +00:00
|
|
|
nsCOMPtr<nsISupportsCString> primitive =
|
|
|
|
do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID);
|
1999-09-21 00:08:25 +00:00
|
|
|
if ( primitive ) {
|
2008-08-19 18:28:24 +00:00
|
|
|
const char * start = reinterpret_cast<const char*>(aDataBuff);
|
2002-08-26 21:20:34 +00:00
|
|
|
primitive->SetData(Substring(start, start + aDataLen));
|
2004-11-01 18:50:36 +00:00
|
|
|
NS_ADDREF(*aPrimitive = primitive);
|
1999-09-21 00:08:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2004-11-01 18:50:36 +00:00
|
|
|
nsCOMPtr<nsISupportsString> primitive =
|
|
|
|
do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID);
|
|
|
|
if (primitive ) {
|
2008-08-19 18:28:24 +00:00
|
|
|
if (aDataLen % 2) {
|
|
|
|
nsAutoArrayPtr<char> buffer(new char[aDataLen + 1]);
|
2012-10-26 13:32:10 +00:00
|
|
|
if (!MOZ_LIKELY(buffer))
|
2008-08-19 18:28:24 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
memcpy(buffer, aDataBuff, aDataLen);
|
|
|
|
buffer[aDataLen] = 0;
|
|
|
|
const PRUnichar* start = reinterpret_cast<const PRUnichar*>(buffer.get());
|
|
|
|
// recall that length takes length as characters, not bytes
|
|
|
|
primitive->SetData(Substring(start, start + (aDataLen + 1) / 2));
|
|
|
|
} else {
|
|
|
|
const PRUnichar* start = reinterpret_cast<const PRUnichar*>(aDataBuff);
|
|
|
|
// recall that length takes length as characters, not bytes
|
|
|
|
primitive->SetData(Substring(start, start + (aDataLen / 2)));
|
|
|
|
}
|
2004-11-01 18:50:36 +00:00
|
|
|
NS_ADDREF(*aPrimitive = primitive);
|
1999-09-21 00:08:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // CreatePrimitiveForData
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// CreateDataFromPrimitive
|
|
|
|
//
|
|
|
|
// Given a nsISupports* primitive and the flavor it represents, creates a new data
|
1999-11-30 22:07:29 +00:00
|
|
|
// buffer with the data in it. This data will be null terminated, but the length
|
|
|
|
// parameter does not reflect that.
|
1999-09-21 00:08:25 +00:00
|
|
|
//
|
|
|
|
void
|
|
|
|
nsPrimitiveHelpers :: CreateDataFromPrimitive ( const char* aFlavor, nsISupports* aPrimitive,
|
2012-08-22 15:56:38 +00:00
|
|
|
void** aDataBuff, uint32_t aDataLen )
|
1999-09-21 00:08:25 +00:00
|
|
|
{
|
|
|
|
if ( !aDataBuff )
|
|
|
|
return;
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
*aDataBuff = nullptr;
|
2008-08-27 12:07:27 +00:00
|
|
|
|
1999-09-21 00:08:25 +00:00
|
|
|
if ( strcmp(aFlavor,kTextMime) == 0 ) {
|
2002-08-06 00:53:19 +00:00
|
|
|
nsCOMPtr<nsISupportsCString> plainText ( do_QueryInterface(aPrimitive) );
|
2002-08-26 21:20:34 +00:00
|
|
|
if ( plainText ) {
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString data;
|
2002-08-26 21:20:34 +00:00
|
|
|
plainText->GetData ( data );
|
|
|
|
*aDataBuff = ToNewCString(data);
|
|
|
|
}
|
1999-09-21 00:08:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2002-08-06 00:53:19 +00:00
|
|
|
nsCOMPtr<nsISupportsString> doubleByteText ( do_QueryInterface(aPrimitive) );
|
2002-08-26 21:20:34 +00:00
|
|
|
if ( doubleByteText ) {
|
|
|
|
nsAutoString data;
|
|
|
|
doubleByteText->GetData ( data );
|
2002-08-27 18:15:38 +00:00
|
|
|
*aDataBuff = ToNewUnicode(data);
|
2002-08-26 21:20:34 +00:00
|
|
|
}
|
1999-09-21 00:08:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
1999-12-02 03:39:41 +00:00
|
|
|
|
|
|
|
|
2000-01-29 20:24:50 +00:00
|
|
|
//
|
|
|
|
// ConvertUnicodeToPlatformPlainText
|
|
|
|
//
|
|
|
|
// Given a unicode buffer (flavor text/unicode), this converts it to plain text using
|
|
|
|
// the appropriate platform charset encoding. |inUnicodeLen| is the length of the input
|
|
|
|
// string, not the # of bytes in the buffer. The |outPlainTextData| is null terminated,
|
|
|
|
// but its length parameter, |outPlainTextLen|, does not reflect that.
|
|
|
|
//
|
2002-07-15 23:01:17 +00:00
|
|
|
nsresult
|
2012-08-22 15:56:38 +00:00
|
|
|
nsPrimitiveHelpers :: ConvertUnicodeToPlatformPlainText ( PRUnichar* inUnicode, int32_t inUnicodeLen,
|
|
|
|
char** outPlainTextData, int32_t* outPlainTextLen )
|
2000-01-29 20:24:50 +00:00
|
|
|
{
|
|
|
|
if ( !outPlainTextData || !outPlainTextLen )
|
2002-07-15 23:01:17 +00:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2000-01-29 20:24:50 +00:00
|
|
|
|
2006-05-07 09:06:58 +00:00
|
|
|
// get the charset
|
2000-01-29 20:24:50 +00:00
|
|
|
nsresult rv;
|
2006-05-07 09:06:58 +00:00
|
|
|
nsCOMPtr <nsIPlatformCharset> platformCharsetService = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &rv);
|
2000-08-17 21:37:09 +00:00
|
|
|
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString platformCharset;
|
2000-08-17 21:37:09 +00:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = platformCharsetService->GetCharset(kPlatformCharsetSel_PlainTextInClipboard, platformCharset);
|
|
|
|
if (NS_FAILED(rv))
|
2004-06-17 00:13:25 +00:00
|
|
|
platformCharset.AssignLiteral("ISO-8859-1");
|
2000-08-17 21:37:09 +00:00
|
|
|
|
|
|
|
// use transliterate to convert things like smart quotes to normal quotes for plain text
|
|
|
|
|
2004-06-22 09:40:13 +00:00
|
|
|
nsCOMPtr<nsISaveAsCharset> converter = do_CreateInstance("@mozilla.org/intl/saveascharset;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2003-06-11 18:16:03 +00:00
|
|
|
rv = converter->Init(platformCharset.get(),
|
2001-06-05 00:02:29 +00:00
|
|
|
nsISaveAsCharset::attr_EntityAfterCharsetConv +
|
2002-09-23 21:35:20 +00:00
|
|
|
nsISaveAsCharset::attr_FallbackQuestionMark,
|
2000-08-17 21:37:09 +00:00
|
|
|
nsIEntityConverter::transliterate);
|
2002-07-15 23:01:17 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2000-08-17 21:37:09 +00:00
|
|
|
|
2002-07-15 23:01:17 +00:00
|
|
|
rv = converter->Convert(inUnicode, outPlainTextData);
|
2001-08-22 22:21:43 +00:00
|
|
|
*outPlainTextLen = *outPlainTextData ? strlen(*outPlainTextData) : 0;
|
2000-01-29 20:24:50 +00:00
|
|
|
|
|
|
|
NS_ASSERTION ( NS_SUCCEEDED(rv), "Error converting unicode to plain text" );
|
|
|
|
|
2002-07-15 23:01:17 +00:00
|
|
|
return rv;
|
2000-01-29 20:24:50 +00:00
|
|
|
} // ConvertUnicodeToPlatformPlainText
|
|
|
|
|
|
|
|
|
2000-02-01 22:26:21 +00:00
|
|
|
//
|
|
|
|
// ConvertPlatformPlainTextToUnicode
|
|
|
|
//
|
|
|
|
// Given a char buffer (flavor text/plaikn), this converts it to unicode using
|
|
|
|
// the appropriate platform charset encoding. |outUnicode| is null terminated,
|
|
|
|
// but its length parameter, |outUnicodeLen|, does not reflect that. |outUnicodeLen| is
|
|
|
|
// the length of the string in characters, not bytes.
|
|
|
|
//
|
2002-07-15 23:01:17 +00:00
|
|
|
nsresult
|
2012-08-22 15:56:38 +00:00
|
|
|
nsPrimitiveHelpers :: ConvertPlatformPlainTextToUnicode ( const char* inText, int32_t inTextLen,
|
|
|
|
PRUnichar** outUnicode, int32_t* outUnicodeLen )
|
2000-02-01 22:26:21 +00:00
|
|
|
{
|
|
|
|
if ( !outUnicode || !outUnicodeLen )
|
2002-07-15 23:01:17 +00:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2000-02-01 22:26:21 +00:00
|
|
|
|
|
|
|
// Get the appropriate unicode decoder. We're guaranteed that this won't change
|
|
|
|
// through the life of the app so we can cache it.
|
2006-05-07 09:06:58 +00:00
|
|
|
nsresult rv = NS_OK;
|
2000-02-01 22:26:21 +00:00
|
|
|
static nsCOMPtr<nsIUnicodeDecoder> decoder;
|
2011-09-29 06:19:26 +00:00
|
|
|
static bool hasConverter = false;
|
2000-02-01 22:26:21 +00:00
|
|
|
if ( !hasConverter ) {
|
|
|
|
// get the charset
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString platformCharset;
|
2000-09-13 23:57:52 +00:00
|
|
|
nsCOMPtr <nsIPlatformCharset> platformCharsetService = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &rv);
|
2000-03-15 01:35:02 +00:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = platformCharsetService->GetCharset(kPlatformCharsetSel_PlainTextInClipboard, platformCharset);
|
|
|
|
if (NS_FAILED(rv))
|
2004-06-17 00:13:25 +00:00
|
|
|
platformCharset.AssignLiteral("ISO-8859-1");
|
2000-02-01 22:26:21 +00:00
|
|
|
|
|
|
|
// get the decoder
|
2001-07-25 07:54:28 +00:00
|
|
|
nsCOMPtr<nsICharsetConverterManager> ccm =
|
|
|
|
do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
|
2003-06-11 18:16:03 +00:00
|
|
|
rv = ccm->GetUnicodeDecoderRaw(platformCharset.get(),
|
|
|
|
getter_AddRefs(decoder));
|
2000-02-01 22:26:21 +00:00
|
|
|
|
2003-07-26 10:15:24 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "GetUnicodeEncoderRaw failed.");
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
hasConverter = true;
|
2000-02-01 22:26:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Estimate out length and allocate the buffer based on a worst-case estimate, then do
|
|
|
|
// the conversion.
|
|
|
|
decoder->GetMaxLength(inText, inTextLen, outUnicodeLen); // |outUnicodeLen| is number of chars
|
|
|
|
if ( *outUnicodeLen ) {
|
2007-07-08 07:08:04 +00:00
|
|
|
*outUnicode = reinterpret_cast<PRUnichar*>(nsMemory::Alloc((*outUnicodeLen + 1) * sizeof(PRUnichar)));
|
2000-02-01 22:26:21 +00:00
|
|
|
if ( *outUnicode ) {
|
|
|
|
rv = decoder->Convert(inText, &inTextLen, *outUnicode, outUnicodeLen);
|
|
|
|
(*outUnicode)[*outUnicodeLen] = '\0'; // null terminate. Convert() doesn't do it for us
|
|
|
|
}
|
|
|
|
} // if valid length
|
|
|
|
|
|
|
|
NS_ASSERTION ( NS_SUCCEEDED(rv), "Error converting plain text to unicode" );
|
|
|
|
|
2002-07-15 23:01:17 +00:00
|
|
|
return rv;
|
2000-02-01 22:26:21 +00:00
|
|
|
} // ConvertPlatformPlainTextToUnicode
|
|
|
|
|
|
|
|
|
1999-12-02 03:39:41 +00:00
|
|
|
//
|
|
|
|
// ConvertPlatformToDOMLinebreaks
|
|
|
|
//
|
|
|
|
// Given some data, convert from the platform linebreaks into the LF expected by the
|
|
|
|
// DOM. This will attempt to convert the data in place, but the buffer may still need to
|
|
|
|
// be reallocated regardless (disposing the old buffer is taken care of internally, see
|
|
|
|
// the note below).
|
|
|
|
//
|
2000-06-03 09:46:12 +00:00
|
|
|
// NOTE: this assumes that it can use nsMemory to dispose of the old buffer.
|
1999-12-02 03:39:41 +00:00
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsLinebreakHelpers :: ConvertPlatformToDOMLinebreaks ( const char* inFlavor, void** ioData,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t* ioLengthInBytes )
|
1999-12-02 03:39:41 +00:00
|
|
|
{
|
|
|
|
NS_ASSERTION ( ioData && *ioData && ioLengthInBytes, "Bad Params");
|
|
|
|
if ( !(ioData && *ioData && ioLengthInBytes) )
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
nsresult retVal = NS_OK;
|
|
|
|
|
|
|
|
if ( strcmp(inFlavor, "text/plain") == 0 ) {
|
2007-07-08 07:08:04 +00:00
|
|
|
char* buffAsChars = reinterpret_cast<char*>(*ioData);
|
1999-12-02 03:39:41 +00:00
|
|
|
char* oldBuffer = buffAsChars;
|
2001-01-11 00:08:28 +00:00
|
|
|
retVal = nsLinebreakConverter::ConvertLineBreaksInSitu ( &buffAsChars, nsLinebreakConverter::eLinebreakAny,
|
1999-12-02 03:39:41 +00:00
|
|
|
nsLinebreakConverter::eLinebreakContent,
|
|
|
|
*ioLengthInBytes, ioLengthInBytes );
|
|
|
|
if ( NS_SUCCEEDED(retVal) ) {
|
|
|
|
if ( buffAsChars != oldBuffer ) // check if buffer was reallocated
|
2000-06-03 09:46:12 +00:00
|
|
|
nsMemory::Free ( oldBuffer );
|
1999-12-02 03:39:41 +00:00
|
|
|
*ioData = buffAsChars;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( strcmp(inFlavor, "image/jpeg") == 0 ) {
|
|
|
|
// I'd assume we don't want to do anything for binary data....
|
|
|
|
}
|
|
|
|
else {
|
2007-07-08 07:08:04 +00:00
|
|
|
PRUnichar* buffAsUnichar = reinterpret_cast<PRUnichar*>(*ioData);
|
1999-12-02 03:39:41 +00:00
|
|
|
PRUnichar* oldBuffer = buffAsUnichar;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t newLengthInChars;
|
2001-01-11 00:08:28 +00:00
|
|
|
retVal = nsLinebreakConverter::ConvertUnicharLineBreaksInSitu ( &buffAsUnichar, nsLinebreakConverter::eLinebreakAny,
|
1999-12-02 03:39:41 +00:00
|
|
|
nsLinebreakConverter::eLinebreakContent,
|
|
|
|
*ioLengthInBytes / sizeof(PRUnichar), &newLengthInChars );
|
|
|
|
if ( NS_SUCCEEDED(retVal) ) {
|
|
|
|
if ( buffAsUnichar != oldBuffer ) // check if buffer was reallocated
|
2000-06-03 09:46:12 +00:00
|
|
|
nsMemory::Free ( oldBuffer );
|
1999-12-02 03:39:41 +00:00
|
|
|
*ioData = buffAsUnichar;
|
|
|
|
*ioLengthInBytes = newLengthInChars * sizeof(PRUnichar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
|
|
|
|
} // ConvertPlatformToDOMLinebreaks
|