1999-09-21 04:08:25 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 15:12:37 +04: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 04:08:25 +04:00
|
|
|
|
1999-12-02 06:39:41 +03: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*
|
2014-01-04 19:02:17 +04:00
|
|
|
// - anything else is a char16_t*
|
1999-12-02 06:39:41 +03:00
|
|
|
//
|
|
|
|
|
|
|
|
|
1999-09-21 04:08:25 +04:00
|
|
|
#include "nsPrimitiveHelpers.h"
|
|
|
|
#include "nsCOMPtr.h"
|
2002-09-04 03:36:13 +04:00
|
|
|
#include "nsXPCOM.h"
|
1999-09-21 04:08:25 +04:00
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsITransferable.h"
|
|
|
|
#include "nsIComponentManager.h"
|
1999-12-02 06:39:41 +03:00
|
|
|
#include "nsLinebreakConverter.h"
|
2002-08-27 01:20:34 +04:00
|
|
|
#include "nsReadableUtils.h"
|
1999-09-21 04:08:25 +04:00
|
|
|
|
2000-01-29 23:24:50 +03:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
// unicode conversion
|
2014-05-08 13:32:00 +04:00
|
|
|
#include "nsIPlatformCharset.h"
|
|
|
|
#include "nsIUnicodeDecoder.h"
|
2000-01-29 23:24:50 +03:00
|
|
|
#include "nsISaveAsCharset.h"
|
2008-08-19 22:28:24 +04:00
|
|
|
#include "nsAutoPtr.h"
|
2012-10-26 17:32:10 +04:00
|
|
|
#include "mozilla/Likely.h"
|
2014-05-08 13:32:00 +04:00
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
|
|
|
|
|
|
|
using mozilla::dom::EncodingUtils;
|
2000-01-29 23:24:50 +03:00
|
|
|
|
1999-09-21 04:08:25 +04: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 04:53:19 +04:00
|
|
|
// creates a two-byte |nsISupportsString|, except for "text/plain" and native
|
2002-05-07 23:45:58 +04:00
|
|
|
// platform HTML (CF_HTML on win32)
|
1999-09-21 04:08:25 +04:00
|
|
|
//
|
|
|
|
void
|
2013-12-17 15:02:08 +04:00
|
|
|
nsPrimitiveHelpers :: CreatePrimitiveForData ( const char* aFlavor, const void* aDataBuff,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aDataLen, nsISupports** aPrimitive )
|
1999-09-21 04:08:25 +04:00
|
|
|
{
|
|
|
|
if ( !aPrimitive )
|
|
|
|
return;
|
|
|
|
|
2014-04-24 22:48:14 +04:00
|
|
|
if ( strcmp(aFlavor,kTextMime) == 0 || strcmp(aFlavor,kNativeHTMLMime) == 0 ) {
|
2004-11-01 21:50:36 +03:00
|
|
|
nsCOMPtr<nsISupportsCString> primitive =
|
|
|
|
do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID);
|
1999-09-21 04:08:25 +04:00
|
|
|
if ( primitive ) {
|
2008-08-19 22:28:24 +04:00
|
|
|
const char * start = reinterpret_cast<const char*>(aDataBuff);
|
2002-08-27 01:20:34 +04:00
|
|
|
primitive->SetData(Substring(start, start + aDataLen));
|
2004-11-01 21:50:36 +03:00
|
|
|
NS_ADDREF(*aPrimitive = primitive);
|
1999-09-21 04:08:25 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2004-11-01 21:50:36 +03:00
|
|
|
nsCOMPtr<nsISupportsString> primitive =
|
|
|
|
do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID);
|
|
|
|
if (primitive ) {
|
2015-04-22 07:21:00 +03:00
|
|
|
if (aDataLen % 2) {
|
2008-08-19 22:28:24 +04:00
|
|
|
nsAutoArrayPtr<char> buffer(new char[aDataLen + 1]);
|
2012-10-26 17:32:10 +04:00
|
|
|
if (!MOZ_LIKELY(buffer))
|
2008-08-19 22:28:24 +04:00
|
|
|
return;
|
2015-04-22 07:21:00 +03:00
|
|
|
|
2008-08-19 22:28:24 +04:00
|
|
|
memcpy(buffer, aDataBuff, aDataLen);
|
|
|
|
buffer[aDataLen] = 0;
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* start = reinterpret_cast<const char16_t*>(buffer.get());
|
2008-08-19 22:28:24 +04:00
|
|
|
// recall that length takes length as characters, not bytes
|
|
|
|
primitive->SetData(Substring(start, start + (aDataLen + 1) / 2));
|
|
|
|
} else {
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* start = reinterpret_cast<const char16_t*>(aDataBuff);
|
2008-08-19 22:28:24 +04:00
|
|
|
// recall that length takes length as characters, not bytes
|
|
|
|
primitive->SetData(Substring(start, start + (aDataLen / 2)));
|
|
|
|
}
|
2004-11-01 21:50:36 +03:00
|
|
|
NS_ADDREF(*aPrimitive = primitive);
|
2015-04-22 07:21:00 +03:00
|
|
|
}
|
1999-09-21 04:08:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // CreatePrimitiveForData
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// CreateDataFromPrimitive
|
|
|
|
//
|
|
|
|
// Given a nsISupports* primitive and the flavor it represents, creates a new data
|
1999-12-01 01:07:29 +03:00
|
|
|
// buffer with the data in it. This data will be null terminated, but the length
|
|
|
|
// parameter does not reflect that.
|
1999-09-21 04:08:25 +04:00
|
|
|
//
|
|
|
|
void
|
2015-04-22 07:21:00 +03:00
|
|
|
nsPrimitiveHelpers :: CreateDataFromPrimitive ( const char* aFlavor, nsISupports* aPrimitive,
|
2012-08-22 19:56:38 +04:00
|
|
|
void** aDataBuff, uint32_t aDataLen )
|
1999-09-21 04:08:25 +04:00
|
|
|
{
|
|
|
|
if ( !aDataBuff )
|
|
|
|
return;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
*aDataBuff = nullptr;
|
2008-08-27 16:07:27 +04:00
|
|
|
|
1999-09-21 04:08:25 +04:00
|
|
|
if ( strcmp(aFlavor,kTextMime) == 0 ) {
|
2002-08-06 04:53:19 +04:00
|
|
|
nsCOMPtr<nsISupportsCString> plainText ( do_QueryInterface(aPrimitive) );
|
2002-08-27 01:20:34 +04:00
|
|
|
if ( plainText ) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString data;
|
2002-08-27 01:20:34 +04:00
|
|
|
plainText->GetData ( data );
|
|
|
|
*aDataBuff = ToNewCString(data);
|
|
|
|
}
|
1999-09-21 04:08:25 +04:00
|
|
|
}
|
|
|
|
else {
|
2002-08-06 04:53:19 +04:00
|
|
|
nsCOMPtr<nsISupportsString> doubleByteText ( do_QueryInterface(aPrimitive) );
|
2002-08-27 01:20:34 +04:00
|
|
|
if ( doubleByteText ) {
|
|
|
|
nsAutoString data;
|
|
|
|
doubleByteText->GetData ( data );
|
2002-08-27 22:15:38 +04:00
|
|
|
*aDataBuff = ToNewUnicode(data);
|
2002-08-27 01:20:34 +04:00
|
|
|
}
|
1999-09-21 04:08:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
1999-12-02 06:39:41 +03:00
|
|
|
|
|
|
|
|
2000-01-29 23:24:50 +03: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
|
2015-04-22 07:21:00 +03:00
|
|
|
// string, not the # of bytes in the buffer. The |outPlainTextData| is null terminated,
|
2000-01-29 23:24:50 +03:00
|
|
|
// but its length parameter, |outPlainTextLen|, does not reflect that.
|
|
|
|
//
|
2002-07-16 03:01:17 +04:00
|
|
|
nsresult
|
2015-04-22 07:21:00 +03:00
|
|
|
nsPrimitiveHelpers :: ConvertUnicodeToPlatformPlainText ( char16_t* inUnicode, int32_t inUnicodeLen,
|
2012-08-22 19:56:38 +04:00
|
|
|
char** outPlainTextData, int32_t* outPlainTextLen )
|
2000-01-29 23:24:50 +03:00
|
|
|
{
|
|
|
|
if ( !outPlainTextData || !outPlainTextLen )
|
2002-07-16 03:01:17 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2000-01-29 23:24:50 +03:00
|
|
|
|
2006-05-07 13:06:58 +04:00
|
|
|
// get the charset
|
2000-01-29 23:24:50 +03:00
|
|
|
nsresult rv;
|
2006-05-07 13:06:58 +04:00
|
|
|
nsCOMPtr <nsIPlatformCharset> platformCharsetService = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &rv);
|
2000-08-18 01:37:09 +04:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString platformCharset;
|
2000-08-18 01:37:09 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = platformCharsetService->GetCharset(kPlatformCharsetSel_PlainTextInClipboard, platformCharset);
|
|
|
|
if (NS_FAILED(rv))
|
2004-06-17 04:13:25 +04:00
|
|
|
platformCharset.AssignLiteral("ISO-8859-1");
|
2000-08-18 01:37:09 +04:00
|
|
|
|
|
|
|
// use transliterate to convert things like smart quotes to normal quotes for plain text
|
|
|
|
|
2004-06-22 13:40:13 +04:00
|
|
|
nsCOMPtr<nsISaveAsCharset> converter = do_CreateInstance("@mozilla.org/intl/saveascharset;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2003-06-11 22:16:03 +04:00
|
|
|
rv = converter->Init(platformCharset.get(),
|
2001-06-05 04:02:29 +04:00
|
|
|
nsISaveAsCharset::attr_EntityAfterCharsetConv +
|
2002-09-24 01:35:20 +04:00
|
|
|
nsISaveAsCharset::attr_FallbackQuestionMark,
|
2015-04-22 07:21:00 +03:00
|
|
|
0);
|
2002-07-16 03:01:17 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2000-08-18 01:37:09 +04:00
|
|
|
|
2002-07-16 03:01:17 +04:00
|
|
|
rv = converter->Convert(inUnicode, outPlainTextData);
|
2001-08-23 02:21:43 +04:00
|
|
|
*outPlainTextLen = *outPlainTextData ? strlen(*outPlainTextData) : 0;
|
2000-01-29 23:24:50 +03:00
|
|
|
|
|
|
|
NS_ASSERTION ( NS_SUCCEEDED(rv), "Error converting unicode to plain text" );
|
2015-04-22 07:21:00 +03:00
|
|
|
|
2002-07-16 03:01:17 +04:00
|
|
|
return rv;
|
2000-01-29 23:24:50 +03:00
|
|
|
} // ConvertUnicodeToPlatformPlainText
|
|
|
|
|
|
|
|
|
2000-02-02 01:26:21 +03:00
|
|
|
//
|
|
|
|
// ConvertPlatformPlainTextToUnicode
|
|
|
|
//
|
|
|
|
// Given a char buffer (flavor text/plaikn), this converts it to unicode using
|
2015-04-22 07:21:00 +03:00
|
|
|
// the appropriate platform charset encoding. |outUnicode| is null terminated,
|
2000-02-02 01:26:21 +03:00
|
|
|
// but its length parameter, |outUnicodeLen|, does not reflect that. |outUnicodeLen| is
|
|
|
|
// the length of the string in characters, not bytes.
|
|
|
|
//
|
2002-07-16 03:01:17 +04:00
|
|
|
nsresult
|
2015-04-22 07:21:00 +03:00
|
|
|
nsPrimitiveHelpers :: ConvertPlatformPlainTextToUnicode ( const char* inText, int32_t inTextLen,
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t** outUnicode, int32_t* outUnicodeLen )
|
2000-02-02 01:26:21 +03:00
|
|
|
{
|
|
|
|
if ( !outUnicode || !outUnicodeLen )
|
2002-07-16 03:01:17 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2000-02-02 01:26:21 +03: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 13:06:58 +04:00
|
|
|
nsresult rv = NS_OK;
|
2000-02-02 01:26:21 +03:00
|
|
|
static nsCOMPtr<nsIUnicodeDecoder> decoder;
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool hasConverter = false;
|
2000-02-02 01:26:21 +03:00
|
|
|
if ( !hasConverter ) {
|
|
|
|
// get the charset
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString platformCharset;
|
2000-09-14 03:57:52 +04:00
|
|
|
nsCOMPtr <nsIPlatformCharset> platformCharsetService = do_GetService(NS_PLATFORMCHARSET_CONTRACTID, &rv);
|
2000-03-15 04:35:02 +03:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = platformCharsetService->GetCharset(kPlatformCharsetSel_PlainTextInClipboard, platformCharset);
|
|
|
|
if (NS_FAILED(rv))
|
2014-05-08 13:32:00 +04:00
|
|
|
platformCharset.AssignLiteral("windows-1252");
|
2015-04-22 07:21:00 +03:00
|
|
|
|
2014-05-08 13:32:00 +04:00
|
|
|
decoder = EncodingUtils::DecoderForEncoding(platformCharset);
|
2003-07-26 14:15:24 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
hasConverter = true;
|
2000-02-02 01:26:21 +03:00
|
|
|
}
|
2015-04-22 07:21:00 +03:00
|
|
|
|
2000-02-02 01:26:21 +03:00
|
|
|
// Estimate out length and allocate the buffer based on a worst-case estimate, then do
|
2015-04-22 07:21:00 +03:00
|
|
|
// the conversion.
|
2015-06-17 14:21:39 +03:00
|
|
|
rv = decoder->GetMaxLength(inText, inTextLen, outUnicodeLen); // |outUnicodeLen| is number of chars
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2000-02-02 01:26:21 +03:00
|
|
|
if ( *outUnicodeLen ) {
|
2015-03-27 03:01:12 +03:00
|
|
|
*outUnicode = reinterpret_cast<char16_t*>(moz_xmalloc((*outUnicodeLen + 1) * sizeof(char16_t)));
|
2000-02-02 01:26:21 +03: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-16 03:01:17 +04:00
|
|
|
return rv;
|
2000-02-02 01:26:21 +03:00
|
|
|
} // ConvertPlatformPlainTextToUnicode
|
|
|
|
|
|
|
|
|
1999-12-02 06:39:41 +03: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).
|
|
|
|
//
|
2015-05-01 18:01:06 +03:00
|
|
|
// NOTE: this assumes that it can use 'free' to dispose of the old buffer.
|
1999-12-02 06:39:41 +03:00
|
|
|
//
|
|
|
|
nsresult
|
2015-04-22 07:21:00 +03:00
|
|
|
nsLinebreakHelpers :: ConvertPlatformToDOMLinebreaks ( const char* inFlavor, void** ioData,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t* ioLengthInBytes )
|
1999-12-02 06:39:41 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION ( ioData && *ioData && ioLengthInBytes, "Bad Params");
|
|
|
|
if ( !(ioData && *ioData && ioLengthInBytes) )
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
2015-04-22 07:21:00 +03:00
|
|
|
|
1999-12-02 06:39:41 +03:00
|
|
|
nsresult retVal = NS_OK;
|
2015-04-22 07:21:00 +03:00
|
|
|
|
1999-12-02 06:39:41 +03:00
|
|
|
if ( strcmp(inFlavor, "text/plain") == 0 ) {
|
2007-07-08 11:08:04 +04:00
|
|
|
char* buffAsChars = reinterpret_cast<char*>(*ioData);
|
1999-12-02 06:39:41 +03:00
|
|
|
char* oldBuffer = buffAsChars;
|
2015-04-22 07:21:00 +03:00
|
|
|
retVal = nsLinebreakConverter::ConvertLineBreaksInSitu ( &buffAsChars, nsLinebreakConverter::eLinebreakAny,
|
|
|
|
nsLinebreakConverter::eLinebreakContent,
|
1999-12-02 06:39:41 +03:00
|
|
|
*ioLengthInBytes, ioLengthInBytes );
|
|
|
|
if ( NS_SUCCEEDED(retVal) ) {
|
|
|
|
if ( buffAsChars != oldBuffer ) // check if buffer was reallocated
|
2015-03-27 03:01:12 +03:00
|
|
|
free ( oldBuffer );
|
1999-12-02 06:39:41 +03:00
|
|
|
*ioData = buffAsChars;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if ( strcmp(inFlavor, "image/jpeg") == 0 ) {
|
|
|
|
// I'd assume we don't want to do anything for binary data....
|
|
|
|
}
|
2015-04-22 07:21:00 +03:00
|
|
|
else {
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t* buffAsUnichar = reinterpret_cast<char16_t*>(*ioData);
|
|
|
|
char16_t* oldBuffer = buffAsUnichar;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t newLengthInChars;
|
2015-04-22 07:21:00 +03:00
|
|
|
retVal = nsLinebreakConverter::ConvertUnicharLineBreaksInSitu ( &buffAsUnichar, nsLinebreakConverter::eLinebreakAny,
|
|
|
|
nsLinebreakConverter::eLinebreakContent,
|
2014-01-04 19:02:17 +04:00
|
|
|
*ioLengthInBytes / sizeof(char16_t), &newLengthInChars );
|
1999-12-02 06:39:41 +03:00
|
|
|
if ( NS_SUCCEEDED(retVal) ) {
|
|
|
|
if ( buffAsUnichar != oldBuffer ) // check if buffer was reallocated
|
2015-03-27 03:01:12 +03:00
|
|
|
free ( oldBuffer );
|
1999-12-02 06:39:41 +03:00
|
|
|
*ioData = buffAsUnichar;
|
2014-01-04 19:02:17 +04:00
|
|
|
*ioLengthInBytes = newLengthInChars * sizeof(char16_t);
|
1999-12-02 06:39:41 +03:00
|
|
|
}
|
|
|
|
}
|
2015-04-22 07:21:00 +03:00
|
|
|
|
1999-12-02 06:39:41 +03:00
|
|
|
return retVal;
|
|
|
|
|
|
|
|
} // ConvertPlatformToDOMLinebreaks
|