ErrorIPCUtils.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  4. * You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "ipc/IPCMessageUtils.h"
  6. #include "mozilla/ErrorResult.h"
  7. #include "mozilla/Assertions.h"
  8. #include "mozilla/Move.h"
  9. #ifndef IPC_ErrorIPCUtils_h
  10. #define IPC_ErrorIPCUtils_h
  11. namespace IPC {
  12. template<>
  13. struct ParamTraits<mozilla::dom::ErrNum> :
  14. public ContiguousEnumSerializer<mozilla::dom::ErrNum,
  15. mozilla::dom::ErrNum(0),
  16. mozilla::dom::ErrNum(mozilla::dom::Err_Limit)> {};
  17. template<>
  18. struct ParamTraits<mozilla::ErrorResult>
  19. {
  20. typedef mozilla::ErrorResult paramType;
  21. static void Write(Message* aMsg, const paramType& aParam)
  22. {
  23. // It should be the case that mMightHaveUnreportedJSException can only be
  24. // true when we're expecting a JS exception. We cannot send such messages
  25. // over the IPC channel since there is no sane way of transferring the JS
  26. // value over to the other side. Callers should never do that.
  27. MOZ_ASSERT_IF(aParam.IsJSException(), aParam.mMightHaveUnreportedJSException);
  28. if (aParam.IsJSException()
  29. #ifdef DEBUG
  30. || aParam.mMightHaveUnreportedJSException
  31. #endif
  32. ) {
  33. MOZ_CRASH("Cannot encode an ErrorResult representing a Javascript exception");
  34. }
  35. WriteParam(aMsg, aParam.mResult);
  36. WriteParam(aMsg, aParam.IsErrorWithMessage());
  37. WriteParam(aMsg, aParam.IsDOMException());
  38. if (aParam.IsErrorWithMessage()) {
  39. aParam.SerializeMessage(aMsg);
  40. } else if (aParam.IsDOMException()) {
  41. aParam.SerializeDOMExceptionInfo(aMsg);
  42. }
  43. }
  44. static bool Read(const Message* aMsg, PickleIterator* aIter, paramType* aResult)
  45. {
  46. paramType readValue;
  47. if (!ReadParam(aMsg, aIter, &readValue.mResult)) {
  48. return false;
  49. }
  50. bool hasMessage = false;
  51. if (!ReadParam(aMsg, aIter, &hasMessage)) {
  52. return false;
  53. }
  54. bool hasDOMExceptionInfo = false;
  55. if (!ReadParam(aMsg, aIter, &hasDOMExceptionInfo)) {
  56. return false;
  57. }
  58. if (hasMessage && hasDOMExceptionInfo) {
  59. // Shouldn't have both!
  60. return false;
  61. }
  62. if (hasMessage && !readValue.DeserializeMessage(aMsg, aIter)) {
  63. return false;
  64. } else if (hasDOMExceptionInfo &&
  65. !readValue.DeserializeDOMExceptionInfo(aMsg, aIter)) {
  66. return false;
  67. }
  68. *aResult = Move(readValue);
  69. return true;
  70. }
  71. };
  72. } // namespace IPC
  73. #endif