2007-12-27 13:34:03 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2011-01-25 16:09:56 -08:00
|
|
|
/* vim: set ts=2 sw=2 et tw=79: */
|
2007-12-27 13:34:03 -08: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.
|
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Mozilla Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2007
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Dave Camp <dcamp@mozilla.com>
|
|
|
|
* Robert Sayre <sayrer@gmail.com>
|
2011-07-22 05:12:34 -07:00
|
|
|
* Nils Maier <maierman@web.de>
|
2007-12-27 13:34:03 -08:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either 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 ***** */
|
|
|
|
|
|
|
|
#include "jsapi.h"
|
2011-06-09 01:12:21 -07:00
|
|
|
#include "jsdbgapi.h"
|
2007-12-27 13:34:03 -08:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsJSON.h"
|
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsIXPCScriptable.h"
|
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsStringStream.h"
|
|
|
|
#include "nsICharsetConverterManager.h"
|
|
|
|
#include "nsXPCOMStrings.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsContentUtils.h"
|
2011-08-01 15:14:31 -07:00
|
|
|
#include "nsIScriptError.h"
|
2007-12-27 13:34:03 -08:00
|
|
|
#include "nsCRTGlue.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2011-01-25 16:09:56 -08:00
|
|
|
#include "nsIScriptSecurityManager.h"
|
2007-12-27 13:34:03 -08:00
|
|
|
|
|
|
|
static const char kXPConnectServiceCID[] = "@mozilla.org/js/xpc/XPConnect;1";
|
|
|
|
|
2010-03-02 17:54:40 -08:00
|
|
|
#define JSON_STREAM_BUFSIZE 4096
|
2008-09-30 23:13:58 -07:00
|
|
|
|
2007-12-27 13:34:03 -08: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-01 15:14:31 -07:00
|
|
|
enum DeprecationWarning { EncodeWarning, DecodeWarning };
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
WarnDeprecatedMethod(DeprecationWarning warning)
|
|
|
|
{
|
2011-12-15 06:47:03 -08:00
|
|
|
return nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
|
|
|
|
"DOM Core", nsnull,
|
|
|
|
nsContentUtils::eDOM_PROPERTIES,
|
2011-08-01 15:14:31 -07:00
|
|
|
warning == EncodeWarning
|
|
|
|
? "nsIJSONEncodeDeprecatedWarning"
|
2011-12-15 06:47:03 -08:00
|
|
|
: "nsIJSONDecodeDeprecatedWarning");
|
2011-08-01 15:14:31 -07:00
|
|
|
}
|
|
|
|
|
2011-07-22 05:12:34 -07:00
|
|
|
NS_IMETHODIMP
|
2011-11-26 02:21:47 -08:00
|
|
|
nsJSON::Encode(const JS::Value& aValue, JSContext* cx, PRUint8 aArgc, nsAString &aJSON)
|
2011-07-22 05:12:34 -07:00
|
|
|
{
|
|
|
|
// This function should only be called from JS.
|
2011-08-01 15:14:31 -07:00
|
|
|
nsresult rv = WarnDeprecatedMethod(EncodeWarning);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2011-07-22 05:12:34 -07:00
|
|
|
|
2011-11-26 02:21:47 -08:00
|
|
|
if (aArgc == 0) {
|
|
|
|
aJSON.Truncate();
|
|
|
|
aJSON.SetIsVoid(true);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-07-22 05:12:34 -07:00
|
|
|
nsJSONWriter writer;
|
2011-11-26 02:21:47 -08:00
|
|
|
rv = EncodeInternal(cx, aValue, &writer);
|
2011-07-22 05:12:34 -07: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()) {
|
|
|
|
aJSON.Truncate();
|
2011-10-17 07:59:28 -07:00
|
|
|
aJSON.SetIsVoid(true);
|
2011-07-22 05:12:34 -07:00
|
|
|
} else {
|
|
|
|
writer.FlushBuffer();
|
|
|
|
aJSON.Append(writer.mOutputString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2007-12-27 13:34:03 -08: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-29 23:35:34 -07:00
|
|
|
strcmp(aCharset, "UTF-16BE") == 0)) {
|
2007-12-27 13:34:03 -08:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJSON::EncodeToStream(nsIOutputStream *aStream,
|
|
|
|
const char* aCharset,
|
2011-11-26 02:21:47 -08:00
|
|
|
const bool aWriteBOM,
|
|
|
|
const JS::Value& val,
|
|
|
|
JSContext* cx,
|
|
|
|
PRUint8 aArgc)
|
2007-12-27 13:34:03 -08: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;
|
|
|
|
//}
|
|
|
|
|
|
|
|
PRUint32 ignored;
|
|
|
|
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 12:06:27 -07:00
|
|
|
nsJSONWriter writer(bufferedStream);
|
|
|
|
rv = writer.SetCharset(aCharset);
|
2007-12-27 13:34:03 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-11-26 02:21:47 -08:00
|
|
|
if (aArgc == 0) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = EncodeInternal(cx, val, &writer);
|
2007-12-27 13:34:03 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = bufferedStream->Flush();
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2008-09-30 23:13:58 -07:00
|
|
|
static JSBool
|
|
|
|
WriteCallback(const jschar *buf, uint32 len, void *data)
|
|
|
|
{
|
|
|
|
nsJSONWriter *writer = static_cast<nsJSONWriter*>(data);
|
|
|
|
nsresult rv = writer->Write((const PRUnichar*)buf, (PRUint32)len);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return JS_FALSE;
|
|
|
|
|
|
|
|
return JS_TRUE;
|
|
|
|
}
|
|
|
|
|
2009-09-01 09:45:05 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJSON::EncodeFromJSVal(jsval *value, JSContext *cx, nsAString &result)
|
|
|
|
{
|
|
|
|
result.Truncate();
|
|
|
|
|
|
|
|
// Begin a new request
|
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
|
2010-09-22 17:34:20 -07:00
|
|
|
JSAutoEnterCompartment ac;
|
|
|
|
JSObject *obj;
|
2011-01-25 16:09:56 -08:00
|
|
|
nsIScriptSecurityManager *ssm = nsnull;
|
|
|
|
if (JSVAL_IS_OBJECT(*value) && (obj = JSVAL_TO_OBJECT(*value))) {
|
|
|
|
if (!ac.enter(cx, obj)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPrincipal> principal;
|
|
|
|
ssm = nsContentUtils::GetSecurityManager();
|
|
|
|
nsresult rv = ssm->GetObjectPrincipal(cx, obj, getter_AddRefs(principal));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
JSStackFrame *fp = nsnull;
|
|
|
|
rv = ssm->PushContextPrincipal(cx, JS_FrameIterator(cx, &fp), principal);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-09-22 17:34:20 -07:00
|
|
|
}
|
|
|
|
|
2009-09-01 09:45:05 -07:00
|
|
|
nsJSONWriter writer;
|
|
|
|
JSBool ok = JS_Stringify(cx, value, NULL, JSVAL_NULL,
|
|
|
|
WriteCallback, &writer);
|
2011-01-25 16:09:56 -08:00
|
|
|
|
|
|
|
if (ssm) {
|
|
|
|
ssm->PopContextPrincipal(cx);
|
|
|
|
}
|
|
|
|
|
2009-09-01 09:45:05 -07:00
|
|
|
if (!ok) {
|
|
|
|
return NS_ERROR_XPC_BAD_CONVERT_JS;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(writer.DidWrite(), NS_ERROR_UNEXPECTED);
|
|
|
|
writer.FlushBuffer();
|
|
|
|
result.Assign(writer.mOutputString);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-12-27 13:34:03 -08:00
|
|
|
nsresult
|
2011-11-26 02:21:47 -08:00
|
|
|
nsJSON::EncodeInternal(JSContext* cx, const JS::Value& aValue, nsJSONWriter* writer)
|
2007-12-27 13:34:03 -08:00
|
|
|
{
|
2011-07-22 05:12:34 -07:00
|
|
|
JSAutoRequest ar(cx);
|
2007-12-27 13:34:03 -08:00
|
|
|
|
2011-07-22 05:12:34 -07:00
|
|
|
// Backward compatibility:
|
|
|
|
// nsIJSON does not allow to serialize anything other than objects
|
2011-11-26 02:21:47 -08:00
|
|
|
if (!JSVAL_IS_OBJECT(aValue)) {
|
2011-07-22 05:12:34 -07:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2011-11-26 02:21:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* obj = JSVAL_TO_OBJECT(aValue);
|
|
|
|
if (!obj) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Value val = aValue;
|
2011-07-22 05:12:34 -07: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
|
|
|
|
*/
|
|
|
|
jsval toJSON;
|
|
|
|
if (JS_GetMethod(cx, obj, "toJSON", NULL, &toJSON) &&
|
|
|
|
!JSVAL_IS_PRIMITIVE(toJSON) &&
|
|
|
|
JS_ObjectIsCallable(cx, JSVAL_TO_OBJECT(toJSON))) {
|
|
|
|
// If toJSON is implemented, it must not throw
|
2011-11-26 02:21:47 -08:00
|
|
|
if (!JS_CallFunctionValue(cx, obj, toJSON, 0, NULL, &val)) {
|
2011-07-22 05:12:34 -07: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
|
2011-11-26 02:21:47 -08:00
|
|
|
if (JSVAL_IS_PRIMITIVE(val))
|
2011-07-22 05:12:34 -07: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:
|
|
|
|
// function/xml shall not pass, just "plain" objects and arrays
|
2011-11-26 02:21:47 -08:00
|
|
|
JSType type = JS_TypeOfValue(cx, val);
|
2011-07-22 05:12:34 -07:00
|
|
|
if (type == JSTYPE_FUNCTION || type == JSTYPE_XML)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
// We're good now; try to stringify
|
2011-11-26 02:21:47 -08:00
|
|
|
if (!JS_Stringify(cx, &val, NULL, JSVAL_NULL, WriteCallback, writer))
|
2007-12-27 13:34:03 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
2011-07-22 05:12:34 -07:00
|
|
|
|
2008-09-30 23:13:58 -07:00
|
|
|
return NS_OK;
|
2007-12-27 13:34:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-06 23:19:26 -08:00
|
|
|
nsJSONWriter::nsJSONWriter() : mStream(nsnull),
|
|
|
|
mBuffer(nsnull),
|
|
|
|
mBufferCount(0),
|
2011-10-17 07:59:28 -07:00
|
|
|
mDidWrite(false),
|
2008-02-06 23:19:26 -08:00
|
|
|
mEncoder(nsnull)
|
2007-12-27 13:34:03 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-04-04 16:10:10 -07:00
|
|
|
nsJSONWriter::nsJSONWriter(nsIOutputStream *aStream) : mStream(aStream),
|
2008-02-06 23:19:26 -08:00
|
|
|
mBuffer(nsnull),
|
|
|
|
mBufferCount(0),
|
2011-10-17 07:59:28 -07:00
|
|
|
mDidWrite(false),
|
2007-12-27 13:34:03 -08:00
|
|
|
mEncoder(nsnull)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsJSONWriter::~nsJSONWriter()
|
|
|
|
{
|
2008-02-06 23:19:26 -08:00
|
|
|
delete [] mBuffer;
|
2007-12-27 13:34:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsJSONWriter::SetCharset(const char* aCharset)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (mStream) {
|
|
|
|
nsCOMPtr<nsICharsetConverterManager> ccm =
|
|
|
|
do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = ccm->GetUnicodeEncoder(aCharset, getter_AddRefs(mEncoder));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = mEncoder->SetOutputErrorBehavior(nsIUnicodeEncoder::kOnError_Signal,
|
|
|
|
nsnull, nsnull);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsJSONWriter::Write(const PRUnichar *aBuffer, PRUint32 aLength)
|
|
|
|
{
|
|
|
|
if (mStream) {
|
2008-02-06 23:19:26 -08:00
|
|
|
return WriteToStream(mStream, mEncoder, aBuffer, aLength);
|
2007-12-27 13:34:03 -08:00
|
|
|
}
|
|
|
|
|
2008-02-06 23:19:26 -08:00
|
|
|
if (!mDidWrite) {
|
2008-09-30 23:13:58 -07:00
|
|
|
mBuffer = new PRUnichar[JSON_STREAM_BUFSIZE];
|
2008-02-06 23:19:26 -08:00
|
|
|
if (!mBuffer)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2011-10-17 07:59:28 -07:00
|
|
|
mDidWrite = true;
|
2008-02-06 23:19:26 -08:00
|
|
|
}
|
|
|
|
|
2008-09-30 23:13:58 -07:00
|
|
|
if (JSON_STREAM_BUFSIZE <= aLength + mBufferCount) {
|
2008-02-06 23:19:26 -08:00
|
|
|
mOutputString.Append(mBuffer, mBufferCount);
|
|
|
|
mBufferCount = 0;
|
|
|
|
}
|
|
|
|
|
2008-09-30 23:13:58 -07:00
|
|
|
if (JSON_STREAM_BUFSIZE <= aLength) {
|
2008-03-17 11:58:38 -07:00
|
|
|
// we know mBufferCount is 0 because we know we hit the if above
|
|
|
|
mOutputString.Append(aBuffer, aLength);
|
|
|
|
} else {
|
|
|
|
memcpy(&mBuffer[mBufferCount], aBuffer, aLength * sizeof(PRUnichar));
|
|
|
|
mBufferCount += aLength;
|
|
|
|
}
|
2008-02-06 23:19:26 -08:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool nsJSONWriter::DidWrite()
|
2008-02-06 23:19:26 -08:00
|
|
|
{
|
|
|
|
return mDidWrite;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsJSONWriter::FlushBuffer()
|
|
|
|
{
|
|
|
|
mOutputString.Append(mBuffer, mBufferCount);
|
2007-12-27 13:34:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsJSONWriter::WriteToStream(nsIOutputStream *aStream,
|
|
|
|
nsIUnicodeEncoder *encoder,
|
|
|
|
const PRUnichar *aBuffer,
|
|
|
|
PRUint32 aLength)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
PRInt32 srcLength = aLength;
|
|
|
|
PRUint32 bytesWritten;
|
|
|
|
|
|
|
|
// The bytes written to the stream might differ from the PRUnichar size
|
|
|
|
PRInt32 aDestLength;
|
|
|
|
rv = encoder->GetMaxLength(aBuffer, srcLength, &aDestLength);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// create the buffer we need
|
|
|
|
char* destBuf = (char *) NS_Alloc(aDestLength);
|
|
|
|
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);
|
|
|
|
|
|
|
|
NS_Free(destBuf);
|
2011-10-17 07:59:28 -07:00
|
|
|
mDidWrite = true;
|
2007-12-27 13:34:03 -08:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2011-07-22 05:12:34 -07:00
|
|
|
NS_IMETHODIMP
|
2011-11-26 02:21:29 -08:00
|
|
|
nsJSON::Decode(const nsAString& json, JSContext* cx, JS::Value* aRetval)
|
2011-07-22 05:12:34 -07:00
|
|
|
{
|
2011-08-01 15:14:31 -07:00
|
|
|
nsresult rv = WarnDeprecatedMethod(DecodeWarning);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2011-07-22 05:12:34 -07:00
|
|
|
const PRUnichar *data;
|
|
|
|
PRUint32 len = NS_StringGetData(json, &data);
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2011-08-01 15:14:31 -07:00
|
|
|
rv = NS_NewByteInputStream(getter_AddRefs(stream),
|
|
|
|
reinterpret_cast<const char*>(data),
|
|
|
|
len * sizeof(PRUnichar),
|
|
|
|
NS_ASSIGNMENT_DEPEND);
|
2011-07-22 05:12:34 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-11-26 02:21:29 -08:00
|
|
|
return DecodeInternal(cx, stream, len, false, aRetval);
|
2011-07-22 05:12:34 -07:00
|
|
|
}
|
|
|
|
|
2007-12-27 13:34:03 -08:00
|
|
|
NS_IMETHODIMP
|
2011-11-26 02:21:29 -08:00
|
|
|
nsJSON::DecodeFromStream(nsIInputStream *aStream, PRInt32 aContentLength,
|
|
|
|
JSContext* cx, JS::Value* aRetval)
|
2007-12-27 13:34:03 -08:00
|
|
|
{
|
2011-11-26 02:21:29 -08:00
|
|
|
return DecodeInternal(cx, aStream, aContentLength, true, aRetval);
|
2007-12-27 13:34:03 -08:00
|
|
|
}
|
|
|
|
|
2009-09-01 09:45:05 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJSON::DecodeToJSVal(const nsAString &str, JSContext *cx, jsval *result)
|
|
|
|
{
|
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
|
2011-11-26 02:21:29 -08:00
|
|
|
if (!JS_ParseJSON(cx, static_cast<const jschar*>(PromiseFlatString(str).get()),
|
|
|
|
str.Length(), result)) {
|
2009-09-01 09:45:05 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-12-27 13:34:03 -08:00
|
|
|
nsresult
|
2011-11-26 02:21:29 -08:00
|
|
|
nsJSON::DecodeInternal(JSContext* cx,
|
|
|
|
nsIInputStream *aStream,
|
2007-12-27 13:34:03 -08:00
|
|
|
PRInt32 aContentLength,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool aNeedsConverter,
|
2011-11-26 02:21:29 -08:00
|
|
|
JS::Value* aRetval,
|
2010-07-14 11:48:36 -07:00
|
|
|
DecodingMode mode /* = STRICT */)
|
2007-12-27 13:34:03 -08:00
|
|
|
{
|
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
|
|
|
|
// Consume the stream
|
|
|
|
nsCOMPtr<nsIChannel> jsonChannel;
|
2008-02-06 23:19:26 -08:00
|
|
|
if (!mURI) {
|
|
|
|
NS_NewURI(getter_AddRefs(mURI), NS_LITERAL_CSTRING("about:blank"), 0, 0 );
|
|
|
|
if (!mURI)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2011-11-26 02:21:29 -08:00
|
|
|
nsresult rv =
|
|
|
|
NS_NewInputStreamChannel(getter_AddRefs(jsonChannel), mURI, aStream,
|
|
|
|
NS_LITERAL_CSTRING("application/json"));
|
2007-12-27 13:34:03 -08:00
|
|
|
if (!jsonChannel || NS_FAILED(rv))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2011-11-26 02:21:29 -08:00
|
|
|
nsRefPtr<nsJSONListener> jsonListener =
|
|
|
|
new nsJSONListener(cx, aRetval, aNeedsConverter, mode);
|
2007-12-27 13:34:03 -08:00
|
|
|
|
|
|
|
//XXX this stream pattern should be consolidated in netwerk
|
|
|
|
rv = jsonListener->OnStartRequest(jsonChannel, nsnull);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
jsonChannel->Cancel(rv);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult status;
|
|
|
|
jsonChannel->GetStatus(&status);
|
|
|
|
PRUint32 offset = 0;
|
|
|
|
while (NS_SUCCEEDED(status)) {
|
|
|
|
PRUint32 available;
|
|
|
|
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
|
|
|
|
|
|
|
|
rv = jsonListener->OnDataAvailable(jsonChannel, nsnull,
|
|
|
|
aStream, offset, available);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
jsonChannel->Cancel(rv);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
offset += available;
|
|
|
|
jsonChannel->GetStatus(&status);
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = jsonListener->OnStopRequest(jsonChannel, nsnull, status);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-07-14 11:48:36 -07:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-11-26 02:21:29 -08:00
|
|
|
nsJSON::LegacyDecode(const nsAString& json, JSContext* cx, JS::Value* aRetval)
|
2010-07-14 11:48:36 -07:00
|
|
|
{
|
|
|
|
const PRUnichar *data;
|
|
|
|
PRUint32 len = NS_StringGetData(json, &data);
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
nsresult rv = NS_NewByteInputStream(getter_AddRefs(stream),
|
|
|
|
(const char*) data,
|
|
|
|
len * sizeof(PRUnichar),
|
|
|
|
NS_ASSIGNMENT_DEPEND);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-11-26 02:21:29 -08:00
|
|
|
return DecodeInternal(cx, stream, len, false, aRetval, LEGACY);
|
2010-07-14 11:48:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-11-26 02:21:29 -08:00
|
|
|
nsJSON::LegacyDecodeFromStream(nsIInputStream *aStream, PRInt32 aContentLength,
|
|
|
|
JSContext* cx, JS::Value* aRetval)
|
2010-07-14 11:48:36 -07:00
|
|
|
{
|
2011-11-26 02:21:29 -08:00
|
|
|
return DecodeInternal(cx, aStream, aContentLength, true, aRetval, LEGACY);
|
2010-07-14 11:48:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJSON::LegacyDecodeToJSVal(const nsAString &str, JSContext *cx, jsval *result)
|
|
|
|
{
|
|
|
|
JSAutoRequest ar(cx);
|
|
|
|
|
2011-11-26 02:21:29 -08:00
|
|
|
if (!js::ParseJSONWithReviver(cx, static_cast<const jschar*>(PromiseFlatString(str).get()),
|
|
|
|
str.Length(), JS::NullValue(),
|
2011-09-20 11:40:24 -07:00
|
|
|
result, LEGACY)) {
|
2010-07-14 11:48:36 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-10 11:11:11 -07:00
|
|
|
nsresult
|
2007-12-27 13:34:03 -08:00
|
|
|
NS_NewJSON(nsISupports* aOuter, REFNSIID aIID, void** aResult)
|
|
|
|
{
|
|
|
|
nsJSON* json = new nsJSON();
|
|
|
|
if (!json)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
NS_ADDREF(json);
|
|
|
|
*aResult = json;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsJSONListener::nsJSONListener(JSContext *cx, jsval *rootVal,
|
2011-09-28 23:19:26 -07:00
|
|
|
bool needsConverter,
|
2010-07-14 11:48:36 -07:00
|
|
|
DecodingMode mode /* = STRICT */)
|
2008-09-30 23:13:58 -07:00
|
|
|
: mNeedsConverter(needsConverter),
|
2007-12-27 13:34:03 -08:00
|
|
|
mCx(cx),
|
2010-07-14 11:48:36 -07:00
|
|
|
mRootVal(rootVal),
|
|
|
|
mDecodingMode(mode)
|
2007-12-27 13:34:03 -08: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();
|
|
|
|
mDecoder = nsnull;
|
2008-09-30 23:13:58 -07:00
|
|
|
|
2007-12-27 13:34:03 -08:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJSONListener::OnStopRequest(nsIRequest *aRequest, nsISupports *aContext,
|
|
|
|
nsresult aStatusCode)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2011-07-07 11:42:11 -07:00
|
|
|
// This can happen with short UTF-8 messages (<4 bytes)
|
2007-12-27 13:34:03 -08:00
|
|
|
if (!mSniffBuffer.IsEmpty()) {
|
2011-07-07 11:42:11 -07:00
|
|
|
// Just consume mSniffBuffer
|
|
|
|
rv = ProcessBytes(nsnull, 0);
|
2007-12-27 13:34:03 -08:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2011-03-21 11:42:14 -07:00
|
|
|
const jschar* chars = reinterpret_cast<const jschar*>(mBufferedChars.Elements());
|
|
|
|
JSBool ok = js::ParseJSONWithReviver(mCx, chars,
|
|
|
|
(uint32) mBufferedChars.Length(),
|
2011-09-20 11:40:24 -07:00
|
|
|
js::NullValue(), mRootVal,
|
2011-03-21 11:42:14 -07:00
|
|
|
mDecodingMode);
|
|
|
|
mBufferedChars.TruncateLength(0);
|
|
|
|
return ok ? NS_OK : NS_ERROR_FAILURE;
|
2007-12-27 13:34:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsJSONListener::OnDataAvailable(nsIRequest *aRequest, nsISupports *aContext,
|
|
|
|
nsIInputStream *aStream,
|
|
|
|
PRUint32 aOffset, PRUint32 aLength)
|
|
|
|
{
|
|
|
|
PRUint32 contentLength;
|
|
|
|
aStream->Available(&contentLength);
|
2008-01-08 23:38:17 -08:00
|
|
|
nsresult rv = NS_OK;
|
2007-12-27 13:34:03 -08:00
|
|
|
|
|
|
|
if (mNeedsConverter && mSniffBuffer.Length() < 4) {
|
|
|
|
PRUint32 readCount = (aLength < 4) ? aLength : 4;
|
|
|
|
rv = NS_ConsumeStream(aStream, readCount, mSniffBuffer);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (mSniffBuffer.Length() < 4)
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-09-30 23:13:58 -07:00
|
|
|
char buffer[JSON_STREAM_BUFSIZE];
|
2007-12-27 13:34:03 -08:00
|
|
|
unsigned long bytesRemaining = aLength - mSniffBuffer.Length();
|
|
|
|
while (bytesRemaining) {
|
|
|
|
unsigned int bytesRead;
|
|
|
|
rv = aStream->Read(buffer,
|
2010-02-20 05:59:07 -08:00
|
|
|
NS_MIN((unsigned long)sizeof(buffer), bytesRemaining),
|
2007-12-27 13:34:03 -08:00
|
|
|
&bytesRead);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = ProcessBytes(buffer, bytesRead);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
bytesRemaining -= bytesRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsJSONListener::ProcessBytes(const char* aBuffer, PRUint32 aByteLength)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
// Check for BOM, or sniff charset
|
|
|
|
nsCAutoString charset;
|
|
|
|
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-29 23:35:34 -07:00
|
|
|
if (buffer[0] == 0x00 && buffer[1] != 0x00 &&
|
2007-12-27 13:34:03 -08: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 11:42:11 -07:00
|
|
|
} else {
|
|
|
|
// Not enough bytes to sniff, assume UTF-8
|
|
|
|
charset = "UTF-8";
|
2007-12-27 13:34:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We should have a unicode charset by now
|
|
|
|
rv = CheckCharset(charset.get());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<nsICharsetConverterManager> ccm =
|
|
|
|
do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = ccm->GetUnicodeDecoderRaw(charset.get(), getter_AddRefs(mDecoder));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// consume the sniffed bytes
|
|
|
|
rv = ConsumeConverted(mSniffBuffer.get(), mSniffBuffer.Length());
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
mSniffBuffer.Truncate();
|
|
|
|
}
|
|
|
|
|
2011-07-07 11:42:11 -07:00
|
|
|
if (!aBuffer)
|
|
|
|
return NS_OK;
|
|
|
|
|
2007-12-27 13:34:03 -08:00
|
|
|
if (mNeedsConverter) {
|
|
|
|
rv = ConsumeConverted(aBuffer, aByteLength);
|
|
|
|
} else {
|
|
|
|
PRUint32 unichars = aByteLength / sizeof(PRUnichar);
|
|
|
|
rv = Consume((PRUnichar *) aBuffer, unichars);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsJSONListener::ConsumeConverted(const char* aBuffer, PRUint32 aByteLength)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
PRInt32 unicharLength = 0;
|
|
|
|
PRInt32 srcLen = aByteLength;
|
|
|
|
|
|
|
|
rv = mDecoder->GetMaxLength(aBuffer, srcLen, &unicharLength);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-03-21 11:42:14 -07:00
|
|
|
PRUnichar* endelems = mBufferedChars.AppendElements(unicharLength);
|
|
|
|
PRInt32 preLength = unicharLength;
|
|
|
|
rv = mDecoder->Convert(aBuffer, &srcLen, endelems, &unicharLength);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
NS_ABORT_IF_FALSE(preLength >= unicharLength, "GetMaxLength lied");
|
|
|
|
if (preLength > unicharLength)
|
|
|
|
mBufferedChars.TruncateLength(mBufferedChars.Length() - (preLength - unicharLength));
|
|
|
|
return NS_OK;
|
2007-12-27 13:34:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2008-09-30 23:13:58 -07:00
|
|
|
nsJSONListener::Consume(const PRUnichar* aBuffer, PRUint32 aByteLength)
|
2007-12-27 13:34:03 -08:00
|
|
|
{
|
2011-03-21 11:42:14 -07:00
|
|
|
if (!mBufferedChars.AppendElements(aBuffer, aByteLength))
|
2007-12-27 13:34:03 -08:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2008-09-30 23:13:58 -07:00
|
|
|
return NS_OK;
|
2007-12-27 13:34:03 -08:00
|
|
|
}
|