|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #include "mozilla/dom/PermissionMessageUtils.h" |
|
7 #include "nsISerializable.h" |
|
8 #include "nsSerializationHelper.h" |
|
9 |
|
10 namespace IPC { |
|
11 |
|
12 void |
|
13 ParamTraits<Principal>::Write(Message* aMsg, const paramType& aParam) { |
|
14 bool isNull = !aParam.mPrincipal; |
|
15 WriteParam(aMsg, isNull); |
|
16 if (isNull) { |
|
17 return; |
|
18 } |
|
19 |
|
20 bool isSerialized = false; |
|
21 nsCString principalString; |
|
22 nsCOMPtr<nsISerializable> serializable = do_QueryInterface(aParam.mPrincipal); |
|
23 if (serializable) { |
|
24 nsresult rv = NS_SerializeToString(serializable, principalString); |
|
25 if (NS_SUCCEEDED(rv)) { |
|
26 isSerialized = true; |
|
27 } |
|
28 } |
|
29 |
|
30 if (!isSerialized) { |
|
31 NS_RUNTIMEABORT("Unable to serialize principal."); |
|
32 return; |
|
33 } |
|
34 |
|
35 WriteParam(aMsg, principalString); |
|
36 } |
|
37 |
|
38 bool |
|
39 ParamTraits<Principal>::Read(const Message* aMsg, void** aIter, paramType* aResult) |
|
40 { |
|
41 bool isNull; |
|
42 if (!ReadParam(aMsg, aIter, &isNull)) { |
|
43 return false; |
|
44 } |
|
45 |
|
46 if (isNull) { |
|
47 aResult->mPrincipal = nullptr; |
|
48 return true; |
|
49 } |
|
50 |
|
51 nsCString principalString; |
|
52 if (!ReadParam(aMsg, aIter, &principalString)) { |
|
53 return false; |
|
54 } |
|
55 |
|
56 nsCOMPtr<nsISupports> iSupports; |
|
57 nsresult rv = NS_DeserializeObject(principalString, getter_AddRefs(iSupports)); |
|
58 NS_ENSURE_SUCCESS(rv, false); |
|
59 |
|
60 nsCOMPtr<nsIPrincipal> principal = do_QueryInterface(iSupports); |
|
61 NS_ENSURE_TRUE(principal, false); |
|
62 |
|
63 principal.swap(aResult->mPrincipal); |
|
64 return true; |
|
65 } |
|
66 |
|
67 } // namespace IPC |
|
68 |