Bug 1677466 - Move ParamTraits specializations with extra dependencies out of IPCMessageUtils.h. r=mccr8

This moves parts of IPCMessageUtils.h to two new header files and adapts
the include directives as necessary. The new header files are:
- EnumSerializer.h, which defines the templates for enum serializers
- IPCMessageUtilsSpecializations.h, which defines template specializations
  of ParamTraits with extra dependencies (building upon both IPCMessageUtils.h
  and EnumSerializer.h)

This should minimize the dependencies pulled in by every consumer of
IPCMessageUtils.h

Differential Revision: https://phabricator.services.mozilla.com/D94459
This commit is contained in:
Simon Giesecke 2020-12-10 11:09:21 +00:00
Родитель 92347a9513
Коммит 4cab6ac723
67 изменённых файлов: 1303 добавлений и 1153 удалений

Просмотреть файл

@ -9,7 +9,7 @@
#ifdef ACCESSIBILITY
# include "mozilla/a11y/Role.h"
# include "ipc/IPCMessageUtils.h"
# include "ipc/EnumSerializer.h"
namespace IPC {

Просмотреть файл

@ -9,6 +9,7 @@
#include "base/basictypes.h"
#include "ipc/IPCMessageUtils.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "mozilla/BasePrincipal.h"
class nsILoadContext;

Просмотреть файл

@ -7,7 +7,7 @@
#define _mozilla_dom_BindingIPCUtils_h
#include "mozilla/dom/BindingDeclarations.h"
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
namespace IPC {
template <>

Просмотреть файл

@ -9,6 +9,7 @@
#include <utility>
#include "ipc/EnumSerializer.h"
#include "ipc/IPCMessageUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/ErrorResult.h"

2
dom/cache/IPCUtils.h поставляемый
Просмотреть файл

@ -7,7 +7,7 @@
#ifndef mozilla_dom_cache_IPCUtils_h
#define mozilla_dom_cache_IPCUtils_h
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
#include "mozilla/dom/cache/Types.h"

Просмотреть файл

@ -12,6 +12,7 @@
#include <tuple>
#include <vector>
#include <unordered_map>
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "mozilla/dom/QueueParamTraits.h"
#include "mozilla/ipc/SharedMemoryBasic.h"
#include "mozilla/Assertions.h"

Просмотреть файл

@ -8,7 +8,7 @@
#ifndef _QUEUEPARAMTRAITS_H_
#define _QUEUEPARAMTRAITS_H_ 1
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
#include "mozilla/gfx/2D.h"
#include "mozilla/Assertions.h"
#include "mozilla/IntegerRange.h"

Просмотреть файл

@ -6,6 +6,7 @@
#ifndef WEBGLIPDL_H_
#define WEBGLIPDL_H_
#include "ipc/EnumSerializer.h"
#include "mozilla/layers/LayersSurfaces.h"
#include "WebGLTypes.h"

Просмотреть файл

@ -8,6 +8,7 @@
#include <type_traits>
#include "ipc/EnumSerializer.h"
#include "mozilla/dom/ProducerConsumerQueue.h"
#include "TexUnpackBlob.h"
#include "WebGLContext.h"

Просмотреть файл

@ -6,7 +6,7 @@
#ifndef _mozilla_dom_ClientIPCUtils_h
#define _mozilla_dom_ClientIPCUtils_h
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
#include "X11UndefineNone.h"
#include "mozilla/dom/ClientBinding.h"

Просмотреть файл

@ -7,7 +7,7 @@
#ifndef mozilla_dom_fetch_IPCUtils_h
#define mozilla_dom_fetch_IPCUtils_h
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
#include "mozilla/dom/HeadersBinding.h"
#include "mozilla/dom/RequestBinding.h"

Просмотреть файл

@ -7,7 +7,7 @@
#ifndef mozilla_dom_filehandle_SerializationHelpers_h
#define mozilla_dom_filehandle_SerializationHelpers_h
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
#include "mozilla/dom/FileModeBinding.h"

Просмотреть файл

@ -7,7 +7,7 @@
#ifndef mozilla_dom_gamepad_GamepadMessageUtils_h
#define mozilla_dom_gamepad_GamepadMessageUtils_h
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
#include "mozilla/dom/GamepadBinding.h"
#include "mozilla/dom/GamepadHandle.h"
#include "mozilla/dom/GamepadLightIndicatorBinding.h"

Просмотреть файл

@ -7,7 +7,8 @@
#ifndef mozilla_dom_indexeddb_serializationhelpers_h__
#define mozilla_dom_indexeddb_serializationhelpers_h__
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "mozilla/dom/indexedDB/Key.h"
#include "mozilla/dom/indexedDB/KeyPath.h"

Просмотреть файл

@ -7,7 +7,7 @@
#ifndef mozilla_dom_docshell_message_utils_h__
#define mozilla_dom_docshell_message_utils_h__
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
#include "nsCOMPtr.h"
#include "nsDocShellLoadState.h"
#include "nsIContentViewer.h"

Просмотреть файл

@ -6,7 +6,7 @@
#include "mozilla/dom/ReferrerInfoUtils.h"
#include "ipc/IPCMessageUtils.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "nsSerializationHelper.h"
#include "nsString.h"

Просмотреть файл

@ -7,7 +7,7 @@
#ifndef TABMESSAGE_UTILS_H
#define TABMESSAGE_UTILS_H
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
#include "mozilla/RefPtr.h"
#include "mozilla/dom/Event.h"
#include "nsIRemoteTab.h"
@ -15,7 +15,6 @@
#include "nsCOMPtr.h"
#include "mozilla/dom/EffectsInfo.h"
#include "mozilla/layers/LayersMessageUtils.h"
#include "ipc/IPCMessageUtils.h"
#include "TabMessageTypes.h"
#include "X11UndefineNone.h"

Просмотреть файл

@ -7,7 +7,7 @@
#ifndef mozilla_dom_useractivation_ipc_utils_h__
#define mozilla_dom_useractivation_ipc_utils_h__
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
// Undo X11/X.h's definition of None
#undef None

Просмотреть файл

@ -8,7 +8,7 @@
#define mozilla_dom_JSActor_h
#include "js/TypeDecls.h"
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
#include "mozilla/Attributes.h"
#include "mozilla/dom/PromiseNativeHandler.h"
#include "nsCycleCollectionParticipant.h"

Просмотреть файл

@ -9,7 +9,7 @@
#include <string>
#include "chrome/common/ipc_message_utils.h"
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
#include "mozilla/dom/LSSnapshot.h"
#include "mozilla/dom/LSValue.h"

Просмотреть файл

@ -5,7 +5,7 @@
#ifndef DOM_MEDIA_GVAUTOPLAYREQUESTSTATUSIPC_H_
#define DOM_MEDIA_GVAUTOPLAYREQUESTSTATUSIPC_H_
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
#include "GVAutoplayRequestUtils.h"

Просмотреть файл

@ -9,7 +9,8 @@
#include "gmp-sanitized-cdm-exports.h"
#include "gmp-video-codec.h"
#include "gmp-video-frame-encoded.h"
#include "IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
namespace IPC {

Просмотреть файл

@ -9,7 +9,7 @@
#include "DecoderDoctorDiagnostics.h"
#include "PlatformDecoderModule.h"
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
#include "mozilla/EnumSet.h"
#include "mozilla/GfxMessageUtils.h"
#include "mozilla/gfx/Rect.h"

Просмотреть файл

@ -7,7 +7,7 @@
#ifndef ipc_MediaControlIPC_h
#define ipc_MediaControlIPC_h
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
#include "mozilla/dom/MediaControllerBinding.h"
#include "mozilla/dom/MediaControlKeySource.h"

Просмотреть файл

@ -5,7 +5,7 @@
#ifndef DOM_MEDIA_MEDIASESSION_MEDIASESSIONIPCUTILS_H_
#define DOM_MEDIA_MEDIASESSION_MEDIASESSIONIPCUTILS_H_
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
#include "MediaMetadata.h"
#include "mozilla/dom/MediaSession.h"
#include "mozilla/dom/MediaSessionBinding.h"

Просмотреть файл

@ -7,7 +7,7 @@
#ifndef mozilla_CamerasTypes_h
#define mozilla_CamerasTypes_h
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
namespace mozilla {

Просмотреть файл

@ -7,7 +7,7 @@
#if !defined(MediaSystemResourceMessageUtils_h_)
# define MediaSystemResourceMessageUtils_h_
# include "ipc/IPCMessageUtils.h"
# include "ipc/EnumSerializer.h"
# include "MediaSystemResourceTypes.h"
namespace IPC {

Просмотреть файл

@ -6,7 +6,8 @@
#define _WEBRTC_GLOBAL_H_
#include "WebrtcIPCTraits.h"
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/RTCDataChannelBinding.h"
#include "mozilla/dom/RTCStatsReportBinding.h"

Просмотреть файл

@ -5,7 +5,9 @@
#ifndef _WEBRTC_IPC_TRAITS_H_
#define _WEBRTC_IPC_TRAITS_H_
#include "ipc/EnumSerializer.h"
#include "ipc/IPCMessageUtils.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/RTCConfigurationBinding.h"
#include "mozilla/media/webrtc/WebrtcGlobal.h"

Просмотреть файл

@ -18,6 +18,7 @@
#include "nsITimedChannel.h"
#include "mozilla/ipc/IPDLParamTraits.h"
#include "ipc/IPCMessageUtils.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "mozilla/net/nsServerTiming.h"
class nsIHttpChannel;

Просмотреть файл

@ -7,6 +7,7 @@
#ifndef dom_plugins_ipc_functionbrokeripcutils_h
#define dom_plugins_ipc_functionbrokeripcutils_h 1
#include "ipc/EnumSerializer.h"
#include "PluginMessageUtils.h"
#if defined(XP_WIN)

Просмотреть файл

@ -7,6 +7,7 @@
#ifndef dom_plugins_ipc_ipdltuple_h
#define dom_plugins_ipc_ipdltuple_h
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "mozilla/plugins/FunctionBrokerIPCUtils.h"
#include "mozilla/Variant.h"

Просмотреть файл

@ -7,7 +7,7 @@
#ifndef DOM_PLUGINS_PLUGINMESSAGEUTILS_H
#define DOM_PLUGINS_PLUGINMESSAGEUTILS_H
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
#include "base/message_loop.h"
#include "base/shared_memory.h"

Просмотреть файл

@ -7,6 +7,7 @@
#ifndef mozilla_dom_quota_SerializationHelpers_h
#define mozilla_dom_quota_SerializationHelpers_h
#include "ipc/EnumSerializer.h"
#include "ipc/IPCMessageUtils.h"
#include "mozilla/dom/quota/Client.h"

Просмотреть файл

@ -6,7 +6,7 @@
#ifndef _mozilla_dom_ServiceWorkerIPCUtils_h
#define _mozilla_dom_ServiceWorkerIPCUtils_h
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
// Undo X11/X.h's definition of None
#undef None

Просмотреть файл

@ -9,7 +9,7 @@
#include <cstdint>
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
#include "mozilla/dom/ServiceWorkerOpArgs.h"
namespace mozilla {

Просмотреть файл

@ -11,7 +11,7 @@
* Utility functions used by both WebAuthnManager and U2FTokenManager.
*/
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
#include "mozilla/dom/CryptoBuffer.h"
#include "mozilla/dom/WebAuthenticationBinding.h"
#include "ipc/IPCMessageUtils.h"

Просмотреть файл

@ -7,6 +7,7 @@
#define WEBGPU_SERIALIZE_H_
#include "WebGPUTypes.h"
#include "ipc/EnumSerializer.h"
#include "ipc/IPCMessageUtils.h"
#include "mozilla/dom/WebGPUBinding.h"
#include "mozilla/webgpu/ffi/wgpu.h"

Просмотреть файл

@ -17,6 +17,8 @@
#include "gfxRect.h"
#include "gfxTelemetry.h"
#include "gfxTypes.h"
#include "ipc/EnumSerializer.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "mozilla/gfx/CrossProcessPaint.h"
#include "mozilla/gfx/Matrix.h"
#include "mozilla/gfx/ScaleFactor.h"

Просмотреть файл

@ -14,6 +14,7 @@
#include "FrameMetrics.h"
#include "VsyncSource.h"
#include "chrome/common/ipc_message_utils.h"
#include "ipc/EnumSerializer.h"
#include "ipc/IPCMessageUtils.h"
#include "mozilla/MotionPathUtils.h"
#include "mozilla/ServoBindings.h"

Просмотреть файл

@ -7,6 +7,7 @@
#ifndef IPC_ShadowLayerUtils_h
#define IPC_ShadowLayerUtils_h
#include "ipc/EnumSerializer.h"
#include "ipc/IPCMessageUtils.h"
#include "GLContextTypes.h"
#include "SurfaceTypes.h"

Просмотреть файл

@ -11,6 +11,7 @@
#include <unordered_map>
#include "base/process.h"
#include "ipc/IPCMessageUtils.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "mozilla/gfx/Point.h" // for IntSize
namespace mozilla {

Просмотреть файл

@ -7,7 +7,7 @@
#ifndef IPC_VideoBridgeUtils_h
#define IPC_VideoBridgeUtils_h
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
namespace mozilla {
namespace layers {

Просмотреть файл

@ -9,6 +9,7 @@
#include "chrome/common/ipc_message_utils.h"
#include "ipc/EnumSerializer.h"
#include "ipc/IPCMessageUtils.h"
#include "mozilla/webrender/webrender_ffi.h"
#include "mozilla/webrender/WebRenderTypes.h"

Просмотреть файл

@ -15,6 +15,7 @@
#include "gfxRect.h"
#include "gfxTypes.h"
#include "harfbuzz/hb.h"
#include "ipc/EnumSerializer.h"
#include "mozilla/AlreadyAddRefed.h"
#include "mozilla/Assertions.h"
#include "mozilla/FontPropertyTypes.h"

Просмотреть файл

@ -12,6 +12,7 @@
#include <utility>
#include "gfxPlatform.h"
#include "ipc/IPCMessageUtils.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "mozilla/Assertions.h"
#include "mozilla/Attributes.h"
#include "mozilla/Casting.h"

Просмотреть файл

@ -7,6 +7,7 @@
#ifndef mozilla_gfx_vr_VRMessageUtils_h
#define mozilla_gfx_vr_VRMessageUtils_h
#include "ipc/EnumSerializer.h"
#include "ipc/IPCMessageUtils.h"
#include "mozilla/ArrayUtils.h"
#include "mozilla/GfxMessageUtils.h"

Просмотреть файл

@ -40,7 +40,7 @@ typedef Observer<SensorAccuracy> ISensorAccuracyObserver;
} // namespace hal
} // namespace mozilla
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
namespace IPC {
/**

Просмотреть файл

@ -6,7 +6,7 @@
#ifndef mozilla_hal_Types_h
#define mozilla_hal_Types_h
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
#include "mozilla/Observer.h"
namespace mozilla {

167
ipc/glue/EnumSerializer.h Normal file
Просмотреть файл

@ -0,0 +1,167 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 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_ENUMSERIALIZER_H__
#define __IPC_GLUE_ENUMSERIALIZER_H__
#include "CrashAnnotations.h"
#include "chrome/common/ipc_message_utils.h"
#include "mozilla/Assertions.h"
#include "mozilla/IntegerTypeTraits.h"
#include "nsExceptionHandler.h"
#include "nsLiteralString.h"
#include "nsString.h"
#include "nsTLiteralString.h"
class PickleIterator;
namespace IPC {
class Message;
}
#ifdef _MSC_VER
# pragma warning(disable : 4800)
#endif
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_RELEASE_ASSERT(EnumValidator::IsLegalValue(aValue));
WriteParam(aMsg, uintParamType(aValue));
}
static bool Read(const Message* aMsg, PickleIterator* aIter,
paramType* aResult) {
uintParamType value;
if (!ReadParam(aMsg, aIter, &value)) {
CrashReporter::AnnotateCrashReport(
CrashReporter::Annotation::IPCReadErrorReason, "Bad iter"_ns);
return false;
} else if (!EnumValidator::IsLegalValue(paramType(value))) {
CrashReporter::AnnotateCrashReport(
CrashReporter::Annotation::IPCReadErrorReason, "Illegal value"_ns);
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 MinLegal, E MaxLegal>
class ContiguousEnumValidatorInclusive {
// 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 <= MaxLegal;
}
};
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>> {};
/**
* This is similar to ContiguousEnumSerializer, but the last template
* parameter is expected to be the highest legal value, rather than a
* sentinel value. This is intended to support enumerations that don't
* have sentinel values.
*/
template <typename E, E MinLegal, E MaxLegal>
struct ContiguousEnumSerializerInclusive
: EnumSerializer<E,
ContiguousEnumValidatorInclusive<E, MinLegal, MaxLegal>> {
};
/**
* 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>> {};
} /* namespace IPC */
#endif /* __IPC_GLUE_ENUMSERIALIZER_H__ */

Просмотреть файл

@ -5,6 +5,8 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "IPCMessageUtils.h"
#include <cstddef>
#include "mozilla/CheckedInt.h"
namespace IPC {

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -0,0 +1,931 @@
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 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_IPCMESSAGEUTILSSPECIALIZATIONS_H__
#define __IPC_GLUE_IPCMESSAGEUTILSSPECIALIZATIONS_H__
#include <cstdint>
#include <cstdlib>
#include <limits>
#include <string>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <vector>
#include "chrome/common/ipc_message.h"
#include "chrome/common/ipc_message_utils.h"
#include "ipc/EnumSerializer.h"
#include "ipc/IPCMessageUtils.h"
#include "mozilla/Assertions.h"
#include "mozilla/BitSet.h"
#include "mozilla/EnumSet.h"
#include "mozilla/EnumTypeTraits.h"
#include "mozilla/IntegerRange.h"
#include "mozilla/Maybe.h"
#include "mozilla/TimeStamp.h"
#ifdef XP_WIN
# include "mozilla/TimeStamp_windows.h"
#endif
#include "mozilla/Unused.h"
#include "mozilla/Vector.h"
#include "mozilla/dom/ipc/StructuredCloneData.h"
#include "nsCSSPropertyID.h"
#include "nsDebug.h"
#include "nsHashKeys.h"
#include "nsID.h"
#include "nsILoadInfo.h"
#include "nsIThread.h"
#include "nsLiteralString.h"
#include "nsString.h"
#include "nsTArray.h"
#include "nsTHashtable.h"
// XXX Includes that are only required by implementations which could be moved
// to the cpp file.
#include "base/string_util.h" // for StringPrintf
#include "mozilla/ArrayUtils.h" // for ArrayLength
#include "mozilla/CheckedInt.h"
#ifdef _MSC_VER
# pragma warning(disable : 4800)
#endif
namespace mozilla {
template <typename... Ts>
class Variant;
namespace detail {
template <typename... Ts>
struct VariantTag;
}
} // namespace mozilla
namespace mozilla::dom {
template <typename T>
class Optional;
}
namespace IPC {
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, PickleIterator* 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)) {
return false;
}
if (!aMsg->HasBytesAvailable(aIter, length)) {
return false;
}
aResult->SetLength(length);
return aMsg->ReadBytesInto(aIter, aResult->BeginWriting(), length);
}
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, PickleIterator* 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)) {
return false;
}
mozilla::CheckedInt<uint32_t> byteLength =
mozilla::CheckedInt<uint32_t>(length) * sizeof(char16_t);
if (!byteLength.isValid() ||
!aMsg->HasBytesAvailable(aIter, byteLength.value())) {
return false;
}
aResult->SetLength(length);
return aMsg->ReadBytesInto(aIter, aResult->BeginWriting(),
byteLength.value());
}
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 <>
struct ParamTraits<nsDependentSubstring> : ParamTraits<nsAString> {
typedef nsDependentSubstring paramType;
};
template <>
struct ParamTraits<nsDependentCSubstring> : ParamTraits<nsACString> {
typedef nsDependentCSubstring paramType;
};
#ifdef MOZILLA_INTERNAL_API
template <>
struct ParamTraits<nsAutoString> : ParamTraits<nsString> {
typedef nsAutoString paramType;
};
#endif // MOZILLA_INTERNAL_API
template <>
struct ParamTraits<nsTHashtable<nsUint64HashKey>> {
typedef nsTHashtable<nsUint64HashKey> paramType;
static void Write(Message* aMsg, const paramType& aParam) {
uint32_t count = aParam.Count();
WriteParam(aMsg, count);
for (auto iter = aParam.ConstIter(); !iter.Done(); iter.Next()) {
WriteParam(aMsg, iter.Get()->GetKey());
}
}
static bool Read(const Message* aMsg, PickleIterator* aIter,
paramType* aResult) {
uint32_t count;
if (!ReadParam(aMsg, aIter, &count)) {
return false;
}
paramType table(count);
for (uint32_t i = 0; i < count; ++i) {
uint64_t key;
if (!ReadParam(aMsg, aIter, &key)) {
return false;
}
table.PutEntry(key);
}
*aResult = std::move(table);
return true;
}
};
// Pickle::ReadBytes and ::WriteBytes take the length in ints, so we must
// ensure there is no overflow. This returns |false| if it would overflow.
// Otherwise, it returns |true| and places the byte length in |aByteLength|.
bool ByteLengthIsValid(uint32_t aNumElements, size_t aElementSize,
int* aByteLength);
// Note: IPDL will sometimes codegen specialized implementations of
// nsTArray serialization and deserialization code in
// implementSpecialArrayPickling(). This is needed when ParamTraits<E>
// is not defined.
template <typename E>
struct ParamTraits<nsTArray<E>> {
typedef nsTArray<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 =
(std::is_integral_v<E> || std::is_floating_point_v<E>);
static void Write(Message* aMsg, const paramType& aParam) {
uint32_t length = aParam.Length();
WriteParam(aMsg, length);
if (sUseWriteBytes) {
int pickledLength = 0;
MOZ_RELEASE_ASSERT(ByteLengthIsValid(length, sizeof(E), &pickledLength));
aMsg->WriteBytes(aParam.Elements(), pickledLength);
} else {
const E* elems = aParam.Elements();
for (uint32_t index = 0; index < length; index++) {
WriteParam(aMsg, elems[index]);
}
}
}
// This method uses infallible allocation so that an OOM failure will
// show up as an OOM crash rather than an IPC FatalError.
static bool Read(const Message* aMsg, PickleIterator* aIter,
paramType* aResult) {
uint32_t length;
if (!ReadParam(aMsg, aIter, &length)) {
return false;
}
if (sUseWriteBytes) {
int pickledLength = 0;
if (!ByteLengthIsValid(length, sizeof(E), &pickledLength)) {
return false;
}
E* elements = aResult->AppendElements(length);
return aMsg->ReadBytesInto(aIter, elements, pickledLength);
} else {
// Each ReadParam<E> may read more than 1 byte each; this is an attempt
// to minimally validate that the length isn't much larger than what's
// actually available in aMsg.
if (!aMsg->HasBytesAvailable(aIter, length)) {
return false;
}
aResult->SetCapacity(length);
for (uint32_t index = 0; index < length; index++) {
E* element = aResult->AppendElement();
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<CopyableTArray<E>> : ParamTraits<nsTArray<E>> {};
template <typename E>
struct ParamTraits<FallibleTArray<E>> {
typedef FallibleTArray<E> paramType;
static void Write(Message* aMsg, const paramType& aParam) {
WriteParam(aMsg, static_cast<const nsTArray<E>&>(aParam));
}
// Deserialize the array infallibly, but return a FallibleTArray.
static bool Read(const Message* aMsg, PickleIterator* aIter,
paramType* aResult) {
nsTArray<E> temp;
if (!ReadParam(aMsg, aIter, &temp)) return false;
*aResult = std::move(temp);
return true;
}
static void Log(const paramType& aParam, std::wstring* aLog) {
LogParam(static_cast<const nsTArray<E>&>(aParam), aLog);
}
};
template <typename E, size_t N>
struct ParamTraits<AutoTArray<E, N>> : ParamTraits<nsTArray<E>> {
typedef AutoTArray<E, N> paramType;
};
template <typename E, size_t N>
struct ParamTraits<CopyableAutoTArray<E, N>> : ParamTraits<AutoTArray<E, N>> {};
template <typename E, size_t N, typename AP>
struct ParamTraits<mozilla::Vector<E, N, AP>> {
typedef mozilla::Vector<E, N, AP> 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 =
(std::is_integral_v<E> || std::is_floating_point_v<E>);
static void Write(Message* aMsg, const paramType& aParam) {
uint32_t length = aParam.length();
WriteParam(aMsg, length);
if (sUseWriteBytes) {
int pickledLength = 0;
MOZ_RELEASE_ASSERT(ByteLengthIsValid(length, sizeof(E), &pickledLength));
aMsg->WriteBytes(aParam.begin(), pickledLength);
return;
}
for (const E& elem : aParam) {
WriteParam(aMsg, elem);
}
}
static bool Read(const Message* aMsg, PickleIterator* aIter,
paramType* aResult) {
uint32_t length;
if (!ReadParam(aMsg, aIter, &length)) {
return false;
}
if (sUseWriteBytes) {
int pickledLength = 0;
if (!ByteLengthIsValid(length, sizeof(E), &pickledLength)) {
return false;
}
if (!aResult->resizeUninitialized(length)) {
// So that OOM failure shows up as OOM crash instead of IPC FatalError.
NS_ABORT_OOM(length * sizeof(E));
}
E* elements = aResult->begin();
return aMsg->ReadBytesInto(aIter, elements, pickledLength);
}
// Each ReadParam<E> may read more than 1 byte each; this is an attempt
// to minimally validate that the length isn't much larger than what's
// actually available in aMsg.
if (!aMsg->HasBytesAvailable(aIter, length)) {
return false;
}
if (!aResult->resize(length)) {
// So that OOM failure shows up as OOM crash instead of IPC FatalError.
NS_ABORT_OOM(length * sizeof(E));
}
for (uint32_t index = 0; index < length; ++index) {
if (!ReadParam(aMsg, aIter, &((*aResult)[index]))) {
return false;
}
}
return true;
}
static void Log(const paramType& aParam, std::wstring* aLog) {
for (uint32_t index = 0, len = aParam.length(); index < len; ++index) {
if (index) {
aLog->append(L" ");
}
LogParam(aParam[index], aLog);
}
}
};
template <typename E>
struct ParamTraits<std::vector<E>> {
typedef std::vector<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 =
(std::is_integral_v<E> || std::is_floating_point_v<E>);
static void Write(Message* aMsg, const paramType& aParam) {
uint32_t length = aParam.size();
WriteParam(aMsg, length);
if (sUseWriteBytes) {
int pickledLength = 0;
MOZ_RELEASE_ASSERT(ByteLengthIsValid(length, sizeof(E), &pickledLength));
aMsg->WriteBytes(aParam.data(), pickledLength);
return;
}
for (const E& elem : aParam) {
WriteParam(aMsg, elem);
}
}
static bool Read(const Message* aMsg, PickleIterator* aIter,
paramType* aResult) {
uint32_t length;
if (!ReadParam(aMsg, aIter, &length)) {
return false;
}
if (sUseWriteBytes) {
int pickledLength = 0;
if (!ByteLengthIsValid(length, sizeof(E), &pickledLength)) {
return false;
}
aResult->resize(length);
E* elements = aResult->data();
return aMsg->ReadBytesInto(aIter, elements, pickledLength);
}
// Each ReadParam<E> may read more than 1 byte each; this is an attempt
// to minimally validate that the length isn't much larger than what's
// actually available in aMsg.
if (!aMsg->HasBytesAvailable(aIter, length)) {
return false;
}
aResult->resize(length);
for (uint32_t index = 0; index < length; ++index) {
if (!ReadParam(aMsg, aIter, &((*aResult)[index]))) {
return false;
}
}
return true;
}
static void Log(const paramType& aParam, std::wstring* aLog) {
for (uint32_t index = 0, len = aParam.size(); index < len; ++index) {
if (index) {
aLog->append(L" ");
}
LogParam(aParam[index], aLog);
}
}
};
template <typename K, typename V>
struct ParamTraits<std::unordered_map<K, V>> final {
using T = std::unordered_map<K, V>;
static void Write(Message* const msg, const T& in) {
WriteParam(msg, in.size());
for (const auto& pair : in) {
WriteParam(msg, pair.first);
WriteParam(msg, pair.second);
}
}
static bool Read(const Message* const msg, PickleIterator* const itr,
T* const out) {
size_t size = 0;
if (!ReadParam(msg, itr, &size)) return false;
T map;
map.reserve(size);
for (const auto i : mozilla::IntegerRange(size)) {
std::pair<K, V> pair;
mozilla::Unused << i;
if (!ReadParam(msg, itr, &(pair.first)) ||
!ReadParam(msg, itr, &(pair.second))) {
return false;
}
map.insert(std::move(pair));
}
*out = std::move(map);
return true;
}
};
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, PickleIterator* aIter,
paramType* aResult) {
return aMsg->ReadBytesInto(aIter, aResult, sizeof(*aResult));
}
static void Log(const paramType& aParam, std::wstring* aLog) {
aLog->append(StringPrintf(L"%g", aParam));
}
};
template <>
struct ParamTraits<nsCSSPropertyID>
: public ContiguousEnumSerializer<nsCSSPropertyID, eCSSProperty_UNKNOWN,
eCSSProperty_COUNT> {};
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, PickleIterator* 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, PickleIterator* 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, PickleIterator* 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.mUsedCanonicalNow);
WriteParam(aMsg, aParam.mIsNull);
WriteParam(aMsg, aParam.mHasQPC);
}
static bool Read(const Message* aMsg, PickleIterator* aIter,
paramType* aResult) {
return (ReadParam(aMsg, aIter, &aResult->mGTC) &&
ReadParam(aMsg, aIter, &aResult->mQPC) &&
ReadParam(aMsg, aIter, &aResult->mUsedCanonicalNow) &&
ReadParam(aMsg, aIter, &aResult->mIsNull) &&
ReadParam(aMsg, aIter, &aResult->mHasQPC));
}
};
#else
template <>
struct ParamTraits<mozilla::TimeStamp63Bit> {
typedef mozilla::TimeStamp63Bit paramType;
static void Write(Message* aMsg, const paramType& aParam) {
WriteParam(aMsg, aParam.mUsedCanonicalNow);
WriteParam(aMsg, aParam.mTimeStamp);
}
static bool Read(const Message* aMsg, PickleIterator* aIter,
paramType* aResult) {
bool success = true;
uint64_t result;
success &= ReadParam(aMsg, aIter, &result);
aResult->mUsedCanonicalNow = result & 0x01;
success &= ReadParam(aMsg, aIter, &result);
aResult->mTimeStamp = result & 0x7FFFFFFFFFFFFFFF;
return success;
}
};
#endif
template <>
struct ParamTraits<mozilla::dom::ipc::StructuredCloneData> {
typedef mozilla::dom::ipc::StructuredCloneData paramType;
static void Write(Message* aMsg, const paramType& aParam) {
aParam.WriteIPCParams(aMsg);
}
static bool Read(const Message* aMsg, PickleIterator* aIter,
paramType* aResult) {
return aResult->ReadIPCParams(aMsg, aIter);
}
static void Log(const paramType& aParam, std::wstring* aLog) {
LogParam(aParam.DataLength(), aLog);
}
};
template <class T>
struct ParamTraits<mozilla::Maybe<T>> {
typedef mozilla::Maybe<T> paramType;
static void Write(Message* msg, const paramType& param) {
if (param.isSome()) {
WriteParam(msg, true);
WriteParam(msg, param.value());
} else {
WriteParam(msg, false);
}
}
static bool Read(const Message* msg, PickleIterator* iter,
paramType* result) {
bool isSome;
if (!ReadParam(msg, iter, &isSome)) {
return false;
}
if (isSome) {
T tmp;
if (!ReadParam(msg, iter, &tmp)) {
return false;
}
*result = mozilla::Some(std::move(tmp));
} else {
*result = mozilla::Nothing();
}
return true;
}
};
template <typename T, typename U>
struct ParamTraits<mozilla::EnumSet<T, U>> {
typedef mozilla::EnumSet<T, U> paramType;
typedef U serializedType;
static void Write(Message* msg, const paramType& param) {
MOZ_RELEASE_ASSERT(IsLegalValue(param.serialize()));
WriteParam(msg, param.serialize());
}
static bool Read(const Message* msg, PickleIterator* iter,
paramType* result) {
serializedType tmp;
if (ReadParam(msg, iter, &tmp)) {
if (IsLegalValue(tmp)) {
result->deserialize(tmp);
return true;
}
}
return false;
}
static constexpr serializedType AllEnumBits() {
return ~serializedType(0) >> (std::numeric_limits<serializedType>::digits -
(mozilla::MaxEnumValue<T>::value + 1));
}
static constexpr bool IsLegalValue(const serializedType value) {
static_assert(mozilla::MaxEnumValue<T>::value <
std::numeric_limits<serializedType>::digits,
"Enum max value is not in the range!");
static_assert(
std::is_unsigned<decltype(mozilla::MaxEnumValue<T>::value)>::value,
"Type of MaxEnumValue<T>::value specialization should be unsigned!");
return (value & AllEnumBits()) == value;
}
};
template <class... Ts>
struct ParamTraits<mozilla::Variant<Ts...>> {
typedef mozilla::Variant<Ts...> paramType;
using Tag = typename mozilla::detail::VariantTag<Ts...>::Type;
static void Write(Message* msg, const paramType& param) {
WriteParam(msg, param.tag);
param.match([msg](const auto& t) { WriteParam(msg, t); });
}
// Because VariantReader is a nested struct, we need the dummy template
// parameter to avoid making VariantReader<0> an explicit specialization,
// which is not allowed for a nested class template
template <size_t N, typename dummy = void>
struct VariantReader {
using Next = VariantReader<N - 1>;
static bool Read(const Message* msg, PickleIterator* iter, Tag tag,
paramType* result) {
// Since the VariantReader specializations start at N , we need to
// subtract one to look at N - 1, the first valid tag. This means our
// comparisons are off by 1. If we get to N = 0 then we have failed to
// find a match to the tag.
if (tag == N - 1) {
// Recall, even though the template parameter is N, we are
// actually interested in the N - 1 tag.
// Default construct our field within the result outparameter and
// directly deserialize into the variant. Note that this means that
// every type in Ts needs to be default constructible
return ReadParam(msg, iter, &result->template emplace<N - 1>());
} else {
return Next::Read(msg, iter, tag, result);
}
}
}; // VariantReader<N>
// Since we are conditioning on tag = N - 1 in the preceding specialization,
// if we get to `VariantReader<0, dummy>` we have failed to find
// a matching tag.
template <typename dummy>
struct VariantReader<0, dummy> {
static bool Read(const Message* msg, PickleIterator* iter, Tag tag,
paramType* result) {
return false;
}
};
static bool Read(const Message* msg, PickleIterator* iter,
paramType* result) {
Tag tag;
if (ReadParam(msg, iter, &tag)) {
return VariantReader<sizeof...(Ts)>::Read(msg, iter, tag, result);
}
return false;
}
};
template <typename T>
struct ParamTraits<mozilla::dom::Optional<T>> {
typedef mozilla::dom::Optional<T> paramType;
static void Write(Message* aMsg, const paramType& aParam) {
if (aParam.WasPassed()) {
WriteParam(aMsg, true);
WriteParam(aMsg, aParam.Value());
return;
}
WriteParam(aMsg, false);
}
static bool Read(const Message* aMsg, PickleIterator* aIter,
paramType* aResult) {
bool wasPassed = false;
if (!ReadParam(aMsg, aIter, &wasPassed)) {
return false;
}
aResult->Reset();
if (wasPassed) {
if (!ReadParam(aMsg, aIter, &aResult->Construct())) {
return false;
}
}
return true;
}
};
struct CrossOriginOpenerPolicyValidator {
static bool IsLegalValue(nsILoadInfo::CrossOriginOpenerPolicy e) {
return e == nsILoadInfo::OPENER_POLICY_UNSAFE_NONE ||
e == nsILoadInfo::OPENER_POLICY_SAME_ORIGIN ||
e == nsILoadInfo::OPENER_POLICY_SAME_ORIGIN_ALLOW_POPUPS ||
e == nsILoadInfo::
OPENER_POLICY_SAME_ORIGIN_EMBEDDER_POLICY_REQUIRE_CORP;
}
};
template <>
struct ParamTraits<nsILoadInfo::CrossOriginOpenerPolicy>
: EnumSerializer<nsILoadInfo::CrossOriginOpenerPolicy,
CrossOriginOpenerPolicyValidator> {};
struct CrossOriginEmbedderPolicyValidator {
static bool IsLegalValue(nsILoadInfo::CrossOriginEmbedderPolicy e) {
return e == nsILoadInfo::EMBEDDER_POLICY_NULL ||
e == nsILoadInfo::EMBEDDER_POLICY_REQUIRE_CORP;
}
};
template <>
struct ParamTraits<nsILoadInfo::CrossOriginEmbedderPolicy>
: EnumSerializer<nsILoadInfo::CrossOriginEmbedderPolicy,
CrossOriginEmbedderPolicyValidator> {};
template <size_t N, typename Word>
struct ParamTraits<mozilla::BitSet<N, Word>> {
typedef mozilla::BitSet<N, Word> paramType;
static void Write(Message* aMsg, const paramType& aParam) {
for (Word word : aParam.Storage()) {
WriteParam(aMsg, word);
}
}
static bool Read(const Message* aMsg, PickleIterator* aIter,
paramType* aResult) {
for (Word& word : aResult->Storage()) {
if (!ReadParam(aMsg, aIter, &word)) {
return false;
}
}
return true;
}
};
} /* namespace IPC */
#endif /* __IPC_GLUE_IPCMESSAGEUTILSSPECIALIZATIONS_H__ */

Просмотреть файл

@ -8,7 +8,7 @@
#ifndef ipc_glue_MessageChannel_h
#define ipc_glue_MessageChannel_h 1
#include "base/basictypes.h"
#include "ipc/EnumSerializer.h"
#include "mozilla/Atomics.h"
#include "mozilla/LinkedList.h"
#include "mozilla/Monitor.h"
@ -16,7 +16,6 @@
#if defined(OS_WIN)
# include "mozilla/ipc/Neutering.h"
#endif // defined(OS_WIN)
#include <math.h>
#include <functional>
#include <map>
@ -31,7 +30,11 @@
#endif
class MessageLoop;
class nsIEventTarget;
namespace IPC {
template <typename T>
struct ParamTraits;
}
namespace mozilla {
namespace ipc {

Просмотреть файл

@ -7,7 +7,20 @@
#ifndef __IPC_GLUE_SERIALIZEDSTRUCTUREDCLONEBUFFER_H__
#define __IPC_GLUE_SERIALIZEDSTRUCTUREDCLONEBUFFER_H__
#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <string>
#include <utility>
#include "chrome/common/ipc_message.h"
#include "chrome/common/ipc_message_utils.h"
#include "js/AllocPolicy.h"
#include "js/StructuredClone.h"
#include "mozilla/Assertions.h"
#include "mozilla/BufferList.h"
#include "mozilla/Vector.h"
#include "mozilla/mozalloc.h"
class PickleIterator;
namespace mozilla {
template <typename...>
@ -46,4 +59,72 @@ struct SerializedStructuredCloneBuffer final {
} // namespace mozilla
namespace IPC {
template <>
struct ParamTraits<JSStructuredCloneData> {
typedef JSStructuredCloneData paramType;
static void Write(Message* aMsg, const paramType& aParam) {
MOZ_ASSERT(!(aParam.Size() % sizeof(uint64_t)));
WriteParam(aMsg, aParam.Size());
aParam.ForEachDataChunk([&](const char* aData, size_t aSize) {
return aMsg->WriteBytes(aData, aSize, sizeof(uint64_t));
});
}
static bool Read(const Message* aMsg, PickleIterator* aIter,
paramType* aResult) {
size_t length = 0;
if (!ReadParam(aMsg, aIter, &length)) {
return false;
}
MOZ_ASSERT(!(length % sizeof(uint64_t)));
mozilla::BufferList<InfallibleAllocPolicy> buffers(0, 0, 4096);
// Borrowing is not suitable to use for IPC to hand out data
// because we often want to store the data somewhere for
// processing after IPC has released the underlying buffers. One
// case is PContentChild::SendGetXPCOMProcessAttributes. We can't
// return a borrowed buffer because the out param outlives the
// IPDL callback.
if (length &&
!aMsg->ExtractBuffers(aIter, length, &buffers, sizeof(uint64_t))) {
return false;
}
bool success;
mozilla::BufferList<js::SystemAllocPolicy> out =
buffers.MoveFallible<js::SystemAllocPolicy>(&success);
if (!success) {
return false;
}
*aResult = JSStructuredCloneData(
std::move(out), JS::StructuredCloneScope::DifferentProcess);
return true;
}
};
template <>
struct ParamTraits<mozilla::SerializedStructuredCloneBuffer> {
typedef mozilla::SerializedStructuredCloneBuffer paramType;
static void Write(Message* aMsg, const paramType& aParam) {
WriteParam(aMsg, aParam.data);
}
static bool Read(const Message* aMsg, PickleIterator* aIter,
paramType* aResult) {
return ReadParam(aMsg, aIter, &aResult->data);
}
static void Log(const paramType& aParam, std::wstring* aLog) {
LogParam(aParam.data.Size(), aLog);
}
};
} // namespace IPC
#endif /* __IPC_GLUE_SERIALIZEDSTRUCTUREDCLONEBUFFER_H__ */

Просмотреть файл

@ -143,7 +143,9 @@ if CONFIG["OS_ARCH"] != "WINNT":
]
EXPORTS.ipc += [
"EnumSerializer.h",
"IPCMessageUtils.h",
"IPCMessageUtilsSpecializations.h",
]
UNIFIED_SOURCES += [

Просмотреть файл

@ -6,6 +6,7 @@
#define mozilla_net_DashboardTypes_h_
#include "ipc/IPCMessageUtils.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "nsHttp.h"
#include "nsString.h"
#include "nsTArray.h"

Просмотреть файл

@ -8,6 +8,7 @@
#include "mozilla/DebugOnly.h"
#include "ipc/EnumSerializer.h"
#include "ipc/IPCMessageUtils.h"
#include "nsExceptionHandler.h"
#include "nsIHttpChannel.h"

Просмотреть файл

@ -11,6 +11,7 @@
#include "base/basictypes.h"
#include "ipc/IPCMessageUtils.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "nsHttp.h"
#include "nsHttpHeaderArray.h"
#include "nsHttpRequestHead.h"

Просмотреть файл

@ -6,10 +6,14 @@
#include "WebSocketFrame.h"
#include "WebSocketChannel.h"
#include "ErrorList.h"
#include "MainThreadUtils.h"
#include "chrome/common/ipc_message_utils.h"
#include "mozilla/Assertions.h"
#include "nsDOMNavigationTiming.h"
#include "nsSocketTransportService2.h"
#include "nsThreadUtils.h" // for NS_IsMainThread
#include "ipc/IPCMessageUtils.h"
#include "nscore.h"
#include "prtime.h"
namespace mozilla {
namespace net {

Просмотреть файл

@ -7,14 +7,22 @@
#ifndef mozilla_net_WebSocketFrame_h
#define mozilla_net_WebSocketFrame_h
#include "nsIWebSocketEventService.h"
#include <cstdint>
#include "nsISupports.h"
#include "nsIWebSocketEventService.h"
#include "nsString.h"
class PickleIterator;
// Avoid including nsDOMNavigationTiming.h here, where the canonical definition
// of DOMHighResTimeStamp resides.
typedef double DOMHighResTimeStamp;
namespace IPC {
class Message;
}
template <class P>
struct ParamTraits;
} // namespace IPC
namespace mozilla {
namespace net {
@ -72,4 +80,21 @@ class WebSocketFrame final : public nsIWebSocketFrame {
} // namespace net
} // namespace mozilla
namespace IPC {
template <>
struct ParamTraits<mozilla::net::WebSocketFrameData> {
typedef mozilla::net::WebSocketFrameData paramType;
static void Write(Message* aMsg, const paramType& aParam) {
aParam.WriteIPCParams(aMsg);
}
static bool Read(const Message* aMsg, PickleIterator* aIter,
paramType* aResult) {
return aResult->ReadIPCParams(aMsg, aIter);
}
};
} // namespace IPC
#endif // mozilla_net_WebSocketFrame_h

Просмотреть файл

@ -7,7 +7,7 @@
#ifndef ipc_DataStorageIPCUtils_h
#define ipc_DataStorageIPCUtils_h
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
#include "mozilla/DataStorage.h"
namespace IPC {

Просмотреть файл

@ -7,7 +7,7 @@
#ifndef mozilla_antitrackingipcutils_h
#define mozilla_antitrackingipcutils_h
#include "ipc/IPCMessageUtils.h"
#include "ipc/EnumSerializer.h"
#include "mozilla/ContentBlockingNotifier.h"
#include "mozilla/ContentBlocking.h"

Просмотреть файл

@ -7,6 +7,7 @@
#define Telemetry_Comms_h__
#include "ipc/IPCMessageUtils.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "mozilla/TelemetryProcessEnums.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/Variant.h"

Просмотреть файл

@ -10,6 +10,7 @@
#include <vector>
#include "ipc/IPCMessageUtils.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "mozilla/ipc/ProtocolUtils.h"
#include "mozilla/Vector.h"
#include "nsString.h"

Просмотреть файл

@ -5,6 +5,7 @@
#ifndef mozilla_WidgetMessageUtils_h
#define mozilla_WidgetMessageUtils_h
#include "ipc/EnumSerializer.h"
#include "ipc/IPCMessageUtils.h"
#include "mozilla/LookAndFeel.h"
#include "mozilla/widget/ThemeChangeKind.h"
@ -49,6 +50,11 @@ template <>
struct ParamTraits<nsCursor>
: ContiguousEnumSerializer<nsCursor, eCursor_standard, eCursorCount> {};
template <>
struct ParamTraits<nsIWidget::TouchPointerState>
: public BitFlagsEnumSerializer<nsIWidget::TouchPointerState,
nsIWidget::TouchPointerState::ALL_BITS> {};
} // namespace IPC
#endif // WidgetMessageUtils_h

Просмотреть файл

@ -6,6 +6,7 @@
#ifndef nsGUIEventIPC_h__
#define nsGUIEventIPC_h__
#include "ipc/EnumSerializer.h"
#include "ipc/IPCMessageUtils.h"
#include "mozilla/ContentCache.h"
#include "mozilla/GfxMessageUtils.h"