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/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/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 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 const base::Closure& callback, | 67 const base::Closure& callback, |
68 util::RefPtr<platform::TaskRunner>&& callback_thread_task_runner); | 68 util::RefPtr<platform::TaskRunner>&& callback_thread_task_runner); |
69 | 69 |
70 // 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 |
71 // |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 |
72 // constructor). |channel_id| should be a valid |ChannelId| (i.e., nonzero) | 72 // constructor). |channel_id| should be a valid |ChannelId| (i.e., nonzero) |
73 // not "assigned" to any other |Channel| being managed by this | 73 // not "assigned" to any other |Channel| being managed by this |
74 // |ChannelManager|. | 74 // |ChannelManager|. |
75 util::RefPtr<MessagePipeDispatcher> CreateChannelOnIOThread( | 75 util::RefPtr<MessagePipeDispatcher> CreateChannelOnIOThread( |
76 ChannelId channel_id, | 76 ChannelId channel_id, |
77 embedder::ScopedPlatformHandle platform_handle); | 77 platform::ScopedPlatformHandle platform_handle); |
78 | 78 |
79 // Like |CreateChannelOnIOThread()|, but doesn't create a bootstrap message | 79 // Like |CreateChannelOnIOThread()|, but doesn't create a bootstrap message |
80 // pipe. Returns the newly-created |Channel|. | 80 // pipe. Returns the newly-created |Channel|. |
81 // 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 |
82 // general). | 82 // general). |
83 util::RefPtr<Channel> CreateChannelWithoutBootstrapOnIOThread( | 83 util::RefPtr<Channel> CreateChannelWithoutBootstrapOnIOThread( |
84 ChannelId channel_id, | 84 ChannelId channel_id, |
85 embedder::ScopedPlatformHandle platform_handle); | 85 platform::ScopedPlatformHandle platform_handle); |
86 | 86 |
87 // Like |CreateChannelOnIOThread()|, but may be called from any thread. On | 87 // Like |CreateChannelOnIOThread()|, but may be called from any thread. On |
88 // completion, will call |callback| (using |callback_thread_task_runner| if it | 88 // completion, will call |callback| (using |callback_thread_task_runner| if it |
89 // 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 |
90 // the I/O thread, even if called from that thread. | 90 // the I/O thread, even if called from that thread. |
91 util::RefPtr<MessagePipeDispatcher> CreateChannel( | 91 util::RefPtr<MessagePipeDispatcher> CreateChannel( |
92 ChannelId channel_id, | 92 ChannelId channel_id, |
93 embedder::ScopedPlatformHandle platform_handle, | 93 platform::ScopedPlatformHandle platform_handle, |
94 const base::Closure& callback, | 94 const base::Closure& callback, |
95 util::RefPtr<platform::TaskRunner>&& callback_thread_task_runner); | 95 util::RefPtr<platform::TaskRunner>&& callback_thread_task_runner); |
96 | 96 |
97 // Gets the |Channel| with the given ID (which must exist). | 97 // Gets the |Channel| with the given ID (which must exist). |
98 util::RefPtr<Channel> GetChannel(ChannelId channel_id) const; | 98 util::RefPtr<Channel> GetChannel(ChannelId channel_id) const; |
99 | 99 |
100 // 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 |
101 // soon (by calling |ShutdownChannel()|). Calling this is optional (and may in | 101 // soon (by calling |ShutdownChannel()|). Calling this is optional (and may in |
102 // 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., |
103 // 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 |
(...skipping 23 matching lines...) Expand all Loading... |
127 // that doesn't work with |base::Bind()|. | 127 // that doesn't work with |base::Bind()|. |
128 void ShutdownHelper( | 128 void ShutdownHelper( |
129 const base::Closure& callback, | 129 const base::Closure& callback, |
130 util::RefPtr<platform::TaskRunner> callback_thread_task_runner); | 130 util::RefPtr<platform::TaskRunner> callback_thread_task_runner); |
131 | 131 |
132 // Used by |CreateChannelOnIOThread()| and |CreateChannelHelper()|. Called on | 132 // Used by |CreateChannelOnIOThread()| and |CreateChannelHelper()|. Called on |
133 // 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. |
134 // Returns the newly-created |Channel|. | 134 // Returns the newly-created |Channel|. |
135 util::RefPtr<Channel> CreateChannelOnIOThreadHelper( | 135 util::RefPtr<Channel> CreateChannelOnIOThreadHelper( |
136 ChannelId channel_id, | 136 ChannelId channel_id, |
137 embedder::ScopedPlatformHandle platform_handle, | 137 platform::ScopedPlatformHandle platform_handle, |
138 util::RefPtr<ChannelEndpoint>&& bootstrap_channel_endpoint); | 138 util::RefPtr<ChannelEndpoint>&& bootstrap_channel_endpoint); |
139 | 139 |
140 // Used by |CreateChannel()|. Called on the I/O thread. | 140 // Used by |CreateChannel()|. Called on the I/O thread. |
141 // TODO(vtl): |bootstrap_channel_endpoint| and |callback_thread_task_runner| | 141 // TODO(vtl): |bootstrap_channel_endpoint| and |callback_thread_task_runner| |
142 // 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 |
143 // base::Bind. | 143 // base::Bind. |
144 void CreateChannelHelper( | 144 void CreateChannelHelper( |
145 ChannelId channel_id, | 145 ChannelId channel_id, |
146 embedder::ScopedPlatformHandle platform_handle, | 146 platform::ScopedPlatformHandle platform_handle, |
147 util::RefPtr<ChannelEndpoint> bootstrap_channel_endpoint, | 147 util::RefPtr<ChannelEndpoint> bootstrap_channel_endpoint, |
148 const base::Closure& callback, | 148 const base::Closure& callback, |
149 util::RefPtr<platform::TaskRunner> callback_thread_task_runner); | 149 util::RefPtr<platform::TaskRunner> callback_thread_task_runner); |
150 | 150 |
151 // Note: These must not be used after shutdown. | 151 // Note: These must not be used after shutdown. |
152 embedder::PlatformSupport* const platform_support_; | 152 embedder::PlatformSupport* const platform_support_; |
153 const util::RefPtr<platform::TaskRunner> io_thread_task_runner_; | 153 const util::RefPtr<platform::TaskRunner> io_thread_task_runner_; |
154 ConnectionManager* const connection_manager_; | 154 ConnectionManager* const connection_manager_; |
155 | 155 |
156 // Note: |Channel| methods should not be called under |mutex_|. | 156 // Note: |Channel| methods should not be called under |mutex_|. |
157 // TODO(vtl): Annotate the above rule using |MOJO_ACQUIRED_{BEFORE,AFTER}()|, | 157 // TODO(vtl): Annotate the above rule using |MOJO_ACQUIRED_{BEFORE,AFTER}()|, |
158 // once clang actually checks such annotations. | 158 // once clang actually checks such annotations. |
159 // https://github.com/domokit/mojo/issues/313 | 159 // https://github.com/domokit/mojo/issues/313 |
160 mutable util::Mutex mutex_; | 160 mutable util::Mutex mutex_; |
161 | 161 |
162 using ChannelIdToChannelMap = | 162 using ChannelIdToChannelMap = |
163 std::unordered_map<ChannelId, util::RefPtr<Channel>>; | 163 std::unordered_map<ChannelId, util::RefPtr<Channel>>; |
164 ChannelIdToChannelMap channels_ MOJO_GUARDED_BY(mutex_); | 164 ChannelIdToChannelMap channels_ MOJO_GUARDED_BY(mutex_); |
165 | 165 |
166 MOJO_DISALLOW_COPY_AND_ASSIGN(ChannelManager); | 166 MOJO_DISALLOW_COPY_AND_ASSIGN(ChannelManager); |
167 }; | 167 }; |
168 | 168 |
169 } // namespace system | 169 } // namespace system |
170 } // namespace mojo | 170 } // namespace mojo |
171 | 171 |
172 #endif // MOJO_EDK_SYSTEM_CHANNEL_MANAGER_H_ | 172 #endif // MOJO_EDK_SYSTEM_CHANNEL_MANAGER_H_ |
OLD | NEW |