[go: nahoru, domu]

blob: a387079e72da19c0d46bb2a93ede0f6d6fbf70f5 [file] [log] [blame]
Avi Drissmanea1be232022-09-14 23:29:061// Copyright 2015 The Chromium Authors
erikchen1c1e6652015-10-01 18:51:322// 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_MAC_H_
6#define IPC_MACH_PORT_MAC_H_
7
8#include <mach/mach.h>
9
Ken Rockotfd907632017-09-14 04:23:4110#include "base/pickle.h"
11#include "ipc/ipc_message_support_export.h"
12#include "ipc/ipc_param_traits.h"
erikchen1c1e6652015-10-01 18:51:3213
14namespace IPC {
15
erikchen3722a322015-10-07 20:51:5516// MachPortMac is a wrapper around an OSX Mach port that can be transported
17// across Chrome IPC channels that support attachment brokering. The send right
18// to the Mach port will be duplicated into the destination process by the
19// attachment broker. If needed, attachment brokering can be trivially extended
20// to support duplication of other types of rights.
Ken Rockotfd907632017-09-14 04:23:4121class IPC_MESSAGE_SUPPORT_EXPORT MachPortMac {
erikchen1c1e6652015-10-01 18:51:3222 public:
23 MachPortMac() : mach_port_(MACH_PORT_NULL) {}
erikchen3722a322015-10-07 20:51:5524
erikchenaf8299d2015-10-09 19:12:0625 explicit MachPortMac(mach_port_t mach_port) : mach_port_(mach_port) {}
erikchen1c1e6652015-10-01 18:51:3226
Peter Boström896f1372021-11-05 01:12:3027 MachPortMac(const MachPortMac&) = delete;
28 MachPortMac& operator=(const MachPortMac&) = delete;
29
erikchen1c1e6652015-10-01 18:51:3230 mach_port_t get_mach_port() const { return mach_port_; }
erikchen3722a322015-10-07 20:51:5531
32 // This method should only be used by ipc/ translation code.
erikchen1c1e6652015-10-01 18:51:3233 void set_mach_port(mach_port_t mach_port) { mach_port_ = mach_port; }
34
35 private:
erikchen3722a322015-10-07 20:51:5536 // The ownership semantics of |mach_port_| cannot be easily expressed with a
37 // C++ scoped object. This is partly due to the mechanism by which Mach ports
38 // are brokered, and partly due to the architecture of Chrome IPC.
39 //
40 // The broker for Mach ports may live in a different process than the sender
41 // of the original Chrome IPC. In this case, it is signalled asynchronously,
42 // and ownership of the Mach port passes from the sender process into the
43 // broker process.
44 //
45 // Chrome IPC is written with the assumption that translation is a stateless
46 // process. There is no good mechanism to convey the semantics of ownership
47 // transfer from the Chrome IPC stack into the client code that receives the
48 // translated message. As a result, Chrome IPC code assumes that every message
49 // has a handler, and that the handler will take ownership of the Mach port.
50 // Note that the same holds true for POSIX fds and Windows HANDLEs.
51 //
52 // When used by client code in the sender process, this class is just a
53 // wrapper. The client code calls Send(new Message(MachPortMac(mach_port)))
54 // and continues on its merry way. Behind the scenes, a MachPortAttachmentMac
55 // takes ownership of the Mach port. When the attachment broker sends the name
56 // of the Mach port to the broker process, it also releases
57 // MachPortAttachmentMac's reference to the Mach port, as ownership has
58 // effectively been transferred to the broker process.
59 //
60 // The broker process receives the name, duplicates the Mach port into the
61 // destination process, and then decrements the ref count in the original
62 // process.
63 //
64 // In the destination process, the attachment broker is responsible for
65 // coupling the Mach port (inserted by the broker process) with Chrome IPC in
66 // the form of a MachPortAttachmentMac. Due to the Chrome IPC translation
67 // semantics discussed above, this MachPortAttachmentMac does not take
68 // ownership of the Mach port, and assumes that the client code which receives
69 // the callback will take ownership of the Mach port.
erikchen1c1e6652015-10-01 18:51:3270 mach_port_t mach_port_;
erikchen1c1e6652015-10-01 18:51:3271};
72
73template <>
Ken Rockotfd907632017-09-14 04:23:4174struct IPC_MESSAGE_SUPPORT_EXPORT ParamTraits<MachPortMac> {
erikchen1c1e6652015-10-01 18:51:3275 typedef MachPortMac param_type;
rockot502c94f2016-02-03 20:20:1676 static void Write(base::Pickle* m, const param_type& p);
77 static bool Read(const base::Pickle* m,
78 base::PickleIterator* iter,
79 param_type* p);
erikchen1c1e6652015-10-01 18:51:3280 static void Log(const param_type& p, std::string* l);
81};
82
83} // namespace IPC
84
85#endif // IPC_MACH_PORT_MAC_H_