Home | History | Annotate | Download | only in ipc
      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
      3 // found in the LICENSE file.
      4 
      5 #include "ipc/ipc_forwarding_message_filter.h"
      6 
      7 #include "base/bind.h"
      8 #include "base/location.h"
      9 #include "ipc/ipc_message.h"
     10 
     11 namespace IPC {
     12 
     13 ForwardingMessageFilter::ForwardingMessageFilter(
     14     const uint32* message_ids_to_filter,
     15     size_t num_message_ids_to_filter,
     16     base::TaskRunner* target_task_runner)
     17     : target_task_runner_(target_task_runner) {
     18   DCHECK(target_task_runner_.get());
     19   for (size_t i = 0; i < num_message_ids_to_filter; i++)
     20     message_ids_to_filter_.insert(message_ids_to_filter[i]);
     21 }
     22 
     23 void ForwardingMessageFilter::AddRoute(int routing_id, const Handler& handler) {
     24   DCHECK(!handler.is_null());
     25   base::AutoLock locked(handlers_lock_);
     26   handlers_.insert(std::make_pair(routing_id, handler));
     27 }
     28 
     29 void ForwardingMessageFilter::RemoveRoute(int routing_id) {
     30   base::AutoLock locked(handlers_lock_);
     31   handlers_.erase(routing_id);
     32 }
     33 
     34 bool ForwardingMessageFilter::OnMessageReceived(const Message& message) {
     35   if (message_ids_to_filter_.find(message.type()) ==
     36       message_ids_to_filter_.end())
     37     return false;
     38 
     39 
     40   Handler handler;
     41 
     42   {
     43     base::AutoLock locked(handlers_lock_);
     44     std::map<int, Handler>::iterator it = handlers_.find(message.routing_id());
     45     if (it == handlers_.end())
     46       return false;
     47     handler = it->second;
     48   }
     49 
     50   target_task_runner_->PostTask(FROM_HERE, base::Bind(handler, message));
     51   return true;
     52 }
     53 
     54 ForwardingMessageFilter::~ForwardingMessageFilter() {
     55 }
     56 
     57 }  // namespace IPC
     58