1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/ipc/NeckoMessageUtils.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,151 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef mozilla_net_NeckoMessageUtils_h 1.10 +#define mozilla_net_NeckoMessageUtils_h 1.11 + 1.12 +#include "mozilla/DebugOnly.h" 1.13 + 1.14 +#include "ipc/IPCMessageUtils.h" 1.15 +#include "nsStringGlue.h" 1.16 +#include "prio.h" 1.17 +#include "mozilla/net/DNS.h" 1.18 + 1.19 +namespace IPC { 1.20 + 1.21 +// nsIPermissionManager utilities 1.22 + 1.23 +struct Permission 1.24 +{ 1.25 + nsCString host, type; 1.26 + uint32_t capability, expireType; 1.27 + int64_t expireTime; 1.28 + uint32_t appId; 1.29 + bool isInBrowserElement; 1.30 + 1.31 + Permission() { } 1.32 + Permission(const nsCString& aHost, 1.33 + const uint32_t aAppId, 1.34 + const bool aIsInBrowserElement, 1.35 + const nsCString& aType, 1.36 + const uint32_t aCapability, 1.37 + const uint32_t aExpireType, 1.38 + const int64_t aExpireTime) : host(aHost), 1.39 + type(aType), 1.40 + capability(aCapability), 1.41 + expireType(aExpireType), 1.42 + expireTime(aExpireTime), 1.43 + appId(aAppId), 1.44 + isInBrowserElement(aIsInBrowserElement) 1.45 + {} 1.46 +}; 1.47 + 1.48 +template<> 1.49 +struct ParamTraits<Permission> 1.50 +{ 1.51 + static void Write(Message* aMsg, const Permission& aParam) 1.52 + { 1.53 + WriteParam(aMsg, aParam.host); 1.54 + WriteParam(aMsg, aParam.type); 1.55 + WriteParam(aMsg, aParam.capability); 1.56 + WriteParam(aMsg, aParam.expireType); 1.57 + WriteParam(aMsg, aParam.expireTime); 1.58 + WriteParam(aMsg, aParam.appId); 1.59 + WriteParam(aMsg, aParam.isInBrowserElement); 1.60 + } 1.61 + 1.62 + static bool Read(const Message* aMsg, void** aIter, Permission* aResult) 1.63 + { 1.64 + return ReadParam(aMsg, aIter, &aResult->host) && 1.65 + ReadParam(aMsg, aIter, &aResult->type) && 1.66 + ReadParam(aMsg, aIter, &aResult->capability) && 1.67 + ReadParam(aMsg, aIter, &aResult->expireType) && 1.68 + ReadParam(aMsg, aIter, &aResult->expireTime) && 1.69 + ReadParam(aMsg, aIter, &aResult->appId) && 1.70 + ReadParam(aMsg, aIter, &aResult->isInBrowserElement); 1.71 + } 1.72 + 1.73 + static void Log(const Permission& p, std::wstring* l) 1.74 + { 1.75 + l->append(L"("); 1.76 + LogParam(p.host, l); 1.77 + l->append(L", "); 1.78 + LogParam(p.appId, l); 1.79 + l->append(L", "); 1.80 + LogParam(p.isInBrowserElement, l); 1.81 + l->append(L", "); 1.82 + LogParam(p.capability, l); 1.83 + l->append(L", "); 1.84 + LogParam(p.expireTime, l); 1.85 + l->append(L", "); 1.86 + LogParam(p.expireType, l); 1.87 + l->append(L")"); 1.88 + } 1.89 +}; 1.90 + 1.91 +template<> 1.92 +struct ParamTraits<mozilla::net::NetAddr> 1.93 +{ 1.94 + static void Write(Message* aMsg, const mozilla::net::NetAddr &aParam) 1.95 + { 1.96 + WriteParam(aMsg, aParam.raw.family); 1.97 + if (aParam.raw.family == AF_UNSPEC) { 1.98 + aMsg->WriteBytes(aParam.raw.data, sizeof(aParam.raw.data)); 1.99 + } else if (aParam.raw.family == AF_INET) { 1.100 + WriteParam(aMsg, aParam.inet.port); 1.101 + WriteParam(aMsg, aParam.inet.ip); 1.102 + } else if (aParam.raw.family == AF_INET6) { 1.103 + WriteParam(aMsg, aParam.inet6.port); 1.104 + WriteParam(aMsg, aParam.inet6.flowinfo); 1.105 + WriteParam(aMsg, aParam.inet6.ip.u64[0]); 1.106 + WriteParam(aMsg, aParam.inet6.ip.u64[1]); 1.107 + WriteParam(aMsg, aParam.inet6.scope_id); 1.108 +#if defined(XP_UNIX) 1.109 + } else if (aParam.raw.family == AF_LOCAL) { 1.110 + // Train's already off the rails: let's get a stack trace at least... 1.111 + NS_RUNTIMEABORT("Error: please post stack trace to " 1.112 + "https://bugzilla.mozilla.org/show_bug.cgi?id=661158"); 1.113 + aMsg->WriteBytes(aParam.local.path, sizeof(aParam.local.path)); 1.114 +#endif 1.115 + } 1.116 + 1.117 + /* If we get here without hitting any of the cases above, there's not much 1.118 + * we can do but let the deserializer fail when it gets this message */ 1.119 + } 1.120 + 1.121 + static bool Read(const Message* aMsg, void** aIter, mozilla::net::NetAddr* aResult) 1.122 + { 1.123 + if (!ReadParam(aMsg, aIter, &aResult->raw.family)) 1.124 + return false; 1.125 + 1.126 + if (aResult->raw.family == AF_UNSPEC) { 1.127 + return aMsg->ReadBytes(aIter, 1.128 + reinterpret_cast<const char**>(&aResult->raw.data), 1.129 + sizeof(aResult->raw.data)); 1.130 + } else if (aResult->raw.family == AF_INET) { 1.131 + return ReadParam(aMsg, aIter, &aResult->inet.port) && 1.132 + ReadParam(aMsg, aIter, &aResult->inet.ip); 1.133 + } else if (aResult->raw.family == AF_INET6) { 1.134 + return ReadParam(aMsg, aIter, &aResult->inet6.port) && 1.135 + ReadParam(aMsg, aIter, &aResult->inet6.flowinfo) && 1.136 + ReadParam(aMsg, aIter, &aResult->inet6.ip.u64[0]) && 1.137 + ReadParam(aMsg, aIter, &aResult->inet6.ip.u64[1]) && 1.138 + ReadParam(aMsg, aIter, &aResult->inet6.scope_id); 1.139 +#if defined(XP_UNIX) 1.140 + } else if (aResult->raw.family == AF_LOCAL) { 1.141 + return aMsg->ReadBytes(aIter, 1.142 + reinterpret_cast<const char**>(&aResult->local.path), 1.143 + sizeof(aResult->local.path)); 1.144 +#endif 1.145 + } 1.146 + 1.147 + /* We've been tricked by some socket family we don't know about! */ 1.148 + return false; 1.149 + } 1.150 +}; 1.151 + 1.152 +} 1.153 + 1.154 +#endif // mozilla_net_NeckoMessageUtils_h