Bug 1856663 - Add more chunks for Android mochitest-plain. r=jmaher,taskgraph-reviewe...
[gecko.git] / dom / bindings / ErrorIPCUtils.h
blob064d85150337b46e05b4cb8cff96e5c6f12ca0d3
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
10 #include <utility>
12 #include "ipc/EnumSerializer.h"
13 #include "ipc/IPCMessageUtils.h"
14 #include "mozilla/Assertions.h"
15 #include "mozilla/ErrorResult.h"
17 namespace IPC {
19 template <>
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)> {};
25 template <>
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()
37 #ifdef DEBUG
38 || aParam.mMightHaveUnreportedJSException
39 #endif
40 ) {
41 MOZ_CRASH(
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) {
61 paramType readValue;
62 if (!ReadParam(aReader, &readValue.mResult)) {
63 return false;
65 bool hasMessage = false;
66 if (!ReadParam(aReader, &hasMessage)) {
67 return false;
69 bool hasDOMExceptionInfo = false;
70 if (!ReadParam(aReader, &hasDOMExceptionInfo)) {
71 return false;
73 if (hasMessage && hasDOMExceptionInfo) {
74 // Shouldn't have both!
75 return false;
77 if (hasMessage && !readValue.DeserializeMessage(aReader)) {
78 return false;
79 } else if (hasDOMExceptionInfo &&
80 !readValue.DeserializeDOMExceptionInfo(aReader)) {
81 return false;
83 *aResult = std::move(readValue);
84 return true;
88 template <>
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
98 // to ErrorResult*.
99 return ParamTraits<mozilla::ErrorResult>::Read(
100 aReader, reinterpret_cast<mozilla::ErrorResult*>(aResult));
104 } // namespace IPC
106 #endif