blob: d4d0d8893f13d3684ce1136427bec6eff7b2e4b9 [file] [log] [blame]
[email protected]4200a2082010-04-27 23:19:471// Copyright (c) 2010 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 "chrome/browser/importer/firefox_proxy_settings.h"
6
7#include "base/string_tokenizer.h"
8#include "base/string_util.h"
9#include "base/values.h"
10#include "chrome/browser/importer/firefox_importer_utils.h"
[email protected]483bf122010-05-04 20:41:2411#include "net/proxy/proxy_config.h"
[email protected]4200a2082010-04-27 23:19:4712
13namespace {
14
15const wchar_t* const kNetworkProxyTypeKey = L"network.proxy.type";
16const char* const kHTTPProxyKey = "network.proxy.http";
17const wchar_t* const kHTTPProxyPortKey = L"network.proxy.http_port";
18const char* const kSSLProxyKey = "network.proxy.ssl";
19const wchar_t* const kSSLProxyPortKey = L"network.proxy.ssl_port";
20const char* const kFTPProxyKey = "network.proxy.ftp";
21const wchar_t* const kFTPProxyPortKey = L"network.proxy.ftp_port";
22const char* const kGopherProxyKey = "network.proxy.gopher";
23const wchar_t* const kGopherProxyPortKey = L"network.proxy.gopher_port";
24const char* const kSOCKSHostKey = "network.proxy.socks";
25const wchar_t* const kSOCKSHostPortKey = L"network.proxy.socks_port";
26const wchar_t* const kSOCKSVersionKey = L"network.proxy.socks_version";
27const char* const kAutoconfigURL = "network.proxy.autoconfig_url";
28const char* const kNoProxyListKey = "network.proxy.no_proxies_on";
29const char* const kPrefFileName = "prefs.js";
30
31FirefoxProxySettings::ProxyConfig IntToProxyConfig(int type) {
32 switch (type) {
33 case 1:
34 return FirefoxProxySettings::MANUAL;
35 case 2:
36 return FirefoxProxySettings::AUTO_FROM_URL;
37 case 4:
38 return FirefoxProxySettings::AUTO_DETECT;
39 case 5:
40 return FirefoxProxySettings::SYSTEM;
41 default:
42 LOG(ERROR) << "Unknown Firefox proxy config type: " << type;
43 return FirefoxProxySettings::NO_PROXY;
44 }
45}
46
47FirefoxProxySettings::SOCKSVersion IntToSOCKSVersion(int type) {
48 switch (type) {
49 case 4:
50 return FirefoxProxySettings::V4;
51 case 5:
52 return FirefoxProxySettings::V5;
53 default:
54 LOG(ERROR) << "Unknown Firefox proxy config type: " << type;
55 return FirefoxProxySettings::UNKNONW;
56 }
57}
58
59} // namespace
60
61FirefoxProxySettings::FirefoxProxySettings() {
62 Reset();
63}
64
65FirefoxProxySettings::~FirefoxProxySettings() {
66}
67
68void FirefoxProxySettings::Reset() {
69 config_type_ = NO_PROXY;
70 http_proxy_.clear();
71 http_proxy_port_ = 0;
72 ssl_proxy_.clear();
73 ssl_proxy_port_ = 0;
74 ftp_proxy_.clear();
75 ftp_proxy_port_ = 0;
76 gopher_proxy_.clear();
77 gopher_proxy_port_ = 0;
78 socks_host_.clear();
79 socks_port_ = 0;
80 socks_version_ = UNKNONW;
81 proxy_bypass_list_.clear();
82 autoconfig_url_.clear();
83}
84
85// static
[email protected]483bf122010-05-04 20:41:2486bool FirefoxProxySettings::GetSettings(FirefoxProxySettings* settings) {
[email protected]4200a2082010-04-27 23:19:4787 DCHECK(settings);
88 settings->Reset();
89
90 FilePath profile_path = GetFirefoxProfilePath();
91 if (profile_path.empty())
92 return false;
93 FilePath pref_file = profile_path.AppendASCII(kPrefFileName);
94 return GetSettingsFromFile(pref_file, settings);
95}
96
[email protected]483bf122010-05-04 20:41:2497bool FirefoxProxySettings::ToProxyConfig(net::ProxyConfig* config) {
98 switch (config_type()) {
99 case NO_PROXY:
100 *config = net::ProxyConfig::CreateDirect();
101 return true;
102 case AUTO_DETECT:
103 *config = net::ProxyConfig::CreateAutoDetect();
104 return true;
105 case AUTO_FROM_URL:
106 *config = net::ProxyConfig::CreateFromCustomPacURL(
107 GURL(autoconfig_url()));
108 return true;
109 case SYSTEM:
110 // Can't convert this directly to a ProxyConfig.
111 return false;
112 case MANUAL:
113 // Handled outside of the switch (since it is a lot of code.)
114 break;
115 default:
116 NOTREACHED();
117 return false;
118 }
119
120 // The rest of this funciton is for handling the MANUAL case.
121 DCHECK_EQ(MANUAL, config_type());
122
123 *config = net::ProxyConfig();
124 config->proxy_rules().type =
125 net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME;
126
127 if (!http_proxy().empty()) {
128 config->proxy_rules().proxy_for_http = net::ProxyServer(
129 net::ProxyServer::SCHEME_HTTP,
130 http_proxy(),
131 http_proxy_port());
132 }
133
134 if (!ftp_proxy().empty()) {
135 config->proxy_rules().proxy_for_ftp = net::ProxyServer(
136 net::ProxyServer::SCHEME_HTTP,
137 ftp_proxy(),
138 ftp_proxy_port());
139 }
140
141 if (!ssl_proxy().empty()) {
142 config->proxy_rules().proxy_for_https = net::ProxyServer(
143 net::ProxyServer::SCHEME_HTTP,
144 ssl_proxy(),
145 ssl_proxy_port());
146 }
147
148 if (!socks_host().empty()) {
149 net::ProxyServer::Scheme proxy_scheme = V5 == socks_version() ?
150 net::ProxyServer::SCHEME_SOCKS5 : net::ProxyServer::SCHEME_SOCKS4;
151
152 config->proxy_rules().socks_proxy = net::ProxyServer(
153 proxy_scheme,
154 socks_host(),
155 socks_port());
156 }
157
158 config->proxy_rules().bypass_rules.ParseFromStringUsingSuffixMatching(
159 JoinString(proxy_bypass_list_, ';'));
160
161 return true;
162}
163
[email protected]4200a2082010-04-27 23:19:47164// static
165bool FirefoxProxySettings::GetSettingsFromFile(const FilePath& pref_file,
166 FirefoxProxySettings* settings) {
167 DictionaryValue dictionary;
168 if (!ParsePrefFile(pref_file, &dictionary))
169 return false;
170
171 int proxy_type = 0;
172 if (!dictionary.GetInteger(kNetworkProxyTypeKey, &proxy_type))
173 return true; // No type means no proxy.
174
175 settings->config_type_ = IntToProxyConfig(proxy_type);
176 if (settings->config_type_ == AUTO_FROM_URL) {
177 if (!dictionary.GetStringASCII(kAutoconfigURL,
178 &(settings->autoconfig_url_))) {
179 LOG(ERROR) << "Failed to retrieve Firefox proxy autoconfig URL";
180 }
181 return false;
182 }
183
184 if (settings->config_type_ == MANUAL) {
185 if (!dictionary.GetStringASCII(kHTTPProxyKey, &(settings->http_proxy_)))
186 LOG(ERROR) << "Failed to retrieve Firefox proxy HTTP host";
187 if (!dictionary.GetInteger(kHTTPProxyPortKey,
188 &(settings->http_proxy_port_))) {
189 LOG(ERROR) << "Failed to retrieve Firefox proxy HTTP port";
190 }
191 if (!dictionary.GetStringASCII(kSSLProxyKey, &(settings->ssl_proxy_)))
192 LOG(ERROR) << "Failed to retrieve Firefox proxy SSL host";
193 if (!dictionary.GetInteger(kSSLProxyPortKey, &(settings->ssl_proxy_port_)))
194 LOG(ERROR) << "Failed to retrieve Firefox proxy SSL port";
195 if (!dictionary.GetStringASCII(kFTPProxyKey, &(settings->ftp_proxy_)))
196 LOG(ERROR) << "Failed to retrieve Firefox proxy FTP host";
197 if (!dictionary.GetInteger(kFTPProxyPortKey, &(settings->ftp_proxy_port_)))
198 LOG(ERROR) << "Failed to retrieve Firefox proxy SSL port";
199 if (!dictionary.GetStringASCII(kGopherProxyKey, &(settings->gopher_proxy_)))
200 LOG(ERROR) << "Failed to retrieve Firefox proxy gopher host";
201 if (!dictionary.GetInteger(kGopherProxyPortKey,
202 &(settings->gopher_proxy_port_))) {
203 LOG(ERROR) << "Failed to retrieve Firefox proxy gopher port";
204 }
205 if (!dictionary.GetStringASCII(kSOCKSHostKey, &(settings->socks_host_)))
206 LOG(ERROR) << "Failed to retrieve Firefox SOCKS host";
207 if (!dictionary.GetInteger(kSOCKSHostPortKey, &(settings->socks_port_)))
208 LOG(ERROR) << "Failed to retrieve Firefox SOCKS port";
209 int socks_version;
210 if (dictionary.GetInteger(kSOCKSVersionKey, &socks_version))
211 settings->socks_version_ = IntToSOCKSVersion(socks_version);
212
213 std::string proxy_bypass;
214 if (dictionary.GetStringASCII(kNoProxyListKey, &proxy_bypass) &&
215 !proxy_bypass.empty()) {
216 StringTokenizer string_tok(proxy_bypass, ",");
217 while (string_tok.GetNext()) {
218 std::string token = string_tok.token();
219 TrimWhitespaceASCII(token, TRIM_ALL, &token);
220 if (!token.empty())
221 settings->proxy_bypass_list_.push_back(token);
222 }
223 }
224 }
225 return true;
226}