| 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" | |
| 12 #include "ipc/handle_win.h" | 11 #include "ipc/handle_win.h" |
| 13 #include "ipc/ipc_export.h" | 12 #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 BrokerableAttachment { | 19 class IPC_EXPORT HandleAttachmentWin : public MessageAttachment { |
| 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 | |
| 53 // This constructor makes a copy of |handle| and takes ownership of the | 21 // This constructor makes a copy of |handle| and takes ownership of the |
| 54 // result. Should only be called by the sender of a Chrome IPC message. | 22 // result. Should only be called by the sender of a Chrome IPC message. |
| 55 HandleAttachmentWin(const HANDLE& handle, HandleWin::Permissions permissions); | 23 HandleAttachmentWin(const HANDLE& handle, HandleWin::Permissions permissions); |
| 56 | 24 |
| 57 enum FromWire { | 25 enum FromWire { |
| 58 FROM_WIRE, | 26 FROM_WIRE, |
| 59 }; | 27 }; |
| 60 // This constructor takes ownership of |handle|. Should only be called by the | 28 // This constructor takes ownership of |handle|. Should only be called by the |
| 61 // receiver of a Chrome IPC message. | 29 // receiver of a Chrome IPC message. |
| 62 HandleAttachmentWin(const HANDLE& handle, FromWire from_wire); | 30 HandleAttachmentWin(const HANDLE& handle, FromWire from_wire); |
| 63 | 31 |
| 64 // This constructor takes ownership of |wire_format.handle| without making a | 32 Type GetType() const override; |
| 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; | |
| 72 | 33 |
| 73 HANDLE get_handle() const { return handle_; } | 34 HANDLE get_handle() const { return handle_; } |
| 74 | 35 |
| 75 // The caller of this method has taken ownership of |handle_|. | 36 // The caller of this method has taken ownership of |handle_|. |
| 76 void reset_handle_ownership() { | 37 void reset_handle_ownership() { |
| 77 owns_handle_ = false; | 38 owns_handle_ = false; |
| 78 handle_ = INVALID_HANDLE_VALUE; | 39 handle_ = INVALID_HANDLE_VALUE; |
| 79 } | 40 } |
| 80 | 41 |
| 81 private: | 42 private: |
| 82 ~HandleAttachmentWin() override; | 43 ~HandleAttachmentWin() override; |
| 83 HANDLE handle_; | 44 HANDLE handle_; |
| 84 HandleWin::Permissions permissions_; | 45 HandleWin::Permissions permissions_; |
| 85 | 46 |
| 86 // In the sender process, the attachment owns the HANDLE of a newly created | 47 // In the sender process, the attachment owns the HANDLE of a newly created |
| 87 // message. The attachment broker will eventually take ownership, and set | 48 // message. The attachment broker will eventually take ownership, and set |
| 88 // this member to |false|. | 49 // this member to |false|. |
| 89 // In the destination process, the attachment owns the Mach port until a call | 50 // In the destination process, the attachment owns the Mach port until a call |
| 90 // to ParamTraits<HandleWin>::Read() takes ownership. | 51 // to ParamTraits<HandleWin>::Read() takes ownership. |
| 91 bool owns_handle_; | 52 bool owns_handle_; |
| 92 }; | 53 }; |
| 93 | 54 |
| 94 } // namespace internal | 55 } // namespace internal |
| 95 } // namespace IPC | 56 } // namespace IPC |
| 96 | 57 |
| 97 #endif // IPC_HANDLE_ATTACHMENT_WIN_H_ | 58 #endif // IPC_HANDLE_ATTACHMENT_WIN_H_ |
| OLD | NEW |