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 "jingle/notifier/communicator/connection_settings.h"
6
7 #include "base/logging.h"
8
9 // Ideally we shouldn't include anything from talk/p2p, but we need
10 // the definition of ProtocolType. Don't use any functions from
11 // port.h, since it won't link.
12 #include "talk/p2p/base/port.h"
13
14 #include "talk/xmpp/xmppclientsettings.h"
15
16 namespace notifier {
17
18 const uint16 kSslTcpPort = 443;
19
ConnectionSettings(const talk_base::SocketAddress & server,SslTcpMode ssltcp_mode,SslTcpSupport ssltcp_support)20 ConnectionSettings::ConnectionSettings(
21 const talk_base::SocketAddress& server,
22 SslTcpMode ssltcp_mode,
23 SslTcpSupport ssltcp_support)
24 : server(server),
25 ssltcp_mode(ssltcp_mode),
26 ssltcp_support(ssltcp_support) {}
27
ConnectionSettings()28 ConnectionSettings::ConnectionSettings()
29 : ssltcp_mode(DO_NOT_USE_SSLTCP),
30 ssltcp_support(DOES_NOT_SUPPORT_SSLTCP) {}
31
~ConnectionSettings()32 ConnectionSettings::~ConnectionSettings() {}
33
Equals(const ConnectionSettings & settings) const34 bool ConnectionSettings::Equals(const ConnectionSettings& settings) const {
35 return
36 server == settings.server &&
37 ssltcp_mode == settings.ssltcp_mode &&
38 ssltcp_support == settings.ssltcp_support;
39 }
40
41 namespace {
42
SslTcpModeToString(SslTcpMode ssltcp_mode)43 const char* SslTcpModeToString(SslTcpMode ssltcp_mode) {
44 return (ssltcp_mode == USE_SSLTCP) ? "USE_SSLTCP" : "DO_NOT_USE_SSLTCP";
45 }
46
SslTcpSupportToString(SslTcpSupport ssltcp_support)47 const char* SslTcpSupportToString(SslTcpSupport ssltcp_support) {
48 return
49 (ssltcp_support == SUPPORTS_SSLTCP) ?
50 "SUPPORTS_SSLTCP" :
51 "DOES_NOT_SUPPORT_SSLTCP";
52 }
53
54 } // namespace
55
ToString() const56 std::string ConnectionSettings::ToString() const {
57 return
58 server.ToString() + ":" + SslTcpModeToString(ssltcp_mode) + ":" +
59 SslTcpSupportToString(ssltcp_support);
60 }
61
FillXmppClientSettings(buzz::XmppClientSettings * client_settings) const62 void ConnectionSettings::FillXmppClientSettings(
63 buzz::XmppClientSettings* client_settings) const {
64 client_settings->set_protocol(
65 (ssltcp_mode == USE_SSLTCP) ?
66 cricket::PROTO_SSLTCP :
67 cricket::PROTO_TCP);
68 client_settings->set_server(server);
69 }
70
MakeConnectionSettingsList(const ServerList & servers,bool try_ssltcp_first)71 ConnectionSettingsList MakeConnectionSettingsList(
72 const ServerList& servers,
73 bool try_ssltcp_first) {
74 ConnectionSettingsList settings_list;
75
76 for (ServerList::const_iterator it = servers.begin();
77 it != servers.end(); ++it) {
78 const ConnectionSettings settings(
79 talk_base::SocketAddress(it->server.host(), it->server.port()),
80 DO_NOT_USE_SSLTCP, it->ssltcp_support);
81
82 if (it->ssltcp_support == SUPPORTS_SSLTCP) {
83 const ConnectionSettings settings_with_ssltcp(
84 talk_base::SocketAddress(it->server.host(), kSslTcpPort),
85 USE_SSLTCP, it->ssltcp_support);
86
87 if (try_ssltcp_first) {
88 settings_list.push_back(settings_with_ssltcp);
89 settings_list.push_back(settings);
90 } else {
91 settings_list.push_back(settings);
92 settings_list.push_back(settings_with_ssltcp);
93 }
94 } else {
95 settings_list.push_back(settings);
96 }
97 }
98
99 return settings_list;
100 }
101
102 } // namespace notifier
103