1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/base/src/nsSerializationHelper.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,73 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "nsSerializationHelper.h" 1.9 + 1.10 + 1.11 +#include "mozilla/Base64.h" 1.12 +#include "nsISerializable.h" 1.13 +#include "nsIObjectOutputStream.h" 1.14 +#include "nsIObjectInputStream.h" 1.15 +#include "nsString.h" 1.16 +#include "nsBase64Encoder.h" 1.17 +#include "nsAutoPtr.h" 1.18 +#include "nsComponentManagerUtils.h" 1.19 +#include "nsStringStream.h" 1.20 + 1.21 +using namespace mozilla; 1.22 + 1.23 +nsresult 1.24 +NS_SerializeToString(nsISerializable* obj, nsCSubstring& str) 1.25 +{ 1.26 + nsRefPtr<nsBase64Encoder> stream(new nsBase64Encoder()); 1.27 + if (!stream) 1.28 + return NS_ERROR_OUT_OF_MEMORY; 1.29 + 1.30 + nsCOMPtr<nsIObjectOutputStream> objstream = 1.31 + do_CreateInstance("@mozilla.org/binaryoutputstream;1"); 1.32 + if (!objstream) 1.33 + return NS_ERROR_OUT_OF_MEMORY; 1.34 + 1.35 + objstream->SetOutputStream(stream); 1.36 + nsresult rv = 1.37 + objstream->WriteCompoundObject(obj, NS_GET_IID(nsISupports), true); 1.38 + NS_ENSURE_SUCCESS(rv, rv); 1.39 + return stream->Finish(str); 1.40 +} 1.41 + 1.42 +nsresult 1.43 +NS_DeserializeObject(const nsCSubstring& str, nsISupports** obj) 1.44 +{ 1.45 + nsCString decodedData; 1.46 + nsresult rv = Base64Decode(str, decodedData); 1.47 + NS_ENSURE_SUCCESS(rv, rv); 1.48 + 1.49 + nsCOMPtr<nsIInputStream> stream; 1.50 + rv = NS_NewCStringInputStream(getter_AddRefs(stream), decodedData); 1.51 + NS_ENSURE_SUCCESS(rv, rv); 1.52 + 1.53 + nsCOMPtr<nsIObjectInputStream> objstream = 1.54 + do_CreateInstance("@mozilla.org/binaryinputstream;1"); 1.55 + if (!objstream) 1.56 + return NS_ERROR_OUT_OF_MEMORY; 1.57 + 1.58 + objstream->SetInputStream(stream); 1.59 + return objstream->ReadObject(true, obj); 1.60 +} 1.61 + 1.62 +NS_IMPL_ISUPPORTS(nsSerializationHelper, nsISerializationHelper) 1.63 + 1.64 +NS_IMETHODIMP 1.65 +nsSerializationHelper::SerializeToString(nsISerializable *serializable, 1.66 + nsACString & _retval) 1.67 +{ 1.68 + return NS_SerializeToString(serializable, _retval); 1.69 +} 1.70 + 1.71 +NS_IMETHODIMP 1.72 +nsSerializationHelper::DeserializeObject(const nsACString & input, 1.73 + nsISupports **_retval) 1.74 +{ 1.75 + return NS_DeserializeObject(input, _retval); 1.76 +}