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 |
124 void ConnectToChildOnIOThread(base::ProcessHandle process_handle, | 131 void ConnectToChildOnIOThread(base::ProcessHandle process_handle, |
125 ScopedPlatformHandle platform_handle); | 132 ScopedPlatformHandle platform_handle, |
133 ports::NodeName token); | |
126 void ConnectToParentOnIOThread(ScopedPlatformHandle platform_handle); | 134 void ConnectToParentOnIOThread(ScopedPlatformHandle platform_handle); |
127 | 135 |
128 scoped_refptr<NodeChannel> GetPeerChannel(const ports::NodeName& name); | 136 scoped_refptr<NodeChannel> GetPeerChannel(const ports::NodeName& name); |
129 scoped_refptr<NodeChannel> GetParentChannel(); | 137 scoped_refptr<NodeChannel> GetParentChannel(); |
130 scoped_refptr<NodeChannel> GetBrokerChannel(); | 138 scoped_refptr<NodeChannel> GetBrokerChannel(); |
131 | 139 |
132 void AddPeer(const ports::NodeName& name, | 140 void AddPeer(const ports::NodeName& name, |
133 scoped_refptr<NodeChannel> channel, | 141 scoped_refptr<NodeChannel> channel, |
134 bool start_channel); | 142 bool start_channel); |
135 void DropPeer(const ports::NodeName& name); | 143 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_|. | 210 // Guards |peers_| and |pending_peer_messages_|. |
203 base::Lock peers_lock_; | 211 base::Lock peers_lock_; |
204 | 212 |
205 // Channels to known peers, including parent and children, if any. | 213 // Channels to known peers, including parent and children, if any. |
206 NodeMap peers_; | 214 NodeMap peers_; |
207 | 215 |
208 // Outgoing message queues for peers we've heard of but can't yet talk to. | 216 // Outgoing message queues for peers we've heard of but can't yet talk to. |
209 std::unordered_map<ports::NodeName, OutgoingMessageQueue> | 217 std::unordered_map<ports::NodeName, OutgoingMessageQueue> |
210 pending_peer_messages_; | 218 pending_peer_messages_; |
211 | 219 |
212 // Guards |reserved_ports_|. | 220 // Guards |reserved_ports_| and |pending_child_tokens_|. |
213 base::Lock reserved_ports_lock_; | 221 base::Lock reserved_ports_lock_; |
214 | 222 |
215 // Ports reserved by token. | 223 // Ports reserved by token. |
ncarter (slow)
2016/06/02 17:43:28
Seems like there are two flavors of token in this
Anand Mistry (off Chromium)
2016/06/03 04:39:41
Done.
| |
216 base::hash_map<std::string, ports::PortRef> reserved_ports_; | 224 base::hash_map<std::string, std::pair<ports::PortRef, std::string>> |
ncarter (slow)
2016/06/02 17:43:28
With c++11 uniform inititalization syntax, it's re
Anand Mistry (off Chromium)
2016/06/03 04:39:40
Done.
| |
225 reserved_ports_; | |
226 // TODO(amistry): This _really_ needs to be a bimap. Unfortunately, we don't | |
227 // have one yet :( | |
228 std::unordered_map<ports::NodeName, std::string> pending_child_tokens_; | |
217 | 229 |
218 // Guards |pending_port_merges_|. | 230 // Guards |pending_port_merges_|. |
219 base::Lock pending_port_merges_lock_; | 231 base::Lock pending_port_merges_lock_; |
220 | 232 |
221 // A set of port merge requests awaiting parent connection. | 233 // A set of port merge requests awaiting parent connection. |
222 std::vector<std::pair<std::string, ports::PortRef>> pending_port_merges_; | 234 std::vector<std::pair<std::string, ports::PortRef>> pending_port_merges_; |
223 | 235 |
224 // Guards |parent_name_| and |bootstrap_parent_channel_|. | 236 // Guards |parent_name_| and |bootstrap_parent_channel_|. |
225 base::Lock parent_lock_; | 237 base::Lock parent_lock_; |
226 | 238 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
281 std::unique_ptr<MachPortRelay> mach_port_relay_; | 293 std::unique_ptr<MachPortRelay> mach_port_relay_; |
282 #endif | 294 #endif |
283 | 295 |
284 DISALLOW_COPY_AND_ASSIGN(NodeController); | 296 DISALLOW_COPY_AND_ASSIGN(NodeController); |
285 }; | 297 }; |
286 | 298 |
287 } // namespace edk | 299 } // namespace edk |
288 } // namespace mojo | 300 } // namespace mojo |
289 | 301 |
290 #endif // MOJO_EDK_SYSTEM_NODE_CONTROLLER_H_ | 302 #endif // MOJO_EDK_SYSTEM_NODE_CONTROLLER_H_ |
OLD | NEW |