Source code

Revision control

Other Tools

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