[email protected] | 0a6fc4b | 2012-04-05 02:38:34 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 7412204 | 2014-04-25 00:07:30 | [diff] [blame] | 5 | #include "ipc/ipc_channel_proxy.h" |
| 6 | |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 9 | |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 10 | #include <utility> |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 11 | |
[email protected] | 72b6f8e2 | 2011-11-12 21:16:41 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | b5322508 | 2011-11-29 02:24:28 | [diff] [blame] | 13 | #include "base/compiler_specific.h" |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 14 | #include "base/location.h" |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 15 | #include "base/memory/ptr_util.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 16 | #include "base/memory/ref_counted.h" |
[email protected] | b243230 | 2012-07-02 21:15:52 | [diff] [blame] | 17 | #include "base/single_thread_task_runner.h" |
gab | f08ccc0 | 2016-05-11 18:51:11 | [diff] [blame] | 18 | #include "base/threading/thread_task_runner_handle.h" |
avi | 246998d8 | 2015-12-22 02:39:04 | [diff] [blame] | 19 | #include "build/build_config.h" |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 20 | #include "ipc/ipc_channel_factory.h" |
[email protected] | 57319ce | 2012-06-11 22:35:26 | [diff] [blame] | 21 | #include "ipc/ipc_listener.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 22 | #include "ipc/ipc_logging.h" |
[email protected] | 9ec1685 | 2012-05-11 23:24:37 | [diff] [blame] | 23 | #include "ipc/ipc_message_macros.h" |
[email protected] | 7412204 | 2014-04-25 00:07:30 | [diff] [blame] | 24 | #include "ipc/message_filter.h" |
| 25 | #include "ipc/message_filter_router.h" |
Julie Jeongeun Kim | 69604f6 | 2019-12-04 01:59:52 | [diff] [blame] | 26 | #include "mojo/public/cpp/bindings/pending_associated_receiver.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 27 | |
| 28 | namespace IPC { |
| 29 | |
[email protected] | 16395142 | 2009-07-28 22:09:45 | [diff] [blame] | 30 | //------------------------------------------------------------------------------ |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 31 | |
dcheng | fd03370 | 2014-08-28 16:59:29 | [diff] [blame] | 32 | ChannelProxy::Context::Context( |
| 33 | Listener* listener, |
Hajime Hoshi | ff15e97 | 2017-11-09 06:37:09 | [diff] [blame] | 34 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
| 35 | const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner) |
Yuzu Saijo | 8866b1d | 2019-05-24 03:23:28 | [diff] [blame] | 36 | : default_listener_task_runner_(listener_task_runner), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | listener_(listener), |
[email protected] | b243230 | 2012-07-02 21:15:52 | [diff] [blame] | 38 | ipc_task_runner_(ipc_task_runner), |
[email protected] | 0a6fc4b | 2012-04-05 02:38:34 | [diff] [blame] | 39 | channel_connected_called_(false), |
[email protected] | b51352a | 2014-02-26 05:18:04 | [diff] [blame] | 40 | message_filter_router_(new MessageFilterRouter()), |
sammc | f810f07f | 2016-11-10 22:34:07 | [diff] [blame] | 41 | peer_pid_(base::kNullProcessId) { |
[email protected] | 1757164 | 2013-06-01 04:11:27 | [diff] [blame] | 42 | DCHECK(ipc_task_runner_.get()); |
[email protected] | 1f9cf47 | 2014-04-17 05:07:18 | [diff] [blame] | 43 | // The Listener thread where Messages are handled must be a separate thread |
| 44 | // to avoid oversubscribing the IO thread. If you trigger this error, you |
| 45 | // need to either: |
| 46 | // 1) Create the ChannelProxy on a different thread, or |
| 47 | // 2) Just use Channel |
| 48 | // Note, we currently make an exception for a NULL listener. That usage |
| 49 | // basically works, but is outside the intent of ChannelProxy. This support |
| 50 | // will disappear, so please don't rely on it. See crbug.com/364241 |
Yuzu Saijo | 8866b1d | 2019-05-24 03:23:28 | [diff] [blame] | 51 | DCHECK(!listener || |
| 52 | (ipc_task_runner_.get() != default_listener_task_runner_.get())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 53 | } |
| 54 | |
Chris Watkins | 2d879af | 2017-11-30 02:11:59 | [diff] [blame] | 55 | ChannelProxy::Context::~Context() = default; |
[email protected] | 92bf906 | 2011-05-02 18:00:49 | [diff] [blame] | 56 | |
[email protected] | b243230 | 2012-07-02 21:15:52 | [diff] [blame] | 57 | void ChannelProxy::Context::ClearIPCTaskRunner() { |
kylechar | 07bacf5 | 2019-11-15 15:37:14 | [diff] [blame] | 58 | ipc_task_runner_.reset(); |
[email protected] | b243230 | 2012-07-02 21:15:52 | [diff] [blame] | 59 | } |
| 60 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 61 | void ChannelProxy::Context::CreateChannel( |
| 62 | std::unique_ptr<ChannelFactory> factory) { |
morrita | b447214 | 2015-04-20 21:20:12 | [diff] [blame] | 63 | base::AutoLock l(channel_lifetime_lock_); |
[email protected] | 7e3d752 | 2014-03-20 21:00:50 | [diff] [blame] | 64 | DCHECK(!channel_); |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 65 | DCHECK_EQ(factory->GetIPCTaskRunner(), ipc_task_runner_); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 66 | channel_ = factory->BuildChannel(this); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 67 | |
| 68 | Channel::AssociatedInterfaceSupport* support = |
| 69 | channel_->GetAssociatedInterfaceSupport(); |
| 70 | if (support) { |
rockot | a628d0b | 2017-02-09 08:40:15 | [diff] [blame] | 71 | thread_safe_channel_ = support->CreateThreadSafeChannel(); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 72 | |
| 73 | base::AutoLock l(pending_filters_lock_); |
rockot | 70bbb5949 | 2017-01-25 00:56:51 | [diff] [blame] | 74 | for (auto& entry : pending_io_thread_interfaces_) |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 75 | support->AddGenericAssociatedInterface(entry.first, entry.second); |
rockot | 70bbb5949 | 2017-01-25 00:56:51 | [diff] [blame] | 76 | pending_io_thread_interfaces_.clear(); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 77 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 78 | } |
| 79 | |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 80 | bool ChannelProxy::Context::TryFilters(const Message& message) { |
[email protected] | b51352a | 2014-02-26 05:18:04 | [diff] [blame] | 81 | DCHECK(message_filter_router_); |
davidsz | 041528a | 2017-05-12 09:19:23 | [diff] [blame] | 82 | #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED) |
[email protected] | 8e8bb6d | 2010-12-13 08:18:55 | [diff] [blame] | 83 | Logging* logger = Logging::GetInstance(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 84 | if (logger->Enabled()) |
| 85 | logger->OnPreDispatchMessage(message); |
| 86 | #endif |
| 87 | |
[email protected] | b51352a | 2014-02-26 05:18:04 | [diff] [blame] | 88 | if (message_filter_router_->TryFilters(message)) { |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 89 | if (message.dispatch_error()) { |
Yuzu Saijo | 8866b1d | 2019-05-24 03:23:28 | [diff] [blame] | 90 | GetTaskRunner(message.routing_id()) |
| 91 | ->PostTask(FROM_HERE, base::BindOnce(&Context::OnDispatchBadMessage, |
| 92 | this, message)); |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 93 | } |
davidsz | 041528a | 2017-05-12 09:19:23 | [diff] [blame] | 94 | #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED) |
[email protected] | b51352a | 2014-02-26 05:18:04 | [diff] [blame] | 95 | if (logger->Enabled()) |
sammc | a1b3b4d | 2016-11-15 00:34:36 | [diff] [blame] | 96 | logger->OnPostDispatchMessage(message); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 97 | #endif |
[email protected] | b51352a | 2014-02-26 05:18:04 | [diff] [blame] | 98 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 99 | } |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 100 | return false; |
| 101 | } |
| 102 | |
| 103 | // Called on the IPC::Channel thread |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 104 | void ChannelProxy::Context::PauseChannel() { |
| 105 | DCHECK(channel_); |
| 106 | channel_->Pause(); |
| 107 | } |
| 108 | |
| 109 | // Called on the IPC::Channel thread |
rockot | 401fb2c | 2016-09-06 18:35:57 | [diff] [blame] | 110 | void ChannelProxy::Context::UnpauseChannel(bool flush) { |
| 111 | DCHECK(channel_); |
| 112 | channel_->Unpause(flush); |
| 113 | } |
| 114 | |
| 115 | // Called on the IPC::Channel thread |
| 116 | void ChannelProxy::Context::FlushChannel() { |
| 117 | DCHECK(channel_); |
| 118 | channel_->Flush(); |
| 119 | } |
| 120 | |
| 121 | // Called on the IPC::Channel thread |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 122 | bool ChannelProxy::Context::OnMessageReceived(const Message& message) { |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 123 | // First give a chance to the filters to process this message. |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 124 | if (!TryFilters(message)) |
| 125 | OnMessageReceivedNoFilter(message); |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 126 | return true; |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 127 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 128 | |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 129 | // Called on the IPC::Channel thread |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 130 | bool ChannelProxy::Context::OnMessageReceivedNoFilter(const Message& message) { |
Yuzu Saijo | 8866b1d | 2019-05-24 03:23:28 | [diff] [blame] | 131 | GetTaskRunner(message.routing_id()) |
| 132 | ->PostTask(FROM_HERE, |
| 133 | base::BindOnce(&Context::OnDispatchMessage, this, message)); |
[email protected] | a95986a8 | 2010-12-24 06:19:28 | [diff] [blame] | 134 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // Called on the IPC::Channel thread |
tfarina | 10a5c06 | 2015-09-04 18:47:57 | [diff] [blame] | 138 | void ChannelProxy::Context::OnChannelConnected(int32_t peer_pid) { |
[email protected] | 7e3d752 | 2014-03-20 21:00:50 | [diff] [blame] | 139 | // We cache off the peer_pid so it can be safely accessed from both threads. |
amistry | 78ef5f1b | 2016-07-19 02:52:31 | [diff] [blame] | 140 | { |
| 141 | base::AutoLock l(peer_pid_lock_); |
sammc | f810f07f | 2016-11-10 22:34:07 | [diff] [blame] | 142 | peer_pid_ = peer_pid; |
amistry | 78ef5f1b | 2016-07-19 02:52:31 | [diff] [blame] | 143 | } |
[email protected] | 7e3d752 | 2014-03-20 21:00:50 | [diff] [blame] | 144 | |
[email protected] | 4b580bf | 2010-12-02 19:16:07 | [diff] [blame] | 145 | // Add any pending filters. This avoids a race condition where someone |
| 146 | // creates a ChannelProxy, calls AddFilter, and then right after starts the |
| 147 | // peer process. The IO thread could receive a message before the task to add |
| 148 | // the filter is run on the IO thread. |
| 149 | OnAddFilter(); |
| 150 | |
Yuzu Saijo | 8866b1d | 2019-05-24 03:23:28 | [diff] [blame] | 151 | // See above comment about using default_listener_task_runner_ here. |
| 152 | default_listener_task_runner_->PostTask( |
kylechar | f448cc9 | 2019-02-19 20:28:09 | [diff] [blame] | 153 | FROM_HERE, base::BindOnce(&Context::OnDispatchConnected, this)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // Called on the IPC::Channel thread |
| 157 | void ChannelProxy::Context::OnChannelError() { |
[email protected] | 5fa1c54 | 2009-05-05 20:36:07 | [diff] [blame] | 158 | for (size_t i = 0; i < filters_.size(); ++i) |
| 159 | filters_[i]->OnChannelError(); |
| 160 | |
Yuzu Saijo | 8866b1d | 2019-05-24 03:23:28 | [diff] [blame] | 161 | // See above comment about using default_listener_task_runner_ here. |
| 162 | default_listener_task_runner_->PostTask( |
kylechar | f448cc9 | 2019-02-19 20:28:09 | [diff] [blame] | 163 | FROM_HERE, base::BindOnce(&Context::OnDispatchError, this)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | // Called on the IPC::Channel thread |
rockot | f62002a | 2016-09-15 00:08:59 | [diff] [blame] | 167 | void ChannelProxy::Context::OnAssociatedInterfaceRequest( |
| 168 | const std::string& interface_name, |
| 169 | mojo::ScopedInterfaceEndpointHandle handle) { |
Yuzu Saijo | 8866b1d | 2019-05-24 03:23:28 | [diff] [blame] | 170 | default_listener_task_runner_->PostTask( |
kylechar | f448cc9 | 2019-02-19 20:28:09 | [diff] [blame] | 171 | FROM_HERE, base::BindOnce(&Context::OnDispatchAssociatedInterfaceRequest, |
| 172 | this, interface_name, std::move(handle))); |
rockot | f62002a | 2016-09-15 00:08:59 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // Called on the IPC::Channel thread |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 176 | void ChannelProxy::Context::OnChannelOpened() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 177 | DCHECK(channel_ != NULL); |
| 178 | |
| 179 | // Assume a reference to ourselves on behalf of this thread. This reference |
| 180 | // will be released when we are closed. |
| 181 | AddRef(); |
| 182 | |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 183 | if (!channel_->Connect()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 184 | OnChannelError(); |
| 185 | return; |
| 186 | } |
| 187 | |
| 188 | for (size_t i = 0; i < filters_.size(); ++i) |
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 189 | filters_[i]->OnFilterAdded(channel_.get()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | // Called on the IPC::Channel thread |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 193 | void ChannelProxy::Context::OnChannelClosed() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 194 | // It's okay for IPC::ChannelProxy::Close to be called more than once, which |
| 195 | // would result in this branch being taken. |
[email protected] | 7e3d752 | 2014-03-20 21:00:50 | [diff] [blame] | 196 | if (!channel_) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 197 | return; |
| 198 | |
tzik | 0e85f1d | 2016-11-05 09:26:44 | [diff] [blame] | 199 | for (auto& filter : pending_filters_) { |
| 200 | filter->OnChannelClosing(); |
| 201 | filter->OnFilterRemoved(); |
| 202 | } |
| 203 | for (auto& filter : filters_) { |
| 204 | filter->OnChannelClosing(); |
| 205 | filter->OnFilterRemoved(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | // We don't need the filters anymore. |
[email protected] | b51352a | 2014-02-26 05:18:04 | [diff] [blame] | 209 | message_filter_router_->Clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 210 | filters_.clear(); |
[email protected] | 7e3d752 | 2014-03-20 21:00:50 | [diff] [blame] | 211 | // We don't need the lock, because at this point, the listener thread can't |
| 212 | // access it any more. |
| 213 | pending_filters_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 214 | |
morrita | b447214 | 2015-04-20 21:20:12 | [diff] [blame] | 215 | ClearChannel(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 216 | |
| 217 | // Balance with the reference taken during startup. This may result in |
| 218 | // self-destruction. |
| 219 | Release(); |
| 220 | } |
| 221 | |
[email protected] | 772a5b2 | 2012-08-09 20:39:12 | [diff] [blame] | 222 | void ChannelProxy::Context::Clear() { |
| 223 | listener_ = NULL; |
| 224 | } |
| 225 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 226 | // Called on the IPC::Channel thread |
Etienne Bergeron | 0a07d73d | 2019-06-27 16:40:47 | [diff] [blame] | 227 | void ChannelProxy::Context::OnSendMessage(std::unique_ptr<Message> message) { |
Sigurdur Asgeirsson | d655dd65f | 2019-11-12 19:32:20 | [diff] [blame] | 228 | if (quota_checker_) |
| 229 | quota_checker_->AfterMessagesDequeued(1); |
| 230 | |
[email protected] | 7e3d752 | 2014-03-20 21:00:50 | [diff] [blame] | 231 | if (!channel_) { |
[email protected] | 87339f0 | 2010-09-02 21:45:50 | [diff] [blame] | 232 | OnChannelClosed(); |
| 233 | return; |
| 234 | } |
[email protected] | 7e3d752 | 2014-03-20 21:00:50 | [diff] [blame] | 235 | |
[email protected] | f729d15a | 2012-04-28 02:12:00 | [diff] [blame] | 236 | if (!channel_->Send(message.release())) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 237 | OnChannelError(); |
| 238 | } |
| 239 | |
| 240 | // Called on the IPC::Channel thread |
[email protected] | 4b580bf | 2010-12-02 19:16:07 | [diff] [blame] | 241 | void ChannelProxy::Context::OnAddFilter() { |
[email protected] | 7e3d752 | 2014-03-20 21:00:50 | [diff] [blame] | 242 | // Our OnChannelConnected method has not yet been called, so we can't be |
| 243 | // sure that channel_ is valid yet. When OnChannelConnected *is* called, |
| 244 | // it invokes OnAddFilter, so any pending filter(s) will be added at that |
| 245 | // time. |
amistry | 78ef5f1b | 2016-07-19 02:52:31 | [diff] [blame] | 246 | // No lock necessary for |peer_pid_| because it is only modified on this |
| 247 | // thread. |
[email protected] | 7e3d752 | 2014-03-20 21:00:50 | [diff] [blame] | 248 | if (peer_pid_ == base::kNullProcessId) |
| 249 | return; |
| 250 | |
[email protected] | 41388930 | 2011-08-16 22:12:39 | [diff] [blame] | 251 | std::vector<scoped_refptr<MessageFilter> > new_filters; |
[email protected] | 4b580bf | 2010-12-02 19:16:07 | [diff] [blame] | 252 | { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 253 | base::AutoLock auto_lock(pending_filters_lock_); |
[email protected] | 41388930 | 2011-08-16 22:12:39 | [diff] [blame] | 254 | new_filters.swap(pending_filters_); |
[email protected] | 4b580bf | 2010-12-02 19:16:07 | [diff] [blame] | 255 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 256 | |
[email protected] | 41388930 | 2011-08-16 22:12:39 | [diff] [blame] | 257 | for (size_t i = 0; i < new_filters.size(); ++i) { |
| 258 | filters_.push_back(new_filters[i]); |
[email protected] | 4b580bf | 2010-12-02 19:16:07 | [diff] [blame] | 259 | |
[email protected] | b51352a | 2014-02-26 05:18:04 | [diff] [blame] | 260 | message_filter_router_->AddFilter(new_filters[i].get()); |
| 261 | |
[email protected] | 7e3d752 | 2014-03-20 21:00:50 | [diff] [blame] | 262 | // The channel has already been created and connected, so we need to |
| 263 | // inform the filters right now. |
| 264 | new_filters[i]->OnFilterAdded(channel_.get()); |
| 265 | new_filters[i]->OnChannelConnected(peer_pid_); |
[email protected] | 4b580bf | 2010-12-02 19:16:07 | [diff] [blame] | 266 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | // Called on the IPC::Channel thread |
| 270 | void ChannelProxy::Context::OnRemoveFilter(MessageFilter* filter) { |
amistry | 78ef5f1b | 2016-07-19 02:52:31 | [diff] [blame] | 271 | // No lock necessary for |peer_pid_| because it is only modified on this |
| 272 | // thread. |
[email protected] | 7e3d752 | 2014-03-20 21:00:50 | [diff] [blame] | 273 | if (peer_pid_ == base::kNullProcessId) { |
| 274 | // The channel is not yet connected, so any filters are still pending. |
| 275 | base::AutoLock auto_lock(pending_filters_lock_); |
| 276 | for (size_t i = 0; i < pending_filters_.size(); ++i) { |
| 277 | if (pending_filters_[i].get() == filter) { |
| 278 | filter->OnFilterRemoved(); |
| 279 | pending_filters_.erase(pending_filters_.begin() + i); |
| 280 | return; |
| 281 | } |
| 282 | } |
| 283 | return; |
| 284 | } |
| 285 | if (!channel_) |
[email protected] | 6d057a0c | 2013-07-09 21:12:07 | [diff] [blame] | 286 | return; // The filters have already been deleted. |
| 287 | |
[email protected] | b51352a | 2014-02-26 05:18:04 | [diff] [blame] | 288 | message_filter_router_->RemoveFilter(filter); |
| 289 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 290 | for (size_t i = 0; i < filters_.size(); ++i) { |
| 291 | if (filters_[i].get() == filter) { |
| 292 | filter->OnFilterRemoved(); |
| 293 | filters_.erase(filters_.begin() + i); |
| 294 | return; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | NOTREACHED() << "filter to be removed not found"; |
| 299 | } |
| 300 | |
| 301 | // Called on the listener's thread |
[email protected] | 4b580bf | 2010-12-02 19:16:07 | [diff] [blame] | 302 | void ChannelProxy::Context::AddFilter(MessageFilter* filter) { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 303 | base::AutoLock auto_lock(pending_filters_lock_); |
kylechar | da69d88 | 2017-10-04 05:49:52 | [diff] [blame] | 304 | pending_filters_.push_back(base::WrapRefCounted(filter)); |
kylechar | f448cc9 | 2019-02-19 20:28:09 | [diff] [blame] | 305 | ipc_task_runner_->PostTask(FROM_HERE, |
| 306 | base::BindOnce(&Context::OnAddFilter, this)); |
[email protected] | 4b580bf | 2010-12-02 19:16:07 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | // Called on the listener's thread |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 310 | void ChannelProxy::Context::OnDispatchMessage(const Message& message) { |
| 311 | if (!listener_) |
| 312 | return; |
| 313 | |
[email protected] | 827ab81 | 2009-03-12 07:17:17 | [diff] [blame] | 314 | OnDispatchConnected(); |
| 315 | |
davidsz | 041528a | 2017-05-12 09:19:23 | [diff] [blame] | 316 | #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED) |
ssid | 6c1dc89 | 2016-03-28 19:56:49 | [diff] [blame] | 317 | Logging* logger = Logging::GetInstance(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 318 | if (message.type() == IPC_LOGGING_ID) { |
| 319 | logger->OnReceivedLoggingMessage(message); |
| 320 | return; |
| 321 | } |
| 322 | |
| 323 | if (logger->Enabled()) |
| 324 | logger->OnPreDispatchMessage(message); |
| 325 | #endif |
| 326 | |
| 327 | listener_->OnMessageReceived(message); |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 328 | if (message.dispatch_error()) |
| 329 | listener_->OnBadMessageReceived(message); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 330 | |
davidsz | 041528a | 2017-05-12 09:19:23 | [diff] [blame] | 331 | #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 332 | if (logger->Enabled()) |
sammc | a1b3b4d | 2016-11-15 00:34:36 | [diff] [blame] | 333 | logger->OnPostDispatchMessage(message); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 334 | #endif |
| 335 | } |
| 336 | |
Yuzu Saijo | 8866b1d | 2019-05-24 03:23:28 | [diff] [blame] | 337 | // Called on the listener's thread. |
| 338 | void ChannelProxy::Context::AddListenerTaskRunner( |
| 339 | int32_t routing_id, |
| 340 | scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| 341 | DCHECK(default_listener_task_runner_->BelongsToCurrentThread()); |
| 342 | DCHECK(task_runner); |
| 343 | base::AutoLock lock(listener_thread_task_runners_lock_); |
Jan Wilken Dörrie | 0cab5a5ab | 2019-06-07 09:11:31 | [diff] [blame] | 344 | if (!base::Contains(listener_thread_task_runners_, routing_id)) |
Yuzu Saijo | 8866b1d | 2019-05-24 03:23:28 | [diff] [blame] | 345 | listener_thread_task_runners_.insert({routing_id, std::move(task_runner)}); |
| 346 | } |
| 347 | |
| 348 | // Called on the listener's thread. |
| 349 | void ChannelProxy::Context::RemoveListenerTaskRunner(int32_t routing_id) { |
| 350 | DCHECK(default_listener_task_runner_->BelongsToCurrentThread()); |
| 351 | base::AutoLock lock(listener_thread_task_runners_lock_); |
Yuzu Saijo | 3b633001 | 2019-07-11 03:51:27 | [diff] [blame] | 352 | listener_thread_task_runners_.erase(routing_id); |
Yuzu Saijo | 8866b1d | 2019-05-24 03:23:28 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | // Called on the IPC::Channel thread. |
Yuzu Saijo | 3b633001 | 2019-07-11 03:51:27 | [diff] [blame] | 356 | scoped_refptr<base::SingleThreadTaskRunner> |
| 357 | ChannelProxy::Context::GetTaskRunner(int32_t routing_id) { |
Yuzu Saijo | 8866b1d | 2019-05-24 03:23:28 | [diff] [blame] | 358 | DCHECK(ipc_task_runner_->BelongsToCurrentThread()); |
| 359 | if (routing_id == MSG_ROUTING_NONE) |
Yuzu Saijo | 3b633001 | 2019-07-11 03:51:27 | [diff] [blame] | 360 | return default_listener_task_runner_; |
Yuzu Saijo | 8866b1d | 2019-05-24 03:23:28 | [diff] [blame] | 361 | |
| 362 | base::AutoLock lock(listener_thread_task_runners_lock_); |
Yuzu Saijo | 3b633001 | 2019-07-11 03:51:27 | [diff] [blame] | 363 | auto task_runner = listener_thread_task_runners_.find(routing_id); |
| 364 | if (task_runner == listener_thread_task_runners_.end()) |
| 365 | return default_listener_task_runner_; |
| 366 | DCHECK(task_runner->second); |
| 367 | return task_runner->second; |
Yuzu Saijo | 8866b1d | 2019-05-24 03:23:28 | [diff] [blame] | 368 | } |
| 369 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 370 | // Called on the listener's thread |
[email protected] | 827ab81 | 2009-03-12 07:17:17 | [diff] [blame] | 371 | void ChannelProxy::Context::OnDispatchConnected() { |
| 372 | if (channel_connected_called_) |
| 373 | return; |
| 374 | |
amistry | 78ef5f1b | 2016-07-19 02:52:31 | [diff] [blame] | 375 | base::ProcessId peer_pid; |
| 376 | { |
| 377 | base::AutoLock l(peer_pid_lock_); |
| 378 | peer_pid = peer_pid_; |
| 379 | } |
[email protected] | 827ab81 | 2009-03-12 07:17:17 | [diff] [blame] | 380 | channel_connected_called_ = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 381 | if (listener_) |
amistry | 78ef5f1b | 2016-07-19 02:52:31 | [diff] [blame] | 382 | listener_->OnChannelConnected(peer_pid); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | // Called on the listener's thread |
| 386 | void ChannelProxy::Context::OnDispatchError() { |
| 387 | if (listener_) |
| 388 | listener_->OnChannelError(); |
| 389 | } |
| 390 | |
[email protected] | ef2f6ba | 2014-05-15 23:06:07 | [diff] [blame] | 391 | // Called on the listener's thread |
| 392 | void ChannelProxy::Context::OnDispatchBadMessage(const Message& message) { |
| 393 | if (listener_) |
| 394 | listener_->OnBadMessageReceived(message); |
| 395 | } |
| 396 | |
rockot | f62002a | 2016-09-15 00:08:59 | [diff] [blame] | 397 | // Called on the listener's thread |
| 398 | void ChannelProxy::Context::OnDispatchAssociatedInterfaceRequest( |
| 399 | const std::string& interface_name, |
| 400 | mojo::ScopedInterfaceEndpointHandle handle) { |
| 401 | if (listener_) |
| 402 | listener_->OnAssociatedInterfaceRequest(interface_name, std::move(handle)); |
| 403 | } |
| 404 | |
morrita | b447214 | 2015-04-20 21:20:12 | [diff] [blame] | 405 | void ChannelProxy::Context::ClearChannel() { |
| 406 | base::AutoLock l(channel_lifetime_lock_); |
| 407 | channel_.reset(); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 408 | } |
| 409 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 410 | void ChannelProxy::Context::AddGenericAssociatedInterfaceForIOThread( |
| 411 | const std::string& name, |
| 412 | const GenericAssociatedInterfaceFactory& factory) { |
| 413 | base::AutoLock l(channel_lifetime_lock_); |
| 414 | if (!channel_) { |
| 415 | base::AutoLock l(pending_filters_lock_); |
rockot | 70bbb5949 | 2017-01-25 00:56:51 | [diff] [blame] | 416 | pending_io_thread_interfaces_.emplace_back(name, factory); |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 417 | return; |
| 418 | } |
| 419 | Channel::AssociatedInterfaceSupport* support = |
| 420 | channel_->GetAssociatedInterfaceSupport(); |
rockot | fd3d9084 | 2016-09-15 23:57:29 | [diff] [blame] | 421 | if (support) |
| 422 | support->AddGenericAssociatedInterface(name, factory); |
morrita | b447214 | 2015-04-20 21:20:12 | [diff] [blame] | 423 | } |
| 424 | |
Etienne Bergeron | 0a07d73d | 2019-06-27 16:40:47 | [diff] [blame] | 425 | void ChannelProxy::Context::Send(Message* message) { |
Sigurdur Asgeirsson | d655dd65f | 2019-11-12 19:32:20 | [diff] [blame] | 426 | if (quota_checker_) |
| 427 | quota_checker_->BeforeMessagesEnqueued(1); |
| 428 | |
morrita | b447214 | 2015-04-20 21:20:12 | [diff] [blame] | 429 | ipc_task_runner()->PostTask( |
kylechar | f448cc9 | 2019-02-19 20:28:09 | [diff] [blame] | 430 | FROM_HERE, base::BindOnce(&ChannelProxy::Context::OnSendMessage, this, |
Etienne Bergeron | 0a07d73d | 2019-06-27 16:40:47 | [diff] [blame] | 431 | base::WrapUnique(message))); |
morrita | b447214 | 2015-04-20 21:20:12 | [diff] [blame] | 432 | } |
| 433 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 434 | //----------------------------------------------------------------------------- |
| 435 | |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 436 | // static |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 437 | std::unique_ptr<ChannelProxy> ChannelProxy::Create( |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 438 | const IPC::ChannelHandle& channel_handle, |
| 439 | Channel::Mode mode, |
| 440 | Listener* listener, |
Hajime Hoshi | ff15e97 | 2017-11-09 06:37:09 | [diff] [blame] | 441 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
| 442 | const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner) { |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 443 | std::unique_ptr<ChannelProxy> channel( |
Hajime Hoshi | ff15e97 | 2017-11-09 06:37:09 | [diff] [blame] | 444 | new ChannelProxy(listener, ipc_task_runner, listener_task_runner)); |
erikchen | 30dc281 | 2015-09-24 03:26:38 | [diff] [blame] | 445 | channel->Init(channel_handle, mode, true); |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 446 | return channel; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 447 | } |
| 448 | |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 449 | // static |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 450 | std::unique_ptr<ChannelProxy> ChannelProxy::Create( |
| 451 | std::unique_ptr<ChannelFactory> factory, |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 452 | Listener* listener, |
Hajime Hoshi | ff15e97 | 2017-11-09 06:37:09 | [diff] [blame] | 453 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
| 454 | const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner) { |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 455 | std::unique_ptr<ChannelProxy> channel( |
Hajime Hoshi | ff15e97 | 2017-11-09 06:37:09 | [diff] [blame] | 456 | new ChannelProxy(listener, ipc_task_runner, listener_task_runner)); |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 457 | channel->Init(std::move(factory), true); |
| 458 | return channel; |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 459 | } |
| 460 | |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 461 | ChannelProxy::ChannelProxy(Context* context) |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 462 | : context_(context), did_init_(false) { |
mbarbella | e97877bc | 2015-03-06 21:51:14 | [diff] [blame] | 463 | #if defined(ENABLE_IPC_FUZZER) |
| 464 | outgoing_message_filter_ = NULL; |
| 465 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 466 | } |
| 467 | |
dcheng | fd03370 | 2014-08-28 16:59:29 | [diff] [blame] | 468 | ChannelProxy::ChannelProxy( |
| 469 | Listener* listener, |
Hajime Hoshi | ff15e97 | 2017-11-09 06:37:09 | [diff] [blame] | 470 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
| 471 | const scoped_refptr<base::SingleThreadTaskRunner>& listener_task_runner) |
| 472 | : context_(new Context(listener, ipc_task_runner, listener_task_runner)), |
| 473 | did_init_(false) { |
rockot | f14a8ae | 2016-06-16 19:28:41 | [diff] [blame] | 474 | #if defined(ENABLE_IPC_FUZZER) |
| 475 | outgoing_message_filter_ = NULL; |
| 476 | #endif |
| 477 | } |
[email protected] | fca876a1 | 2014-06-05 16:15:38 | [diff] [blame] | 478 | |
[email protected] | dec76e80 | 2010-09-23 22:43:53 | [diff] [blame] | 479 | ChannelProxy::~ChannelProxy() { |
gab | 21edbfa | 2017-05-31 16:49:12 | [diff] [blame] | 480 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | f775d10 | 2012-12-05 01:00:36 | [diff] [blame] | 481 | |
[email protected] | dec76e80 | 2010-09-23 22:43:53 | [diff] [blame] | 482 | Close(); |
| 483 | } |
| 484 | |
[email protected] | 3b0e466 | 2014-06-02 20:29:30 | [diff] [blame] | 485 | void ChannelProxy::Init(const IPC::ChannelHandle& channel_handle, |
| 486 | Channel::Mode mode, |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 487 | bool create_pipe_now) { |
Fabrice de Gans-Riberi | 894661c | 2018-05-24 18:43:22 | [diff] [blame] | 488 | #if defined(OS_POSIX) || defined(OS_FUCHSIA) |
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 489 | // When we are creating a server on POSIX, we need its file descriptor |
| 490 | // to be created immediately so that it can be accessed and passed |
| 491 | // to other processes. Forcing it to be created immediately avoids |
| 492 | // race conditions that may otherwise arise. |
[email protected] | 1707726c | 2011-02-03 20:35:09 | [diff] [blame] | 493 | if (mode & Channel::MODE_SERVER_FLAG) { |
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 494 | create_pipe_now = true; |
| 495 | } |
Fabrice de Gans-Riberi | 894661c | 2018-05-24 18:43:22 | [diff] [blame] | 496 | #endif // defined(OS_POSIX) || defined(OS_FUCHSIA) |
rockot | a34707ca | 2016-07-20 04:28:32 | [diff] [blame] | 497 | Init( |
| 498 | ChannelFactory::Create(channel_handle, mode, context_->ipc_task_runner()), |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 499 | create_pipe_now); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 500 | } |
| 501 | |
danakj | 03de39b2 | 2016-04-23 04:21:09 | [diff] [blame] | 502 | void ChannelProxy::Init(std::unique_ptr<ChannelFactory> factory, |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 503 | bool create_pipe_now) { |
gab | 21edbfa | 2017-05-31 16:49:12 | [diff] [blame] | 504 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | 6486088 | 2014-08-04 23:44:17 | [diff] [blame] | 505 | DCHECK(!did_init_); |
[email protected] | 42ce94e | 2010-12-08 19:28:09 | [diff] [blame] | 506 | |
Sigurdur Asgeirsson | d655dd65f | 2019-11-12 19:32:20 | [diff] [blame] | 507 | DCHECK(!context_->quota_checker_); |
| 508 | context_->quota_checker_ = factory->GetQuotaChecker(); |
| 509 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 510 | if (create_pipe_now) { |
| 511 | // Create the channel immediately. This effectively sets up the |
| 512 | // low-level pipe so that the client can connect. Without creating |
| 513 | // the pipe immediately, it is possible for a listener to attempt |
| 514 | // to connect and get an error since the pipe doesn't exist yet. |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 515 | context_->CreateChannel(std::move(factory)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 516 | } else { |
[email protected] | b243230 | 2012-07-02 21:15:52 | [diff] [blame] | 517 | context_->ipc_task_runner()->PostTask( |
kylechar | f448cc9 | 2019-02-19 20:28:09 | [diff] [blame] | 518 | FROM_HERE, |
| 519 | base::BindOnce(&Context::CreateChannel, context_, std::move(factory))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | // complete initialization on the background thread |
[email protected] | b243230 | 2012-07-02 21:15:52 | [diff] [blame] | 523 | context_->ipc_task_runner()->PostTask( |
kylechar | f448cc9 | 2019-02-19 20:28:09 | [diff] [blame] | 524 | FROM_HERE, base::BindOnce(&Context::OnChannelOpened, context_)); |
[email protected] | 952394af | 2011-11-16 01:06:46 | [diff] [blame] | 525 | |
| 526 | did_init_ = true; |
rockot | 29ade1b | 2015-08-07 06:23:59 | [diff] [blame] | 527 | OnChannelInit(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 528 | } |
| 529 | |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 530 | void ChannelProxy::Pause() { |
| 531 | context_->ipc_task_runner()->PostTask( |
kylechar | f448cc9 | 2019-02-19 20:28:09 | [diff] [blame] | 532 | FROM_HERE, base::BindOnce(&Context::PauseChannel, context_)); |
rockot | 1018875 | 2016-09-08 18:24:56 | [diff] [blame] | 533 | } |
| 534 | |
rockot | 401fb2c | 2016-09-06 18:35:57 | [diff] [blame] | 535 | void ChannelProxy::Unpause(bool flush) { |
| 536 | context_->ipc_task_runner()->PostTask( |
kylechar | f448cc9 | 2019-02-19 20:28:09 | [diff] [blame] | 537 | FROM_HERE, base::BindOnce(&Context::UnpauseChannel, context_, flush)); |
rockot | 401fb2c | 2016-09-06 18:35:57 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | void ChannelProxy::Flush() { |
| 541 | context_->ipc_task_runner()->PostTask( |
kylechar | f448cc9 | 2019-02-19 20:28:09 | [diff] [blame] | 542 | FROM_HERE, base::BindOnce(&Context::FlushChannel, context_)); |
rockot | 401fb2c | 2016-09-06 18:35:57 | [diff] [blame] | 543 | } |
| 544 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 545 | void ChannelProxy::Close() { |
gab | 21edbfa | 2017-05-31 16:49:12 | [diff] [blame] | 546 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | f775d10 | 2012-12-05 01:00:36 | [diff] [blame] | 547 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 548 | // Clear the backpointer to the listener so that any pending calls to |
| 549 | // Context::OnDispatchMessage or OnDispatchError will be ignored. It is |
| 550 | // possible that the channel could be closed while it is receiving messages! |
[email protected] | 3cdb7af81 | 2008-10-24 19:21:13 | [diff] [blame] | 551 | context_->Clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 552 | |
[email protected] | b243230 | 2012-07-02 21:15:52 | [diff] [blame] | 553 | if (context_->ipc_task_runner()) { |
| 554 | context_->ipc_task_runner()->PostTask( |
kylechar | f448cc9 | 2019-02-19 20:28:09 | [diff] [blame] | 555 | FROM_HERE, base::BindOnce(&Context::OnChannelClosed, context_)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
| 559 | bool ChannelProxy::Send(Message* message) { |
John Abd-El-Malek | d1d6f6a | 2017-07-18 20:01:00 | [diff] [blame] | 560 | DCHECK(!message->is_sync()) << "Need to use IPC::SyncChannel"; |
Etienne Bergeron | 0a07d73d | 2019-06-27 16:40:47 | [diff] [blame] | 561 | SendInternal(message); |
John Abd-El-Malek | d1d6f6a | 2017-07-18 20:01:00 | [diff] [blame] | 562 | return true; |
| 563 | } |
| 564 | |
Etienne Bergeron | 0a07d73d | 2019-06-27 16:40:47 | [diff] [blame] | 565 | void ChannelProxy::SendInternal(Message* message) { |
rockot | f14a8ae | 2016-06-16 19:28:41 | [diff] [blame] | 566 | DCHECK(did_init_); |
[email protected] | f775d10 | 2012-12-05 01:00:36 | [diff] [blame] | 567 | |
rockot | f14a8ae | 2016-06-16 19:28:41 | [diff] [blame] | 568 | // TODO(alexeypa): add DCHECK(CalledOnValidThread()) here. Currently there are |
| 569 | // tests that call Send() from a wrong thread. See http://crbug.com/163523. |
[email protected] | 8948031 | 2011-05-04 17:14:16 | [diff] [blame] | 570 | |
rockot | f14a8ae | 2016-06-16 19:28:41 | [diff] [blame] | 571 | #ifdef ENABLE_IPC_FUZZER |
| 572 | // In IPC fuzzing builds, it is possible to define a filter to apply to |
| 573 | // outgoing messages. It will either rewrite the message and return a new |
| 574 | // one, freeing the original, or return the message unchanged. |
| 575 | if (outgoing_message_filter()) |
| 576 | message = outgoing_message_filter()->Rewrite(message); |
| 577 | #endif |
| 578 | |
davidsz | 041528a | 2017-05-12 09:19:23 | [diff] [blame] | 579 | #if BUILDFLAG(IPC_MESSAGE_LOG_ENABLED) |
sammc | a1b3b4d | 2016-11-15 00:34:36 | [diff] [blame] | 580 | Logging::GetInstance()->OnSendMessage(message); |
rockot | f14a8ae | 2016-06-16 19:28:41 | [diff] [blame] | 581 | #endif |
| 582 | |
Etienne Bergeron | 0a07d73d | 2019-06-27 16:40:47 | [diff] [blame] | 583 | context_->Send(message); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | void ChannelProxy::AddFilter(MessageFilter* filter) { |
gab | 21edbfa | 2017-05-31 16:49:12 | [diff] [blame] | 587 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | f775d10 | 2012-12-05 01:00:36 | [diff] [blame] | 588 | |
[email protected] | 4b580bf | 2010-12-02 19:16:07 | [diff] [blame] | 589 | context_->AddFilter(filter); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 590 | } |
| 591 | |
| 592 | void ChannelProxy::RemoveFilter(MessageFilter* filter) { |
gab | 21edbfa | 2017-05-31 16:49:12 | [diff] [blame] | 593 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | f775d10 | 2012-12-05 01:00:36 | [diff] [blame] | 594 | |
[email protected] | b243230 | 2012-07-02 21:15:52 | [diff] [blame] | 595 | context_->ipc_task_runner()->PostTask( |
kylechar | f448cc9 | 2019-02-19 20:28:09 | [diff] [blame] | 596 | FROM_HERE, base::BindOnce(&Context::OnRemoveFilter, context_, |
| 597 | base::RetainedRef(filter))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 598 | } |
| 599 | |
rockot | 0e4de5f | 2016-07-22 21:18:07 | [diff] [blame] | 600 | void ChannelProxy::AddGenericAssociatedInterfaceForIOThread( |
| 601 | const std::string& name, |
| 602 | const GenericAssociatedInterfaceFactory& factory) { |
| 603 | context()->AddGenericAssociatedInterfaceForIOThread(name, factory); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 604 | } |
| 605 | |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 606 | void ChannelProxy::GetGenericRemoteAssociatedInterface( |
| 607 | const std::string& name, |
| 608 | mojo::ScopedInterfaceEndpointHandle handle) { |
rockot | 5a90895 | 2016-07-28 20:08:17 | [diff] [blame] | 609 | DCHECK(did_init_); |
Ken Rockot | 96d1b7b5 | 2017-05-13 00:29:21 | [diff] [blame] | 610 | context()->thread_safe_channel().GetAssociatedInterface( |
Julie Jeongeun Kim | 69604f6 | 2019-12-04 01:59:52 | [diff] [blame] | 611 | name, mojo::PendingAssociatedReceiver<mojom::GenericInterface>( |
| 612 | std::move(handle))); |
rockot | 8d890f6 | 2016-07-14 16:37:14 | [diff] [blame] | 613 | } |
| 614 | |
[email protected] | b243230 | 2012-07-02 21:15:52 | [diff] [blame] | 615 | void ChannelProxy::ClearIPCTaskRunner() { |
gab | 21edbfa | 2017-05-31 16:49:12 | [diff] [blame] | 616 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | b243230 | 2012-07-02 21:15:52 | [diff] [blame] | 617 | context()->ClearIPCTaskRunner(); |
[email protected] | 42f1d782 | 2009-07-23 18:17:55 | [diff] [blame] | 618 | } |
| 619 | |
rockot | 29ade1b | 2015-08-07 06:23:59 | [diff] [blame] | 620 | void ChannelProxy::OnChannelInit() { |
| 621 | } |
| 622 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 623 | //----------------------------------------------------------------------------- |
| 624 | |
| 625 | } // namespace IPC |