blob: 60fa54c7cd1643cd08a3ed8093ab7d02d4f4e7b7 [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());
erikchen959039d2015-08-11 21:17:4741 if (attachment->GetType() != MessageAttachment::TYPE_BROKERABLE_ATTACHMENT)
42 return false;
43 BrokerableAttachment* brokerable_attachment =
rockot502c94f2016-02-03 20:20:1644 static_cast<BrokerableAttachment*>(attachment);
erikchen959039d2015-08-11 21:17:4745 if (brokerable_attachment->GetBrokerableType() !=
46 BrokerableAttachment::WIN_HANDLE) {
47 return false;
48 }
49 IPC::internal::HandleAttachmentWin* handle_attachment =
50 static_cast<IPC::internal::HandleAttachmentWin*>(brokerable_attachment);
51 r->set_handle(handle_attachment->get_handle());
erikchen3d87ecf72016-01-08 02:17:0452 handle_attachment->reset_handle_ownership();
erikchen959039d2015-08-11 21:17:4753 return true;
54}
55
56// static
57void ParamTraits<HandleWin>::Log(const param_type& p, std::string* l) {
brucedawson5604a11d2015-10-06 19:22:0058 l->append(base::StringPrintf("0x%p", p.get_handle()));
erikchen959039d2015-08-11 21:17:4759 l->append(base::IntToString(p.get_permissions()));
60}
61
62} // namespace IPC