michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsSerializationHelper.h" michael@0: michael@0: michael@0: #include "mozilla/Base64.h" michael@0: #include "nsISerializable.h" michael@0: #include "nsIObjectOutputStream.h" michael@0: #include "nsIObjectInputStream.h" michael@0: #include "nsString.h" michael@0: #include "nsBase64Encoder.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsComponentManagerUtils.h" michael@0: #include "nsStringStream.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: nsresult michael@0: NS_SerializeToString(nsISerializable* obj, nsCSubstring& str) michael@0: { michael@0: nsRefPtr stream(new nsBase64Encoder()); michael@0: if (!stream) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: nsCOMPtr objstream = michael@0: do_CreateInstance("@mozilla.org/binaryoutputstream;1"); michael@0: if (!objstream) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: objstream->SetOutputStream(stream); michael@0: nsresult rv = michael@0: objstream->WriteCompoundObject(obj, NS_GET_IID(nsISupports), true); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: return stream->Finish(str); michael@0: } michael@0: michael@0: nsresult michael@0: NS_DeserializeObject(const nsCSubstring& str, nsISupports** obj) michael@0: { michael@0: nsCString decodedData; michael@0: nsresult rv = Base64Decode(str, decodedData); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsCOMPtr stream; michael@0: rv = NS_NewCStringInputStream(getter_AddRefs(stream), decodedData); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsCOMPtr objstream = michael@0: do_CreateInstance("@mozilla.org/binaryinputstream;1"); michael@0: if (!objstream) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: objstream->SetInputStream(stream); michael@0: return objstream->ReadObject(true, obj); michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(nsSerializationHelper, nsISerializationHelper) michael@0: michael@0: NS_IMETHODIMP michael@0: nsSerializationHelper::SerializeToString(nsISerializable *serializable, michael@0: nsACString & _retval) michael@0: { michael@0: return NS_SerializeToString(serializable, _retval); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsSerializationHelper::DeserializeObject(const nsACString & input, michael@0: nsISupports **_retval) michael@0: { michael@0: return NS_DeserializeObject(input, _retval); michael@0: }