Home | History | Annotate | Download | only in child
      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/child/websocket_dispatcher.h"
      6 
      7 #include <stdint.h>
      8 #include <map>
      9 
     10 #include "base/logging.h"
     11 #include "content/child/websocket_bridge.h"
     12 #include "content/common/websocket_messages.h"
     13 #include "ipc/ipc_message.h"
     14 #include "url/gurl.h"
     15 
     16 namespace content {
     17 
     18 WebSocketDispatcher::WebSocketDispatcher() : channel_id_max_(0) {}
     19 
     20 WebSocketDispatcher::~WebSocketDispatcher() {}
     21 
     22 int WebSocketDispatcher::AddBridge(WebSocketBridge* bridge) {
     23   ++channel_id_max_;
     24   bridges_.insert(std::make_pair(channel_id_max_, bridge));
     25   return channel_id_max_;
     26 }
     27 
     28 void WebSocketDispatcher::RemoveBridge(int channel_id) {
     29   std::map<int, WebSocketBridge*>::iterator iter = bridges_.find(channel_id);
     30   if (iter == bridges_.end()) {
     31     DVLOG(1) << "Remove a non-existent bridge(" << channel_id << ")";
     32     return;
     33   }
     34   bridges_.erase(iter);
     35 }
     36 
     37 bool WebSocketDispatcher::OnMessageReceived(const IPC::Message& msg) {
     38   switch (msg.type()) {
     39     case WebSocketMsg_AddChannelResponse::ID:
     40     case WebSocketMsg_NotifyStartOpeningHandshake::ID:
     41     case WebSocketMsg_NotifyFinishOpeningHandshake::ID:
     42     case WebSocketMsg_NotifyFailure::ID:
     43     case WebSocketMsg_SendFrame::ID:
     44     case WebSocketMsg_FlowControl::ID:
     45     case WebSocketMsg_DropChannel::ID:
     46       break;
     47     default:
     48       return false;
     49   }
     50 
     51   WebSocketBridge* bridge = GetBridge(msg.routing_id(), msg.type());
     52   if (!bridge)
     53     return true;
     54   return bridge->OnMessageReceived(msg);
     55 }
     56 
     57 WebSocketBridge* WebSocketDispatcher::GetBridge(int channel_id, uint32 type) {
     58   std::map<int, WebSocketBridge*>::iterator iter = bridges_.find(channel_id);
     59   if (iter == bridges_.end()) {
     60     DVLOG(1) << "No bridge for channel_id=" << channel_id
     61              << ", type=" << type;
     62     return NULL;
     63   }
     64   return iter->second;
     65 }
     66 
     67 }  // namespace content
     68