This source file includes following definitions.
- AddContext
- QueueMessage
- QueueReply
- DispatchMessagesTask
- DispatchMessages
- RemoveContext
- dispatch_event
- listener_task_runner
- DispatchReplies
- top_send_done_watcher
- set_top_send_done_watcher
- ReceivedSyncMsgQueue
- restrict_dispatch_group_
- Push
- Pop
- GetSendDoneEvent
- GetDispatchEvent
- DispatchMessages
- TryToUnblockListener
- Clear
- OnMessageReceived
- OnChannelError
- OnChannelOpened
- OnChannelClosed
- OnSendTimeout
- CancelPendingSends
- OnWaitableEventSignaled
- MakeWaitableEventCallback
- sync_messages_with_no_timeout_allowed_
- sync_messages_with_no_timeout_allowed_
- SetRestrictDispatchChannelGroup
- Send
- SendWithTimeout
- WaitForReply
- WaitForReplyWithNestedMessageLoop
- OnWaitableEventSignaled
- StartWatching
#include "ipc/ipc_sync_channel.h"
#include "base/bind.h"
#include "base/debug/trace_event.h"
#include "base/lazy_instance.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/synchronization/waitable_event.h"
#include "base/synchronization/waitable_event_watcher.h"
#include "base/thread_task_runner_handle.h"
#include "base/threading/thread_local.h"
#include "ipc/ipc_logging.h"
#include "ipc/ipc_message_macros.h"
#include "ipc/ipc_sync_message.h"
using base::TimeDelta;
using base::TimeTicks;
using base::WaitableEvent;
namespace IPC {
class SyncChannel::ReceivedSyncMsgQueue :
public base::RefCountedThreadSafe<ReceivedSyncMsgQueue> {
public:
static ReceivedSyncMsgQueue* AddContext() {
ReceivedSyncMsgQueue* rv = lazy_tls_ptr_.Pointer()->Get();
if (!rv) {
rv = new ReceivedSyncMsgQueue();
ReceivedSyncMsgQueue::lazy_tls_ptr_.Pointer()->Set(rv);
}
rv->listener_count_++;
return rv;
}
void QueueMessage(const Message& msg, SyncChannel::SyncContext* context) {
bool was_task_pending;
{
base::AutoLock auto_lock(message_lock_);
was_task_pending = task_pending_;
task_pending_ = true;
message_queue_.push_back(QueuedMessage(new Message(msg), context));
message_queue_version_++;
}
dispatch_event_.Signal();
if (!was_task_pending) {
listener_task_runner_->PostTask(
FROM_HERE, base::Bind(&ReceivedSyncMsgQueue::DispatchMessagesTask,
this, scoped_refptr<SyncContext>(context)));
}
}
void QueueReply(const Message &msg, SyncChannel::SyncContext* context) {
received_replies_.push_back(QueuedMessage(new Message(msg), context));
}
void DispatchMessagesTask(SyncContext* context) {
{
base::AutoLock auto_lock(message_lock_);
task_pending_ = false;
}
context->DispatchMessages();
}
void DispatchMessages(SyncContext* dispatching_context) {
bool first_time = true;
uint32 expected_version = 0;
SyncMessageQueue::iterator it;
while (true) {
Message* message = NULL;
scoped_refptr<SyncChannel::SyncContext> context;
{
base::AutoLock auto_lock(message_lock_);
if (first_time || message_queue_version_ != expected_version) {
it = message_queue_.begin();
first_time = false;
}
for (; it != message_queue_.end(); it++) {
int message_group = it->context->restrict_dispatch_group();
if (message_group == kRestrictDispatchGroup_None ||
message_group == dispatching_context->restrict_dispatch_group()) {
message = it->message;
context = it->context;
it = message_queue_.erase(it);
message_queue_version_++;
expected_version = message_queue_version_;
break;
}
}
}
if (message == NULL)
break;
context->OnDispatchMessage(*message);
delete message;
}
}
void RemoveContext(SyncContext* context) {
base::AutoLock auto_lock(message_lock_);
SyncMessageQueue::iterator iter = message_queue_.begin();
while (iter != message_queue_.end()) {
if (iter->context.get() == context) {
delete iter->message;
iter = message_queue_.erase(iter);
message_queue_version_++;
} else {
iter++;
}
}
if (--listener_count_ == 0) {
DCHECK(lazy_tls_ptr_.Pointer()->Get());
lazy_tls_ptr_.Pointer()->Set(NULL);
}
}
WaitableEvent* dispatch_event() { return &dispatch_event_; }
base::SingleThreadTaskRunner* listener_task_runner() {
return listener_task_runner_.get();
}
static base::LazyInstance<base::ThreadLocalPointer<ReceivedSyncMsgQueue> >
lazy_tls_ptr_;
void DispatchReplies() {
for (size_t i = 0; i < received_replies_.size(); ++i) {
Message* message = received_replies_[i].message;
if (received_replies_[i].context->TryToUnblockListener(message)) {
delete message;
received_replies_.erase(received_replies_.begin() + i);
return;
}
}
}
base::WaitableEventWatcher* top_send_done_watcher() {
return top_send_done_watcher_;
}
void set_top_send_done_watcher(base::WaitableEventWatcher* watcher) {
top_send_done_watcher_ = watcher;
}
private:
friend class base::RefCountedThreadSafe<ReceivedSyncMsgQueue>;
ReceivedSyncMsgQueue() :
message_queue_version_(0),
dispatch_event_(true, false),
listener_task_runner_(base::ThreadTaskRunnerHandle::Get()),
task_pending_(false),
listener_count_(0),
top_send_done_watcher_(NULL) {
}
~ReceivedSyncMsgQueue() {}
struct QueuedMessage {
QueuedMessage(Message* m, SyncContext* c) : message(m), context(c) { }
Message* message;
scoped_refptr<SyncChannel::SyncContext> context;
};
typedef std::list<QueuedMessage> SyncMessageQueue;
SyncMessageQueue message_queue_;
uint32 message_queue_version_;
std::vector<QueuedMessage> received_replies_;
WaitableEvent dispatch_event_;
scoped_refptr<base::SingleThreadTaskRunner> listener_task_runner_;
base::Lock message_lock_;
bool task_pending_;
int listener_count_;
base::WaitableEventWatcher* top_send_done_watcher_;
};
base::LazyInstance<base::ThreadLocalPointer<SyncChannel::ReceivedSyncMsgQueue> >
SyncChannel::ReceivedSyncMsgQueue::lazy_tls_ptr_ =
LAZY_INSTANCE_INITIALIZER;
SyncChannel::SyncContext::SyncContext(
Listener* listener,
base::SingleThreadTaskRunner* ipc_task_runner,
WaitableEvent* shutdown_event)
: ChannelProxy::Context(listener, ipc_task_runner),
received_sync_msgs_(ReceivedSyncMsgQueue::AddContext()),
shutdown_event_(shutdown_event),
restrict_dispatch_group_(kRestrictDispatchGroup_None) {
}
SyncChannel::SyncContext::~SyncContext() {
while (!deserializers_.empty())
Pop();
}
void SyncChannel::SyncContext::Push(SyncMessage* sync_msg) {
PendingSyncMsg pending(SyncMessage::GetMessageId(*sync_msg),
sync_msg->GetReplyDeserializer(),
new WaitableEvent(true, false));
base::AutoLock auto_lock(deserializers_lock_);
deserializers_.push_back(pending);
}
bool SyncChannel::SyncContext::Pop() {
bool result;
{
base::AutoLock auto_lock(deserializers_lock_);
PendingSyncMsg msg = deserializers_.back();
delete msg.deserializer;
delete msg.done_event;
msg.done_event = NULL;
deserializers_.pop_back();
result = msg.send_result;
}
ipc_task_runner()->PostTask(
FROM_HERE, base::Bind(&ReceivedSyncMsgQueue::DispatchReplies,
received_sync_msgs_.get()));
return result;
}
WaitableEvent* SyncChannel::SyncContext::GetSendDoneEvent() {
base::AutoLock auto_lock(deserializers_lock_);
return deserializers_.back().done_event;
}
WaitableEvent* SyncChannel::SyncContext::GetDispatchEvent() {
return received_sync_msgs_->dispatch_event();
}
void SyncChannel::SyncContext::DispatchMessages() {
received_sync_msgs_->DispatchMessages(this);
}
bool SyncChannel::SyncContext::TryToUnblockListener(const Message* msg) {
base::AutoLock auto_lock(deserializers_lock_);
if (deserializers_.empty() ||
!SyncMessage::IsMessageReplyTo(*msg, deserializers_.back().id)) {
return false;
}
if (!msg->is_reply_error()) {
bool send_result = deserializers_.back().deserializer->
SerializeOutputParameters(*msg);
deserializers_.back().send_result = send_result;
VLOG_IF(1, !send_result) << "Couldn't deserialize reply message";
} else {
VLOG(1) << "Received error reply";
}
deserializers_.back().done_event->Signal();
return true;
}
void SyncChannel::SyncContext::Clear() {
CancelPendingSends();
received_sync_msgs_->RemoveContext(this);
Context::Clear();
}
bool SyncChannel::SyncContext::OnMessageReceived(const Message& msg) {
if (TryFilters(msg))
return true;
if (TryToUnblockListener(&msg))
return true;
if (msg.is_reply()) {
received_sync_msgs_->QueueReply(msg, this);
return true;
}
if (msg.should_unblock()) {
received_sync_msgs_->QueueMessage(msg, this);
return true;
}
return Context::OnMessageReceivedNoFilter(msg);
}
void SyncChannel::SyncContext::OnChannelError() {
CancelPendingSends();
shutdown_watcher_.StopWatching();
Context::OnChannelError();
}
void SyncChannel::SyncContext::OnChannelOpened() {
shutdown_watcher_.StartWatching(
shutdown_event_,
base::Bind(&SyncChannel::SyncContext::OnWaitableEventSignaled,
base::Unretained(this)));
Context::OnChannelOpened();
}
void SyncChannel::SyncContext::OnChannelClosed() {
CancelPendingSends();
shutdown_watcher_.StopWatching();
Context::OnChannelClosed();
}
void SyncChannel::SyncContext::OnSendTimeout(int message_id) {
base::AutoLock auto_lock(deserializers_lock_);
PendingSyncMessageQueue::iterator iter;
VLOG(1) << "Send timeout";
for (iter = deserializers_.begin(); iter != deserializers_.end(); iter++) {
if (iter->id == message_id) {
iter->done_event->Signal();
break;
}
}
}
void SyncChannel::SyncContext::CancelPendingSends() {
base::AutoLock auto_lock(deserializers_lock_);
PendingSyncMessageQueue::iterator iter;
VLOG(1) << "Canceling pending sends";
for (iter = deserializers_.begin(); iter != deserializers_.end(); iter++)
iter->done_event->Signal();
}
void SyncChannel::SyncContext::OnWaitableEventSignaled(WaitableEvent* event) {
if (event == shutdown_event_) {
CancelPendingSends();
} else {
DCHECK_EQ(GetSendDoneEvent(), event);
base::MessageLoop::current()->QuitNow();
}
}
base::WaitableEventWatcher::EventCallback
SyncChannel::SyncContext::MakeWaitableEventCallback() {
return base::Bind(&SyncChannel::SyncContext::OnWaitableEventSignaled, this);
}
SyncChannel::SyncChannel(
const IPC::ChannelHandle& channel_handle,
Channel::Mode mode,
Listener* listener,
base::SingleThreadTaskRunner* ipc_task_runner,
bool create_pipe_now,
WaitableEvent* shutdown_event)
: ChannelProxy(new SyncContext(listener, ipc_task_runner, shutdown_event)),
sync_messages_with_no_timeout_allowed_(true) {
ChannelProxy::Init(channel_handle, mode, create_pipe_now);
StartWatching();
}
SyncChannel::SyncChannel(
Listener* listener,
base::SingleThreadTaskRunner* ipc_task_runner,
WaitableEvent* shutdown_event)
: ChannelProxy(new SyncContext(listener, ipc_task_runner, shutdown_event)),
sync_messages_with_no_timeout_allowed_(true) {
StartWatching();
}
SyncChannel::~SyncChannel() {
}
void SyncChannel::SetRestrictDispatchChannelGroup(int group) {
sync_context()->set_restrict_dispatch_group(group);
}
bool SyncChannel::Send(Message* message) {
return SendWithTimeout(message, base::kNoTimeout);
}
bool SyncChannel::SendWithTimeout(Message* message, int timeout_ms) {
#ifdef IPC_MESSAGE_LOG_ENABLED
Logging* logger = Logging::GetInstance();
std::string name;
logger->GetMessageText(message->type(), &name, message, NULL);
TRACE_EVENT1("toplevel", "SyncChannel::SendWithTimeout",
"name", name);
#else
TRACE_EVENT2("toplevel", "SyncChannel::SendWithTimeout",
"class", IPC_MESSAGE_ID_CLASS(message->type()),
"line", IPC_MESSAGE_ID_LINE(message->type()));
#endif
if (!message->is_sync()) {
ChannelProxy::Send(message);
return true;
}
scoped_refptr<SyncContext> context(sync_context());
if (context->shutdown_event()->IsSignaled()) {
VLOG(1) << "shutdown event is signaled";
delete message;
return false;
}
DCHECK(sync_messages_with_no_timeout_allowed_ ||
timeout_ms != base::kNoTimeout);
SyncMessage* sync_msg = static_cast<SyncMessage*>(message);
context->Push(sync_msg);
int message_id = SyncMessage::GetMessageId(*sync_msg);
WaitableEvent* pump_messages_event = sync_msg->pump_messages_event();
ChannelProxy::Send(message);
if (timeout_ms != base::kNoTimeout) {
context->ipc_task_runner()->PostDelayedTask(
FROM_HERE,
base::Bind(&SyncContext::OnSendTimeout, context.get(), message_id),
base::TimeDelta::FromMilliseconds(timeout_ms));
}
WaitForReply(context.get(), pump_messages_event);
return context->Pop();
}
void SyncChannel::WaitForReply(
SyncContext* context, WaitableEvent* pump_messages_event) {
context->DispatchMessages();
while (true) {
WaitableEvent* objects[] = {
context->GetDispatchEvent(),
context->GetSendDoneEvent(),
pump_messages_event
};
unsigned count = pump_messages_event ? 3: 2;
size_t result = WaitableEvent::WaitMany(objects, count);
if (result == 0 ) {
context->GetDispatchEvent()->Reset();
context->DispatchMessages();
continue;
}
if (result == 2 )
WaitForReplyWithNestedMessageLoop(context);
break;
}
}
void SyncChannel::WaitForReplyWithNestedMessageLoop(SyncContext* context) {
base::WaitableEventWatcher send_done_watcher;
ReceivedSyncMsgQueue* sync_msg_queue = context->received_sync_msgs();
DCHECK(sync_msg_queue != NULL);
base::WaitableEventWatcher* old_send_done_event_watcher =
sync_msg_queue->top_send_done_watcher();
base::WaitableEventWatcher::EventCallback old_callback;
base::WaitableEvent* old_event = NULL;
if (old_send_done_event_watcher) {
old_callback = old_send_done_event_watcher->callback();
old_event = old_send_done_event_watcher->GetWatchedEvent();
old_send_done_event_watcher->StopWatching();
}
sync_msg_queue->set_top_send_done_watcher(&send_done_watcher);
send_done_watcher.StartWatching(context->GetSendDoneEvent(),
context->MakeWaitableEventCallback());
{
base::MessageLoop::ScopedNestableTaskAllower allow(
base::MessageLoop::current());
base::MessageLoop::current()->Run();
}
sync_msg_queue->set_top_send_done_watcher(old_send_done_event_watcher);
if (old_send_done_event_watcher && old_event) {
old_send_done_event_watcher->StartWatching(old_event, old_callback);
}
}
void SyncChannel::OnWaitableEventSignaled(WaitableEvent* event) {
DCHECK(event == sync_context()->GetDispatchEvent());
event->Reset();
dispatch_watcher_.StartWatching(event, dispatch_watcher_callback_);
sync_context()->DispatchMessages();
}
void SyncChannel::StartWatching() {
dispatch_watcher_callback_ =
base::Bind(&SyncChannel::OnWaitableEventSignaled,
base::Unretained(this));
dispatch_watcher_.StartWatching(sync_context()->GetDispatchEvent(),
dispatch_watcher_callback_);
}
}