blob: efc2c11a784a74f8af82fdadd1a467cc97d63a88 [file] [log] [blame]
erikchen1c1e6652015-10-01 18:51:321// 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
avi246998d82015-12-22 02:39:0411#include "base/macros.h"
erikchen1c1e6652015-10-01 18:51:3212#include "base/process/process_handle.h"
horocff1de92016-11-16 01:52:5413#include "ipc/brokerable_attachment.h"
erikchen1c1e6652015-10-01 18:51:3214#include "ipc/ipc_export.h"
15#include "ipc/mach_port_mac.h"
16
17namespace IPC {
18namespace internal {
19
20// This class represents an OSX mach_port_t attached to a Chrome IPC message.
horocff1de92016-11-16 01:52:5421class IPC_EXPORT MachPortAttachmentMac : public BrokerableAttachment {
erikchen1c1e6652015-10-01 18:51:3222 public:
horocff1de92016-11-16 01:52:5423 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
erikchen3722a322015-10-07 20:51:5543 // 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.
erikchen1c1e6652015-10-01 18:51:3246 explicit MachPortAttachmentMac(mach_port_t mach_port);
erikchen3722a322015-10-07 20:51:5547
sammc57ed9f982016-03-10 06:28:3548 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
horocff1de92016-11-16 01:52:5455 // 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;
erikchen1c1e6652015-10-01 18:51:3264
65 mach_port_t get_mach_port() const { return mach_port_; }
66
erikchen3722a322015-10-07 20:51:5567 // The caller of this method has taken ownership of |mach_port_|.
68 void reset_mach_port_ownership() { owns_mach_port_ = false; }
69
erikchen1c1e6652015-10-01 18:51:3270 private:
71 ~MachPortAttachmentMac() override;
erikchenfa705362015-10-30 23:16:2972 const mach_port_t mach_port_;
erikchen3722a322015-10-07 20:51:5573
74 // In the sender process, the attachment owns the Mach port of a newly created
erikchen8a6f3f4e2016-01-06 22:04:4375 // 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.
erikchen3722a322015-10-07 20:51:5579 bool owns_mach_port_;
80 DISALLOW_COPY_AND_ASSIGN(MachPortAttachmentMac);
erikchen1c1e6652015-10-01 18:51:3281};
82
83} // namespace internal
84} // namespace IPC
85
86#endif // IPC_MACH_PORT_ATTACHMENT_MAC_H_