| 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_MACH_PORT_ATTACHMENT_MAC_H_ | 5 #ifndef IPC_MACH_PORT_ATTACHMENT_MAC_H_ |
| 6 #define IPC_MACH_PORT_ATTACHMENT_MAC_H_ | 6 #define IPC_MACH_PORT_ATTACHMENT_MAC_H_ |
| 7 | 7 |
| 8 #include <mach/mach.h> | 8 #include <mach/mach.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| 11 #include "base/macros.h" | 11 #include "base/macros.h" |
| 12 #include "base/process/process_handle.h" | 12 #include "base/process/process_handle.h" |
| 13 #include "ipc/brokerable_attachment.h" |
| 13 #include "ipc/ipc_export.h" | 14 #include "ipc/ipc_export.h" |
| 14 #include "ipc/ipc_message_attachment.h" | |
| 15 #include "ipc/mach_port_mac.h" | 15 #include "ipc/mach_port_mac.h" |
| 16 | 16 |
| 17 namespace IPC { | 17 namespace IPC { |
| 18 namespace internal { | 18 namespace internal { |
| 19 | 19 |
| 20 // This class represents an OSX mach_port_t attached to a Chrome IPC message. | 20 // This class represents an OSX mach_port_t attached to a Chrome IPC message. |
| 21 class IPC_EXPORT MachPortAttachmentMac : public MessageAttachment { | 21 class IPC_EXPORT MachPortAttachmentMac : public BrokerableAttachment { |
| 22 public: | 22 public: |
| 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 |
| 23 // This constructor increments the ref count of |mach_port_| and takes | 43 // This constructor increments the ref count of |mach_port_| and takes |
| 24 // ownership of the result. Should only be called by the sender of a Chrome | 44 // ownership of the result. Should only be called by the sender of a Chrome |
| 25 // IPC message. | 45 // IPC message. |
| 26 explicit MachPortAttachmentMac(mach_port_t mach_port); | 46 explicit MachPortAttachmentMac(mach_port_t mach_port); |
| 27 | 47 |
| 28 enum FromWire { | 48 enum FromWire { |
| 29 FROM_WIRE, | 49 FROM_WIRE, |
| 30 }; | 50 }; |
| 31 // This constructor takes ownership of |mach_port|, but does not modify its | 51 // This constructor takes ownership of |mach_port|, but does not modify its |
| 32 // ref count. Should only be called by the receiver of a Chrome IPC message. | 52 // ref count. Should only be called by the receiver of a Chrome IPC message. |
| 33 MachPortAttachmentMac(mach_port_t mach_port, FromWire from_wire); | 53 MachPortAttachmentMac(mach_port_t mach_port, FromWire from_wire); |
| 34 | 54 |
| 35 Type GetType() const override; | 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; |
| 36 | 64 |
| 37 mach_port_t get_mach_port() const { return mach_port_; } | 65 mach_port_t get_mach_port() const { return mach_port_; } |
| 38 | 66 |
| 39 // The caller of this method has taken ownership of |mach_port_|. | 67 // The caller of this method has taken ownership of |mach_port_|. |
| 40 void reset_mach_port_ownership() { owns_mach_port_ = false; } | 68 void reset_mach_port_ownership() { owns_mach_port_ = false; } |
| 41 | 69 |
| 42 private: | 70 private: |
| 43 ~MachPortAttachmentMac() override; | 71 ~MachPortAttachmentMac() override; |
| 44 const mach_port_t mach_port_; | 72 const mach_port_t mach_port_; |
| 45 | 73 |
| 46 // In the sender process, the attachment owns the Mach port of a newly created | 74 // In the sender process, the attachment owns the Mach port of a newly created |
| 47 // message. The attachment broker will eventually take ownership of | 75 // message. The attachment broker will eventually take ownership of |
| 48 // |mach_port_|. | 76 // |mach_port_|. |
| 49 // In the destination process, the attachment owns |mach_port_| until | 77 // In the destination process, the attachment owns |mach_port_| until |
| 50 // ParamTraits<MachPortMac>::Read() is called, which takes ownership. | 78 // ParamTraits<MachPortMac>::Read() is called, which takes ownership. |
| 51 bool owns_mach_port_; | 79 bool owns_mach_port_; |
| 52 DISALLOW_COPY_AND_ASSIGN(MachPortAttachmentMac); | 80 DISALLOW_COPY_AND_ASSIGN(MachPortAttachmentMac); |
| 53 }; | 81 }; |
| 54 | 82 |
| 55 } // namespace internal | 83 } // namespace internal |
| 56 } // namespace IPC | 84 } // namespace IPC |
| 57 | 85 |
| 58 #endif // IPC_MACH_PORT_ATTACHMENT_MAC_H_ | 86 #endif // IPC_MACH_PORT_ATTACHMENT_MAC_H_ |
| OLD | NEW |