OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef MOJO_EDK_SYSTEM_MASTER_CONNECTION_MANAGER_H_ |
| 6 #define MOJO_EDK_SYSTEM_MASTER_CONNECTION_MANAGER_H_ |
| 7 |
| 8 #include <stdint.h> |
| 9 |
| 10 #include "base/containers/hash_tables.h" |
| 11 #include "base/macros.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/synchronization/lock.h" |
| 14 #include "base/threading/thread.h" |
| 15 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 16 #include "mojo/edk/system/connection_manager.h" |
| 17 #include "mojo/edk/system/system_impl_export.h" |
| 18 |
| 19 namespace base { |
| 20 class TaskRunner; |
| 21 class WaitableEvent; |
| 22 } |
| 23 |
| 24 namespace mojo { |
| 25 |
| 26 namespace embedder { |
| 27 class MasterProcessDelegate; |
| 28 class SlaveInfo; |
| 29 } |
| 30 |
| 31 namespace system { |
| 32 |
| 33 // The |ConnectionManager| implementation for the master process. |
| 34 // |
| 35 // Objects of this class must be created, initialized (via |Init()|), shut down |
| 36 // (via |Shutdown()|), and destroyed on the same thread (the "creation thread"). |
| 37 // Otherwise, its public methods are thread-safe (except that they may not be |
| 38 // called from its internal, private thread). |
| 39 class MOJO_SYSTEM_IMPL_EXPORT MasterConnectionManager |
| 40 : public ConnectionManager { |
| 41 public: |
| 42 // Note: None of the public methods may be called from |private_thread_|. |
| 43 |
| 44 MasterConnectionManager(); |
| 45 ~MasterConnectionManager() override; |
| 46 |
| 47 // No other methods may be called until after this has been called. |
| 48 // |master_process_delegate| must stay alive at least until after |Shutdown()| |
| 49 // has been called; its methods will be called on this object's creation |
| 50 // thread. |
| 51 void Init(embedder::MasterProcessDelegate* master_process_delegate); |
| 52 |
| 53 // No other methods may be called after this is (or while it is being) called. |
| 54 void Shutdown(); |
| 55 |
| 56 // 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 // |embedder::MasterProcessDelegate| to track this process; ownership of |
| 59 // |slave_info| will be returned to the delegate via |OnSlaveDisconnect()|, |
| 60 // which will always be called for each slave, assuming proper shutdown.) |
| 61 void AddSlave(scoped_ptr<embedder::SlaveInfo> slave_info, |
| 62 embedder::ScopedPlatformHandle platform_handle); |
| 63 |
| 64 // |ConnectionManager| methods: |
| 65 bool AllowConnect(const ConnectionIdentifier& connection_id) override; |
| 66 bool CancelConnect(const ConnectionIdentifier& connection_id) override; |
| 67 bool Connect(const ConnectionIdentifier& connection_id, |
| 68 ProcessIdentifier* peer_process_identifier, |
| 69 embedder::ScopedPlatformHandle* platform_handle) override; |
| 70 |
| 71 private: |
| 72 class Helper; |
| 73 |
| 74 // These should be thread-safe and may be called on any thread, including |
| 75 // |private_thread_|: |
| 76 bool AllowConnectImpl(ProcessIdentifier process_identifier, |
| 77 const ConnectionIdentifier& connection_id); |
| 78 bool CancelConnectImpl(ProcessIdentifier process_identifier, |
| 79 const ConnectionIdentifier& connection_id); |
| 80 bool ConnectImpl(ProcessIdentifier process_identifier, |
| 81 const ConnectionIdentifier& connection_id, |
| 82 ProcessIdentifier* peer_process_identifier, |
| 83 embedder::ScopedPlatformHandle* platform_handle); |
| 84 |
| 85 // These should only be called on |private_thread_|: |
| 86 void ShutdownOnPrivateThread(); |
| 87 // Signals |*event| on completion. |
| 88 void AddSlaveOnPrivateThread(scoped_ptr<embedder::SlaveInfo> slave_info, |
| 89 embedder::ScopedPlatformHandle platform_handle, |
| 90 base::WaitableEvent* event); |
| 91 // Called by |Helper::OnError()|. |
| 92 void OnError(ProcessIdentifier process_identifier); |
| 93 // Posts a call to |master_process_delegate_->OnSlaveDisconnect()|. |
| 94 void CallOnSlaveDisconnect(scoped_ptr<embedder::SlaveInfo> slave_info); |
| 95 |
| 96 // Asserts that the current thread is the creation thread. (This actually |
| 97 // checks the current message loop, which is what we depend on, not the thread |
| 98 // per se.) |
| 99 void AssertOnCreationThread() const; |
| 100 |
| 101 // Asserts that the current thread is *not* |private_thread_| (no-op if |
| 102 // DCHECKs are not enabled). This should only be called while |
| 103 // |private_thread_| is alive (i.e., after |Init()| but before |Shutdown()|). |
| 104 void AssertNotOnPrivateThread() const; |
| 105 |
| 106 // Asserts that the current thread is |private_thread_| (no-op if DCHECKs are |
| 107 // not enabled). This should only be called while |private_thread_| is alive |
| 108 // (i.e., after |Init()| but before |Shutdown()|). |
| 109 void AssertOnPrivateThread() const; |
| 110 |
| 111 const scoped_refptr<base::TaskRunner> creation_thread_task_runner_; |
| 112 |
| 113 // This is set in |Init()| before |private_thread_| exists and only cleared in |
| 114 // |Shutdown()| after |private_thread_| is dead. Thus it's safe to "use" on |
| 115 // |private_thread_|. (Note that |master_process_delegate_| may only be called |
| 116 // from the creation thread.) |
| 117 embedder::MasterProcessDelegate* master_process_delegate_; |
| 118 |
| 119 // This is a private I/O thread on which this class does the bulk of its work. |
| 120 // It is started in |Init()| and terminated in |Shutdown()|. |
| 121 base::Thread private_thread_; |
| 122 |
| 123 // The following members are only accessed on |private_thread_|: |
| 124 ProcessIdentifier next_process_identifier_; |
| 125 base::hash_map<ProcessIdentifier, Helper*> helpers_; // Owns its values. |
| 126 |
| 127 // Protects the members below (except in the constructor, |Init()|, |
| 128 // |Shutdown()|/|ShutdownOnPrivateThread()|, and the destructor). |
| 129 base::Lock lock_; |
| 130 |
| 131 struct PendingConnectionInfo; |
| 132 base::hash_map<ConnectionIdentifier, PendingConnectionInfo*> |
| 133 pending_connections_; // Owns its values. |
| 134 |
| 135 DISALLOW_COPY_AND_ASSIGN(MasterConnectionManager); |
| 136 }; |
| 137 |
| 138 } // namespace system |
| 139 } // namespace mojo |
| 140 |
| 141 #endif // MOJO_EDK_SYSTEM_MASTER_CONNECTION_MANAGER_H_ |
OLD | NEW |