Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / p2p / port_allocator.cc
blob7a1abdc216648d18b693d50dd36db484bfb13348
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 #include "content/renderer/p2p/port_allocator.h"
7 #include "base/command_line.h"
8 #include "content/public/common/content_switches.h"
10 namespace content {
12 P2PPortAllocator::Config::Config()
13 : disable_tcp_transport(false) {
16 P2PPortAllocator::Config::~Config() {
19 P2PPortAllocator::Config::RelayServerConfig::RelayServerConfig()
20 : port(0) {
23 P2PPortAllocator::Config::RelayServerConfig::~RelayServerConfig() {
26 P2PPortAllocator::P2PPortAllocator(
27 P2PSocketDispatcher* socket_dispatcher,
28 rtc::NetworkManager* network_manager,
29 rtc::PacketSocketFactory* socket_factory,
30 const Config& config,
31 const GURL& origin)
32 : cricket::BasicPortAllocator(network_manager, socket_factory),
33 socket_dispatcher_(socket_dispatcher),
34 config_(config),
35 origin_(origin)
37 uint32 flags = 0;
38 if (config_.disable_tcp_transport)
39 flags |= cricket::PORTALLOCATOR_DISABLE_TCP;
40 if (!config_.enable_multiple_routes)
41 flags |= cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION;
42 set_flags(flags);
43 set_allow_tcp_listen(false);
44 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
45 bool enable_webrtc_stun_origin =
46 cmd_line->HasSwitch(switches::kEnableWebRtcStunOrigin);
47 if (enable_webrtc_stun_origin) {
48 set_origin(origin.spec());
52 P2PPortAllocator::~P2PPortAllocator() {
55 cricket::PortAllocatorSession* P2PPortAllocator::CreateSessionInternal(
56 const std::string& content_name,
57 int component,
58 const std::string& ice_username_fragment,
59 const std::string& ice_password) {
60 return new P2PPortAllocatorSession(
61 this, content_name, component, ice_username_fragment, ice_password);
64 P2PPortAllocatorSession::P2PPortAllocatorSession(
65 P2PPortAllocator* allocator,
66 const std::string& content_name,
67 int component,
68 const std::string& ice_username_fragment,
69 const std::string& ice_password)
70 : cricket::BasicPortAllocatorSession(allocator,
71 content_name,
72 component,
73 ice_username_fragment,
74 ice_password),
75 allocator_(allocator) {
78 P2PPortAllocatorSession::~P2PPortAllocatorSession() {
81 void P2PPortAllocatorSession::GetPortConfigurations() {
82 const P2PPortAllocator::Config& config = allocator_->config_;
83 cricket::PortConfiguration* port_config = new cricket::PortConfiguration(
84 config.stun_servers, std::string(), std::string());
86 for (size_t i = 0; i < config.relays.size(); ++i) {
87 cricket::RelayCredentials credentials(config.relays[i].username,
88 config.relays[i].password);
89 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
90 cricket::ProtocolType protocol;
91 if (!cricket::StringToProto(config.relays[i].transport_type.c_str(),
92 &protocol)) {
93 DLOG(WARNING) << "Ignoring TURN server "
94 << config.relays[i].server_address << ". "
95 << "Reason= Incorrect "
96 << config.relays[i].transport_type
97 << " transport parameter.";
98 continue;
101 relay_server.ports.push_back(cricket::ProtocolAddress(
102 rtc::SocketAddress(config.relays[i].server_address,
103 config.relays[i].port),
104 protocol,
105 config.relays[i].secure));
106 relay_server.credentials = credentials;
107 port_config->AddRelay(relay_server);
109 ConfigReady(port_config);
112 } // namespace content