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_MAC_H_ | 5 #ifndef IPC_MACH_PORT_MAC_H_ |
6 #define IPC_MACH_PORT_MAC_H_ | 6 #define IPC_MACH_PORT_MAC_H_ |
7 | 7 |
8 #include <mach/mach.h> | 8 #include <mach/mach.h> |
9 | 9 |
| 10 #include "base/macros.h" |
10 #include "ipc/ipc_export.h" | 11 #include "ipc/ipc_export.h" |
11 #include "ipc/ipc_message_macros.h" | 12 #include "ipc/ipc_message_macros.h" |
12 | 13 |
13 namespace IPC { | 14 namespace IPC { |
14 | 15 |
15 // MachPortMac is a wrapper around an OSX mach_port_t that can be transported | 16 // MachPortMac is a wrapper around an OSX Mach port that can be transported |
16 // across Chrome IPC channels that support attachment brokering. The mach_port_t | 17 // across Chrome IPC channels that support attachment brokering. The send right |
17 // will be duplicated into the destination process by the attachment broker. | 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. |
18 class IPC_EXPORT MachPortMac { | 21 class IPC_EXPORT MachPortMac { |
19 public: | 22 public: |
20 MachPortMac() : mach_port_(MACH_PORT_NULL) {} | 23 MachPortMac() : mach_port_(MACH_PORT_NULL) {} |
21 explicit MachPortMac(const mach_port_t& mach_port) : mach_port_(mach_port) {} | 24 |
| 25 explicit MachPortMac(mach_port_t mach_port) {}; |
22 | 26 |
23 mach_port_t get_mach_port() const { return mach_port_; } | 27 mach_port_t get_mach_port() const { return mach_port_; } |
| 28 |
| 29 // This method should only be used by ipc/ translation code. |
24 void set_mach_port(mach_port_t mach_port) { mach_port_ = mach_port; } | 30 void set_mach_port(mach_port_t mach_port) { mach_port_ = mach_port; } |
25 | 31 |
26 private: | 32 private: |
| 33 // The ownership semantics of |mach_port_| cannot be easily expressed with a |
| 34 // C++ scoped object. This is partly due to the mechanism by which Mach ports |
| 35 // are brokered, and partly due to the architecture of Chrome IPC. |
| 36 // |
| 37 // The broker for Mach ports may live in a different process than the sender |
| 38 // of the original Chrome IPC. In this case, it is signalled asynchronously, |
| 39 // and ownership of the Mach port passes from the sender process into the |
| 40 // broker process. |
| 41 // |
| 42 // Chrome IPC is written with the assumption that translation is a stateless |
| 43 // process. There is no good mechanism to convey the semantics of ownership |
| 44 // transfer from the Chrome IPC stack into the client code that receives the |
| 45 // translated message. As a result, Chrome IPC code assumes that every message |
| 46 // has a handler, and that the handler will take ownership of the Mach port. |
| 47 // Note that the same holds true for POSIX fds and Windows HANDLEs. |
| 48 // |
| 49 // When used by client code in the sender process, this class is just a |
| 50 // wrapper. The client code calls Send(new Message(MachPortMac(mach_port))) |
| 51 // and continues on its merry way. Behind the scenes, a MachPortAttachmentMac |
| 52 // takes ownership of the Mach port. When the attachment broker sends the name |
| 53 // of the Mach port to the broker process, it also releases |
| 54 // MachPortAttachmentMac's reference to the Mach port, as ownership has |
| 55 // effectively been transferred to the broker process. |
| 56 // |
| 57 // The broker process receives the name, duplicates the Mach port into the |
| 58 // destination process, and then decrements the ref count in the original |
| 59 // process. |
| 60 // |
| 61 // In the destination process, the attachment broker is responsible for |
| 62 // coupling the Mach port (inserted by the broker process) with Chrome IPC in |
| 63 // the form of a MachPortAttachmentMac. Due to the Chrome IPC translation |
| 64 // semantics discussed above, this MachPortAttachmentMac does not take |
| 65 // ownership of the Mach port, and assumes that the client code which receives |
| 66 // the callback will take ownership of the Mach port. |
27 mach_port_t mach_port_; | 67 mach_port_t mach_port_; |
| 68 DISALLOW_COPY_AND_ASSIGN(MachPortMac); |
28 }; | 69 }; |
29 | 70 |
30 template <> | 71 template <> |
31 struct IPC_EXPORT ParamTraits<MachPortMac> { | 72 struct IPC_EXPORT ParamTraits<MachPortMac> { |
32 typedef MachPortMac param_type; | 73 typedef MachPortMac param_type; |
33 static void Write(Message* m, const param_type& p); | 74 static void Write(Message* m, const param_type& p); |
34 static bool Read(const Message* m, base::PickleIterator* iter, param_type* p); | 75 static bool Read(const Message* m, base::PickleIterator* iter, param_type* p); |
35 static void Log(const param_type& p, std::string* l); | 76 static void Log(const param_type& p, std::string* l); |
36 }; | 77 }; |
37 | 78 |
38 } // namespace IPC | 79 } // namespace IPC |
39 | 80 |
40 #endif // IPC_MACH_PORT_MAC_H_ | 81 #endif // IPC_MACH_PORT_MAC_H_ |
OLD | NEW |