Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef nsJSON_h__ |
michael@0 | 7 | #define nsJSON_h__ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsIJSON.h" |
michael@0 | 10 | #include "nsString.h" |
michael@0 | 11 | #include "nsCOMPtr.h" |
michael@0 | 12 | #include "nsIOutputStream.h" |
michael@0 | 13 | #include "nsIUnicodeEncoder.h" |
michael@0 | 14 | #include "nsIUnicodeDecoder.h" |
michael@0 | 15 | #include "nsIRequestObserver.h" |
michael@0 | 16 | #include "nsIStreamListener.h" |
michael@0 | 17 | #include "nsTArray.h" |
michael@0 | 18 | |
michael@0 | 19 | class nsIURI; |
michael@0 | 20 | |
michael@0 | 21 | class MOZ_STACK_CLASS nsJSONWriter |
michael@0 | 22 | { |
michael@0 | 23 | public: |
michael@0 | 24 | nsJSONWriter(); |
michael@0 | 25 | nsJSONWriter(nsIOutputStream *aStream); |
michael@0 | 26 | virtual ~nsJSONWriter(); |
michael@0 | 27 | nsresult SetCharset(const char *aCharset); |
michael@0 | 28 | nsCOMPtr<nsIOutputStream> mStream; |
michael@0 | 29 | nsresult Write(const char16_t *aBuffer, uint32_t aLength); |
michael@0 | 30 | nsString mOutputString; |
michael@0 | 31 | bool DidWrite(); |
michael@0 | 32 | void FlushBuffer(); |
michael@0 | 33 | |
michael@0 | 34 | protected: |
michael@0 | 35 | char16_t *mBuffer; |
michael@0 | 36 | uint32_t mBufferCount; |
michael@0 | 37 | bool mDidWrite; |
michael@0 | 38 | nsresult WriteToStream(nsIOutputStream *aStream, nsIUnicodeEncoder *encoder, |
michael@0 | 39 | const char16_t *aBuffer, uint32_t aLength); |
michael@0 | 40 | |
michael@0 | 41 | nsCOMPtr<nsIUnicodeEncoder> mEncoder; |
michael@0 | 42 | }; |
michael@0 | 43 | |
michael@0 | 44 | class nsJSON : public nsIJSON |
michael@0 | 45 | { |
michael@0 | 46 | public: |
michael@0 | 47 | nsJSON(); |
michael@0 | 48 | virtual ~nsJSON(); |
michael@0 | 49 | |
michael@0 | 50 | NS_DECL_ISUPPORTS |
michael@0 | 51 | NS_DECL_NSIJSON |
michael@0 | 52 | |
michael@0 | 53 | protected: |
michael@0 | 54 | nsresult EncodeInternal(JSContext* cx, |
michael@0 | 55 | const JS::Value& val, |
michael@0 | 56 | nsJSONWriter* writer); |
michael@0 | 57 | |
michael@0 | 58 | nsresult DecodeInternal(JSContext* cx, |
michael@0 | 59 | nsIInputStream* aStream, |
michael@0 | 60 | int32_t aContentLength, |
michael@0 | 61 | bool aNeedsConverter, |
michael@0 | 62 | JS::MutableHandle<JS::Value> aRetVal); |
michael@0 | 63 | nsCOMPtr<nsIURI> mURI; |
michael@0 | 64 | }; |
michael@0 | 65 | |
michael@0 | 66 | nsresult |
michael@0 | 67 | NS_NewJSON(nsISupports* aOuter, REFNSIID aIID, void** aResult); |
michael@0 | 68 | |
michael@0 | 69 | class nsJSONListener : public nsIStreamListener |
michael@0 | 70 | { |
michael@0 | 71 | public: |
michael@0 | 72 | nsJSONListener(JSContext *cx, JS::Value *rootVal, bool needsConverter); |
michael@0 | 73 | virtual ~nsJSONListener(); |
michael@0 | 74 | |
michael@0 | 75 | NS_DECL_ISUPPORTS |
michael@0 | 76 | NS_DECL_NSIREQUESTOBSERVER |
michael@0 | 77 | NS_DECL_NSISTREAMLISTENER |
michael@0 | 78 | |
michael@0 | 79 | protected: |
michael@0 | 80 | bool mNeedsConverter; |
michael@0 | 81 | JSContext *mCx; |
michael@0 | 82 | JS::Value *mRootVal; |
michael@0 | 83 | nsCOMPtr<nsIUnicodeDecoder> mDecoder; |
michael@0 | 84 | nsCString mSniffBuffer; |
michael@0 | 85 | nsTArray<char16_t> mBufferedChars; |
michael@0 | 86 | nsresult ProcessBytes(const char* aBuffer, uint32_t aByteLength); |
michael@0 | 87 | nsresult ConsumeConverted(const char* aBuffer, uint32_t aByteLength); |
michael@0 | 88 | nsresult Consume(const char16_t *data, uint32_t len); |
michael@0 | 89 | }; |
michael@0 | 90 | |
michael@0 | 91 | #endif |