Home | History | Annotate | Download | only in p2p
      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_RENDERER_P2P_PORT_ALLOCATOR_H_
      6 #define CONTENT_RENDERER_P2P_PORT_ALLOCATOR_H_
      7 
      8 #include "base/memory/ref_counted.h"
      9 #include "base/memory/scoped_ptr.h"
     10 #include "net/base/net_util.h"
     11 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
     12 #include "third_party/libjingle/source/talk/p2p/client/basicportallocator.h"
     13 
     14 namespace blink {
     15 class WebFrame;
     16 class WebURLLoader;
     17 }  // namespace blink
     18 
     19 namespace content {
     20 
     21 class P2PHostAddressRequest;
     22 class P2PPortAllocatorSession;
     23 class P2PSocketDispatcher;
     24 
     25 // TODO(sergeyu): There is overlap between this class and HttpPortAllocator.
     26 // Refactor this class to inherit from HttpPortAllocator to avoid code
     27 // duplication.
     28 class P2PPortAllocator : public cricket::BasicPortAllocator {
     29  public:
     30   struct Config {
     31     Config();
     32     ~Config();
     33 
     34     struct RelayServerConfig {
     35       RelayServerConfig();
     36       ~RelayServerConfig();
     37 
     38       std::string username;
     39       std::string password;
     40       std::string server_address;
     41       int port;
     42       std::string transport_type;
     43       bool secure;
     44     };
     45 
     46     // STUN server address and port.
     47     std::string stun_server;
     48     int stun_server_port;
     49 
     50     std::vector<RelayServerConfig> relays;
     51 
     52     bool legacy_relay;
     53     // Disable TCP-based transport when set to true.
     54     bool disable_tcp_transport;
     55   };
     56 
     57   P2PPortAllocator(blink::WebFrame* web_frame,
     58                    P2PSocketDispatcher* socket_dispatcher,
     59                    talk_base::NetworkManager* network_manager,
     60                    talk_base::PacketSocketFactory* socket_factory,
     61                    const Config& config);
     62   virtual ~P2PPortAllocator();
     63 
     64   virtual cricket::PortAllocatorSession* CreateSessionInternal(
     65       const std::string& content_name,
     66       int component,
     67       const std::string& ice_username_fragment,
     68       const std::string& ice_password) OVERRIDE;
     69 
     70  private:
     71   friend class P2PPortAllocatorSession;
     72 
     73   blink::WebFrame* web_frame_;
     74   P2PSocketDispatcher* socket_dispatcher_;
     75   Config config_;
     76 
     77   DISALLOW_COPY_AND_ASSIGN(P2PPortAllocator);
     78 };
     79 
     80 class P2PPortAllocatorSession : public cricket::BasicPortAllocatorSession,
     81                                 public blink::WebURLLoaderClient  {
     82  public:
     83   P2PPortAllocatorSession(
     84       P2PPortAllocator* allocator,
     85       const std::string& content_name,
     86       int component,
     87       const std::string& ice_username_fragment,
     88       const std::string& ice_password);
     89   virtual ~P2PPortAllocatorSession();
     90 
     91   // blink::WebURLLoaderClient overrides.
     92   virtual void didReceiveData(blink::WebURLLoader* loader,
     93                               const char* data,
     94                               int data_length,
     95                               int encoded_data_length) OVERRIDE;
     96   virtual void didFinishLoading(blink::WebURLLoader* loader,
     97                                 double finish_time) OVERRIDE;
     98   virtual void didFail(blink::WebURLLoader* loader,
     99                        const blink::WebURLError& error) OVERRIDE;
    100 
    101  protected:
    102   // Overrides for cricket::BasicPortAllocatorSession.
    103   virtual void GetPortConfigurations() OVERRIDE;
    104 
    105  private:
    106 
    107   struct RelayServer {
    108     RelayServer();
    109     ~RelayServer();
    110 
    111     P2PPortAllocator::Config::RelayServerConfig config;
    112     talk_base::SocketAddress resolved_relay_address;
    113     scoped_refptr<P2PHostAddressRequest> relay_address_request;
    114   };
    115 
    116   void ResolveStunServerAddress();
    117   void OnStunServerAddress(const net::IPAddressNumber& address);
    118 
    119   void ResolveRelayServerAddresses();
    120   void OnRelayServerAddressResolved(size_t index,
    121                                     const net::IPAddressNumber& address);
    122   bool IsRelayAddressesResolved() const;
    123 
    124   // This method allocates non-TURN relay sessions.
    125   void AllocateLegacyRelaySession();
    126   void ParseRelayResponse();
    127 
    128   void AddConfig();
    129 
    130   P2PPortAllocator* allocator_;
    131 
    132   scoped_refptr<P2PHostAddressRequest> stun_address_request_;
    133   talk_base::SocketAddress stun_server_address_;
    134 
    135   std::vector<RelayServer> relay_info_;
    136 
    137   scoped_ptr<blink::WebURLLoader> relay_session_request_;
    138   int relay_session_attempts_;
    139   std::string relay_session_response_;
    140   talk_base::SocketAddress relay_ip_;
    141   int relay_udp_port_;
    142   int relay_tcp_port_;
    143   int relay_ssltcp_port_;
    144   int pending_relay_requests_;
    145 
    146   DISALLOW_COPY_AND_ASSIGN(P2PPortAllocatorSession);
    147 };
    148 
    149 }  // namespace content
    150 
    151 #endif  // CONTENT_RENDERER_P2P_PORT_ALLOCATOR_H_
    152