ipc_mojo_message_helper.cc 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Copyright (c) 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ipc/ipc_mojo_message_helper.h"
  5. #include <utility>
  6. #include "base/logging.h"
  7. #include "ipc/ipc_mojo_handle_attachment.h"
  8. namespace IPC {
  9. // static
  10. bool MojoMessageHelper::WriteMessagePipeTo(
  11. base::Pickle* message,
  12. mojo::ScopedMessagePipeHandle handle) {
  13. message->WriteAttachment(new internal::MojoHandleAttachment(
  14. mojo::ScopedHandle::From(std::move(handle))));
  15. return true;
  16. }
  17. // static
  18. bool MojoMessageHelper::ReadMessagePipeFrom(
  19. const base::Pickle* message,
  20. base::PickleIterator* iter,
  21. mojo::ScopedMessagePipeHandle* handle) {
  22. scoped_refptr<base::Pickle::Attachment> attachment;
  23. if (!message->ReadAttachment(iter, &attachment)) {
  24. LOG(ERROR) << "Failed to read attachment for message pipe.";
  25. return false;
  26. }
  27. MessageAttachment::Type type =
  28. static_cast<MessageAttachment*>(attachment.get())->GetType();
  29. if (type != MessageAttachment::Type::MOJO_HANDLE) {
  30. LOG(ERROR) << "Unxpected attachment type:" << static_cast<int>(type);
  31. return false;
  32. }
  33. handle->reset(mojo::MessagePipeHandle(
  34. static_cast<internal::MojoHandleAttachment*>(attachment.get())
  35. ->TakeHandle()
  36. .release()
  37. .value()));
  38. return true;
  39. }
  40. MojoMessageHelper::MojoMessageHelper() = default;
  41. } // namespace IPC