2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-11-30 21:05:54 +03:00
|
|
|
|
|
|
|
#include "nsSerializationHelper.h"
|
|
|
|
|
|
|
|
|
2012-12-10 12:19:02 +04:00
|
|
|
#include "mozilla/Base64.h"
|
2007-11-30 21:05:54 +03:00
|
|
|
#include "nsISerializable.h"
|
|
|
|
#include "nsIObjectOutputStream.h"
|
|
|
|
#include "nsIObjectInputStream.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsBase64Encoder.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsStringStream.h"
|
|
|
|
|
2012-12-10 12:19:02 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2007-11-30 21:05:54 +03:00
|
|
|
nsresult
|
|
|
|
NS_SerializeToString(nsISerializable* obj, nsCSubstring& str)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsBase64Encoder> stream(new nsBase64Encoder());
|
2007-11-30 21:05:54 +03:00
|
|
|
if (!stream)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObjectOutputStream> objstream =
|
|
|
|
do_CreateInstance("@mozilla.org/binaryoutputstream;1");
|
|
|
|
if (!objstream)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
objstream->SetOutputStream(stream);
|
|
|
|
nsresult rv =
|
2011-10-17 18:59:28 +04:00
|
|
|
objstream->WriteCompoundObject(obj, NS_GET_IID(nsISupports), true);
|
2007-11-30 21:05:54 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return stream->Finish(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_DeserializeObject(const nsCSubstring& str, nsISupports** obj)
|
|
|
|
{
|
2012-12-10 12:19:02 +04:00
|
|
|
nsCString decodedData;
|
|
|
|
nsresult rv = Base64Decode(str, decodedData);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-09-18 22:53:17 +04:00
|
|
|
|
2007-11-30 21:05:54 +03:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2012-12-10 12:19:02 +04:00
|
|
|
rv = NS_NewCStringInputStream(getter_AddRefs(stream), decodedData);
|
2007-11-30 21:05:54 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIObjectInputStream> objstream =
|
|
|
|
do_CreateInstance("@mozilla.org/binaryinputstream;1");
|
|
|
|
if (!objstream)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
objstream->SetInputStream(stream);
|
2011-10-17 18:59:28 +04:00
|
|
|
return objstream->ReadObject(true, obj);
|
2007-11-30 21:05:54 +03:00
|
|
|
}
|
2010-10-26 15:57:18 +04:00
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsSerializationHelper, nsISerializationHelper)
|
2010-10-26 15:57:18 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSerializationHelper::SerializeToString(nsISerializable *serializable,
|
2012-07-07 00:14:07 +04:00
|
|
|
nsACString & _retval)
|
2010-10-26 15:57:18 +04:00
|
|
|
{
|
|
|
|
return NS_SerializeToString(serializable, _retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsSerializationHelper::DeserializeObject(const nsACString & input,
|
2012-07-07 00:14:07 +04:00
|
|
|
nsISupports **_retval)
|
2010-10-26 15:57:18 +04:00
|
|
|
{
|
|
|
|
return NS_DeserializeObject(input, _retval);
|
|
|
|
}
|