jschuh@chromium.org | a5cd076 | 2012-04-05 11:38:34 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
dmichael@chromium.org | c0c370e | 2014-04-25 09:07:30 +0900 | [diff] [blame] | 5 | #include "ipc/ipc_channel_proxy.h" |
| 6 | |
jhawkins@chromium.org | 9827bd1 | 2011-11-13 06:16:41 +0900 | [diff] [blame] | 7 | #include "base/bind.h" |
jhawkins@chromium.org | e34d0ad | 2011-11-29 11:24:28 +0900 | [diff] [blame] | 8 | #include "base/compiler_specific.h" |
ajwong@chromium.org | 8e2e300 | 2011-09-22 03:05:41 +0900 | [diff] [blame] | 9 | #include "base/location.h" |
levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 10 | #include "base/memory/ref_counted.h" |
| 11 | #include "base/memory/scoped_ptr.h" |
sergeyu@chromium.org | 5b6d49c | 2012-07-03 06:15:52 +0900 | [diff] [blame] | 12 | #include "base/single_thread_task_runner.h" |
| 13 | #include "base/thread_task_runner_handle.h" |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 14 | #include "ipc/ipc_channel_factory.h" |
brettw@chromium.org | f947ed0 | 2012-06-12 07:35:26 +0900 | [diff] [blame] | 15 | #include "ipc/ipc_listener.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 16 | #include "ipc/ipc_logging.h" |
piman@chromium.org | 24b15ad | 2012-05-12 08:24:37 +0900 | [diff] [blame] | 17 | #include "ipc/ipc_message_macros.h" |
dmichael@chromium.org | c0c370e | 2014-04-25 09:07:30 +0900 | [diff] [blame] | 18 | #include "ipc/message_filter.h" |
| 19 | #include "ipc/message_filter_router.h" |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 20 | |
| 21 | namespace IPC { |
| 22 | |
jcampan@chromium.org | 1c86b55 | 2009-07-29 07:09:45 +0900 | [diff] [blame] | 23 | //------------------------------------------------------------------------------ |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 24 | |
dcheng | ea780ac | 2014-08-29 01:59:29 +0900 | [diff] [blame] | 25 | ChannelProxy::Context::Context( |
| 26 | Listener* listener, |
| 27 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) |
sergeyu@chromium.org | 5b6d49c | 2012-07-03 06:15:52 +0900 | [diff] [blame] | 28 | : listener_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 29 | listener_(listener), |
sergeyu@chromium.org | 5b6d49c | 2012-07-03 06:15:52 +0900 | [diff] [blame] | 30 | ipc_task_runner_(ipc_task_runner), |
jschuh@chromium.org | a5cd076 | 2012-04-05 11:38:34 +0900 | [diff] [blame] | 31 | channel_connected_called_(false), |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 32 | message_filter_router_(new MessageFilterRouter()), |
jschuh@chromium.org | a5cd076 | 2012-04-05 11:38:34 +0900 | [diff] [blame] | 33 | peer_pid_(base::kNullProcessId) { |
rsleevi@chromium.org | 23b6623 | 2013-06-01 13:11:27 +0900 | [diff] [blame] | 34 | DCHECK(ipc_task_runner_.get()); |
dmichael@chromium.org | 1446d8a | 2014-04-17 14:07:18 +0900 | [diff] [blame] | 35 | // The Listener thread where Messages are handled must be a separate thread |
| 36 | // to avoid oversubscribing the IO thread. If you trigger this error, you |
| 37 | // need to either: |
| 38 | // 1) Create the ChannelProxy on a different thread, or |
| 39 | // 2) Just use Channel |
| 40 | // Note, we currently make an exception for a NULL listener. That usage |
| 41 | // basically works, but is outside the intent of ChannelProxy. This support |
| 42 | // will disappear, so please don't rely on it. See crbug.com/364241 |
| 43 | DCHECK(!listener || (ipc_task_runner_.get() != listener_task_runner_.get())); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 44 | } |
| 45 | |
jam@chromium.org | 06d1844 | 2011-05-03 03:00:49 +0900 | [diff] [blame] | 46 | ChannelProxy::Context::~Context() { |
| 47 | } |
| 48 | |
sergeyu@chromium.org | 5b6d49c | 2012-07-03 06:15:52 +0900 | [diff] [blame] | 49 | void ChannelProxy::Context::ClearIPCTaskRunner() { |
| 50 | ipc_task_runner_ = NULL; |
| 51 | } |
| 52 | |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 53 | void ChannelProxy::Context::CreateChannel(scoped_ptr<ChannelFactory> factory) { |
dmichael@chromium.org | 31a1681 | 2014-03-21 06:00:50 +0900 | [diff] [blame] | 54 | DCHECK(!channel_); |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 55 | channel_id_ = factory->GetName(); |
| 56 | channel_ = factory->BuildChannel(this); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | bool ChannelProxy::Context::TryFilters(const Message& message) { |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 60 | DCHECK(message_filter_router_); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 61 | #ifdef IPC_MESSAGE_LOG_ENABLED |
satish@chromium.org | aa87060 | 2010-12-13 17:18:55 +0900 | [diff] [blame] | 62 | Logging* logger = Logging::GetInstance(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 63 | if (logger->Enabled()) |
| 64 | logger->OnPreDispatchMessage(message); |
| 65 | #endif |
| 66 | |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 67 | if (message_filter_router_->TryFilters(message)) { |
jam@chromium.org | 822f1fb | 2014-05-16 08:06:07 +0900 | [diff] [blame] | 68 | if (message.dispatch_error()) { |
| 69 | listener_task_runner_->PostTask( |
| 70 | FROM_HERE, base::Bind(&Context::OnDispatchBadMessage, this, message)); |
| 71 | } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 72 | #ifdef IPC_MESSAGE_LOG_ENABLED |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 73 | if (logger->Enabled()) |
| 74 | logger->OnPostDispatchMessage(message, channel_id_); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 75 | #endif |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 76 | return true; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 77 | } |
| 78 | return false; |
| 79 | } |
| 80 | |
| 81 | // Called on the IPC::Channel thread |
jam@chromium.org | 8a2c784 | 2010-12-24 15:19:28 +0900 | [diff] [blame] | 82 | bool ChannelProxy::Context::OnMessageReceived(const Message& message) { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 83 | // First give a chance to the filters to process this message. |
| 84 | if (!TryFilters(message)) |
| 85 | OnMessageReceivedNoFilter(message); |
jam@chromium.org | 8a2c784 | 2010-12-24 15:19:28 +0900 | [diff] [blame] | 86 | return true; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | // Called on the IPC::Channel thread |
jam@chromium.org | 8a2c784 | 2010-12-24 15:19:28 +0900 | [diff] [blame] | 90 | bool ChannelProxy::Context::OnMessageReceivedNoFilter(const Message& message) { |
sergeyu@chromium.org | 5b6d49c | 2012-07-03 06:15:52 +0900 | [diff] [blame] | 91 | listener_task_runner_->PostTask( |
jhawkins@chromium.org | 9827bd1 | 2011-11-13 06:16:41 +0900 | [diff] [blame] | 92 | FROM_HERE, base::Bind(&Context::OnDispatchMessage, this, message)); |
jam@chromium.org | 8a2c784 | 2010-12-24 15:19:28 +0900 | [diff] [blame] | 93 | return true; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // Called on the IPC::Channel thread |
| 97 | void ChannelProxy::Context::OnChannelConnected(int32 peer_pid) { |
dmichael@chromium.org | 31a1681 | 2014-03-21 06:00:50 +0900 | [diff] [blame] | 98 | // We cache off the peer_pid so it can be safely accessed from both threads. |
morrita@chromium.org | fde2b6b | 2014-06-07 05:13:51 +0900 | [diff] [blame] | 99 | peer_pid_ = channel_->GetPeerPID(); |
dmichael@chromium.org | 31a1681 | 2014-03-21 06:00:50 +0900 | [diff] [blame] | 100 | |
jam@chromium.org | e57135c | 2010-12-03 04:16:07 +0900 | [diff] [blame] | 101 | // Add any pending filters. This avoids a race condition where someone |
| 102 | // creates a ChannelProxy, calls AddFilter, and then right after starts the |
| 103 | // peer process. The IO thread could receive a message before the task to add |
| 104 | // the filter is run on the IO thread. |
| 105 | OnAddFilter(); |
| 106 | |
sergeyu@chromium.org | 5b6d49c | 2012-07-03 06:15:52 +0900 | [diff] [blame] | 107 | // See above comment about using listener_task_runner_ here. |
| 108 | listener_task_runner_->PostTask( |
jhawkins@chromium.org | 9827bd1 | 2011-11-13 06:16:41 +0900 | [diff] [blame] | 109 | FROM_HERE, base::Bind(&Context::OnDispatchConnected, this)); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | // Called on the IPC::Channel thread |
| 113 | void ChannelProxy::Context::OnChannelError() { |
| 114 | for (size_t i = 0; i < filters_.size(); ++i) |
| 115 | filters_[i]->OnChannelError(); |
| 116 | |
sergeyu@chromium.org | 5b6d49c | 2012-07-03 06:15:52 +0900 | [diff] [blame] | 117 | // See above comment about using listener_task_runner_ here. |
| 118 | listener_task_runner_->PostTask( |
jhawkins@chromium.org | 9827bd1 | 2011-11-13 06:16:41 +0900 | [diff] [blame] | 119 | FROM_HERE, base::Bind(&Context::OnDispatchError, this)); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | // Called on the IPC::Channel thread |
| 123 | void ChannelProxy::Context::OnChannelOpened() { |
| 124 | DCHECK(channel_ != NULL); |
| 125 | |
| 126 | // Assume a reference to ourselves on behalf of this thread. This reference |
| 127 | // will be released when we are closed. |
| 128 | AddRef(); |
| 129 | |
| 130 | if (!channel_->Connect()) { |
| 131 | OnChannelError(); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | for (size_t i = 0; i < filters_.size(); ++i) |
dmaclach@chromium.org | 058c4a7 | 2010-12-09 04:28:09 +0900 | [diff] [blame] | 136 | filters_[i]->OnFilterAdded(channel_.get()); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | // Called on the IPC::Channel thread |
| 140 | void ChannelProxy::Context::OnChannelClosed() { |
| 141 | // It's okay for IPC::ChannelProxy::Close to be called more than once, which |
| 142 | // would result in this branch being taken. |
dmichael@chromium.org | 31a1681 | 2014-03-21 06:00:50 +0900 | [diff] [blame] | 143 | if (!channel_) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 144 | return; |
| 145 | |
| 146 | for (size_t i = 0; i < filters_.size(); ++i) { |
| 147 | filters_[i]->OnChannelClosing(); |
| 148 | filters_[i]->OnFilterRemoved(); |
| 149 | } |
| 150 | |
| 151 | // We don't need the filters anymore. |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 152 | message_filter_router_->Clear(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 153 | filters_.clear(); |
dmichael@chromium.org | 31a1681 | 2014-03-21 06:00:50 +0900 | [diff] [blame] | 154 | // We don't need the lock, because at this point, the listener thread can't |
| 155 | // access it any more. |
| 156 | pending_filters_.clear(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 157 | |
dmaclach@chromium.org | 058c4a7 | 2010-12-09 04:28:09 +0900 | [diff] [blame] | 158 | channel_.reset(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 159 | |
| 160 | // Balance with the reference taken during startup. This may result in |
| 161 | // self-destruction. |
| 162 | Release(); |
| 163 | } |
| 164 | |
hans@chromium.org | e62750c | 2012-08-10 05:39:12 +0900 | [diff] [blame] | 165 | void ChannelProxy::Context::Clear() { |
| 166 | listener_ = NULL; |
| 167 | } |
| 168 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 169 | // Called on the IPC::Channel thread |
rsleevi@chromium.org | 997c1d4 | 2012-04-28 11:12:00 +0900 | [diff] [blame] | 170 | void ChannelProxy::Context::OnSendMessage(scoped_ptr<Message> message) { |
dmichael@chromium.org | 31a1681 | 2014-03-21 06:00:50 +0900 | [diff] [blame] | 171 | if (!channel_) { |
ananta@chromium.org | 999f297 | 2010-09-03 06:45:50 +0900 | [diff] [blame] | 172 | OnChannelClosed(); |
| 173 | return; |
| 174 | } |
dmichael@chromium.org | 31a1681 | 2014-03-21 06:00:50 +0900 | [diff] [blame] | 175 | |
rsleevi@chromium.org | 997c1d4 | 2012-04-28 11:12:00 +0900 | [diff] [blame] | 176 | if (!channel_->Send(message.release())) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 177 | OnChannelError(); |
| 178 | } |
| 179 | |
| 180 | // Called on the IPC::Channel thread |
jam@chromium.org | e57135c | 2010-12-03 04:16:07 +0900 | [diff] [blame] | 181 | void ChannelProxy::Context::OnAddFilter() { |
dmichael@chromium.org | 31a1681 | 2014-03-21 06:00:50 +0900 | [diff] [blame] | 182 | // Our OnChannelConnected method has not yet been called, so we can't be |
| 183 | // sure that channel_ is valid yet. When OnChannelConnected *is* called, |
| 184 | // it invokes OnAddFilter, so any pending filter(s) will be added at that |
| 185 | // time. |
| 186 | if (peer_pid_ == base::kNullProcessId) |
| 187 | return; |
| 188 | |
evan@chromium.org | aa53871 | 2011-08-17 07:12:39 +0900 | [diff] [blame] | 189 | std::vector<scoped_refptr<MessageFilter> > new_filters; |
jam@chromium.org | e57135c | 2010-12-03 04:16:07 +0900 | [diff] [blame] | 190 | { |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 191 | base::AutoLock auto_lock(pending_filters_lock_); |
evan@chromium.org | aa53871 | 2011-08-17 07:12:39 +0900 | [diff] [blame] | 192 | new_filters.swap(pending_filters_); |
jam@chromium.org | e57135c | 2010-12-03 04:16:07 +0900 | [diff] [blame] | 193 | } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 194 | |
evan@chromium.org | aa53871 | 2011-08-17 07:12:39 +0900 | [diff] [blame] | 195 | for (size_t i = 0; i < new_filters.size(); ++i) { |
| 196 | filters_.push_back(new_filters[i]); |
jam@chromium.org | e57135c | 2010-12-03 04:16:07 +0900 | [diff] [blame] | 197 | |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 198 | message_filter_router_->AddFilter(new_filters[i].get()); |
| 199 | |
dmichael@chromium.org | 31a1681 | 2014-03-21 06:00:50 +0900 | [diff] [blame] | 200 | // The channel has already been created and connected, so we need to |
| 201 | // inform the filters right now. |
| 202 | new_filters[i]->OnFilterAdded(channel_.get()); |
| 203 | new_filters[i]->OnChannelConnected(peer_pid_); |
jam@chromium.org | e57135c | 2010-12-03 04:16:07 +0900 | [diff] [blame] | 204 | } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // Called on the IPC::Channel thread |
| 208 | void ChannelProxy::Context::OnRemoveFilter(MessageFilter* filter) { |
dmichael@chromium.org | 31a1681 | 2014-03-21 06:00:50 +0900 | [diff] [blame] | 209 | if (peer_pid_ == base::kNullProcessId) { |
| 210 | // The channel is not yet connected, so any filters are still pending. |
| 211 | base::AutoLock auto_lock(pending_filters_lock_); |
| 212 | for (size_t i = 0; i < pending_filters_.size(); ++i) { |
| 213 | if (pending_filters_[i].get() == filter) { |
| 214 | filter->OnFilterRemoved(); |
| 215 | pending_filters_.erase(pending_filters_.begin() + i); |
| 216 | return; |
| 217 | } |
| 218 | } |
| 219 | return; |
| 220 | } |
| 221 | if (!channel_) |
jam@chromium.org | ca4f6e2 | 2013-07-10 06:12:07 +0900 | [diff] [blame] | 222 | return; // The filters have already been deleted. |
| 223 | |
jdduke@chromium.org | 03f232f | 2014-02-26 14:18:04 +0900 | [diff] [blame] | 224 | message_filter_router_->RemoveFilter(filter); |
| 225 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 226 | for (size_t i = 0; i < filters_.size(); ++i) { |
| 227 | if (filters_[i].get() == filter) { |
| 228 | filter->OnFilterRemoved(); |
| 229 | filters_.erase(filters_.begin() + i); |
| 230 | return; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | NOTREACHED() << "filter to be removed not found"; |
| 235 | } |
| 236 | |
| 237 | // Called on the listener's thread |
jam@chromium.org | e57135c | 2010-12-03 04:16:07 +0900 | [diff] [blame] | 238 | void ChannelProxy::Context::AddFilter(MessageFilter* filter) { |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 239 | base::AutoLock auto_lock(pending_filters_lock_); |
jam@chromium.org | e57135c | 2010-12-03 04:16:07 +0900 | [diff] [blame] | 240 | pending_filters_.push_back(make_scoped_refptr(filter)); |
sergeyu@chromium.org | 5b6d49c | 2012-07-03 06:15:52 +0900 | [diff] [blame] | 241 | ipc_task_runner_->PostTask( |
jhawkins@chromium.org | 9827bd1 | 2011-11-13 06:16:41 +0900 | [diff] [blame] | 242 | FROM_HERE, base::Bind(&Context::OnAddFilter, this)); |
jam@chromium.org | e57135c | 2010-12-03 04:16:07 +0900 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | // Called on the listener's thread |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 246 | void ChannelProxy::Context::OnDispatchMessage(const Message& message) { |
rbyers@chromium.org | 8255d6c | 2012-02-15 05:08:09 +0900 | [diff] [blame] | 247 | #ifdef IPC_MESSAGE_LOG_ENABLED |
| 248 | Logging* logger = Logging::GetInstance(); |
| 249 | std::string name; |
| 250 | logger->GetMessageText(message.type(), &name, &message, NULL); |
epenner@chromium.org | 24f2bea | 2014-05-03 06:29:24 +0900 | [diff] [blame] | 251 | TRACE_EVENT1("ipc", "ChannelProxy::Context::OnDispatchMessage", |
rbyers@chromium.org | 8255d6c | 2012-02-15 05:08:09 +0900 | [diff] [blame] | 252 | "name", name); |
| 253 | #else |
epenner@chromium.org | 24f2bea | 2014-05-03 06:29:24 +0900 | [diff] [blame] | 254 | TRACE_EVENT2("ipc", "ChannelProxy::Context::OnDispatchMessage", |
piman@chromium.org | 24b15ad | 2012-05-12 08:24:37 +0900 | [diff] [blame] | 255 | "class", IPC_MESSAGE_ID_CLASS(message.type()), |
| 256 | "line", IPC_MESSAGE_ID_LINE(message.type())); |
rbyers@chromium.org | 8255d6c | 2012-02-15 05:08:09 +0900 | [diff] [blame] | 257 | #endif |
| 258 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 259 | if (!listener_) |
| 260 | return; |
| 261 | |
| 262 | OnDispatchConnected(); |
| 263 | |
| 264 | #ifdef IPC_MESSAGE_LOG_ENABLED |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 265 | if (message.type() == IPC_LOGGING_ID) { |
| 266 | logger->OnReceivedLoggingMessage(message); |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | if (logger->Enabled()) |
| 271 | logger->OnPreDispatchMessage(message); |
| 272 | #endif |
| 273 | |
| 274 | listener_->OnMessageReceived(message); |
jam@chromium.org | 822f1fb | 2014-05-16 08:06:07 +0900 | [diff] [blame] | 275 | if (message.dispatch_error()) |
| 276 | listener_->OnBadMessageReceived(message); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 277 | |
| 278 | #ifdef IPC_MESSAGE_LOG_ENABLED |
| 279 | if (logger->Enabled()) |
| 280 | logger->OnPostDispatchMessage(message, channel_id_); |
| 281 | #endif |
| 282 | } |
| 283 | |
| 284 | // Called on the listener's thread |
| 285 | void ChannelProxy::Context::OnDispatchConnected() { |
| 286 | if (channel_connected_called_) |
| 287 | return; |
| 288 | |
| 289 | channel_connected_called_ = true; |
| 290 | if (listener_) |
| 291 | listener_->OnChannelConnected(peer_pid_); |
| 292 | } |
| 293 | |
| 294 | // Called on the listener's thread |
| 295 | void ChannelProxy::Context::OnDispatchError() { |
| 296 | if (listener_) |
| 297 | listener_->OnChannelError(); |
| 298 | } |
| 299 | |
jam@chromium.org | 822f1fb | 2014-05-16 08:06:07 +0900 | [diff] [blame] | 300 | // Called on the listener's thread |
| 301 | void ChannelProxy::Context::OnDispatchBadMessage(const Message& message) { |
| 302 | if (listener_) |
| 303 | listener_->OnBadMessageReceived(message); |
| 304 | } |
| 305 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 306 | //----------------------------------------------------------------------------- |
| 307 | |
morrita@chromium.org | 15b4860 | 2014-06-06 01:15:38 +0900 | [diff] [blame] | 308 | // static |
| 309 | scoped_ptr<ChannelProxy> ChannelProxy::Create( |
| 310 | const IPC::ChannelHandle& channel_handle, |
| 311 | Channel::Mode mode, |
| 312 | Listener* listener, |
dcheng | ea780ac | 2014-08-29 01:59:29 +0900 | [diff] [blame] | 313 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { |
morrita@chromium.org | 15b4860 | 2014-06-06 01:15:38 +0900 | [diff] [blame] | 314 | scoped_ptr<ChannelProxy> channel(new ChannelProxy(listener, ipc_task_runner)); |
| 315 | channel->Init(channel_handle, mode, true); |
| 316 | return channel.Pass(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 317 | } |
| 318 | |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 319 | // static |
| 320 | scoped_ptr<ChannelProxy> ChannelProxy::Create( |
| 321 | scoped_ptr<ChannelFactory> factory, |
| 322 | Listener* listener, |
dcheng | ea780ac | 2014-08-29 01:59:29 +0900 | [diff] [blame] | 323 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) { |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 324 | scoped_ptr<ChannelProxy> channel(new ChannelProxy(listener, ipc_task_runner)); |
| 325 | channel->Init(factory.Pass(), true); |
| 326 | return channel.Pass(); |
| 327 | } |
| 328 | |
kkania@chromium.org | 9ccb469 | 2011-11-16 10:06:46 +0900 | [diff] [blame] | 329 | ChannelProxy::ChannelProxy(Context* context) |
tsepez@chromium.org | e68ddef | 2011-05-05 02:14:16 +0900 | [diff] [blame] | 330 | : context_(context), |
kkania@chromium.org | 9ccb469 | 2011-11-16 10:06:46 +0900 | [diff] [blame] | 331 | did_init_(false) { |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 332 | } |
| 333 | |
dcheng | ea780ac | 2014-08-29 01:59:29 +0900 | [diff] [blame] | 334 | ChannelProxy::ChannelProxy( |
| 335 | Listener* listener, |
| 336 | const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner) |
morrita@chromium.org | 15b4860 | 2014-06-06 01:15:38 +0900 | [diff] [blame] | 337 | : context_(new Context(listener, ipc_task_runner)), did_init_(false) { |
| 338 | } |
| 339 | |
erg@google.com | 2ec53b4 | 2010-09-24 07:43:53 +0900 | [diff] [blame] | 340 | ChannelProxy::~ChannelProxy() { |
alexeypa@chromium.org | 0192ff0 | 2012-12-05 10:00:36 +0900 | [diff] [blame] | 341 | DCHECK(CalledOnValidThread()); |
| 342 | |
erg@google.com | 2ec53b4 | 2010-09-24 07:43:53 +0900 | [diff] [blame] | 343 | Close(); |
| 344 | } |
| 345 | |
lambroslambrou@chromium.org | 773476a | 2014-06-03 05:29:30 +0900 | [diff] [blame] | 346 | void ChannelProxy::Init(const IPC::ChannelHandle& channel_handle, |
| 347 | Channel::Mode mode, |
| 348 | bool create_pipe_now) { |
dmaclach@chromium.org | 058c4a7 | 2010-12-09 04:28:09 +0900 | [diff] [blame] | 349 | #if defined(OS_POSIX) |
| 350 | // When we are creating a server on POSIX, we need its file descriptor |
| 351 | // to be created immediately so that it can be accessed and passed |
| 352 | // to other processes. Forcing it to be created immediately avoids |
| 353 | // race conditions that may otherwise arise. |
dmaclach@chromium.org | f146c29 | 2011-02-04 05:35:09 +0900 | [diff] [blame] | 354 | if (mode & Channel::MODE_SERVER_FLAG) { |
dmaclach@chromium.org | 058c4a7 | 2010-12-09 04:28:09 +0900 | [diff] [blame] | 355 | create_pipe_now = true; |
| 356 | } |
| 357 | #endif // defined(OS_POSIX) |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 358 | Init(ChannelFactory::Create(channel_handle, mode), |
| 359 | create_pipe_now); |
| 360 | } |
| 361 | |
| 362 | void ChannelProxy::Init(scoped_ptr<ChannelFactory> factory, |
| 363 | bool create_pipe_now) { |
| 364 | DCHECK(CalledOnValidThread()); |
| 365 | DCHECK(!did_init_); |
dmaclach@chromium.org | 058c4a7 | 2010-12-09 04:28:09 +0900 | [diff] [blame] | 366 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 367 | if (create_pipe_now) { |
| 368 | // Create the channel immediately. This effectively sets up the |
| 369 | // low-level pipe so that the client can connect. Without creating |
| 370 | // the pipe immediately, it is possible for a listener to attempt |
| 371 | // to connect and get an error since the pipe doesn't exist yet. |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 372 | context_->CreateChannel(factory.Pass()); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 373 | } else { |
sergeyu@chromium.org | 5b6d49c | 2012-07-03 06:15:52 +0900 | [diff] [blame] | 374 | context_->ipc_task_runner()->PostTask( |
morrita@chromium.org | 15996aa | 2014-08-05 08:44:17 +0900 | [diff] [blame] | 375 | FROM_HERE, base::Bind(&Context::CreateChannel, |
| 376 | context_.get(), Passed(factory.Pass()))); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | // complete initialization on the background thread |
sergeyu@chromium.org | 5b6d49c | 2012-07-03 06:15:52 +0900 | [diff] [blame] | 380 | context_->ipc_task_runner()->PostTask( |
jhawkins@chromium.org | 9827bd1 | 2011-11-13 06:16:41 +0900 | [diff] [blame] | 381 | FROM_HERE, base::Bind(&Context::OnChannelOpened, context_.get())); |
kkania@chromium.org | 9ccb469 | 2011-11-16 10:06:46 +0900 | [diff] [blame] | 382 | |
| 383 | did_init_ = true; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | void ChannelProxy::Close() { |
alexeypa@chromium.org | 0192ff0 | 2012-12-05 10:00:36 +0900 | [diff] [blame] | 387 | DCHECK(CalledOnValidThread()); |
| 388 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 389 | // Clear the backpointer to the listener so that any pending calls to |
| 390 | // Context::OnDispatchMessage or OnDispatchError will be ignored. It is |
| 391 | // possible that the channel could be closed while it is receiving messages! |
| 392 | context_->Clear(); |
| 393 | |
sergeyu@chromium.org | 5b6d49c | 2012-07-03 06:15:52 +0900 | [diff] [blame] | 394 | if (context_->ipc_task_runner()) { |
| 395 | context_->ipc_task_runner()->PostTask( |
jhawkins@chromium.org | 9827bd1 | 2011-11-13 06:16:41 +0900 | [diff] [blame] | 396 | FROM_HERE, base::Bind(&Context::OnChannelClosed, context_.get())); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
| 400 | bool ChannelProxy::Send(Message* message) { |
kkania@chromium.org | 9ccb469 | 2011-11-16 10:06:46 +0900 | [diff] [blame] | 401 | DCHECK(did_init_); |
alexeypa@chromium.org | 0192ff0 | 2012-12-05 10:00:36 +0900 | [diff] [blame] | 402 | |
| 403 | // TODO(alexeypa): add DCHECK(CalledOnValidThread()) here. Currently there are |
| 404 | // tests that call Send() from a wrong thread. See http://crbug.com/163523. |
tsepez@chromium.org | e68ddef | 2011-05-05 02:14:16 +0900 | [diff] [blame] | 405 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 406 | #ifdef IPC_MESSAGE_LOG_ENABLED |
satish@chromium.org | aa87060 | 2010-12-13 17:18:55 +0900 | [diff] [blame] | 407 | Logging::GetInstance()->OnSendMessage(message, context_->channel_id()); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 408 | #endif |
| 409 | |
sergeyu@chromium.org | 5b6d49c | 2012-07-03 06:15:52 +0900 | [diff] [blame] | 410 | context_->ipc_task_runner()->PostTask( |
jhawkins@chromium.org | e34d0ad | 2011-11-29 11:24:28 +0900 | [diff] [blame] | 411 | FROM_HERE, |
rsleevi@chromium.org | 997c1d4 | 2012-04-28 11:12:00 +0900 | [diff] [blame] | 412 | base::Bind(&ChannelProxy::Context::OnSendMessage, |
| 413 | context_, base::Passed(scoped_ptr<Message>(message)))); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 414 | return true; |
| 415 | } |
| 416 | |
| 417 | void ChannelProxy::AddFilter(MessageFilter* filter) { |
alexeypa@chromium.org | 0192ff0 | 2012-12-05 10:00:36 +0900 | [diff] [blame] | 418 | DCHECK(CalledOnValidThread()); |
| 419 | |
jam@chromium.org | e57135c | 2010-12-03 04:16:07 +0900 | [diff] [blame] | 420 | context_->AddFilter(filter); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | void ChannelProxy::RemoveFilter(MessageFilter* filter) { |
alexeypa@chromium.org | 0192ff0 | 2012-12-05 10:00:36 +0900 | [diff] [blame] | 424 | DCHECK(CalledOnValidThread()); |
| 425 | |
sergeyu@chromium.org | 5b6d49c | 2012-07-03 06:15:52 +0900 | [diff] [blame] | 426 | context_->ipc_task_runner()->PostTask( |
jhawkins@chromium.org | 9827bd1 | 2011-11-13 06:16:41 +0900 | [diff] [blame] | 427 | FROM_HERE, base::Bind(&Context::OnRemoveFilter, context_.get(), |
| 428 | make_scoped_refptr(filter))); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 429 | } |
| 430 | |
sergeyu@chromium.org | 5b6d49c | 2012-07-03 06:15:52 +0900 | [diff] [blame] | 431 | void ChannelProxy::ClearIPCTaskRunner() { |
alexeypa@chromium.org | 0192ff0 | 2012-12-05 10:00:36 +0900 | [diff] [blame] | 432 | DCHECK(CalledOnValidThread()); |
| 433 | |
sergeyu@chromium.org | 5b6d49c | 2012-07-03 06:15:52 +0900 | [diff] [blame] | 434 | context()->ClearIPCTaskRunner(); |
nsylvain@chromium.org | c12dde3 | 2009-07-24 03:17:55 +0900 | [diff] [blame] | 435 | } |
| 436 | |
dmichael@chromium.org | b798bb4 | 2012-06-01 04:37:54 +0900 | [diff] [blame] | 437 | #if defined(OS_POSIX) && !defined(OS_NACL) |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 438 | // See the TODO regarding lazy initialization of the channel in |
| 439 | // ChannelProxy::Init(). |
phajdan.jr@chromium.org | af9455b | 2011-09-20 02:08:12 +0900 | [diff] [blame] | 440 | int ChannelProxy::GetClientFileDescriptor() { |
alexeypa@chromium.org | 0192ff0 | 2012-12-05 10:00:36 +0900 | [diff] [blame] | 441 | DCHECK(CalledOnValidThread()); |
| 442 | |
phajdan.jr@chromium.org | af9455b | 2011-09-20 02:08:12 +0900 | [diff] [blame] | 443 | Channel* channel = context_.get()->channel_.get(); |
dmaclach@chromium.org | 058c4a7 | 2010-12-09 04:28:09 +0900 | [diff] [blame] | 444 | // Channel must have been created first. |
| 445 | DCHECK(channel) << context_.get()->channel_id_; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 446 | return channel->GetClientFileDescriptor(); |
| 447 | } |
wez@chromium.org | 7cce091 | 2011-04-06 21:01:44 +0900 | [diff] [blame] | 448 | |
phajdan.jr@chromium.org | af9455b | 2011-09-20 02:08:12 +0900 | [diff] [blame] | 449 | int ChannelProxy::TakeClientFileDescriptor() { |
alexeypa@chromium.org | 0192ff0 | 2012-12-05 10:00:36 +0900 | [diff] [blame] | 450 | DCHECK(CalledOnValidThread()); |
| 451 | |
phajdan.jr@chromium.org | af9455b | 2011-09-20 02:08:12 +0900 | [diff] [blame] | 452 | Channel* channel = context_.get()->channel_.get(); |
| 453 | // Channel must have been created first. |
| 454 | DCHECK(channel) << context_.get()->channel_id_; |
| 455 | return channel->TakeClientFileDescriptor(); |
| 456 | } |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 457 | #endif |
| 458 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 459 | //----------------------------------------------------------------------------- |
| 460 | |
| 461 | } // namespace IPC |