michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef mozilla_net_NeckoMessageUtils_h michael@0: #define mozilla_net_NeckoMessageUtils_h michael@0: michael@0: #include "mozilla/DebugOnly.h" michael@0: michael@0: #include "ipc/IPCMessageUtils.h" michael@0: #include "nsStringGlue.h" michael@0: #include "prio.h" michael@0: #include "mozilla/net/DNS.h" michael@0: michael@0: namespace IPC { michael@0: michael@0: // nsIPermissionManager utilities michael@0: michael@0: struct Permission michael@0: { michael@0: nsCString host, type; michael@0: uint32_t capability, expireType; michael@0: int64_t expireTime; michael@0: uint32_t appId; michael@0: bool isInBrowserElement; michael@0: michael@0: Permission() { } michael@0: Permission(const nsCString& aHost, michael@0: const uint32_t aAppId, michael@0: const bool aIsInBrowserElement, michael@0: const nsCString& aType, michael@0: const uint32_t aCapability, michael@0: const uint32_t aExpireType, michael@0: const int64_t aExpireTime) : host(aHost), michael@0: type(aType), michael@0: capability(aCapability), michael@0: expireType(aExpireType), michael@0: expireTime(aExpireTime), michael@0: appId(aAppId), michael@0: isInBrowserElement(aIsInBrowserElement) michael@0: {} michael@0: }; michael@0: michael@0: template<> michael@0: struct ParamTraits michael@0: { michael@0: static void Write(Message* aMsg, const Permission& aParam) michael@0: { michael@0: WriteParam(aMsg, aParam.host); michael@0: WriteParam(aMsg, aParam.type); michael@0: WriteParam(aMsg, aParam.capability); michael@0: WriteParam(aMsg, aParam.expireType); michael@0: WriteParam(aMsg, aParam.expireTime); michael@0: WriteParam(aMsg, aParam.appId); michael@0: WriteParam(aMsg, aParam.isInBrowserElement); michael@0: } michael@0: michael@0: static bool Read(const Message* aMsg, void** aIter, Permission* aResult) michael@0: { michael@0: return ReadParam(aMsg, aIter, &aResult->host) && michael@0: ReadParam(aMsg, aIter, &aResult->type) && michael@0: ReadParam(aMsg, aIter, &aResult->capability) && michael@0: ReadParam(aMsg, aIter, &aResult->expireType) && michael@0: ReadParam(aMsg, aIter, &aResult->expireTime) && michael@0: ReadParam(aMsg, aIter, &aResult->appId) && michael@0: ReadParam(aMsg, aIter, &aResult->isInBrowserElement); michael@0: } michael@0: michael@0: static void Log(const Permission& p, std::wstring* l) michael@0: { michael@0: l->append(L"("); michael@0: LogParam(p.host, l); michael@0: l->append(L", "); michael@0: LogParam(p.appId, l); michael@0: l->append(L", "); michael@0: LogParam(p.isInBrowserElement, l); michael@0: l->append(L", "); michael@0: LogParam(p.capability, l); michael@0: l->append(L", "); michael@0: LogParam(p.expireTime, l); michael@0: l->append(L", "); michael@0: LogParam(p.expireType, l); michael@0: l->append(L")"); michael@0: } michael@0: }; michael@0: michael@0: template<> michael@0: struct ParamTraits michael@0: { michael@0: static void Write(Message* aMsg, const mozilla::net::NetAddr &aParam) michael@0: { michael@0: WriteParam(aMsg, aParam.raw.family); michael@0: if (aParam.raw.family == AF_UNSPEC) { michael@0: aMsg->WriteBytes(aParam.raw.data, sizeof(aParam.raw.data)); michael@0: } else if (aParam.raw.family == AF_INET) { michael@0: WriteParam(aMsg, aParam.inet.port); michael@0: WriteParam(aMsg, aParam.inet.ip); michael@0: } else if (aParam.raw.family == AF_INET6) { michael@0: WriteParam(aMsg, aParam.inet6.port); michael@0: WriteParam(aMsg, aParam.inet6.flowinfo); michael@0: WriteParam(aMsg, aParam.inet6.ip.u64[0]); michael@0: WriteParam(aMsg, aParam.inet6.ip.u64[1]); michael@0: WriteParam(aMsg, aParam.inet6.scope_id); michael@0: #if defined(XP_UNIX) michael@0: } else if (aParam.raw.family == AF_LOCAL) { michael@0: // Train's already off the rails: let's get a stack trace at least... michael@0: NS_RUNTIMEABORT("Error: please post stack trace to " michael@0: "https://bugzilla.mozilla.org/show_bug.cgi?id=661158"); michael@0: aMsg->WriteBytes(aParam.local.path, sizeof(aParam.local.path)); michael@0: #endif michael@0: } michael@0: michael@0: /* If we get here without hitting any of the cases above, there's not much michael@0: * we can do but let the deserializer fail when it gets this message */ michael@0: } michael@0: michael@0: static bool Read(const Message* aMsg, void** aIter, mozilla::net::NetAddr* aResult) michael@0: { michael@0: if (!ReadParam(aMsg, aIter, &aResult->raw.family)) michael@0: return false; michael@0: michael@0: if (aResult->raw.family == AF_UNSPEC) { michael@0: return aMsg->ReadBytes(aIter, michael@0: reinterpret_cast(&aResult->raw.data), michael@0: sizeof(aResult->raw.data)); michael@0: } else if (aResult->raw.family == AF_INET) { michael@0: return ReadParam(aMsg, aIter, &aResult->inet.port) && michael@0: ReadParam(aMsg, aIter, &aResult->inet.ip); michael@0: } else if (aResult->raw.family == AF_INET6) { michael@0: return ReadParam(aMsg, aIter, &aResult->inet6.port) && michael@0: ReadParam(aMsg, aIter, &aResult->inet6.flowinfo) && michael@0: ReadParam(aMsg, aIter, &aResult->inet6.ip.u64[0]) && michael@0: ReadParam(aMsg, aIter, &aResult->inet6.ip.u64[1]) && michael@0: ReadParam(aMsg, aIter, &aResult->inet6.scope_id); michael@0: #if defined(XP_UNIX) michael@0: } else if (aResult->raw.family == AF_LOCAL) { michael@0: return aMsg->ReadBytes(aIter, michael@0: reinterpret_cast(&aResult->local.path), michael@0: sizeof(aResult->local.path)); michael@0: #endif michael@0: } michael@0: michael@0: /* We've been tricked by some socket family we don't know about! */ michael@0: return false; michael@0: } michael@0: }; michael@0: michael@0: } michael@0: michael@0: #endif // mozilla_net_NeckoMessageUtils_h