| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/proxy/polling_proxy_config_service.h" | 5 #include "net/proxy/polling_proxy_config_service.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/message_loop/message_loop_proxy.h" | 10 #include "base/message_loop/message_loop_proxy.h" |
| 11 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
| 12 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
| 13 #include "base/threading/worker_pool.h" | 13 #include "base/threading/worker_pool.h" |
| 14 #include "net/proxy/proxy_config.h" | 14 #include "net/proxy/proxy_config.h" |
| 15 | 15 |
| 16 namespace net { | 16 namespace net { |
| 17 | 17 |
| 18 // Reference-counted wrapper that does all the work (needs to be | 18 // Reference-counted wrapper that does all the work (needs to be |
| 19 // reference-counted since we post tasks between threads; may outlive | 19 // reference-counted since we post tasks between threads; may outlive |
| 20 // the parent PollingProxyConfigService). | 20 // the parent PollingProxyConfigService). |
| 21 class PollingProxyConfigService::Core | 21 class PollingProxyConfigService::Core |
| 22 : public base::RefCountedThreadSafe<PollingProxyConfigService::Core> { | 22 : public base::RefCountedThreadSafe<PollingProxyConfigService::Core> { |
| 23 public: | 23 public: |
| 24 Core(base::TimeDelta poll_interval, | 24 Core(base::TimeDelta poll_interval, GetConfigFunction get_config_func) |
| 25 GetConfigFunction get_config_func) | |
| 26 : get_config_func_(get_config_func), | 25 : get_config_func_(get_config_func), |
| 27 poll_interval_(poll_interval), | 26 poll_interval_(poll_interval), |
| 28 have_initialized_origin_loop_(false), | 27 have_initialized_origin_loop_(false), |
| 29 has_config_(false), | 28 has_config_(false), |
| 30 poll_task_outstanding_(false), | 29 poll_task_outstanding_(false), |
| 31 poll_task_queued_(false) { | 30 poll_task_queued_(false) {} |
| 32 } | |
| 33 | 31 |
| 34 // Called when the parent PollingProxyConfigService is destroyed | 32 // Called when the parent PollingProxyConfigService is destroyed |
| 35 // (observers should not be called past this point). | 33 // (observers should not be called past this point). |
| 36 void Orphan() { | 34 void Orphan() { |
| 37 base::AutoLock l(lock_); | 35 base::AutoLock l(lock_); |
| 38 origin_loop_proxy_ = NULL; | 36 origin_loop_proxy_ = NULL; |
| 39 } | 37 } |
| 40 | 38 |
| 41 bool GetLatestProxyConfig(ProxyConfig* config) { | 39 bool GetLatestProxyConfig(ProxyConfig* config) { |
| 42 LazyInitializeOriginLoop(); | 40 LazyInitializeOriginLoop(); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 | 116 |
| 119 if (!origin_loop_proxy_.get()) | 117 if (!origin_loop_proxy_.get()) |
| 120 return; // Was orphaned (parent has already been destroyed). | 118 return; // Was orphaned (parent has already been destroyed). |
| 121 | 119 |
| 122 DCHECK(origin_loop_proxy_->BelongsToCurrentThread()); | 120 DCHECK(origin_loop_proxy_->BelongsToCurrentThread()); |
| 123 | 121 |
| 124 if (!has_config_ || !last_config_.Equals(config)) { | 122 if (!has_config_ || !last_config_.Equals(config)) { |
| 125 // If the configuration has changed, notify the observers. | 123 // If the configuration has changed, notify the observers. |
| 126 has_config_ = true; | 124 has_config_ = true; |
| 127 last_config_ = config; | 125 last_config_ = config; |
| 128 FOR_EACH_OBSERVER(Observer, observers_, | 126 FOR_EACH_OBSERVER( |
| 129 OnProxyConfigChanged(config, | 127 Observer, |
| 130 ProxyConfigService::CONFIG_VALID)); | 128 observers_, |
| 129 OnProxyConfigChanged(config, ProxyConfigService::CONFIG_VALID)); |
| 131 } | 130 } |
| 132 | 131 |
| 133 if (poll_task_queued_) | 132 if (poll_task_queued_) |
| 134 CheckForChangesNow(); | 133 CheckForChangesNow(); |
| 135 } | 134 } |
| 136 | 135 |
| 137 void LazyInitializeOriginLoop() { | 136 void LazyInitializeOriginLoop() { |
| 138 // TODO(eroman): Really this should be done in the constructor, but right | 137 // TODO(eroman): Really this should be done in the constructor, but right |
| 139 // now chrome is constructing the ProxyConfigService on the | 138 // now chrome is constructing the ProxyConfigService on the |
| 140 // UI thread so we can't cache the IO thread for the purpose | 139 // UI thread so we can't cache the IO thread for the purpose |
| (...skipping 21 matching lines...) Expand all Loading... |
| 162 | 161 |
| 163 void PollingProxyConfigService::AddObserver(Observer* observer) { | 162 void PollingProxyConfigService::AddObserver(Observer* observer) { |
| 164 core_->AddObserver(observer); | 163 core_->AddObserver(observer); |
| 165 } | 164 } |
| 166 | 165 |
| 167 void PollingProxyConfigService::RemoveObserver(Observer* observer) { | 166 void PollingProxyConfigService::RemoveObserver(Observer* observer) { |
| 168 core_->RemoveObserver(observer); | 167 core_->RemoveObserver(observer); |
| 169 } | 168 } |
| 170 | 169 |
| 171 ProxyConfigService::ConfigAvailability | 170 ProxyConfigService::ConfigAvailability |
| 172 PollingProxyConfigService::GetLatestProxyConfig(ProxyConfig* config) { | 171 PollingProxyConfigService::GetLatestProxyConfig(ProxyConfig* config) { |
| 173 return core_->GetLatestProxyConfig(config) ? CONFIG_VALID : CONFIG_PENDING; | 172 return core_->GetLatestProxyConfig(config) ? CONFIG_VALID : CONFIG_PENDING; |
| 174 } | 173 } |
| 175 | 174 |
| 176 void PollingProxyConfigService::OnLazyPoll() { | 175 void PollingProxyConfigService::OnLazyPoll() { |
| 177 core_->OnLazyPoll(); | 176 core_->OnLazyPoll(); |
| 178 } | 177 } |
| 179 | 178 |
| 180 PollingProxyConfigService::PollingProxyConfigService( | 179 PollingProxyConfigService::PollingProxyConfigService( |
| 181 base::TimeDelta poll_interval, | 180 base::TimeDelta poll_interval, |
| 182 GetConfigFunction get_config_func) | 181 GetConfigFunction get_config_func) |
| 183 : core_(new Core(poll_interval, get_config_func)) { | 182 : core_(new Core(poll_interval, get_config_func)) { |
| 184 } | 183 } |
| 185 | 184 |
| 186 PollingProxyConfigService::~PollingProxyConfigService() { | 185 PollingProxyConfigService::~PollingProxyConfigService() { |
| 187 core_->Orphan(); | 186 core_->Orphan(); |
| 188 } | 187 } |
| 189 | 188 |
| 190 void PollingProxyConfigService::CheckForChangesNow() { | 189 void PollingProxyConfigService::CheckForChangesNow() { |
| 191 core_->CheckForChangesNow(); | 190 core_->CheckForChangesNow(); |
| 192 } | 191 } |
| 193 | 192 |
| 194 } // namespace net | 193 } // namespace net |
| OLD | NEW |