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     std::set<rtc::SocketAddress> stun_servers;
     47 
     48     std::vector<RelayServerConfig> relays;
     49 
     50     bool legacy_relay;
     51     // Disable TCP-based transport when set to true.
     52     bool disable_tcp_transport;
     53   };
     54 
     55   P2PPortAllocator(blink::WebFrame* web_frame,
     56                    P2PSocketDispatcher* socket_dispatcher,
     57                    rtc::NetworkManager* network_manager,
     58                    rtc::PacketSocketFactory* socket_factory,
     59                    const Config& config);
     60   virtual ~P2PPortAllocator();
     61 
     62   virtual cricket::PortAllocatorSession* CreateSessionInternal(
     63       const std::string& content_name,
     64       int component,
     65       const std::string& ice_username_fragment,
     66       const std::string& ice_password) OVERRIDE;
     67 
     68  private:
     69   friend class P2PPortAllocatorSession;
     70 
     71   blink::WebFrame* web_frame_;
     72   P2PSocketDispatcher* socket_dispatcher_;
     73   Config config_;
     74 
     75   DISALLOW_COPY_AND_ASSIGN(P2PPortAllocator);
     76 };
     77 
     78 class P2PPortAllocatorSession : public cricket::BasicPortAllocatorSession,
     79                                 public blink::WebURLLoaderClient  {
     80  public:
     81   P2PPortAllocatorSession(
     82       P2PPortAllocator* allocator,
     83       const std::string& content_name,
     84       int component,
     85       const std::string& ice_username_fragment,
     86       const std::string& ice_password);
     87   virtual ~P2PPortAllocatorSession();
     88 
     89   // blink::WebURLLoaderClient overrides.
     90   virtual void didReceiveData(blink::WebURLLoader* loader,
     91                               const char* data,
     92                               int data_length,
     93                               int encoded_data_length) OVERRIDE;
     94   virtual void didFinishLoading(blink::WebURLLoader* loader,
     95                                 double finish_time,
     96                                 int64_t total_encoded_data_length) OVERRIDE;
     97   virtual void didFail(blink::WebURLLoader* loader,
     98                        const blink::WebURLError& error) OVERRIDE;
     99 
    100  protected:
    101   // Overrides for cricket::BasicPortAllocatorSession.
    102   virtual void GetPortConfigurations() OVERRIDE;
    103 
    104  private:
    105   // This method allocates non-TURN relay sessions.
    106   void AllocateLegacyRelaySession();
    107   void ParseRelayResponse();
    108 
    109   void AddConfig();
    110 
    111   P2PPortAllocator* allocator_;
    112 
    113   scoped_ptr<blink::WebURLLoader> relay_session_request_;
    114   int relay_session_attempts_;
    115   std::string relay_session_response_;
    116   rtc::SocketAddress relay_ip_;
    117   int relay_udp_port_;
    118   int relay_tcp_port_;
    119   int relay_ssltcp_port_;
    120   int pending_relay_requests_;
    121 
    122   DISALLOW_COPY_AND_ASSIGN(P2PPortAllocatorSession);
    123 };
    124 
    125 }  // namespace content
    126 
    127 #endif  // CONTENT_RENDERER_P2P_PORT_ALLOCATOR_H_
    128