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