Home | History | Annotate | Download | only in renderer_host
      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 #ifndef CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_
      6 #define CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/callback.h"
     13 #include "base/compiler_specific.h"
     14 #include "base/containers/hash_tables.h"
     15 #include "content/common/content_export.h"
     16 #include "content/common/websocket.h"
     17 #include "content/public/browser/browser_message_filter.h"
     18 
     19 namespace net {
     20 class URLRequestContext;
     21 }  // namespace net
     22 
     23 namespace content {
     24 
     25 struct WebSocketHandshakeRequest;
     26 struct WebSocketHandshakeResponse;
     27 class WebSocketHost;
     28 
     29 // Creates a WebSocketHost object for each WebSocket channel, and dispatches
     30 // WebSocketMsg_* messages sent from renderer to the appropriate WebSocketHost.
     31 class CONTENT_EXPORT WebSocketDispatcherHost : public BrowserMessageFilter {
     32  public:
     33   typedef base::Callback<net::URLRequestContext*()> GetRequestContextCallback;
     34 
     35   // Given a routing_id, WebSocketHostFactory returns a new instance of
     36   // WebSocketHost or its subclass.
     37   typedef base::Callback<WebSocketHost*(int)> WebSocketHostFactory;  // NOLINT
     38 
     39   // Return value for methods that may delete the WebSocketHost. This enum is
     40   // binary-compatible with net::WebSocketEventInterface::ChannelState, to make
     41   // conversion cheap. By using a separate enum including net/ header files can
     42   // be avoided.
     43   enum WebSocketHostState {
     44     WEBSOCKET_HOST_ALIVE,
     45     WEBSOCKET_HOST_DELETED
     46   };
     47 
     48   explicit WebSocketDispatcherHost(
     49       const GetRequestContextCallback& get_context_callback);
     50 
     51   // For testing. Specify a factory method that creates mock version of
     52   // WebSocketHost.
     53   WebSocketDispatcherHost(
     54       const GetRequestContextCallback& get_context_callback,
     55       const WebSocketHostFactory& websocket_host_factory);
     56 
     57   // BrowserMessageFilter:
     58   virtual bool OnMessageReceived(const IPC::Message& message,
     59                                  bool* message_was_ok) OVERRIDE;
     60 
     61   // The following methods are used by WebSocketHost::EventInterface to send
     62   // IPCs from the browser to the renderer or child process. Any of them may
     63   // return WEBSOCKET_HOST_DELETED and delete the WebSocketHost on failure,
     64   // leading to the WebSocketChannel and EventInterface also being deleted.
     65 
     66   // Sends a WebSocketMsg_AddChannelResponse IPC, and then deletes and
     67   // unregisters the WebSocketHost if |fail| is true.
     68   WebSocketHostState SendAddChannelResponse(
     69       int routing_id,
     70       bool fail,
     71       const std::string& selected_protocol,
     72       const std::string& extensions) WARN_UNUSED_RESULT;
     73 
     74   // Sends a WebSocketMsg_SendFrame IPC.
     75   WebSocketHostState SendFrame(int routing_id,
     76                                bool fin,
     77                                WebSocketMessageType type,
     78                                const std::vector<char>& data);
     79 
     80   // Sends a WebSocketMsg_FlowControl IPC.
     81   WebSocketHostState SendFlowControl(int routing_id,
     82                                      int64 quota) WARN_UNUSED_RESULT;
     83 
     84   // Sends a WebSocketMsg_SendClosing IPC
     85   WebSocketHostState SendClosing(int routing_id) WARN_UNUSED_RESULT;
     86 
     87   // Sends a WebSocketMsg_NotifyStartOpeningHandshake IPC.
     88   WebSocketHostState SendStartOpeningHandshake(
     89       int routing_id,
     90       const WebSocketHandshakeRequest& request) WARN_UNUSED_RESULT;
     91 
     92   // Sends a WebSocketMsg_NotifyFinishOpeningHandshake IPC.
     93   WebSocketHostState SendFinishOpeningHandshake(
     94       int routing_id,
     95       const WebSocketHandshakeResponse& response) WARN_UNUSED_RESULT;
     96 
     97   // Sends a WebSocketMsg_DropChannel IPC and deletes and unregisters the
     98   // channel.
     99   WebSocketHostState DoDropChannel(
    100       int routing_id,
    101       uint16 code,
    102       const std::string& reason) WARN_UNUSED_RESULT;
    103 
    104  private:
    105   typedef base::hash_map<int, WebSocketHost*> WebSocketHostTable;
    106 
    107   virtual ~WebSocketDispatcherHost();
    108 
    109   WebSocketHost* CreateWebSocketHost(int routing_id);
    110 
    111   // Looks up a WebSocketHost object by |routing_id|. Returns the object if one
    112   // is found, or NULL otherwise.
    113   WebSocketHost* GetHost(int routing_id) const;
    114 
    115   // Sends the passed in IPC::Message via the BrowserMessageFilter::Send()
    116   // method. If sending the IPC fails, assumes that this connection is no
    117   // longer useable, calls DeleteWebSocketHost(), and returns
    118   // WEBSOCKET_HOST_DELETED. The behaviour is the same for all message types.
    119   WebSocketHostState SendOrDrop(IPC::Message* message) WARN_UNUSED_RESULT;
    120 
    121   // Deletes the WebSocketHost object associated with the given |routing_id| and
    122   // removes it from the |hosts_| table.
    123   void DeleteWebSocketHost(int routing_id);
    124 
    125   // Table of WebSocketHost objects, owned by this object, indexed by
    126   // routing_id.
    127   WebSocketHostTable hosts_;
    128 
    129   // A callback which returns the appropriate net::URLRequestContext for us to
    130   // use.
    131   GetRequestContextCallback get_context_callback_;
    132 
    133   WebSocketHostFactory websocket_host_factory_;
    134 
    135   DISALLOW_COPY_AND_ASSIGN(WebSocketDispatcherHost);
    136 };
    137 
    138 }  // namespace content
    139 
    140 #endif  // CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_
    141