1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef IPC_ErrorIPCUtils_h
8 #define IPC_ErrorIPCUtils_h
12 #include "ipc/EnumSerializer.h"
13 #include "ipc/IPCMessageUtils.h"
14 #include "mozilla/Assertions.h"
15 #include "mozilla/ErrorResult.h"
20 struct ParamTraits
<mozilla::dom::ErrNum
>
21 : public ContiguousEnumSerializer
<
22 mozilla::dom::ErrNum
, mozilla::dom::ErrNum(0),
23 mozilla::dom::ErrNum(mozilla::dom::Err_Limit
)> {};
26 struct ParamTraits
<mozilla::ErrorResult
> {
27 typedef mozilla::ErrorResult paramType
;
29 static void Write(MessageWriter
* aWriter
, const paramType
& aParam
) {
30 // It should be the case that mMightHaveUnreportedJSException can only be
31 // true when we're expecting a JS exception. We cannot send such messages
32 // over the IPC channel since there is no sane way of transferring the JS
33 // value over to the other side. Callers should never do that.
34 MOZ_ASSERT_IF(aParam
.IsJSException(),
35 aParam
.mMightHaveUnreportedJSException
);
36 if (aParam
.IsJSException()
38 || aParam
.mMightHaveUnreportedJSException
42 "Cannot encode an ErrorResult representing a Javascript exception");
45 WriteParam(aWriter
, aParam
.mResult
);
46 WriteParam(aWriter
, aParam
.IsErrorWithMessage());
47 WriteParam(aWriter
, aParam
.IsDOMException());
48 if (aParam
.IsErrorWithMessage()) {
49 aParam
.SerializeMessage(aWriter
);
50 } else if (aParam
.IsDOMException()) {
51 aParam
.SerializeDOMExceptionInfo(aWriter
);
55 static void Write(MessageWriter
* aWriter
, paramType
&& aParam
) {
56 Write(aWriter
, static_cast<const paramType
&>(aParam
));
57 aParam
.SuppressException();
60 static bool Read(MessageReader
* aReader
, paramType
* aResult
) {
62 if (!ReadParam(aReader
, &readValue
.mResult
)) {
65 bool hasMessage
= false;
66 if (!ReadParam(aReader
, &hasMessage
)) {
69 bool hasDOMExceptionInfo
= false;
70 if (!ReadParam(aReader
, &hasDOMExceptionInfo
)) {
73 if (hasMessage
&& hasDOMExceptionInfo
) {
74 // Shouldn't have both!
77 if (hasMessage
&& !readValue
.DeserializeMessage(aReader
)) {
79 } else if (hasDOMExceptionInfo
&&
80 !readValue
.DeserializeDOMExceptionInfo(aReader
)) {
83 *aResult
= std::move(readValue
);
89 struct ParamTraits
<mozilla::CopyableErrorResult
> {
90 typedef mozilla::CopyableErrorResult paramType
;
92 static void Write(MessageWriter
* aWriter
, const paramType
& aParam
) {
93 ParamTraits
<mozilla::ErrorResult
>::Write(aWriter
, aParam
);
96 static bool Read(MessageReader
* aReader
, paramType
* aResult
) {
97 // We can't cast *aResult to ErrorResult&, so cheat and just cast
99 return ParamTraits
<mozilla::ErrorResult
>::Read(
100 aReader
, reinterpret_cast<mozilla::ErrorResult
*>(aResult
));