NeckoMessageUtils.h 6.3 KB

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