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