| 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 MOJO_EDK_SYSTEM_MASTER_CONNECTION_MANAGER_H_ | 5 #ifndef MOJO_EDK_SYSTEM_MASTER_CONNECTION_MANAGER_H_ |
| 6 #define MOJO_EDK_SYSTEM_MASTER_CONNECTION_MANAGER_H_ | 6 #define MOJO_EDK_SYSTEM_MASTER_CONNECTION_MANAGER_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/synchronization/lock.h" | |
| 13 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
| 14 #include "mojo/edk/embedder/scoped_platform_handle.h" | 13 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 15 #include "mojo/edk/system/connection_manager.h" | 14 #include "mojo/edk/system/connection_manager.h" |
| 15 #include "mojo/edk/system/mutex.h" |
| 16 #include "mojo/edk/system/system_impl_export.h" | 16 #include "mojo/edk/system/system_impl_export.h" |
| 17 #include "mojo/public/cpp/system/macros.h" | 17 #include "mojo/public/cpp/system/macros.h" |
| 18 | 18 |
| 19 namespace base { | 19 namespace base { |
| 20 class TaskRunner; | 20 class TaskRunner; |
| 21 class WaitableEvent; | 21 class WaitableEvent; |
| 22 } | 22 } |
| 23 | 23 |
| 24 namespace mojo { | 24 namespace mojo { |
| 25 | 25 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 44 // |platform_support| must be valid and remain alive until after |Shutdown()| | 44 // |platform_support| must be valid and remain alive until after |Shutdown()| |
| 45 // has completed. | 45 // has completed. |
| 46 explicit MasterConnectionManager(embedder::PlatformSupport* platform_support); | 46 explicit MasterConnectionManager(embedder::PlatformSupport* platform_support); |
| 47 ~MasterConnectionManager() override; | 47 ~MasterConnectionManager() override; |
| 48 | 48 |
| 49 // No other methods may be called until after this has been called. | 49 // No other methods may be called until after this has been called. |
| 50 // |delegate_thread_task_runner| should be the task runner for the "delegate | 50 // |delegate_thread_task_runner| should be the task runner for the "delegate |
| 51 // thread", on which |master_process_delegate|'s methods will be called. Both | 51 // thread", on which |master_process_delegate|'s methods will be called. Both |
| 52 // must stay alive at least until after |Shutdown()| has been called. | 52 // must stay alive at least until after |Shutdown()| has been called. |
| 53 void Init(scoped_refptr<base::TaskRunner> delegate_thread_task_runner, | 53 void Init(scoped_refptr<base::TaskRunner> delegate_thread_task_runner, |
| 54 embedder::MasterProcessDelegate* master_process_delegate); | 54 embedder::MasterProcessDelegate* master_process_delegate) |
| 55 MOJO_NOT_THREAD_SAFE; |
| 55 | 56 |
| 56 // Adds a slave process and sets up/tracks a connection to that slave (using | 57 // Adds a slave process and sets up/tracks a connection to that slave (using |
| 57 // |platform_handle|). |slave_info| is used by the caller/implementation of | 58 // |platform_handle|). |slave_info| is used by the caller/implementation of |
| 58 // |embedder::MasterProcessDelegate| to track this process. It must remain | 59 // |embedder::MasterProcessDelegate| to track this process. It must remain |
| 59 // alive until the delegate's |OnSlaveDisconnect()| is called with it as the | 60 // alive until the delegate's |OnSlaveDisconnect()| is called with it as the |
| 60 // argument. |OnSlaveDisconnect()| will always be called for each slave, | 61 // argument. |OnSlaveDisconnect()| will always be called for each slave, |
| 61 // assuming proper shutdown. Returns the process identifier for the | 62 // assuming proper shutdown. Returns the process identifier for the |
| 62 // newly-added slave. | 63 // newly-added slave. |
| 63 ProcessIdentifier AddSlave(embedder::SlaveInfo slave_info, | 64 ProcessIdentifier AddSlave(embedder::SlaveInfo slave_info, |
| 64 embedder::ScopedPlatformHandle platform_handle); | 65 embedder::ScopedPlatformHandle platform_handle); |
| 65 | 66 |
| 66 // Like |AddSlave()|, but allows a connection to be bootstrapped: both the | 67 // Like |AddSlave()|, but allows a connection to be bootstrapped: both the |
| 67 // master and slave may call |Connect()| with |connection_id| immediately (as | 68 // master and slave may call |Connect()| with |connection_id| immediately (as |
| 68 // if both had already called |AllowConnect()|). |connection_id| must be | 69 // if both had already called |AllowConnect()|). |connection_id| must be |
| 69 // unique (i.e., not previously used). | 70 // unique (i.e., not previously used). |
| 70 // TODO(vtl): Is |AddSlave()| really needed? (It's probably mostly useful for | 71 // TODO(vtl): Is |AddSlave()| really needed? (It's probably mostly useful for |
| 71 // tests.) | 72 // tests.) |
| 72 ProcessIdentifier AddSlaveAndBootstrap( | 73 ProcessIdentifier AddSlaveAndBootstrap( |
| 73 embedder::SlaveInfo slave_info, | 74 embedder::SlaveInfo slave_info, |
| 74 embedder::ScopedPlatformHandle platform_handle, | 75 embedder::ScopedPlatformHandle platform_handle, |
| 75 const ConnectionIdentifier& connection_id); | 76 const ConnectionIdentifier& connection_id); |
| 76 | 77 |
| 77 // |ConnectionManager| methods: | 78 // |ConnectionManager| methods: |
| 78 void Shutdown() override; | 79 void Shutdown() override MOJO_NOT_THREAD_SAFE; |
| 79 bool AllowConnect(const ConnectionIdentifier& connection_id) override; | 80 bool AllowConnect(const ConnectionIdentifier& connection_id) override; |
| 80 bool CancelConnect(const ConnectionIdentifier& connection_id) override; | 81 bool CancelConnect(const ConnectionIdentifier& connection_id) override; |
| 81 bool Connect(const ConnectionIdentifier& connection_id, | 82 Result Connect(const ConnectionIdentifier& connection_id, |
| 82 ProcessIdentifier* peer_process_identifier, | 83 ProcessIdentifier* peer_process_identifier, |
| 83 embedder::ScopedPlatformHandle* platform_handle) override; | 84 embedder::ScopedPlatformHandle* platform_handle) override; |
| 84 | 85 |
| 85 private: | 86 private: |
| 86 class Helper; | 87 class Helper; |
| 87 | 88 |
| 88 // These should be thread-safe and may be called on any thread, including | 89 // These should be thread-safe and may be called on any thread, including |
| 89 // |private_thread_|: | 90 // |private_thread_|: |
| 90 bool AllowConnectImpl(ProcessIdentifier process_identifier, | 91 bool AllowConnectImpl(ProcessIdentifier process_identifier, |
| 91 const ConnectionIdentifier& connection_id); | 92 const ConnectionIdentifier& connection_id); |
| 92 bool CancelConnectImpl(ProcessIdentifier process_identifier, | 93 bool CancelConnectImpl(ProcessIdentifier process_identifier, |
| 93 const ConnectionIdentifier& connection_id); | 94 const ConnectionIdentifier& connection_id); |
| 94 bool ConnectImpl(ProcessIdentifier process_identifier, | 95 Result ConnectImpl(ProcessIdentifier process_identifier, |
| 95 const ConnectionIdentifier& connection_id, | 96 const ConnectionIdentifier& connection_id, |
| 96 ProcessIdentifier* peer_process_identifier, | 97 ProcessIdentifier* peer_process_identifier, |
| 97 embedder::ScopedPlatformHandle* platform_handle); | 98 embedder::ScopedPlatformHandle* platform_handle); |
| 98 | 99 |
| 99 // These should only be called on |private_thread_|: | 100 // These should only be called on |private_thread_|: |
| 100 void ShutdownOnPrivateThread(); | 101 void ShutdownOnPrivateThread() MOJO_NOT_THREAD_SAFE; |
| 101 // Signals |*event| on completion. | 102 // Signals |*event| on completion. |
| 102 void AddSlaveOnPrivateThread(embedder::SlaveInfo slave_info, | 103 void AddSlaveOnPrivateThread(embedder::SlaveInfo slave_info, |
| 103 embedder::ScopedPlatformHandle platform_handle, | 104 embedder::ScopedPlatformHandle platform_handle, |
| 104 ProcessIdentifier slave_process_identifier, | 105 ProcessIdentifier slave_process_identifier, |
| 105 base::WaitableEvent* event); | 106 base::WaitableEvent* event); |
| 106 // Called by |Helper::OnError()|. | 107 // Called by |Helper::OnError()|. |
| 107 void OnError(ProcessIdentifier process_identifier); | 108 void OnError(ProcessIdentifier process_identifier); |
| 108 // Posts a call to |master_process_delegate_->OnSlaveDisconnect()|. | 109 // Posts a call to |master_process_delegate_->OnSlaveDisconnect()|. |
| 109 void CallOnSlaveDisconnect(embedder::SlaveInfo slave_info); | 110 void CallOnSlaveDisconnect(embedder::SlaveInfo slave_info); |
| 110 | 111 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 125 scoped_refptr<base::TaskRunner> delegate_thread_task_runner_; | 126 scoped_refptr<base::TaskRunner> delegate_thread_task_runner_; |
| 126 embedder::MasterProcessDelegate* master_process_delegate_; | 127 embedder::MasterProcessDelegate* master_process_delegate_; |
| 127 | 128 |
| 128 // This is a private I/O thread on which this class does the bulk of its work. | 129 // This is a private I/O thread on which this class does the bulk of its work. |
| 129 // It is started in |Init()| and terminated in |Shutdown()|. | 130 // It is started in |Init()| and terminated in |Shutdown()|. |
| 130 base::Thread private_thread_; | 131 base::Thread private_thread_; |
| 131 | 132 |
| 132 // The following members are only accessed on |private_thread_|: | 133 // The following members are only accessed on |private_thread_|: |
| 133 base::hash_map<ProcessIdentifier, Helper*> helpers_; // Owns its values. | 134 base::hash_map<ProcessIdentifier, Helper*> helpers_; // Owns its values. |
| 134 | 135 |
| 135 // Protects the members below (except in the constructor, |Init()|, | 136 // Note: |mutex_| is not needed in the constructor, |Init()|, |
| 136 // |Shutdown()|/|ShutdownOnPrivateThread()|, and the destructor). | 137 // |Shutdown()|/|ShutdownOnPrivateThread()|, or the destructor |
| 137 base::Lock lock_; | 138 Mutex mutex_; |
| 138 | 139 |
| 139 ProcessIdentifier next_process_identifier_; | 140 ProcessIdentifier next_process_identifier_ MOJO_GUARDED_BY(mutex_); |
| 140 | 141 |
| 141 struct PendingConnectionInfo; | 142 struct PendingConnectionInfo; |
| 142 base::hash_map<ConnectionIdentifier, PendingConnectionInfo*> | 143 base::hash_map<ConnectionIdentifier, PendingConnectionInfo*> |
| 143 pending_connections_; // Owns its values. | 144 pending_connections_ MOJO_GUARDED_BY(mutex_); // Owns its values. |
| 144 | 145 |
| 145 MOJO_DISALLOW_COPY_AND_ASSIGN(MasterConnectionManager); | 146 MOJO_DISALLOW_COPY_AND_ASSIGN(MasterConnectionManager); |
| 146 }; | 147 }; |
| 147 | 148 |
| 148 } // namespace system | 149 } // namespace system |
| 149 } // namespace mojo | 150 } // namespace mojo |
| 150 | 151 |
| 151 #endif // MOJO_EDK_SYSTEM_MASTER_CONNECTION_MANAGER_H_ | 152 #endif // MOJO_EDK_SYSTEM_MASTER_CONNECTION_MANAGER_H_ |
| OLD | NEW |