OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef IPC_HANDLE_ATTACHMENT_WIN_H_ | 5 #ifndef IPC_HANDLE_ATTACHMENT_WIN_H_ |
6 #define IPC_HANDLE_ATTACHMENT_WIN_H_ | 6 #define IPC_HANDLE_ATTACHMENT_WIN_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/process/process_handle.h" | 10 #include "base/process/process_handle.h" |
| 11 #include "ipc/brokerable_attachment.h" |
11 #include "ipc/handle_win.h" | 12 #include "ipc/handle_win.h" |
12 #include "ipc/ipc_export.h" | 13 #include "ipc/ipc_export.h" |
13 #include "ipc/ipc_message_attachment.h" | |
14 | 14 |
15 namespace IPC { | 15 namespace IPC { |
16 namespace internal { | 16 namespace internal { |
17 | 17 |
18 // This class represents a Windows HANDLE attached to a Chrome IPC message. | 18 // This class represents a Windows HANDLE attached to a Chrome IPC message. |
19 class IPC_EXPORT HandleAttachmentWin : public MessageAttachment { | 19 class IPC_EXPORT HandleAttachmentWin : public BrokerableAttachment { |
20 public: | 20 public: |
| 21 // The wire format for this handle. |
| 22 struct IPC_EXPORT WireFormat { |
| 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 : handle(handle), |
| 34 destination_process(destination_process), |
| 35 permissions(permissions) {} |
| 36 |
| 37 // The HANDLE that is intended for duplication, or the HANDLE that has been |
| 38 // duplicated, depending on context. |
| 39 // The type is int32_t instead of HANDLE because HANDLE gets typedefed to |
| 40 // void*, whose size varies between 32 and 64-bit processes. Using a |
| 41 // int32_t means that 64-bit processes will need to perform both up-casting |
| 42 // and down-casting. This is performed using the appropriate Windows APIs. |
| 43 // A value of 0 is equivalent to an invalid handle. |
| 44 int32_t handle; |
| 45 |
| 46 // The id of the destination process that the handle is duplicated into. |
| 47 base::ProcessId destination_process; |
| 48 |
| 49 // The permissions to use when duplicating the handle. |
| 50 HandleWin::Permissions permissions; |
| 51 }; |
| 52 |
21 // This constructor makes a copy of |handle| and takes ownership of the | 53 // This constructor makes a copy of |handle| and takes ownership of the |
22 // result. Should only be called by the sender of a Chrome IPC message. | 54 // result. Should only be called by the sender of a Chrome IPC message. |
23 HandleAttachmentWin(const HANDLE& handle, HandleWin::Permissions permissions); | 55 HandleAttachmentWin(const HANDLE& handle, HandleWin::Permissions permissions); |
24 | 56 |
25 enum FromWire { | 57 enum FromWire { |
26 FROM_WIRE, | 58 FROM_WIRE, |
27 }; | 59 }; |
28 // This constructor takes ownership of |handle|. Should only be called by the | 60 // This constructor takes ownership of |handle|. Should only be called by the |
29 // receiver of a Chrome IPC message. | 61 // receiver of a Chrome IPC message. |
30 HandleAttachmentWin(const HANDLE& handle, FromWire from_wire); | 62 HandleAttachmentWin(const HANDLE& handle, FromWire from_wire); |
31 | 63 |
32 Type GetType() const override; | 64 // This constructor takes ownership of |wire_format.handle| without making a |
| 65 // copy. Should only be called by the receiver of a Chrome IPC message. |
| 66 explicit HandleAttachmentWin(const WireFormat& wire_format); |
| 67 |
| 68 BrokerableType GetBrokerableType() const override; |
| 69 |
| 70 // Returns the wire format of this attachment. |
| 71 WireFormat GetWireFormat(const base::ProcessId& destination) const; |
33 | 72 |
34 HANDLE get_handle() const { return handle_; } | 73 HANDLE get_handle() const { return handle_; } |
35 | 74 |
36 // The caller of this method has taken ownership of |handle_|. | 75 // The caller of this method has taken ownership of |handle_|. |
37 void reset_handle_ownership() { | 76 void reset_handle_ownership() { |
38 owns_handle_ = false; | 77 owns_handle_ = false; |
39 handle_ = INVALID_HANDLE_VALUE; | 78 handle_ = INVALID_HANDLE_VALUE; |
40 } | 79 } |
41 | 80 |
42 private: | 81 private: |
43 ~HandleAttachmentWin() override; | 82 ~HandleAttachmentWin() override; |
44 HANDLE handle_; | 83 HANDLE handle_; |
45 HandleWin::Permissions permissions_; | 84 HandleWin::Permissions permissions_; |
46 | 85 |
47 // In the sender process, the attachment owns the HANDLE of a newly created | 86 // In the sender process, the attachment owns the HANDLE of a newly created |
48 // message. The attachment broker will eventually take ownership, and set | 87 // message. The attachment broker will eventually take ownership, and set |
49 // this member to |false|. | 88 // this member to |false|. |
50 // In the destination process, the attachment owns the Mach port until a call | 89 // In the destination process, the attachment owns the Mach port until a call |
51 // to ParamTraits<HandleWin>::Read() takes ownership. | 90 // to ParamTraits<HandleWin>::Read() takes ownership. |
52 bool owns_handle_; | 91 bool owns_handle_; |
53 }; | 92 }; |
54 | 93 |
55 } // namespace internal | 94 } // namespace internal |
56 } // namespace IPC | 95 } // namespace IPC |
57 | 96 |
58 #endif // IPC_HANDLE_ATTACHMENT_WIN_H_ | 97 #endif // IPC_HANDLE_ATTACHMENT_WIN_H_ |
OLD | NEW |