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 CHROME_BROWSER_NET_PREF_PROXY_CONFIG_TRACKER_IMPL_H_
6 #define CHROME_BROWSER_NET_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 "chrome/browser/net/pref_proxy_config_tracker.h"
14 #include "chrome/browser/prefs/proxy_config_dictionary.h"
15 #include "net/proxy/proxy_config.h"
16 #include "net/proxy/proxy_config_service.h"
19 class PrefRegistrySimple
;
21 namespace user_prefs
{
22 class PrefRegistrySyncable
;
25 // A net::ProxyConfigService implementation that applies preference proxy
26 // settings (pushed from PrefProxyConfigTrackerImpl) as overrides to the proxy
27 // configuration determined by a baseline delegate ProxyConfigService on
28 // non-ChromeOS platforms. ChromeOS has its own implementation of overrides in
29 // chromeos::ProxyConfigServiceImpl.
30 class ChromeProxyConfigService
31 : public net::ProxyConfigService
,
32 public net::ProxyConfigService::Observer
{
34 // Takes ownership of the passed |base_service|.
35 // GetLatestProxyConfig returns ConfigAvailability::CONFIG_PENDING until
36 // UpdateProxyConfig has been called.
37 explicit ChromeProxyConfigService(net::ProxyConfigService
* base_service
);
38 ~ChromeProxyConfigService() override
;
40 // ProxyConfigService implementation:
41 void AddObserver(net::ProxyConfigService::Observer
* observer
) override
;
42 void RemoveObserver(net::ProxyConfigService::Observer
* observer
) override
;
43 ConfigAvailability
GetLatestProxyConfig(net::ProxyConfig
* config
) override
;
44 void OnLazyPoll() override
;
46 // Method on IO thread that receives the preference proxy settings pushed from
47 // PrefProxyConfigTrackerImpl.
48 void UpdateProxyConfig(ProxyPrefs::ConfigState config_state
,
49 const net::ProxyConfig
& config
);
52 // ProxyConfigService::Observer implementation:
53 void OnProxyConfigChanged(const net::ProxyConfig
& config
,
54 ConfigAvailability availability
) override
;
56 // Makes sure that the observer registration with the base service is set up.
57 void RegisterObserver();
59 scoped_ptr
<net::ProxyConfigService
> base_service_
;
60 ObserverList
<net::ProxyConfigService::Observer
, true> observers_
;
62 // Tracks configuration state of |pref_config_|. |pref_config_| is valid only
63 // if |pref_config_state_| is not CONFIG_UNSET.
64 ProxyPrefs::ConfigState pref_config_state_
;
66 // Configuration as defined by prefs.
67 net::ProxyConfig pref_config_
;
69 // Flag that indicates that a PrefProxyConfigTracker needs to inform us
70 // about a proxy configuration before we may return any configuration.
71 bool pref_config_read_pending_
;
73 // Indicates whether the base service registration is done.
74 bool registered_observer_
;
76 DISALLOW_COPY_AND_ASSIGN(ChromeProxyConfigService
);
79 // A class that tracks proxy preferences. It translates the configuration
80 // to net::ProxyConfig and pushes the result over to the IO thread for
81 // ChromeProxyConfigService::UpdateProxyConfig to use.
82 class PrefProxyConfigTrackerImpl
: public PrefProxyConfigTracker
{
84 explicit PrefProxyConfigTrackerImpl(PrefService
* pref_service
);
85 ~PrefProxyConfigTrackerImpl() override
;
87 // PrefProxyConfigTracker implementation:
88 scoped_ptr
<net::ProxyConfigService
> CreateTrackingProxyConfigService(
89 scoped_ptr
<net::ProxyConfigService
> base_service
) override
;
91 // Notifies the tracker that the pref service passed upon construction is
92 // about to go away. This must be called from the UI thread.
93 void DetachFromPrefService() override
;
95 // Determines if |config_state| takes precedence regardless, which happens if
96 // config is from policy or extension or other-precede.
97 static bool PrefPrecedes(ProxyPrefs::ConfigState config_state
);
99 // Determines the proxy configuration that should take effect in the network
100 // layer, based on prefs and system configurations.
101 // |pref_state| refers to state of |pref_config|.
102 // |system_availability| refers to availability of |system_config|.
103 // |ignore_fallback_config| indicates if fallback config from prefs should
105 // Returns effective |effective_config| and its state in
106 // |effective_config_source|.
107 static net::ProxyConfigService::ConfigAvailability
GetEffectiveProxyConfig(
108 ProxyPrefs::ConfigState pref_state
,
109 const net::ProxyConfig
& pref_config
,
110 net::ProxyConfigService::ConfigAvailability system_availability
,
111 const net::ProxyConfig
& system_config
,
112 bool ignore_fallback_config
,
113 ProxyPrefs::ConfigState
* effective_config_state
,
114 net::ProxyConfig
* effective_config
);
116 // Converts a ProxyConfigDictionary to net::ProxyConfig representation.
117 // Returns true if the data from in the dictionary is valid, false otherwise.
118 static bool PrefConfigToNetConfig(const ProxyConfigDictionary
& proxy_dict
,
119 net::ProxyConfig
* config
);
121 // Registers the proxy preferences. These are actually registered
122 // the same way in local state and in user prefs.
123 static void RegisterPrefs(PrefRegistrySimple
* registry
);
124 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
126 // Creates a proxy configuration from proxy-related preferences of
127 // |pref_service|. Configuration is stored in |config|, return value indicates
128 // whether the configuration is valid.
129 static ProxyPrefs::ConfigState
ReadPrefConfig(const PrefService
* pref_service
,
130 net::ProxyConfig
* config
);
133 // Get the proxy configuration currently defined by preferences.
134 // Status is indicated in the return value.
135 // Writes the configuration to |config| unless the return value is
136 // CONFIG_UNSET, in which case |config| and |config_source| are not touched.
137 ProxyPrefs::ConfigState
GetProxyConfig(net::ProxyConfig
* config
);
139 // Called when there's a change in prefs proxy config.
140 // Subclasses can extend it for changes in other sources of proxy config.
141 virtual void OnProxyConfigChanged(ProxyPrefs::ConfigState config_state
,
142 const net::ProxyConfig
& config
);
144 void OnProxyPrefChanged();
146 const PrefService
* prefs() const { return pref_service_
; }
147 bool update_pending() const { return update_pending_
; }
150 // Tracks configuration state. |pref_config_| is valid only if |config_state_|
151 // is not CONFIG_UNSET.
152 ProxyPrefs::ConfigState config_state_
;
154 // Configuration as defined by prefs.
155 net::ProxyConfig pref_config_
;
157 PrefService
* pref_service_
;
158 ChromeProxyConfigService
* chrome_proxy_config_service_
; // Weak ptr.
159 bool update_pending_
; // True if config has not been pushed to network stack.
160 PrefChangeRegistrar proxy_prefs_
;
162 DISALLOW_COPY_AND_ASSIGN(PrefProxyConfigTrackerImpl
);
165 #endif // CHROME_BROWSER_NET_PREF_PROXY_CONFIG_TRACKER_IMPL_H_