/usr/include/thunderbird/ipc/IPCMessageUtils.h is in thunderbird-dev 1:38.6.0+build1-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set sw=2 ts=8 et tw=80 : */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef __IPC_GLUE_IPCMESSAGEUTILS_H__
#define __IPC_GLUE_IPCMESSAGEUTILS_H__
#include "base/process_util.h"
#include "chrome/common/ipc_message_utils.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/TimeStamp.h"
#ifdef XP_WIN
#include "mozilla/TimeStamp_windows.h"
#endif
#include "mozilla/TypeTraits.h"
#include "mozilla/IntegerTypeTraits.h"
#include <stdint.h>
#include "nsID.h"
#include "nsMemory.h"
#include "nsString.h"
#include "nsTArray.h"
#include "js/StructuredClone.h"
#include "nsCSSProperty.h"
#ifdef _MSC_VER
#pragma warning( disable : 4800 )
#endif
#if !defined(OS_POSIX)
// This condition must be kept in sync with the one in
// ipc_message_utils.h, but this dummy definition of
// base::FileDescriptor acts as a static assert that we only get one
// def or the other (or neither, in which case code using
// FileDescriptor fails to build)
namespace base { struct FileDescriptor { }; }
#endif
namespace mozilla {
// This is a cross-platform approximation to HANDLE, which we expect
// to be typedef'd to void* or thereabouts.
typedef uintptr_t WindowsHandle;
// XXX there are out of place and might be generally useful. Could
// move to nscore.h or something.
struct void_t {
bool operator==(const void_t&) const { return true; }
};
struct null_t {
bool operator==(const null_t&) const { return true; }
};
struct SerializedStructuredCloneBuffer
{
SerializedStructuredCloneBuffer()
: data(nullptr), dataLength(0)
{ }
explicit SerializedStructuredCloneBuffer(const JSAutoStructuredCloneBuffer& aOther)
{
*this = aOther;
}
bool
operator==(const SerializedStructuredCloneBuffer& aOther) const
{
return this->data == aOther.data &&
this->dataLength == aOther.dataLength;
}
SerializedStructuredCloneBuffer&
operator=(const JSAutoStructuredCloneBuffer& aOther)
{
data = aOther.data();
dataLength = aOther.nbytes();
return *this;
}
uint64_t* data;
size_t dataLength;
};
} // namespace mozilla
namespace IPC {
/**
* Generic enum serializer.
*
* Consider using the specializations below, such as ContiguousEnumSerializer.
*
* This is a generic serializer for any enum type used in IPDL.
* Programmers can define ParamTraits<E> for enum type E by deriving
* EnumSerializer<E, MyEnumValidator> where MyEnumValidator is a struct
* that has to define a static IsLegalValue function returning whether
* a given value is a legal value of the enum type at hand.
*
* \sa https://developer.mozilla.org/en/IPDL/Type_Serialization
*/
template <typename E, typename EnumValidator>
struct EnumSerializer {
typedef E paramType;
typedef typename mozilla::UnsignedStdintTypeForSize<sizeof(paramType)>::Type
uintParamType;
static void Write(Message* aMsg, const paramType& aValue) {
MOZ_ASSERT(EnumValidator::IsLegalValue(aValue));
WriteParam(aMsg, uintParamType(aValue));
}
static bool Read(const Message* aMsg, void** aIter, paramType* aResult) {
uintParamType value;
if(!ReadParam(aMsg, aIter, &value) ||
!EnumValidator::IsLegalValue(paramType(value))) {
return false;
}
*aResult = paramType(value);
return true;
}
};
template <typename E,
E MinLegal,
E HighBound>
class ContiguousEnumValidator
{
// Silence overzealous -Wtype-limits bug in GCC fixed in GCC 4.8:
// "comparison of unsigned expression >= 0 is always true"
// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11856
template <typename T>
static bool IsLessThanOrEqual(T a, T b) { return a <= b; }
public:
static bool IsLegalValue(E e)
{
return IsLessThanOrEqual(MinLegal, e) && e < HighBound;
}
};
template <typename E,
E AllBits>
struct BitFlagsEnumValidator
{
static bool IsLegalValue(E e)
{
return (e & AllBits) == e;
}
};
/**
* Specialization of EnumSerializer for enums with contiguous enum values.
*
* Provide two values: MinLegal, HighBound. An enum value x will be
* considered legal if MinLegal <= x < HighBound.
*
* For example, following is definition of serializer for enum type FOO.
* \code
* enum FOO { FOO_FIRST, FOO_SECOND, FOO_LAST, NUM_FOO };
*
* template <>
* struct ParamTraits<FOO>:
* public ContiguousEnumSerializer<FOO, FOO_FIRST, NUM_FOO> {};
* \endcode
* FOO_FIRST, FOO_SECOND, and FOO_LAST are valid value.
*/
template <typename E,
E MinLegal,
E HighBound>
struct ContiguousEnumSerializer
: EnumSerializer<E,
ContiguousEnumValidator<E, MinLegal, HighBound>>
{};
/**
* Specialization of EnumSerializer for enums representing bit flags.
*
* Provide one value: AllBits. An enum value x will be
* considered legal if (x & AllBits) == x;
*
* Example:
* \code
* enum FOO {
* FOO_FIRST = 1 << 0,
* FOO_SECOND = 1 << 1,
* FOO_LAST = 1 << 2,
* ALL_BITS = (1 << 3) - 1
* };
*
* template <>
* struct ParamTraits<FOO>:
* public BitFlagsEnumSerializer<FOO, FOO::ALL_BITS> {};
* \endcode
*/
template <typename E,
E AllBits>
struct BitFlagsEnumSerializer
: EnumSerializer<E,
BitFlagsEnumValidator<E, AllBits>>
{};
template <>
struct ParamTraits<base::ChildPrivileges>
: public ContiguousEnumSerializer<base::ChildPrivileges,
base::PRIVILEGES_DEFAULT,
base::PRIVILEGES_LAST>
{ };
template<>
struct ParamTraits<int8_t>
{
typedef int8_t paramType;
static void Write(Message* aMsg, const paramType& aParam)
{
aMsg->WriteBytes(&aParam, sizeof(aParam));
}
static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
{
const char* outp;
if (!aMsg->ReadBytes(aIter, &outp, sizeof(*aResult)))
return false;
*aResult = *reinterpret_cast<const paramType*>(outp);
return true;
}
};
template<>
struct ParamTraits<uint8_t>
{
typedef uint8_t paramType;
static void Write(Message* aMsg, const paramType& aParam)
{
aMsg->WriteBytes(&aParam, sizeof(aParam));
}
static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
{
const char* outp;
if (!aMsg->ReadBytes(aIter, &outp, sizeof(*aResult)))
return false;
*aResult = *reinterpret_cast<const paramType*>(outp);
return true;
}
};
#if !defined(OS_POSIX)
// See above re: keeping definitions in sync
template<>
struct ParamTraits<base::FileDescriptor>
{
typedef base::FileDescriptor paramType;
static void Write(Message* aMsg, const paramType& aParam) {
NS_RUNTIMEABORT("FileDescriptor isn't meaningful on this platform");
}
static bool Read(const Message* aMsg, void** aIter, paramType* aResult) {
NS_RUNTIMEABORT("FileDescriptor isn't meaningful on this platform");
return false;
}
};
#endif // !defined(OS_POSIX)
template <>
struct ParamTraits<nsACString>
{
typedef nsACString paramType;
static void Write(Message* aMsg, const paramType& aParam)
{
bool isVoid = aParam.IsVoid();
aMsg->WriteBool(isVoid);
if (isVoid)
// represents a nullptr pointer
return;
uint32_t length = aParam.Length();
WriteParam(aMsg, length);
aMsg->WriteBytes(aParam.BeginReading(), length);
}
static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
{
bool isVoid;
if (!aMsg->ReadBool(aIter, &isVoid))
return false;
if (isVoid) {
aResult->SetIsVoid(true);
return true;
}
uint32_t length;
if (ReadParam(aMsg, aIter, &length)) {
const char* buf;
if (aMsg->ReadBytes(aIter, &buf, length)) {
aResult->Assign(buf, length);
return true;
}
}
return false;
}
static void Log(const paramType& aParam, std::wstring* aLog)
{
if (aParam.IsVoid())
aLog->append(L"(NULL)");
else
aLog->append(UTF8ToWide(aParam.BeginReading()));
}
};
template <>
struct ParamTraits<nsAString>
{
typedef nsAString paramType;
static void Write(Message* aMsg, const paramType& aParam)
{
bool isVoid = aParam.IsVoid();
aMsg->WriteBool(isVoid);
if (isVoid)
// represents a nullptr pointer
return;
uint32_t length = aParam.Length();
WriteParam(aMsg, length);
aMsg->WriteBytes(aParam.BeginReading(), length * sizeof(char16_t));
}
static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
{
bool isVoid;
if (!aMsg->ReadBool(aIter, &isVoid))
return false;
if (isVoid) {
aResult->SetIsVoid(true);
return true;
}
uint32_t length;
if (ReadParam(aMsg, aIter, &length)) {
const char16_t* buf;
if (aMsg->ReadBytes(aIter, reinterpret_cast<const char**>(&buf),
length * sizeof(char16_t))) {
aResult->Assign(buf, length);
return true;
}
}
return false;
}
static void Log(const paramType& aParam, std::wstring* aLog)
{
if (aParam.IsVoid())
aLog->append(L"(NULL)");
else {
#ifdef WCHAR_T_IS_UTF16
aLog->append(reinterpret_cast<const wchar_t*>(aParam.BeginReading()));
#else
uint32_t length = aParam.Length();
for (uint32_t index = 0; index < length; index++) {
aLog->push_back(std::wstring::value_type(aParam[index]));
}
#endif
}
}
};
template <>
struct ParamTraits<nsCString> : ParamTraits<nsACString>
{
typedef nsCString paramType;
};
template <>
struct ParamTraits<nsLiteralCString> : ParamTraits<nsACString>
{
typedef nsLiteralCString paramType;
};
#ifdef MOZILLA_INTERNAL_API
template<>
struct ParamTraits<nsAutoCString> : ParamTraits<nsCString>
{
typedef nsAutoCString paramType;
};
#endif // MOZILLA_INTERNAL_API
template <>
struct ParamTraits<nsString> : ParamTraits<nsAString>
{
typedef nsString paramType;
};
template <>
struct ParamTraits<nsLiteralString> : ParamTraits<nsAString>
{
typedef nsLiteralString paramType;
};
template <typename E>
struct ParamTraits<FallibleTArray<E> >
{
typedef FallibleTArray<E> paramType;
// We write arrays of integer or floating-point data using a single pickling
// call, rather than writing each element individually. We deliberately do
// not use mozilla::IsPod here because it is perfectly reasonable to have
// a data structure T for which IsPod<T>::value is true, yet also have a
// ParamTraits<T> specialization.
static const bool sUseWriteBytes = (mozilla::IsIntegral<E>::value ||
mozilla::IsFloatingPoint<E>::value);
// Compute the byte length for |aNumElements| of type E. If that length
// would overflow an int, return false. Otherwise, return true and place
// the byte length in |aTotalLength|.
//
// Pickle's ReadBytes/WriteBytes interface takes lengths in ints, hence this
// dance.
static bool ByteLengthIsValid(size_t aNumElements, int* aTotalLength) {
static_assert(sizeof(int) == sizeof(int32_t), "int is an unexpected size!");
// nsTArray only handles sizes up to INT32_MAX.
if (aNumElements > size_t(INT32_MAX)) {
return false;
}
int64_t numBytes = static_cast<int64_t>(aNumElements) * sizeof(E);
if (numBytes > int64_t(INT32_MAX)) {
return false;
}
*aTotalLength = static_cast<int>(numBytes);
return true;
}
static void Write(Message* aMsg, const paramType& aParam)
{
uint32_t length = aParam.Length();
WriteParam(aMsg, length);
if (sUseWriteBytes) {
int pickledLength = 0;
mozilla::DebugOnly<bool> valid = ByteLengthIsValid(length, &pickledLength);
MOZ_ASSERT(valid);
aMsg->WriteBytes(aParam.Elements(), pickledLength);
} else {
for (uint32_t index = 0; index < length; index++) {
WriteParam(aMsg, aParam[index]);
}
}
}
static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
{
uint32_t length;
if (!ReadParam(aMsg, aIter, &length)) {
return false;
}
if (sUseWriteBytes) {
int pickledLength = 0;
if (!ByteLengthIsValid(length, &pickledLength)) {
return false;
}
const char* outdata;
if (!aMsg->ReadBytes(aIter, &outdata, pickledLength)) {
return false;
}
E* elements = aResult->AppendElements(length);
if (!elements) {
return false;
}
memcpy(elements, outdata, pickledLength);
} else {
if (!aResult->SetCapacity(length)) {
return false;
}
for (uint32_t index = 0; index < length; index++) {
E* element = aResult->AppendElement();
MOZ_ASSERT(element);
if (!ReadParam(aMsg, aIter, element)) {
return false;
}
}
}
return true;
}
static void Log(const paramType& aParam, std::wstring* aLog)
{
for (uint32_t index = 0; index < aParam.Length(); index++) {
if (index) {
aLog->append(L" ");
}
LogParam(aParam[index], aLog);
}
}
};
template<typename E>
struct ParamTraits<InfallibleTArray<E> >
{
typedef InfallibleTArray<E> paramType;
static void Write(Message* aMsg, const paramType& aParam)
{
WriteParam(aMsg, static_cast<const FallibleTArray<E>&>(aParam));
}
// deserialize the array fallibly, but return an InfallibleTArray
static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
{
FallibleTArray<E> temp;
if (!ReadParam(aMsg, aIter, &temp))
return false;
aResult->SwapElements(temp);
return true;
}
static void Log(const paramType& aParam, std::wstring* aLog)
{
LogParam(static_cast<const FallibleTArray<E>&>(aParam), aLog);
}
};
template<typename E, size_t N>
struct ParamTraits<nsAutoTArray<E, N>> : ParamTraits<nsTArray<E>>
{
typedef nsAutoTArray<E, N> paramType;
};
template<>
struct ParamTraits<float>
{
typedef float paramType;
static void Write(Message* aMsg, const paramType& aParam)
{
aMsg->WriteBytes(&aParam, sizeof(paramType));
}
static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
{
const char* outFloat;
if (!aMsg->ReadBytes(aIter, &outFloat, sizeof(float)))
return false;
*aResult = *reinterpret_cast<const float*>(outFloat);
return true;
}
static void Log(const paramType& aParam, std::wstring* aLog)
{
aLog->append(StringPrintf(L"%g", aParam));
}
};
template <>
struct ParamTraits<nsCSSProperty>
: public ContiguousEnumSerializer<nsCSSProperty,
eCSSProperty_UNKNOWN,
eCSSProperty_COUNT>
{};
template<>
struct ParamTraits<mozilla::void_t>
{
typedef mozilla::void_t paramType;
static void Write(Message* aMsg, const paramType& aParam) { }
static bool
Read(const Message* aMsg, void** aIter, paramType* aResult)
{
*aResult = paramType();
return true;
}
};
template<>
struct ParamTraits<mozilla::null_t>
{
typedef mozilla::null_t paramType;
static void Write(Message* aMsg, const paramType& aParam) { }
static bool
Read(const Message* aMsg, void** aIter, paramType* aResult)
{
*aResult = paramType();
return true;
}
};
template<>
struct ParamTraits<nsID>
{
typedef nsID paramType;
static void Write(Message* aMsg, const paramType& aParam)
{
WriteParam(aMsg, aParam.m0);
WriteParam(aMsg, aParam.m1);
WriteParam(aMsg, aParam.m2);
for (unsigned int i = 0; i < mozilla::ArrayLength(aParam.m3); i++) {
WriteParam(aMsg, aParam.m3[i]);
}
}
static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
{
if(!ReadParam(aMsg, aIter, &(aResult->m0)) ||
!ReadParam(aMsg, aIter, &(aResult->m1)) ||
!ReadParam(aMsg, aIter, &(aResult->m2)))
return false;
for (unsigned int i = 0; i < mozilla::ArrayLength(aResult->m3); i++)
if (!ReadParam(aMsg, aIter, &(aResult->m3[i])))
return false;
return true;
}
static void Log(const paramType& aParam, std::wstring* aLog)
{
aLog->append(L"{");
aLog->append(StringPrintf(L"%8.8X-%4.4X-%4.4X-",
aParam.m0,
aParam.m1,
aParam.m2));
for (unsigned int i = 0; i < mozilla::ArrayLength(aParam.m3); i++)
aLog->append(StringPrintf(L"%2.2X", aParam.m3[i]));
aLog->append(L"}");
}
};
template<>
struct ParamTraits<mozilla::TimeDuration>
{
typedef mozilla::TimeDuration paramType;
static void Write(Message* aMsg, const paramType& aParam)
{
WriteParam(aMsg, aParam.mValue);
}
static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
{
return ReadParam(aMsg, aIter, &aResult->mValue);
};
};
template<>
struct ParamTraits<mozilla::TimeStamp>
{
typedef mozilla::TimeStamp paramType;
static void Write(Message* aMsg, const paramType& aParam)
{
WriteParam(aMsg, aParam.mValue);
}
static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
{
return ReadParam(aMsg, aIter, &aResult->mValue);
};
};
#ifdef XP_WIN
template<>
struct ParamTraits<mozilla::TimeStampValue>
{
typedef mozilla::TimeStampValue paramType;
static void Write(Message* aMsg, const paramType& aParam)
{
WriteParam(aMsg, aParam.mGTC);
WriteParam(aMsg, aParam.mQPC);
WriteParam(aMsg, aParam.mHasQPC);
WriteParam(aMsg, aParam.mIsNull);
}
static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
{
return (ReadParam(aMsg, aIter, &aResult->mGTC) &&
ReadParam(aMsg, aIter, &aResult->mQPC) &&
ReadParam(aMsg, aIter, &aResult->mHasQPC) &&
ReadParam(aMsg, aIter, &aResult->mIsNull));
}
};
#endif
template <>
struct ParamTraits<mozilla::SerializedStructuredCloneBuffer>
{
typedef mozilla::SerializedStructuredCloneBuffer paramType;
static void Write(Message* aMsg, const paramType& aParam)
{
WriteParam(aMsg, aParam.dataLength);
if (aParam.dataLength) {
// Structured clone data must be 64-bit aligned.
aMsg->WriteBytes(aParam.data, aParam.dataLength, sizeof(uint64_t));
}
}
static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
{
if (!ReadParam(aMsg, aIter, &aResult->dataLength)) {
return false;
}
if (aResult->dataLength) {
const char** buffer =
const_cast<const char**>(reinterpret_cast<char**>(&aResult->data));
// Structured clone data must be 64-bit aligned.
if (!aMsg->ReadBytes(aIter, buffer, aResult->dataLength,
sizeof(uint64_t))) {
return false;
}
} else {
aResult->data = nullptr;
}
return true;
}
static void Log(const paramType& aParam, std::wstring* aLog)
{
LogParam(aParam.dataLength, aLog);
}
};
} /* namespace IPC */
#endif /* __IPC_GLUE_IPCMESSAGEUTILS_H__ */
|