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.
5 #ifndef CONTENT_RENDERER_P2P_PORT_ALLOCATOR_H_
6 #define CONTENT_RENDERER_P2P_PORT_ALLOCATOR_H_
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"
21 class P2PHostAddressRequest;
22 class P2PPortAllocatorSession;
23 class P2PSocketDispatcher;
25 // TODO(sergeyu): There is overlap between this class and HttpPortAllocator.
26 // Refactor this class to inherit from HttpPortAllocator to avoid code
28 class P2PPortAllocator : public cricket::BasicPortAllocator {
34 struct RelayServerConfig {
40 std::string server_address;
42 std::string transport_type;
46 // STUN server address and port.
47 std::string stun_server;
50 std::vector<RelayServerConfig> relays;
53 // Disable TCP-based transport when set to true.
54 bool disable_tcp_transport;
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();
64 virtual cricket::PortAllocatorSession* CreateSessionInternal(
65 const std::string& content_name,
67 const std::string& ice_username_fragment,
68 const std::string& ice_password) OVERRIDE;
71 friend class P2PPortAllocatorSession;
73 blink::WebFrame* web_frame_;
74 P2PSocketDispatcher* socket_dispatcher_;
77 DISALLOW_COPY_AND_ASSIGN(P2PPortAllocator);
80 class P2PPortAllocatorSession : public cricket::BasicPortAllocatorSession,
81 public blink::WebURLLoaderClient {
83 P2PPortAllocatorSession(
84 P2PPortAllocator* allocator,
85 const std::string& content_name,
87 const std::string& ice_username_fragment,
88 const std::string& ice_password);
89 virtual ~P2PPortAllocatorSession();
91 // blink::WebURLLoaderClient overrides.
92 virtual void didReceiveData(blink::WebURLLoader* loader,
95 int encoded_data_length) OVERRIDE;
96 virtual void didFinishLoading(blink::WebURLLoader* loader,
98 int64_t total_encoded_data_length) OVERRIDE;
99 virtual void didFail(blink::WebURLLoader* loader,
100 const blink::WebURLError& error) OVERRIDE;
103 // Overrides for cricket::BasicPortAllocatorSession.
104 virtual void GetPortConfigurations() OVERRIDE;
107 // This method allocates non-TURN relay sessions.
108 void AllocateLegacyRelaySession();
109 void ParseRelayResponse();
113 P2PPortAllocator* allocator_;
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_;
121 int relay_ssltcp_port_;
122 int pending_relay_requests_;
124 DISALLOW_COPY_AND_ASSIGN(P2PPortAllocatorSession);
127 } // namespace content
129 #endif // CONTENT_RENDERER_P2P_PORT_ALLOCATOR_H_