blob: ff0ce91a851950d54fbeee15fe3e4fe64c550c9d [file] [log] [blame]
erikchen151b2f92015-06-16 20:20:511// 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
erikcheneece6c32015-07-07 22:13:118#include <stdint.h>
9
10#include "base/process/process_handle.h"
erikchen151b2f92015-06-16 20:20:5111#include "ipc/brokerable_attachment.h"
erikchen959039d2015-08-11 21:17:4712#include "ipc/handle_win.h"
erikchen151b2f92015-06-16 20:20:5113#include "ipc/ipc_export.h"
14
15namespace IPC {
16namespace internal {
17
18// This class represents a Windows HANDLE attached to a Chrome IPC message.
19class IPC_EXPORT HandleAttachmentWin : public BrokerableAttachment {
erikcheneece6c32015-07-07 22:13:1120 public:
21 // The wire format for this handle.
22 struct IPC_EXPORT WireFormat {
erikchen28299a12015-09-24 00:10:2723 // 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
erikcheneece6c32015-07-07 22:13:1139 // 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
erikchen17b34832015-12-04 21:20:1244 // and down-casting. This is performed using the appropriate Windows APIs.
erikchenc04ab34c2015-07-27 20:28:2045 // A value of 0 is equivalent to an invalid handle.
erikcheneece6c32015-07-07 22:13:1146 int32_t handle;
erikchen28299a12015-09-24 00:10:2747
erikcheneece6c32015-07-07 22:13:1148 // The id of the destination process that the handle is duplicated into.
49 base::ProcessId destination_process;
erikchen28299a12015-09-24 00:10:2750
erikchen959039d2015-08-11 21:17:4751 // The permissions to use when duplicating the handle.
52 HandleWin::Permissions permissions;
erikchen28299a12015-09-24 00:10:2753
erikcheneece6c32015-07-07 22:13:1154 AttachmentId attachment_id;
55 };
56
erikchen17b34832015-12-04 21:20:1257 // 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.
erikchen959039d2015-08-11 21:17:4759 HandleAttachmentWin(const HANDLE& handle, HandleWin::Permissions permissions);
erikchen17b34832015-12-04 21:20:1260
sammc57ed9f982016-03-10 06:28:3561 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
erikchen3d87ecf72016-01-08 02:17:0468 // 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.
erikchen7252aa362015-07-15 01:35:3970 explicit HandleAttachmentWin(const WireFormat& wire_format);
erikcheneece6c32015-07-07 22:13:1171
72 BrokerableType GetBrokerableType() const override;
73
74 // Returns the wire format of this attachment.
75 WireFormat GetWireFormat(const base::ProcessId& destination) const;
76
erikchen7252aa362015-07-15 01:35:3977 HANDLE get_handle() const { return handle_; }
78
erikchen17b34832015-12-04 21:20:1279 // The caller of this method has taken ownership of |handle_|.
erikchen3d87ecf72016-01-08 02:17:0480 void reset_handle_ownership() {
81 owns_handle_ = false;
82 handle_ = INVALID_HANDLE_VALUE;
83 }
erikchen17b34832015-12-04 21:20:1284
erikchen151b2f92015-06-16 20:20:5185 private:
erikcheneece6c32015-07-07 22:13:1186 ~HandleAttachmentWin() override;
erikchen151b2f92015-06-16 20:20:5187 HANDLE handle_;
erikchen959039d2015-08-11 21:17:4788 HandleWin::Permissions permissions_;
erikchen17b34832015-12-04 21:20:1289
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|.
erikchen3d87ecf72016-01-08 02:17:0493 // In the destination process, the attachment owns the Mach port until a call
94 // to ParamTraits<HandleWin>::Read() takes ownership.
erikchen17b34832015-12-04 21:20:1295 bool owns_handle_;
erikchen151b2f92015-06-16 20:20:5196};
97
98} // namespace internal
99} // namespace IPC
100
101#endif // IPC_HANDLE_ATTACHMENT_WIN_H_