blob: 35807b4da908ecc92ad21615bd2b7b6cfb9c13d3 [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
61 // These constructors do not take ownership of the HANDLE, and should only be
62 // called by the receiver of a Chrome IPC message.
erikchen7252aa362015-07-15 01:35:3963 explicit HandleAttachmentWin(const WireFormat& wire_format);
erikchende9412b82015-07-27 18:26:1464 explicit HandleAttachmentWin(const BrokerableAttachment::AttachmentId& id);
erikcheneece6c32015-07-07 22:13:1165
66 BrokerableType GetBrokerableType() const override;
67
68 // Returns the wire format of this attachment.
69 WireFormat GetWireFormat(const base::ProcessId& destination) const;
70
erikchen7252aa362015-07-15 01:35:3971 HANDLE get_handle() const { return handle_; }
72
erikchen17b34832015-12-04 21:20:1273 // The caller of this method has taken ownership of |handle_|.
74 void reset_handle_ownership() { owns_handle_ = false; }
75
erikchen151b2f92015-06-16 20:20:5176 private:
erikcheneece6c32015-07-07 22:13:1177 ~HandleAttachmentWin() override;
erikchen151b2f92015-06-16 20:20:5178 HANDLE handle_;
erikchen959039d2015-08-11 21:17:4779 HandleWin::Permissions permissions_;
erikchen17b34832015-12-04 21:20:1280
81 // In the sender process, the attachment owns the HANDLE of a newly created
82 // message. The attachment broker will eventually take ownership, and set
83 // this member to |false|.
84 // In the destination process, the attachment never owns the Mach port. The
85 // client code that receives the Chrome IPC message is always expected to take
86 // ownership.
87 bool owns_handle_;
erikchen151b2f92015-06-16 20:20:5188};
89
90} // namespace internal
91} // namespace IPC
92
93#endif // IPC_HANDLE_ATTACHMENT_WIN_H_