Bug 1707290 [wpt PR 28671] - Auto-expand details elements for find-in-page, a=testonly
[gecko.git] / dom / bindings / ErrorIPCUtils.h
blobe492618ececd1ac0b03528b8ceaaa0fd87448fc8
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(Message* aMsg, 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(aMsg, aParam.mResult);
46 WriteParam(aMsg, aParam.IsErrorWithMessage());
47 WriteParam(aMsg, aParam.IsDOMException());
48 if (aParam.IsErrorWithMessage()) {
49 aParam.SerializeMessage(aMsg);
50 } else if (aParam.IsDOMException()) {
51 aParam.SerializeDOMExceptionInfo(aMsg);
55 static void Write(Message* aMsg, paramType&& aParam) {
56 Write(aMsg, static_cast<const paramType&>(aParam));
57 aParam.SuppressException();
60 static bool Read(const Message* aMsg, PickleIterator* aIter,
61 paramType* aResult) {
62 paramType readValue;
63 if (!ReadParam(aMsg, aIter, &readValue.mResult)) {
64 return false;
66 bool hasMessage = false;
67 if (!ReadParam(aMsg, aIter, &hasMessage)) {
68 return false;
70 bool hasDOMExceptionInfo = false;
71 if (!ReadParam(aMsg, aIter, &hasDOMExceptionInfo)) {
72 return false;
74 if (hasMessage && hasDOMExceptionInfo) {
75 // Shouldn't have both!
76 return false;
78 if (hasMessage && !readValue.DeserializeMessage(aMsg, aIter)) {
79 return false;
80 } else if (hasDOMExceptionInfo &&
81 !readValue.DeserializeDOMExceptionInfo(aMsg, aIter)) {
82 return false;
84 *aResult = std::move(readValue);
85 return true;
89 template <>
90 struct ParamTraits<mozilla::CopyableErrorResult> {
91 typedef mozilla::CopyableErrorResult paramType;
93 static void Write(Message* aMsg, const paramType& aParam) {
94 ParamTraits<mozilla::ErrorResult>::Write(aMsg, aParam);
97 static bool Read(const Message* aMsg, PickleIterator* aIter,
98 paramType* aResult) {
99 // We can't cast *aResult to ErrorResult&, so cheat and just cast
100 // to ErrorResult*.
101 return ParamTraits<mozilla::ErrorResult>::Read(
102 aMsg, aIter, reinterpret_cast<mozilla::ErrorResult*>(aResult));
106 } // namespace IPC
108 #endif