[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "net/proxy/polling_proxy_config_service.h" |
| 6 | |
danakj | 8a98ca2 | 2016-04-16 02:47:36 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
[email protected] | 23578681 | 2011-12-20 02:15:31 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 10 | #include "base/location.h" |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 11 | #include "base/observer_list.h" |
pranay.kumar | 55c065b6 | 2015-05-15 04:19:01 | [diff] [blame] | 12 | #include "base/single_thread_task_runner.h" |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 13 | #include "base/synchronization/lock.h" |
fdoray | a16c9ba | 2017-02-17 17:51:39 | [diff] [blame] | 14 | #include "base/task_scheduler/post_task.h" |
gab | f767595f | 2016-05-11 18:50:35 | [diff] [blame] | 15 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 16 | #include "net/proxy/proxy_config.h" |
| 17 | |
| 18 | namespace net { |
| 19 | |
| 20 | // Reference-counted wrapper that does all the work (needs to be |
| 21 | // reference-counted since we post tasks between threads; may outlive |
| 22 | // the parent PollingProxyConfigService). |
| 23 | class PollingProxyConfigService::Core |
| 24 | : public base::RefCountedThreadSafe<PollingProxyConfigService::Core> { |
| 25 | public: |
pranay.kumar | 55c065b6 | 2015-05-15 04:19:01 | [diff] [blame] | 26 | Core(base::TimeDelta poll_interval, GetConfigFunction get_config_func) |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 27 | : get_config_func_(get_config_func), |
[email protected] | e9b8d0af | 2010-08-17 04:10:29 | [diff] [blame] | 28 | poll_interval_(poll_interval), |
pranay.kumar | 55c065b6 | 2015-05-15 04:19:01 | [diff] [blame] | 29 | have_initialized_origin_runner_(false), |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 30 | has_config_(false), |
| 31 | poll_task_outstanding_(false), |
pranay.kumar | 55c065b6 | 2015-05-15 04:19:01 | [diff] [blame] | 32 | poll_task_queued_(false) {} |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 33 | |
| 34 | // Called when the parent PollingProxyConfigService is destroyed |
| 35 | // (observers should not be called past this point). |
| 36 | void Orphan() { |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 37 | base::AutoLock l(lock_); |
pranay.kumar | 55c065b6 | 2015-05-15 04:19:01 | [diff] [blame] | 38 | origin_task_runner_ = NULL; |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | bool GetLatestProxyConfig(ProxyConfig* config) { |
| 42 | LazyInitializeOriginLoop(); |
pranay.kumar | 55c065b6 | 2015-05-15 04:19:01 | [diff] [blame] | 43 | DCHECK(origin_task_runner_->BelongsToCurrentThread()); |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 44 | |
| 45 | OnLazyPoll(); |
| 46 | |
| 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; |
| 52 | } |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | void AddObserver(Observer* observer) { |
| 57 | LazyInitializeOriginLoop(); |
pranay.kumar | 55c065b6 | 2015-05-15 04:19:01 | [diff] [blame] | 58 | DCHECK(origin_task_runner_->BelongsToCurrentThread()); |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 59 | observers_.AddObserver(observer); |
| 60 | } |
| 61 | |
| 62 | void RemoveObserver(Observer* observer) { |
pranay.kumar | 55c065b6 | 2015-05-15 04:19:01 | [diff] [blame] | 63 | DCHECK(origin_task_runner_->BelongsToCurrentThread()); |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 64 | observers_.RemoveObserver(observer); |
| 65 | } |
| 66 | |
| 67 | // Check for a new configuration if enough time has elapsed. |
| 68 | void OnLazyPoll() { |
| 69 | LazyInitializeOriginLoop(); |
pranay.kumar | 55c065b6 | 2015-05-15 04:19:01 | [diff] [blame] | 70 | DCHECK(origin_task_runner_->BelongsToCurrentThread()); |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 71 | |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 72 | if (last_poll_time_.is_null() || |
[email protected] | e9b8d0af | 2010-08-17 04:10:29 | [diff] [blame] | 73 | (base::TimeTicks::Now() - last_poll_time_) > poll_interval_) { |
| 74 | CheckForChangesNow(); |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
[email protected] | e9b8d0af | 2010-08-17 04:10:29 | [diff] [blame] | 78 | void CheckForChangesNow() { |
| 79 | LazyInitializeOriginLoop(); |
pranay.kumar | 55c065b6 | 2015-05-15 04:19:01 | [diff] [blame] | 80 | DCHECK(origin_task_runner_->BelongsToCurrentThread()); |
[email protected] | e9b8d0af | 2010-08-17 04:10:29 | [diff] [blame] | 81 | |
| 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; |
| 88 | } |
| 89 | |
| 90 | last_poll_time_ = base::TimeTicks::Now(); |
| 91 | poll_task_outstanding_ = true; |
| 92 | poll_task_queued_ = false; |
fdoray | a16c9ba | 2017-02-17 17:51:39 | [diff] [blame] | 93 | base::PostTaskWithTraits( |
fdoray | 2f60799d | 2017-05-03 22:54:58 | [diff] [blame] | 94 | FROM_HERE, |
| 95 | {base::MayBlock(), base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}, |
fdoray | a16c9ba | 2017-02-17 17:51:39 | [diff] [blame] | 96 | base::Bind(&Core::PollAsync, this, get_config_func_)); |
[email protected] | e9b8d0af | 2010-08-17 04:10:29 | [diff] [blame] | 97 | } |
| 98 | |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 99 | private: |
[email protected] | a981330 | 2012-04-28 09:29:28 | [diff] [blame] | 100 | friend class base::RefCountedThreadSafe<Core>; |
| 101 | ~Core() {} |
| 102 | |
fdoray | a16c9ba | 2017-02-17 17:51:39 | [diff] [blame] | 103 | void PollAsync(GetConfigFunction func) { |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 104 | ProxyConfig config; |
| 105 | func(&config); |
| 106 | |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 107 | base::AutoLock l(lock_); |
pranay.kumar | 55c065b6 | 2015-05-15 04:19:01 | [diff] [blame] | 108 | if (origin_task_runner_.get()) { |
| 109 | origin_task_runner_->PostTask( |
[email protected] | 9049948 | 2013-06-01 00:39:50 | [diff] [blame] | 110 | FROM_HERE, base::Bind(&Core::GetConfigCompleted, this, config)); |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 111 | } |
| 112 | } |
| 113 | |
| 114 | // Called after the worker thread has finished retrieving a configuration. |
| 115 | void GetConfigCompleted(const ProxyConfig& config) { |
| 116 | DCHECK(poll_task_outstanding_); |
| 117 | poll_task_outstanding_ = false; |
| 118 | |
pranay.kumar | 55c065b6 | 2015-05-15 04:19:01 | [diff] [blame] | 119 | if (!origin_task_runner_.get()) |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 120 | return; // Was orphaned (parent has already been destroyed). |
| 121 | |
pranay.kumar | 55c065b6 | 2015-05-15 04:19:01 | [diff] [blame] | 122 | DCHECK(origin_task_runner_->BelongsToCurrentThread()); |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 123 | |
| 124 | if (!has_config_ || !last_config_.Equals(config)) { |
| 125 | // If the configuration has changed, notify the observers. |
| 126 | has_config_ = true; |
| 127 | last_config_ = config; |
ericwilligers | 9d64a5f | 2016-10-18 00:28:49 | [diff] [blame] | 128 | for (auto& observer : observers_) |
| 129 | observer.OnProxyConfigChanged(config, ProxyConfigService::CONFIG_VALID); |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 130 | } |
[email protected] | e9b8d0af | 2010-08-17 04:10:29 | [diff] [blame] | 131 | |
| 132 | if (poll_task_queued_) |
| 133 | CheckForChangesNow(); |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void LazyInitializeOriginLoop() { |
| 137 | // TODO(eroman): Really this should be done in the constructor, but right |
| 138 | // now chrome is constructing the ProxyConfigService on the |
| 139 | // UI thread so we can't cache the IO thread for the purpose |
| 140 | // of DCHECKs until the first call is made. |
pranay.kumar | 55c065b6 | 2015-05-15 04:19:01 | [diff] [blame] | 141 | if (!have_initialized_origin_runner_) { |
| 142 | origin_task_runner_ = base::ThreadTaskRunnerHandle::Get(); |
| 143 | have_initialized_origin_runner_ = true; |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | GetConfigFunction get_config_func_; |
brettw | 236d317 | 2015-06-03 16:31:43 | [diff] [blame] | 148 | base::ObserverList<Observer> observers_; |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 149 | ProxyConfig last_config_; |
| 150 | base::TimeTicks last_poll_time_; |
| 151 | base::TimeDelta poll_interval_; |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 152 | |
[email protected] | 20305ec | 2011-01-21 04:55:52 | [diff] [blame] | 153 | base::Lock lock_; |
pranay.kumar | 55c065b6 | 2015-05-15 04:19:01 | [diff] [blame] | 154 | scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_; |
[email protected] | e9b8d0af | 2010-08-17 04:10:29 | [diff] [blame] | 155 | |
pranay.kumar | 55c065b6 | 2015-05-15 04:19:01 | [diff] [blame] | 156 | bool have_initialized_origin_runner_; |
[email protected] | e9b8d0af | 2010-08-17 04:10:29 | [diff] [blame] | 157 | bool has_config_; |
| 158 | bool poll_task_outstanding_; |
| 159 | bool poll_task_queued_; |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 160 | }; |
| 161 | |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 162 | void PollingProxyConfigService::AddObserver(Observer* observer) { |
| 163 | core_->AddObserver(observer); |
| 164 | } |
| 165 | |
| 166 | void PollingProxyConfigService::RemoveObserver(Observer* observer) { |
| 167 | core_->RemoveObserver(observer); |
| 168 | } |
| 169 | |
[email protected] | 3a29593d | 2011-04-11 10:07:52 | [diff] [blame] | 170 | ProxyConfigService::ConfigAvailability |
| 171 | PollingProxyConfigService::GetLatestProxyConfig(ProxyConfig* config) { |
| 172 | return core_->GetLatestProxyConfig(config) ? CONFIG_VALID : CONFIG_PENDING; |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void PollingProxyConfigService::OnLazyPoll() { |
| 176 | core_->OnLazyPoll(); |
| 177 | } |
| 178 | |
[email protected] | be1a48b | 2011-01-20 00:12:13 | [diff] [blame] | 179 | PollingProxyConfigService::PollingProxyConfigService( |
| 180 | base::TimeDelta poll_interval, |
| 181 | GetConfigFunction get_config_func) |
| 182 | : core_(new Core(poll_interval, get_config_func)) { |
| 183 | } |
| 184 | |
| 185 | PollingProxyConfigService::~PollingProxyConfigService() { |
| 186 | core_->Orphan(); |
| 187 | } |
| 188 | |
[email protected] | e9b8d0af | 2010-08-17 04:10:29 | [diff] [blame] | 189 | void PollingProxyConfigService::CheckForChangesNow() { |
| 190 | core_->CheckForChangesNow(); |
| 191 | } |
| 192 | |
[email protected] | 11965500 | 2010-07-23 06:02:40 | [diff] [blame] | 193 | } // namespace net |