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"
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"
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
> {
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),
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).
37 base::AutoLock
l(lock_
);
38 origin_loop_proxy_
= NULL
;
41 bool GetLatestProxyConfig(ProxyConfig
* config
) {
42 LazyInitializeOriginLoop();
43 DCHECK(origin_loop_proxy_
->BelongsToCurrentThread());
47 // If we have already retrieved the proxy settings (on worker thread)
48 // then return what we last saw.
50 *config
= last_config_
;
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.
69 LazyInitializeOriginLoop();
70 DCHECK(origin_loop_proxy_
->BelongsToCurrentThread());
72 if (last_poll_time_
.is_null() ||
73 (base::TimeTicks::Now() - last_poll_time_
) > poll_interval_
) {
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
86 poll_task_queued_
= true;
90 last_poll_time_
= base::TimeTicks::Now();
91 poll_task_outstanding_
= true;
92 poll_task_queued_
= false;
93 base::WorkerPool::PostTask(
95 base::Bind(&Core::PollOnWorkerThread
, this, get_config_func_
),
100 friend class base::RefCountedThreadSafe
<Core
>;
103 void PollOnWorkerThread(GetConfigFunction func
) {
107 base::AutoLock
l(lock_
);
108 if (origin_loop_proxy_
) {
109 origin_loop_proxy_
->PostTask(
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.
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_
;
156 scoped_refptr
<base::MessageLoopProxy
> origin_loop_proxy_
;
158 bool have_initialized_origin_loop_
;
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() {
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() {
191 void PollingProxyConfigService::CheckForChangesNow() {
192 core_
->CheckForChangesNow();