2009-09-22 02:02:15 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set sw=2 ts=8 et tw=80 : */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* 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/. */
|
2009-06-29 18:38:29 +00:00
|
|
|
|
|
|
|
#ifndef __IPC_GLUE_IPCMESSAGEUTILS_H__
|
|
|
|
#define __IPC_GLUE_IPCMESSAGEUTILS_H__
|
|
|
|
|
2013-01-17 20:06:36 +00:00
|
|
|
#include "base/process_util.h"
|
2009-06-29 18:38:29 +00:00
|
|
|
#include "chrome/common/ipc_message_utils.h"
|
|
|
|
|
2013-12-09 02:52:54 +00:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2014-11-04 13:57:25 +00:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2012-07-31 17:28:20 +00:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2013-02-11 21:56:58 +00:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#include "mozilla/TimeStamp_windows.h"
|
|
|
|
#endif
|
2014-11-04 13:57:25 +00:00
|
|
|
#include "mozilla/TypeTraits.h"
|
2014-04-14 14:17:40 +00:00
|
|
|
#include "mozilla/IntegerTypeTraits.h"
|
2013-07-30 14:25:31 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2011-10-11 05:50:08 +00:00
|
|
|
|
2011-06-24 14:19:56 +00:00
|
|
|
#include "nsID.h"
|
2015-04-14 15:36:36 +00:00
|
|
|
#include "nsIWidget.h"
|
2011-06-24 14:19:56 +00:00
|
|
|
#include "nsMemory.h"
|
2013-09-23 17:25:00 +00:00
|
|
|
#include "nsString.h"
|
2009-06-29 18:38:29 +00:00
|
|
|
#include "nsTArray.h"
|
2013-08-20 06:43:47 +00:00
|
|
|
#include "js/StructuredClone.h"
|
2012-08-20 13:35:41 +00:00
|
|
|
#include "nsCSSProperty.h"
|
2009-06-29 18:38:29 +00:00
|
|
|
|
2010-01-22 15:06:13 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma warning( disable : 4800 )
|
|
|
|
#endif
|
|
|
|
|
2010-11-09 02:48:59 +00:00
|
|
|
#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)
|
2012-01-18 02:01:51 +00:00
|
|
|
namespace base { struct FileDescriptor { }; }
|
2010-11-09 02:48:59 +00:00
|
|
|
#endif
|
2010-05-22 19:35:40 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2011-07-22 00:49:35 +00:00
|
|
|
// This is a cross-platform approximation to HANDLE, which we expect
|
|
|
|
// to be typedef'd to void* or thereabouts.
|
|
|
|
typedef uintptr_t WindowsHandle;
|
|
|
|
|
2010-05-22 19:35:40 +00:00
|
|
|
// XXX there are out of place and might be generally useful. Could
|
|
|
|
// move to nscore.h or something.
|
2010-05-22 19:35:42 +00:00
|
|
|
struct void_t {
|
|
|
|
bool operator==(const void_t&) const { return true; }
|
|
|
|
};
|
|
|
|
struct null_t {
|
|
|
|
bool operator==(const null_t&) const { return true; }
|
|
|
|
};
|
2010-05-22 19:35:40 +00:00
|
|
|
|
2012-08-02 06:02:29 +00:00
|
|
|
struct SerializedStructuredCloneBuffer
|
|
|
|
{
|
|
|
|
SerializedStructuredCloneBuffer()
|
|
|
|
: data(nullptr), dataLength(0)
|
|
|
|
{ }
|
|
|
|
|
2014-07-28 17:17:51 +00:00
|
|
|
explicit SerializedStructuredCloneBuffer(const JSAutoStructuredCloneBuffer& aOther)
|
2012-08-02 06:02:29 +00:00
|
|
|
{
|
|
|
|
*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;
|
|
|
|
};
|
|
|
|
|
2010-05-22 19:35:40 +00:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2009-06-29 18:38:29 +00:00
|
|
|
namespace IPC {
|
|
|
|
|
2011-11-24 11:45:55 +00:00
|
|
|
/**
|
|
|
|
* Generic enum serializer.
|
2012-02-05 19:51:06 +00:00
|
|
|
*
|
2014-04-14 14:17:40 +00:00
|
|
|
* Consider using the specializations below, such as ContiguousEnumSerializer.
|
|
|
|
*
|
2012-02-05 19:51:06 +00:00
|
|
|
* This is a generic serializer for any enum type used in IPDL.
|
|
|
|
* Programmers can define ParamTraits<E> for enum type E by deriving
|
2014-04-14 14:17:40 +00:00
|
|
|
* 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.
|
2012-02-05 19:51:06 +00:00
|
|
|
*
|
|
|
|
* \sa https://developer.mozilla.org/en/IPDL/Type_Serialization
|
2011-11-24 11:45:55 +00:00
|
|
|
*/
|
2014-04-14 14:17:40 +00:00
|
|
|
template <typename E, typename EnumValidator>
|
2011-11-24 11:45:55 +00:00
|
|
|
struct EnumSerializer {
|
|
|
|
typedef E paramType;
|
2014-04-14 14:17:40 +00:00
|
|
|
typedef typename mozilla::UnsignedStdintTypeForSize<sizeof(paramType)>::Type
|
|
|
|
uintParamType;
|
2011-11-24 11:45:55 +00:00
|
|
|
|
|
|
|
static void Write(Message* aMsg, const paramType& aValue) {
|
2014-04-14 14:17:40 +00:00
|
|
|
MOZ_ASSERT(EnumValidator::IsLegalValue(aValue));
|
|
|
|
WriteParam(aMsg, uintParamType(aValue));
|
2011-11-24 11:45:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool Read(const Message* aMsg, void** aIter, paramType* aResult) {
|
2014-04-14 14:17:40 +00:00
|
|
|
uintParamType value;
|
2011-11-24 11:45:55 +00:00
|
|
|
if(!ReadParam(aMsg, aIter, &value) ||
|
2014-04-14 14:17:40 +00:00
|
|
|
!EnumValidator::IsLegalValue(paramType(value))) {
|
2011-11-24 11:45:55 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*aResult = paramType(value);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-01-10 19:06:12 +00:00
|
|
|
template <typename E,
|
2014-04-14 14:17:40 +00:00
|
|
|
E MinLegal,
|
|
|
|
E HighBound>
|
2015-01-25 22:22:08 +00:00
|
|
|
class ContiguousEnumValidator
|
2014-04-14 14:17:40 +00:00
|
|
|
{
|
|
|
|
// 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)
|
|
|
|
{
|
2015-01-25 22:22:08 +00:00
|
|
|
return IsLessThanOrEqual(MinLegal, e) && e < HighBound;
|
2014-01-10 19:06:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-26 02:34:04 +00:00
|
|
|
template <typename E,
|
|
|
|
E AllBits>
|
|
|
|
struct BitFlagsEnumValidator
|
|
|
|
{
|
|
|
|
static bool IsLegalValue(E e)
|
|
|
|
{
|
|
|
|
return (e & AllBits) == e;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-14 14:17:40 +00:00
|
|
|
/**
|
|
|
|
* 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>>
|
|
|
|
{};
|
|
|
|
|
2014-04-26 02:34:04 +00:00
|
|
|
/**
|
|
|
|
* 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>>
|
|
|
|
{};
|
|
|
|
|
2013-01-17 20:06:36 +00:00
|
|
|
template <>
|
|
|
|
struct ParamTraits<base::ChildPrivileges>
|
2014-04-14 14:17:40 +00:00
|
|
|
: public ContiguousEnumSerializer<base::ChildPrivileges,
|
|
|
|
base::PRIVILEGES_DEFAULT,
|
|
|
|
base::PRIVILEGES_LAST>
|
2013-01-17 20:06:36 +00:00
|
|
|
{ };
|
|
|
|
|
2010-01-25 22:47:39 +00:00
|
|
|
template<>
|
2012-08-22 15:56:38 +00:00
|
|
|
struct ParamTraits<int8_t>
|
2010-01-25 22:47:39 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
typedef int8_t paramType;
|
2010-01-25 22:47:39 +00:00
|
|
|
|
|
|
|
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<>
|
2012-08-22 15:56:38 +00:00
|
|
|
struct ParamTraits<uint8_t>
|
2010-01-25 22:47:39 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
typedef uint8_t paramType;
|
2010-01-25 22:47:39 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-11-09 02:48:59 +00:00
|
|
|
#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)
|
|
|
|
|
2009-06-29 18:38:29 +00:00
|
|
|
template <>
|
|
|
|
struct ParamTraits<nsACString>
|
|
|
|
{
|
|
|
|
typedef nsACString paramType;
|
|
|
|
|
|
|
|
static void Write(Message* aMsg, const paramType& aParam)
|
|
|
|
{
|
2009-09-22 02:02:15 +00:00
|
|
|
bool isVoid = aParam.IsVoid();
|
|
|
|
aMsg->WriteBool(isVoid);
|
|
|
|
|
|
|
|
if (isVoid)
|
2013-08-23 19:51:58 +00:00
|
|
|
// represents a nullptr pointer
|
2009-09-22 02:02:15 +00:00
|
|
|
return;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t length = aParam.Length();
|
2009-06-29 18:38:29 +00:00
|
|
|
WriteParam(aMsg, length);
|
|
|
|
aMsg->WriteBytes(aParam.BeginReading(), length);
|
|
|
|
}
|
|
|
|
|
2009-07-29 21:12:15 +00:00
|
|
|
static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
|
2009-06-29 18:38:29 +00:00
|
|
|
{
|
2009-09-22 02:02:15 +00:00
|
|
|
bool isVoid;
|
|
|
|
if (!aMsg->ReadBool(aIter, &isVoid))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (isVoid) {
|
2011-10-17 14:59:28 +00:00
|
|
|
aResult->SetIsVoid(true);
|
2009-09-22 02:02:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t length;
|
2009-06-29 18:38:29 +00:00
|
|
|
if (ReadParam(aMsg, aIter, &length)) {
|
|
|
|
const char* buf;
|
|
|
|
if (aMsg->ReadBytes(aIter, &buf, length)) {
|
2009-07-29 21:12:15 +00:00
|
|
|
aResult->Assign(buf, length);
|
2009-06-29 18:38:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Log(const paramType& aParam, std::wstring* aLog)
|
|
|
|
{
|
2009-09-22 02:02:15 +00:00
|
|
|
if (aParam.IsVoid())
|
|
|
|
aLog->append(L"(NULL)");
|
|
|
|
else
|
|
|
|
aLog->append(UTF8ToWide(aParam.BeginReading()));
|
2009-06-29 18:38:29 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct ParamTraits<nsAString>
|
|
|
|
{
|
|
|
|
typedef nsAString paramType;
|
|
|
|
|
|
|
|
static void Write(Message* aMsg, const paramType& aParam)
|
|
|
|
{
|
2009-09-22 02:02:15 +00:00
|
|
|
bool isVoid = aParam.IsVoid();
|
|
|
|
aMsg->WriteBool(isVoid);
|
|
|
|
|
|
|
|
if (isVoid)
|
2013-08-23 19:51:58 +00:00
|
|
|
// represents a nullptr pointer
|
2009-09-22 02:02:15 +00:00
|
|
|
return;
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t length = aParam.Length();
|
2009-06-29 18:38:29 +00:00
|
|
|
WriteParam(aMsg, length);
|
2014-01-04 15:02:17 +00:00
|
|
|
aMsg->WriteBytes(aParam.BeginReading(), length * sizeof(char16_t));
|
2009-06-29 18:38:29 +00:00
|
|
|
}
|
|
|
|
|
2009-07-29 21:12:15 +00:00
|
|
|
static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
|
2009-06-29 18:38:29 +00:00
|
|
|
{
|
2009-09-22 02:02:15 +00:00
|
|
|
bool isVoid;
|
|
|
|
if (!aMsg->ReadBool(aIter, &isVoid))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (isVoid) {
|
2011-10-17 14:59:28 +00:00
|
|
|
aResult->SetIsVoid(true);
|
2009-09-22 02:02:15 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t length;
|
2009-06-29 18:38:29 +00:00
|
|
|
if (ReadParam(aMsg, aIter, &length)) {
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t* buf;
|
2009-06-29 18:38:29 +00:00
|
|
|
if (aMsg->ReadBytes(aIter, reinterpret_cast<const char**>(&buf),
|
2014-01-04 15:02:17 +00:00
|
|
|
length * sizeof(char16_t))) {
|
2009-07-29 21:12:15 +00:00
|
|
|
aResult->Assign(buf, length);
|
2009-06-29 18:38:29 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Log(const paramType& aParam, std::wstring* aLog)
|
|
|
|
{
|
2009-09-22 02:02:15 +00:00
|
|
|
if (aParam.IsVoid())
|
|
|
|
aLog->append(L"(NULL)");
|
|
|
|
else {
|
2009-06-29 18:38:29 +00:00
|
|
|
#ifdef WCHAR_T_IS_UTF16
|
2009-09-22 02:02:15 +00:00
|
|
|
aLog->append(reinterpret_cast<const wchar_t*>(aParam.BeginReading()));
|
2009-06-29 18:38:29 +00:00
|
|
|
#else
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t length = aParam.Length();
|
|
|
|
for (uint32_t index = 0; index < length; index++) {
|
2009-09-22 02:02:15 +00:00
|
|
|
aLog->push_back(std::wstring::value_type(aParam[index]));
|
|
|
|
}
|
2009-06-29 18:38:29 +00:00
|
|
|
#endif
|
2009-09-22 02:02:15 +00:00
|
|
|
}
|
2009-06-29 18:38:29 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-07-29 21:12:15 +00:00
|
|
|
template <>
|
|
|
|
struct ParamTraits<nsCString> : ParamTraits<nsACString>
|
|
|
|
{
|
|
|
|
typedef nsCString paramType;
|
|
|
|
};
|
|
|
|
|
2014-01-08 20:51:38 +00:00
|
|
|
template <>
|
|
|
|
struct ParamTraits<nsLiteralCString> : ParamTraits<nsACString>
|
|
|
|
{
|
|
|
|
typedef nsLiteralCString paramType;
|
|
|
|
};
|
|
|
|
|
2010-03-23 15:14:36 +00:00
|
|
|
#ifdef MOZILLA_INTERNAL_API
|
|
|
|
|
|
|
|
template<>
|
2012-09-02 02:35:17 +00:00
|
|
|
struct ParamTraits<nsAutoCString> : ParamTraits<nsCString>
|
2010-03-23 15:14:36 +00:00
|
|
|
{
|
2012-09-02 02:35:17 +00:00
|
|
|
typedef nsAutoCString paramType;
|
2010-03-23 15:14:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // MOZILLA_INTERNAL_API
|
|
|
|
|
2009-07-29 21:12:15 +00:00
|
|
|
template <>
|
|
|
|
struct ParamTraits<nsString> : ParamTraits<nsAString>
|
|
|
|
{
|
|
|
|
typedef nsString paramType;
|
|
|
|
};
|
|
|
|
|
2014-01-08 20:51:38 +00:00
|
|
|
template <>
|
|
|
|
struct ParamTraits<nsLiteralString> : ParamTraits<nsAString>
|
|
|
|
{
|
|
|
|
typedef nsLiteralString paramType;
|
|
|
|
};
|
|
|
|
|
2012-12-19 01:16:06 +00:00
|
|
|
template <typename E>
|
|
|
|
struct ParamTraits<FallibleTArray<E> >
|
2009-06-29 18:38:29 +00:00
|
|
|
{
|
2012-12-19 01:16:06 +00:00
|
|
|
typedef FallibleTArray<E> paramType;
|
2009-06-29 18:38:29 +00:00
|
|
|
|
2014-11-04 13:57:25 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2009-06-29 18:38:29 +00:00
|
|
|
static void Write(Message* aMsg, const paramType& aParam)
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t length = aParam.Length();
|
2009-06-29 18:38:29 +00:00
|
|
|
WriteParam(aMsg, length);
|
2014-11-04 13:57:25 +00:00
|
|
|
|
|
|
|
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]);
|
|
|
|
}
|
2009-06-29 18:38:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-29 21:12:15 +00:00
|
|
|
static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
|
2009-06-29 18:38:29 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t length;
|
2009-06-29 18:38:29 +00:00
|
|
|
if (!ReadParam(aMsg, aIter, &length)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-04 13:57:25 +00:00
|
|
|
if (sUseWriteBytes) {
|
|
|
|
int pickledLength = 0;
|
|
|
|
if (!ByteLengthIsValid(length, &pickledLength)) {
|
2009-08-05 22:36:33 +00:00
|
|
|
return false;
|
2009-06-29 18:38:29 +00:00
|
|
|
}
|
2014-11-04 13:57:25 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2009-06-29 18:38:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Log(const paramType& aParam, std::wstring* aLog)
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t index = 0; index < aParam.Length(); index++) {
|
2009-06-29 18:38:29 +00:00
|
|
|
if (index) {
|
|
|
|
aLog->append(L" ");
|
|
|
|
}
|
|
|
|
LogParam(aParam[index], aLog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2010-11-09 02:49:00 +00:00
|
|
|
template<typename E>
|
2012-12-19 01:16:06 +00:00
|
|
|
struct ParamTraits<InfallibleTArray<E> >
|
2010-11-09 02:49:00 +00:00
|
|
|
{
|
|
|
|
typedef InfallibleTArray<E> paramType;
|
|
|
|
|
2012-12-19 01:16:06 +00:00
|
|
|
static void Write(Message* aMsg, const paramType& aParam)
|
|
|
|
{
|
|
|
|
WriteParam(aMsg, static_cast<const FallibleTArray<E>&>(aParam));
|
|
|
|
}
|
2010-11-09 02:49:00 +00:00
|
|
|
|
|
|
|
// deserialize the array fallibly, but return an InfallibleTArray
|
|
|
|
static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
|
|
|
|
{
|
2012-12-19 01:16:06 +00:00
|
|
|
FallibleTArray<E> temp;
|
2010-11-09 02:49:00 +00:00
|
|
|
if (!ReadParam(aMsg, aIter, &temp))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
aResult->SwapElements(temp);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-12-19 01:16:06 +00:00
|
|
|
static void Log(const paramType& aParam, std::wstring* aLog)
|
|
|
|
{
|
|
|
|
LogParam(static_cast<const FallibleTArray<E>&>(aParam), aLog);
|
|
|
|
}
|
2010-11-09 02:49:00 +00:00
|
|
|
};
|
|
|
|
|
2015-01-31 07:17:12 +00:00
|
|
|
template<typename E, size_t N>
|
|
|
|
struct ParamTraits<nsAutoTArray<E, N>> : ParamTraits<nsTArray<E>>
|
|
|
|
{
|
|
|
|
typedef nsAutoTArray<E, N> paramType;
|
|
|
|
};
|
|
|
|
|
2009-11-06 20:24:35 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-08-20 13:35:41 +00:00
|
|
|
template <>
|
|
|
|
struct ParamTraits<nsCSSProperty>
|
2014-04-14 14:17:40 +00:00
|
|
|
: public ContiguousEnumSerializer<nsCSSProperty,
|
|
|
|
eCSSProperty_UNKNOWN,
|
|
|
|
eCSSProperty_COUNT>
|
2012-08-20 13:35:41 +00:00
|
|
|
{};
|
|
|
|
|
2010-05-22 19:35:40 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-06-24 14:19:56 +00:00
|
|
|
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);
|
2011-10-11 05:50:08 +00:00
|
|
|
for (unsigned int i = 0; i < mozilla::ArrayLength(aParam.m3); i++) {
|
2011-06-24 14:19:56 +00:00
|
|
|
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;
|
|
|
|
|
2011-10-11 05:50:08 +00:00
|
|
|
for (unsigned int i = 0; i < mozilla::ArrayLength(aResult->m3); i++)
|
2011-06-24 14:19:56 +00:00
|
|
|
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));
|
2011-10-11 05:50:08 +00:00
|
|
|
for (unsigned int i = 0; i < mozilla::ArrayLength(aParam.m3); i++)
|
2011-06-24 14:19:56 +00:00
|
|
|
aLog->append(StringPrintf(L"%2.2X", aParam.m3[i]));
|
|
|
|
aLog->append(L"}");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-07-31 17:28:20 +00:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2013-02-11 21:56:58 +00:00
|
|
|
#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
|
|
|
|
|
2012-08-02 06:02:29 +00:00
|
|
|
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 {
|
2013-08-23 19:51:58 +00:00
|
|
|
aResult->data = nullptr;
|
2012-08-02 06:02:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Log(const paramType& aParam, std::wstring* aLog)
|
|
|
|
{
|
|
|
|
LogParam(aParam.dataLength, aLog);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-04-14 15:36:36 +00:00
|
|
|
template <>
|
|
|
|
struct ParamTraits<nsIWidget::TouchPointerState>
|
|
|
|
: public BitFlagsEnumSerializer<nsIWidget::TouchPointerState,
|
|
|
|
nsIWidget::TouchPointerState::ALL_BITS>
|
|
|
|
{
|
|
|
|
};
|
|
|
|
|
2009-06-29 18:38:29 +00:00
|
|
|
} /* namespace IPC */
|
|
|
|
|
|
|
|
#endif /* __IPC_GLUE_IPCMESSAGEUTILS_H__ */
|