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