erikchen | 151b2f9 | 2015-06-16 20:20:51 | [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 | #ifndef IPC_HANDLE_ATTACHMENT_WIN_H_ |
| 6 | #define IPC_HANDLE_ATTACHMENT_WIN_H_ |
| 7 | |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
| 10 | #include "base/process/process_handle.h" |
erikchen | 151b2f9 | 2015-06-16 20:20:51 | [diff] [blame] | 11 | #include "ipc/brokerable_attachment.h" |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 12 | #include "ipc/handle_win.h" |
erikchen | 151b2f9 | 2015-06-16 20:20:51 | [diff] [blame] | 13 | #include "ipc/ipc_export.h" |
| 14 | |
| 15 | namespace IPC { |
| 16 | namespace internal { |
| 17 | |
| 18 | // This class represents a Windows HANDLE attached to a Chrome IPC message. |
| 19 | class IPC_EXPORT HandleAttachmentWin : public BrokerableAttachment { |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 20 | public: |
| 21 | // The wire format for this handle. |
| 22 | struct IPC_EXPORT WireFormat { |
erikchen | 28299a1 | 2015-09-24 00:10:27 | [diff] [blame] | 23 | // IPC translation requires that classes passed through IPC have a default |
| 24 | // constructor. |
| 25 | WireFormat() |
| 26 | : handle(0), |
| 27 | destination_process(0), |
| 28 | permissions(HandleWin::INVALID) {} |
| 29 | |
| 30 | WireFormat(int32_t handle, |
| 31 | const base::ProcessId& destination_process, |
| 32 | HandleWin::Permissions permissions, |
| 33 | const AttachmentId& attachment_id) |
| 34 | : handle(handle), |
| 35 | destination_process(destination_process), |
| 36 | permissions(permissions), |
| 37 | attachment_id(attachment_id) {} |
| 38 | |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 39 | // The HANDLE that is intended for duplication, or the HANDLE that has been |
| 40 | // duplicated, depending on context. |
| 41 | // The type is int32_t instead of HANDLE because HANDLE gets typedefed to |
| 42 | // void*, whose size varies between 32 and 64-bit processes. Using a |
| 43 | // int32_t means that 64-bit processes will need to perform both up-casting |
erikchen | 17b3483 | 2015-12-04 21:20:12 | [diff] [blame] | 44 | // and down-casting. This is performed using the appropriate Windows APIs. |
erikchen | c04ab34c | 2015-07-27 20:28:20 | [diff] [blame] | 45 | // A value of 0 is equivalent to an invalid handle. |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 46 | int32_t handle; |
erikchen | 28299a1 | 2015-09-24 00:10:27 | [diff] [blame] | 47 | |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 48 | // The id of the destination process that the handle is duplicated into. |
| 49 | base::ProcessId destination_process; |
erikchen | 28299a1 | 2015-09-24 00:10:27 | [diff] [blame] | 50 | |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 51 | // The permissions to use when duplicating the handle. |
| 52 | HandleWin::Permissions permissions; |
erikchen | 28299a1 | 2015-09-24 00:10:27 | [diff] [blame] | 53 | |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 54 | AttachmentId attachment_id; |
| 55 | }; |
| 56 | |
erikchen | 17b3483 | 2015-12-04 21:20:12 | [diff] [blame] | 57 | // This constructor makes a copy of |handle| and takes ownership of the |
| 58 | // result. Should only be called by the sender of a Chrome IPC message. |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 59 | HandleAttachmentWin(const HANDLE& handle, HandleWin::Permissions permissions); |
erikchen | 17b3483 | 2015-12-04 21:20:12 | [diff] [blame] | 60 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 61 | enum FromWire { |
| 62 | FROM_WIRE, |
| 63 | }; |
| 64 | // This constructor takes ownership of |handle|. Should only be called by the |
| 65 | // receiver of a Chrome IPC message. |
| 66 | HandleAttachmentWin(const HANDLE& handle, FromWire from_wire); |
| 67 | |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 68 | // This constructor takes ownership of |wire_format.handle| without making a |
| 69 | // copy. Should only be called by the receiver of a Chrome IPC message. |
erikchen | 7252aa36 | 2015-07-15 01:35:39 | [diff] [blame] | 70 | explicit HandleAttachmentWin(const WireFormat& wire_format); |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 71 | |
| 72 | BrokerableType GetBrokerableType() const override; |
| 73 | |
| 74 | // Returns the wire format of this attachment. |
| 75 | WireFormat GetWireFormat(const base::ProcessId& destination) const; |
| 76 | |
erikchen | 7252aa36 | 2015-07-15 01:35:39 | [diff] [blame] | 77 | HANDLE get_handle() const { return handle_; } |
| 78 | |
erikchen | 17b3483 | 2015-12-04 21:20:12 | [diff] [blame] | 79 | // The caller of this method has taken ownership of |handle_|. |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 80 | void reset_handle_ownership() { |
| 81 | owns_handle_ = false; |
| 82 | handle_ = INVALID_HANDLE_VALUE; |
| 83 | } |
erikchen | 17b3483 | 2015-12-04 21:20:12 | [diff] [blame] | 84 | |
erikchen | 151b2f9 | 2015-06-16 20:20:51 | [diff] [blame] | 85 | private: |
erikchen | eece6c3 | 2015-07-07 22:13:11 | [diff] [blame] | 86 | ~HandleAttachmentWin() override; |
erikchen | 151b2f9 | 2015-06-16 20:20:51 | [diff] [blame] | 87 | HANDLE handle_; |
erikchen | 959039d | 2015-08-11 21:17:47 | [diff] [blame] | 88 | HandleWin::Permissions permissions_; |
erikchen | 17b3483 | 2015-12-04 21:20:12 | [diff] [blame] | 89 | |
| 90 | // In the sender process, the attachment owns the HANDLE of a newly created |
| 91 | // message. The attachment broker will eventually take ownership, and set |
| 92 | // this member to |false|. |
erikchen | 3d87ecf7 | 2016-01-08 02:17:04 | [diff] [blame] | 93 | // In the destination process, the attachment owns the Mach port until a call |
| 94 | // to ParamTraits<HandleWin>::Read() takes ownership. |
erikchen | 17b3483 | 2015-12-04 21:20:12 | [diff] [blame] | 95 | bool owns_handle_; |
erikchen | 151b2f9 | 2015-06-16 20:20:51 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | } // namespace internal |
| 99 | } // namespace IPC |
| 100 | |
| 101 | #endif // IPC_HANDLE_ATTACHMENT_WIN_H_ |