Roll src/third_party/WebKit a452221:9ff6d11 (svn 202117:202119)
[chromium-blink-merge.git] / content / renderer / p2p / port_allocator.cc
blob1e29b83c582d11cb856c7ee691db335d3c1ee03b
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() {}
14 P2PPortAllocator::Config::~Config() {
17 P2PPortAllocator::Config::RelayServerConfig::RelayServerConfig()
18 : port(0) {
21 P2PPortAllocator::Config::RelayServerConfig::~RelayServerConfig() {
24 P2PPortAllocator::P2PPortAllocator(
25 P2PSocketDispatcher* socket_dispatcher,
26 rtc::NetworkManager* network_manager,
27 rtc::PacketSocketFactory* socket_factory,
28 const Config& config,
29 const GURL& origin)
30 : cricket::BasicPortAllocator(network_manager, socket_factory),
31 socket_dispatcher_(socket_dispatcher),
32 config_(config),
33 origin_(origin)
35 uint32 flags = 0;
36 if (!config_.enable_multiple_routes)
37 flags |= cricket::PORTALLOCATOR_DISABLE_ADAPTER_ENUMERATION;
38 if (!config_.enable_nonproxied_udp) {
39 flags |= cricket::PORTALLOCATOR_DISABLE_UDP |
40 cricket::PORTALLOCATOR_DISABLE_STUN |
41 cricket::PORTALLOCATOR_DISABLE_UDP_RELAY;
43 set_flags(flags);
44 set_allow_tcp_listen(false);
45 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
46 bool enable_webrtc_stun_origin =
47 cmd_line->HasSwitch(switches::kEnableWebRtcStunOrigin);
48 if (enable_webrtc_stun_origin) {
49 set_origin(origin.spec());
53 P2PPortAllocator::~P2PPortAllocator() {
56 cricket::PortAllocatorSession* P2PPortAllocator::CreateSessionInternal(
57 const std::string& content_name,
58 int component,
59 const std::string& ice_username_fragment,
60 const std::string& ice_password) {
61 return new P2PPortAllocatorSession(
62 this, content_name, component, ice_username_fragment, ice_password);
65 P2PPortAllocatorSession::P2PPortAllocatorSession(
66 P2PPortAllocator* allocator,
67 const std::string& content_name,
68 int component,
69 const std::string& ice_username_fragment,
70 const std::string& ice_password)
71 : cricket::BasicPortAllocatorSession(allocator,
72 content_name,
73 component,
74 ice_username_fragment,
75 ice_password),
76 allocator_(allocator) {
79 P2PPortAllocatorSession::~P2PPortAllocatorSession() {
82 void P2PPortAllocatorSession::GetPortConfigurations() {
83 const P2PPortAllocator::Config& config = allocator_->config_;
84 cricket::PortConfiguration* port_config = new cricket::PortConfiguration(
85 config.stun_servers, std::string(), std::string());
87 for (size_t i = 0; i < config.relays.size(); ++i) {
88 cricket::RelayCredentials credentials(config.relays[i].username,
89 config.relays[i].password);
90 cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
91 cricket::ProtocolType protocol;
92 if (!cricket::StringToProto(config.relays[i].transport_type.c_str(),
93 &protocol)) {
94 DLOG(WARNING) << "Ignoring TURN server "
95 << config.relays[i].server_address << ". "
96 << "Reason= Incorrect "
97 << config.relays[i].transport_type
98 << " transport parameter.";
99 continue;
102 relay_server.ports.push_back(cricket::ProtocolAddress(
103 rtc::SocketAddress(config.relays[i].server_address,
104 config.relays[i].port),
105 protocol,
106 config.relays[i].secure));
107 relay_server.credentials = credentials;
108 port_config->AddRelay(relay_server);
110 ConfigReady(port_config);
113 } // namespace content