2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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/. */
|
2007-12-28 00:34:03 +03:00
|
|
|
|
|
|
|
#include "jsapi.h"
|
2014-01-31 02:58:53 +04:00
|
|
|
#include "js/CharacterEncoding.h"
|
2007-12-28 00:34:03 +03:00
|
|
|
#include "nsJSON.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsIXPCScriptable.h"
|
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsStringStream.h"
|
2013-11-26 11:31:52 +04:00
|
|
|
#include "mozilla/dom/EncodingUtils.h"
|
|
|
|
#include "nsIUnicodeEncoder.h"
|
|
|
|
#include "nsIUnicodeDecoder.h"
|
2007-12-28 00:34:03 +03:00
|
|
|
#include "nsNetUtil.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2007-12-28 00:34:03 +03:00
|
|
|
#include "nsContentUtils.h"
|
2011-08-02 02:14:31 +04:00
|
|
|
#include "nsIScriptError.h"
|
2007-12-28 00:34:03 +03:00
|
|
|
#include "nsCRTGlue.h"
|
2011-01-26 03:09:56 +03:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2017-03-22 13:38:40 +03:00
|
|
|
#include "NullPrincipal.h"
|
2013-08-28 02:10:28 +04:00
|
|
|
#include "mozilla/Maybe.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2007-12-28 00:34:03 +03:00
|
|
|
|
2013-11-26 11:31:52 +04:00
|
|
|
using mozilla::dom::EncodingUtils;
|
|
|
|
|
2010-03-03 04:54:40 +03:00
|
|
|
#define JSON_STREAM_BUFSIZE 4096
|
2008-10-01 10:13:58 +04:00
|
|
|
|
2007-12-28 00:34:03 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN(nsJSON)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIJSON)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIJSON)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsJSON)
|
|
|
|
NS_IMPL_RELEASE(nsJSON)
|
|
|
|
|
|
|
|
nsJSON::nsJSON()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsJSON::~nsJSON()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-08-02 02:14:31 +04:00
|
|
|
enum DeprecationWarning { EncodeWarning, DecodeWarning };
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
WarnDeprecatedMethod(DeprecationWarning warning)
|
|
|
|
{
|
2011-12-15 18:47:03 +04:00
|
|
|
return nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
|
2013-08-21 23:28:26 +04:00
|
|
|
NS_LITERAL_CSTRING("DOM Core"), nullptr,
|
2011-12-15 18:47:03 +04:00
|
|
|
nsContentUtils::eDOM_PROPERTIES,
|
2011-08-02 02:14:31 +04:00
|
|
|
warning == EncodeWarning
|
|
|
|
? "nsIJSONEncodeDeprecatedWarning"
|
2011-12-15 18:47:03 +04:00
|
|
|
: "nsIJSONDecodeDeprecatedWarning");
|
2011-08-02 02:14:31 +04:00
|
|
|
}
|
|
|
|
|
2011-07-22 16:12:34 +04:00
|
|
|
NS_IMETHODIMP
|
2014-01-09 21:39:36 +04:00
|
|
|
nsJSON::Encode(JS::Handle<JS::Value> aValue, JSContext* cx, uint8_t aArgc,
|
2013-04-03 03:05:37 +04:00
|
|
|
nsAString &aJSON)
|
2011-07-22 16:12:34 +04:00
|
|
|
{
|
|
|
|
// This function should only be called from JS.
|
2011-08-02 02:14:31 +04:00
|
|
|
nsresult rv = WarnDeprecatedMethod(EncodeWarning);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2011-07-22 16:12:34 +04:00
|
|
|
|
2011-11-26 14:21:47 +04:00
|
|
|
if (aArgc == 0) {
|
|
|
|
aJSON.SetIsVoid(true);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-07-22 16:12:34 +04:00
|
|
|
nsJSONWriter writer;
|
2011-11-26 14:21:47 +04:00
|
|
|
rv = EncodeInternal(cx, aValue, &writer);
|
2011-07-22 16:12:34 +04:00
|
|
|
|
|
|
|
// FIXME: bug 408838. Get exception types sorted out
|
|
|
|
if (NS_SUCCEEDED(rv) || rv == NS_ERROR_INVALID_ARG) {
|
|
|
|
rv = NS_OK;
|
|
|
|
// if we didn't consume anything, it's not JSON, so return null
|
|
|
|
if (!writer.DidWrite()) {
|
2011-10-17 18:59:28 +04:00
|
|
|
aJSON.SetIsVoid(true);
|
2011-07-22 16:12:34 +04:00
|
|
|
} else {
|
|
|
|
writer.FlushBuffer();
|
|
|
|
aJSON.Append(writer.mOutputString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2007-12-28 00:34:03 +03:00
|
|
|
static const char UTF8BOM[] = "\xEF\xBB\xBF";
|
|
|
|
static const char UTF16LEBOM[] = "\xFF\xFE";
|
|
|
|
static const char UTF16BEBOM[] = "\xFE\xFF";
|
|
|
|
|
|
|
|
static nsresult CheckCharset(const char* aCharset)
|
|
|
|
{
|
|
|
|
// Check that the charset is permissible
|
|
|
|
if (!(strcmp(aCharset, "UTF-8") == 0 ||
|
|
|
|
strcmp(aCharset, "UTF-16LE") == 0 ||
|
2011-03-30 10:35:34 +04:00
|
|
|
strcmp(aCharset, "UTF-16BE") == 0)) {
|
2007-12-28 00:34:03 +03:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJSON::EncodeToStream(nsIOutputStream *aStream,
|
|
|
|
const char* aCharset,
|
2011-11-26 14:21:47 +04:00
|
|
|
const bool aWriteBOM,
|
2014-01-09 21:39:36 +04:00
|
|
|
JS::Handle<JS::Value> val,
|
2011-11-26 14:21:47 +04:00
|
|
|
JSContext* cx,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t aArgc)
|
2007-12-28 00:34:03 +03:00
|
|
|
{
|
|
|
|
// This function should only be called from JS.
|
|
|
|
NS_ENSURE_ARG(aStream);
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
rv = CheckCharset(aCharset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Check to see if we have a buffered stream
|
|
|
|
nsCOMPtr<nsIOutputStream> bufferedStream;
|
|
|
|
// FIXME: bug 408514.
|
|
|
|
// NS_OutputStreamIsBuffered(aStream) asserts on file streams...
|
|
|
|
//if (!NS_OutputStreamIsBuffered(aStream)) {
|
|
|
|
rv = NS_NewBufferedOutputStream(getter_AddRefs(bufferedStream),
|
|
|
|
aStream, 4096);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
// aStream = bufferedStream;
|
|
|
|
//}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t ignored;
|
2007-12-28 00:34:03 +03:00
|
|
|
if (aWriteBOM) {
|
|
|
|
if (strcmp(aCharset, "UTF-8") == 0)
|
|
|
|
rv = aStream->Write(UTF8BOM, 3, &ignored);
|
|
|
|
else if (strcmp(aCharset, "UTF-16LE") == 0)
|
|
|
|
rv = aStream->Write(UTF16LEBOM, 2, &ignored);
|
|
|
|
else if (strcmp(aCharset, "UTF-16BE") == 0)
|
|
|
|
rv = aStream->Write(UTF16BEBOM, 2, &ignored);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2008-08-11 23:06:27 +04:00
|
|
|
nsJSONWriter writer(bufferedStream);
|
|
|
|
rv = writer.SetCharset(aCharset);
|
2007-12-28 00:34:03 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-11-26 14:21:47 +04:00
|
|
|
if (aArgc == 0) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = EncodeInternal(cx, val, &writer);
|
2007-12-28 00:34:03 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = bufferedStream->Flush();
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-08-09 02:53:04 +04:00
|
|
|
static bool
|
2014-07-22 08:43:21 +04:00
|
|
|
WriteCallback(const char16_t *buf, uint32_t len, void *data)
|
2008-10-01 10:13:58 +04:00
|
|
|
{
|
|
|
|
nsJSONWriter *writer = static_cast<nsJSONWriter*>(data);
|
2014-01-04 19:02:17 +04:00
|
|
|
nsresult rv = writer->Write((const char16_t*)buf, (uint32_t)len);
|
2008-10-01 10:13:58 +04:00
|
|
|
if (NS_FAILED(rv))
|
2013-08-07 10:59:54 +04:00
|
|
|
return false;
|
2008-10-01 10:13:58 +04:00
|
|
|
|
2013-08-07 10:59:54 +04:00
|
|
|
return true;
|
2008-10-01 10:13:58 +04:00
|
|
|
}
|
|
|
|
|
2009-09-01 20:45:05 +04:00
|
|
|
NS_IMETHODIMP
|
2012-05-11 19:46:26 +04:00
|
|
|
nsJSON::EncodeFromJSVal(JS::Value *value, JSContext *cx, nsAString &result)
|
2009-09-01 20:45:05 +04:00
|
|
|
{
|
|
|
|
result.Truncate();
|
|
|
|
|
2012-08-22 05:42:53 +04:00
|
|
|
mozilla::Maybe<JSAutoCompartment> ac;
|
2012-05-11 19:46:26 +04:00
|
|
|
if (value->isObject()) {
|
2013-05-30 00:16:04 +04:00
|
|
|
JS::Rooted<JSObject*> obj(cx, &value->toObject());
|
2014-08-14 02:39:41 +04:00
|
|
|
ac.emplace(cx, obj);
|
2010-09-23 04:34:20 +04:00
|
|
|
}
|
|
|
|
|
2009-09-01 20:45:05 +04:00
|
|
|
nsJSONWriter writer;
|
2013-09-17 05:33:40 +04:00
|
|
|
JS::Rooted<JS::Value> vp(cx, *value);
|
2015-05-14 00:07:34 +03:00
|
|
|
if (!JS_Stringify(cx, &vp, nullptr, JS::NullHandleValue, WriteCallback, &writer)) {
|
2009-09-01 20:45:05 +04:00
|
|
|
return NS_ERROR_XPC_BAD_CONVERT_JS;
|
|
|
|
}
|
2013-09-17 05:33:40 +04:00
|
|
|
*value = vp;
|
2009-09-01 20:45:05 +04:00
|
|
|
|
|
|
|
NS_ENSURE_TRUE(writer.DidWrite(), NS_ERROR_UNEXPECTED);
|
|
|
|
writer.FlushBuffer();
|
|
|
|
result.Assign(writer.mOutputString);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-12-28 00:34:03 +03:00
|
|
|
nsresult
|
2013-04-03 03:05:37 +04:00
|
|
|
nsJSON::EncodeInternal(JSContext* cx, const JS::Value& aValue,
|
|
|
|
nsJSONWriter* writer)
|
2007-12-28 00:34:03 +03:00
|
|
|
{
|
2011-07-22 16:12:34 +04:00
|
|
|
// Backward compatibility:
|
|
|
|
// nsIJSON does not allow to serialize anything other than objects
|
2012-05-11 19:46:26 +04:00
|
|
|
if (!aValue.isObject()) {
|
2011-07-22 16:12:34 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2011-11-26 14:21:47 +04:00
|
|
|
}
|
2013-04-27 19:37:05 +04:00
|
|
|
JS::Rooted<JSObject*> obj(cx, &aValue.toObject());
|
2011-07-22 16:12:34 +04:00
|
|
|
|
|
|
|
/* Backward compatibility:
|
|
|
|
* Manually call toJSON if implemented by the object and check that
|
|
|
|
* the result is still an object
|
|
|
|
* Note: It is perfectly fine to not implement toJSON, so it is
|
|
|
|
* perfectly fine for GetMethod to fail
|
|
|
|
*/
|
2013-04-27 19:37:05 +04:00
|
|
|
JS::Rooted<JS::Value> val(cx, aValue);
|
|
|
|
JS::Rooted<JS::Value> toJSON(cx);
|
2013-07-26 13:00:38 +04:00
|
|
|
if (JS_GetProperty(cx, obj, "toJSON", &toJSON) &&
|
2014-09-25 15:13:28 +04:00
|
|
|
toJSON.isObject() && JS::IsCallable(&toJSON.toObject())) {
|
2011-07-22 16:12:34 +04:00
|
|
|
// If toJSON is implemented, it must not throw
|
2014-02-13 11:38:36 +04:00
|
|
|
if (!JS_CallFunctionValue(cx, obj, toJSON, JS::HandleValueArray::empty(), &val)) {
|
2011-07-22 16:12:34 +04:00
|
|
|
if (JS_IsExceptionPending(cx))
|
|
|
|
// passing NS_OK will throw the pending exception
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// No exception, but still failed
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Backward compatibility:
|
|
|
|
// nsIJSON does not allow to serialize anything other than objects
|
2013-04-27 19:37:05 +04:00
|
|
|
if (val.isPrimitive())
|
2011-07-22 16:12:34 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
// GetMethod may have thrown
|
|
|
|
else if (JS_IsExceptionPending(cx))
|
|
|
|
// passing NS_OK will throw the pending exception
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// Backward compatibility:
|
2013-01-25 09:24:57 +04:00
|
|
|
// function shall not pass, just "plain" objects and arrays
|
2011-11-26 14:21:47 +04:00
|
|
|
JSType type = JS_TypeOfValue(cx, val);
|
2013-01-25 09:24:57 +04:00
|
|
|
if (type == JSTYPE_FUNCTION)
|
2011-07-22 16:12:34 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
// We're good now; try to stringify
|
2015-05-14 00:07:34 +03:00
|
|
|
if (!JS_Stringify(cx, &val, nullptr, JS::NullHandleValue, WriteCallback, writer))
|
2007-12-28 00:34:03 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2011-07-22 16:12:34 +04:00
|
|
|
|
2008-10-01 10:13:58 +04:00
|
|
|
return NS_OK;
|
2007-12-28 00:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsJSONWriter::nsJSONWriter() : mStream(nullptr),
|
|
|
|
mBuffer(nullptr),
|
2008-02-07 10:19:26 +03:00
|
|
|
mBufferCount(0),
|
2011-10-17 18:59:28 +04:00
|
|
|
mDidWrite(false),
|
2012-07-30 18:20:58 +04:00
|
|
|
mEncoder(nullptr)
|
2007-12-28 00:34:03 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-04-05 03:10:10 +04:00
|
|
|
nsJSONWriter::nsJSONWriter(nsIOutputStream *aStream) : mStream(aStream),
|
2012-07-30 18:20:58 +04:00
|
|
|
mBuffer(nullptr),
|
2008-02-07 10:19:26 +03:00
|
|
|
mBufferCount(0),
|
2011-10-17 18:59:28 +04:00
|
|
|
mDidWrite(false),
|
2012-07-30 18:20:58 +04:00
|
|
|
mEncoder(nullptr)
|
2007-12-28 00:34:03 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsJSONWriter::~nsJSONWriter()
|
|
|
|
{
|
2008-02-07 10:19:26 +03:00
|
|
|
delete [] mBuffer;
|
2007-12-28 00:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsJSONWriter::SetCharset(const char* aCharset)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (mStream) {
|
2013-11-26 11:31:52 +04:00
|
|
|
mEncoder = EncodingUtils::EncoderForEncoding(aCharset);
|
2007-12-28 00:34:03 +03:00
|
|
|
rv = mEncoder->SetOutputErrorBehavior(nsIUnicodeEncoder::kOnError_Signal,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr, '\0');
|
2007-12-28 00:34:03 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-01-04 19:02:17 +04:00
|
|
|
nsJSONWriter::Write(const char16_t *aBuffer, uint32_t aLength)
|
2007-12-28 00:34:03 +03:00
|
|
|
{
|
|
|
|
if (mStream) {
|
2008-02-07 10:19:26 +03:00
|
|
|
return WriteToStream(mStream, mEncoder, aBuffer, aLength);
|
2007-12-28 00:34:03 +03:00
|
|
|
}
|
|
|
|
|
2008-02-07 10:19:26 +03:00
|
|
|
if (!mDidWrite) {
|
2014-01-04 19:02:17 +04:00
|
|
|
mBuffer = new char16_t[JSON_STREAM_BUFSIZE];
|
2011-10-17 18:59:28 +04:00
|
|
|
mDidWrite = true;
|
2008-02-07 10:19:26 +03:00
|
|
|
}
|
|
|
|
|
2008-10-01 10:13:58 +04:00
|
|
|
if (JSON_STREAM_BUFSIZE <= aLength + mBufferCount) {
|
2008-02-07 10:19:26 +03:00
|
|
|
mOutputString.Append(mBuffer, mBufferCount);
|
|
|
|
mBufferCount = 0;
|
|
|
|
}
|
|
|
|
|
2008-10-01 10:13:58 +04:00
|
|
|
if (JSON_STREAM_BUFSIZE <= aLength) {
|
2008-03-17 21:58:38 +03:00
|
|
|
// we know mBufferCount is 0 because we know we hit the if above
|
|
|
|
mOutputString.Append(aBuffer, aLength);
|
|
|
|
} else {
|
2014-01-04 19:02:17 +04:00
|
|
|
memcpy(&mBuffer[mBufferCount], aBuffer, aLength * sizeof(char16_t));
|
2008-03-17 21:58:38 +03:00
|
|
|
mBufferCount += aLength;
|
|
|
|
}
|
2008-02-07 10:19:26 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsJSONWriter::DidWrite()
|
2008-02-07 10:19:26 +03:00
|
|
|
{
|
|
|
|
return mDidWrite;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsJSONWriter::FlushBuffer()
|
|
|
|
{
|
|
|
|
mOutputString.Append(mBuffer, mBufferCount);
|
2007-12-28 00:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsJSONWriter::WriteToStream(nsIOutputStream *aStream,
|
|
|
|
nsIUnicodeEncoder *encoder,
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *aBuffer,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aLength)
|
2007-12-28 00:34:03 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t srcLength = aLength;
|
|
|
|
uint32_t bytesWritten;
|
2007-12-28 00:34:03 +03:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
// The bytes written to the stream might differ from the char16_t size
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aDestLength;
|
2007-12-28 00:34:03 +03:00
|
|
|
rv = encoder->GetMaxLength(aBuffer, srcLength, &aDestLength);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// create the buffer we need
|
2015-04-01 08:29:55 +03:00
|
|
|
char* destBuf = (char *) moz_xmalloc(aDestLength);
|
2007-12-28 00:34:03 +03:00
|
|
|
if (!destBuf)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
rv = encoder->Convert(aBuffer, &srcLength, destBuf, &aDestLength);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = aStream->Write(destBuf, aDestLength, &bytesWritten);
|
|
|
|
|
2015-04-01 08:29:55 +03:00
|
|
|
free(destBuf);
|
2011-10-17 18:59:28 +04:00
|
|
|
mDidWrite = true;
|
2007-12-28 00:34:03 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-07-22 16:12:34 +04:00
|
|
|
NS_IMETHODIMP
|
2014-01-09 21:39:36 +04:00
|
|
|
nsJSON::Decode(const nsAString& json, JSContext* cx,
|
|
|
|
JS::MutableHandle<JS::Value> aRetval)
|
2011-07-22 16:12:34 +04:00
|
|
|
{
|
2011-08-02 02:14:31 +04:00
|
|
|
nsresult rv = WarnDeprecatedMethod(DecodeWarning);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2017-03-06 07:52:09 +03:00
|
|
|
const char16_t *data = json.BeginReading();
|
|
|
|
uint32_t len = json.Length();
|
2011-07-22 16:12:34 +04:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2011-08-02 02:14:31 +04:00
|
|
|
rv = NS_NewByteInputStream(getter_AddRefs(stream),
|
|
|
|
reinterpret_cast<const char*>(data),
|
2014-01-04 19:02:17 +04:00
|
|
|
len * sizeof(char16_t),
|
2011-08-02 02:14:31 +04:00
|
|
|
NS_ASSIGNMENT_DEPEND);
|
2011-07-22 16:12:34 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-11-26 14:21:29 +04:00
|
|
|
return DecodeInternal(cx, stream, len, false, aRetval);
|
2011-07-22 16:12:34 +04:00
|
|
|
}
|
|
|
|
|
2007-12-28 00:34:03 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsJSON::DecodeFromStream(nsIInputStream *aStream, int32_t aContentLength,
|
2014-01-09 21:39:36 +04:00
|
|
|
JSContext* cx, JS::MutableHandle<JS::Value> aRetval)
|
2007-12-28 00:34:03 +03:00
|
|
|
{
|
2011-11-26 14:21:29 +04:00
|
|
|
return DecodeInternal(cx, aStream, aContentLength, true, aRetval);
|
2007-12-28 00:34:03 +03:00
|
|
|
}
|
|
|
|
|
2009-09-01 20:45:05 +04:00
|
|
|
NS_IMETHODIMP
|
2014-01-09 21:39:36 +04:00
|
|
|
nsJSON::DecodeToJSVal(const nsAString &str, JSContext *cx,
|
|
|
|
JS::MutableHandle<JS::Value> result)
|
2009-09-01 20:45:05 +04:00
|
|
|
{
|
2014-07-22 08:43:21 +04:00
|
|
|
if (!JS_ParseJSON(cx, static_cast<const char16_t*>(PromiseFlatString(str).get()),
|
2014-01-09 21:39:36 +04:00
|
|
|
str.Length(), result)) {
|
2009-09-01 20:45:05 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-12-28 00:34:03 +03:00
|
|
|
nsresult
|
2011-11-26 14:21:29 +04:00
|
|
|
nsJSON::DecodeInternal(JSContext* cx,
|
|
|
|
nsIInputStream *aStream,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aContentLength,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aNeedsConverter,
|
2014-01-09 21:39:36 +04:00
|
|
|
JS::MutableHandle<JS::Value> aRetval)
|
2007-12-28 00:34:03 +03:00
|
|
|
{
|
|
|
|
// Consume the stream
|
|
|
|
nsCOMPtr<nsIChannel> jsonChannel;
|
2008-02-07 10:19:26 +03:00
|
|
|
if (!mURI) {
|
|
|
|
NS_NewURI(getter_AddRefs(mURI), NS_LITERAL_CSTRING("about:blank"), 0, 0 );
|
|
|
|
if (!mURI)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2014-10-16 22:14:52 +04:00
|
|
|
nsresult rv;
|
2017-03-22 13:38:40 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> nullPrincipal = NullPrincipal::Create();
|
2014-10-16 22:14:52 +04:00
|
|
|
|
2016-03-08 22:28:48 +03:00
|
|
|
// The ::Decode function is deprecated [Bug 675797] and the following
|
|
|
|
// channel is never openend, so it does not matter what securityFlags
|
|
|
|
// we pass to NS_NewInputStreamChannel here.
|
2014-10-16 22:14:52 +04:00
|
|
|
rv = NS_NewInputStreamChannel(getter_AddRefs(jsonChannel),
|
|
|
|
mURI,
|
|
|
|
aStream,
|
|
|
|
nullPrincipal,
|
2016-03-08 22:28:48 +03:00
|
|
|
nsILoadInfo::SEC_REQUIRE_SAME_ORIGIN_DATA_IS_BLOCKED,
|
2014-10-16 22:14:52 +04:00
|
|
|
nsIContentPolicy::TYPE_OTHER,
|
|
|
|
NS_LITERAL_CSTRING("application/json"));
|
|
|
|
|
2007-12-28 00:34:03 +03:00
|
|
|
if (!jsonChannel || NS_FAILED(rv))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsJSONListener> jsonListener =
|
2014-01-09 21:39:36 +04:00
|
|
|
new nsJSONListener(cx, aRetval.address(), aNeedsConverter);
|
2007-12-28 00:34:03 +03:00
|
|
|
|
|
|
|
//XXX this stream pattern should be consolidated in netwerk
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = jsonListener->OnStartRequest(jsonChannel, nullptr);
|
2007-12-28 00:34:03 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
jsonChannel->Cancel(rv);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult status;
|
|
|
|
jsonChannel->GetStatus(&status);
|
2012-08-22 19:56:38 +04:00
|
|
|
uint64_t offset = 0;
|
2007-12-28 00:34:03 +03:00
|
|
|
while (NS_SUCCEEDED(status)) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint64_t available;
|
2007-12-28 00:34:03 +03:00
|
|
|
rv = aStream->Available(&available);
|
|
|
|
if (rv == NS_BASE_STREAM_CLOSED) {
|
|
|
|
rv = NS_OK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
jsonChannel->Cancel(rv);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!available)
|
|
|
|
break; // blocking input stream has none available when done
|
|
|
|
|
2012-09-28 10:57:33 +04:00
|
|
|
if (available > UINT32_MAX)
|
|
|
|
available = UINT32_MAX;
|
2012-08-11 06:44:11 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = jsonListener->OnDataAvailable(jsonChannel, nullptr,
|
2012-08-11 06:44:11 +04:00
|
|
|
aStream,
|
2012-09-06 06:41:02 +04:00
|
|
|
offset,
|
2012-08-22 19:56:38 +04:00
|
|
|
(uint32_t)available);
|
2007-12-28 00:34:03 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
jsonChannel->Cancel(rv);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += available;
|
|
|
|
jsonChannel->GetStatus(&status);
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = jsonListener->OnStopRequest(jsonChannel, nullptr, status);
|
2007-12-28 00:34:03 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-10 22:11:11 +04:00
|
|
|
nsresult
|
2007-12-28 00:34:03 +03:00
|
|
|
NS_NewJSON(nsISupports* aOuter, REFNSIID aIID, void** aResult)
|
|
|
|
{
|
|
|
|
nsJSON* json = new nsJSON();
|
|
|
|
NS_ADDREF(json);
|
|
|
|
*aResult = json;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-04-03 03:05:37 +04:00
|
|
|
nsJSONListener::nsJSONListener(JSContext *cx, JS::Value *rootVal,
|
2013-05-24 02:28:31 +04:00
|
|
|
bool needsConverter)
|
2014-02-13 11:38:36 +04:00
|
|
|
: mNeedsConverter(needsConverter),
|
2007-12-28 00:34:03 +03:00
|
|
|
mCx(cx),
|
2013-05-24 02:28:31 +04:00
|
|
|
mRootVal(rootVal)
|
2007-12-28 00:34:03 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsJSONListener::~nsJSONListener()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsJSONListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsJSONListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIRequestObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsJSONListener)
|
|
|
|
NS_IMPL_RELEASE(nsJSONListener)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJSONListener::OnStartRequest(nsIRequest *aRequest, nsISupports *aContext)
|
|
|
|
{
|
|
|
|
mSniffBuffer.Truncate();
|
2012-07-30 18:20:58 +04:00
|
|
|
mDecoder = nullptr;
|
2008-10-01 10:13:58 +04:00
|
|
|
|
2007-12-28 00:34:03 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJSONListener::OnStopRequest(nsIRequest *aRequest, nsISupports *aContext,
|
|
|
|
nsresult aStatusCode)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2011-07-07 22:42:11 +04:00
|
|
|
// This can happen with short UTF-8 messages (<4 bytes)
|
2007-12-28 00:34:03 +03:00
|
|
|
if (!mSniffBuffer.IsEmpty()) {
|
2011-07-07 22:42:11 +04:00
|
|
|
// Just consume mSniffBuffer
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = ProcessBytes(nullptr, 0);
|
2007-12-28 00:34:03 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2013-11-11 12:04:41 +04:00
|
|
|
JS::Rooted<JS::Value> reviver(mCx, JS::NullValue()), value(mCx);
|
2012-07-30 15:19:09 +04:00
|
|
|
|
2014-07-22 08:43:21 +04:00
|
|
|
JS::ConstTwoByteChars chars(reinterpret_cast<const char16_t*>(mBufferedChars.Elements()),
|
2014-01-31 02:58:53 +04:00
|
|
|
mBufferedChars.Length());
|
2016-11-06 04:13:38 +03:00
|
|
|
bool ok = JS_ParseJSONWithReviver(mCx, chars.begin().get(),
|
2013-05-24 02:28:31 +04:00
|
|
|
uint32_t(mBufferedChars.Length()),
|
2013-09-17 05:33:40 +04:00
|
|
|
reviver, &value);
|
2012-07-30 15:19:09 +04:00
|
|
|
|
|
|
|
*mRootVal = value;
|
2011-03-21 21:42:14 +03:00
|
|
|
mBufferedChars.TruncateLength(0);
|
|
|
|
return ok ? NS_OK : NS_ERROR_FAILURE;
|
2007-12-28 00:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJSONListener::OnDataAvailable(nsIRequest *aRequest, nsISupports *aContext,
|
|
|
|
nsIInputStream *aStream,
|
2012-09-06 06:41:02 +04:00
|
|
|
uint64_t aOffset, uint32_t aLength)
|
2007-12-28 00:34:03 +03:00
|
|
|
{
|
2008-01-09 10:38:17 +03:00
|
|
|
nsresult rv = NS_OK;
|
2007-12-28 00:34:03 +03:00
|
|
|
|
|
|
|
if (mNeedsConverter && mSniffBuffer.Length() < 4) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t readCount = (aLength < 4) ? aLength : 4;
|
2007-12-28 00:34:03 +03:00
|
|
|
rv = NS_ConsumeStream(aStream, readCount, mSniffBuffer);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (mSniffBuffer.Length() < 4)
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2014-02-13 11:38:36 +04:00
|
|
|
|
2008-10-01 10:13:58 +04:00
|
|
|
char buffer[JSON_STREAM_BUFSIZE];
|
2007-12-28 00:34:03 +03:00
|
|
|
unsigned long bytesRemaining = aLength - mSniffBuffer.Length();
|
|
|
|
while (bytesRemaining) {
|
|
|
|
unsigned int bytesRead;
|
|
|
|
rv = aStream->Read(buffer,
|
2013-01-15 16:22:03 +04:00
|
|
|
std::min((unsigned long)sizeof(buffer), bytesRemaining),
|
2007-12-28 00:34:03 +03:00
|
|
|
&bytesRead);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = ProcessBytes(buffer, bytesRead);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
bytesRemaining -= bytesRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsJSONListener::ProcessBytes(const char* aBuffer, uint32_t aByteLength)
|
2007-12-28 00:34:03 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
// Check for BOM, or sniff charset
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString charset;
|
2007-12-28 00:34:03 +03:00
|
|
|
if (mNeedsConverter && !mDecoder) {
|
|
|
|
if (!nsContentUtils::CheckForBOM((const unsigned char*) mSniffBuffer.get(),
|
|
|
|
mSniffBuffer.Length(), charset)) {
|
|
|
|
// OK, found no BOM, sniff the first character to see what this is
|
|
|
|
// See section 3 of RFC4627 for details on why this works.
|
|
|
|
const char *buffer = mSniffBuffer.get();
|
|
|
|
if (mSniffBuffer.Length() >= 4) {
|
2011-03-30 10:35:34 +04:00
|
|
|
if (buffer[0] == 0x00 && buffer[1] != 0x00 &&
|
2007-12-28 00:34:03 +03:00
|
|
|
buffer[2] == 0x00 && buffer[3] != 0x00) {
|
|
|
|
charset = "UTF-16BE";
|
|
|
|
} else if (buffer[0] != 0x00 && buffer[1] == 0x00 &&
|
|
|
|
buffer[2] != 0x00 && buffer[3] == 0x00) {
|
|
|
|
charset = "UTF-16LE";
|
|
|
|
} else if (buffer[0] != 0x00 && buffer[1] != 0x00 &&
|
|
|
|
buffer[2] != 0x00 && buffer[3] != 0x00) {
|
|
|
|
charset = "UTF-8";
|
|
|
|
}
|
2011-07-07 22:42:11 +04:00
|
|
|
} else {
|
|
|
|
// Not enough bytes to sniff, assume UTF-8
|
|
|
|
charset = "UTF-8";
|
2007-12-28 00:34:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should have a unicode charset by now
|
|
|
|
rv = CheckCharset(charset.get());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-11-26 11:31:52 +04:00
|
|
|
mDecoder = EncodingUtils::DecoderForEncoding(charset);
|
2007-12-28 00:34:03 +03:00
|
|
|
|
|
|
|
// consume the sniffed bytes
|
|
|
|
rv = ConsumeConverted(mSniffBuffer.get(), mSniffBuffer.Length());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
mSniffBuffer.Truncate();
|
|
|
|
}
|
|
|
|
|
2011-07-07 22:42:11 +04:00
|
|
|
if (!aBuffer)
|
|
|
|
return NS_OK;
|
|
|
|
|
2007-12-28 00:34:03 +03:00
|
|
|
if (mNeedsConverter) {
|
|
|
|
rv = ConsumeConverted(aBuffer, aByteLength);
|
|
|
|
} else {
|
2014-01-04 19:02:17 +04:00
|
|
|
uint32_t unichars = aByteLength / sizeof(char16_t);
|
|
|
|
rv = Consume((char16_t *) aBuffer, unichars);
|
2007-12-28 00:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsJSONListener::ConsumeConverted(const char* aBuffer, uint32_t aByteLength)
|
2007-12-28 00:34:03 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t unicharLength = 0;
|
|
|
|
int32_t srcLen = aByteLength;
|
2007-12-28 00:34:03 +03:00
|
|
|
|
|
|
|
rv = mDecoder->GetMaxLength(aBuffer, srcLen, &unicharLength);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t* endelems = mBufferedChars.AppendElements(unicharLength);
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t preLength = unicharLength;
|
2011-03-21 21:42:14 +03:00
|
|
|
rv = mDecoder->Convert(aBuffer, &srcLen, endelems, &unicharLength);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(preLength >= unicharLength, "GetMaxLength lied");
|
2011-03-21 21:42:14 +03:00
|
|
|
if (preLength > unicharLength)
|
|
|
|
mBufferedChars.TruncateLength(mBufferedChars.Length() - (preLength - unicharLength));
|
|
|
|
return NS_OK;
|
2007-12-28 00:34:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-01-04 19:02:17 +04:00
|
|
|
nsJSONListener::Consume(const char16_t* aBuffer, uint32_t aByteLength)
|
2007-12-28 00:34:03 +03:00
|
|
|
{
|
2011-03-21 21:42:14 +03:00
|
|
|
if (!mBufferedChars.AppendElements(aBuffer, aByteLength))
|
2007-12-28 00:34:03 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2008-10-01 10:13:58 +04:00
|
|
|
return NS_OK;
|
2007-12-28 00:34:03 +03:00
|
|
|
}
|