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   WebSocketDispatcherHost(
     49       int process_id,
     50       const GetRequestContextCallback& get_context_callback);
     51 
     52   // For testing. Specify a factory method that creates mock version of
     53   // WebSocketHost.
     54   WebSocketDispatcherHost(int process_id,
     55                           const GetRequestContextCallback& get_context_callback,
     56                           const WebSocketHostFactory& websocket_host_factory);
     57 
     58   // BrowserMessageFilter:
     59   virtual bool OnMessageReceived(const IPC::Message& message) 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_NotifyClosing IPC
     85   WebSocketHostState NotifyClosingHandshake(int routing_id) WARN_UNUSED_RESULT;
     86 
     87   // Sends a WebSocketMsg_NotifyStartOpeningHandshake IPC.
     88   WebSocketHostState NotifyStartOpeningHandshake(
     89       int routing_id,
     90       const WebSocketHandshakeRequest& request) WARN_UNUSED_RESULT;
     91 
     92   // Sends a WebSocketMsg_NotifyFinishOpeningHandshake IPC.
     93   WebSocketHostState NotifyFinishOpeningHandshake(
     94       int routing_id,
     95       const WebSocketHandshakeResponse& response) WARN_UNUSED_RESULT;
     96 
     97   // Sends a WebSocketMsg_NotifyFailure IPC and deletes and unregisters the
     98   // channel.
     99   WebSocketHostState NotifyFailure(
    100       int routing_id,
    101       const std::string& message) WARN_UNUSED_RESULT;
    102 
    103   // Sends a WebSocketMsg_DropChannel IPC and deletes and unregisters the
    104   // channel.
    105   WebSocketHostState DoDropChannel(
    106       int routing_id,
    107       bool was_clean,
    108       uint16 code,
    109       const std::string& reason) WARN_UNUSED_RESULT;
    110 
    111   // Returns whether the associated renderer process can read raw cookies.
    112   bool CanReadRawCookies() const;
    113 
    114   int render_process_id() const { return process_id_; }
    115 
    116  private:
    117   typedef base::hash_map<int, WebSocketHost*> WebSocketHostTable;
    118 
    119   virtual ~WebSocketDispatcherHost();
    120 
    121   WebSocketHost* CreateWebSocketHost(int routing_id);
    122 
    123   // Looks up a WebSocketHost object by |routing_id|. Returns the object if one
    124   // is found, or NULL otherwise.
    125   WebSocketHost* GetHost(int routing_id) const;
    126 
    127   // Sends the passed in IPC::Message via the BrowserMessageFilter::Send()
    128   // method. If sending the IPC fails, assumes that this connection is no
    129   // longer useable, calls DeleteWebSocketHost(), and returns
    130   // WEBSOCKET_HOST_DELETED. The behaviour is the same for all message types.
    131   WebSocketHostState SendOrDrop(IPC::Message* message) WARN_UNUSED_RESULT;
    132 
    133   // Deletes the WebSocketHost object associated with the given |routing_id| and
    134   // removes it from the |hosts_| table.
    135   void DeleteWebSocketHost(int routing_id);
    136 
    137   // Table of WebSocketHost objects, owned by this object, indexed by
    138   // routing_id.
    139   WebSocketHostTable hosts_;
    140 
    141   // The the process ID of the associated renderer process.
    142   const int process_id_;
    143 
    144   // A callback which returns the appropriate net::URLRequestContext for us to
    145   // use.
    146   GetRequestContextCallback get_context_callback_;
    147 
    148   WebSocketHostFactory websocket_host_factory_;
    149 
    150   DISALLOW_COPY_AND_ASSIGN(WebSocketDispatcherHost);
    151 };
    152 
    153 }  // namespace content
    154 
    155 #endif  // CONTENT_BROWSER_RENDERER_HOST_WEBSOCKET_DISPATCHER_HOST_H_
    156