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 #ifndef COMPONENTS_PROXY_CONFIG_PREF_PROXY_CONFIG_TRACKER_IMPL_H_
6 #define COMPONENTS_PROXY_CONFIG_PREF_PROXY_CONFIG_TRACKER_IMPL_H_
8 #include "base/basictypes.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/observer_list.h"
12 #include "base/prefs/pref_change_registrar.h"
13 #include "base/threading/thread_checker.h"
14 #include "components/proxy_config/pref_proxy_config_tracker.h"
15 #include "components/proxy_config/proxy_config_dictionary.h"
16 #include "net/proxy/proxy_config.h"
17 #include "net/proxy/proxy_config_service.h"
20 class PrefRegistrySimple
;
23 class SingleThreadTaskRunner
;
26 namespace user_prefs
{
27 class PrefRegistrySyncable
;
30 // A net::ProxyConfigService implementation that applies preference proxy
31 // settings (pushed from PrefProxyConfigTrackerImpl) as overrides to the proxy
32 // configuration determined by a baseline delegate ProxyConfigService on
33 // non-ChromeOS platforms. ChromeOS has its own implementation of overrides in
34 // chromeos::ProxyConfigServiceImpl.
35 class ProxyConfigServiceImpl
: public net::ProxyConfigService
,
36 public net::ProxyConfigService::Observer
{
38 // Takes ownership of the passed |base_service|.
39 // GetLatestProxyConfig returns ConfigAvailability::CONFIG_PENDING until
40 // UpdateProxyConfig has been called.
41 explicit ProxyConfigServiceImpl(net::ProxyConfigService
* base_service
);
42 ~ProxyConfigServiceImpl() override
;
44 // ProxyConfigService implementation:
45 void AddObserver(net::ProxyConfigService::Observer
* observer
) override
;
46 void RemoveObserver(net::ProxyConfigService::Observer
* observer
) override
;
47 ConfigAvailability
GetLatestProxyConfig(net::ProxyConfig
* config
) override
;
48 void OnLazyPoll() override
;
50 // Method on IO thread that receives the preference proxy settings pushed from
51 // PrefProxyConfigTrackerImpl.
52 void UpdateProxyConfig(ProxyPrefs::ConfigState config_state
,
53 const net::ProxyConfig
& config
);
56 // ProxyConfigService::Observer implementation:
57 void OnProxyConfigChanged(const net::ProxyConfig
& config
,
58 ConfigAvailability availability
) override
;
60 // Makes sure that the observer registration with the base service is set up.
61 void RegisterObserver();
63 scoped_ptr
<net::ProxyConfigService
> base_service_
;
64 base::ObserverList
<net::ProxyConfigService::Observer
, true> observers_
;
66 // Tracks configuration state of |pref_config_|. |pref_config_| is valid only
67 // if |pref_config_state_| is not CONFIG_UNSET.
68 ProxyPrefs::ConfigState pref_config_state_
;
70 // Configuration as defined by prefs.
71 net::ProxyConfig pref_config_
;
73 // Flag that indicates that a PrefProxyConfigTracker needs to inform us
74 // about a proxy configuration before we may return any configuration.
75 bool pref_config_read_pending_
;
77 // Indicates whether the base service registration is done.
78 bool registered_observer_
;
80 base::ThreadChecker thread_checker_
;
82 DISALLOW_COPY_AND_ASSIGN(ProxyConfigServiceImpl
);
85 // A class that tracks proxy preferences. It translates the configuration
86 // to net::ProxyConfig and pushes the result over to the IO thread for
87 // ProxyConfigServiceImpl::UpdateProxyConfig to use.
88 class PROXY_CONFIG_EXPORT PrefProxyConfigTrackerImpl
89 : public PrefProxyConfigTracker
{
91 PrefProxyConfigTrackerImpl(
92 PrefService
* pref_service
,
93 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner
);
94 ~PrefProxyConfigTrackerImpl() override
;
96 // PrefProxyConfigTracker implementation:
97 scoped_ptr
<net::ProxyConfigService
> CreateTrackingProxyConfigService(
98 scoped_ptr
<net::ProxyConfigService
> base_service
) override
;
100 // Notifies the tracker that the pref service passed upon construction is
101 // about to go away. This must be called from the UI thread.
102 void DetachFromPrefService() override
;
104 // Determines if |config_state| takes precedence regardless, which happens if
105 // config is from policy or extension or other-precede.
106 static bool PrefPrecedes(ProxyPrefs::ConfigState config_state
);
108 // Determines the proxy configuration that should take effect in the network
109 // layer, based on prefs and system configurations.
110 // |pref_state| refers to state of |pref_config|.
111 // |system_availability| refers to availability of |system_config|.
112 // |ignore_fallback_config| indicates if fallback config from prefs should
114 // Returns effective |effective_config| and its state in
115 // |effective_config_source|.
116 static net::ProxyConfigService::ConfigAvailability
GetEffectiveProxyConfig(
117 ProxyPrefs::ConfigState pref_state
,
118 const net::ProxyConfig
& pref_config
,
119 net::ProxyConfigService::ConfigAvailability system_availability
,
120 const net::ProxyConfig
& system_config
,
121 bool ignore_fallback_config
,
122 ProxyPrefs::ConfigState
* effective_config_state
,
123 net::ProxyConfig
* effective_config
);
125 // Converts a ProxyConfigDictionary to net::ProxyConfig representation.
126 // Returns true if the data from in the dictionary is valid, false otherwise.
127 static bool PrefConfigToNetConfig(const ProxyConfigDictionary
& proxy_dict
,
128 net::ProxyConfig
* config
);
130 // Registers the proxy preferences. These are actually registered
131 // the same way in local state and in user prefs.
132 static void RegisterPrefs(PrefRegistrySimple
* registry
);
133 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
135 // Creates a proxy configuration from proxy-related preferences of
136 // |pref_service|. Configuration is stored in |config|, return value indicates
137 // whether the configuration is valid.
138 static ProxyPrefs::ConfigState
ReadPrefConfig(const PrefService
* pref_service
,
139 net::ProxyConfig
* config
);
142 // Get the proxy configuration currently defined by preferences.
143 // Status is indicated in the return value.
144 // Writes the configuration to |config| unless the return value is
145 // CONFIG_UNSET, in which case |config| and |config_source| are not touched.
146 ProxyPrefs::ConfigState
GetProxyConfig(net::ProxyConfig
* config
);
148 // Called when there's a change in prefs proxy config.
149 // Subclasses can extend it for changes in other sources of proxy config.
150 virtual void OnProxyConfigChanged(ProxyPrefs::ConfigState config_state
,
151 const net::ProxyConfig
& config
);
153 void OnProxyPrefChanged();
155 const PrefService
* prefs() const { return pref_service_
; }
156 bool update_pending() const { return update_pending_
; }
159 // Tracks configuration state. |pref_config_| is valid only if |config_state_|
160 // is not CONFIG_UNSET.
161 ProxyPrefs::ConfigState config_state_
;
163 // Configuration as defined by prefs.
164 net::ProxyConfig pref_config_
;
166 PrefService
* pref_service_
;
167 ProxyConfigServiceImpl
* proxy_config_service_impl_
; // Weak ptr.
168 bool update_pending_
; // True if config has not been pushed to network stack.
169 PrefChangeRegistrar proxy_prefs_
;
171 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner_
;
173 base::ThreadChecker thread_checker_
;
175 DISALLOW_COPY_AND_ASSIGN(PrefProxyConfigTrackerImpl
);
178 #endif // COMPONENTS_PROXY_CONFIG_PREF_PROXY_CONFIG_TRACKER_IMPL_H_