chromeos: Lock proxy settings UI for policy managed network.
[chromium-blink-merge.git] / chrome / browser / chromeos / proxy_config_service_impl.h
blobeacbfb125e2b38e148a8ab8da6cced787e64f17b
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_CHROMEOS_PROXY_CONFIG_SERVICE_IMPL_H_
6 #define CHROME_BROWSER_CHROMEOS_PROXY_CONFIG_SERVICE_IMPL_H_
7 #pragma once
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/values.h"
14 #include "chrome/browser/chromeos/cros/network_library.h"
15 #include "chrome/browser/net/pref_proxy_config_tracker_impl.h"
16 #include "chrome/browser/prefs/pref_member.h"
18 namespace chromeos {
20 // Implementation of proxy config service for chromeos that:
21 // - extends PrefProxyConfigTrackerImpl (and so lives and runs entirely on UI
22 // thread) to handle proxy from prefs (via PrefProxyConfigTrackerImpl) and
23 // system i.e. network (via flimflam notifications)
24 // - exists one per profile and one per local state
25 // - retrieves initial system proxy configuration from cros settings persisted
26 // on chromeos device from chromeos revisions before migration to flimflam,
27 // - persists proxy setting per network in flimflim
28 // - provides network stack with latest effective proxy configuration for
29 // currently active network via PrefProxyConfigTrackerImpl's mechanism of
30 // pushing config to ChromeProxyConfigService
31 // - provides UI with methods to retrieve and modify proxy configuration for
32 // any remembered network (either currently active or non-active) of current
33 // user profile
34 class ProxyConfigServiceImpl
35 : public PrefProxyConfigTrackerImpl,
36 public NetworkLibrary::NetworkManagerObserver,
37 public NetworkLibrary::NetworkObserver {
38 public:
39 // ProxyConfigServiceImpl is created in ProxyServiceFactory::
40 // CreatePrefProxyConfigTrackerImpl via Profile::GetProxyConfigTracker() for
41 // profile or IOThread constructor for local state and is owned by the
42 // respective classes.
44 // From the UI, it is accessed via Profile::GetProxyConfigTracker to allow
45 // user to read or modify the proxy configuration via UIGetProxyConfig or
46 // UISetProxyConfigTo* respectively.
47 // The new modified proxy config, together with proxy from prefs if available,
48 // are used to determine the effective proxy config, which is then pushed
49 // through PrefProxyConfigTrackerImpl to ChromeProxyConfigService to the
50 // network stack.
52 // In contrary to other platforms which simply use the systems' UI to allow
53 // users to configure proxies, we have to implement our own UI on the chromeos
54 // device. This requires extra and specific UI requirements that
55 // net::ProxyConfig does not suffice. So we create an augmented analog to
56 // net:ProxyConfig here to include and handle these UI requirements, e.g.
57 // - state of configuration e.g. where it was picked up from - policy,
58 // extension, etc (refer to ProxyPrefs::ConfigState)
59 // - the read/write access of a proxy setting
60 // - may add more stuff later.
61 // This is then converted to the common net::ProxyConfig before being pushed
62 // to PrefProxyConfigTrackerImpl::OnProxyConfigChanged and then to the network
63 // stack.
64 struct ProxyConfig {
65 // Specifies if proxy config is direct, auto-detect, using pac script,
66 // single-proxy, or proxy-per-scheme.
67 enum Mode {
68 MODE_DIRECT,
69 MODE_AUTO_DETECT,
70 MODE_PAC_SCRIPT,
71 MODE_SINGLE_PROXY,
72 MODE_PROXY_PER_SCHEME,
75 // Proxy setting for mode = direct or auto-detect or using pac script.
76 struct AutomaticProxy {
77 GURL pac_url; // Set if proxy is using pac script.
80 // Proxy setting for mode = single-proxy or proxy-per-scheme.
81 struct ManualProxy {
82 net::ProxyServer server;
85 ProxyConfig();
86 ~ProxyConfig();
88 // Converts net::ProxyConfig to |this|.
89 bool FromNetProxyConfig(const net::ProxyConfig& net_config);
91 // Converts |this| to Dictionary of ProxyConfigDictionary format (which
92 // is the same format used by prefs).
93 DictionaryValue* ToPrefProxyConfig();
95 // Map |scheme| (one of "http", "https", "ftp" or "socks") to the correct
96 // ManualProxy. Returns NULL if scheme is invalid.
97 ManualProxy* MapSchemeToProxy(const std::string& scheme);
99 // We've migrated device settings to flimflam, so we only need to
100 // deserialize previously persisted device settings.
101 // Deserializes from signed setting on device as std::string into a
102 // protobuf and then into the config.
103 bool DeserializeForDevice(const std::string& input);
105 // Serializes config into a ProxyConfigDictionary and then std::string
106 // persisted as string property in flimflam for a network.
107 bool SerializeForNetwork(std::string* output);
109 // Encodes the proxy server as "<url-scheme>=<proxy-scheme>://<proxy>"
110 static void EncodeAndAppendProxyServer(const std::string& scheme,
111 const net::ProxyServer& server,
112 std::string* spec);
114 Mode mode;
116 ProxyPrefs::ConfigState state;
118 // True if user can modify proxy settings via UI.
119 // If proxy is managed by policy or extension or other_precde or is for
120 // shared network but kUseSharedProxies is turned off, it can't be modified
121 // by user.
122 bool user_modifiable;
124 // Set if mode is MODE_DIRECT or MODE_AUTO_DETECT or MODE_PAC_SCRIPT.
125 AutomaticProxy automatic_proxy;
126 // Set if mode is MODE_SINGLE_PROXY.
127 ManualProxy single_proxy;
128 // Set if mode is MODE_PROXY_PER_SCHEME and has http proxy.
129 ManualProxy http_proxy;
130 // Set if mode is MODE_PROXY_PER_SCHEME and has https proxy.
131 ManualProxy https_proxy;
132 // Set if mode is MODE_PROXY_PER_SCHEME and has ftp proxy.
133 ManualProxy ftp_proxy;
134 // Set if mode is MODE_PROXY_PER_SCHEME and has socks proxy.
135 ManualProxy socks_proxy;
137 // Exceptions for when not to use a proxy.
138 net::ProxyBypassRules bypass_rules;
141 // Constructor.
142 explicit ProxyConfigServiceImpl(PrefService* pref_service);
143 virtual ~ProxyConfigServiceImpl();
145 // Called by UI to set service path of |network| to be displayed or edited.
146 // Subsequent UISet* methods will use this network, until UI calls it again
147 // with a different network.
148 void UISetCurrentNetwork(const std::string& current_network);
150 // Called from UI to make the currently active network the one to be displayed
151 // or edited. Subsequent UISet* methods will use this network until UI calls
152 // it again when the active network has changed.
153 void UIMakeActiveNetworkCurrent();
155 // Called from UI to get name of the current network set via
156 // UISetCurrentNetwork or UIMakeActiveNetworkCurrent.
157 void UIGetCurrentNetworkName(std::string* network_name);
159 // Called from UI to retrieve proxy configuration in |current_ui_config_|.
160 void UIGetProxyConfig(ProxyConfig* config);
162 // Called from UI to update proxy configuration for different modes.
163 // Returns true if config is set properly and persisted to flimflam for the
164 // current network (set via UISetCurrentNetwork/UIMakeActiveNetworkCurrent).
165 // If this network is also currently active, config service proceeds to start
166 // activating it on network stack.
167 // Returns false if config is not set properly, probably because information
168 // is incomplete or invalid; while config service won't proceed to activate or
169 // persist this config, the information is "cached" in the service, so that
170 // the next UIGetProxyConfig call will return this latest information.
171 bool UISetProxyConfigToDirect();
172 bool UISetProxyConfigToAutoDetect();
173 bool UISetProxyConfigToPACScript(const GURL& pac_url);
174 bool UISetProxyConfigToSingleProxy(const net::ProxyServer& server);
175 // |scheme| is one of "http", "https", "ftp" or "socks".
176 bool UISetProxyConfigToProxyPerScheme(const std::string& scheme,
177 const net::ProxyServer& server);
178 // Only valid for MODE_SINGLE_PROXY or MODE_PROXY_PER_SCHEME.
179 bool UISetProxyConfigBypassRules(const net::ProxyBypassRules& bypass_rules);
181 // Add/Remove callback functions for notification when network to be viewed is
182 // changed by the UI.
183 void AddNotificationCallback(base::Closure callback);
184 void RemoveNotificationCallback(base::Closure callback);
186 // PrefProxyConfigTrackerImpl implementation.
187 virtual void OnProxyConfigChanged(ProxyPrefs::ConfigState config_state,
188 const net::ProxyConfig& config) OVERRIDE;
190 // NetworkLibrary::NetworkManagerObserver implementation.
191 virtual void OnNetworkManagerChanged(NetworkLibrary* cros) OVERRIDE;
193 // NetworkLibrary::NetworkObserver implementation.
194 virtual void OnNetworkChanged(NetworkLibrary* cros,
195 const Network* network) OVERRIDE;
197 // Register UseShardProxies preference.
198 static void RegisterPrefs(PrefService* pref_service);
200 #if defined(UNIT_TEST)
201 void SetTesting(ProxyConfig* test_config) {
202 UIMakeActiveNetworkCurrent();
203 if (test_config) {
204 std::string value;
205 test_config->SerializeForNetwork(&value);
206 SetProxyConfigForNetwork(active_network_, value, false);
209 #endif // defined(UNIT_TEST)
211 private:
212 // content::NotificationObserver implementation.
213 virtual void Observe(int type,
214 const content::NotificationSource& source,
215 const content::NotificationDetails& details) OVERRIDE;
217 // Called from the various UISetProxyConfigTo*.
218 void OnUISetProxyConfig();
220 // Called from OnNetworkManagerChanged and OnNetworkChanged for currently
221 // active network, to handle previously active network, new active network,
222 // and if necessary, migrates device settings to flimflam and/or activates
223 // proxy setting of new network.
224 void OnActiveNetworkChanged(NetworkLibrary* cros,
225 const Network* active_network);
227 // Sets proxy config for |network_path| into flimflam and activates setting
228 // if the network is currently active. If |only_set_if_empty| is true,
229 // proxy will be set and saved only if network has no proxy.
230 void SetProxyConfigForNetwork(const std::string& network_path,
231 const std::string& value,
232 bool only_set_if_empty);
234 // Returns value of UseSharedProxies preference if it's not default, else
235 // returns false if user is logged in and true otherwise.
236 bool GetUseSharedProxies();
238 // Determines effective proxy config based on prefs from config tracker,
239 // |network| and if user is using shared proxies.
240 // If |activate| is true, effective config is stored in |active_config_| and
241 // activated on network stack, and hence, picked up by observers.
242 // if |activate| is false, effective config is stored in |current_ui_config_|
243 // but not activated on network stack, and hence, not picked up by observers.
244 void DetermineEffectiveConfig(const Network* network, bool activate);
246 // Determines |current_ui_config_| based on |network|, called from
247 // UISetCurrentNetwork and UIMakeActiveNetworkActive.
248 void OnUISetCurrentNetwork(const Network* network);
250 // Returns true if proxy is to be ignored for network, which happens if
251 // network is shared and use-shared-proxies is turned off.
252 bool IgnoreProxy(const Network* network) {
253 return network->profile_type() == PROFILE_SHARED && !GetUseSharedProxies();
256 // Reset UI cache variables that keep track of UI activities.
257 void ResetUICache();
259 void FetchProxyPolicy();
261 // Data members.
263 // Service path of currently active network (determined via flimflam
264 // notifications); if effective proxy config is from system, proxy of this
265 // network will be the one taking effect.
266 std::string active_network_;
268 // State of |active_config_|. |active_config_| is only valid if
269 // |active_config_state_| is not ProxyPrefs::CONFIG_UNSET.
270 ProxyPrefs::ConfigState active_config_state_;
272 // Active proxy configuration, which could be from prefs or network.
273 net::ProxyConfig active_config_;
275 // Proxy config retreived from device, in format generated from
276 // SerializeForNetwork, that can be directly set into flimflam.
277 std::string device_config_;
279 // Service path of network whose proxy configuration is being displayed or
280 // edited via UI, separate from |active_network_| which may be same or
281 // different.
282 std::string current_ui_network_;
284 // Proxy configuration of |current_ui_network_|.
285 ProxyConfig current_ui_config_;
287 // Track changes in UseSharedProxies user preference.
288 BooleanPrefMember use_shared_proxies_;
290 // Callbacks for notification when network to be viewed has been changed from
291 // the UI.
292 std::vector<base::Closure> callbacks_;
294 base::WeakPtrFactory<ProxyConfigServiceImpl> pointer_factory_;
296 DISALLOW_COPY_AND_ASSIGN(ProxyConfigServiceImpl);
299 } // namespace chromeos
301 #endif // CHROME_BROWSER_CHROMEOS_PROXY_CONFIG_SERVICE_IMPL_H_