diff options
Diffstat (limited to 'net')
-rw-r--r-- | net/base/net_log_event_type_list.h | 13 | ||||
-rw-r--r-- | net/proxy/proxy_config.cc | 211 | ||||
-rw-r--r-- | net/proxy/proxy_config.h | 19 | ||||
-rw-r--r-- | net/proxy/proxy_config_unittest.cc | 22 | ||||
-rw-r--r-- | net/proxy/proxy_service.cc | 46 |
5 files changed, 190 insertions, 121 deletions
diff --git a/net/base/net_log_event_type_list.h b/net/base/net_log_event_type_list.h index 2a7afeb..f729c2d 100644 --- a/net/base/net_log_event_type_list.h +++ b/net/base/net_log_event_type_list.h @@ -91,6 +91,19 @@ EVENT_TYPE(PROXY_SERVICE_WAITING_FOR_INIT_PAC) // } EVENT_TYPE(PROXY_SERVICE_RESOLVED_PROXY_LIST) +// This event is emitted whenever the proxy settings used by ProxyService +// change. +// +// It contains these parameters: +// { +// "old_config": <Dump of the previous proxy settings>, +// "new_config": <Dump of the new proxy settings> +// } +// +// Note that the "old_config" key will be omitted on the first fetch of the +// proxy settings (since there wasn't a previous value). +EVENT_TYPE(PROXY_CONFIG_CHANGED) + // ------------------------------------------------------------------------ // Proxy Resolver // ------------------------------------------------------------------------ diff --git a/net/proxy/proxy_config.cc b/net/proxy/proxy_config.cc index f27fbc4..19fa926 100644 --- a/net/proxy/proxy_config.cc +++ b/net/proxy/proxy_config.cc @@ -4,14 +4,122 @@ #include "net/proxy/proxy_config.h" +#include <sstream> + #include "base/logging.h" #include "base/string_number_conversions.h" #include "base/string_tokenizer.h" #include "base/string_util.h" +#include "base/values.h" #include "net/proxy/proxy_info.h" namespace net { +namespace { + +// Helper to stringize a ProxyServer. +std::ostream& operator<<(std::ostream& out, + const ProxyServer& proxy_server) { + if (proxy_server.is_valid()) + out << proxy_server.ToURI(); + return out; +} + +const char* BoolToYesNoString(bool b) { + return b ? "Yes" : "No"; +} + +std::ostream& operator<<(std::ostream& out, + const ProxyConfig::ProxyRules& rules) { + // Stringize the type enum. + std::string type; + switch (rules.type) { + case net::ProxyConfig::ProxyRules::TYPE_NO_RULES: + type = "TYPE_NO_RULES"; + break; + case net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME: + type = "TYPE_PROXY_PER_SCHEME"; + break; + case net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY: + type = "TYPE_SINGLE_PROXY"; + break; + default: + type = base::IntToString(rules.type); + break; + } + return out << " {\n" + << " type: " << type << "\n" + << " single_proxy: " << rules.single_proxy << "\n" + << " proxy_for_http: " << rules.proxy_for_http << "\n" + << " proxy_for_https: " << rules.proxy_for_https << "\n" + << " proxy_for_ftp: " << rules.proxy_for_ftp << "\n" + << " socks_proxy: " << rules.socks_proxy << "\n" + << " }"; +} + +std::ostream& operator<<(std::ostream& out, const ProxyConfig& config) { + // "Automatic" settings. + out << "Automatic settings:\n"; + out << " Auto-detect: " << BoolToYesNoString(config.auto_detect()) << "\n"; + out << " Custom PAC script: "; + if (config.has_pac_url()) + out << config.pac_url(); + else + out << "[None]"; + out << "\n"; + + // "Manual" settings. + out << "Manual settings:\n"; + out << " Proxy server: "; + + switch (config.proxy_rules().type) { + case net::ProxyConfig::ProxyRules::TYPE_NO_RULES: + out << "[None]\n"; + break; + case net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY: + out << config.proxy_rules().single_proxy; + out << "\n"; + break; + case net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME: + out << "\n"; + if (config.proxy_rules().proxy_for_http.is_valid()) + out << " HTTP: " << config.proxy_rules().proxy_for_http << "\n"; + if (config.proxy_rules().proxy_for_https.is_valid()) + out << " HTTPS: " << config.proxy_rules().proxy_for_https << "\n"; + if (config.proxy_rules().proxy_for_ftp.is_valid()) + out << " FTP: " << config.proxy_rules().proxy_for_ftp << "\n"; + if (config.proxy_rules().socks_proxy.is_valid()) + out << " SOCKS: " << config.proxy_rules().socks_proxy << "\n"; + break; + } + + if (config.proxy_rules().reverse_bypass) + out << " Only use proxy for: "; + else + out << " Bypass list: "; + if (config.proxy_rules().bypass_rules.rules().empty()) { + out << "[None]"; + } else { + const net::ProxyBypassRules& bypass_rules = + config.proxy_rules().bypass_rules; + net::ProxyBypassRules::RuleList::const_iterator it; + for (it = bypass_rules.rules().begin(); + it != bypass_rules.rules().end(); ++it) { + out << "\n " << (*it)->ToString(); + } + } + + return out; +} + +std::string ProxyConfigToString(const ProxyConfig& proxy_config) { + std::ostringstream stream; + stream << proxy_config; + return stream.str(); +} + +} // namespace + bool ProxyConfig::ProxyRules::Equals(const ProxyRules& other) const { return type == other.type && single_proxy == other.single_proxy && @@ -145,103 +253,16 @@ bool ProxyConfig::MayRequirePACResolver() const { return auto_detect_ || has_pac_url(); } -} // namespace net - -namespace { - -// Helper to stringize a ProxyServer. -std::ostream& operator<<(std::ostream& out, - const net::ProxyServer& proxy_server) { - if (proxy_server.is_valid()) - out << proxy_server.ToURI(); - return out; -} - -const char* BoolToYesNoString(bool b) { - return b ? "Yes" : "No"; +std::string ProxyConfig::ToString() const { + return ProxyConfigToString(*this); } -} // namespace - -std::ostream& operator<<(std::ostream& out, - const net::ProxyConfig::ProxyRules& rules) { - // Stringize the type enum. - std::string type; - switch (rules.type) { - case net::ProxyConfig::ProxyRules::TYPE_NO_RULES: - type = "TYPE_NO_RULES"; - break; - case net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME: - type = "TYPE_PROXY_PER_SCHEME"; - break; - case net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY: - type = "TYPE_SINGLE_PROXY"; - break; - default: - type = base::IntToString(rules.type); - break; - } - return out << " {\n" - << " type: " << type << "\n" - << " single_proxy: " << rules.single_proxy << "\n" - << " proxy_for_http: " << rules.proxy_for_http << "\n" - << " proxy_for_https: " << rules.proxy_for_https << "\n" - << " proxy_for_ftp: " << rules.proxy_for_ftp << "\n" - << " socks_proxy: " << rules.socks_proxy << "\n" - << " }"; +Value* ProxyConfig::ToValue() const { + // TODO(eroman): send a dictionary rather than a flat string, so the + // javascript client can do prettier formatting. + // crbug.com/52011 + return Value::CreateStringValue(ToString()); } -std::ostream& operator<<(std::ostream& out, const net::ProxyConfig& config) { - // "Automatic" settings. - out << "Automatic settings:\n"; - out << " Auto-detect: " << BoolToYesNoString(config.auto_detect()) << "\n"; - out << " Custom PAC script: "; - if (config.has_pac_url()) - out << config.pac_url(); - else - out << "[None]"; - out << "\n"; - - // "Manual" settings. - out << "Manual settings:\n"; - out << " Proxy server: "; - - switch (config.proxy_rules().type) { - case net::ProxyConfig::ProxyRules::TYPE_NO_RULES: - out << "[None]\n"; - break; - case net::ProxyConfig::ProxyRules::TYPE_SINGLE_PROXY: - out << config.proxy_rules().single_proxy; - out << "\n"; - break; - case net::ProxyConfig::ProxyRules::TYPE_PROXY_PER_SCHEME: - out << "\n"; - if (config.proxy_rules().proxy_for_http.is_valid()) - out << " HTTP: " << config.proxy_rules().proxy_for_http << "\n"; - if (config.proxy_rules().proxy_for_https.is_valid()) - out << " HTTPS: " << config.proxy_rules().proxy_for_https << "\n"; - if (config.proxy_rules().proxy_for_ftp.is_valid()) - out << " FTP: " << config.proxy_rules().proxy_for_ftp << "\n"; - if (config.proxy_rules().socks_proxy.is_valid()) - out << " SOCKS: " << config.proxy_rules().socks_proxy << "\n"; - break; - } - - if (config.proxy_rules().reverse_bypass) - out << " Only use proxy for: "; - else - out << " Bypass list: "; - if (config.proxy_rules().bypass_rules.rules().empty()) { - out << "[None]"; - } else { - const net::ProxyBypassRules& bypass_rules = - config.proxy_rules().bypass_rules; - net::ProxyBypassRules::RuleList::const_iterator it; - for (it = bypass_rules.rules().begin(); - it != bypass_rules.rules().end(); ++it) { - out << "\n " << (*it)->ToString(); - } - } +} // namespace net - return out; -} diff --git a/net/proxy/proxy_config.h b/net/proxy/proxy_config.h index a4d535da..5254216 100644 --- a/net/proxy/proxy_config.h +++ b/net/proxy/proxy_config.h @@ -6,7 +6,6 @@ #define NET_PROXY_PROXY_CONFIG_H_ #pragma once -#include <ostream> #include <string> #include <vector> @@ -14,6 +13,8 @@ #include "net/proxy/proxy_bypass_rules.h" #include "net/proxy/proxy_server.h" +class Value; + namespace net { class ProxyInfo; @@ -115,7 +116,7 @@ class ProxyConfig { // Used to numerically identify this configuration. ID id() const { return id_; } void set_id(int id) { id_ = id; } - bool is_valid() { return id_ != INVALID_ID; } + bool is_valid() const { return id_ != INVALID_ID; } // Returns true if the given config is equivalent to this config. bool Equals(const ProxyConfig& other) const; @@ -124,6 +125,13 @@ class ProxyConfig { // use a PAC resolver. bool MayRequirePACResolver() const; + // Creates a textual dump of the configuration. + std::string ToString() const; + + // Creates a Value dump of this configuration. The caller is responsible for + // deleting the returned value. + Value* ToValue() const; + ProxyRules& proxy_rules() { return proxy_rules_; } @@ -185,13 +193,6 @@ class ProxyConfig { } // namespace net -// Dumps a human-readable string representation of the configuration to |out|; -// used when logging the configuration changes. -std::ostream& operator<<(std::ostream& out, const net::ProxyConfig& config); -// Dumps a human-readable string representation of the |rules| to |out|; -// used for logging and for better unittest failure output. -std::ostream& operator<<(std::ostream& out, - const net::ProxyConfig::ProxyRules& rules); #endif // NET_PROXY_PROXY_CONFIG_H_ diff --git a/net/proxy/proxy_config_unittest.cc b/net/proxy/proxy_config_unittest.cc index 5806f30..864b200 100644 --- a/net/proxy/proxy_config_unittest.cc +++ b/net/proxy/proxy_config_unittest.cc @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include <ostream> - #include "net/proxy/proxy_config.h" #include "net/proxy/proxy_config_service_common_unittest.h" #include "net/proxy/proxy_info.h" @@ -253,12 +251,6 @@ TEST(ProxyConfigTest, ParseProxyRules) { } } -std::string ProxyConfigToString(const ProxyConfig& config) { - std::ostringstream stream; - stream << config; - return stream.str(); -} - TEST(ProxyConfigTest, ToString) { // Manual proxy. { @@ -272,7 +264,7 @@ TEST(ProxyConfigTest, ToString) { "Manual settings:\n" " Proxy server: single-proxy:81\n" " Bypass list: [None]", - ProxyConfigToString(config)); + config.ToString()); } // Autodetect + custom PAC + manual proxy. @@ -288,7 +280,7 @@ TEST(ProxyConfigTest, ToString) { "Manual settings:\n" " Proxy server: single-proxy:81\n" " Bypass list: [None]", - ProxyConfigToString(config)); + config.ToString()); } // Manual proxy with bypass list + bypass local. @@ -309,7 +301,7 @@ TEST(ProxyConfigTest, ToString) { " google.com\n" " bypass2.net:1730\n" " <local>", - ProxyConfigToString(config)); + config.ToString()); } // Proxy-per scheme (HTTP and HTTPS) @@ -327,7 +319,7 @@ TEST(ProxyConfigTest, ToString) { " HTTP: proxy-for-http:1801\n" " HTTPS: proxy-for-https:1802\n" " Bypass list: [None]", - ProxyConfigToString(config)); + config.ToString()); } // Proxy-per scheme (HTTP and SOCKS) @@ -345,7 +337,7 @@ TEST(ProxyConfigTest, ToString) { " HTTP: proxy-for-http:1801\n" " SOCKS: socks4://socks-server:6083\n" " Bypass list: [None]", - ProxyConfigToString(config)); + config.ToString()); } // No proxy. @@ -359,7 +351,7 @@ TEST(ProxyConfigTest, ToString) { "Manual settings:\n" " Proxy server: [None]\n" " Bypass list: [None]", - ProxyConfigToString(config)); + config.ToString()); } // Manual proxy with bypass list + bypass local, list reversed. @@ -381,7 +373,7 @@ TEST(ProxyConfigTest, ToString) { " google.com\n" " bypass2.net:1730\n" " <local>", - ProxyConfigToString(config)); + config.ToString()); } } diff --git a/net/proxy/proxy_service.cc b/net/proxy/proxy_service.cc index 7358f1c..e5d3167 100644 --- a/net/proxy/proxy_service.cc +++ b/net/proxy/proxy_service.cc @@ -8,6 +8,7 @@ #include "base/compiler_specific.h" #include "base/logging.h" +#include "base/values.h" #include "base/histogram.h" #include "base/message_loop.h" #include "base/string_util.h" @@ -40,8 +41,10 @@ using base::TimeTicks; namespace net { -static const size_t kMaxNumNetLogEntries = 100; -static const size_t kDefaultNumPacThreads = 4; +namespace { + +const size_t kMaxNumNetLogEntries = 100; +const size_t kDefaultNumPacThreads = 4; // Config getter that always returns direct settings. class ProxyConfigServiceDirect : public ProxyConfigService { @@ -139,6 +142,33 @@ class ProxyResolverFactoryForNonV8 : public ProxyResolverFactory { } }; +// NetLog parameter to describe a proxy configuration change. +class ProxyConfigChangedNetLogParam : public NetLog::EventParameters { + public: + ProxyConfigChangedNetLogParam(const ProxyConfig& old_config, + const ProxyConfig& new_config) + : old_config_(old_config), + new_config_(new_config) { + } + + virtual Value* ToValue() const { + DictionaryValue* dict = new DictionaryValue(); + // The "old_config" is optional -- the first notification will not have + // any "previous" configuration. + if (old_config_.is_valid()) + dict->Set("old_config", old_config_.ToValue()); + dict->Set("new_config", new_config_.ToValue()); + return dict; + } + + private: + const ProxyConfig old_config_; + const ProxyConfig new_config_; + DISALLOW_COPY_AND_ASSIGN(ProxyConfigChangedNetLogParam); +}; + +} // namespace + // ProxyService::PacRequest --------------------------------------------------- class ProxyService::PacRequest @@ -643,12 +673,24 @@ ProxyConfigService* ProxyService::CreateSystemProxyConfigService( } void ProxyService::OnProxyConfigChanged(const ProxyConfig& config) { + ProxyConfig old_config = config_; ResetProxyConfig(); // Increment the ID to reflect that the config has changed. config_ = config; config_.set_id(next_config_id_++); + // Emit the proxy settings change to the NetLog stream. + if (net_log_) { + scoped_refptr<NetLog::EventParameters> params = + new ProxyConfigChangedNetLogParam(old_config, config); + net_log_->AddEntry(net::NetLog::TYPE_PROXY_CONFIG_CHANGED, + base::TimeTicks::Now(), + NetLog::Source(), + NetLog::PHASE_NONE, + params); + } + if (!config_.MayRequirePACResolver()) { SetReady(); return; |