OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 MOJO_EDK_SYSTEM_NODE_CONTROLLER_H_ | 5 #ifndef MOJO_EDK_SYSTEM_NODE_CONTROLLER_H_ |
6 #define MOJO_EDK_SYSTEM_NODE_CONTROLLER_H_ | 6 #define MOJO_EDK_SYSTEM_NODE_CONTROLLER_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <queue> | 9 #include <queue> |
10 #include <unordered_map> | 10 #include <unordered_map> |
11 #include <unordered_set> | 11 #include <unordered_set> |
| 12 #include <utility> |
12 #include <vector> | 13 #include <vector> |
13 | 14 |
14 #include "base/callback.h" | 15 #include "base/callback.h" |
15 #include "base/containers/hash_tables.h" | 16 #include "base/containers/hash_tables.h" |
16 #include "base/macros.h" | 17 #include "base/macros.h" |
17 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
18 #include "base/task_runner.h" | 19 #include "base/task_runner.h" |
19 #include "mojo/edk/embedder/platform_handle_vector.h" | 20 #include "mojo/edk/embedder/platform_handle_vector.h" |
20 #include "mojo/edk/embedder/platform_shared_buffer.h" | 21 #include "mojo/edk/embedder/platform_shared_buffer.h" |
21 #include "mojo/edk/embedder/scoped_platform_handle.h" | 22 #include "mojo/edk/embedder/scoped_platform_handle.h" |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 // Create the relay used to transfer mach ports between processes. | 66 // Create the relay used to transfer mach ports between processes. |
66 void CreateMachPortRelay(base::PortProvider* port_provider); | 67 void CreateMachPortRelay(base::PortProvider* port_provider); |
67 #endif | 68 #endif |
68 | 69 |
69 // Called exactly once, shortly after construction, and before any other | 70 // Called exactly once, shortly after construction, and before any other |
70 // methods are called on this object. | 71 // methods are called on this object. |
71 void SetIOTaskRunner(scoped_refptr<base::TaskRunner> io_task_runner); | 72 void SetIOTaskRunner(scoped_refptr<base::TaskRunner> io_task_runner); |
72 | 73 |
73 // Connects this node to a child node. This node will initiate a handshake. | 74 // Connects this node to a child node. This node will initiate a handshake. |
74 void ConnectToChild(base::ProcessHandle process_handle, | 75 void ConnectToChild(base::ProcessHandle process_handle, |
75 ScopedPlatformHandle platform_handle); | 76 ScopedPlatformHandle platform_handle, |
| 77 const std::string& child_token); |
| 78 |
| 79 // Closes all reserved ports which associated with the child process |
| 80 // |child_token|. |
| 81 void CloseChildPorts(const std::string& child_token); |
76 | 82 |
77 // Connects this node to a parent node. The parent node will initiate a | 83 // Connects this node to a parent node. The parent node will initiate a |
78 // handshake. | 84 // handshake. |
79 void ConnectToParent(ScopedPlatformHandle platform_handle); | 85 void ConnectToParent(ScopedPlatformHandle platform_handle); |
80 | 86 |
81 // Sets a port's observer. If |observer| is null the port's current observer | 87 // Sets a port's observer. If |observer| is null the port's current observer |
82 // is removed. | 88 // is removed. |
83 void SetPortObserver(const ports::PortRef& port, | 89 void SetPortObserver(const ports::PortRef& port, |
84 const scoped_refptr<PortObserver>& observer); | 90 const scoped_refptr<PortObserver>& observer); |
85 | 91 |
86 // Closes a port. Use this in lieu of calling Node::ClosePort() directly, as | 92 // Closes a port. Use this in lieu of calling Node::ClosePort() directly, as |
87 // it ensures the port's observer has also been removed. | 93 // it ensures the port's observer has also been removed. |
88 void ClosePort(const ports::PortRef& port); | 94 void ClosePort(const ports::PortRef& port); |
89 | 95 |
90 // Sends a message on a port to its peer. | 96 // Sends a message on a port to its peer. |
91 int SendMessage(const ports::PortRef& port_ref, | 97 int SendMessage(const ports::PortRef& port_ref, |
92 std::unique_ptr<PortsMessage> message); | 98 std::unique_ptr<PortsMessage> message); |
93 | 99 |
94 // Reserves a local port |port| associated with |token|. A peer holding a copy | 100 // Reserves a local port |port| associated with |token|. A peer holding a copy |
95 // of |token| can merge one of its own ports into this one. | 101 // of |token| can merge one of its own ports into this one. |
96 void ReservePort(const std::string& token, const ports::PortRef& port); | 102 void ReservePort(const std::string& token, const ports::PortRef& port, |
| 103 const std::string& child_token); |
97 | 104 |
98 // Merges a local port |port| into a port reserved by |token| in the parent. | 105 // Merges a local port |port| into a port reserved by |token| in the parent. |
99 void MergePortIntoParent(const std::string& token, | 106 void MergePortIntoParent(const std::string& token, |
100 const ports::PortRef& port); | 107 const ports::PortRef& port); |
101 | 108 |
102 // Merges two local ports together. | 109 // Merges two local ports together. |
103 int MergeLocalPorts(const ports::PortRef& port0, const ports::PortRef& port1); | 110 int MergeLocalPorts(const ports::PortRef& port0, const ports::PortRef& port1); |
104 | 111 |
105 // Creates a new shared buffer for use in the current process. | 112 // Creates a new shared buffer for use in the current process. |
106 scoped_refptr<PlatformSharedBuffer> CreateSharedBuffer(size_t num_bytes); | 113 scoped_refptr<PlatformSharedBuffer> CreateSharedBuffer(size_t num_bytes); |
107 | 114 |
108 // Request that the Node be shut down cleanly. This may take an arbitrarily | 115 // Request that the Node be shut down cleanly. This may take an arbitrarily |
109 // long time to complete, at which point |callback| will be called. | 116 // long time to complete, at which point |callback| will be called. |
110 // | 117 // |
111 // Note that while it is safe to continue using the NodeController's public | 118 // Note that while it is safe to continue using the NodeController's public |
112 // interface after requesting shutdown, you do so at your own risk and there | 119 // interface after requesting shutdown, you do so at your own risk and there |
113 // is NO guarantee that new messages will be sent or ports will complete | 120 // is NO guarantee that new messages will be sent or ports will complete |
114 // transfer. | 121 // transfer. |
115 void RequestShutdown(const base::Closure& callback); | 122 void RequestShutdown(const base::Closure& callback); |
116 | 123 |
117 private: | 124 private: |
118 friend Core; | 125 friend Core; |
119 | 126 |
120 using NodeMap = std::unordered_map<ports::NodeName, | 127 using NodeMap = std::unordered_map<ports::NodeName, |
121 scoped_refptr<NodeChannel>>; | 128 scoped_refptr<NodeChannel>>; |
122 using OutgoingMessageQueue = std::queue<Channel::MessagePtr>; | 129 using OutgoingMessageQueue = std::queue<Channel::MessagePtr>; |
123 | 130 |
| 131 struct ReservedPort { |
| 132 ports::PortRef port; |
| 133 const std::string child_token; |
| 134 }; |
| 135 |
124 void ConnectToChildOnIOThread(base::ProcessHandle process_handle, | 136 void ConnectToChildOnIOThread(base::ProcessHandle process_handle, |
125 ScopedPlatformHandle platform_handle); | 137 ScopedPlatformHandle platform_handle, |
| 138 ports::NodeName token); |
126 void ConnectToParentOnIOThread(ScopedPlatformHandle platform_handle); | 139 void ConnectToParentOnIOThread(ScopedPlatformHandle platform_handle); |
127 | 140 |
128 scoped_refptr<NodeChannel> GetPeerChannel(const ports::NodeName& name); | 141 scoped_refptr<NodeChannel> GetPeerChannel(const ports::NodeName& name); |
129 scoped_refptr<NodeChannel> GetParentChannel(); | 142 scoped_refptr<NodeChannel> GetParentChannel(); |
130 scoped_refptr<NodeChannel> GetBrokerChannel(); | 143 scoped_refptr<NodeChannel> GetBrokerChannel(); |
131 | 144 |
132 void AddPeer(const ports::NodeName& name, | 145 void AddPeer(const ports::NodeName& name, |
133 scoped_refptr<NodeChannel> channel, | 146 scoped_refptr<NodeChannel> channel, |
134 bool start_channel); | 147 bool start_channel); |
135 void DropPeer(const ports::NodeName& name); | 148 void DropPeer(const ports::NodeName& name); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 // Guards |peers_| and |pending_peer_messages_|. | 215 // Guards |peers_| and |pending_peer_messages_|. |
203 base::Lock peers_lock_; | 216 base::Lock peers_lock_; |
204 | 217 |
205 // Channels to known peers, including parent and children, if any. | 218 // Channels to known peers, including parent and children, if any. |
206 NodeMap peers_; | 219 NodeMap peers_; |
207 | 220 |
208 // Outgoing message queues for peers we've heard of but can't yet talk to. | 221 // Outgoing message queues for peers we've heard of but can't yet talk to. |
209 std::unordered_map<ports::NodeName, OutgoingMessageQueue> | 222 std::unordered_map<ports::NodeName, OutgoingMessageQueue> |
210 pending_peer_messages_; | 223 pending_peer_messages_; |
211 | 224 |
212 // Guards |reserved_ports_|. | 225 // Guards |reserved_ports_| and |pending_child_tokens_|. |
213 base::Lock reserved_ports_lock_; | 226 base::Lock reserved_ports_lock_; |
214 | 227 |
215 // Ports reserved by token. | 228 // Ports reserved by token. Key is the port token. |
216 base::hash_map<std::string, ports::PortRef> reserved_ports_; | 229 base::hash_map<std::string, ReservedPort> reserved_ports_; |
| 230 // TODO(amistry): This _really_ needs to be a bimap. Unfortunately, we don't |
| 231 // have one yet :( |
| 232 std::unordered_map<ports::NodeName, std::string> pending_child_tokens_; |
217 | 233 |
218 // Guards |pending_port_merges_|. | 234 // Guards |pending_port_merges_|. |
219 base::Lock pending_port_merges_lock_; | 235 base::Lock pending_port_merges_lock_; |
220 | 236 |
221 // A set of port merge requests awaiting parent connection. | 237 // A set of port merge requests awaiting parent connection. |
222 std::vector<std::pair<std::string, ports::PortRef>> pending_port_merges_; | 238 std::vector<std::pair<std::string, ports::PortRef>> pending_port_merges_; |
223 | 239 |
224 // Guards |parent_name_| and |bootstrap_parent_channel_|. | 240 // Guards |parent_name_| and |bootstrap_parent_channel_|. |
225 base::Lock parent_lock_; | 241 base::Lock parent_lock_; |
226 | 242 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 std::unique_ptr<MachPortRelay> mach_port_relay_; | 297 std::unique_ptr<MachPortRelay> mach_port_relay_; |
282 #endif | 298 #endif |
283 | 299 |
284 DISALLOW_COPY_AND_ASSIGN(NodeController); | 300 DISALLOW_COPY_AND_ASSIGN(NodeController); |
285 }; | 301 }; |
286 | 302 |
287 } // namespace edk | 303 } // namespace edk |
288 } // namespace mojo | 304 } // namespace mojo |
289 | 305 |
290 #endif // MOJO_EDK_SYSTEM_NODE_CONTROLLER_H_ | 306 #endif // MOJO_EDK_SYSTEM_NODE_CONTROLLER_H_ |
OLD | NEW |