ErrorIPCUtils.h (3364B)
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/. */ 6 7 #ifndef IPC_ErrorIPCUtils_h 8 #define IPC_ErrorIPCUtils_h 9 10 #include <utility> 11 12 #include "ipc/EnumSerializer.h" 13 #include "ipc/IPCMessageUtils.h" 14 #include "mozilla/Assertions.h" 15 #include "mozilla/ErrorResult.h" 16 17 namespace IPC { 18 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)> {}; 24 25 template <> 26 struct ParamTraits<mozilla::ErrorResult> { 27 typedef mozilla::ErrorResult paramType; 28 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"); 43 } 44 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); 52 } 53 } 54 55 static void Write(MessageWriter* aWriter, paramType&& aParam) { 56 Write(aWriter, static_cast<const paramType&>(aParam)); 57 aParam.SuppressException(); 58 } 59 60 static bool Read(MessageReader* aReader, paramType* aResult) { 61 paramType readValue; 62 if (!ReadParam(aReader, &readValue.mResult)) { 63 return false; 64 } 65 bool hasMessage = false; 66 if (!ReadParam(aReader, &hasMessage)) { 67 return false; 68 } 69 bool hasDOMExceptionInfo = false; 70 if (!ReadParam(aReader, &hasDOMExceptionInfo)) { 71 return false; 72 } 73 if (hasMessage && hasDOMExceptionInfo) { 74 // Shouldn't have both! 75 return false; 76 } 77 if (hasMessage && !readValue.DeserializeMessage(aReader)) { 78 return false; 79 } else if (hasDOMExceptionInfo && 80 !readValue.DeserializeDOMExceptionInfo(aReader)) { 81 return false; 82 } 83 *aResult = std::move(readValue); 84 return true; 85 } 86 }; 87 88 template <> 89 struct ParamTraits<mozilla::CopyableErrorResult> { 90 typedef mozilla::CopyableErrorResult paramType; 91 92 static void Write(MessageWriter* aWriter, const paramType& aParam) { 93 ParamTraits<mozilla::ErrorResult>::Write(aWriter, aParam); 94 } 95 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)); 101 } 102 }; 103 104 } // namespace IPC 105 106 #endif