Bug 1640839 - Add IPC serializer for WebIDL enums. r=mccr8,jgilbert,media-playback-reviewers,dom-storage-reviewers,janv,padenot

Differential Revision: https://phabricator.services.mozilla.com/D201339
This commit is contained in:
Peter Van der Beken 2024-03-01 14:31:12 +00:00
parent e0ed150b44
commit a0448e7053
16 changed files with 93 additions and 128 deletions

View File

@ -20,6 +20,7 @@
#endif
#include "mozilla/AppShutdown.h"
#include "mozilla/dom/CanonicalBrowsingContext.h"
#include "mozilla/dom/BindingIPCUtils.h"
#include "mozilla/dom/BrowserHost.h"
#include "mozilla/dom/BrowserChild.h"
#include "mozilla/dom/BrowserParent.h"
@ -96,23 +97,17 @@ namespace IPC {
// Allow serialization and deserialization of OrientationType over IPC
template <>
struct ParamTraits<mozilla::dom::OrientationType>
: public ContiguousEnumSerializer<
mozilla::dom::OrientationType,
mozilla::dom::OrientationType::Portrait_primary,
mozilla::dom::OrientationType::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<mozilla::dom::OrientationType> {
};
template <>
struct ParamTraits<mozilla::dom::DisplayMode>
: public ContiguousEnumSerializer<mozilla::dom::DisplayMode,
mozilla::dom::DisplayMode::Browser,
mozilla::dom::DisplayMode::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<mozilla::dom::DisplayMode> {};
template <>
struct ParamTraits<mozilla::dom::PrefersColorSchemeOverride>
: public ContiguousEnumSerializer<
mozilla::dom::PrefersColorSchemeOverride,
mozilla::dom::PrefersColorSchemeOverride::None,
mozilla::dom::PrefersColorSchemeOverride::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<
mozilla::dom::PrefersColorSchemeOverride> {};
template <>
struct ParamTraits<mozilla::dom::ExplicitActiveStatus>
@ -124,10 +119,8 @@ struct ParamTraits<mozilla::dom::ExplicitActiveStatus>
// Allow serialization and deserialization of TouchEventsOverride over IPC
template <>
struct ParamTraits<mozilla::dom::TouchEventsOverride>
: public ContiguousEnumSerializer<
mozilla::dom::TouchEventsOverride,
mozilla::dom::TouchEventsOverride::Disabled,
mozilla::dom::TouchEventsOverride::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<
mozilla::dom::TouchEventsOverride> {};
template <>
struct ParamTraits<mozilla::dom::EmbedderColorSchemes> {

View File

@ -20,7 +20,7 @@
#include "nsXULAppAPI.h"
#include "mozilla/PresState.h"
#include "mozilla/StaticPrefs_fission.h"
#include "mozilla/dom/BindingIPCUtils.h"
#include "mozilla/dom/BrowserParent.h"
#include "mozilla/dom/CanonicalBrowsingContext.h"
#include "mozilla/dom/ContentChild.h"
@ -1797,10 +1797,8 @@ namespace IPC {
// Allow sending mozilla::dom::WireframeRectType enums over IPC.
template <>
struct ParamTraits<mozilla::dom::WireframeRectType>
: public ContiguousEnumSerializer<
mozilla::dom::WireframeRectType,
mozilla::dom::WireframeRectType::Image,
mozilla::dom::WireframeRectType::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<
mozilla::dom::WireframeRectType> {};
template <>
struct ParamTraits<mozilla::dom::WireframeTaggedRect> {

View File

@ -6,9 +6,18 @@
#ifndef _mozilla_dom_BindingIPCUtils_h
#define _mozilla_dom_BindingIPCUtils_h
#include "mozilla/EnumTypeTraits.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "ipc/EnumSerializer.h"
namespace mozilla::dom {
template <class Enum>
using WebIDLEnumSerializer = IPC::ContiguousEnumSerializerInclusive<
Enum, ContiguousEnumValues<Enum>::min, ContiguousEnumValues<Enum>::max>;
} // namespace mozilla::dom
namespace IPC {
template <>
struct ParamTraits<mozilla::dom::CallerType>

View File

@ -323,7 +323,9 @@ struct ParamTraits<mozilla::dom::PredefinedColorSpace> final
mozilla::dom::PredefinedColorSpace> {
using T = mozilla::dom::PredefinedColorSpace;
static bool Validate(const T& val) { return val < T::EndGuard_; }
static bool Validate(const T& val) {
return val <= mozilla::ContiguousEnumValues<T>::max;
}
};
template <>

View File

@ -9,6 +9,7 @@
#include "ipc/EnumSerializer.h"
#include "X11UndefineNone.h"
#include "mozilla/dom/BindingIPCUtils.h"
#include "mozilla/dom/ClientBinding.h"
#include "mozilla/dom/ClientsBinding.h"
#include "mozilla/dom/DocumentBinding.h"
@ -17,21 +18,16 @@
namespace IPC {
template <>
struct ParamTraits<mozilla::dom::ClientType>
: public ContiguousEnumSerializer<mozilla::dom::ClientType,
mozilla::dom::ClientType::Window,
mozilla::dom::ClientType::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<mozilla::dom::ClientType> {};
template <>
struct ParamTraits<mozilla::dom::FrameType>
: public ContiguousEnumSerializer<mozilla::dom::FrameType,
mozilla::dom::FrameType::Auxiliary,
mozilla::dom::FrameType::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<mozilla::dom::FrameType> {};
template <>
struct ParamTraits<mozilla::dom::VisibilityState>
: public ContiguousEnumSerializer<
mozilla::dom::VisibilityState, mozilla::dom::VisibilityState::Hidden,
mozilla::dom::VisibilityState::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<mozilla::dom::VisibilityState> {
};
template <>
struct ParamTraits<mozilla::StorageAccess>

View File

@ -938,6 +938,13 @@ value passed to it as an argument is not one of the string values for the enum
then it returns `mozilla::Nothing()`, else it returns the right enum value in
the `mozilla::Maybe`.
`mozilla::dom::WebIDLEnumSerializer` is a templated alias in
[`BindingIPCUtils.h`](https://searchfox.org/mozilla-central/source/dom/bindings/BindingIPCUtils.h)
exported to `mozilla/dom/BindingIPCUtils.h` to implement an IPC serializer with
the right validation for WebIDL enums. It uses a
`mozilla::MaxContinuousEnumValue` that is generated for every WebIDL enum to
implement the validation.
#### Callback function types
Callback functions are represented as an object, inheriting from

View File

@ -9,6 +9,7 @@
#include "ipc/EnumSerializer.h"
#include "mozilla/dom/BindingIPCUtils.h"
#include "mozilla/dom/HeadersBinding.h"
#include "mozilla/dom/RequestBinding.h"
#include "mozilla/dom/ResponseBinding.h"
@ -17,41 +18,30 @@
namespace IPC {
template <>
struct ParamTraits<mozilla::dom::HeadersGuardEnum>
: public ContiguousEnumSerializer<
mozilla::dom::HeadersGuardEnum, mozilla::dom::HeadersGuardEnum::None,
mozilla::dom::HeadersGuardEnum::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<
mozilla::dom::HeadersGuardEnum> {};
template <>
struct ParamTraits<mozilla::dom::ReferrerPolicy>
: public ContiguousEnumSerializer<mozilla::dom::ReferrerPolicy,
mozilla::dom::ReferrerPolicy::_empty,
mozilla::dom::ReferrerPolicy::EndGuard_> {
: public mozilla::dom::WebIDLEnumSerializer<mozilla::dom::ReferrerPolicy> {
};
template <>
struct ParamTraits<mozilla::dom::RequestMode>
: public ContiguousEnumSerializer<mozilla::dom::RequestMode,
mozilla::dom::RequestMode::Same_origin,
mozilla::dom::RequestMode::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<mozilla::dom::RequestMode> {};
template <>
struct ParamTraits<mozilla::dom::RequestCredentials>
: public ContiguousEnumSerializer<
mozilla::dom::RequestCredentials,
mozilla::dom::RequestCredentials::Omit,
mozilla::dom::RequestCredentials::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<
mozilla::dom::RequestCredentials> {};
template <>
struct ParamTraits<mozilla::dom::RequestCache>
: public ContiguousEnumSerializer<mozilla::dom::RequestCache,
mozilla::dom::RequestCache::Default,
mozilla::dom::RequestCache::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<mozilla::dom::RequestCache> {};
template <>
struct ParamTraits<mozilla::dom::RequestRedirect>
: public ContiguousEnumSerializer<
mozilla::dom::RequestRedirect, mozilla::dom::RequestRedirect::Follow,
mozilla::dom::RequestRedirect::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<mozilla::dom::RequestRedirect> {
};
template <>
struct ParamTraits<mozilla::dom::ResponseType>
: public ContiguousEnumSerializer<mozilla::dom::ResponseType,
mozilla::dom::ResponseType::Basic,
mozilla::dom::ResponseType::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<mozilla::dom::ResponseType> {};
template <>
struct ParamTraits<mozilla::dom::FetchDriverObserver::EndReason>
: public ContiguousEnumSerializerInclusive<

View File

@ -8,6 +8,7 @@
#define mozilla_dom_gamepad_GamepadMessageUtils_h
#include "ipc/EnumSerializer.h"
#include "mozilla/dom/BindingIPCUtils.h"
#include "mozilla/dom/GamepadBinding.h"
#include "mozilla/dom/GamepadHandle.h"
#include "mozilla/dom/GamepadLightIndicatorBinding.h"
@ -18,24 +19,17 @@ namespace IPC {
template <>
struct ParamTraits<mozilla::dom::GamepadLightIndicatorType>
: public ContiguousEnumSerializer<
mozilla::dom::GamepadLightIndicatorType,
mozilla::dom::GamepadLightIndicatorType(0),
mozilla::dom::GamepadLightIndicatorType(
mozilla::dom::GamepadLightIndicatorType::EndGuard_)> {};
: public mozilla::dom::WebIDLEnumSerializer<
mozilla::dom::GamepadLightIndicatorType> {};
template <>
struct ParamTraits<mozilla::dom::GamepadMappingType>
: public ContiguousEnumSerializer<
mozilla::dom::GamepadMappingType, mozilla::dom::GamepadMappingType(0),
mozilla::dom::GamepadMappingType(
mozilla::dom::GamepadMappingType::EndGuard_)> {};
: public mozilla::dom::WebIDLEnumSerializer<
mozilla::dom::GamepadMappingType> {};
template <>
struct ParamTraits<mozilla::dom::GamepadHand>
: public ContiguousEnumSerializer<
mozilla::dom::GamepadHand, mozilla::dom::GamepadHand(0),
mozilla::dom::GamepadHand(mozilla::dom::GamepadHand::EndGuard_)> {};
: public mozilla::dom::WebIDLEnumSerializer<mozilla::dom::GamepadHand> {};
template <>
struct ParamTraits<mozilla::dom::GamepadCapabilityFlags>

View File

@ -10,6 +10,7 @@
#include "ipc/EnumSerializer.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "mozilla/dom/BindingIPCUtils.h"
#include "mozilla/dom/indexedDB/Key.h"
#include "mozilla/dom/indexedDB/KeyPath.h"
#include "mozilla/dom/IDBCursor.h"
@ -61,10 +62,8 @@ struct ParamTraits<mozilla::dom::indexedDB::KeyPath> {
template <>
struct ParamTraits<mozilla::dom::IDBCursor::Direction>
: public ContiguousEnumSerializer<
mozilla::dom::IDBCursor::Direction,
mozilla::dom::IDBCursor::Direction::Next,
mozilla::dom::IDBCursor::Direction::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<
mozilla::dom::IDBCursor::Direction> {};
template <>
struct ParamTraits<mozilla::dom::IDBTransaction::Mode>

View File

@ -5,9 +5,9 @@
#ifndef DOM_MEDIA_IPC_MFCDMSERIALIZERS_H_
#define DOM_MEDIA_IPC_MFCDMSERIALIZERS_H_
#include "ipc/EnumSerializer.h"
#include "MediaData.h"
#include "mozilla/KeySystemConfig.h"
#include "mozilla/dom/BindingIPCUtils.h"
#include "mozilla/dom/MediaKeyMessageEventBinding.h"
#include "mozilla/dom/MediaKeyStatusMapBinding.h"
@ -35,23 +35,17 @@ struct ParamTraits<mozilla::CryptoScheme>
template <>
struct ParamTraits<mozilla::dom::MediaKeyMessageType>
: public ContiguousEnumSerializer<
mozilla::dom::MediaKeyMessageType,
mozilla::dom::MediaKeyMessageType::License_request,
mozilla::dom::MediaKeyMessageType::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<
mozilla::dom::MediaKeyMessageType> {};
template <>
struct ParamTraits<mozilla::dom::MediaKeyStatus>
: public ContiguousEnumSerializer<mozilla::dom::MediaKeyStatus,
mozilla::dom::MediaKeyStatus::Usable,
mozilla::dom::MediaKeyStatus::EndGuard_> {
: public mozilla::dom::WebIDLEnumSerializer<mozilla::dom::MediaKeyStatus> {
};
template <>
struct ParamTraits<mozilla::dom::HDCPVersion>
: public ContiguousEnumSerializer<mozilla::dom::HDCPVersion,
mozilla::dom::HDCPVersion::_1_0,
mozilla::dom::HDCPVersion::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<mozilla::dom::HDCPVersion> {};
} // namespace IPC

View File

@ -6,6 +6,7 @@
#define DOM_MEDIA_MEDIASESSION_MEDIASESSIONIPCUTILS_H_
#include "ipc/EnumSerializer.h"
#include "mozilla/dom/BindingIPCUtils.h"
#include "MediaMetadata.h"
#include "mozilla/dom/MediaSession.h"
#include "mozilla/dom/MediaSessionBinding.h"
@ -87,17 +88,13 @@ struct ParamTraits<mozilla::dom::PositionState> {
template <>
struct ParamTraits<mozilla::dom::MediaSessionPlaybackState>
: public ContiguousEnumSerializer<
mozilla::dom::MediaSessionPlaybackState,
mozilla::dom::MediaSessionPlaybackState::None,
mozilla::dom::MediaSessionPlaybackState::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<
mozilla::dom::MediaSessionPlaybackState> {};
template <>
struct ParamTraits<mozilla::dom::MediaSessionAction>
: public ContiguousEnumSerializer<
mozilla::dom::MediaSessionAction,
mozilla::dom::MediaSessionAction::Play,
mozilla::dom::MediaSessionAction::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<
mozilla::dom::MediaSessionAction> {};
} // namespace IPC

View File

@ -8,6 +8,7 @@
#include "WebrtcIPCTraits.h"
#include "ipc/EnumSerializer.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "mozilla/dom/BindingIPCUtils.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/RTCDataChannelBinding.h"
#include "mozilla/dom/RTCStatsReportBinding.h"
@ -62,30 +63,22 @@ namespace IPC {
template <>
struct ParamTraits<mozilla::dom::RTCStatsType>
: public ContiguousEnumSerializer<mozilla::dom::RTCStatsType,
mozilla::dom::RTCStatsType::Codec,
mozilla::dom::RTCStatsType::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<mozilla::dom::RTCStatsType> {};
template <>
struct ParamTraits<mozilla::dom::RTCStatsIceCandidatePairState>
: public ContiguousEnumSerializer<
mozilla::dom::RTCStatsIceCandidatePairState,
mozilla::dom::RTCStatsIceCandidatePairState::Frozen,
mozilla::dom::RTCStatsIceCandidatePairState::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<
mozilla::dom::RTCStatsIceCandidatePairState> {};
template <>
struct ParamTraits<mozilla::dom::RTCIceCandidateType>
: public ContiguousEnumSerializer<
mozilla::dom::RTCIceCandidateType,
mozilla::dom::RTCIceCandidateType::Host,
mozilla::dom::RTCIceCandidateType::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<
mozilla::dom::RTCIceCandidateType> {};
template <>
struct ParamTraits<mozilla::dom::RTCBundlePolicy>
: public ContiguousEnumSerializer<
mozilla::dom::RTCBundlePolicy,
mozilla::dom::RTCBundlePolicy::Balanced,
mozilla::dom::RTCBundlePolicy::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<mozilla::dom::RTCBundlePolicy> {
};
DEFINE_IPC_SERIALIZER_WITH_FIELDS(mozilla::dom::RTCIceServerInternal, mUrls,
mCredentialProvided, mUserNameProvided);
@ -218,10 +211,8 @@ DEFINE_IPC_SERIALIZER_WITH_FIELDS(mozilla::dom::RTCDataChannelStats, mId,
template <>
struct ParamTraits<mozilla::dom::RTCDataChannelState>
: public ContiguousEnumSerializer<
mozilla::dom::RTCDataChannelState,
mozilla::dom::RTCDataChannelState::Connecting,
mozilla::dom::RTCDataChannelState::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<
mozilla::dom::RTCDataChannelState> {};
DEFINE_IPC_SERIALIZER_WITH_FIELDS(mozilla::dom::RTCCodecStats, mTimestamp,
mType, mId, mPayloadType, mCodecType,
@ -230,9 +221,7 @@ DEFINE_IPC_SERIALIZER_WITH_FIELDS(mozilla::dom::RTCCodecStats, mTimestamp,
template <>
struct ParamTraits<mozilla::dom::RTCCodecType>
: public ContiguousEnumSerializer<mozilla::dom::RTCCodecType,
mozilla::dom::RTCCodecType::Encode,
mozilla::dom::RTCCodecType::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<mozilla::dom::RTCCodecType> {};
} // namespace IPC
#endif // _WEBRTC_GLOBAL_H_

View File

@ -9,6 +9,7 @@
#include "ipc/IPCMessageUtils.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/BindingIPCUtils.h"
#include "mozilla/dom/RTCConfigurationBinding.h"
#include "mozilla/media/webrtc/WebrtcGlobal.h"
#include "mozilla/dom/CandidateInfo.h"
@ -62,17 +63,15 @@ struct ParamTraits<mozilla::dom::OwningStringOrStringSequence> {
}
};
template <typename T>
struct WebidlEnumSerializer
: public ContiguousEnumSerializer<T, T(0), T::EndGuard_> {};
template <>
struct ParamTraits<mozilla::dom::RTCIceCredentialType>
: public WebidlEnumSerializer<mozilla::dom::RTCIceCredentialType> {};
: public mozilla::dom::WebIDLEnumSerializer<
mozilla::dom::RTCIceCredentialType> {};
template <>
struct ParamTraits<mozilla::dom::RTCIceTransportPolicy>
: public WebidlEnumSerializer<mozilla::dom::RTCIceTransportPolicy> {};
: public mozilla::dom::WebIDLEnumSerializer<
mozilla::dom::RTCIceTransportPolicy> {};
DEFINE_IPC_SERIALIZER_WITH_FIELDS(mozilla::dom::RTCIceServer, mCredential,
mCredentialType, mUrl, mUrls, mUsername)

View File

@ -11,6 +11,7 @@
// Undo X11/X.h's definition of None
#undef None
#include "mozilla/dom/BindingIPCUtils.h"
#include "mozilla/dom/ServiceWorkerBinding.h"
#include "mozilla/dom/ServiceWorkerRegistrationBinding.h"
@ -18,17 +19,13 @@ namespace IPC {
template <>
struct ParamTraits<mozilla::dom::ServiceWorkerState>
: public ContiguousEnumSerializer<
mozilla::dom::ServiceWorkerState,
mozilla::dom::ServiceWorkerState::Parsed,
mozilla::dom::ServiceWorkerState::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<
mozilla::dom::ServiceWorkerState> {};
template <>
struct ParamTraits<mozilla::dom::ServiceWorkerUpdateViaCache>
: public ContiguousEnumSerializer<
mozilla::dom::ServiceWorkerUpdateViaCache,
mozilla::dom::ServiceWorkerUpdateViaCache::Imports,
mozilla::dom::ServiceWorkerUpdateViaCache::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<
mozilla::dom::ServiceWorkerUpdateViaCache> {};
} // namespace IPC

View File

@ -9,6 +9,7 @@
#include "WebGPUTypes.h"
#include "ipc/EnumSerializer.h"
#include "ipc/IPCMessageUtils.h"
#include "mozilla/dom/BindingIPCUtils.h"
#include "mozilla/dom/WebGPUBinding.h"
#include "mozilla/webgpu/ffi/wgpu.h"
@ -20,7 +21,9 @@ namespace IPC {
: public ContiguousEnumSerializer<something, something(0), guard> {}
#define DEFINE_IPC_SERIALIZER_DOM_ENUM(something) \
DEFINE_IPC_SERIALIZER_ENUM_GUARD(something, something::EndGuard_)
template <> \
struct ParamTraits<something> \
: public mozilla::dom::WebIDLEnumSerializer<something> {}
#define DEFINE_IPC_SERIALIZER_FFI_ENUM(something) \
DEFINE_IPC_SERIALIZER_ENUM_GUARD(something, something##_Sentinel)

View File

@ -6,7 +6,7 @@
#ifndef _mozilla_dom_WorkerIPCUtils_h
#define _mozilla_dom_WorkerIPCUtils_h
#include "ipc/EnumSerializer.h"
#include "mozilla/dom/BindingIPCUtils.h"
// Undo X11/X.h's definition of None
#undef None
@ -17,9 +17,7 @@ namespace IPC {
template <>
struct ParamTraits<mozilla::dom::WorkerType>
: public ContiguousEnumSerializer<mozilla::dom::WorkerType,
mozilla::dom::WorkerType::Classic,
mozilla::dom::WorkerType::EndGuard_> {};
: public mozilla::dom::WebIDLEnumSerializer<mozilla::dom::WorkerType> {};
} // namespace IPC