| 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> |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 // 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. |
| 75 void ConnectToChild(base::ProcessHandle process_handle, | 75 void ConnectToChild(base::ProcessHandle process_handle, |
| 76 ScopedPlatformHandle platform_handle, | 76 ScopedPlatformHandle platform_handle, |
| 77 const std::string& child_token, | 77 const std::string& child_token, |
| 78 const ProcessErrorCallback& process_error_callback); | 78 const ProcessErrorCallback& process_error_callback); |
| 79 | 79 |
| 80 // Closes all reserved ports which associated with the child process | 80 // Closes all reserved ports which associated with the child process |
| 81 // |child_token|. | 81 // |child_token|. |
| 82 void CloseChildPorts(const std::string& child_token); | 82 void CloseChildPorts(const std::string& child_token); |
| 83 | 83 |
| 84 // Close a connection to a peer associated with |peer_token|. |
| 85 void ClosePeerConnection(const std::string& peer_token); |
| 86 |
| 84 // Connects this node to a parent node. The parent node will initiate a | 87 // Connects this node to a parent node. The parent node will initiate a |
| 85 // handshake. | 88 // handshake. |
| 86 void ConnectToParent(ScopedPlatformHandle platform_handle); | 89 void ConnectToParent(ScopedPlatformHandle platform_handle); |
| 87 | 90 |
| 88 // Connects this node to a peer node. On success, |port| will be merged with | 91 // Connects this node to a peer node. On success, |port| will be merged with |
| 89 // the corresponding port in the peer node. | 92 // the corresponding port in the peer node. |
| 90 void ConnectToPeer(ScopedPlatformHandle handle, const ports::PortRef& port); | 93 void ConnectToPeer(ScopedPlatformHandle handle, |
| 94 const ports::PortRef& port, |
| 95 const std::string& peer_token); |
| 91 | 96 |
| 92 // Sets a port's observer. If |observer| is null the port's current observer | 97 // Sets a port's observer. If |observer| is null the port's current observer |
| 93 // is removed. | 98 // is removed. |
| 94 void SetPortObserver(const ports::PortRef& port, | 99 void SetPortObserver(const ports::PortRef& port, |
| 95 const scoped_refptr<PortObserver>& observer); | 100 const scoped_refptr<PortObserver>& observer); |
| 96 | 101 |
| 97 // Closes a port. Use this in lieu of calling Node::ClosePort() directly, as | 102 // Closes a port. Use this in lieu of calling Node::ClosePort() directly, as |
| 98 // it ensures the port's observer has also been removed. | 103 // it ensures the port's observer has also been removed. |
| 99 void ClosePort(const ports::PortRef& port); | 104 void ClosePort(const ports::PortRef& port); |
| 100 | 105 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 136 | 141 |
| 137 using NodeMap = std::unordered_map<ports::NodeName, | 142 using NodeMap = std::unordered_map<ports::NodeName, |
| 138 scoped_refptr<NodeChannel>>; | 143 scoped_refptr<NodeChannel>>; |
| 139 using OutgoingMessageQueue = std::queue<Channel::MessagePtr>; | 144 using OutgoingMessageQueue = std::queue<Channel::MessagePtr>; |
| 140 | 145 |
| 141 struct ReservedPort { | 146 struct ReservedPort { |
| 142 ports::PortRef port; | 147 ports::PortRef port; |
| 143 const std::string child_token; | 148 const std::string child_token; |
| 144 }; | 149 }; |
| 145 | 150 |
| 151 struct PeerConnection { |
| 152 PeerConnection(); |
| 153 PeerConnection(const PeerConnection& other); |
| 154 PeerConnection(PeerConnection&& other); |
| 155 PeerConnection(const scoped_refptr<NodeChannel>& channel, |
| 156 const ports::PortRef& local_port, |
| 157 const std::string& peer_token); |
| 158 ~PeerConnection(); |
| 159 |
| 160 PeerConnection& operator=(const PeerConnection& other); |
| 161 PeerConnection& operator=(PeerConnection&& other); |
| 162 |
| 163 |
| 164 scoped_refptr<NodeChannel> channel; |
| 165 ports::PortRef local_port; |
| 166 std::string peer_token; |
| 167 }; |
| 168 |
| 146 void ConnectToChildOnIOThread( | 169 void ConnectToChildOnIOThread( |
| 147 base::ProcessHandle process_handle, | 170 base::ProcessHandle process_handle, |
| 148 ScopedPlatformHandle platform_handle, | 171 ScopedPlatformHandle platform_handle, |
| 149 ports::NodeName token, | 172 ports::NodeName token, |
| 150 const ProcessErrorCallback& process_error_callback); | 173 const ProcessErrorCallback& process_error_callback); |
| 151 void ConnectToParentOnIOThread(ScopedPlatformHandle platform_handle); | 174 void ConnectToParentOnIOThread(ScopedPlatformHandle platform_handle); |
| 152 | 175 |
| 153 void ConnectToPeerOnIOThread(ScopedPlatformHandle handle, | 176 void ConnectToPeerOnIOThread(ScopedPlatformHandle handle, |
| 154 ports::NodeName token, | 177 ports::NodeName token, |
| 155 ports::PortRef port); | 178 ports::PortRef port, |
| 179 const std::string& peer_token); |
| 180 void ClosePeerConnectionOnIOThread(const std::string& node_name); |
| 156 | 181 |
| 157 scoped_refptr<NodeChannel> GetPeerChannel(const ports::NodeName& name); | 182 scoped_refptr<NodeChannel> GetPeerChannel(const ports::NodeName& name); |
| 158 scoped_refptr<NodeChannel> GetParentChannel(); | 183 scoped_refptr<NodeChannel> GetParentChannel(); |
| 159 scoped_refptr<NodeChannel> GetBrokerChannel(); | 184 scoped_refptr<NodeChannel> GetBrokerChannel(); |
| 160 | 185 |
| 161 void AddPeer(const ports::NodeName& name, | 186 void AddPeer(const ports::NodeName& name, |
| 162 scoped_refptr<NodeChannel> channel, | 187 scoped_refptr<NodeChannel> channel, |
| 163 bool start_channel); | 188 bool start_channel); |
| 164 void DropPeer(const ports::NodeName& name, NodeChannel* channel); | 189 void DropPeer(const ports::NodeName& name, NodeChannel* channel); |
| 165 void SendPeerMessage(const ports::NodeName& name, | 190 void SendPeerMessage(const ports::NodeName& name, |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 // Flag to fast-path checking |shutdown_callback_|. | 341 // Flag to fast-path checking |shutdown_callback_|. |
| 317 AtomicFlag shutdown_callback_flag_; | 342 AtomicFlag shutdown_callback_flag_; |
| 318 | 343 |
| 319 // All other fields below must only be accessed on the I/O thread, i.e., the | 344 // All other fields below must only be accessed on the I/O thread, i.e., the |
| 320 // thread on which core_->io_task_runner() runs tasks. | 345 // thread on which core_->io_task_runner() runs tasks. |
| 321 | 346 |
| 322 // Channels to children during handshake. | 347 // Channels to children during handshake. |
| 323 NodeMap pending_children_; | 348 NodeMap pending_children_; |
| 324 | 349 |
| 325 using PeerNodeMap = | 350 using PeerNodeMap = |
| 326 std::unordered_map<ports::NodeName, | 351 std::unordered_map<ports::NodeName, PeerConnection>; |
| 327 std::pair<scoped_refptr<NodeChannel>, ports::PortRef>>; | 352 PeerNodeMap peer_connections_; |
| 328 PeerNodeMap pending_peers_; | 353 |
| 354 // Maps from peer token to node name, pending or not. |
| 355 std::unordered_map<std::string, ports::NodeName> peers_by_token_; |
| 329 | 356 |
| 330 // Indicates whether this object should delete itself on IO thread shutdown. | 357 // Indicates whether this object should delete itself on IO thread shutdown. |
| 331 // Must only be accessed from the IO thread. | 358 // Must only be accessed from the IO thread. |
| 332 bool destroy_on_io_thread_shutdown_ = false; | 359 bool destroy_on_io_thread_shutdown_ = false; |
| 333 | 360 |
| 334 #if !defined(OS_MACOSX) && !defined(OS_NACL_SFI) | 361 #if !defined(OS_MACOSX) && !defined(OS_NACL_SFI) |
| 335 // Broker for sync shared buffer creation in children. | 362 // Broker for sync shared buffer creation in children. |
| 336 std::unique_ptr<Broker> broker_; | 363 std::unique_ptr<Broker> broker_; |
| 337 #endif | 364 #endif |
| 338 | 365 |
| 339 #if defined(OS_MACOSX) && !defined(OS_IOS) | 366 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 340 base::Lock mach_port_relay_lock_; | 367 base::Lock mach_port_relay_lock_; |
| 341 // Relay for transferring mach ports to/from children. | 368 // Relay for transferring mach ports to/from children. |
| 342 std::unique_ptr<MachPortRelay> mach_port_relay_; | 369 std::unique_ptr<MachPortRelay> mach_port_relay_; |
| 343 #endif | 370 #endif |
| 344 | 371 |
| 345 DISALLOW_COPY_AND_ASSIGN(NodeController); | 372 DISALLOW_COPY_AND_ASSIGN(NodeController); |
| 346 }; | 373 }; |
| 347 | 374 |
| 348 } // namespace edk | 375 } // namespace edk |
| 349 } // namespace mojo | 376 } // namespace mojo |
| 350 | 377 |
| 351 #endif // MOJO_EDK_SYSTEM_NODE_CONTROLLER_H_ | 378 #endif // MOJO_EDK_SYSTEM_NODE_CONTROLLER_H_ |
| OLD | NEW |