tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

RustMessageUtils.h (2726B)


      1 /* -*- Mode: C++; tab-width: 4; 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
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #ifndef mozilla_ipc_RustMessageUtils_h
      7 #define mozilla_ipc_RustMessageUtils_h
      8 
      9 #include "chrome/common/ipc_message_utils.h"
     10 
     11 // Some macros for rust integrations. See ipc/rust/ipdl_utils
     12 #define MOZ_DEFINE_RUST_PARAMTRAITS(type_, serializer_, deserializer_)        \
     13  extern "C" uint8_t* serializer_(const type_*, size_t* len, size_t* cap);    \
     14  extern "C" bool deserializer_(const uint8_t*, size_t len, type_*);          \
     15                                                                              \
     16  template <>                                                                 \
     17  struct IPC::ParamTraits<type_> {                                            \
     18    using paramType = type_;                                                  \
     19    static void Write(IPC::MessageWriter* aWriter, const paramType& aParam) { \
     20      size_t len, cap;                                                        \
     21      uint8_t* buf = serializer_(&aParam, &cap, &len);                        \
     22      MOZ_DIAGNOSTIC_ASSERT(buf, #type_ " serialization failed");             \
     23      WriteParam(aWriter, mozilla::ipc::ByteBuf(buf, len, cap));              \
     24    }                                                                         \
     25    static IPC::ReadResult<paramType> Read(IPC::MessageReader* aReader) {     \
     26      mozilla::ipc::ByteBuf in;                                               \
     27      IPC::ReadResult<paramType> result;                                      \
     28      if (!ReadParam(aReader, &in) || !in.mData) {                            \
     29        return result;                                                        \
     30      }                                                                       \
     31      /* TODO: Should be able to initialize `result` in-place instead */      \
     32      mozilla::AlignedStorage2<paramType> value;                              \
     33      if (!deserializer_(in.mData, in.mLen, value.addr())) {                  \
     34        return result;                                                        \
     35      }                                                                       \
     36      result = std::move(*value.addr());                                      \
     37      value.addr()->~paramType();                                             \
     38      return result;                                                          \
     39    }                                                                         \
     40  };
     41 
     42 #endif