From aafb7821da75fc427ae025ba2c4a864340abf3ac Mon Sep 17 00:00:00 2001 From: "ericroman@google.com" Date: Mon, 4 May 2009 16:44:29 +0000 Subject: Get rid of the static next_id_ counter, to make construction of ProxyConfig on different threads safe. BUG=11323 Review URL: http://codereview.chromium.org/102023 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@15209 0039d316-1c4b-4281-b951-d872f2087c98 --- net/proxy/proxy_config.cc | 8 +++----- net/proxy/proxy_config.h | 2 +- net/proxy/proxy_service.cc | 31 ++++++++++++++++++++++--------- net/proxy/proxy_service.h | 16 ++++++++++++---- 4 files changed, 38 insertions(+), 19 deletions(-) diff --git a/net/proxy/proxy_config.cc b/net/proxy/proxy_config.cc index 20c0e3a..a860c99 100644 --- a/net/proxy/proxy_config.cc +++ b/net/proxy/proxy_config.cc @@ -9,13 +9,10 @@ namespace net { -// static -ProxyConfig::ID ProxyConfig::last_id_ = ProxyConfig::INVALID_ID; - ProxyConfig::ProxyConfig() : auto_detect(false), proxy_bypass_local_names(false), - id_(++last_id_) { + id_(INVALID_ID) { } bool ProxyConfig::Equals(const ProxyConfig& other) const { @@ -140,6 +137,7 @@ std::ostream& operator<<(std::ostream& out, const net::ProxyConfig& config) { out << " }\n"; } - out << "}"; + out << " id: " << config.id() << "\n" + << "}"; return out; } diff --git a/net/proxy/proxy_config.h b/net/proxy/proxy_config.h index 50d9497..868fba9 100644 --- a/net/proxy/proxy_config.h +++ b/net/proxy/proxy_config.h @@ -27,6 +27,7 @@ class ProxyConfig { // Used to numerically identify this configuration. ID id() const { return id_; } + void set_id(int id) { id_ = id; } // True if the proxy configuration should be auto-detected. bool auto_detect; @@ -103,7 +104,6 @@ class ProxyConfig { bool Equals(const ProxyConfig& other) const; private: - static int last_id_; int id_; }; diff --git a/net/proxy/proxy_service.cc b/net/proxy/proxy_service.cc index 4657ec9..0919f45 100644 --- a/net/proxy/proxy_service.cc +++ b/net/proxy/proxy_service.cc @@ -183,8 +183,8 @@ ProxyService::ProxyService(ProxyConfigService* config_service, ProxyResolver* resolver) : config_service_(config_service), resolver_(resolver), + next_config_id_(1), config_is_bad_(false), - config_has_been_updated_(false), ALLOW_THIS_IN_INITIALIZER_LIST(proxy_script_fetcher_callback_( this, &ProxyService::OnScriptFetchCompletion)), fetched_pac_config_id_(ProxyConfig::INVALID_ID), @@ -539,22 +539,35 @@ void ProxyService::DidCompletePacRequest(int config_id, int result_code) { } void ProxyService::UpdateConfig() { - config_has_been_updated_ = true; + bool is_first_update = !config_has_been_initialized(); ProxyConfig latest; - if (config_service_->GetProxyConfig(&latest) != OK) + if (config_service_->GetProxyConfig(&latest) != OK) { + if (is_first_update) { + // Default to direct-connection if the first fetch fails. + LOG(INFO) << "Failed initial proxy configuration fetch."; + SetConfig(ProxyConfig()); + } return; + } config_last_update_time_ = TimeTicks::Now(); - if (latest.Equals(config_)) + if (!is_first_update && latest.Equals(config_)) return; - LOG(INFO) << "New proxy configuration was loaded:\n" << latest; + SetConfig(latest); +} - config_ = latest; - config_is_bad_ = false; +void ProxyService::SetConfig(const ProxyConfig& config) { + config_ = config; - // We have a new config, we should clear the list of bad proxies. + // Increment the ID to reflect that the config has changed. + config_.set_id(next_config_id_++); + + LOG(INFO) << "New proxy configuration was loaded:\n" << config_; + + // Reset state associated with latest config. + config_is_bad_ = false; proxy_retry_info_.clear(); } @@ -563,7 +576,7 @@ void ProxyService::UpdateConfigIfOld() { const TimeDelta kProxyConfigMaxAge = TimeDelta::FromSeconds(5); // Periodically check for a new config. - if (!config_has_been_updated_ || + if (!config_has_been_initialized() || (TimeTicks::Now() - config_last_update_time_) > kProxyConfigMaxAge) UpdateConfig(); } diff --git a/net/proxy/proxy_service.h b/net/proxy/proxy_service.h index 7e8fad4..3ebee8d 100644 --- a/net/proxy/proxy_service.h +++ b/net/proxy/proxy_service.h @@ -118,10 +118,18 @@ class ProxyService { // Identifies the proxy configuration. ProxyConfig::ID config_id() const { return config_.id(); } + // Returns true if we have called UpdateConfig() at least once. + bool config_has_been_initialized() const { + return config_.id() != ProxyConfig::INVALID_ID; + } + // Checks to see if the proxy configuration changed, and then updates config_ // to reference the new configuration. void UpdateConfig(); + // Assign |config| as the current configuration. + void SetConfig(const ProxyConfig& config); + // Tries to update the configuration if it hasn't been checked in a while. void UpdateConfigIfOld(); @@ -174,16 +182,16 @@ class ProxyService { scoped_ptr resolver_; scoped_ptr pac_thread_; - // We store the proxy config and a counter that is incremented each time + // We store the proxy config and a counter (ID) that is incremented each time // the config changes. ProxyConfig config_; + // Increasing ID to give to the next ProxyConfig that we set. + int next_config_id_; + // Indicates that the configuration is bad and should be ignored. bool config_is_bad_; - // false if the ProxyService has not been initialized yet. - bool config_has_been_updated_; - // The time when the proxy configuration was last read from the system. base::TimeTicks config_last_update_time_; -- cgit v1.1