OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "components/proxy_config/pref_proxy_config_tracker_impl.h" | 5 #include "components/proxy_config/pref_proxy_config_tracker_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "components/pref_registry/pref_registry_syncable.h" | 16 #include "components/pref_registry/pref_registry_syncable.h" |
17 #include "components/prefs/pref_registry_simple.h" | 17 #include "components/prefs/pref_registry_simple.h" |
18 #include "components/prefs/pref_service.h" | 18 #include "components/prefs/pref_service.h" |
19 #include "components/proxy_config/proxy_config_dictionary.h" | 19 #include "components/proxy_config/proxy_config_dictionary.h" |
20 #include "components/proxy_config/proxy_config_pref_names.h" | 20 #include "components/proxy_config/proxy_config_pref_names.h" |
21 #include "net/proxy/proxy_server.h" | 21 #include "net/proxy/proxy_server.h" |
22 #include "url/gurl.h" | 22 #include "url/gurl.h" |
23 | 23 |
24 //============================= ProxyConfigServiceImpl ======================= | 24 //============================= ProxyConfigServiceImpl ======================= |
25 | 25 |
26 ProxyConfigServiceImpl::ProxyConfigServiceImpl( | 26 ProxyConfigServiceImpl::ProxyConfigServiceImpl( |
27 net::ProxyConfigService* base_service) | 27 net::ProxyConfigService* base_service, |
28 ProxyPrefs::ConfigState initial_config_state, | |
29 const net::ProxyConfig& initial_config) | |
28 : base_service_(base_service), | 30 : base_service_(base_service), |
29 pref_config_state_(ProxyPrefs::CONFIG_UNSET), | 31 pref_config_state_(initial_config_state), |
30 pref_config_read_pending_(true), | 32 pref_config_(initial_config), |
31 registered_observer_(false) { | 33 registered_observer_(false) { |
32 // ProxyConfigServiceImpl is created on the UI thread, but used on the network | 34 // ProxyConfigServiceImpl is created on the UI thread, but used on the network |
33 // thread. | 35 // thread. |
34 thread_checker_.DetachFromThread(); | 36 thread_checker_.DetachFromThread(); |
35 } | 37 } |
36 | 38 |
37 ProxyConfigServiceImpl::~ProxyConfigServiceImpl() { | 39 ProxyConfigServiceImpl::~ProxyConfigServiceImpl() { |
38 if (registered_observer_ && base_service_.get()) | 40 if (registered_observer_ && base_service_.get()) |
39 base_service_->RemoveObserver(this); | 41 base_service_->RemoveObserver(this); |
40 } | 42 } |
41 | 43 |
42 void ProxyConfigServiceImpl::AddObserver( | 44 void ProxyConfigServiceImpl::AddObserver( |
43 net::ProxyConfigService::Observer* observer) { | 45 net::ProxyConfigService::Observer* observer) { |
44 RegisterObserver(); | 46 RegisterObserver(); |
45 observers_.AddObserver(observer); | 47 observers_.AddObserver(observer); |
46 } | 48 } |
47 | 49 |
48 void ProxyConfigServiceImpl::RemoveObserver( | 50 void ProxyConfigServiceImpl::RemoveObserver( |
49 net::ProxyConfigService::Observer* observer) { | 51 net::ProxyConfigService::Observer* observer) { |
50 observers_.RemoveObserver(observer); | 52 observers_.RemoveObserver(observer); |
51 } | 53 } |
52 | 54 |
53 net::ProxyConfigService::ConfigAvailability | 55 net::ProxyConfigService::ConfigAvailability |
54 ProxyConfigServiceImpl::GetLatestProxyConfig(net::ProxyConfig* config) { | 56 ProxyConfigServiceImpl::GetLatestProxyConfig(net::ProxyConfig* config) { |
55 RegisterObserver(); | 57 RegisterObserver(); |
56 | 58 |
57 if (pref_config_read_pending_) | |
58 return net::ProxyConfigService::CONFIG_PENDING; | |
59 | |
60 // Ask the base service if available. | 59 // Ask the base service if available. |
61 net::ProxyConfig system_config; | 60 net::ProxyConfig system_config; |
62 ConfigAvailability system_availability = | 61 ConfigAvailability system_availability = |
63 net::ProxyConfigService::CONFIG_UNSET; | 62 net::ProxyConfigService::CONFIG_UNSET; |
64 if (base_service_.get()) | 63 if (base_service_.get()) |
65 system_availability = base_service_->GetLatestProxyConfig(&system_config); | 64 system_availability = base_service_->GetLatestProxyConfig(&system_config); |
66 | 65 |
67 ProxyPrefs::ConfigState config_state; | 66 ProxyPrefs::ConfigState config_state; |
68 return PrefProxyConfigTrackerImpl::GetEffectiveProxyConfig( | 67 return PrefProxyConfigTrackerImpl::GetEffectiveProxyConfig( |
69 pref_config_state_, pref_config_, system_availability, system_config, | 68 pref_config_state_, pref_config_, system_availability, system_config, |
70 false, &config_state, config); | 69 false, &config_state, config); |
71 } | 70 } |
72 | 71 |
73 void ProxyConfigServiceImpl::OnLazyPoll() { | 72 void ProxyConfigServiceImpl::OnLazyPoll() { |
74 if (base_service_.get()) | 73 if (base_service_.get()) |
75 base_service_->OnLazyPoll(); | 74 base_service_->OnLazyPoll(); |
76 } | 75 } |
77 | 76 |
78 void ProxyConfigServiceImpl::UpdateProxyConfig( | 77 void ProxyConfigServiceImpl::UpdateProxyConfig( |
79 ProxyPrefs::ConfigState config_state, | 78 ProxyPrefs::ConfigState config_state, |
80 const net::ProxyConfig& config) { | 79 const net::ProxyConfig& config) { |
81 DCHECK(thread_checker_.CalledOnValidThread()); | 80 DCHECK(thread_checker_.CalledOnValidThread()); |
82 pref_config_read_pending_ = false; | |
83 pref_config_state_ = config_state; | 81 pref_config_state_ = config_state; |
84 pref_config_ = config; | 82 pref_config_ = config; |
85 | 83 |
86 if (!observers_.might_have_observers()) | 84 if (!observers_.might_have_observers()) |
87 return; | 85 return; |
88 | 86 |
89 // Evaluate the proxy configuration. If GetLatestProxyConfig returns | 87 // Evaluate the proxy configuration. If GetLatestProxyConfig returns |
90 // CONFIG_PENDING, we are using the system proxy service, but it doesn't have | 88 // CONFIG_PENDING, we are using the system proxy service, but it doesn't have |
91 // a valid configuration yet. Once it is ready, OnProxyConfigChanged() will be | 89 // a valid configuration yet. Once it is ready, OnProxyConfigChanged() will be |
92 // called and broadcast the proxy configuration. | 90 // called and broadcast the proxy configuration. |
93 // Note: If a switch between a preference proxy configuration and the system | 91 // Note: If a switch between a preference proxy configuration and the system |
94 // proxy configuration occurs an unnecessary notification might get send if | 92 // proxy configuration occurs an unnecessary notification might get send if |
95 // the two configurations agree. This case should be rare however, so we don't | 93 // the two configurations agree. This case should be rare however, so we don't |
96 // handle that case specially. | 94 // handle that case specially. |
97 net::ProxyConfig new_config; | 95 net::ProxyConfig new_config; |
98 ConfigAvailability availability = GetLatestProxyConfig(&new_config); | 96 ConfigAvailability availability = GetLatestProxyConfig(&new_config); |
99 if (availability != CONFIG_PENDING) { | 97 if (availability != CONFIG_PENDING) { |
100 for (net::ProxyConfigService::Observer& observer : observers_) | 98 for (net::ProxyConfigService::Observer& observer : observers_) |
101 observer.OnProxyConfigChanged(new_config, availability); | 99 observer.OnProxyConfigChanged(new_config, availability); |
102 } | 100 } |
103 } | 101 } |
104 | 102 |
105 void ProxyConfigServiceImpl::OnProxyConfigChanged( | 103 void ProxyConfigServiceImpl::OnProxyConfigChanged( |
106 const net::ProxyConfig& config, | 104 const net::ProxyConfig& config, |
107 ConfigAvailability availability) { | 105 ConfigAvailability availability) { |
108 DCHECK(thread_checker_.CalledOnValidThread()); | 106 DCHECK(thread_checker_.CalledOnValidThread()); |
109 | 107 |
110 // Check whether there is a proxy configuration defined by preferences. In | 108 // Check whether there is a proxy configuration defined by preferences. In |
111 // this case that proxy configuration takes precedence and the change event | 109 // this case that proxy configuration takes precedence and the change event |
112 // from the delegate proxy service can be disregarded. | 110 // from the delegate proxy config service can be disregarded. |
113 if (!PrefProxyConfigTrackerImpl::PrefPrecedes(pref_config_state_)) { | 111 if (!PrefProxyConfigTrackerImpl::PrefPrecedes(pref_config_state_)) { |
114 net::ProxyConfig actual_config; | 112 net::ProxyConfig actual_config; |
115 availability = GetLatestProxyConfig(&actual_config); | 113 availability = GetLatestProxyConfig(&actual_config); |
116 for (net::ProxyConfigService::Observer& observer : observers_) | 114 for (net::ProxyConfigService::Observer& observer : observers_) |
117 observer.OnProxyConfigChanged(actual_config, availability); | 115 observer.OnProxyConfigChanged(actual_config, availability); |
118 } | 116 } |
119 } | 117 } |
120 | 118 |
121 void ProxyConfigServiceImpl::RegisterObserver() { | 119 void ProxyConfigServiceImpl::RegisterObserver() { |
122 DCHECK(thread_checker_.CalledOnValidThread()); | 120 DCHECK(thread_checker_.CalledOnValidThread()); |
123 if (!registered_observer_ && base_service_.get()) { | 121 if (!registered_observer_ && base_service_.get()) { |
124 base_service_->AddObserver(this); | 122 base_service_->AddObserver(this); |
125 registered_observer_ = true; | 123 registered_observer_ = true; |
126 } | 124 } |
127 } | 125 } |
128 | 126 |
129 //========================= PrefProxyConfigTrackerImpl ========================= | 127 //========================= PrefProxyConfigTrackerImpl ========================= |
130 | 128 |
131 PrefProxyConfigTrackerImpl::PrefProxyConfigTrackerImpl( | 129 PrefProxyConfigTrackerImpl::PrefProxyConfigTrackerImpl( |
132 PrefService* pref_service, | 130 PrefService* pref_service, |
133 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) | 131 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) |
134 : pref_service_(pref_service), | 132 : pref_service_(pref_service), |
135 proxy_config_service_impl_(NULL), | 133 proxy_config_service_impl_(NULL), |
136 update_pending_(true), | 134 update_pending_(true), |
eroman
2017/05/12 21:12:59
Can you delete this variable?
Seems like the only
mmenke
2017/05/12 21:29:57
This is used in a subclass, chromeos::ProxyConfigS
eroman
2017/05/12 21:54:55
No need to follow-up if it isn't simple.
Just fig
mmenke
2017/05/12 21:57:58
I think it's probably pretty simple, just didn't w
| |
137 io_task_runner_(io_task_runner) { | 135 io_task_runner_(io_task_runner) { |
138 config_state_ = ReadPrefConfig(pref_service_, &pref_config_); | 136 config_state_ = ReadPrefConfig(pref_service_, &pref_config_); |
139 proxy_prefs_.Init(pref_service); | 137 proxy_prefs_.Init(pref_service); |
140 proxy_prefs_.Add(proxy_config::prefs::kProxy, | 138 proxy_prefs_.Add(proxy_config::prefs::kProxy, |
141 base::Bind(&PrefProxyConfigTrackerImpl::OnProxyPrefChanged, | 139 base::Bind(&PrefProxyConfigTrackerImpl::OnProxyPrefChanged, |
142 base::Unretained(this))); | 140 base::Unretained(this))); |
143 } | 141 } |
144 | 142 |
145 PrefProxyConfigTrackerImpl::~PrefProxyConfigTrackerImpl() { | 143 PrefProxyConfigTrackerImpl::~PrefProxyConfigTrackerImpl() { |
146 DCHECK(pref_service_ == NULL); | 144 DCHECK(pref_service_ == NULL); |
147 } | 145 } |
148 | 146 |
149 std::unique_ptr<net::ProxyConfigService> | 147 std::unique_ptr<net::ProxyConfigService> |
150 PrefProxyConfigTrackerImpl::CreateTrackingProxyConfigService( | 148 PrefProxyConfigTrackerImpl::CreateTrackingProxyConfigService( |
eroman
2017/05/12 21:12:59
The interface for CreateTrackingProxyConfigService
mmenke
2017/05/12 21:29:57
Done (Docs added to PrefProxyConfigTracker)
| |
151 std::unique_ptr<net::ProxyConfigService> base_service) { | 149 std::unique_ptr<net::ProxyConfigService> base_service) { |
152 proxy_config_service_impl_ = | 150 proxy_config_service_impl_ = new ProxyConfigServiceImpl( |
153 new ProxyConfigServiceImpl(base_service.release()); | 151 base_service.release(), config_state_, pref_config_); |
154 VLOG(1) << this << ": set chrome proxy config service to " | 152 VLOG(1) << this << ": set chrome proxy config service to " |
155 << proxy_config_service_impl_; | 153 << proxy_config_service_impl_; |
156 if (proxy_config_service_impl_ && update_pending_) | 154 update_pending_ = false; |
157 OnProxyConfigChanged(config_state_, pref_config_); | |
158 | 155 |
159 return std::unique_ptr<net::ProxyConfigService>(proxy_config_service_impl_); | 156 return std::unique_ptr<net::ProxyConfigService>(proxy_config_service_impl_); |
160 } | 157 } |
161 | 158 |
162 void PrefProxyConfigTrackerImpl::DetachFromPrefService() { | 159 void PrefProxyConfigTrackerImpl::DetachFromPrefService() { |
163 DCHECK(thread_checker_.CalledOnValidThread()); | 160 DCHECK(thread_checker_.CalledOnValidThread()); |
164 // Stop notifications. | 161 // Stop notifications. |
165 proxy_prefs_.RemoveAll(); | 162 proxy_prefs_.RemoveAll(); |
166 pref_service_ = NULL; | 163 pref_service_ = NULL; |
167 proxy_config_service_impl_ = NULL; | 164 proxy_config_service_impl_ = NULL; |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
352 (config_state_ != ProxyPrefs::CONFIG_UNSET && | 349 (config_state_ != ProxyPrefs::CONFIG_UNSET && |
353 !pref_config_.Equals(new_config))) { | 350 !pref_config_.Equals(new_config))) { |
354 config_state_ = config_state; | 351 config_state_ = config_state; |
355 if (config_state_ != ProxyPrefs::CONFIG_UNSET) | 352 if (config_state_ != ProxyPrefs::CONFIG_UNSET) |
356 pref_config_ = new_config; | 353 pref_config_ = new_config; |
357 update_pending_ = true; | 354 update_pending_ = true; |
358 } | 355 } |
359 if (update_pending_) | 356 if (update_pending_) |
360 OnProxyConfigChanged(config_state, new_config); | 357 OnProxyConfigChanged(config_state, new_config); |
361 } | 358 } |
OLD | NEW |