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