Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: chrome/browser/net/pref_proxy_config_service.cc

Issue 6549007: Make CrOS proxy configuration write prefs directly. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove default request context proxy config service patch. Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "chrome/browser/net/pref_proxy_config_service.h" 5 #include "chrome/browser/net/pref_proxy_config_service.h"
6 6
7 #include "base/values.h" 7 #include "base/values.h"
8 #include "chrome/browser/browser_process.h"
8 #include "chrome/browser/browser_thread.h" 9 #include "chrome/browser/browser_thread.h"
9 #include "chrome/browser/prefs/pref_service.h" 10 #include "chrome/browser/prefs/pref_service.h"
10 #include "chrome/browser/prefs/pref_set_observer.h" 11 #include "chrome/browser/prefs/pref_set_observer.h"
11 #include "chrome/browser/prefs/proxy_config_dictionary.h" 12 #include "chrome/browser/prefs/proxy_config_dictionary.h"
12 #include "chrome/common/notification_details.h" 13 #include "chrome/common/notification_details.h"
13 #include "chrome/common/notification_source.h" 14 #include "chrome/common/notification_source.h"
14 #include "chrome/common/notification_type.h" 15 #include "chrome/common/notification_type.h"
15 #include "chrome/common/pref_names.h" 16 #include "chrome/common/pref_names.h"
16 17
17 PrefProxyConfigTracker::PrefProxyConfigTracker(PrefService* pref_service) 18 PrefProxyConfigTracker::PrefProxyConfigTracker(
18 : pref_service_(pref_service) { 19 PrefService* pref_service,
20 PrefService* fallback_pref_service)
21 : pref_service_(pref_service),
22 fallback_pref_service_(fallback_pref_service) {
19 valid_ = ReadPrefConfig(&pref_config_); 23 valid_ = ReadPrefConfig(&pref_config_);
20 proxy_prefs_observer_.reset( 24 proxy_prefs_observer_.reset(
21 PrefSetObserver::CreateProxyPrefSetObserver(pref_service_, this)); 25 PrefSetObserver::CreateProxyPrefSetObserver(pref_service_, this));
26
27 // Also install an observer on |fallback_pref_service_|, if appropriate.
28 if (fallback_pref_service_) {
29 fallback_proxy_prefs_observer_.reset(
30 PrefSetObserver::CreateProxyPrefSetObserver(fallback_pref_service_,
31 this));
32 }
22 } 33 }
23 34
24 PrefProxyConfigTracker::~PrefProxyConfigTracker() { 35 PrefProxyConfigTracker::~PrefProxyConfigTracker() {
25 DCHECK(pref_service_ == NULL); 36 DCHECK(pref_service_ == NULL);
26 } 37 }
27 38
28 bool PrefProxyConfigTracker::GetProxyConfig(net::ProxyConfig* config) { 39 bool PrefProxyConfigTracker::GetProxyConfig(net::ProxyConfig* config) {
29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 40 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
30 if (valid_) 41 if (valid_)
31 *config = pref_config_; 42 *config = pref_config_;
32 return valid_; 43 return valid_;
33 } 44 }
34 45
35 void PrefProxyConfigTracker::DetachFromPrefService() { 46 void PrefProxyConfigTracker::DetachFromPrefService() {
36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 47 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
37 // Stop notifications. 48 // Stop notifications.
38 proxy_prefs_observer_.reset(); 49 proxy_prefs_observer_.reset();
39 pref_service_ = NULL; 50 pref_service_ = NULL;
51 fallback_proxy_prefs_observer_.reset();
52 fallback_pref_service_ = NULL;
40 } 53 }
41 54
42 void PrefProxyConfigTracker::AddObserver( 55 void PrefProxyConfigTracker::AddObserver(
43 PrefProxyConfigTracker::Observer* observer) { 56 PrefProxyConfigTracker::Observer* observer) {
44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
45 observers_.AddObserver(observer); 58 observers_.AddObserver(observer);
46 } 59 }
47 60
48 void PrefProxyConfigTracker::RemoveObserver( 61 void PrefProxyConfigTracker::RemoveObserver(
49 PrefProxyConfigTracker::Observer* observer) { 62 PrefProxyConfigTracker::Observer* observer) {
50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
51 observers_.RemoveObserver(observer); 64 observers_.RemoveObserver(observer);
52 } 65 }
53 66
54 void PrefProxyConfigTracker::Observe(NotificationType type, 67 void PrefProxyConfigTracker::Observe(NotificationType type,
55 const NotificationSource& source, 68 const NotificationSource& source,
56 const NotificationDetails& details) { 69 const NotificationDetails& details) {
57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
71 const PrefService* pref_source = Source<PrefService>(source).ptr();
58 if (type == NotificationType::PREF_CHANGED && 72 if (type == NotificationType::PREF_CHANGED &&
59 Source<PrefService>(source).ptr() == pref_service_) { 73 (pref_source == pref_service_ || pref_source == fallback_pref_service_)) {
60 net::ProxyConfig new_config; 74 net::ProxyConfig new_config;
61 bool valid = ReadPrefConfig(&new_config); 75 bool valid = ReadPrefConfig(&new_config);
62 BrowserThread::PostTask( 76 BrowserThread::PostTask(
63 BrowserThread::IO, FROM_HERE, 77 BrowserThread::IO, FROM_HERE,
64 NewRunnableMethod(this, 78 NewRunnableMethod(this,
65 &PrefProxyConfigTracker::InstallProxyConfig, 79 &PrefProxyConfigTracker::InstallProxyConfig,
66 new_config, valid)); 80 new_config, valid));
67 } else { 81 } else {
68 NOTREACHED() << "Unexpected notification of type " << type.value; 82 NOTREACHED() << "Unexpected notification of type " << type.value;
69 } 83 }
70 } 84 }
71 85
72 void PrefProxyConfigTracker::InstallProxyConfig(const net::ProxyConfig& config, 86 void PrefProxyConfigTracker::InstallProxyConfig(const net::ProxyConfig& config,
73 bool valid) { 87 bool valid) {
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
75 if (valid_ != valid || (valid && !pref_config_.Equals(config))) { 89 if (valid_ != valid || (valid && !pref_config_.Equals(config))) {
76 valid_ = valid; 90 valid_ = valid;
77 if (valid_) 91 if (valid_)
78 pref_config_ = config; 92 pref_config_ = config;
79 FOR_EACH_OBSERVER(Observer, observers_, OnPrefProxyConfigChanged()); 93 FOR_EACH_OBSERVER(Observer, observers_, OnPrefProxyConfigChanged());
80 } 94 }
81 } 95 }
82 96
83 bool PrefProxyConfigTracker::ReadPrefConfig(net::ProxyConfig* config) { 97 bool PrefProxyConfigTracker::ReadPrefConfig(net::ProxyConfig* config) {
84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
85 99
86 // Clear the configuration. 100 // Clear the configuration.
87 *config = net::ProxyConfig(); 101 *config = net::ProxyConfig();
88 102
89 ProxyConfigDictionary proxy_dict(pref_service_->GetDictionary(prefs::kProxy)); 103 const PrefService::Preference* pref =
104 pref_service_->FindPreference(prefs::kProxy);
105 if (!pref) {
106 NOTREACHED();
107 return true;
108 }
109
110 const DictionaryValue* proxy_value =
111 pref_service_->GetDictionary(prefs::kProxy);
112 if (fallback_pref_service_ &&
113 pref->IsUserModifiable() &&
114 !pref->HasUserSetting()) {
115 // Default to the fallback, if existing.
116 const PrefService::Preference* fallback_pref =
117 fallback_pref_service_->FindPreference(prefs::kProxy);
118 if (!fallback_pref)
119 NOTREACHED();
120 else if (fallback_pref->HasUserSetting())
121 proxy_value = fallback_pref_service_->GetMutableDictionary(prefs::kProxy);
122 }
123
124 ProxyConfigDictionary proxy_dict(proxy_value);
90 125
91 ProxyPrefs::ProxyMode mode; 126 ProxyPrefs::ProxyMode mode;
92 if (!proxy_dict.GetMode(&mode)) { 127 if (!proxy_dict.GetMode(&mode)) {
93 // Fall back to system settings if the mode preference is invalid. 128 // Fall back to system settings if the mode preference is invalid.
94 return false; 129 return false;
95 } 130 }
96 131
97 switch (mode) { 132 switch (mode) {
98 case ProxyPrefs::MODE_SYSTEM: 133 case ProxyPrefs::MODE_SYSTEM:
99 // Use system settings. 134 // Use system settings.
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 PrefProxyConfigService::PrefProxyConfigService( 181 PrefProxyConfigService::PrefProxyConfigService(
147 PrefProxyConfigTracker* tracker, 182 PrefProxyConfigTracker* tracker,
148 net::ProxyConfigService* base_service) 183 net::ProxyConfigService* base_service)
149 : base_service_(base_service), 184 : base_service_(base_service),
150 pref_config_tracker_(tracker), 185 pref_config_tracker_(tracker),
151 registered_observers_(false) { 186 registered_observers_(false) {
152 } 187 }
153 188
154 PrefProxyConfigService::~PrefProxyConfigService() { 189 PrefProxyConfigService::~PrefProxyConfigService() {
155 if (registered_observers_) { 190 if (registered_observers_) {
156 base_service_->RemoveObserver(this); 191 if (base_service_.get())
192 base_service_->RemoveObserver(this);
157 pref_config_tracker_->RemoveObserver(this); 193 pref_config_tracker_->RemoveObserver(this);
158 } 194 }
159 } 195 }
160 196
161 void PrefProxyConfigService::AddObserver( 197 void PrefProxyConfigService::AddObserver(
162 net::ProxyConfigService::Observer* observer) { 198 net::ProxyConfigService::Observer* observer) {
163 RegisterObservers(); 199 RegisterObservers();
164 observers_.AddObserver(observer); 200 observers_.AddObserver(observer);
165 } 201 }
166 202
167 void PrefProxyConfigService::RemoveObserver( 203 void PrefProxyConfigService::RemoveObserver(
168 net::ProxyConfigService::Observer* observer) { 204 net::ProxyConfigService::Observer* observer) {
169 observers_.RemoveObserver(observer); 205 observers_.RemoveObserver(observer);
170 } 206 }
171 207
172 bool PrefProxyConfigService::GetLatestProxyConfig(net::ProxyConfig* config) { 208 bool PrefProxyConfigService::GetLatestProxyConfig(net::ProxyConfig* config) {
173 RegisterObservers(); 209 RegisterObservers();
174 net::ProxyConfig pref_config; 210 net::ProxyConfig pref_config;
175 if (pref_config_tracker_->GetProxyConfig(&pref_config)) { 211 if (pref_config_tracker_->GetProxyConfig(&pref_config)) {
176 *config = pref_config; 212 *config = pref_config;
177 return true; 213 return true;
178 } 214 }
179 215
180 return base_service_->GetLatestProxyConfig(config); 216 if (base_service_.get())
217 return base_service_->GetLatestProxyConfig(config);
218
219 // Fall back to direct configuration.
220 *config = net::ProxyConfig::CreateDirect();
221 return true;
181 } 222 }
182 223
183 void PrefProxyConfigService::OnLazyPoll() { 224 void PrefProxyConfigService::OnLazyPoll() {
184 base_service_->OnLazyPoll(); 225 if (base_service_.get())
226 base_service_->OnLazyPoll();
185 } 227 }
186 228
187 void PrefProxyConfigService::OnProxyConfigChanged( 229 void PrefProxyConfigService::OnProxyConfigChanged(
188 const net::ProxyConfig& config) { 230 const net::ProxyConfig& config) {
189 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 231 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
190 232
191 // Check whether there is a proxy configuration defined by preferences. In 233 // Check whether there is a proxy configuration defined by preferences. In
192 // this case that proxy configuration takes precedence and the change event 234 // this case that proxy configuration takes precedence and the change event
193 // from the delegate proxy service can be disregarded. 235 // from the delegate proxy service can be disregarded.
194 net::ProxyConfig pref_config; 236 net::ProxyConfig pref_config;
(...skipping 17 matching lines...) Expand all
212 net::ProxyConfig new_config; 254 net::ProxyConfig new_config;
213 if (GetLatestProxyConfig(&new_config)) { 255 if (GetLatestProxyConfig(&new_config)) {
214 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_, 256 FOR_EACH_OBSERVER(net::ProxyConfigService::Observer, observers_,
215 OnProxyConfigChanged(new_config)); 257 OnProxyConfigChanged(new_config));
216 } 258 }
217 } 259 }
218 260
219 void PrefProxyConfigService::RegisterObservers() { 261 void PrefProxyConfigService::RegisterObservers() {
220 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 262 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
221 if (!registered_observers_) { 263 if (!registered_observers_) {
222 base_service_->AddObserver(this); 264 if (base_service_.get())
265 base_service_->AddObserver(this);
223 pref_config_tracker_->AddObserver(this); 266 pref_config_tracker_->AddObserver(this);
224 registered_observers_ = true; 267 registered_observers_ = true;
225 } 268 }
226 } 269 }
227 270
228 // static 271 // static
229 void PrefProxyConfigService::RegisterPrefs(PrefService* pref_service) { 272 void PrefProxyConfigService::RegisterPrefs(PrefService* pref_service) {
230 DictionaryValue* default_settings = ProxyConfigDictionary::CreateSystem(); 273 DictionaryValue* default_settings = ProxyConfigDictionary::CreateSystem();
231 pref_service->RegisterDictionaryPref(prefs::kProxy, default_settings); 274 pref_service->RegisterDictionaryPref(prefs::kProxy, default_settings);
232 } 275 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698