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