xref: /aosp_15_r20/external/cronet/ipc/handle_win.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/handle_win.h"
6 
7 #include <utility>
8 
9 #include "base/memory/ref_counted.h"
10 #include "base/notreached.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/stringprintf.h"
13 #include "ipc/handle_attachment_win.h"
14 #include "ipc/ipc_message.h"
15 
16 namespace IPC {
17 
HandleWin()18 HandleWin::HandleWin() : handle_(INVALID_HANDLE_VALUE) {}
19 
HandleWin(const HANDLE & handle)20 HandleWin::HandleWin(const HANDLE& handle) : handle_(handle) {}
21 
22 // static
Write(base::Pickle * m,const param_type & p)23 void ParamTraits<HandleWin>::Write(base::Pickle* m, const param_type& p) {
24   scoped_refptr<IPC::internal::HandleAttachmentWin> attachment(
25       new IPC::internal::HandleAttachmentWin(p.get_handle()));
26   if (!m->WriteAttachment(std::move(attachment)))
27     NOTREACHED();
28 }
29 
30 // static
Read(const base::Pickle * m,base::PickleIterator * iter,param_type * r)31 bool ParamTraits<HandleWin>::Read(const base::Pickle* m,
32                                   base::PickleIterator* iter,
33                                   param_type* r) {
34   scoped_refptr<base::Pickle::Attachment> base_attachment;
35   if (!m->ReadAttachment(iter, &base_attachment))
36     return false;
37   MessageAttachment* attachment =
38       static_cast<MessageAttachment*>(base_attachment.get());
39   if (attachment->GetType() != MessageAttachment::Type::WIN_HANDLE)
40     return false;
41   IPC::internal::HandleAttachmentWin* handle_attachment =
42       static_cast<IPC::internal::HandleAttachmentWin*>(attachment);
43   r->set_handle(handle_attachment->Take());
44   return true;
45 }
46 
47 // static
Log(const param_type & p,std::string * l)48 void ParamTraits<HandleWin>::Log(const param_type& p, std::string* l) {
49   l->append(base::StringPrintf("0x%p", p.get_handle()));
50 }
51 
52 }  // namespace IPC
53