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 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "nsSerializationHelper.h" |
michael@0 | 6 | |
michael@0 | 7 | |
michael@0 | 8 | #include "mozilla/Base64.h" |
michael@0 | 9 | #include "nsISerializable.h" |
michael@0 | 10 | #include "nsIObjectOutputStream.h" |
michael@0 | 11 | #include "nsIObjectInputStream.h" |
michael@0 | 12 | #include "nsString.h" |
michael@0 | 13 | #include "nsBase64Encoder.h" |
michael@0 | 14 | #include "nsAutoPtr.h" |
michael@0 | 15 | #include "nsComponentManagerUtils.h" |
michael@0 | 16 | #include "nsStringStream.h" |
michael@0 | 17 | |
michael@0 | 18 | using namespace mozilla; |
michael@0 | 19 | |
michael@0 | 20 | nsresult |
michael@0 | 21 | NS_SerializeToString(nsISerializable* obj, nsCSubstring& str) |
michael@0 | 22 | { |
michael@0 | 23 | nsRefPtr<nsBase64Encoder> stream(new nsBase64Encoder()); |
michael@0 | 24 | if (!stream) |
michael@0 | 25 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 26 | |
michael@0 | 27 | nsCOMPtr<nsIObjectOutputStream> objstream = |
michael@0 | 28 | do_CreateInstance("@mozilla.org/binaryoutputstream;1"); |
michael@0 | 29 | if (!objstream) |
michael@0 | 30 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 31 | |
michael@0 | 32 | objstream->SetOutputStream(stream); |
michael@0 | 33 | nsresult rv = |
michael@0 | 34 | objstream->WriteCompoundObject(obj, NS_GET_IID(nsISupports), true); |
michael@0 | 35 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 36 | return stream->Finish(str); |
michael@0 | 37 | } |
michael@0 | 38 | |
michael@0 | 39 | nsresult |
michael@0 | 40 | NS_DeserializeObject(const nsCSubstring& str, nsISupports** obj) |
michael@0 | 41 | { |
michael@0 | 42 | nsCString decodedData; |
michael@0 | 43 | nsresult rv = Base64Decode(str, decodedData); |
michael@0 | 44 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 45 | |
michael@0 | 46 | nsCOMPtr<nsIInputStream> stream; |
michael@0 | 47 | rv = NS_NewCStringInputStream(getter_AddRefs(stream), decodedData); |
michael@0 | 48 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 49 | |
michael@0 | 50 | nsCOMPtr<nsIObjectInputStream> objstream = |
michael@0 | 51 | do_CreateInstance("@mozilla.org/binaryinputstream;1"); |
michael@0 | 52 | if (!objstream) |
michael@0 | 53 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 54 | |
michael@0 | 55 | objstream->SetInputStream(stream); |
michael@0 | 56 | return objstream->ReadObject(true, obj); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | NS_IMPL_ISUPPORTS(nsSerializationHelper, nsISerializationHelper) |
michael@0 | 60 | |
michael@0 | 61 | NS_IMETHODIMP |
michael@0 | 62 | nsSerializationHelper::SerializeToString(nsISerializable *serializable, |
michael@0 | 63 | nsACString & _retval) |
michael@0 | 64 | { |
michael@0 | 65 | return NS_SerializeToString(serializable, _retval); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | NS_IMETHODIMP |
michael@0 | 69 | nsSerializationHelper::DeserializeObject(const nsACString & input, |
michael@0 | 70 | nsISupports **_retval) |
michael@0 | 71 | { |
michael@0 | 72 | return NS_DeserializeObject(input, _retval); |
michael@0 | 73 | } |