erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 1 | // 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 | |
| 9 | namespace IPC { |
| 10 | namespace internal { |
| 11 | |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 12 | HandleAttachmentWin::HandleAttachmentWin(const HANDLE& handle, |
| 13 | HandleWin::Permissions permissions) |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame^] | 14 | : 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 | } |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 26 | |
erikchen | 7252aa36 | 2015-07-15 01:35:39 | [diff] [blame] | 27 | HandleAttachmentWin::HandleAttachmentWin(const WireFormat& wire_format) |
erikchen | 87351da | 2015-09-15 19:11:09 | [diff] [blame] | 28 | : BrokerableAttachment(wire_format.attachment_id), |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 29 | handle_(LongToHandle(wire_format.handle)), |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame^] | 30 | permissions_(wire_format.permissions), |
| 31 | owns_handle_(true) {} |
erikchen | de9412b8 | 2015-07-27 18:26:14 | [diff] [blame] | 32 | |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 33 | HandleAttachmentWin::~HandleAttachmentWin() { |
erikchen | 17b3483 | 2015-12-04 21:20:12 | [diff] [blame] | 34 | if (handle_ != INVALID_HANDLE_VALUE && owns_handle_) |
| 35 | ::CloseHandle(handle_); |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | HandleAttachmentWin::BrokerableType HandleAttachmentWin::GetBrokerableType() |
| 39 | const { |
| 40 | return WIN_HANDLE; |
| 41 | } |
| 42 | |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 43 | HandleAttachmentWin::WireFormat HandleAttachmentWin::GetWireFormat( |
| 44 | const base::ProcessId& destination) const { |
erikchen | 28299a1 | 2015-09-24 00:10:27 | [diff] [blame] | 45 | return WireFormat(HandleToLong(handle_), destination, permissions_, |
| 46 | GetIdentifier()); |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | } // namespace internal |
| 50 | } // namespace IPC |