OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_CHANNEL_MANAGER_H_ | 5 #ifndef MOJO_EDK_SYSTEM_CHANNEL_MANAGER_H_ |
6 #define MOJO_EDK_SYSTEM_CHANNEL_MANAGER_H_ | 6 #define MOJO_EDK_SYSTEM_CHANNEL_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/callback_forward.h" | 12 #include "base/callback_forward.h" |
13 #include "mojo/edk/embedder/platform_task_runner.h" | |
14 #include "mojo/edk/embedder/scoped_platform_handle.h" | 13 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 14 #include "mojo/edk/platform/task_runner.h" |
15 #include "mojo/edk/system/channel_id.h" | 15 #include "mojo/edk/system/channel_id.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 } |
24 | 24 |
(...skipping 12 matching lines...) Expand all Loading... |
37 | 37 |
38 // This class manages and "owns" |Channel|s (which typically connect to other | 38 // This class manages and "owns" |Channel|s (which typically connect to other |
39 // processes) for a given process. This class is thread-safe, except as | 39 // processes) for a given process. This class is thread-safe, except as |
40 // specifically noted. | 40 // specifically noted. |
41 class ChannelManager { | 41 class ChannelManager { |
42 public: | 42 public: |
43 // |io_thread_task_runner| should be the |TaskRunner| for the I/O thread, on | 43 // |io_thread_task_runner| should be the |TaskRunner| for the I/O thread, on |
44 // which this channel manager will create all channels. Connection manager is | 44 // which this channel manager will create all channels. Connection manager is |
45 // optional and may be null. All arguments (if non-null) must remain alive at | 45 // optional and may be null. All arguments (if non-null) must remain alive at |
46 // least until after shutdown completion. | 46 // least until after shutdown completion. |
47 ChannelManager( | 47 ChannelManager(embedder::PlatformSupport* platform_support, |
48 embedder::PlatformSupport* platform_support, | 48 util::RefPtr<platform::TaskRunner>&& io_thread_task_runner, |
49 util::RefPtr<embedder::PlatformTaskRunner>&& io_thread_task_runner, | 49 ConnectionManager* connection_manager); |
50 ConnectionManager* connection_manager); | |
51 ~ChannelManager(); | 50 ~ChannelManager(); |
52 | 51 |
53 // Shuts down the channel manager, including shutting down all channels (as if | 52 // Shuts down the channel manager, including shutting down all channels (as if |
54 // |ShutdownChannelOnIOThread()| were called for each channel). This must be | 53 // |ShutdownChannelOnIOThread()| were called for each channel). This must be |
55 // called from the I/O thread (given to the constructor) and completes | 54 // called from the I/O thread (given to the constructor) and completes |
56 // synchronously. This, or |Shutdown()|, must be called before destroying this | 55 // synchronously. This, or |Shutdown()|, must be called before destroying this |
57 // object. | 56 // object. |
58 void ShutdownOnIOThread(); | 57 void ShutdownOnIOThread(); |
59 | 58 |
60 // Like |ShutdownOnIOThread()|, but may be called from any thread. On | 59 // Like |ShutdownOnIOThread()|, but may be called from any thread. On |
61 // completion, will call |callback| ("on" |io_thread_task_runner| if | 60 // completion, will call |callback| ("on" |io_thread_task_runner| if |
62 // |callback_thread_task_runner| is null else by posted using | 61 // |callback_thread_task_runner| is null else by posted using |
63 // |callback_thread_task_runner|). Note: This will always post a task to the | 62 // |callback_thread_task_runner|). Note: This will always post a task to the |
64 // I/O thread, even it is the current thread. | 63 // I/O thread, even it is the current thread. |
65 // TODO(vtl): Consider if this is really necessary, since it only has one use | 64 // TODO(vtl): Consider if this is really necessary, since it only has one use |
66 // (in tests). | 65 // (in tests). |
67 void Shutdown( | 66 void Shutdown( |
68 const base::Closure& callback, | 67 const base::Closure& callback, |
69 util::RefPtr<embedder::PlatformTaskRunner>&& callback_thread_task_runner); | 68 util::RefPtr<platform::TaskRunner>&& callback_thread_task_runner); |
70 | 69 |
71 // Creates a |Channel| and adds it to the set of channels managed by this | 70 // Creates a |Channel| and adds it to the set of channels managed by this |
72 // |ChannelManager|. This must be called from the I/O thread (given to the | 71 // |ChannelManager|. This must be called from the I/O thread (given to the |
73 // constructor). |channel_id| should be a valid |ChannelId| (i.e., nonzero) | 72 // constructor). |channel_id| should be a valid |ChannelId| (i.e., nonzero) |
74 // not "assigned" to any other |Channel| being managed by this | 73 // not "assigned" to any other |Channel| being managed by this |
75 // |ChannelManager|. | 74 // |ChannelManager|. |
76 util::RefPtr<MessagePipeDispatcher> CreateChannelOnIOThread( | 75 util::RefPtr<MessagePipeDispatcher> CreateChannelOnIOThread( |
77 ChannelId channel_id, | 76 ChannelId channel_id, |
78 embedder::ScopedPlatformHandle platform_handle); | 77 embedder::ScopedPlatformHandle platform_handle); |
79 | 78 |
80 // Like |CreateChannelOnIOThread()|, but doesn't create a bootstrap message | 79 // Like |CreateChannelOnIOThread()|, but doesn't create a bootstrap message |
81 // pipe. Returns the newly-created |Channel|. | 80 // pipe. Returns the newly-created |Channel|. |
82 // TODO(vtl): Maybe get rid of the others (and bootstrap message pipes in | 81 // TODO(vtl): Maybe get rid of the others (and bootstrap message pipes in |
83 // general). | 82 // general). |
84 util::RefPtr<Channel> CreateChannelWithoutBootstrapOnIOThread( | 83 util::RefPtr<Channel> CreateChannelWithoutBootstrapOnIOThread( |
85 ChannelId channel_id, | 84 ChannelId channel_id, |
86 embedder::ScopedPlatformHandle platform_handle); | 85 embedder::ScopedPlatformHandle platform_handle); |
87 | 86 |
88 // Like |CreateChannelOnIOThread()|, but may be called from any thread. On | 87 // Like |CreateChannelOnIOThread()|, but may be called from any thread. On |
89 // completion, will call |callback| (using |callback_thread_task_runner| if it | 88 // completion, will call |callback| (using |callback_thread_task_runner| if it |
90 // is non-null, else on the I/O thread). Note: This will always post a task to | 89 // is non-null, else on the I/O thread). Note: This will always post a task to |
91 // the I/O thread, even if called from that thread. | 90 // the I/O thread, even if called from that thread. |
92 util::RefPtr<MessagePipeDispatcher> CreateChannel( | 91 util::RefPtr<MessagePipeDispatcher> CreateChannel( |
93 ChannelId channel_id, | 92 ChannelId channel_id, |
94 embedder::ScopedPlatformHandle platform_handle, | 93 embedder::ScopedPlatformHandle platform_handle, |
95 const base::Closure& callback, | 94 const base::Closure& callback, |
96 util::RefPtr<embedder::PlatformTaskRunner>&& callback_thread_task_runner); | 95 util::RefPtr<platform::TaskRunner>&& callback_thread_task_runner); |
97 | 96 |
98 // Gets the |Channel| with the given ID (which must exist). | 97 // Gets the |Channel| with the given ID (which must exist). |
99 util::RefPtr<Channel> GetChannel(ChannelId channel_id) const; | 98 util::RefPtr<Channel> GetChannel(ChannelId channel_id) const; |
100 | 99 |
101 // Informs the channel manager (and thus channel) that it will be shutdown | 100 // Informs the channel manager (and thus channel) that it will be shutdown |
102 // soon (by calling |ShutdownChannel()|). Calling this is optional (and may in | 101 // soon (by calling |ShutdownChannel()|). Calling this is optional (and may in |
103 // fact be called multiple times) but it will suppress certain warnings (e.g., | 102 // fact be called multiple times) but it will suppress certain warnings (e.g., |
104 // for the channel being broken) and enable others (if messages are written to | 103 // for the channel being broken) and enable others (if messages are written to |
105 // the channel). | 104 // the channel). |
106 void WillShutdownChannel(ChannelId channel_id); | 105 void WillShutdownChannel(ChannelId channel_id); |
107 | 106 |
108 // Shuts down the channel specified by the given ID. This, or | 107 // Shuts down the channel specified by the given ID. This, or |
109 // |ShutdownChannel()|, should be called once per channel (created using | 108 // |ShutdownChannel()|, should be called once per channel (created using |
110 // |CreateChannelOnIOThread()| or |CreateChannel()|). This must be called from | 109 // |CreateChannelOnIOThread()| or |CreateChannel()|). This must be called from |
111 // the I/O thread. | 110 // the I/O thread. |
112 void ShutdownChannelOnIOThread(ChannelId channel_id); | 111 void ShutdownChannelOnIOThread(ChannelId channel_id); |
113 | 112 |
114 // Like |ShutdownChannelOnIOThread()|, but may be called from any thread. It | 113 // Like |ShutdownChannelOnIOThread()|, but may be called from any thread. It |
115 // will always post a task to the I/O thread, and post |callback| to | 114 // will always post a task to the I/O thread, and post |callback| to |
116 // |callback_thread_task_runner| (or execute it directly on the I/O thread if | 115 // |callback_thread_task_runner| (or execute it directly on the I/O thread if |
117 // |callback_thread_task_runner| is null) on completion. | 116 // |callback_thread_task_runner| is null) on completion. |
118 void ShutdownChannel( | 117 void ShutdownChannel( |
119 ChannelId channel_id, | 118 ChannelId channel_id, |
120 const base::Closure& callback, | 119 const base::Closure& callback, |
121 util::RefPtr<embedder::PlatformTaskRunner>&& callback_thread_task_runner); | 120 util::RefPtr<platform::TaskRunner>&& callback_thread_task_runner); |
122 | 121 |
123 ConnectionManager* connection_manager() const { return connection_manager_; } | 122 ConnectionManager* connection_manager() const { return connection_manager_; } |
124 | 123 |
125 private: | 124 private: |
126 // Used by |Shutdown()|. Called on the I/O thread. | 125 // Used by |Shutdown()|. Called on the I/O thread. |
127 // TODO(vtl): |callback_thread_task_runner| should be an rvalue reference, but | 126 // TODO(vtl): |callback_thread_task_runner| should be an rvalue reference, but |
128 // that doesn't work with |base::Bind()|. | 127 // that doesn't work with |base::Bind()|. |
129 void ShutdownHelper( | 128 void ShutdownHelper( |
130 const base::Closure& callback, | 129 const base::Closure& callback, |
131 util::RefPtr<embedder::PlatformTaskRunner> callback_thread_task_runner); | 130 util::RefPtr<platform::TaskRunner> callback_thread_task_runner); |
132 | 131 |
133 // Used by |CreateChannelOnIOThread()| and |CreateChannelHelper()|. Called on | 132 // Used by |CreateChannelOnIOThread()| and |CreateChannelHelper()|. Called on |
134 // the I/O thread. |bootstrap_channel_endpoint| is optional and may be null. | 133 // the I/O thread. |bootstrap_channel_endpoint| is optional and may be null. |
135 // Returns the newly-created |Channel|. | 134 // Returns the newly-created |Channel|. |
136 util::RefPtr<Channel> CreateChannelOnIOThreadHelper( | 135 util::RefPtr<Channel> CreateChannelOnIOThreadHelper( |
137 ChannelId channel_id, | 136 ChannelId channel_id, |
138 embedder::ScopedPlatformHandle platform_handle, | 137 embedder::ScopedPlatformHandle platform_handle, |
139 util::RefPtr<ChannelEndpoint>&& bootstrap_channel_endpoint); | 138 util::RefPtr<ChannelEndpoint>&& bootstrap_channel_endpoint); |
140 | 139 |
141 // Used by |CreateChannel()|. Called on the I/O thread. | 140 // Used by |CreateChannel()|. Called on the I/O thread. |
142 // TODO(vtl): |bootstrap_channel_endpoint| and |callback_thread_task_runner| | 141 // TODO(vtl): |bootstrap_channel_endpoint| and |callback_thread_task_runner| |
143 // should be rvalue references, but that doesn't currently work correctly with | 142 // should be rvalue references, but that doesn't currently work correctly with |
144 // base::Bind. | 143 // base::Bind. |
145 void CreateChannelHelper( | 144 void CreateChannelHelper( |
146 ChannelId channel_id, | 145 ChannelId channel_id, |
147 embedder::ScopedPlatformHandle platform_handle, | 146 embedder::ScopedPlatformHandle platform_handle, |
148 util::RefPtr<ChannelEndpoint> bootstrap_channel_endpoint, | 147 util::RefPtr<ChannelEndpoint> bootstrap_channel_endpoint, |
149 const base::Closure& callback, | 148 const base::Closure& callback, |
150 util::RefPtr<embedder::PlatformTaskRunner> callback_thread_task_runner); | 149 util::RefPtr<platform::TaskRunner> callback_thread_task_runner); |
151 | 150 |
152 // Note: These must not be used after shutdown. | 151 // Note: These must not be used after shutdown. |
153 embedder::PlatformSupport* const platform_support_; | 152 embedder::PlatformSupport* const platform_support_; |
154 const util::RefPtr<embedder::PlatformTaskRunner> io_thread_task_runner_; | 153 const util::RefPtr<platform::TaskRunner> io_thread_task_runner_; |
155 ConnectionManager* const connection_manager_; | 154 ConnectionManager* const connection_manager_; |
156 | 155 |
157 // Note: |Channel| methods should not be called under |mutex_|. | 156 // Note: |Channel| methods should not be called under |mutex_|. |
158 // TODO(vtl): Annotate the above rule using |MOJO_ACQUIRED_{BEFORE,AFTER}()|, | 157 // TODO(vtl): Annotate the above rule using |MOJO_ACQUIRED_{BEFORE,AFTER}()|, |
159 // once clang actually checks such annotations. | 158 // once clang actually checks such annotations. |
160 // https://github.com/domokit/mojo/issues/313 | 159 // https://github.com/domokit/mojo/issues/313 |
161 mutable util::Mutex mutex_; | 160 mutable util::Mutex mutex_; |
162 | 161 |
163 using ChannelIdToChannelMap = | 162 using ChannelIdToChannelMap = |
164 std::unordered_map<ChannelId, util::RefPtr<Channel>>; | 163 std::unordered_map<ChannelId, util::RefPtr<Channel>>; |
165 ChannelIdToChannelMap channels_ MOJO_GUARDED_BY(mutex_); | 164 ChannelIdToChannelMap channels_ MOJO_GUARDED_BY(mutex_); |
166 | 165 |
167 MOJO_DISALLOW_COPY_AND_ASSIGN(ChannelManager); | 166 MOJO_DISALLOW_COPY_AND_ASSIGN(ChannelManager); |
168 }; | 167 }; |
169 | 168 |
170 } // namespace system | 169 } // namespace system |
171 } // namespace mojo | 170 } // namespace mojo |
172 | 171 |
173 #endif // MOJO_EDK_SYSTEM_CHANNEL_MANAGER_H_ | 172 #endif // MOJO_EDK_SYSTEM_CHANNEL_MANAGER_H_ |
OLD | NEW |