erikchen | 1c1e665 | 2015-10-01 18:51:32 | [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_MACH_PORT_ATTACHMENT_MAC_H_ |
| 6 | #define IPC_MACH_PORT_ATTACHMENT_MAC_H_ |
| 7 | |
| 8 | #include <mach/mach.h> |
| 9 | #include <stdint.h> |
| 10 | |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 11 | #include "base/macros.h" |
erikchen | 1c1e665 | 2015-10-01 18:51:32 | [diff] [blame] | 12 | #include "base/process/process_handle.h" |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 13 | #include "ipc/ipc_message_attachment.h" |
Ken Rockot | fd90763 | 2017-09-14 04:23:41 | [diff] [blame] | 14 | #include "ipc/ipc_message_support_export.h" |
erikchen | 1c1e665 | 2015-10-01 18:51:32 | [diff] [blame] | 15 | #include "ipc/mach_port_mac.h" |
| 16 | |
| 17 | namespace IPC { |
| 18 | namespace internal { |
| 19 | |
| 20 | // This class represents an OSX mach_port_t attached to a Chrome IPC message. |
Ken Rockot | fd90763 | 2017-09-14 04:23:41 | [diff] [blame] | 21 | class IPC_MESSAGE_SUPPORT_EXPORT MachPortAttachmentMac |
| 22 | : public MessageAttachment { |
erikchen | 1c1e665 | 2015-10-01 18:51:32 | [diff] [blame] | 23 | public: |
erikchen | 3722a32 | 2015-10-07 20:51:55 | [diff] [blame] | 24 | // This constructor increments the ref count of |mach_port_| and takes |
| 25 | // ownership of the result. Should only be called by the sender of a Chrome |
| 26 | // IPC message. |
erikchen | 1c1e665 | 2015-10-01 18:51:32 | [diff] [blame] | 27 | explicit MachPortAttachmentMac(mach_port_t mach_port); |
erikchen | 3722a32 | 2015-10-07 20:51:55 | [diff] [blame] | 28 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 29 | enum FromWire { |
| 30 | FROM_WIRE, |
| 31 | }; |
| 32 | // This constructor takes ownership of |mach_port|, but does not modify its |
| 33 | // ref count. Should only be called by the receiver of a Chrome IPC message. |
| 34 | MachPortAttachmentMac(mach_port_t mach_port, FromWire from_wire); |
| 35 | |
sammc | 6ed3efb | 2016-11-23 03:17:35 | [diff] [blame] | 36 | Type GetType() const override; |
erikchen | 1c1e665 | 2015-10-01 18:51:32 | [diff] [blame] | 37 | |
| 38 | mach_port_t get_mach_port() const { return mach_port_; } |
| 39 | |
erikchen | 3722a32 | 2015-10-07 20:51:55 | [diff] [blame] | 40 | // The caller of this method has taken ownership of |mach_port_|. |
| 41 | void reset_mach_port_ownership() { owns_mach_port_ = false; } |
| 42 | |
erikchen | 1c1e665 | 2015-10-01 18:51:32 | [diff] [blame] | 43 | private: |
| 44 | ~MachPortAttachmentMac() override; |
erikchen | fa70536 | 2015-10-30 23:16:29 | [diff] [blame] | 45 | const mach_port_t mach_port_; |
erikchen | 3722a32 | 2015-10-07 20:51:55 | [diff] [blame] | 46 | |
| 47 | // In the sender process, the attachment owns the Mach port of a newly created |
erikchen | 8a6f3f4e | 2016-01-06 22:04:43 | [diff] [blame] | 48 | // message. The attachment broker will eventually take ownership of |
| 49 | // |mach_port_|. |
| 50 | // In the destination process, the attachment owns |mach_port_| until |
| 51 | // ParamTraits<MachPortMac>::Read() is called, which takes ownership. |
erikchen | 3722a32 | 2015-10-07 20:51:55 | [diff] [blame] | 52 | bool owns_mach_port_; |
| 53 | DISALLOW_COPY_AND_ASSIGN(MachPortAttachmentMac); |
erikchen | 1c1e665 | 2015-10-01 18:51:32 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | } // namespace internal |
| 57 | } // namespace IPC |
| 58 | |
| 59 | #endif // IPC_MACH_PORT_ATTACHMENT_MAC_H_ |