Home | History | Annotate | Download | only in chrome_frame
      1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome_frame/sync_msg_reply_dispatcher.h"
      6 
      7 #include "ipc/ipc_sync_message.h"
      8 
      9 void SyncMessageReplyDispatcher::Push(IPC::SyncMessage* msg,
     10                                       SyncMessageCallContext* context,
     11                                       void* key) {
     12   if (context) {
     13     context->message_type_ = msg->type();
     14     context->id_ = IPC::SyncMessage::GetMessageId(*msg);
     15     context->key_ = key;
     16 
     17     base::AutoLock lock(message_queue_lock_);
     18     message_queue_.push_back(context);
     19   }
     20 }
     21 
     22 bool SyncMessageReplyDispatcher::HandleMessageType(
     23     const IPC::Message& msg, SyncMessageCallContext* context) {
     24   return false;
     25 }
     26 
     27 bool SyncMessageReplyDispatcher::OnMessageReceived(const IPC::Message& msg) {
     28   SyncMessageCallContext* context = GetContext(msg);
     29   // No context e.g. no return values and/or don't care
     30   if (!context) {
     31     return false;
     32   }
     33 
     34   return HandleMessageType(msg, context);
     35 }
     36 
     37 void SyncMessageReplyDispatcher::Cancel(void* key) {
     38   DCHECK(key != NULL);
     39   base::AutoLock lock(message_queue_lock_);
     40   PendingSyncMessageQueue::iterator it = message_queue_.begin();
     41   while (it != message_queue_.end()) {
     42     SyncMessageCallContext* context = *it;
     43     if (context->key_ == key) {
     44       it = message_queue_.erase(it);
     45       delete context;
     46     } else {
     47       ++it;
     48     }
     49   }
     50 }
     51 
     52 SyncMessageReplyDispatcher::SyncMessageCallContext*
     53     SyncMessageReplyDispatcher::GetContext(const IPC::Message& msg) {
     54   if (!msg.is_reply())
     55     return NULL;
     56 
     57   int id = IPC::SyncMessage::GetMessageId(msg);
     58   base::AutoLock lock(message_queue_lock_);
     59   PendingSyncMessageQueue::iterator it;
     60   for (it = message_queue_.begin(); it != message_queue_.end(); ++it) {
     61     SyncMessageCallContext* context = *it;
     62     if (context->id_ == id) {
     63       message_queue_.erase(it);
     64       return context;
     65     }
     66   }
     67   return NULL;
     68 }
     69