Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / content / renderer / p2p / port_allocator.h
blobd9294498108633858a9bf3c0fe85cb30aa7ff596
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/libjingle/source/talk/p2p/client/basicportallocator.h"
12 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURLLoaderClient.h"
14 namespace WebKit {
15 class WebFrame;
16 class WebURLLoader;
17 } // namespace WebKit
19 namespace content {
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
27 // duplication.
28 class P2PPortAllocator : public cricket::BasicPortAllocator {
29 public:
30 struct Config {
31 Config();
32 ~Config();
34 // STUN server address and port.
35 std::string stun_server;
36 int stun_server_port;
38 // Relay server address and port.
39 std::string relay_server;
40 int relay_server_port;
42 // Relay server username.
43 std::string relay_username;
45 // Relay server password.
46 std::string relay_password;
48 // When set to true relay is a legacy Google relay (not TURN compliant).
49 bool legacy_relay;
51 // Disable TCP-based transport when set to true.
52 bool disable_tcp_transport;
55 P2PPortAllocator(WebKit::WebFrame* web_frame,
56 P2PSocketDispatcher* socket_dispatcher,
57 talk_base::NetworkManager* network_manager,
58 talk_base::PacketSocketFactory* socket_factory,
59 const Config& config);
60 virtual ~P2PPortAllocator();
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;
68 private:
69 friend class P2PPortAllocatorSession;
71 WebKit::WebFrame* web_frame_;
72 P2PSocketDispatcher* socket_dispatcher_;
73 Config config_;
75 DISALLOW_COPY_AND_ASSIGN(P2PPortAllocator);
78 class P2PPortAllocatorSession : public cricket::BasicPortAllocatorSession,
79 public WebKit::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();
89 // WebKit::WebURLLoaderClient overrides.
90 virtual void didReceiveData(WebKit::WebURLLoader* loader,
91 const char* data,
92 int data_length,
93 int encoded_data_length) OVERRIDE;
94 virtual void didFinishLoading(WebKit::WebURLLoader* loader,
95 double finish_time) OVERRIDE;
96 virtual void didFail(WebKit::WebURLLoader* loader,
97 const WebKit::WebURLError& error) OVERRIDE;
99 protected:
100 // Overrides for cricket::BasicPortAllocatorSession.
101 virtual void GetPortConfigurations() OVERRIDE;
103 private:
104 void ResolveStunServerAddress();
105 void OnStunServerAddress(const net::IPAddressNumber& address);
107 // This method allocates non-TURN relay sessions.
108 void AllocateLegacyRelaySession();
109 void ParseRelayResponse();
111 void AddConfig();
113 P2PPortAllocator* allocator_;
115 scoped_refptr<P2PHostAddressRequest> stun_address_request_;
116 talk_base::SocketAddress stun_server_address_;
118 scoped_ptr<WebKit::WebURLLoader> relay_session_request_;
119 int relay_session_attempts_;
120 std::string relay_session_response_;
121 talk_base::SocketAddress relay_ip_;
122 int relay_udp_port_;
123 int relay_tcp_port_;
124 int relay_ssltcp_port_;
126 DISALLOW_COPY_AND_ASSIGN(P2PPortAllocatorSession);
129 } // namespace content
131 #endif // CONTENT_RENDERER_P2P_PORT_ALLOCATOR_H_