/src/mozilla-central/netwerk/base/nsSerializationHelper.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
8 | | #include "mozilla/Base64.h" |
9 | | #include "nsISerializable.h" |
10 | | #include "nsIObjectOutputStream.h" |
11 | | #include "nsIObjectInputStream.h" |
12 | | #include "nsString.h" |
13 | | #include "nsBase64Encoder.h" |
14 | | #include "nsAutoPtr.h" |
15 | | #include "nsComponentManagerUtils.h" |
16 | | #include "nsStringStream.h" |
17 | | |
18 | | using namespace mozilla; |
19 | | |
20 | | nsresult |
21 | | NS_SerializeToString(nsISerializable* obj, nsACString& str) |
22 | 0 | { |
23 | 0 | RefPtr<nsBase64Encoder> stream(new nsBase64Encoder()); |
24 | 0 | if (!stream) |
25 | 0 | return NS_ERROR_OUT_OF_MEMORY; |
26 | 0 | |
27 | 0 | nsCOMPtr<nsIObjectOutputStream> objstream = |
28 | 0 | NS_NewObjectOutputStream(stream); |
29 | 0 | nsresult rv = |
30 | 0 | objstream->WriteCompoundObject(obj, NS_GET_IID(nsISupports), true); |
31 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
32 | 0 | return stream->Finish(str); |
33 | 0 | } |
34 | | |
35 | | nsresult |
36 | | NS_DeserializeObject(const nsACString& str, nsISupports** obj) |
37 | 0 | { |
38 | 0 | nsCString decodedData; |
39 | 0 | nsresult rv = Base64Decode(str, decodedData); |
40 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
41 | 0 |
|
42 | 0 | nsCOMPtr<nsIInputStream> stream; |
43 | 0 | rv = NS_NewCStringInputStream(getter_AddRefs(stream), std::move(decodedData)); |
44 | 0 | NS_ENSURE_SUCCESS(rv, rv); |
45 | 0 |
|
46 | 0 | nsCOMPtr<nsIObjectInputStream> objstream = |
47 | 0 | NS_NewObjectInputStream(stream); |
48 | 0 | return objstream->ReadObject(true, obj); |
49 | 0 | } |
50 | | |
51 | | NS_IMPL_ISUPPORTS(nsSerializationHelper, nsISerializationHelper) |
52 | | |
53 | | NS_IMETHODIMP |
54 | | nsSerializationHelper::SerializeToString(nsISerializable *serializable, |
55 | | nsACString & _retval) |
56 | 0 | { |
57 | 0 | return NS_SerializeToString(serializable, _retval); |
58 | 0 | } |
59 | | |
60 | | NS_IMETHODIMP |
61 | | nsSerializationHelper::DeserializeObject(const nsACString & input, |
62 | | nsISupports **_retval) |
63 | 0 | { |
64 | 0 | return NS_DeserializeObject(input, _retval); |
65 | 0 | } |