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" |
horo | cff1de9 | 2016-11-16 01:52:54 | [diff] [blame] | 13 | #include "ipc/brokerable_attachment.h" |
erikchen | 1c1e665 | 2015-10-01 18:51:32 | [diff] [blame] | 14 | #include "ipc/ipc_export.h" |
| 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. |
horo | cff1de9 | 2016-11-16 01:52:54 | [diff] [blame] | 21 | class IPC_EXPORT MachPortAttachmentMac : public BrokerableAttachment { |
erikchen | 1c1e665 | 2015-10-01 18:51:32 | [diff] [blame] | 22 | public: |
horo | cff1de9 | 2016-11-16 01:52:54 | [diff] [blame] | 23 | struct IPC_EXPORT WireFormat { |
| 24 | // IPC translation requires that classes passed through IPC have a default |
| 25 | // constructor. |
| 26 | WireFormat() : mach_port(0), destination_process(0) {} |
| 27 | |
| 28 | WireFormat(uint32_t mach_port, const base::ProcessId& destination_process) |
| 29 | : mach_port(mach_port), destination_process(destination_process) {} |
| 30 | |
| 31 | // The mach port that is intended for duplication, or the mach port that has |
| 32 | // been duplicated, depending on context. |
| 33 | // The type is uint32_t instead of mach_port_t to ensure that the wire |
| 34 | // format stays consistent. |
| 35 | uint32_t mach_port; |
| 36 | static_assert(sizeof(mach_port_t) <= sizeof(uint32_t), |
| 37 | "mach_port_t must be smaller than uint32_t"); |
| 38 | |
| 39 | // The id of the destination process that the handle is duplicated into. |
| 40 | base::ProcessId destination_process; |
| 41 | }; |
| 42 | |
erikchen | 3722a32 | 2015-10-07 20:51:55 | [diff] [blame] | 43 | // This constructor increments the ref count of |mach_port_| and takes |
| 44 | // ownership of the result. Should only be called by the sender of a Chrome |
| 45 | // IPC message. |
erikchen | 1c1e665 | 2015-10-01 18:51:32 | [diff] [blame] | 46 | explicit MachPortAttachmentMac(mach_port_t mach_port); |
erikchen | 3722a32 | 2015-10-07 20:51:55 | [diff] [blame] | 47 | |
sammc | 57ed9f98 | 2016-03-10 06:28:35 | [diff] [blame] | 48 | enum FromWire { |
| 49 | FROM_WIRE, |
| 50 | }; |
| 51 | // This constructor takes ownership of |mach_port|, but does not modify its |
| 52 | // ref count. Should only be called by the receiver of a Chrome IPC message. |
| 53 | MachPortAttachmentMac(mach_port_t mach_port, FromWire from_wire); |
| 54 | |
horo | cff1de9 | 2016-11-16 01:52:54 | [diff] [blame] | 55 | // This constructor takes ownership of |wire_format.mach_port|, but does not |
| 56 | // modify its ref count. Should only be called by the receiver of a Chrome IPC |
| 57 | // message. |
| 58 | explicit MachPortAttachmentMac(const WireFormat& wire_format); |
| 59 | |
| 60 | BrokerableType GetBrokerableType() const override; |
| 61 | |
| 62 | // Returns the wire format of this attachment. |
| 63 | WireFormat GetWireFormat(const base::ProcessId& destination) const; |
erikchen | 1c1e665 | 2015-10-01 18:51:32 | [diff] [blame] | 64 | |
| 65 | mach_port_t get_mach_port() const { return mach_port_; } |
| 66 | |
erikchen | 3722a32 | 2015-10-07 20:51:55 | [diff] [blame] | 67 | // The caller of this method has taken ownership of |mach_port_|. |
| 68 | void reset_mach_port_ownership() { owns_mach_port_ = false; } |
| 69 | |
erikchen | 1c1e665 | 2015-10-01 18:51:32 | [diff] [blame] | 70 | private: |
| 71 | ~MachPortAttachmentMac() override; |
erikchen | fa70536 | 2015-10-30 23:16:29 | [diff] [blame] | 72 | const mach_port_t mach_port_; |
erikchen | 3722a32 | 2015-10-07 20:51:55 | [diff] [blame] | 73 | |
| 74 | // In the sender process, the attachment owns the Mach port of a newly created |
erikchen | 8a6f3f4e | 2016-01-06 22:04:43 | [diff] [blame] | 75 | // message. The attachment broker will eventually take ownership of |
| 76 | // |mach_port_|. |
| 77 | // In the destination process, the attachment owns |mach_port_| until |
| 78 | // ParamTraits<MachPortMac>::Read() is called, which takes ownership. |
erikchen | 3722a32 | 2015-10-07 20:51:55 | [diff] [blame] | 79 | bool owns_mach_port_; |
| 80 | DISALLOW_COPY_AND_ASSIGN(MachPortAttachmentMac); |
erikchen | 1c1e665 | 2015-10-01 18:51:32 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | } // namespace internal |
| 84 | } // namespace IPC |
| 85 | |
| 86 | #endif // IPC_MACH_PORT_ATTACHMENT_MAC_H_ |