blob: f964c8320570c3171157ee907cc5206cc7862df6 [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
24void ParamTraits<HandleWin>::Write(Message* m, const param_type& p) {
25 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
33bool ParamTraits<HandleWin>::Read(const Message* m,
34 base::PickleIterator* iter,
35 param_type* r) {
36 scoped_refptr<MessageAttachment> attachment;
37 if (!m->ReadAttachment(iter, &attachment))
38 return false;
39 if (attachment->GetType() != MessageAttachment::TYPE_BROKERABLE_ATTACHMENT)
40 return false;
41 BrokerableAttachment* brokerable_attachment =
42 static_cast<BrokerableAttachment*>(attachment.get());
43 if (brokerable_attachment->GetBrokerableType() !=
44 BrokerableAttachment::WIN_HANDLE) {
45 return false;
46 }
47 IPC::internal::HandleAttachmentWin* handle_attachment =
48 static_cast<IPC::internal::HandleAttachmentWin*>(brokerable_attachment);
49 r->set_handle(handle_attachment->get_handle());
erikchen3d87ecf72016-01-08 02:17:0450 handle_attachment->reset_handle_ownership();
erikchen959039d2015-08-11 21:17:4751 return true;
52}
53
54// static
55void ParamTraits<HandleWin>::Log(const param_type& p, std::string* l) {
brucedawson5604a11d2015-10-06 19:22:0056 l->append(base::StringPrintf("0x%p", p.get_handle()));
erikchen959039d2015-08-11 21:17:4757 l->append(base::IntToString(p.get_permissions()));
58}
59
60} // namespace IPC