|
1 /* -*- Mode: C++; tab-width: 8; 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 #ifndef mozilla_net_NeckoMessageUtils_h |
|
7 #define mozilla_net_NeckoMessageUtils_h |
|
8 |
|
9 #include "mozilla/DebugOnly.h" |
|
10 |
|
11 #include "ipc/IPCMessageUtils.h" |
|
12 #include "nsStringGlue.h" |
|
13 #include "prio.h" |
|
14 #include "mozilla/net/DNS.h" |
|
15 |
|
16 namespace IPC { |
|
17 |
|
18 // nsIPermissionManager utilities |
|
19 |
|
20 struct Permission |
|
21 { |
|
22 nsCString host, type; |
|
23 uint32_t capability, expireType; |
|
24 int64_t expireTime; |
|
25 uint32_t appId; |
|
26 bool isInBrowserElement; |
|
27 |
|
28 Permission() { } |
|
29 Permission(const nsCString& aHost, |
|
30 const uint32_t aAppId, |
|
31 const bool aIsInBrowserElement, |
|
32 const nsCString& aType, |
|
33 const uint32_t aCapability, |
|
34 const uint32_t aExpireType, |
|
35 const int64_t aExpireTime) : host(aHost), |
|
36 type(aType), |
|
37 capability(aCapability), |
|
38 expireType(aExpireType), |
|
39 expireTime(aExpireTime), |
|
40 appId(aAppId), |
|
41 isInBrowserElement(aIsInBrowserElement) |
|
42 {} |
|
43 }; |
|
44 |
|
45 template<> |
|
46 struct ParamTraits<Permission> |
|
47 { |
|
48 static void Write(Message* aMsg, const Permission& aParam) |
|
49 { |
|
50 WriteParam(aMsg, aParam.host); |
|
51 WriteParam(aMsg, aParam.type); |
|
52 WriteParam(aMsg, aParam.capability); |
|
53 WriteParam(aMsg, aParam.expireType); |
|
54 WriteParam(aMsg, aParam.expireTime); |
|
55 WriteParam(aMsg, aParam.appId); |
|
56 WriteParam(aMsg, aParam.isInBrowserElement); |
|
57 } |
|
58 |
|
59 static bool Read(const Message* aMsg, void** aIter, Permission* aResult) |
|
60 { |
|
61 return ReadParam(aMsg, aIter, &aResult->host) && |
|
62 ReadParam(aMsg, aIter, &aResult->type) && |
|
63 ReadParam(aMsg, aIter, &aResult->capability) && |
|
64 ReadParam(aMsg, aIter, &aResult->expireType) && |
|
65 ReadParam(aMsg, aIter, &aResult->expireTime) && |
|
66 ReadParam(aMsg, aIter, &aResult->appId) && |
|
67 ReadParam(aMsg, aIter, &aResult->isInBrowserElement); |
|
68 } |
|
69 |
|
70 static void Log(const Permission& p, std::wstring* l) |
|
71 { |
|
72 l->append(L"("); |
|
73 LogParam(p.host, l); |
|
74 l->append(L", "); |
|
75 LogParam(p.appId, l); |
|
76 l->append(L", "); |
|
77 LogParam(p.isInBrowserElement, l); |
|
78 l->append(L", "); |
|
79 LogParam(p.capability, l); |
|
80 l->append(L", "); |
|
81 LogParam(p.expireTime, l); |
|
82 l->append(L", "); |
|
83 LogParam(p.expireType, l); |
|
84 l->append(L")"); |
|
85 } |
|
86 }; |
|
87 |
|
88 template<> |
|
89 struct ParamTraits<mozilla::net::NetAddr> |
|
90 { |
|
91 static void Write(Message* aMsg, const mozilla::net::NetAddr &aParam) |
|
92 { |
|
93 WriteParam(aMsg, aParam.raw.family); |
|
94 if (aParam.raw.family == AF_UNSPEC) { |
|
95 aMsg->WriteBytes(aParam.raw.data, sizeof(aParam.raw.data)); |
|
96 } else if (aParam.raw.family == AF_INET) { |
|
97 WriteParam(aMsg, aParam.inet.port); |
|
98 WriteParam(aMsg, aParam.inet.ip); |
|
99 } else if (aParam.raw.family == AF_INET6) { |
|
100 WriteParam(aMsg, aParam.inet6.port); |
|
101 WriteParam(aMsg, aParam.inet6.flowinfo); |
|
102 WriteParam(aMsg, aParam.inet6.ip.u64[0]); |
|
103 WriteParam(aMsg, aParam.inet6.ip.u64[1]); |
|
104 WriteParam(aMsg, aParam.inet6.scope_id); |
|
105 #if defined(XP_UNIX) |
|
106 } else if (aParam.raw.family == AF_LOCAL) { |
|
107 // Train's already off the rails: let's get a stack trace at least... |
|
108 NS_RUNTIMEABORT("Error: please post stack trace to " |
|
109 "https://bugzilla.mozilla.org/show_bug.cgi?id=661158"); |
|
110 aMsg->WriteBytes(aParam.local.path, sizeof(aParam.local.path)); |
|
111 #endif |
|
112 } |
|
113 |
|
114 /* If we get here without hitting any of the cases above, there's not much |
|
115 * we can do but let the deserializer fail when it gets this message */ |
|
116 } |
|
117 |
|
118 static bool Read(const Message* aMsg, void** aIter, mozilla::net::NetAddr* aResult) |
|
119 { |
|
120 if (!ReadParam(aMsg, aIter, &aResult->raw.family)) |
|
121 return false; |
|
122 |
|
123 if (aResult->raw.family == AF_UNSPEC) { |
|
124 return aMsg->ReadBytes(aIter, |
|
125 reinterpret_cast<const char**>(&aResult->raw.data), |
|
126 sizeof(aResult->raw.data)); |
|
127 } else if (aResult->raw.family == AF_INET) { |
|
128 return ReadParam(aMsg, aIter, &aResult->inet.port) && |
|
129 ReadParam(aMsg, aIter, &aResult->inet.ip); |
|
130 } else if (aResult->raw.family == AF_INET6) { |
|
131 return ReadParam(aMsg, aIter, &aResult->inet6.port) && |
|
132 ReadParam(aMsg, aIter, &aResult->inet6.flowinfo) && |
|
133 ReadParam(aMsg, aIter, &aResult->inet6.ip.u64[0]) && |
|
134 ReadParam(aMsg, aIter, &aResult->inet6.ip.u64[1]) && |
|
135 ReadParam(aMsg, aIter, &aResult->inet6.scope_id); |
|
136 #if defined(XP_UNIX) |
|
137 } else if (aResult->raw.family == AF_LOCAL) { |
|
138 return aMsg->ReadBytes(aIter, |
|
139 reinterpret_cast<const char**>(&aResult->local.path), |
|
140 sizeof(aResult->local.path)); |
|
141 #endif |
|
142 } |
|
143 |
|
144 /* We've been tricked by some socket family we don't know about! */ |
|
145 return false; |
|
146 } |
|
147 }; |
|
148 |
|
149 } |
|
150 |
|
151 #endif // mozilla_net_NeckoMessageUtils_h |