Home | History | Annotate | Download | only in renderer_host
      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 #ifndef CONTENT_BROWSER_RENDERER_HOST_SOCKET_STREAM_DISPATCHER_HOST_H_
      6 #define CONTENT_BROWSER_RENDERER_HOST_SOCKET_STREAM_DISPATCHER_HOST_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/callback_forward.h"
     11 #include "base/id_map.h"
     12 #include "base/memory/weak_ptr.h"
     13 #include "content/browser/ssl/ssl_error_handler.h"
     14 #include "content/public/browser/browser_message_filter.h"
     15 #include "net/socket_stream/socket_stream.h"
     16 
     17 class GURL;
     18 
     19 namespace net {
     20 class SSLInfo;
     21 }
     22 
     23 namespace content {
     24 class ResourceContext;
     25 class SocketStreamHost;
     26 
     27 // Dispatches ViewHostMsg_SocketStream_* messages sent from renderer.
     28 // It also acts as SocketStream::Delegate so that it sends
     29 // ViewMsg_SocketStream_* messages back to renderer.
     30 class SocketStreamDispatcherHost
     31     : public BrowserMessageFilter,
     32       public net::SocketStream::Delegate,
     33       public SSLErrorHandler::Delegate {
     34  public:
     35   typedef base::Callback<net::URLRequestContext*(ResourceType::Type)>
     36       GetRequestContextCallback;
     37   SocketStreamDispatcherHost(
     38       int render_process_id,
     39       const GetRequestContextCallback& request_context_callback,
     40       ResourceContext* resource_context);
     41 
     42   // BrowserMessageFilter:
     43   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     44 
     45   // Make this object inactive.
     46   // Remove all active SocketStreamHost objects.
     47   void Shutdown();
     48 
     49   // SocketStream::Delegate:
     50   virtual void OnConnected(net::SocketStream* socket,
     51                            int max_pending_send_allowed) OVERRIDE;
     52   virtual void OnSentData(net::SocketStream* socket, int amount_sent) OVERRIDE;
     53   virtual void OnReceivedData(net::SocketStream* socket,
     54                               const char* data, int len) OVERRIDE;
     55   virtual void OnClose(net::SocketStream* socket) OVERRIDE;
     56   virtual void OnError(const net::SocketStream* socket, int error) OVERRIDE;
     57   virtual void OnSSLCertificateError(net::SocketStream* socket,
     58                                      const net::SSLInfo& ssl_info,
     59                                      bool fatal) OVERRIDE;
     60   virtual bool CanGetCookies(net::SocketStream* socket,
     61                              const GURL& url) OVERRIDE;
     62   virtual bool CanSetCookie(net::SocketStream* request,
     63                             const GURL& url,
     64                             const std::string& cookie_line,
     65                             net::CookieOptions* options) OVERRIDE;
     66 
     67   // SSLErrorHandler::Delegate methods:
     68   virtual void CancelSSLRequest(const GlobalRequestID& id,
     69                                 int error,
     70                                 const net::SSLInfo* ssl_info) OVERRIDE;
     71   virtual void ContinueSSLRequest(const GlobalRequestID& id) OVERRIDE;
     72 
     73  protected:
     74   virtual ~SocketStreamDispatcherHost();
     75 
     76  private:
     77   // Message handlers called by OnMessageReceived.
     78   void OnConnect(int render_frame_id, const GURL& url, int socket_id);
     79   void OnSendData(int socket_id, const std::vector<char>& data);
     80   void OnCloseReq(int socket_id);
     81 
     82   void DeleteSocketStreamHost(int socket_id);
     83 
     84   net::URLRequestContext* GetURLRequestContext();
     85 
     86   IDMap<SocketStreamHost> hosts_;
     87   int render_process_id_;
     88   GetRequestContextCallback request_context_callback_;
     89   ResourceContext* resource_context_;
     90 
     91   base::WeakPtrFactory<SocketStreamDispatcherHost> weak_ptr_factory_;
     92   bool on_shutdown_;
     93 
     94   DISALLOW_COPY_AND_ASSIGN(SocketStreamDispatcherHost);
     95 };
     96 
     97 }  // namespace content
     98 
     99 #endif  // CONTENT_BROWSER_RENDERER_HOST_SOCKET_STREAM_DISPATCHER_HOST_H_
    100