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, 98 int64_t total_encoded_data_length) OVERRIDE; 99 virtual void didFail(blink::WebURLLoader* loader, 100 const blink::WebURLError& error) OVERRIDE; 101 102 protected: 103 // Overrides for cricket::BasicPortAllocatorSession. 104 virtual void GetPortConfigurations() OVERRIDE; 105 106 private: 107 // This method allocates non-TURN relay sessions. 108 void AllocateLegacyRelaySession(); 109 void ParseRelayResponse(); 110 111 void AddConfig(); 112 113 P2PPortAllocator* allocator_; 114 115 scoped_ptr<blink::WebURLLoader> relay_session_request_; 116 int relay_session_attempts_; 117 std::string relay_session_response_; 118 talk_base::SocketAddress relay_ip_; 119 int relay_udp_port_; 120 int relay_tcp_port_; 121 int relay_ssltcp_port_; 122 int pending_relay_requests_; 123 124 DISALLOW_COPY_AND_ASSIGN(P2PPortAllocatorSession); 125 }; 126 127 } // namespace content 128 129 #endif // CONTENT_RENDERER_P2P_PORT_ALLOCATOR_H_ 130