blob: c8511e2017387ac523f238f6c8c76dae04f8c90d [file] [log] [blame]
erikchen959039d2015-08-11 21:17:471// Copyright 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
5#include "ipc/handle_win.h"
6
danakj4b041ab2015-12-04 20:12:277#include <utility>
8
erikchen959039d2015-08-11 21:17:479#include "base/logging.h"
10#include "base/memory/ref_counted.h"
11#include "base/strings/string_number_conversions.h"
erikchenb82097cc2015-10-12 23:27:5512#include "base/strings/stringprintf.h"
erikchen959039d2015-08-11 21:17:4713#include "ipc/handle_attachment_win.h"
erikchenb82097cc2015-10-12 23:27:5514#include "ipc/ipc_message.h"
erikchen959039d2015-08-11 21:17:4715
16namespace IPC {
17
erikchen98daa732015-09-25 18:30:0318HandleWin::HandleWin() : handle_(nullptr), permissions_(INVALID) {}
19
erikchen959039d2015-08-11 21:17:4720HandleWin::HandleWin(const HANDLE& handle, Permissions permissions)
21 : handle_(handle), permissions_(permissions) {}
22
23// static
rockot502c94f2016-02-03 20:20:1624void ParamTraits<HandleWin>::Write(base::Pickle* m, const param_type& p) {
erikchen959039d2015-08-11 21:17:4725 scoped_refptr<IPC::internal::HandleAttachmentWin> attachment(
26 new IPC::internal::HandleAttachmentWin(p.get_handle(),
27 p.get_permissions()));
danakj4b041ab2015-12-04 20:12:2728 if (!m->WriteAttachment(std::move(attachment)))
erikchen959039d2015-08-11 21:17:4729 NOTREACHED();
30}
31
32// static
rockot502c94f2016-02-03 20:20:1633bool ParamTraits<HandleWin>::Read(const base::Pickle* m,
erikchen959039d2015-08-11 21:17:4734 base::PickleIterator* iter,
35 param_type* r) {
rockot502c94f2016-02-03 20:20:1636 scoped_refptr<base::Pickle::Attachment> base_attachment;
37 if (!m->ReadAttachment(iter, &base_attachment))
erikchen959039d2015-08-11 21:17:4738 return false;
rockot502c94f2016-02-03 20:20:1639 MessageAttachment* attachment =
40 static_cast<MessageAttachment*>(base_attachment.get());
sammc6ed3efb2016-11-23 03:17:3541 if (attachment->GetType() != MessageAttachment::Type::WIN_HANDLE)
erikchen959039d2015-08-11 21:17:4742 return false;
erikchen959039d2015-08-11 21:17:4743 IPC::internal::HandleAttachmentWin* handle_attachment =
sammc6ed3efb2016-11-23 03:17:3544 static_cast<IPC::internal::HandleAttachmentWin*>(attachment);
erikchen959039d2015-08-11 21:17:4745 r->set_handle(handle_attachment->get_handle());
erikchen3d87ecf72016-01-08 02:17:0446 handle_attachment->reset_handle_ownership();
erikchen959039d2015-08-11 21:17:4747 return true;
48}
49
50// static
51void ParamTraits<HandleWin>::Log(const param_type& p, std::string* l) {
brucedawson5604a11d2015-10-06 19:22:0052 l->append(base::StringPrintf("0x%p", p.get_handle()));
erikchen959039d2015-08-11 21:17:4753 l->append(base::IntToString(p.get_permissions()));
54}
55
56} // namespace IPC