netwerk/ipc/NeckoMessageUtils.h

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

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

mercurial