зеркало из https://github.com/mozilla/gecko-dev.git
Bug 795542 - Part 1: Create Text(En|De)coderBase. r=bent
--HG-- rename : dom/encoding/TextDecoder.h => dom/encoding/TextDecoderBase.h rename : dom/encoding/TextEncoder.h => dom/encoding/TextEncoderBase.h
This commit is contained in:
Родитель
1db359a008
Коммит
e9c46e4563
|
@ -24,7 +24,9 @@ EXPORTS_NAMESPACES = mozilla/dom
|
|||
EXPORTS_mozilla/dom = \
|
||||
EncodingUtils.h \
|
||||
TextDecoder.h \
|
||||
TextDecoderBase.h \
|
||||
TextEncoder.h \
|
||||
TextEncoderBase.h \
|
||||
$(NULL)
|
||||
|
||||
CPPSRCS = \
|
||||
|
|
|
@ -14,9 +14,8 @@ namespace dom {
|
|||
static const PRUnichar kReplacementChar = static_cast<PRUnichar>(0xFFFD);
|
||||
|
||||
void
|
||||
TextDecoder::Init(const nsAString& aEncoding,
|
||||
const TextDecoderOptions& aFatal,
|
||||
ErrorResult& aRv)
|
||||
TextDecoderBase::Init(const nsAString& aEncoding, const bool aFatal,
|
||||
ErrorResult& aRv)
|
||||
{
|
||||
nsAutoString label(aEncoding);
|
||||
EncodingUtils::TrimSpaceCharacters(label);
|
||||
|
@ -31,7 +30,7 @@ TextDecoder::Init(const nsAString& aEncoding,
|
|||
// If the constructor is called with an options argument,
|
||||
// and the fatal property of the dictionary is set,
|
||||
// set the internal fatal flag of the decoder object.
|
||||
mFatal = aFatal.mFatal;
|
||||
mFatal = aFatal;
|
||||
|
||||
// Create a decoder object for mEncoding.
|
||||
nsCOMPtr<nsICharsetConverterManager> ccm =
|
||||
|
@ -53,10 +52,10 @@ TextDecoder::Init(const nsAString& aEncoding,
|
|||
}
|
||||
|
||||
void
|
||||
TextDecoder::Decode(const ArrayBufferView* aView,
|
||||
const TextDecodeOptions& aOptions,
|
||||
nsAString& aOutDecodedString,
|
||||
ErrorResult& aRv)
|
||||
TextDecoderBase::Decode(const ArrayBufferView* aView,
|
||||
const bool aStream,
|
||||
nsAString& aOutDecodedString,
|
||||
ErrorResult& aRv)
|
||||
{
|
||||
const char* data;
|
||||
int32_t length;
|
||||
|
@ -94,7 +93,7 @@ TextDecoder::Decode(const ArrayBufferView* aView,
|
|||
|
||||
// If the internal streaming flag of the decoder object is not set,
|
||||
// then reset the encoding algorithm state to the default values
|
||||
if (!aOptions.mStream) {
|
||||
if (!aStream) {
|
||||
mDecoder->Reset();
|
||||
if (rv == NS_OK_UDEC_MOREINPUT) {
|
||||
if (mFatal) {
|
||||
|
@ -113,7 +112,7 @@ TextDecoder::Decode(const ArrayBufferView* aView,
|
|||
}
|
||||
|
||||
void
|
||||
TextDecoder::GetEncoding(nsAString& aEncoding)
|
||||
TextDecoderBase::GetEncoding(nsAString& aEncoding)
|
||||
{
|
||||
CopyASCIItoUTF16(mEncoding, aEncoding);
|
||||
nsContentUtils::ASCIIToLower(aEncoding);
|
||||
|
|
|
@ -5,21 +5,14 @@
|
|||
#ifndef mozilla_dom_textdecoder_h_
|
||||
#define mozilla_dom_textdecoder_h_
|
||||
|
||||
#include "jsapi.h"
|
||||
#include "mozilla/dom/BindingUtils.h"
|
||||
#include "mozilla/dom/TextDecoderBase.h"
|
||||
#include "mozilla/dom/TextDecoderBinding.h"
|
||||
#include "mozilla/dom/TypedArray.h"
|
||||
#include "mozilla/ErrorResult.h"
|
||||
#include "nsIUnicodeDecoder.h"
|
||||
#include "nsString.h"
|
||||
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsCycleCollectionParticipant.h"
|
||||
|
||||
namespace mozilla {
|
||||
namespace dom {
|
||||
|
||||
class TextDecoder : public nsISupports, public nsWrapperCache
|
||||
class TextDecoder MOZ_FINAL
|
||||
: public nsISupports, public nsWrapperCache, public TextDecoderBase
|
||||
{
|
||||
public:
|
||||
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
||||
|
@ -29,11 +22,11 @@ public:
|
|||
static already_AddRefed<TextDecoder>
|
||||
Constructor(nsISupports* aGlobal,
|
||||
const nsAString& aEncoding,
|
||||
const TextDecoderOptions& aFatal,
|
||||
const TextDecoderOptions& aOptions,
|
||||
ErrorResult& aRv)
|
||||
{
|
||||
nsRefPtr<TextDecoder> txtDecoder = new TextDecoder(aGlobal);
|
||||
txtDecoder->Init(aEncoding, aFatal, aRv);
|
||||
txtDecoder->Init(aEncoding, aOptions.mFatal, aRv);
|
||||
if (aRv.Failed()) {
|
||||
return nullptr;
|
||||
}
|
||||
|
@ -41,7 +34,7 @@ public:
|
|||
}
|
||||
|
||||
TextDecoder(nsISupports* aGlobal)
|
||||
: mGlobal(aGlobal), mFatal(false)
|
||||
: mGlobal(aGlobal)
|
||||
{
|
||||
MOZ_ASSERT(aGlobal);
|
||||
SetIsDOMBinding();
|
||||
|
@ -52,7 +45,7 @@ public:
|
|||
{}
|
||||
|
||||
virtual JSObject*
|
||||
WrapObject(JSContext* aCx, JSObject* aScope, bool* aTriedToWrap)
|
||||
WrapObject(JSContext* aCx, JSObject* aScope, bool* aTriedToWrap) MOZ_OVERRIDE
|
||||
{
|
||||
return TextDecoderBinding::Wrap(aCx, aScope, this, aTriedToWrap);
|
||||
}
|
||||
|
@ -63,53 +56,16 @@ public:
|
|||
return mGlobal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the encoding name.
|
||||
*
|
||||
* @param aEncoding, current encoding.
|
||||
*/
|
||||
void GetEncoding(nsAString& aEncoding);
|
||||
|
||||
/**
|
||||
* Decodes incoming byte stream of characters in charset indicated by
|
||||
* encoding.
|
||||
*
|
||||
* The encoding algorithm state is reset if aOptions.stream is not set.
|
||||
*
|
||||
* If the fatal flag is set then a decoding error will throw EncodingError.
|
||||
* Else the decoder will return a decoded string with replacement
|
||||
* character(s) for unidentified character(s).
|
||||
*
|
||||
* @param aView, incoming byte stream of characters to be decoded to
|
||||
* to UTF-16 code points.
|
||||
* @param aOptions, indicates if streaming or not.
|
||||
* @param aOutDecodedString, decoded string of UTF-16 code points.
|
||||
* @param aRv, error result.
|
||||
*/
|
||||
void Decode(const ArrayBufferView* aView,
|
||||
const TextDecodeOptions& aOptions,
|
||||
nsAString& aOutDecodedString,
|
||||
ErrorResult& aRv);
|
||||
ErrorResult& aRv) {
|
||||
return TextDecoderBase::Decode(aView, aOptions.mStream,
|
||||
aOutDecodedString, aRv);
|
||||
}
|
||||
|
||||
private:
|
||||
nsCString mEncoding;
|
||||
nsCOMPtr<nsIUnicodeDecoder> mDecoder;
|
||||
nsCOMPtr<nsISupports> mGlobal;
|
||||
bool mFatal;
|
||||
|
||||
/**
|
||||
* Validates provided encoding and throws an exception if invalid encoding.
|
||||
* If no encoding is provided then mEncoding is default initialised to "utf-8".
|
||||
*
|
||||
* @param aEncoding Optional encoding (case insensitive) provided.
|
||||
* Default value is "utf-8" if no encoding is provided.
|
||||
* @param aFatal aFatal, indicates whether to throw an 'EncodingError'
|
||||
* exception or not.
|
||||
* @return aRv EncodingError exception else null.
|
||||
*/
|
||||
void Init(const nsAString& aEncoding,
|
||||
const TextDecoderOptions& aFatal,
|
||||
ErrorResult& aRv);
|
||||
};
|
||||
|
||||
} // dom
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
/* 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/. */
|
||||
|
||||
#ifndef mozilla_dom_textdecoderbase_h_
|
||||
#define mozilla_dom_textdecoderbase_h_
|
||||
|
||||
#include "mozilla/dom/BindingUtils.h"
|
||||
#include "mozilla/dom/TypedArray.h"
|
||||
#include "nsIUnicodeDecoder.h"
|
||||
|
||||
namespace mozilla {
|
||||
class ErrorResult;
|
||||
|
||||
namespace dom {
|
||||
|
||||
class TextDecoderBase
|
||||
{
|
||||
protected:
|
||||
TextDecoderBase()
|
||||
: mFatal(false)
|
||||
{}
|
||||
|
||||
virtual
|
||||
~TextDecoderBase()
|
||||
{}
|
||||
|
||||
/**
|
||||
* Validates provided encoding and throws an exception if invalid encoding.
|
||||
* If no encoding is provided then mEncoding is default initialised to "utf-8".
|
||||
*
|
||||
* @param aEncoding Optional encoding (case insensitive) provided.
|
||||
* Default value is "utf-8" if no encoding is provided.
|
||||
* @param aFatal aFatal, indicates whether to throw an 'EncodingError'
|
||||
* exception or not.
|
||||
* @return aRv EncodingError exception else null.
|
||||
*/
|
||||
void Init(const nsAString& aEncoding, const bool aFatal, ErrorResult& aRv);
|
||||
|
||||
public:
|
||||
/**
|
||||
* Return the encoding name.
|
||||
*
|
||||
* @param aEncoding, current encoding.
|
||||
*/
|
||||
void GetEncoding(nsAString& aEncoding);
|
||||
|
||||
/**
|
||||
* Decodes incoming byte stream of characters in charset indicated by
|
||||
* encoding.
|
||||
*
|
||||
* The encoding algorithm state is reset if aOptions.mStream is not set.
|
||||
*
|
||||
* If the fatal flag is set then a decoding error will throw EncodingError.
|
||||
* Else the decoder will return a decoded string with replacement
|
||||
* character(s) for unidentified character(s).
|
||||
*
|
||||
* @param aView, incoming byte stream of characters to be decoded to
|
||||
* to UTF-16 code points.
|
||||
* @param aOptions, indicates if streaming or not.
|
||||
* @param aOutDecodedString, decoded string of UTF-16 code points.
|
||||
* @param aRv, error result.
|
||||
*/
|
||||
void Decode(const ArrayBufferView* aView, const bool aStream,
|
||||
nsAString& aOutDecodedString, ErrorResult& aRv);
|
||||
|
||||
private:
|
||||
nsCString mEncoding;
|
||||
nsCOMPtr<nsIUnicodeDecoder> mDecoder;
|
||||
bool mFatal;
|
||||
};
|
||||
|
||||
} // dom
|
||||
} // mozilla
|
||||
|
||||
#endif // mozilla_dom_textdecoderbase_h_
|
|
@ -12,8 +12,7 @@ namespace mozilla {
|
|||
namespace dom {
|
||||
|
||||
void
|
||||
TextEncoder::Init(const nsAString& aEncoding,
|
||||
ErrorResult& aRv)
|
||||
TextEncoderBase::Init(const nsAString& aEncoding, ErrorResult& aRv)
|
||||
{
|
||||
nsAutoString label(aEncoding);
|
||||
EncodingUtils::TrimSpaceCharacters(label);
|
||||
|
@ -49,10 +48,10 @@ TextEncoder::Init(const nsAString& aEncoding,
|
|||
}
|
||||
|
||||
JSObject*
|
||||
TextEncoder::Encode(JSContext* aCx,
|
||||
const nsAString& aString,
|
||||
const TextEncodeOptions& aOptions,
|
||||
ErrorResult& aRv)
|
||||
TextEncoderBase::Encode(JSContext* aCx,
|
||||
const nsAString& aString,
|
||||
const bool aStream,
|
||||
ErrorResult& aRv)
|
||||
{
|
||||
// Run the steps of the encoding algorithm.
|
||||
int32_t srcLen = aString.Length();
|
||||
|
@ -77,7 +76,7 @@ TextEncoder::Encode(JSContext* aCx,
|
|||
|
||||
// If the internal streaming flag is not set, then reset
|
||||
// the encoding algorithm state to the default values for encoding.
|
||||
if (!aOptions.mStream) {
|
||||
if (!aStream) {
|
||||
int32_t finishLen = maxLen - dstLen;
|
||||
rv = mEncoder->Finish(buf + dstLen, &finishLen);
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
|
@ -88,8 +87,7 @@ TextEncoder::Encode(JSContext* aCx,
|
|||
JSObject* outView = nullptr;
|
||||
if (NS_SUCCEEDED(rv)) {
|
||||
buf[dstLen] = '\0';
|
||||
outView = Uint8Array::Create(aCx, this, dstLen,
|
||||
reinterpret_cast<uint8_t*>(buf.get()));
|
||||
outView = CreateUint8Array(aCx, buf, dstLen);
|
||||
if (!outView) {
|
||||
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
||||
return nullptr;
|
||||
|
@ -103,7 +101,7 @@ TextEncoder::Encode(JSContext* aCx,
|
|||
}
|
||||
|
||||
void
|
||||
TextEncoder::GetEncoding(nsAString& aEncoding)
|
||||
TextEncoderBase::GetEncoding(nsAString& aEncoding)
|
||||
{
|
||||
CopyASCIItoUTF16(mEncoding, aEncoding);
|
||||
nsContentUtils::ASCIIToLower(aEncoding);
|
||||
|
|
|
@ -5,21 +5,14 @@
|
|||
#ifndef mozilla_dom_textencoder_h_
|
||||
#define mozilla_dom_textencoder_h_
|
||||
|
||||
#include "jsapi.h"
|
||||
#include "mozilla/dom/BindingUtils.h"
|
||||
#include "mozilla/dom/TextEncoderBase.h"
|
||||
#include "mozilla/dom/TextEncoderBinding.h"
|
||||
#include "mozilla/dom/TypedArray.h"
|
||||
#include "mozilla/ErrorResult.h"
|
||||
#include "nsIUnicodeEncoder.h"
|
||||
#include "nsString.h"
|
||||
|
||||
#include "nsCOMPtr.h"
|
||||
#include "nsCycleCollectionParticipant.h"
|
||||
|
||||
namespace mozilla {
|
||||
namespace dom {
|
||||
|
||||
class TextEncoder : public nsISupports, public nsWrapperCache
|
||||
class TextEncoder MOZ_FINAL
|
||||
: public nsISupports, public nsWrapperCache, public TextEncoderBase
|
||||
{
|
||||
public:
|
||||
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
||||
|
@ -51,7 +44,7 @@ public:
|
|||
{}
|
||||
|
||||
virtual JSObject*
|
||||
WrapObject(JSContext* aCx, JSObject* aScope, bool* aTriedToWrap)
|
||||
WrapObject(JSContext* aCx, JSObject* aScope, bool* aTriedToWrap) MOZ_OVERRIDE
|
||||
{
|
||||
return TextEncoderBinding::Wrap(aCx, aScope, this, aTriedToWrap);
|
||||
}
|
||||
|
@ -62,44 +55,23 @@ public:
|
|||
return mGlobal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the encoding name.
|
||||
*
|
||||
* @param aEncoding, current encoding.
|
||||
*/
|
||||
void GetEncoding(nsAString& aEncoding);
|
||||
|
||||
/**
|
||||
* Encodes incoming utf-16 code units/ DOM string to the requested encoding.
|
||||
*
|
||||
* @param aCx Javascript context.
|
||||
* @param aString utf-16 code units to be encoded.
|
||||
* @param aOptions Streaming option. Initialised by default to false.
|
||||
* If the streaming option is false, then the encoding
|
||||
* algorithm state will get reset. If set to true then
|
||||
* the previous encoding is reused/continued.
|
||||
* @return JSObject* The Uint8Array wrapped in a JS object.
|
||||
*/
|
||||
JSObject* Encode(JSContext* aCx,
|
||||
const nsAString& aString,
|
||||
const TextEncodeOptions& aOptions,
|
||||
ErrorResult& aRv);
|
||||
private:
|
||||
nsCString mEncoding;
|
||||
nsCOMPtr<nsIUnicodeEncoder> mEncoder;
|
||||
nsCOMPtr<nsISupports> mGlobal;
|
||||
ErrorResult& aRv) {
|
||||
return TextEncoderBase::Encode(aCx, aString, aOptions.mStream, aRv);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates provided encoding and throws an exception if invalid encoding.
|
||||
* If no encoding is provided then mEncoding is default initialised to "utf-8".
|
||||
*
|
||||
* @param aEncoding Optional encoding (case insensitive) provided.
|
||||
* (valid values are "utf-8", "utf-16", "utf-16be")
|
||||
* Default value is "utf-8" if no encoding is provided.
|
||||
* @return aRv EncodingError exception else null.
|
||||
*/
|
||||
void Init(const nsAString& aEncoding,
|
||||
ErrorResult& aRv);
|
||||
protected:
|
||||
virtual JSObject*
|
||||
CreateUint8Array(JSContext* aCx, char* aBuf, uint32_t aLen) MOZ_OVERRIDE
|
||||
{
|
||||
return Uint8Array::Create(aCx, this, aLen,
|
||||
reinterpret_cast<uint8_t*>(aBuf));
|
||||
}
|
||||
|
||||
private:
|
||||
nsCOMPtr<nsISupports> mGlobal;
|
||||
};
|
||||
|
||||
} // dom
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
/* 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/. */
|
||||
|
||||
#ifndef mozilla_dom_textencoderbase_h_
|
||||
#define mozilla_dom_textencoderbase_h_
|
||||
|
||||
#include "mozilla/dom/BindingUtils.h"
|
||||
#include "mozilla/dom/TypedArray.h"
|
||||
#include "nsIUnicodeEncoder.h"
|
||||
|
||||
namespace mozilla {
|
||||
class ErrorResult;
|
||||
|
||||
namespace dom {
|
||||
|
||||
class TextEncoderBase
|
||||
{
|
||||
protected:
|
||||
TextEncoderBase()
|
||||
{}
|
||||
|
||||
virtual
|
||||
~TextEncoderBase()
|
||||
{}
|
||||
|
||||
/**
|
||||
* Validates provided encoding and throws an exception if invalid encoding.
|
||||
* If no encoding is provided then mEncoding is default initialised to "utf-8".
|
||||
*
|
||||
* @param aEncoding Optional encoding (case insensitive) provided.
|
||||
* (valid values are "utf-8", "utf-16", "utf-16be")
|
||||
* Default value is "utf-8" if no encoding is provided.
|
||||
* @return aRv EncodingError exception else null.
|
||||
*/
|
||||
void Init(const nsAString& aEncoding, ErrorResult& aRv);
|
||||
|
||||
public:
|
||||
/**
|
||||
* Return the encoding name.
|
||||
*
|
||||
* @param aEncoding, current encoding.
|
||||
*/
|
||||
void GetEncoding(nsAString& aEncoding);
|
||||
|
||||
/**
|
||||
* Encodes incoming utf-16 code units/ DOM string to the requested encoding.
|
||||
*
|
||||
* @param aCx Javascript context.
|
||||
* @param aString utf-16 code units to be encoded.
|
||||
* @param aOptions Streaming option. Initialised by default to false.
|
||||
* If the streaming option is false, then the encoding
|
||||
* algorithm state will get reset. If set to true then
|
||||
* the previous encoding is reused/continued.
|
||||
* @return JSObject* The Uint8Array wrapped in a JS object.
|
||||
*/
|
||||
JSObject* Encode(JSContext* aCx, const nsAString& aString,
|
||||
const bool aStream, ErrorResult& aRv);
|
||||
|
||||
protected:
|
||||
virtual JSObject*
|
||||
CreateUint8Array(JSContext* aCx, char* aBuf, uint32_t aLen) = 0;
|
||||
|
||||
private:
|
||||
nsCString mEncoding;
|
||||
nsCOMPtr<nsIUnicodeEncoder> mEncoder;
|
||||
};
|
||||
|
||||
} // dom
|
||||
} // mozilla
|
||||
|
||||
#endif // mozilla_dom_textencoderbase_h_
|
Загрузка…
Ссылка в новой задаче