blob: 39c4ef78c7fa2e62315b1252de7b8b35c44e80ce [file] [log] [blame]
erikcheneece6c32015-07-07 22:13:111// 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_attachment_win.h"
6
7#include <windows.h>
8
9namespace IPC {
10namespace internal {
11
erikchen959039d2015-08-11 21:17:4712HandleAttachmentWin::HandleAttachmentWin(const HANDLE& handle,
13 HandleWin::Permissions permissions)
erikchen3d87ecf72016-01-08 02:17:0414 : handle_(INVALID_HANDLE_VALUE),
15 permissions_(HandleWin::INVALID),
16 owns_handle_(true) {
17 HANDLE duplicated_handle;
18 BOOL result =
19 ::DuplicateHandle(GetCurrentProcess(), handle, GetCurrentProcess(),
20 &duplicated_handle, 0, FALSE, DUPLICATE_SAME_ACCESS);
21 if (result) {
22 handle_ = duplicated_handle;
23 permissions_ = permissions;
24 }
25}
erikcheneece6c32015-07-07 22:13:1126
erikchen7252aa362015-07-15 01:35:3927HandleAttachmentWin::HandleAttachmentWin(const WireFormat& wire_format)
erikchen87351da2015-09-15 19:11:0928 : BrokerableAttachment(wire_format.attachment_id),
erikchen959039d2015-08-11 21:17:4729 handle_(LongToHandle(wire_format.handle)),
erikchen3d87ecf72016-01-08 02:17:0430 permissions_(wire_format.permissions),
31 owns_handle_(true) {}
erikchende9412b82015-07-27 18:26:1432
erikcheneece6c32015-07-07 22:13:1133HandleAttachmentWin::~HandleAttachmentWin() {
erikchen17b34832015-12-04 21:20:1234 if (handle_ != INVALID_HANDLE_VALUE && owns_handle_)
35 ::CloseHandle(handle_);
erikcheneece6c32015-07-07 22:13:1136}
37
38HandleAttachmentWin::BrokerableType HandleAttachmentWin::GetBrokerableType()
39 const {
40 return WIN_HANDLE;
41}
42
erikcheneece6c32015-07-07 22:13:1143HandleAttachmentWin::WireFormat HandleAttachmentWin::GetWireFormat(
44 const base::ProcessId& destination) const {
erikchen28299a12015-09-24 00:10:2745 return WireFormat(HandleToLong(handle_), destination, permissions_,
46 GetIdentifier());
erikcheneece6c32015-07-07 22:13:1147}
48
49} // namespace internal
50} // namespace IPC