Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / renderer / p2p / port_allocator.cc
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 #include "content/renderer/p2p/port_allocator.h"
6
7 namespace content {
8
9 P2PPortAllocator::Config::Config()
10     : disable_tcp_transport(false) {
11 }
12
13 P2PPortAllocator::Config::~Config() {
14 }
15
16 P2PPortAllocator::Config::RelayServerConfig::RelayServerConfig()
17     : port(0) {
18 }
19
20 P2PPortAllocator::Config::RelayServerConfig::~RelayServerConfig() {
21 }
22
23 P2PPortAllocator::P2PPortAllocator(
24     P2PSocketDispatcher* socket_dispatcher,
25     rtc::NetworkManager* network_manager,
26     rtc::PacketSocketFactory* socket_factory,
27     const Config& config)
28     : cricket::BasicPortAllocator(network_manager, socket_factory),
29       socket_dispatcher_(socket_dispatcher),
30       config_(config) {
31   uint32 flags = 0;
32   if (config_.disable_tcp_transport)
33     flags |= cricket::PORTALLOCATOR_DISABLE_TCP;
34   set_flags(flags);
35   set_allow_tcp_listen(false);
36 }
37
38 P2PPortAllocator::~P2PPortAllocator() {
39 }
40
41 cricket::PortAllocatorSession* P2PPortAllocator::CreateSessionInternal(
42     const std::string& content_name,
43     int component,
44     const std::string& ice_username_fragment,
45     const std::string& ice_password) {
46   return new P2PPortAllocatorSession(
47       this, content_name, component, ice_username_fragment, ice_password);
48 }
49
50 P2PPortAllocatorSession::P2PPortAllocatorSession(
51     P2PPortAllocator* allocator,
52     const std::string& content_name,
53     int component,
54     const std::string& ice_username_fragment,
55     const std::string& ice_password)
56     : cricket::BasicPortAllocatorSession(allocator,
57                                          content_name,
58                                          component,
59                                          ice_username_fragment,
60                                          ice_password),
61       allocator_(allocator) {
62 }
63
64 P2PPortAllocatorSession::~P2PPortAllocatorSession() {
65 }
66
67 void P2PPortAllocatorSession::GetPortConfigurations() {
68   const P2PPortAllocator::Config& config = allocator_->config_;
69   cricket::PortConfiguration* port_config = new cricket::PortConfiguration(
70       config.stun_servers, std::string(), std::string());
71
72   for (size_t i = 0; i < config.relays.size(); ++i) {
73     cricket::RelayCredentials credentials(config.relays[i].username,
74                                           config.relays[i].password);
75     cricket::RelayServerConfig relay_server(cricket::RELAY_TURN);
76     cricket::ProtocolType protocol;
77     if (!cricket::StringToProto(config.relays[i].transport_type.c_str(),
78                                 &protocol)) {
79       DLOG(WARNING) << "Ignoring TURN server "
80                     << config.relays[i].server_address << ". "
81                     << "Reason= Incorrect "
82                     << config.relays[i].transport_type
83                     << " transport parameter.";
84       continue;
85     }
86
87     relay_server.ports.push_back(cricket::ProtocolAddress(
88         rtc::SocketAddress(config.relays[i].server_address,
89                                  config.relays[i].port),
90         protocol,
91         config.relays[i].secure));
92     relay_server.credentials = credentials;
93     port_config->AddRelay(relay_server);
94   }
95   ConfigReady(port_config);
96 }
97
98 }  // namespace content