| 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/compiler_specific.h" |
| 10 #include "base/containers/hash_tables.h" | 11 #include "base/containers/hash_tables.h" |
| 11 #include "base/macros.h" | 12 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 13 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 14 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 15 #include "mojo/edk/embedder/scoped_platform_handle.h" | 16 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 16 #include "mojo/edk/system/connection_manager.h" | 17 #include "mojo/edk/system/connection_manager.h" |
| 17 #include "mojo/edk/system/system_impl_export.h" | 18 #include "mojo/edk/system/system_impl_export.h" |
| 18 | 19 |
| 19 namespace base { | 20 namespace base { |
| 20 class TaskRunner; | 21 class TaskRunner; |
| 21 class WaitableEvent; | 22 class WaitableEvent; |
| 22 } | 23 } |
| 23 | 24 |
| 24 namespace mojo { | 25 namespace mojo { |
| 25 | 26 |
| 26 namespace embedder { | 27 namespace embedder { |
| 27 class MasterProcessDelegate; | 28 class MasterProcessDelegate; |
| 28 class SlaveInfo; | 29 typedef void* SlaveInfo; |
| 29 } | 30 } |
| 30 | 31 |
| 31 namespace system { | 32 namespace system { |
| 32 | 33 |
| 33 // The master process will always have this "process identifier". | 34 // The master process will always have this "process identifier". |
| 34 const ProcessIdentifier kMasterProcessIdentifier = 1; | 35 const ProcessIdentifier kMasterProcessIdentifier = 1; |
| 35 | 36 |
| 36 // The |ConnectionManager| implementation for the master process. | 37 // The |ConnectionManager| implementation for the master process. |
| 37 // | 38 // |
| 38 // Objects of this class may be created and destroyed on any thread. However, | 39 // This class is thread-safe (except that no public methods may be called from |
| 39 // |Init()| and |Shutdown()| must be called on the "delegate thread". Otherwise, | 40 // its internal, private thread), with condition that |Init()| be called before |
| 40 // its public methods are thread-safe (except that they may not be called from | 41 // anything else and |Shutdown()| be called before destruction (and no other |
| 41 // its internal, private thread). | 42 // public methods may be called during/after |Shutdown()|). |
| 42 class MOJO_SYSTEM_IMPL_EXPORT MasterConnectionManager | 43 class MOJO_SYSTEM_IMPL_EXPORT MasterConnectionManager |
| 43 : public ConnectionManager { | 44 : public ConnectionManager { |
| 44 public: | 45 public: |
| 45 // Note: None of the public methods may be called from |private_thread_|. | 46 // Note: None of the public methods may be called from |private_thread_|. |
| 46 | 47 |
| 47 MasterConnectionManager(); | 48 MasterConnectionManager(); |
| 48 ~MasterConnectionManager() override; | 49 ~MasterConnectionManager() override; |
| 49 | 50 |
| 50 // No other methods may be called until after this has been called. | 51 // No other methods may be called until after this has been called. |
| 51 // |delegate_thread_task_runner| should be the task runner for the "delegate | 52 // |delegate_thread_task_runner| should be the task runner for the "delegate |
| 52 // thread", on which |master_process_delegate|'s methods will be called. Both | 53 // thread", on which |master_process_delegate|'s methods will be called. Both |
| 53 // must stay alive at least until after |Shutdown()| has been called. | 54 // must stay alive at least until after |Shutdown()| has been called. |
| 54 void Init(scoped_refptr<base::TaskRunner> delegate_thread_task_runner, | 55 void Init(scoped_refptr<base::TaskRunner> delegate_thread_task_runner, |
| 55 embedder::MasterProcessDelegate* master_process_delegate); | 56 embedder::MasterProcessDelegate* master_process_delegate); |
| 56 | 57 |
| 57 // No other methods may be called after this is (or while it is being) called. | |
| 58 void Shutdown(); | |
| 59 | |
| 60 // Adds a slave process and sets up/tracks a connection to that slave (using | 58 // Adds a slave process and sets up/tracks a connection to that slave (using |
| 61 // |platform_handle|). (|slave_info| is used by the caller/implementation of | 59 // |platform_handle|). |slave_info| is used by the caller/implementation of |
| 62 // |embedder::MasterProcessDelegate| to track this process; ownership of | 60 // |embedder::MasterProcessDelegate| to track this process. It must remain |
| 63 // |slave_info| will be returned to the delegate via |OnSlaveDisconnect()|, | 61 // alive until the delegate's |OnSlaveDisconnect()| is called with it as the |
| 64 // which will always be called for each slave, assuming proper shutdown.) | 62 // argument. |OnSlaveDisconnect()| will always be called for each slave, |
| 65 void AddSlave(scoped_ptr<embedder::SlaveInfo> slave_info, | 63 // assuming proper shutdown.) |
| 64 void AddSlave(embedder::SlaveInfo slave_info, |
| 66 embedder::ScopedPlatformHandle platform_handle); | 65 embedder::ScopedPlatformHandle platform_handle); |
| 67 | 66 |
| 68 // |ConnectionManager| methods: | 67 // |ConnectionManager| methods: |
| 68 void Shutdown() override; |
| 69 bool AllowConnect(const ConnectionIdentifier& connection_id) override; | 69 bool AllowConnect(const ConnectionIdentifier& connection_id) override; |
| 70 bool CancelConnect(const ConnectionIdentifier& connection_id) override; | 70 bool CancelConnect(const ConnectionIdentifier& connection_id) override; |
| 71 bool Connect(const ConnectionIdentifier& connection_id, | 71 bool Connect(const ConnectionIdentifier& connection_id, |
| 72 ProcessIdentifier* peer_process_identifier, | 72 ProcessIdentifier* peer_process_identifier, |
| 73 embedder::ScopedPlatformHandle* platform_handle) override; | 73 embedder::ScopedPlatformHandle* platform_handle) override; |
| 74 | 74 |
| 75 private: | 75 private: |
| 76 class Helper; | 76 class Helper; |
| 77 | 77 |
| 78 // These should be thread-safe and may be called on any thread, including | 78 // These should be thread-safe and may be called on any thread, including |
| 79 // |private_thread_|: | 79 // |private_thread_|: |
| 80 bool AllowConnectImpl(ProcessIdentifier process_identifier, | 80 bool AllowConnectImpl(ProcessIdentifier process_identifier, |
| 81 const ConnectionIdentifier& connection_id); | 81 const ConnectionIdentifier& connection_id); |
| 82 bool CancelConnectImpl(ProcessIdentifier process_identifier, | 82 bool CancelConnectImpl(ProcessIdentifier process_identifier, |
| 83 const ConnectionIdentifier& connection_id); | 83 const ConnectionIdentifier& connection_id); |
| 84 bool ConnectImpl(ProcessIdentifier process_identifier, | 84 bool ConnectImpl(ProcessIdentifier process_identifier, |
| 85 const ConnectionIdentifier& connection_id, | 85 const ConnectionIdentifier& connection_id, |
| 86 ProcessIdentifier* peer_process_identifier, | 86 ProcessIdentifier* peer_process_identifier, |
| 87 embedder::ScopedPlatformHandle* platform_handle); | 87 embedder::ScopedPlatformHandle* platform_handle); |
| 88 | 88 |
| 89 // These should only be called on |private_thread_|: | 89 // These should only be called on |private_thread_|: |
| 90 void ShutdownOnPrivateThread(); | 90 void ShutdownOnPrivateThread(); |
| 91 // Signals |*event| on completion. | 91 // Signals |*event| on completion. |
| 92 void AddSlaveOnPrivateThread(scoped_ptr<embedder::SlaveInfo> slave_info, | 92 void AddSlaveOnPrivateThread(embedder::SlaveInfo slave_info, |
| 93 embedder::ScopedPlatformHandle platform_handle, | 93 embedder::ScopedPlatformHandle platform_handle, |
| 94 base::WaitableEvent* event); | 94 base::WaitableEvent* event); |
| 95 // Called by |Helper::OnError()|. | 95 // Called by |Helper::OnError()|. |
| 96 void OnError(ProcessIdentifier process_identifier); | 96 void OnError(ProcessIdentifier process_identifier); |
| 97 // Posts a call to |master_process_delegate_->OnSlaveDisconnect()|. | 97 // Posts a call to |master_process_delegate_->OnSlaveDisconnect()|. |
| 98 void CallOnSlaveDisconnect(scoped_ptr<embedder::SlaveInfo> slave_info); | 98 void CallOnSlaveDisconnect(embedder::SlaveInfo slave_info); |
| 99 | |
| 100 // Asserts that the current thread is the delegate thread. (This actually | |
| 101 // checks the current message loop.) | |
| 102 // TODO(vtl): Probably we should actually check the thread. | |
| 103 void AssertOnDelegateThread() const; | |
| 104 | 99 |
| 105 // Asserts that the current thread is *not* |private_thread_| (no-op if | 100 // Asserts that the current thread is *not* |private_thread_| (no-op if |
| 106 // DCHECKs are not enabled). This should only be called while | 101 // DCHECKs are not enabled). This should only be called while |
| 107 // |private_thread_| is alive (i.e., after |Init()| but before |Shutdown()|). | 102 // |private_thread_| is alive (i.e., after |Init()| but before |Shutdown()|). |
| 108 void AssertNotOnPrivateThread() const; | 103 void AssertNotOnPrivateThread() const; |
| 109 | 104 |
| 110 // Asserts that the current thread is |private_thread_| (no-op if DCHECKs are | 105 // Asserts that the current thread is |private_thread_| (no-op if DCHECKs are |
| 111 // not enabled). This should only be called while |private_thread_| is alive | 106 // not enabled). This should only be called while |private_thread_| is alive |
| 112 // (i.e., after |Init()| but before |Shutdown()|). | 107 // (i.e., after |Init()| but before |Shutdown()|). |
| 113 void AssertOnPrivateThread() const; | 108 void AssertOnPrivateThread() const; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 135 base::hash_map<ConnectionIdentifier, PendingConnectionInfo*> | 130 base::hash_map<ConnectionIdentifier, PendingConnectionInfo*> |
| 136 pending_connections_; // Owns its values. | 131 pending_connections_; // Owns its values. |
| 137 | 132 |
| 138 DISALLOW_COPY_AND_ASSIGN(MasterConnectionManager); | 133 DISALLOW_COPY_AND_ASSIGN(MasterConnectionManager); |
| 139 }; | 134 }; |
| 140 | 135 |
| 141 } // namespace system | 136 } // namespace system |
| 142 } // namespace mojo | 137 } // namespace mojo |
| 143 | 138 |
| 144 #endif // MOJO_EDK_SYSTEM_MASTER_CONNECTION_MANAGER_H_ | 139 #endif // MOJO_EDK_SYSTEM_MASTER_CONNECTION_MANAGER_H_ |
| OLD | NEW |