| 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 "base/memory/ref_counted.h" | |
| 14 #include "mojo/edk/embedder/platform_task_runner.h" | 13 #include "mojo/edk/embedder/platform_task_runner.h" |
| 15 #include "mojo/edk/embedder/scoped_platform_handle.h" | 14 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 16 #include "mojo/edk/system/channel_id.h" | 15 #include "mojo/edk/system/channel_id.h" |
| 17 #include "mojo/edk/system/mutex.h" | 16 #include "mojo/edk/system/mutex.h" |
| 18 #include "mojo/edk/system/ref_ptr.h" | 17 #include "mojo/edk/util/ref_ptr.h" |
| 19 #include "mojo/public/cpp/system/macros.h" | 18 #include "mojo/public/cpp/system/macros.h" |
| 20 | 19 |
| 21 namespace base { | 20 namespace base { |
| 22 class TaskRunner; | 21 class TaskRunner; |
| 23 } | 22 } |
| 24 | 23 |
| 25 namespace mojo { | 24 namespace mojo { |
| 26 | 25 |
| 27 namespace embedder { | 26 namespace embedder { |
| 28 class PlatformSupport; | 27 class PlatformSupport; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 // TODO(vtl): Consider if this is really necessary, since it only has one use | 63 // TODO(vtl): Consider if this is really necessary, since it only has one use |
| 65 // (in tests). | 64 // (in tests). |
| 66 void Shutdown(const base::Closure& callback, | 65 void Shutdown(const base::Closure& callback, |
| 67 embedder::PlatformTaskRunnerRefPtr callback_thread_task_runner); | 66 embedder::PlatformTaskRunnerRefPtr callback_thread_task_runner); |
| 68 | 67 |
| 69 // Creates a |Channel| and adds it to the set of channels managed by this | 68 // Creates a |Channel| and adds it to the set of channels managed by this |
| 70 // |ChannelManager|. This must be called from the I/O thread (given to the | 69 // |ChannelManager|. This must be called from the I/O thread (given to the |
| 71 // constructor). |channel_id| should be a valid |ChannelId| (i.e., nonzero) | 70 // constructor). |channel_id| should be a valid |ChannelId| (i.e., nonzero) |
| 72 // not "assigned" to any other |Channel| being managed by this | 71 // not "assigned" to any other |Channel| being managed by this |
| 73 // |ChannelManager|. | 72 // |ChannelManager|. |
| 74 RefPtr<MessagePipeDispatcher> CreateChannelOnIOThread( | 73 util::RefPtr<MessagePipeDispatcher> CreateChannelOnIOThread( |
| 75 ChannelId channel_id, | 74 ChannelId channel_id, |
| 76 embedder::ScopedPlatformHandle platform_handle); | 75 embedder::ScopedPlatformHandle platform_handle); |
| 77 | 76 |
| 78 // Like |CreateChannelOnIOThread()|, but doesn't create a bootstrap message | 77 // Like |CreateChannelOnIOThread()|, but doesn't create a bootstrap message |
| 79 // pipe. Returns the newly-created |Channel|. | 78 // pipe. Returns the newly-created |Channel|. |
| 80 // TODO(vtl): Maybe get rid of the others (and bootstrap message pipes in | 79 // TODO(vtl): Maybe get rid of the others (and bootstrap message pipes in |
| 81 // general). | 80 // general). |
| 82 RefPtr<Channel> CreateChannelWithoutBootstrapOnIOThread( | 81 util::RefPtr<Channel> CreateChannelWithoutBootstrapOnIOThread( |
| 83 ChannelId channel_id, | 82 ChannelId channel_id, |
| 84 embedder::ScopedPlatformHandle platform_handle); | 83 embedder::ScopedPlatformHandle platform_handle); |
| 85 | 84 |
| 86 // Like |CreateChannelOnIOThread()|, but may be called from any thread. On | 85 // Like |CreateChannelOnIOThread()|, but may be called from any thread. On |
| 87 // completion, will call |callback| (using |callback_thread_task_runner| if it | 86 // completion, will call |callback| (using |callback_thread_task_runner| if it |
| 88 // is non-null, else on the I/O thread). Note: This will always post a task to | 87 // is non-null, else on the I/O thread). Note: This will always post a task to |
| 89 // the I/O thread, even if called from that thread. | 88 // the I/O thread, even if called from that thread. |
| 90 RefPtr<MessagePipeDispatcher> CreateChannel( | 89 util::RefPtr<MessagePipeDispatcher> CreateChannel( |
| 91 ChannelId channel_id, | 90 ChannelId channel_id, |
| 92 embedder::ScopedPlatformHandle platform_handle, | 91 embedder::ScopedPlatformHandle platform_handle, |
| 93 const base::Closure& callback, | 92 const base::Closure& callback, |
| 94 embedder::PlatformTaskRunnerRefPtr callback_thread_task_runner); | 93 embedder::PlatformTaskRunnerRefPtr callback_thread_task_runner); |
| 95 | 94 |
| 96 // Gets the |Channel| with the given ID (which must exist). | 95 // Gets the |Channel| with the given ID (which must exist). |
| 97 RefPtr<Channel> GetChannel(ChannelId channel_id) const; | 96 util::RefPtr<Channel> GetChannel(ChannelId channel_id) const; |
| 98 | 97 |
| 99 // Informs the channel manager (and thus channel) that it will be shutdown | 98 // Informs the channel manager (and thus channel) that it will be shutdown |
| 100 // soon (by calling |ShutdownChannel()|). Calling this is optional (and may in | 99 // soon (by calling |ShutdownChannel()|). Calling this is optional (and may in |
| 101 // fact be called multiple times) but it will suppress certain warnings (e.g., | 100 // fact be called multiple times) but it will suppress certain warnings (e.g., |
| 102 // for the channel being broken) and enable others (if messages are written to | 101 // for the channel being broken) and enable others (if messages are written to |
| 103 // the channel). | 102 // the channel). |
| 104 void WillShutdownChannel(ChannelId channel_id); | 103 void WillShutdownChannel(ChannelId channel_id); |
| 105 | 104 |
| 106 // Shuts down the channel specified by the given ID. This, or | 105 // Shuts down the channel specified by the given ID. This, or |
| 107 // |ShutdownChannel()|, should be called once per channel (created using | 106 // |ShutdownChannel()|, should be called once per channel (created using |
| (...skipping 14 matching lines...) Expand all Loading... |
| 122 | 121 |
| 123 private: | 122 private: |
| 124 // Used by |Shutdown()|. Called on the I/O thread. | 123 // Used by |Shutdown()|. Called on the I/O thread. |
| 125 void ShutdownHelper( | 124 void ShutdownHelper( |
| 126 const base::Closure& callback, | 125 const base::Closure& callback, |
| 127 embedder::PlatformTaskRunnerRefPtr callback_thread_task_runner); | 126 embedder::PlatformTaskRunnerRefPtr callback_thread_task_runner); |
| 128 | 127 |
| 129 // Used by |CreateChannelOnIOThread()| and |CreateChannelHelper()|. Called on | 128 // Used by |CreateChannelOnIOThread()| and |CreateChannelHelper()|. Called on |
| 130 // the I/O thread. |bootstrap_channel_endpoint| is optional and may be null. | 129 // the I/O thread. |bootstrap_channel_endpoint| is optional and may be null. |
| 131 // Returns the newly-created |Channel|. | 130 // Returns the newly-created |Channel|. |
| 132 RefPtr<Channel> CreateChannelOnIOThreadHelper( | 131 util::RefPtr<Channel> CreateChannelOnIOThreadHelper( |
| 133 ChannelId channel_id, | 132 ChannelId channel_id, |
| 134 embedder::ScopedPlatformHandle platform_handle, | 133 embedder::ScopedPlatformHandle platform_handle, |
| 135 RefPtr<ChannelEndpoint>&& bootstrap_channel_endpoint); | 134 util::RefPtr<ChannelEndpoint>&& bootstrap_channel_endpoint); |
| 136 | 135 |
| 137 // Used by |CreateChannel()|. Called on the I/O thread. | 136 // Used by |CreateChannel()|. Called on the I/O thread. |
| 138 // TODO(vtl): |bootstrap_channel_endpoint| should be an rvalue reference, but | 137 // TODO(vtl): |bootstrap_channel_endpoint| should be an rvalue reference, but |
| 139 // that doesn't currently work correctly with base::Bind. | 138 // that doesn't currently work correctly with base::Bind. |
| 140 void CreateChannelHelper( | 139 void CreateChannelHelper( |
| 141 ChannelId channel_id, | 140 ChannelId channel_id, |
| 142 embedder::ScopedPlatformHandle platform_handle, | 141 embedder::ScopedPlatformHandle platform_handle, |
| 143 RefPtr<ChannelEndpoint> bootstrap_channel_endpoint, | 142 util::RefPtr<ChannelEndpoint> bootstrap_channel_endpoint, |
| 144 const base::Closure& callback, | 143 const base::Closure& callback, |
| 145 embedder::PlatformTaskRunnerRefPtr callback_thread_task_runner); | 144 embedder::PlatformTaskRunnerRefPtr callback_thread_task_runner); |
| 146 | 145 |
| 147 // Note: These must not be used after shutdown. | 146 // Note: These must not be used after shutdown. |
| 148 embedder::PlatformSupport* const platform_support_; | 147 embedder::PlatformSupport* const platform_support_; |
| 149 const embedder::PlatformTaskRunnerRefPtr io_thread_task_runner_; | 148 const embedder::PlatformTaskRunnerRefPtr io_thread_task_runner_; |
| 150 ConnectionManager* const connection_manager_; | 149 ConnectionManager* const connection_manager_; |
| 151 | 150 |
| 152 // Note: |Channel| methods should not be called under |mutex_|. | 151 // Note: |Channel| methods should not be called under |mutex_|. |
| 153 // TODO(vtl): Annotate the above rule using |MOJO_ACQUIRED_{BEFORE,AFTER}()|, | 152 // TODO(vtl): Annotate the above rule using |MOJO_ACQUIRED_{BEFORE,AFTER}()|, |
| 154 // once clang actually checks such annotations. | 153 // once clang actually checks such annotations. |
| 155 // https://github.com/domokit/mojo/issues/313 | 154 // https://github.com/domokit/mojo/issues/313 |
| 156 mutable Mutex mutex_; | 155 mutable Mutex mutex_; |
| 157 | 156 |
| 158 using ChannelIdToChannelMap = std::unordered_map<ChannelId, RefPtr<Channel>>; | 157 using ChannelIdToChannelMap = |
| 158 std::unordered_map<ChannelId, util::RefPtr<Channel>>; |
| 159 ChannelIdToChannelMap channels_ MOJO_GUARDED_BY(mutex_); | 159 ChannelIdToChannelMap channels_ MOJO_GUARDED_BY(mutex_); |
| 160 | 160 |
| 161 MOJO_DISALLOW_COPY_AND_ASSIGN(ChannelManager); | 161 MOJO_DISALLOW_COPY_AND_ASSIGN(ChannelManager); |
| 162 }; | 162 }; |
| 163 | 163 |
| 164 } // namespace system | 164 } // namespace system |
| 165 } // namespace mojo | 165 } // namespace mojo |
| 166 | 166 |
| 167 #endif // MOJO_EDK_SYSTEM_CHANNEL_MANAGER_H_ | 167 #endif // MOJO_EDK_SYSTEM_CHANNEL_MANAGER_H_ |
| OLD | NEW |