xref: /aosp_15_r20/external/cronet/ipc/mach_port_mac.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/mach_port_mac.h"
6 
7 #include "base/memory/ref_counted.h"
8 #include "base/notreached.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/stringprintf.h"
11 #include "ipc/mach_port_attachment_mac.h"
12 
13 namespace IPC {
14 
15 // static
Write(base::Pickle * m,const param_type & p)16 void ParamTraits<MachPortMac>::Write(base::Pickle* m, const param_type& p) {
17   if (!m->WriteAttachment(
18           new IPC::internal::MachPortAttachmentMac(p.get_mach_port()))) {
19     NOTREACHED();
20   }
21 }
22 
23 // static
Read(const base::Pickle * m,base::PickleIterator * iter,param_type * r)24 bool ParamTraits<MachPortMac>::Read(const base::Pickle* m,
25                                     base::PickleIterator* iter,
26                                     param_type* r) {
27   scoped_refptr<base::Pickle::Attachment> base_attachment;
28   if (!m->ReadAttachment(iter, &base_attachment))
29     return false;
30   MessageAttachment* attachment =
31       static_cast<MessageAttachment*>(base_attachment.get());
32   if (attachment->GetType() != MessageAttachment::Type::MACH_PORT)
33     return false;
34   IPC::internal::MachPortAttachmentMac* mach_port_attachment =
35       static_cast<IPC::internal::MachPortAttachmentMac*>(attachment);
36   r->set_mach_port(mach_port_attachment->get_mach_port());
37   mach_port_attachment->reset_mach_port_ownership();
38   return true;
39 }
40 
41 // static
Log(const param_type & p,std::string * l)42 void ParamTraits<MachPortMac>::Log(const param_type& p, std::string* l) {
43   l->append(base::StringPrintf("mach port: 0x%X", p.get_mach_port()));
44 }
45 
46 }  // namespace IPC
47