Home | History | Annotate | Download | only in browser
      1 // Copyright 2013 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 "content/browser/message_port_message_filter.h"
      6 
      7 #include "content/browser/message_port_service.h"
      8 #include "content/common/message_port_messages.h"
      9 
     10 namespace content {
     11 
     12 MessagePortMessageFilter::MessagePortMessageFilter(
     13     const NextRoutingIDCallback& callback)
     14     : BrowserMessageFilter(MessagePortMsgStart),
     15       next_routing_id_(callback) {
     16 }
     17 
     18 MessagePortMessageFilter::~MessagePortMessageFilter() {
     19 }
     20 
     21 void MessagePortMessageFilter::OnChannelClosing() {
     22   MessagePortService::GetInstance()->OnMessagePortMessageFilterClosing(this);
     23 }
     24 
     25 bool MessagePortMessageFilter::OnMessageReceived(const IPC::Message& message) {
     26   bool handled = true;
     27   IPC_BEGIN_MESSAGE_MAP(MessagePortMessageFilter, message)
     28     IPC_MESSAGE_HANDLER(MessagePortHostMsg_CreateMessagePort,
     29                         OnCreateMessagePort)
     30     IPC_MESSAGE_FORWARD(MessagePortHostMsg_DestroyMessagePort,
     31                         MessagePortService::GetInstance(),
     32                         MessagePortService::Destroy)
     33     IPC_MESSAGE_FORWARD(MessagePortHostMsg_Entangle,
     34                         MessagePortService::GetInstance(),
     35                         MessagePortService::Entangle)
     36     IPC_MESSAGE_FORWARD(MessagePortHostMsg_PostMessage,
     37                         MessagePortService::GetInstance(),
     38                         MessagePortService::PostMessage)
     39     IPC_MESSAGE_FORWARD(MessagePortHostMsg_QueueMessages,
     40                         MessagePortService::GetInstance(),
     41                         MessagePortService::QueueMessages)
     42     IPC_MESSAGE_FORWARD(MessagePortHostMsg_SendQueuedMessages,
     43                         MessagePortService::GetInstance(),
     44                         MessagePortService::SendQueuedMessages)
     45     IPC_MESSAGE_UNHANDLED(handled = false)
     46   IPC_END_MESSAGE_MAP()
     47 
     48   return handled;
     49 }
     50 
     51 void MessagePortMessageFilter::OnDestruct() const {
     52   BrowserThread::DeleteOnIOThread::Destruct(this);
     53 }
     54 
     55 int MessagePortMessageFilter::GetNextRoutingID() {
     56   return next_routing_id_.Run();
     57 }
     58 
     59 void MessagePortMessageFilter::UpdateMessagePortsWithNewRoutes(
     60     const std::vector<int>& message_port_ids,
     61     std::vector<int>* new_routing_ids) {
     62   DCHECK(new_routing_ids);
     63   new_routing_ids->clear();
     64   new_routing_ids->resize(message_port_ids.size());
     65 
     66   for (size_t i = 0; i < message_port_ids.size(); ++i) {
     67     (*new_routing_ids)[i] = GetNextRoutingID();
     68     MessagePortService::GetInstance()->UpdateMessagePort(
     69         message_port_ids[i],
     70         this,
     71         (*new_routing_ids)[i]);
     72   }
     73 }
     74 
     75 void MessagePortMessageFilter::OnCreateMessagePort(int *route_id,
     76                                                    int* message_port_id) {
     77   *route_id = next_routing_id_.Run();
     78   MessagePortService::GetInstance()->Create(*route_id, this, message_port_id);
     79 }
     80 
     81 }  // namespace content
     82