blob: 2ea722df79b086f1cd1ba6f728a53052026bae07 [file] [log] [blame]
jschuh@chromium.orga5cd0762012-04-05 11:38:34 +09001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
agl@chromium.org1c6dcf22009-07-23 08:57:21 +09002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
dmichael@chromium.orgc0c370e2014-04-25 09:07:30 +09005#include "ipc/ipc_channel_proxy.h"
6
jhawkins@chromium.org9827bd12011-11-13 06:16:41 +09007#include "base/bind.h"
jhawkins@chromium.orge34d0ad2011-11-29 11:24:28 +09008#include "base/compiler_specific.h"
ajwong@chromium.org8e2e3002011-09-22 03:05:41 +09009#include "base/location.h"
levin@chromium.org5c528682011-03-28 10:54:15 +090010#include "base/memory/ref_counted.h"
11#include "base/memory/scoped_ptr.h"
sergeyu@chromium.org5b6d49c2012-07-03 06:15:52 +090012#include "base/single_thread_task_runner.h"
13#include "base/thread_task_runner_handle.h"
morrita@chromium.org15996aa2014-08-05 08:44:17 +090014#include "ipc/ipc_channel_factory.h"
brettw@chromium.orgf947ed02012-06-12 07:35:26 +090015#include "ipc/ipc_listener.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090016#include "ipc/ipc_logging.h"
piman@chromium.org24b15ad2012-05-12 08:24:37 +090017#include "ipc/ipc_message_macros.h"
dmichael@chromium.orgc0c370e2014-04-25 09:07:30 +090018#include "ipc/message_filter.h"
19#include "ipc/message_filter_router.h"
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090020
21namespace IPC {
22
jcampan@chromium.org1c86b552009-07-29 07:09:45 +090023//------------------------------------------------------------------------------
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090024
brettw@chromium.orgf947ed02012-06-12 07:35:26 +090025ChannelProxy::Context::Context(Listener* listener,
sergeyu@chromium.org5b6d49c2012-07-03 06:15:52 +090026 base::SingleThreadTaskRunner* ipc_task_runner)
27 : listener_task_runner_(base::ThreadTaskRunnerHandle::Get()),
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090028 listener_(listener),
sergeyu@chromium.org5b6d49c2012-07-03 06:15:52 +090029 ipc_task_runner_(ipc_task_runner),
jschuh@chromium.orga5cd0762012-04-05 11:38:34 +090030 channel_connected_called_(false),
jdduke@chromium.org03f232f2014-02-26 14:18:04 +090031 message_filter_router_(new MessageFilterRouter()),
jschuh@chromium.orga5cd0762012-04-05 11:38:34 +090032 peer_pid_(base::kNullProcessId) {
rsleevi@chromium.org23b66232013-06-01 13:11:27 +090033 DCHECK(ipc_task_runner_.get());
dmichael@chromium.org1446d8a2014-04-17 14:07:18 +090034 // The Listener thread where Messages are handled must be a separate thread
35 // to avoid oversubscribing the IO thread. If you trigger this error, you
36 // need to either:
37 // 1) Create the ChannelProxy on a different thread, or
38 // 2) Just use Channel
39 // Note, we currently make an exception for a NULL listener. That usage
40 // basically works, but is outside the intent of ChannelProxy. This support
41 // will disappear, so please don't rely on it. See crbug.com/364241
42 DCHECK(!listener || (ipc_task_runner_.get() != listener_task_runner_.get()));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090043}
44
jam@chromium.org06d18442011-05-03 03:00:49 +090045ChannelProxy::Context::~Context() {
46}
47
sergeyu@chromium.org5b6d49c2012-07-03 06:15:52 +090048void ChannelProxy::Context::ClearIPCTaskRunner() {
49 ipc_task_runner_ = NULL;
50}
51
morrita@chromium.org15996aa2014-08-05 08:44:17 +090052void ChannelProxy::Context::CreateChannel(scoped_ptr<ChannelFactory> factory) {
dmichael@chromium.org31a16812014-03-21 06:00:50 +090053 DCHECK(!channel_);
morrita@chromium.org15996aa2014-08-05 08:44:17 +090054 channel_id_ = factory->GetName();
55 channel_ = factory->BuildChannel(this);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090056}
57
58bool ChannelProxy::Context::TryFilters(const Message& message) {
jdduke@chromium.org03f232f2014-02-26 14:18:04 +090059 DCHECK(message_filter_router_);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090060#ifdef IPC_MESSAGE_LOG_ENABLED
satish@chromium.orgaa870602010-12-13 17:18:55 +090061 Logging* logger = Logging::GetInstance();
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090062 if (logger->Enabled())
63 logger->OnPreDispatchMessage(message);
64#endif
65
jdduke@chromium.org03f232f2014-02-26 14:18:04 +090066 if (message_filter_router_->TryFilters(message)) {
jam@chromium.org822f1fb2014-05-16 08:06:07 +090067 if (message.dispatch_error()) {
68 listener_task_runner_->PostTask(
69 FROM_HERE, base::Bind(&Context::OnDispatchBadMessage, this, message));
70 }
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090071#ifdef IPC_MESSAGE_LOG_ENABLED
jdduke@chromium.org03f232f2014-02-26 14:18:04 +090072 if (logger->Enabled())
73 logger->OnPostDispatchMessage(message, channel_id_);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090074#endif
jdduke@chromium.org03f232f2014-02-26 14:18:04 +090075 return true;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090076 }
77 return false;
78}
79
80// Called on the IPC::Channel thread
jam@chromium.org8a2c7842010-12-24 15:19:28 +090081bool ChannelProxy::Context::OnMessageReceived(const Message& message) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090082 // First give a chance to the filters to process this message.
83 if (!TryFilters(message))
84 OnMessageReceivedNoFilter(message);
jam@chromium.org8a2c7842010-12-24 15:19:28 +090085 return true;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090086}
87
88// Called on the IPC::Channel thread
jam@chromium.org8a2c7842010-12-24 15:19:28 +090089bool ChannelProxy::Context::OnMessageReceivedNoFilter(const Message& message) {
sergeyu@chromium.org5b6d49c2012-07-03 06:15:52 +090090 listener_task_runner_->PostTask(
jhawkins@chromium.org9827bd12011-11-13 06:16:41 +090091 FROM_HERE, base::Bind(&Context::OnDispatchMessage, this, message));
jam@chromium.org8a2c7842010-12-24 15:19:28 +090092 return true;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +090093}
94
95// Called on the IPC::Channel thread
96void ChannelProxy::Context::OnChannelConnected(int32 peer_pid) {
dmichael@chromium.org31a16812014-03-21 06:00:50 +090097 // We cache off the peer_pid so it can be safely accessed from both threads.
morrita@chromium.orgfde2b6b2014-06-07 05:13:51 +090098 peer_pid_ = channel_->GetPeerPID();
dmichael@chromium.org31a16812014-03-21 06:00:50 +090099
jam@chromium.orge57135c2010-12-03 04:16:07 +0900100 // Add any pending filters. This avoids a race condition where someone
101 // creates a ChannelProxy, calls AddFilter, and then right after starts the
102 // peer process. The IO thread could receive a message before the task to add
103 // the filter is run on the IO thread.
104 OnAddFilter();
105
sergeyu@chromium.org5b6d49c2012-07-03 06:15:52 +0900106 // See above comment about using listener_task_runner_ here.
107 listener_task_runner_->PostTask(
jhawkins@chromium.org9827bd12011-11-13 06:16:41 +0900108 FROM_HERE, base::Bind(&Context::OnDispatchConnected, this));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900109}
110
111// Called on the IPC::Channel thread
112void ChannelProxy::Context::OnChannelError() {
113 for (size_t i = 0; i < filters_.size(); ++i)
114 filters_[i]->OnChannelError();
115
sergeyu@chromium.org5b6d49c2012-07-03 06:15:52 +0900116 // See above comment about using listener_task_runner_ here.
117 listener_task_runner_->PostTask(
jhawkins@chromium.org9827bd12011-11-13 06:16:41 +0900118 FROM_HERE, base::Bind(&Context::OnDispatchError, this));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900119}
120
121// Called on the IPC::Channel thread
122void ChannelProxy::Context::OnChannelOpened() {
123 DCHECK(channel_ != NULL);
124
125 // Assume a reference to ourselves on behalf of this thread. This reference
126 // will be released when we are closed.
127 AddRef();
128
129 if (!channel_->Connect()) {
130 OnChannelError();
131 return;
132 }
133
134 for (size_t i = 0; i < filters_.size(); ++i)
dmaclach@chromium.org058c4a72010-12-09 04:28:09 +0900135 filters_[i]->OnFilterAdded(channel_.get());
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900136}
137
138// Called on the IPC::Channel thread
139void ChannelProxy::Context::OnChannelClosed() {
140 // It's okay for IPC::ChannelProxy::Close to be called more than once, which
141 // would result in this branch being taken.
dmichael@chromium.org31a16812014-03-21 06:00:50 +0900142 if (!channel_)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900143 return;
144
145 for (size_t i = 0; i < filters_.size(); ++i) {
146 filters_[i]->OnChannelClosing();
147 filters_[i]->OnFilterRemoved();
148 }
149
150 // We don't need the filters anymore.
jdduke@chromium.org03f232f2014-02-26 14:18:04 +0900151 message_filter_router_->Clear();
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900152 filters_.clear();
dmichael@chromium.org31a16812014-03-21 06:00:50 +0900153 // We don't need the lock, because at this point, the listener thread can't
154 // access it any more.
155 pending_filters_.clear();
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900156
dmaclach@chromium.org058c4a72010-12-09 04:28:09 +0900157 channel_.reset();
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900158
159 // Balance with the reference taken during startup. This may result in
160 // self-destruction.
161 Release();
162}
163
hans@chromium.orge62750c2012-08-10 05:39:12 +0900164void ChannelProxy::Context::Clear() {
165 listener_ = NULL;
166}
167
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900168// Called on the IPC::Channel thread
rsleevi@chromium.org997c1d42012-04-28 11:12:00 +0900169void ChannelProxy::Context::OnSendMessage(scoped_ptr<Message> message) {
dmichael@chromium.org31a16812014-03-21 06:00:50 +0900170 if (!channel_) {
ananta@chromium.org999f2972010-09-03 06:45:50 +0900171 OnChannelClosed();
172 return;
173 }
dmichael@chromium.org31a16812014-03-21 06:00:50 +0900174
rsleevi@chromium.org997c1d42012-04-28 11:12:00 +0900175 if (!channel_->Send(message.release()))
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900176 OnChannelError();
177}
178
179// Called on the IPC::Channel thread
jam@chromium.orge57135c2010-12-03 04:16:07 +0900180void ChannelProxy::Context::OnAddFilter() {
dmichael@chromium.org31a16812014-03-21 06:00:50 +0900181 // Our OnChannelConnected method has not yet been called, so we can't be
182 // sure that channel_ is valid yet. When OnChannelConnected *is* called,
183 // it invokes OnAddFilter, so any pending filter(s) will be added at that
184 // time.
185 if (peer_pid_ == base::kNullProcessId)
186 return;
187
evan@chromium.orgaa538712011-08-17 07:12:39 +0900188 std::vector<scoped_refptr<MessageFilter> > new_filters;
jam@chromium.orge57135c2010-12-03 04:16:07 +0900189 {
brettw@chromium.orgabe477a2011-01-21 13:55:52 +0900190 base::AutoLock auto_lock(pending_filters_lock_);
evan@chromium.orgaa538712011-08-17 07:12:39 +0900191 new_filters.swap(pending_filters_);
jam@chromium.orge57135c2010-12-03 04:16:07 +0900192 }
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900193
evan@chromium.orgaa538712011-08-17 07:12:39 +0900194 for (size_t i = 0; i < new_filters.size(); ++i) {
195 filters_.push_back(new_filters[i]);
jam@chromium.orge57135c2010-12-03 04:16:07 +0900196
jdduke@chromium.org03f232f2014-02-26 14:18:04 +0900197 message_filter_router_->AddFilter(new_filters[i].get());
198
dmichael@chromium.org31a16812014-03-21 06:00:50 +0900199 // The channel has already been created and connected, so we need to
200 // inform the filters right now.
201 new_filters[i]->OnFilterAdded(channel_.get());
202 new_filters[i]->OnChannelConnected(peer_pid_);
jam@chromium.orge57135c2010-12-03 04:16:07 +0900203 }
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900204}
205
206// Called on the IPC::Channel thread
207void ChannelProxy::Context::OnRemoveFilter(MessageFilter* filter) {
dmichael@chromium.org31a16812014-03-21 06:00:50 +0900208 if (peer_pid_ == base::kNullProcessId) {
209 // The channel is not yet connected, so any filters are still pending.
210 base::AutoLock auto_lock(pending_filters_lock_);
211 for (size_t i = 0; i < pending_filters_.size(); ++i) {
212 if (pending_filters_[i].get() == filter) {
213 filter->OnFilterRemoved();
214 pending_filters_.erase(pending_filters_.begin() + i);
215 return;
216 }
217 }
218 return;
219 }
220 if (!channel_)
jam@chromium.orgca4f6e22013-07-10 06:12:07 +0900221 return; // The filters have already been deleted.
222
jdduke@chromium.org03f232f2014-02-26 14:18:04 +0900223 message_filter_router_->RemoveFilter(filter);
224
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900225 for (size_t i = 0; i < filters_.size(); ++i) {
226 if (filters_[i].get() == filter) {
227 filter->OnFilterRemoved();
228 filters_.erase(filters_.begin() + i);
229 return;
230 }
231 }
232
233 NOTREACHED() << "filter to be removed not found";
234}
235
236// Called on the listener's thread
jam@chromium.orge57135c2010-12-03 04:16:07 +0900237void ChannelProxy::Context::AddFilter(MessageFilter* filter) {
brettw@chromium.orgabe477a2011-01-21 13:55:52 +0900238 base::AutoLock auto_lock(pending_filters_lock_);
jam@chromium.orge57135c2010-12-03 04:16:07 +0900239 pending_filters_.push_back(make_scoped_refptr(filter));
sergeyu@chromium.org5b6d49c2012-07-03 06:15:52 +0900240 ipc_task_runner_->PostTask(
jhawkins@chromium.org9827bd12011-11-13 06:16:41 +0900241 FROM_HERE, base::Bind(&Context::OnAddFilter, this));
jam@chromium.orge57135c2010-12-03 04:16:07 +0900242}
243
244// Called on the listener's thread
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900245void ChannelProxy::Context::OnDispatchMessage(const Message& message) {
rbyers@chromium.org8255d6c2012-02-15 05:08:09 +0900246#ifdef IPC_MESSAGE_LOG_ENABLED
247 Logging* logger = Logging::GetInstance();
248 std::string name;
249 logger->GetMessageText(message.type(), &name, &message, NULL);
epenner@chromium.org24f2bea2014-05-03 06:29:24 +0900250 TRACE_EVENT1("ipc", "ChannelProxy::Context::OnDispatchMessage",
rbyers@chromium.org8255d6c2012-02-15 05:08:09 +0900251 "name", name);
252#else
epenner@chromium.org24f2bea2014-05-03 06:29:24 +0900253 TRACE_EVENT2("ipc", "ChannelProxy::Context::OnDispatchMessage",
piman@chromium.org24b15ad2012-05-12 08:24:37 +0900254 "class", IPC_MESSAGE_ID_CLASS(message.type()),
255 "line", IPC_MESSAGE_ID_LINE(message.type()));
rbyers@chromium.org8255d6c2012-02-15 05:08:09 +0900256#endif
257
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900258 if (!listener_)
259 return;
260
261 OnDispatchConnected();
262
263#ifdef IPC_MESSAGE_LOG_ENABLED
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900264 if (message.type() == IPC_LOGGING_ID) {
265 logger->OnReceivedLoggingMessage(message);
266 return;
267 }
268
269 if (logger->Enabled())
270 logger->OnPreDispatchMessage(message);
271#endif
272
273 listener_->OnMessageReceived(message);
jam@chromium.org822f1fb2014-05-16 08:06:07 +0900274 if (message.dispatch_error())
275 listener_->OnBadMessageReceived(message);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900276
277#ifdef IPC_MESSAGE_LOG_ENABLED
278 if (logger->Enabled())
279 logger->OnPostDispatchMessage(message, channel_id_);
280#endif
281}
282
283// Called on the listener's thread
284void ChannelProxy::Context::OnDispatchConnected() {
285 if (channel_connected_called_)
286 return;
287
288 channel_connected_called_ = true;
289 if (listener_)
290 listener_->OnChannelConnected(peer_pid_);
291}
292
293// Called on the listener's thread
294void ChannelProxy::Context::OnDispatchError() {
295 if (listener_)
296 listener_->OnChannelError();
297}
298
jam@chromium.org822f1fb2014-05-16 08:06:07 +0900299// Called on the listener's thread
300void ChannelProxy::Context::OnDispatchBadMessage(const Message& message) {
301 if (listener_)
302 listener_->OnBadMessageReceived(message);
303}
304
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900305//-----------------------------------------------------------------------------
306
morrita@chromium.org15b48602014-06-06 01:15:38 +0900307// static
308scoped_ptr<ChannelProxy> ChannelProxy::Create(
309 const IPC::ChannelHandle& channel_handle,
310 Channel::Mode mode,
311 Listener* listener,
312 base::SingleThreadTaskRunner* ipc_task_runner) {
313 scoped_ptr<ChannelProxy> channel(new ChannelProxy(listener, ipc_task_runner));
314 channel->Init(channel_handle, mode, true);
315 return channel.Pass();
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900316}
317
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900318// static
319scoped_ptr<ChannelProxy> ChannelProxy::Create(
320 scoped_ptr<ChannelFactory> factory,
321 Listener* listener,
322 base::SingleThreadTaskRunner* ipc_task_runner) {
323 scoped_ptr<ChannelProxy> channel(new ChannelProxy(listener, ipc_task_runner));
324 channel->Init(factory.Pass(), true);
325 return channel.Pass();
326}
327
kkania@chromium.org9ccb4692011-11-16 10:06:46 +0900328ChannelProxy::ChannelProxy(Context* context)
tsepez@chromium.orge68ddef2011-05-05 02:14:16 +0900329 : context_(context),
kkania@chromium.org9ccb4692011-11-16 10:06:46 +0900330 did_init_(false) {
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900331}
332
morrita@chromium.org15b48602014-06-06 01:15:38 +0900333ChannelProxy::ChannelProxy(Listener* listener,
334 base::SingleThreadTaskRunner* ipc_task_runner)
335 : context_(new Context(listener, ipc_task_runner)), did_init_(false) {
336}
337
erg@google.com2ec53b42010-09-24 07:43:53 +0900338ChannelProxy::~ChannelProxy() {
alexeypa@chromium.org0192ff02012-12-05 10:00:36 +0900339 DCHECK(CalledOnValidThread());
340
erg@google.com2ec53b42010-09-24 07:43:53 +0900341 Close();
342}
343
lambroslambrou@chromium.org773476a2014-06-03 05:29:30 +0900344void ChannelProxy::Init(const IPC::ChannelHandle& channel_handle,
345 Channel::Mode mode,
346 bool create_pipe_now) {
dmaclach@chromium.org058c4a72010-12-09 04:28:09 +0900347#if defined(OS_POSIX)
348 // When we are creating a server on POSIX, we need its file descriptor
349 // to be created immediately so that it can be accessed and passed
350 // to other processes. Forcing it to be created immediately avoids
351 // race conditions that may otherwise arise.
dmaclach@chromium.orgf146c292011-02-04 05:35:09 +0900352 if (mode & Channel::MODE_SERVER_FLAG) {
dmaclach@chromium.org058c4a72010-12-09 04:28:09 +0900353 create_pipe_now = true;
354 }
355#endif // defined(OS_POSIX)
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900356 Init(ChannelFactory::Create(channel_handle, mode),
357 create_pipe_now);
358}
359
360void ChannelProxy::Init(scoped_ptr<ChannelFactory> factory,
361 bool create_pipe_now) {
362 DCHECK(CalledOnValidThread());
363 DCHECK(!did_init_);
dmaclach@chromium.org058c4a72010-12-09 04:28:09 +0900364
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900365 if (create_pipe_now) {
366 // Create the channel immediately. This effectively sets up the
367 // low-level pipe so that the client can connect. Without creating
368 // the pipe immediately, it is possible for a listener to attempt
369 // to connect and get an error since the pipe doesn't exist yet.
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900370 context_->CreateChannel(factory.Pass());
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900371 } else {
sergeyu@chromium.org5b6d49c2012-07-03 06:15:52 +0900372 context_->ipc_task_runner()->PostTask(
morrita@chromium.org15996aa2014-08-05 08:44:17 +0900373 FROM_HERE, base::Bind(&Context::CreateChannel,
374 context_.get(), Passed(factory.Pass())));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900375 }
376
377 // complete initialization on the background thread
sergeyu@chromium.org5b6d49c2012-07-03 06:15:52 +0900378 context_->ipc_task_runner()->PostTask(
jhawkins@chromium.org9827bd12011-11-13 06:16:41 +0900379 FROM_HERE, base::Bind(&Context::OnChannelOpened, context_.get()));
kkania@chromium.org9ccb4692011-11-16 10:06:46 +0900380
381 did_init_ = true;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900382}
383
384void ChannelProxy::Close() {
alexeypa@chromium.org0192ff02012-12-05 10:00:36 +0900385 DCHECK(CalledOnValidThread());
386
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900387 // Clear the backpointer to the listener so that any pending calls to
388 // Context::OnDispatchMessage or OnDispatchError will be ignored. It is
389 // possible that the channel could be closed while it is receiving messages!
390 context_->Clear();
391
sergeyu@chromium.org5b6d49c2012-07-03 06:15:52 +0900392 if (context_->ipc_task_runner()) {
393 context_->ipc_task_runner()->PostTask(
jhawkins@chromium.org9827bd12011-11-13 06:16:41 +0900394 FROM_HERE, base::Bind(&Context::OnChannelClosed, context_.get()));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900395 }
396}
397
398bool ChannelProxy::Send(Message* message) {
kkania@chromium.org9ccb4692011-11-16 10:06:46 +0900399 DCHECK(did_init_);
alexeypa@chromium.org0192ff02012-12-05 10:00:36 +0900400
401 // TODO(alexeypa): add DCHECK(CalledOnValidThread()) here. Currently there are
402 // tests that call Send() from a wrong thread. See http://crbug.com/163523.
tsepez@chromium.orge68ddef2011-05-05 02:14:16 +0900403
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900404#ifdef IPC_MESSAGE_LOG_ENABLED
satish@chromium.orgaa870602010-12-13 17:18:55 +0900405 Logging::GetInstance()->OnSendMessage(message, context_->channel_id());
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900406#endif
407
sergeyu@chromium.org5b6d49c2012-07-03 06:15:52 +0900408 context_->ipc_task_runner()->PostTask(
jhawkins@chromium.orge34d0ad2011-11-29 11:24:28 +0900409 FROM_HERE,
rsleevi@chromium.org997c1d42012-04-28 11:12:00 +0900410 base::Bind(&ChannelProxy::Context::OnSendMessage,
411 context_, base::Passed(scoped_ptr<Message>(message))));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900412 return true;
413}
414
415void ChannelProxy::AddFilter(MessageFilter* filter) {
alexeypa@chromium.org0192ff02012-12-05 10:00:36 +0900416 DCHECK(CalledOnValidThread());
417
jam@chromium.orge57135c2010-12-03 04:16:07 +0900418 context_->AddFilter(filter);
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900419}
420
421void ChannelProxy::RemoveFilter(MessageFilter* filter) {
alexeypa@chromium.org0192ff02012-12-05 10:00:36 +0900422 DCHECK(CalledOnValidThread());
423
sergeyu@chromium.org5b6d49c2012-07-03 06:15:52 +0900424 context_->ipc_task_runner()->PostTask(
jhawkins@chromium.org9827bd12011-11-13 06:16:41 +0900425 FROM_HERE, base::Bind(&Context::OnRemoveFilter, context_.get(),
426 make_scoped_refptr(filter)));
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900427}
428
sergeyu@chromium.org5b6d49c2012-07-03 06:15:52 +0900429void ChannelProxy::ClearIPCTaskRunner() {
alexeypa@chromium.org0192ff02012-12-05 10:00:36 +0900430 DCHECK(CalledOnValidThread());
431
sergeyu@chromium.org5b6d49c2012-07-03 06:15:52 +0900432 context()->ClearIPCTaskRunner();
nsylvain@chromium.orgc12dde32009-07-24 03:17:55 +0900433}
434
dmichael@chromium.orgb798bb42012-06-01 04:37:54 +0900435#if defined(OS_POSIX) && !defined(OS_NACL)
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900436// See the TODO regarding lazy initialization of the channel in
437// ChannelProxy::Init().
phajdan.jr@chromium.orgaf9455b2011-09-20 02:08:12 +0900438int ChannelProxy::GetClientFileDescriptor() {
alexeypa@chromium.org0192ff02012-12-05 10:00:36 +0900439 DCHECK(CalledOnValidThread());
440
phajdan.jr@chromium.orgaf9455b2011-09-20 02:08:12 +0900441 Channel* channel = context_.get()->channel_.get();
dmaclach@chromium.org058c4a72010-12-09 04:28:09 +0900442 // Channel must have been created first.
443 DCHECK(channel) << context_.get()->channel_id_;
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900444 return channel->GetClientFileDescriptor();
445}
wez@chromium.org7cce0912011-04-06 21:01:44 +0900446
phajdan.jr@chromium.orgaf9455b2011-09-20 02:08:12 +0900447int ChannelProxy::TakeClientFileDescriptor() {
alexeypa@chromium.org0192ff02012-12-05 10:00:36 +0900448 DCHECK(CalledOnValidThread());
449
phajdan.jr@chromium.orgaf9455b2011-09-20 02:08:12 +0900450 Channel* channel = context_.get()->channel_.get();
451 // Channel must have been created first.
452 DCHECK(channel) << context_.get()->channel_id_;
453 return channel->TakeClientFileDescriptor();
454}
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900455#endif
456
agl@chromium.org1c6dcf22009-07-23 08:57:21 +0900457//-----------------------------------------------------------------------------
458
459} // namespace IPC