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

Side by Side Diff: components/proxy_config/pref_proxy_config_tracker_impl.cc

Issue 2860033003: Don't delay creation of system URLRequestContext until first use (Closed)
Patch Set: Remove comment Created 3 years, 7 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
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 "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 std::unique_ptr<net::ProxyConfigService> base_service,
28 : base_service_(base_service), 28 ProxyPrefs::ConfigState initial_config_state,
29 pref_config_state_(ProxyPrefs::CONFIG_UNSET), 29 const net::ProxyConfig& initial_config)
30 pref_config_read_pending_(true), 30 : base_service_(std::move(base_service)),
31 pref_config_state_(initial_config_state),
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());
(...skipping 19 matching lines...) Expand all
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(
151 std::unique_ptr<net::ProxyConfigService> base_service) { 149 std::unique_ptr<net::ProxyConfigService> base_service) {
152 proxy_config_service_impl_ = 150 DCHECK(!proxy_config_service_impl_);
153 new ProxyConfigServiceImpl(base_service.release()); 151 proxy_config_service_impl_ = new ProxyConfigServiceImpl(
152 std::move(base_service), config_state_, pref_config_);
154 VLOG(1) << this << ": set chrome proxy config service to " 153 VLOG(1) << this << ": set chrome proxy config service to "
155 << proxy_config_service_impl_; 154 << proxy_config_service_impl_;
156 if (proxy_config_service_impl_ && update_pending_) 155 update_pending_ = false;
157 OnProxyConfigChanged(config_state_, pref_config_);
158 156
159 return std::unique_ptr<net::ProxyConfigService>(proxy_config_service_impl_); 157 return std::unique_ptr<net::ProxyConfigService>(proxy_config_service_impl_);
160 } 158 }
161 159
162 void PrefProxyConfigTrackerImpl::DetachFromPrefService() { 160 void PrefProxyConfigTrackerImpl::DetachFromPrefService() {
163 DCHECK(thread_checker_.CalledOnValidThread()); 161 DCHECK(thread_checker_.CalledOnValidThread());
164 // Stop notifications. 162 // Stop notifications.
165 proxy_prefs_.RemoveAll(); 163 proxy_prefs_.RemoveAll();
166 pref_service_ = NULL; 164 pref_service_ = NULL;
167 proxy_config_service_impl_ = NULL; 165 proxy_config_service_impl_ = NULL;
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
352 (config_state_ != ProxyPrefs::CONFIG_UNSET && 350 (config_state_ != ProxyPrefs::CONFIG_UNSET &&
353 !pref_config_.Equals(new_config))) { 351 !pref_config_.Equals(new_config))) {
354 config_state_ = config_state; 352 config_state_ = config_state;
355 if (config_state_ != ProxyPrefs::CONFIG_UNSET) 353 if (config_state_ != ProxyPrefs::CONFIG_UNSET)
356 pref_config_ = new_config; 354 pref_config_ = new_config;
357 update_pending_ = true; 355 update_pending_ = true;
358 } 356 }
359 if (update_pending_) 357 if (update_pending_)
360 OnProxyConfigChanged(config_state, new_config); 358 OnProxyConfigChanged(config_state, new_config);
361 } 359 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698