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 <unordered_map> | 10 #include <unordered_map> |
11 | 11 |
12 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
13 #include "mojo/edk/embedder/scoped_platform_handle.h" | 13 #include "mojo/edk/platform/scoped_platform_handle.h" |
14 #include "mojo/edk/platform/task_runner.h" | 14 #include "mojo/edk/platform/task_runner.h" |
15 #include "mojo/edk/system/connection_manager.h" | 15 #include "mojo/edk/system/connection_manager.h" |
16 #include "mojo/edk/util/mutex.h" | 16 #include "mojo/edk/util/mutex.h" |
17 #include "mojo/edk/util/ref_ptr.h" | 17 #include "mojo/edk/util/ref_ptr.h" |
18 #include "mojo/edk/util/thread_annotations.h" | 18 #include "mojo/edk/util/thread_annotations.h" |
19 #include "mojo/public/cpp/system/macros.h" | 19 #include "mojo/public/cpp/system/macros.h" |
20 | 20 |
21 namespace base { | 21 namespace base { |
22 class TaskRunner; | 22 class TaskRunner; |
23 } | 23 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 MOJO_NOT_THREAD_SAFE; | 59 MOJO_NOT_THREAD_SAFE; |
60 | 60 |
61 // Adds a slave process and sets up/tracks a connection to that slave (using | 61 // Adds a slave process and sets up/tracks a connection to that slave (using |
62 // |platform_handle|). |slave_info| is used by the caller/implementation of | 62 // |platform_handle|). |slave_info| is used by the caller/implementation of |
63 // |embedder::MasterProcessDelegate| to track this process. It must remain | 63 // |embedder::MasterProcessDelegate| to track this process. It must remain |
64 // alive until the delegate's |OnSlaveDisconnect()| is called with it as the | 64 // alive until the delegate's |OnSlaveDisconnect()| is called with it as the |
65 // argument. |OnSlaveDisconnect()| will always be called for each slave, | 65 // argument. |OnSlaveDisconnect()| will always be called for each slave, |
66 // assuming proper shutdown. Returns the process identifier for the | 66 // assuming proper shutdown. Returns the process identifier for the |
67 // newly-added slave. | 67 // newly-added slave. |
68 ProcessIdentifier AddSlave(embedder::SlaveInfo slave_info, | 68 ProcessIdentifier AddSlave(embedder::SlaveInfo slave_info, |
69 embedder::ScopedPlatformHandle platform_handle); | 69 platform::ScopedPlatformHandle platform_handle); |
70 | 70 |
71 // Like |AddSlave()|, but allows a connection to be bootstrapped: both the | 71 // Like |AddSlave()|, but allows a connection to be bootstrapped: both the |
72 // master and slave may call |Connect()| with |connection_id| immediately (as | 72 // master and slave may call |Connect()| with |connection_id| immediately (as |
73 // if both had already called |AllowConnect()|). |connection_id| must be | 73 // if both had already called |AllowConnect()|). |connection_id| must be |
74 // unique (i.e., not previously used). | 74 // unique (i.e., not previously used). |
75 // TODO(vtl): Is |AddSlave()| really needed? (It's probably mostly useful for | 75 // TODO(vtl): Is |AddSlave()| really needed? (It's probably mostly useful for |
76 // tests.) | 76 // tests.) |
77 ProcessIdentifier AddSlaveAndBootstrap( | 77 ProcessIdentifier AddSlaveAndBootstrap( |
78 embedder::SlaveInfo slave_info, | 78 embedder::SlaveInfo slave_info, |
79 embedder::ScopedPlatformHandle platform_handle, | 79 platform::ScopedPlatformHandle platform_handle, |
80 const ConnectionIdentifier& connection_id); | 80 const ConnectionIdentifier& connection_id); |
81 | 81 |
82 // |ConnectionManager| methods: | 82 // |ConnectionManager| methods: |
83 void Shutdown() override MOJO_NOT_THREAD_SAFE; | 83 void Shutdown() override MOJO_NOT_THREAD_SAFE; |
84 bool AllowConnect(const ConnectionIdentifier& connection_id) override; | 84 bool AllowConnect(const ConnectionIdentifier& connection_id) override; |
85 bool CancelConnect(const ConnectionIdentifier& connection_id) override; | 85 bool CancelConnect(const ConnectionIdentifier& connection_id) override; |
86 Result Connect(const ConnectionIdentifier& connection_id, | 86 Result Connect(const ConnectionIdentifier& connection_id, |
87 ProcessIdentifier* peer_process_identifier, | 87 ProcessIdentifier* peer_process_identifier, |
88 bool* is_first, | 88 bool* is_first, |
89 embedder::ScopedPlatformHandle* platform_handle) override; | 89 platform::ScopedPlatformHandle* platform_handle) override; |
90 | 90 |
91 private: | 91 private: |
92 class Helper; | 92 class Helper; |
93 | 93 |
94 // These should be thread-safe and may be called on any thread, including | 94 // These should be thread-safe and may be called on any thread, including |
95 // |private_thread_|: | 95 // |private_thread_|: |
96 bool AllowConnectImpl(ProcessIdentifier process_identifier, | 96 bool AllowConnectImpl(ProcessIdentifier process_identifier, |
97 const ConnectionIdentifier& connection_id); | 97 const ConnectionIdentifier& connection_id); |
98 bool CancelConnectImpl(ProcessIdentifier process_identifier, | 98 bool CancelConnectImpl(ProcessIdentifier process_identifier, |
99 const ConnectionIdentifier& connection_id); | 99 const ConnectionIdentifier& connection_id); |
100 Result ConnectImpl(ProcessIdentifier process_identifier, | 100 Result ConnectImpl(ProcessIdentifier process_identifier, |
101 const ConnectionIdentifier& connection_id, | 101 const ConnectionIdentifier& connection_id, |
102 ProcessIdentifier* peer_process_identifier, | 102 ProcessIdentifier* peer_process_identifier, |
103 bool* is_first, | 103 bool* is_first, |
104 embedder::ScopedPlatformHandle* platform_handle); | 104 platform::ScopedPlatformHandle* platform_handle); |
105 | 105 |
106 // Helper for |ConnectImpl()|. This is called when the two process identifiers | 106 // Helper for |ConnectImpl()|. This is called when the two process identifiers |
107 // are known (and known to be valid), and all that remains is to determine the | 107 // are known (and known to be valid), and all that remains is to determine the |
108 // |Result| and provide a platform handle if appropriate. (This will never | 108 // |Result| and provide a platform handle if appropriate. (This will never |
109 // return |Result::FAILURE|.) | 109 // return |Result::FAILURE|.) |
110 Result ConnectImplHelperNoLock( | 110 Result ConnectImplHelperNoLock( |
111 ProcessIdentifier process_identifier, | 111 ProcessIdentifier process_identifier, |
112 ProcessIdentifier peer_process_identifier, | 112 ProcessIdentifier peer_process_identifier, |
113 embedder::ScopedPlatformHandle* platform_handle) | 113 platform::ScopedPlatformHandle* platform_handle) |
114 MOJO_EXCLUSIVE_LOCKS_REQUIRED(mutex_); | 114 MOJO_EXCLUSIVE_LOCKS_REQUIRED(mutex_); |
115 | 115 |
116 // These should only be called on |private_thread_|: | 116 // These should only be called on |private_thread_|: |
117 void ShutdownOnPrivateThread() MOJO_NOT_THREAD_SAFE; | 117 void ShutdownOnPrivateThread() MOJO_NOT_THREAD_SAFE; |
118 // Signals |*event| on completion. | 118 // Signals |*event| on completion. |
119 void AddSlaveOnPrivateThread(embedder::SlaveInfo slave_info, | 119 void AddSlaveOnPrivateThread(embedder::SlaveInfo slave_info, |
120 embedder::ScopedPlatformHandle platform_handle, | 120 platform::ScopedPlatformHandle platform_handle, |
121 ProcessIdentifier slave_process_identifier, | 121 ProcessIdentifier slave_process_identifier, |
122 util::AutoResetWaitableEvent* event); | 122 util::AutoResetWaitableEvent* event); |
123 // Called by |Helper::OnError()|. | 123 // Called by |Helper::OnError()|. |
124 void OnError(ProcessIdentifier process_identifier); | 124 void OnError(ProcessIdentifier process_identifier); |
125 // Posts a call to |master_process_delegate_->OnSlaveDisconnect()|. | 125 // Posts a call to |master_process_delegate_->OnSlaveDisconnect()|. |
126 void CallOnSlaveDisconnect(embedder::SlaveInfo slave_info); | 126 void CallOnSlaveDisconnect(embedder::SlaveInfo slave_info); |
127 | 127 |
128 // Asserts that the current thread is *not* |private_thread_| (no-op if | 128 // Asserts that the current thread is *not* |private_thread_| (no-op if |
129 // DCHECKs are not enabled). This should only be called while | 129 // DCHECKs are not enabled). This should only be called while |
130 // |private_thread_| is alive (i.e., after |Init()| but before |Shutdown()|). | 130 // |private_thread_| is alive (i.e., after |Init()| but before |Shutdown()|). |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
173 std::unordered_map<ProcessIdentifier, ProcessConnections*> connections_ | 173 std::unordered_map<ProcessIdentifier, ProcessConnections*> connections_ |
174 MOJO_GUARDED_BY(mutex_); // Owns its values. | 174 MOJO_GUARDED_BY(mutex_); // Owns its values. |
175 | 175 |
176 MOJO_DISALLOW_COPY_AND_ASSIGN(MasterConnectionManager); | 176 MOJO_DISALLOW_COPY_AND_ASSIGN(MasterConnectionManager); |
177 }; | 177 }; |
178 | 178 |
179 } // namespace system | 179 } // namespace system |
180 } // namespace mojo | 180 } // namespace mojo |
181 | 181 |
182 #endif // MOJO_EDK_SYSTEM_MASTER_CONNECTION_MANAGER_H_ | 182 #endif // MOJO_EDK_SYSTEM_MASTER_CONNECTION_MANAGER_H_ |
OLD | NEW |