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