blob: ba981c1a57bca4ab013e708a204f102207886b84 [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
Wez51eaaad2017-08-09 05:51:3818HandleWin::HandleWin() : handle_(INVALID_HANDLE_VALUE) {}
erikchen98daa732015-09-25 18:30:0319
Wez51eaaad2017-08-09 05:51:3820HandleWin::HandleWin(const HANDLE& handle) : handle_(handle) {}
erikchen959039d2015-08-11 21:17:4721
22// static
rockot502c94f2016-02-03 20:20:1623void ParamTraits<HandleWin>::Write(base::Pickle* m, const param_type& p) {
erikchen959039d2015-08-11 21:17:4724 scoped_refptr<IPC::internal::HandleAttachmentWin> attachment(
Wez51eaaad2017-08-09 05:51:3825 new IPC::internal::HandleAttachmentWin(p.get_handle()));
danakj4b041ab2015-12-04 20:12:2726 if (!m->WriteAttachment(std::move(attachment)))
erikchen959039d2015-08-11 21:17:4727 NOTREACHED();
28}
29
30// static
rockot502c94f2016-02-03 20:20:1631bool ParamTraits<HandleWin>::Read(const base::Pickle* m,
erikchen959039d2015-08-11 21:17:4732 base::PickleIterator* iter,
33 param_type* r) {
rockot502c94f2016-02-03 20:20:1634 scoped_refptr<base::Pickle::Attachment> base_attachment;
35 if (!m->ReadAttachment(iter, &base_attachment))
erikchen959039d2015-08-11 21:17:4736 return false;
rockot502c94f2016-02-03 20:20:1637 MessageAttachment* attachment =
38 static_cast<MessageAttachment*>(base_attachment.get());
sammc6ed3efb2016-11-23 03:17:3539 if (attachment->GetType() != MessageAttachment::Type::WIN_HANDLE)
erikchen959039d2015-08-11 21:17:4740 return false;
erikchen959039d2015-08-11 21:17:4741 IPC::internal::HandleAttachmentWin* handle_attachment =
sammc6ed3efb2016-11-23 03:17:3542 static_cast<IPC::internal::HandleAttachmentWin*>(attachment);
Wez51eaaad2017-08-09 05:51:3843 r->set_handle(handle_attachment->Take());
erikchen959039d2015-08-11 21:17:4744 return true;
45}
46
47// static
48void ParamTraits<HandleWin>::Log(const param_type& p, std::string* l) {
brucedawson5604a11d2015-10-06 19:22:0049 l->append(base::StringPrintf("0x%p", p.get_handle()));
erikchen959039d2015-08-11 21:17:4750}
51
52} // namespace IPC