Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / net / proxy / polling_proxy_config_service.cc
bloba03422a300d64cbaeffb0edd91a007d5c1754d6e
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.
5 #include "net/proxy/polling_proxy_config_service.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop_proxy.h"
11 #include "base/observer_list.h"
12 #include "base/synchronization/lock.h"
13 #include "base/threading/worker_pool.h"
14 #include "net/proxy/proxy_config.h"
16 namespace net {
18 // Reference-counted wrapper that does all the work (needs to be
19 // reference-counted since we post tasks between threads; may outlive
20 // the parent PollingProxyConfigService).
21 class PollingProxyConfigService::Core
22 : public base::RefCountedThreadSafe<PollingProxyConfigService::Core> {
23 public:
24 Core(base::TimeDelta poll_interval,
25 GetConfigFunction get_config_func)
26 : get_config_func_(get_config_func),
27 poll_interval_(poll_interval),
28 have_initialized_origin_loop_(false),
29 has_config_(false),
30 poll_task_outstanding_(false),
31 poll_task_queued_(false) {
34 // Called when the parent PollingProxyConfigService is destroyed
35 // (observers should not be called past this point).
36 void Orphan() {
37 base::AutoLock l(lock_);
38 origin_loop_proxy_ = NULL;
41 bool GetLatestProxyConfig(ProxyConfig* config) {
42 LazyInitializeOriginLoop();
43 DCHECK(origin_loop_proxy_->BelongsToCurrentThread());
45 OnLazyPoll();
47 // If we have already retrieved the proxy settings (on worker thread)
48 // then return what we last saw.
49 if (has_config_) {
50 *config = last_config_;
51 return true;
53 return false;
56 void AddObserver(Observer* observer) {
57 LazyInitializeOriginLoop();
58 DCHECK(origin_loop_proxy_->BelongsToCurrentThread());
59 observers_.AddObserver(observer);
62 void RemoveObserver(Observer* observer) {
63 DCHECK(origin_loop_proxy_->BelongsToCurrentThread());
64 observers_.RemoveObserver(observer);
67 // Check for a new configuration if enough time has elapsed.
68 void OnLazyPoll() {
69 LazyInitializeOriginLoop();
70 DCHECK(origin_loop_proxy_->BelongsToCurrentThread());
72 if (last_poll_time_.is_null() ||
73 (base::TimeTicks::Now() - last_poll_time_) > poll_interval_) {
74 CheckForChangesNow();
78 void CheckForChangesNow() {
79 LazyInitializeOriginLoop();
80 DCHECK(origin_loop_proxy_->BelongsToCurrentThread());
82 if (poll_task_outstanding_) {
83 // Only allow one task to be outstanding at a time. If we get a poll
84 // request while we are busy, we will defer it until the current poll
85 // completes.
86 poll_task_queued_ = true;
87 return;
90 last_poll_time_ = base::TimeTicks::Now();
91 poll_task_outstanding_ = true;
92 poll_task_queued_ = false;
93 base::WorkerPool::PostTask(
94 FROM_HERE,
95 base::Bind(&Core::PollOnWorkerThread, this, get_config_func_),
96 true);
99 private:
100 friend class base::RefCountedThreadSafe<Core>;
101 ~Core() {}
103 void PollOnWorkerThread(GetConfigFunction func) {
104 ProxyConfig config;
105 func(&config);
107 base::AutoLock l(lock_);
108 if (origin_loop_proxy_) {
109 origin_loop_proxy_->PostTask(
110 FROM_HERE,
111 base::Bind(&Core::GetConfigCompleted, this, config));
115 // Called after the worker thread has finished retrieving a configuration.
116 void GetConfigCompleted(const ProxyConfig& config) {
117 DCHECK(poll_task_outstanding_);
118 poll_task_outstanding_ = false;
120 if (!origin_loop_proxy_)
121 return; // Was orphaned (parent has already been destroyed).
123 DCHECK(origin_loop_proxy_->BelongsToCurrentThread());
125 if (!has_config_ || !last_config_.Equals(config)) {
126 // If the configuration has changed, notify the observers.
127 has_config_ = true;
128 last_config_ = config;
129 FOR_EACH_OBSERVER(Observer, observers_,
130 OnProxyConfigChanged(config,
131 ProxyConfigService::CONFIG_VALID));
134 if (poll_task_queued_)
135 CheckForChangesNow();
138 void LazyInitializeOriginLoop() {
139 // TODO(eroman): Really this should be done in the constructor, but right
140 // now chrome is constructing the ProxyConfigService on the
141 // UI thread so we can't cache the IO thread for the purpose
142 // of DCHECKs until the first call is made.
143 if (!have_initialized_origin_loop_) {
144 origin_loop_proxy_ = base::MessageLoopProxy::current();
145 have_initialized_origin_loop_ = true;
149 GetConfigFunction get_config_func_;
150 ObserverList<Observer> observers_;
151 ProxyConfig last_config_;
152 base::TimeTicks last_poll_time_;
153 base::TimeDelta poll_interval_;
155 base::Lock lock_;
156 scoped_refptr<base::MessageLoopProxy> origin_loop_proxy_;
158 bool have_initialized_origin_loop_;
159 bool has_config_;
160 bool poll_task_outstanding_;
161 bool poll_task_queued_;
164 void PollingProxyConfigService::AddObserver(Observer* observer) {
165 core_->AddObserver(observer);
168 void PollingProxyConfigService::RemoveObserver(Observer* observer) {
169 core_->RemoveObserver(observer);
172 ProxyConfigService::ConfigAvailability
173 PollingProxyConfigService::GetLatestProxyConfig(ProxyConfig* config) {
174 return core_->GetLatestProxyConfig(config) ? CONFIG_VALID : CONFIG_PENDING;
177 void PollingProxyConfigService::OnLazyPoll() {
178 core_->OnLazyPoll();
181 PollingProxyConfigService::PollingProxyConfigService(
182 base::TimeDelta poll_interval,
183 GetConfigFunction get_config_func)
184 : core_(new Core(poll_interval, get_config_func)) {
187 PollingProxyConfigService::~PollingProxyConfigService() {
188 core_->Orphan();
191 void PollingProxyConfigService::CheckForChangesNow() {
192 core_->CheckForChangesNow();
195 } // namespace net