OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/compiler_specific.h" | 6 #include "base/compiler_specific.h" |
7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 #include "base/location.h" | 8 #include "base/location.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 Init(channel_handle, mode, true); | 294 Init(channel_handle, mode, true); |
295 } | 295 } |
296 | 296 |
297 ChannelProxy::ChannelProxy(Context* context) | 297 ChannelProxy::ChannelProxy(Context* context) |
298 : context_(context), | 298 : context_(context), |
299 outgoing_message_filter_(NULL), | 299 outgoing_message_filter_(NULL), |
300 did_init_(false) { | 300 did_init_(false) { |
301 } | 301 } |
302 | 302 |
303 ChannelProxy::~ChannelProxy() { | 303 ChannelProxy::~ChannelProxy() { |
| 304 DCHECK(CalledOnValidThread()); |
| 305 |
304 Close(); | 306 Close(); |
305 } | 307 } |
306 | 308 |
307 void ChannelProxy::Init(const IPC::ChannelHandle& channel_handle, | 309 void ChannelProxy::Init(const IPC::ChannelHandle& channel_handle, |
308 Channel::Mode mode, | 310 Channel::Mode mode, |
309 bool create_pipe_now) { | 311 bool create_pipe_now) { |
| 312 DCHECK(CalledOnValidThread()); |
310 DCHECK(!did_init_); | 313 DCHECK(!did_init_); |
311 #if defined(OS_POSIX) | 314 #if defined(OS_POSIX) |
312 // When we are creating a server on POSIX, we need its file descriptor | 315 // When we are creating a server on POSIX, we need its file descriptor |
313 // to be created immediately so that it can be accessed and passed | 316 // to be created immediately so that it can be accessed and passed |
314 // to other processes. Forcing it to be created immediately avoids | 317 // to other processes. Forcing it to be created immediately avoids |
315 // race conditions that may otherwise arise. | 318 // race conditions that may otherwise arise. |
316 if (mode & Channel::MODE_SERVER_FLAG) { | 319 if (mode & Channel::MODE_SERVER_FLAG) { |
317 create_pipe_now = true; | 320 create_pipe_now = true; |
318 } | 321 } |
319 #endif // defined(OS_POSIX) | 322 #endif // defined(OS_POSIX) |
(...skipping 11 matching lines...) Expand all Loading... |
331 } | 334 } |
332 | 335 |
333 // complete initialization on the background thread | 336 // complete initialization on the background thread |
334 context_->ipc_task_runner()->PostTask( | 337 context_->ipc_task_runner()->PostTask( |
335 FROM_HERE, base::Bind(&Context::OnChannelOpened, context_.get())); | 338 FROM_HERE, base::Bind(&Context::OnChannelOpened, context_.get())); |
336 | 339 |
337 did_init_ = true; | 340 did_init_ = true; |
338 } | 341 } |
339 | 342 |
340 void ChannelProxy::Close() { | 343 void ChannelProxy::Close() { |
| 344 DCHECK(CalledOnValidThread()); |
| 345 |
341 // Clear the backpointer to the listener so that any pending calls to | 346 // Clear the backpointer to the listener so that any pending calls to |
342 // Context::OnDispatchMessage or OnDispatchError will be ignored. It is | 347 // Context::OnDispatchMessage or OnDispatchError will be ignored. It is |
343 // possible that the channel could be closed while it is receiving messages! | 348 // possible that the channel could be closed while it is receiving messages! |
344 context_->Clear(); | 349 context_->Clear(); |
345 | 350 |
346 if (context_->ipc_task_runner()) { | 351 if (context_->ipc_task_runner()) { |
347 context_->ipc_task_runner()->PostTask( | 352 context_->ipc_task_runner()->PostTask( |
348 FROM_HERE, base::Bind(&Context::OnChannelClosed, context_.get())); | 353 FROM_HERE, base::Bind(&Context::OnChannelClosed, context_.get())); |
349 } | 354 } |
350 } | 355 } |
351 | 356 |
352 bool ChannelProxy::Send(Message* message) { | 357 bool ChannelProxy::Send(Message* message) { |
353 DCHECK(did_init_); | 358 DCHECK(did_init_); |
| 359 |
| 360 // TODO(alexeypa): add DCHECK(CalledOnValidThread()) here. Currently there are |
| 361 // tests that call Send() from a wrong thread. See http://crbug.com/163523. |
354 if (outgoing_message_filter()) | 362 if (outgoing_message_filter()) |
355 message = outgoing_message_filter()->Rewrite(message); | 363 message = outgoing_message_filter()->Rewrite(message); |
356 | 364 |
357 #ifdef IPC_MESSAGE_LOG_ENABLED | 365 #ifdef IPC_MESSAGE_LOG_ENABLED |
358 Logging::GetInstance()->OnSendMessage(message, context_->channel_id()); | 366 Logging::GetInstance()->OnSendMessage(message, context_->channel_id()); |
359 #endif | 367 #endif |
360 | 368 |
361 context_->ipc_task_runner()->PostTask( | 369 context_->ipc_task_runner()->PostTask( |
362 FROM_HERE, | 370 FROM_HERE, |
363 base::Bind(&ChannelProxy::Context::OnSendMessage, | 371 base::Bind(&ChannelProxy::Context::OnSendMessage, |
364 context_, base::Passed(scoped_ptr<Message>(message)))); | 372 context_, base::Passed(scoped_ptr<Message>(message)))); |
365 return true; | 373 return true; |
366 } | 374 } |
367 | 375 |
368 void ChannelProxy::AddFilter(MessageFilter* filter) { | 376 void ChannelProxy::AddFilter(MessageFilter* filter) { |
| 377 DCHECK(CalledOnValidThread()); |
| 378 |
369 context_->AddFilter(filter); | 379 context_->AddFilter(filter); |
370 } | 380 } |
371 | 381 |
372 void ChannelProxy::RemoveFilter(MessageFilter* filter) { | 382 void ChannelProxy::RemoveFilter(MessageFilter* filter) { |
| 383 DCHECK(CalledOnValidThread()); |
| 384 |
373 context_->ipc_task_runner()->PostTask( | 385 context_->ipc_task_runner()->PostTask( |
374 FROM_HERE, base::Bind(&Context::OnRemoveFilter, context_.get(), | 386 FROM_HERE, base::Bind(&Context::OnRemoveFilter, context_.get(), |
375 make_scoped_refptr(filter))); | 387 make_scoped_refptr(filter))); |
376 } | 388 } |
377 | 389 |
378 void ChannelProxy::ClearIPCTaskRunner() { | 390 void ChannelProxy::ClearIPCTaskRunner() { |
| 391 DCHECK(CalledOnValidThread()); |
| 392 |
379 context()->ClearIPCTaskRunner(); | 393 context()->ClearIPCTaskRunner(); |
380 } | 394 } |
381 | 395 |
382 #if defined(OS_POSIX) && !defined(OS_NACL) | 396 #if defined(OS_POSIX) && !defined(OS_NACL) |
383 // See the TODO regarding lazy initialization of the channel in | 397 // See the TODO regarding lazy initialization of the channel in |
384 // ChannelProxy::Init(). | 398 // ChannelProxy::Init(). |
385 int ChannelProxy::GetClientFileDescriptor() { | 399 int ChannelProxy::GetClientFileDescriptor() { |
| 400 DCHECK(CalledOnValidThread()); |
| 401 |
386 Channel* channel = context_.get()->channel_.get(); | 402 Channel* channel = context_.get()->channel_.get(); |
387 // Channel must have been created first. | 403 // Channel must have been created first. |
388 DCHECK(channel) << context_.get()->channel_id_; | 404 DCHECK(channel) << context_.get()->channel_id_; |
389 return channel->GetClientFileDescriptor(); | 405 return channel->GetClientFileDescriptor(); |
390 } | 406 } |
391 | 407 |
392 int ChannelProxy::TakeClientFileDescriptor() { | 408 int ChannelProxy::TakeClientFileDescriptor() { |
| 409 DCHECK(CalledOnValidThread()); |
| 410 |
393 Channel* channel = context_.get()->channel_.get(); | 411 Channel* channel = context_.get()->channel_.get(); |
394 // Channel must have been created first. | 412 // Channel must have been created first. |
395 DCHECK(channel) << context_.get()->channel_id_; | 413 DCHECK(channel) << context_.get()->channel_id_; |
396 return channel->TakeClientFileDescriptor(); | 414 return channel->TakeClientFileDescriptor(); |
397 } | 415 } |
398 | 416 |
399 bool ChannelProxy::GetClientEuid(uid_t* client_euid) const { | 417 bool ChannelProxy::GetClientEuid(uid_t* client_euid) const { |
| 418 DCHECK(CalledOnValidThread()); |
| 419 |
400 Channel* channel = context_.get()->channel_.get(); | 420 Channel* channel = context_.get()->channel_.get(); |
401 // Channel must have been created first. | 421 // Channel must have been created first. |
402 DCHECK(channel) << context_.get()->channel_id_; | 422 DCHECK(channel) << context_.get()->channel_id_; |
403 return channel->GetClientEuid(client_euid); | 423 return channel->GetClientEuid(client_euid); |
404 } | 424 } |
405 #endif | 425 #endif |
406 | 426 |
407 //----------------------------------------------------------------------------- | 427 //----------------------------------------------------------------------------- |
408 | 428 |
409 } // namespace IPC | 429 } // namespace IPC |
OLD | NEW |