OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_CHROMEOS_PROXY_CONFIG_SERVICE_IMPL_H_ | 5 #ifndef CHROME_BROWSER_CHROMEOS_PROXY_CONFIG_SERVICE_IMPL_H_ |
6 #define CHROME_BROWSER_CHROMEOS_PROXY_CONFIG_SERVICE_IMPL_H_ | 6 #define CHROME_BROWSER_CHROMEOS_PROXY_CONFIG_SERVICE_IMPL_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/prefs/pref_member.h" | 12 #include "base/prefs/pref_member.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" | 13 #include "chrome/browser/net/pref_proxy_config_tracker_impl.h" |
| 14 #include "chromeos/network/network_state_handler_observer.h" |
16 | 15 |
17 class PrefRegistrySimple; | 16 class PrefRegistrySimple; |
18 | 17 |
| 18 namespace base { |
| 19 class DictionaryValue; |
| 20 } |
| 21 |
19 namespace user_prefs { | 22 namespace user_prefs { |
20 class PrefRegistrySyncable; | 23 class PrefRegistrySyncable; |
21 } | 24 } |
22 | 25 |
23 namespace chromeos { | 26 namespace chromeos { |
24 | 27 |
| 28 class NetworkState; |
| 29 |
25 // Implementation of proxy config service for chromeos that: | 30 // Implementation of proxy config service for chromeos that: |
26 // - extends PrefProxyConfigTrackerImpl (and so lives and runs entirely on UI | 31 // - extends PrefProxyConfigTrackerImpl (and so lives and runs entirely on UI |
27 // thread) to handle proxy from prefs (via PrefProxyConfigTrackerImpl) and | 32 // thread) to handle proxy from prefs (via PrefProxyConfigTrackerImpl) and |
28 // system i.e. network (via shill notifications) | 33 // system i.e. network (via shill notifications) |
29 // - exists one per profile and one per local state | 34 // - exists one per profile and one per local state |
30 // - retrieves initial system proxy configuration from cros settings persisted | 35 // - retrieves initial system proxy configuration from cros settings persisted |
31 // on chromeos device from chromeos revisions before migration to shill, | 36 // on chromeos device from chromeos revisions before migration to shill, |
32 // - persists proxy setting per network in flimflim | 37 // - persists proxy setting per network in flimflim |
33 // - provides network stack with latest effective proxy configuration for | 38 // - provides network stack with latest effective proxy configuration for |
34 // currently active network via PrefProxyConfigTrackerImpl's mechanism of | 39 // currently active network via PrefProxyConfigTrackerImpl's mechanism of |
35 // pushing config to ChromeProxyConfigService | 40 // pushing config to ChromeProxyConfigService |
36 // - provides UI with methods to retrieve and modify proxy configuration for | 41 // - provides UI with methods to retrieve and modify proxy configuration for |
37 // any remembered network (either currently active or non-active) of current | 42 // any remembered network (either currently active or non-active) of current |
38 // user profile | 43 // user profile |
39 class ProxyConfigServiceImpl : public PrefProxyConfigTrackerImpl, | 44 class ProxyConfigServiceImpl : public PrefProxyConfigTrackerImpl, |
40 public NetworkLibrary::NetworkManagerObserver, | 45 public NetworkStateHandlerObserver { |
41 public NetworkLibrary::NetworkObserver { | |
42 public: | 46 public: |
43 // ProxyConfigServiceImpl is created in ProxyServiceFactory:: | 47 // ProxyConfigServiceImpl is created in ProxyServiceFactory:: |
44 // CreatePrefProxyConfigTrackerImpl via Profile::GetProxyConfigTracker() for | 48 // CreatePrefProxyConfigTrackerImpl via Profile::GetProxyConfigTracker() for |
45 // profile or IOThread constructor for local state and is owned by the | 49 // profile or IOThread constructor for local state and is owned by the |
46 // respective classes. | 50 // respective classes. |
47 // | 51 // |
48 // From the UI, it is accessed via Profile::GetProxyConfigTracker to allow | 52 // From the UI, it is accessed via Profile::GetProxyConfigTracker to allow |
49 // user to read or modify the proxy configuration via UIGetProxyConfig or | 53 // user to read or modify the proxy configuration via UIGetProxyConfig or |
50 // UISetProxyConfigTo* respectively. | 54 // UISetProxyConfigTo* respectively. |
51 // The new modified proxy config, together with proxy from prefs if available, | 55 // The new modified proxy config, together with proxy from prefs if available, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 }; | 91 }; |
88 | 92 |
89 ProxyConfig(); | 93 ProxyConfig(); |
90 ~ProxyConfig(); | 94 ~ProxyConfig(); |
91 | 95 |
92 // Converts net::ProxyConfig to |this|. | 96 // Converts net::ProxyConfig to |this|. |
93 bool FromNetProxyConfig(const net::ProxyConfig& net_config); | 97 bool FromNetProxyConfig(const net::ProxyConfig& net_config); |
94 | 98 |
95 // Converts |this| to Dictionary of ProxyConfigDictionary format (which | 99 // Converts |this| to Dictionary of ProxyConfigDictionary format (which |
96 // is the same format used by prefs). | 100 // is the same format used by prefs). |
97 DictionaryValue* ToPrefProxyConfig(); | 101 base::DictionaryValue* ToPrefProxyConfig(); |
98 | 102 |
99 // Map |scheme| (one of "http", "https", "ftp" or "socks") to the correct | 103 // Map |scheme| (one of "http", "https", "ftp" or "socks") to the correct |
100 // ManualProxy. Returns NULL if scheme is invalid. | 104 // ManualProxy. Returns NULL if scheme is invalid. |
101 ManualProxy* MapSchemeToProxy(const std::string& scheme); | 105 ManualProxy* MapSchemeToProxy(const std::string& scheme); |
102 | 106 |
103 // We've migrated device settings to shill, so we only need to | 107 // We've migrated device settings to shill, so we only need to |
104 // deserialize previously persisted device settings. | 108 // deserialize previously persisted device settings. |
105 // Deserializes from signed setting on device as std::string into a | 109 // Deserializes from signed setting on device as std::string into a |
106 // protobuf and then into the config. | 110 // protobuf and then into the config. |
107 bool DeserializeForDevice(const std::string& input); | 111 bool DeserializeForDevice(const std::string& input); |
(...skipping 27 matching lines...) Expand all Loading... |
135 ManualProxy https_proxy; | 139 ManualProxy https_proxy; |
136 // Set if mode is MODE_PROXY_PER_SCHEME and has ftp proxy. | 140 // Set if mode is MODE_PROXY_PER_SCHEME and has ftp proxy. |
137 ManualProxy ftp_proxy; | 141 ManualProxy ftp_proxy; |
138 // Set if mode is MODE_PROXY_PER_SCHEME and has socks proxy. | 142 // Set if mode is MODE_PROXY_PER_SCHEME and has socks proxy. |
139 ManualProxy socks_proxy; | 143 ManualProxy socks_proxy; |
140 | 144 |
141 // Exceptions for when not to use a proxy. | 145 // Exceptions for when not to use a proxy. |
142 net::ProxyBypassRules bypass_rules; | 146 net::ProxyBypassRules bypass_rules; |
143 }; | 147 }; |
144 | 148 |
145 // Constructor. | |
146 explicit ProxyConfigServiceImpl(PrefService* pref_service); | 149 explicit ProxyConfigServiceImpl(PrefService* pref_service); |
147 virtual ~ProxyConfigServiceImpl(); | 150 virtual ~ProxyConfigServiceImpl(); |
148 | 151 |
149 // Called by UI to set service path of |network| to be displayed or edited. | 152 // Called by UI to set the network with service path |current_network| to be |
150 // Subsequent UISet* methods will use this network, until UI calls it again | 153 // displayed or edited. Subsequent UISet*/UIGet* methods will use this |
151 // with a different network. | 154 // network, until this method is called again. |
152 void UISetCurrentNetwork(const std::string& current_network); | 155 void UISetCurrentNetwork(const std::string& current_network); |
153 | 156 |
154 // Called from UI to make the currently active network the one to be displayed | 157 // Called from UI to make the current default network the one to be displayed |
155 // or edited. Subsequent UISet* methods will use this network until UI calls | 158 // or edited. See UISetCurrentNetwork. |
156 // it again when the active network has changed. | 159 void UIMakeDefaultNetworkCurrent(); |
157 void UIMakeActiveNetworkCurrent(); | |
158 | 160 |
159 // Called from UI to get name of the current network set via | 161 // Called from UI to get name of the current network set via |
160 // UISetCurrentNetwork or UIMakeActiveNetworkCurrent. | 162 // UISetCurrentNetwork or UIMakeDefaultNetworkCurrent. |
161 void UIGetCurrentNetworkName(std::string* network_name); | 163 void UIGetCurrentNetworkName(std::string* network_name); |
162 | 164 |
163 // Called from UI to retrieve proxy configuration in |current_ui_config_|. | 165 // Called from UI to retrieve proxy configuration in |current_ui_config_|. |
164 void UIGetProxyConfig(ProxyConfig* config); | 166 void UIGetProxyConfig(ProxyConfig* config); |
165 | 167 |
166 // Called from UI to update proxy configuration for different modes. | 168 // Called from UI to update proxy configuration for different modes. |
167 // Returns true if config is set properly and persisted to shill for the | 169 // Returns true if config is set properly and persisted to shill for the |
168 // current network (set via UISetCurrentNetwork/UIMakeActiveNetworkCurrent). | 170 // current network (set via UISetCurrentNetwork/UIMakeDefaultNetworkCurrent). |
169 // If this network is also currently active, config service proceeds to start | 171 // If this network is also currently active, config service proceeds to start |
170 // activating it on network stack. | 172 // activating it on network stack. |
171 // Returns false if config is not set properly, probably because information | 173 // Returns false if config is not set properly, probably because information |
172 // is incomplete or invalid; while config service won't proceed to activate or | 174 // is incomplete or invalid; while config service won't proceed to activate or |
173 // persist this config, the information is "cached" in the service, so that | 175 // persist this config, the information is "cached" in the service, so that |
174 // the next UIGetProxyConfig call will return this latest information. | 176 // the next UIGetProxyConfig call will return this latest information. |
175 bool UISetProxyConfigToDirect(); | 177 bool UISetProxyConfigToDirect(); |
176 bool UISetProxyConfigToAutoDetect(); | 178 bool UISetProxyConfigToAutoDetect(); |
177 bool UISetProxyConfigToPACScript(const GURL& pac_url); | 179 bool UISetProxyConfigToPACScript(const GURL& pac_url); |
178 bool UISetProxyConfigToSingleProxy(const net::ProxyServer& server); | 180 bool UISetProxyConfigToSingleProxy(const net::ProxyServer& server); |
179 // |scheme| is one of "http", "https", "ftp" or "socks". | 181 // |scheme| is one of "http", "https", "ftp" or "socks". |
180 bool UISetProxyConfigToProxyPerScheme(const std::string& scheme, | 182 bool UISetProxyConfigToProxyPerScheme(const std::string& scheme, |
181 const net::ProxyServer& server); | 183 const net::ProxyServer& server); |
182 // Only valid for MODE_SINGLE_PROXY or MODE_PROXY_PER_SCHEME. | 184 // Only valid for MODE_SINGLE_PROXY or MODE_PROXY_PER_SCHEME. |
183 bool UISetProxyConfigBypassRules(const net::ProxyBypassRules& bypass_rules); | 185 bool UISetProxyConfigBypassRules(const net::ProxyBypassRules& bypass_rules); |
184 | 186 |
185 // Add/Remove callback functions for notification when network to be viewed is | 187 // Add/Remove callback functions for notification when network to be viewed is |
186 // changed by the UI. | 188 // changed by the UI. |
187 void AddNotificationCallback(base::Closure callback); | 189 void AddNotificationCallback(base::Closure callback); |
188 void RemoveNotificationCallback(base::Closure callback); | 190 void RemoveNotificationCallback(base::Closure callback); |
189 | 191 |
190 // PrefProxyConfigTrackerImpl implementation. | 192 // PrefProxyConfigTrackerImpl implementation. |
191 virtual void OnProxyConfigChanged(ProxyPrefs::ConfigState config_state, | 193 virtual void OnProxyConfigChanged(ProxyPrefs::ConfigState config_state, |
192 const net::ProxyConfig& config) OVERRIDE; | 194 const net::ProxyConfig& config) OVERRIDE; |
193 | 195 |
194 // NetworkLibrary::NetworkManagerObserver implementation. | 196 // NetworkStateHandlerObserver implementation. |
195 virtual void OnNetworkManagerChanged(NetworkLibrary* cros) OVERRIDE; | 197 virtual void DefaultNetworkChanged(const NetworkState* network) OVERRIDE; |
196 | 198 |
197 // NetworkLibrary::NetworkObserver implementation. | 199 // Convert and store the proxy config of |network| to |proxy_config|. Returns |
198 virtual void OnNetworkChanged(NetworkLibrary* cros, | 200 // true if any proxy config is stored in |network| and proxy config was |
199 const Network* network) OVERRIDE; | 201 // successfully converted. |
200 | 202 static bool ParseProxyConfig(const NetworkState& network, |
201 // Parse |network| proxy config and store result in |proxy_config|. | |
202 // Returns true if proxy config was successfully parsed. | |
203 static bool ParseProxyConfig(const Network* network, | |
204 net::ProxyConfig* proxy_config); | 203 net::ProxyConfig* proxy_config); |
205 | 204 |
206 // Register UseShardProxies preference. | 205 // Register UseShardProxies preference. |
207 static void RegisterPrefs(PrefRegistrySimple* registry); | 206 static void RegisterPrefs(PrefRegistrySimple* registry); |
208 static void RegisterUserPrefs(user_prefs::PrefRegistrySyncable* registry); | 207 static void RegisterUserPrefs(user_prefs::PrefRegistrySyncable* registry); |
209 | 208 |
210 #if defined(UNIT_TEST) | 209 #if defined(UNIT_TEST) |
211 void SetTesting(ProxyConfig* test_config) { | 210 void SetTesting(ProxyConfig* test_config) { |
212 UIMakeActiveNetworkCurrent(); | 211 UIMakeDefaultNetworkCurrent(); |
213 if (test_config) { | 212 if (test_config) { |
214 std::string value; | 213 std::string value; |
215 test_config->SerializeForNetwork(&value); | 214 test_config->SerializeForNetwork(&value); |
216 SetProxyConfigForNetwork(active_network_, value, false); | 215 SetProxyConfigForNetwork(current_ui_network_, value, false); |
217 } | 216 } |
218 } | 217 } |
219 #endif // defined(UNIT_TEST) | 218 #endif // defined(UNIT_TEST) |
220 | 219 |
221 private: | 220 private: |
222 // Called when the kUseSharedProxies preference changes. | 221 // Called when the kUseSharedProxies preference changes. |
223 void OnUseSharedProxiesChanged(); | 222 void OnUseSharedProxiesChanged(); |
224 | 223 |
225 // Called from the various UISetProxyConfigTo*. | 224 // Called from the various UISetProxyConfigTo*. |
226 void OnUISetProxyConfig(); | 225 void OnUISetProxyConfig(); |
227 | 226 |
228 // Called from OnNetworkManagerChanged and OnNetworkChanged for currently | |
229 // active network, to handle previously active network, new active network, | |
230 // and if necessary, migrates device settings to shill and/or activates | |
231 // proxy setting of new network. | |
232 void OnActiveNetworkChanged(NetworkLibrary* cros, | |
233 const Network* active_network); | |
234 | |
235 // Sets proxy config for |network_path| into shill and activates setting | 227 // Sets proxy config for |network_path| into shill and activates setting |
236 // if the network is currently active. If |only_set_if_empty| is true, | 228 // if the network is currently active. If |only_set_if_empty| is true, |
237 // proxy will be set and saved only if network has no proxy. | 229 // proxy will be set and saved only if network has no proxy. |
238 void SetProxyConfigForNetwork(const std::string& network_path, | 230 void SetProxyConfigForNetwork(const std::string& network_path, |
239 const std::string& value, | 231 const std::string& value, |
240 bool only_set_if_empty); | 232 bool only_set_if_empty); |
241 | 233 |
242 // Returns value of UseSharedProxies preference if it's not default, else | 234 // Returns value of UseSharedProxies preference if it's not default, else |
243 // returns false if user is logged in and true otherwise. | 235 // returns false if user is logged in and true otherwise. |
244 bool GetUseSharedProxies(); | 236 bool GetUseSharedProxies(); |
245 | 237 |
246 // Returns true if proxy is to be ignored for network, which happens if | 238 // Returns true if proxy is to be ignored for network, which happens if |
247 // network is shared and use-shared-proxies is turned off. | 239 // network is shared and use-shared-proxies is turned off. |
248 bool IgnoreProxy(const Network* network); | 240 bool IgnoreProxy(const NetworkState& network); |
| 241 |
| 242 // Determines, applies and stores the effective proxy config based on the |
| 243 // current default network. |
| 244 void UpdateProxyConfigOfDefaultNetwork(); |
249 | 245 |
250 // Determines effective proxy config based on prefs from config tracker, | 246 // Determines effective proxy config based on prefs from config tracker, |
251 // |network| and if user is using shared proxies. | 247 // |network| and if user is using shared proxies. |
252 // If |activate| is true, effective config is stored in |active_config_| and | 248 // If |activate| is true, effective config is stored in |active_config_| and |
253 // activated on network stack, and hence, picked up by observers. | 249 // activated on network stack, and hence, picked up by observers. |
254 // if |activate| is false, effective config is stored in |current_ui_config_| | 250 // if |activate| is false, effective config is stored in |current_ui_config_| |
255 // but not activated on network stack, and hence, not picked up by observers. | 251 // but not activated on network stack, and hence, not picked up by observers. |
256 void DetermineEffectiveConfig(const Network* network, bool activate); | 252 void DetermineEffectiveConfig(const NetworkState* network, bool activate); |
257 | |
258 // Determines |current_ui_config_| based on |network|, called from | |
259 // UISetCurrentNetwork and UIMakeActiveNetworkActive. | |
260 void OnUISetCurrentNetwork(const Network* network); | |
261 | 253 |
262 // Reset UI cache variables that keep track of UI activities. | 254 // Reset UI cache variables that keep track of UI activities. |
263 void ResetUICache(); | 255 void ResetUICache(); |
264 | 256 |
265 void FetchProxyPolicy(); | 257 void FetchProxyPolicy(); |
266 | 258 |
267 // Data members. | 259 // Service path of last known default network; if effective proxy config is |
268 | 260 // from system, proxy of this network will be the one taking effect. |
269 // Service path of currently active network (determined via shill | 261 std::string default_network_; |
270 // notifications); if effective proxy config is from system, proxy of this | |
271 // network will be the one taking effect. | |
272 std::string active_network_; | |
273 | 262 |
274 // State of |active_config_|. |active_config_| is only valid if | 263 // State of |active_config_|. |active_config_| is only valid if |
275 // |active_config_state_| is not ProxyPrefs::CONFIG_UNSET. | 264 // |active_config_state_| is not ProxyPrefs::CONFIG_UNSET. |
276 ProxyPrefs::ConfigState active_config_state_; | 265 ProxyPrefs::ConfigState active_config_state_; |
277 | 266 |
278 // Active proxy configuration, which could be from prefs or network. | 267 // Active proxy configuration, which could be from prefs or network. |
279 net::ProxyConfig active_config_; | 268 net::ProxyConfig active_config_; |
280 | 269 |
281 // Proxy config retreived from device, in format generated from | 270 // Proxy config retreived from device, in format generated from |
282 // SerializeForNetwork, that can be directly set into shill. | 271 // SerializeForNetwork, that can be directly set into shill. |
283 std::string device_config_; | 272 std::string device_config_; |
284 | 273 |
285 // Service path of network whose proxy configuration is being displayed or | 274 // Service path of network whose proxy configuration is being displayed or |
286 // edited via UI, separate from |active_network_| which may be same or | 275 // edited via UI, separate from |default_network_| which may be same or |
287 // different. | 276 // different. |
288 std::string current_ui_network_; | 277 std::string current_ui_network_; |
289 | 278 |
290 // Proxy configuration of |current_ui_network_|. | 279 // Proxy configuration of |current_ui_network_|. |
291 ProxyConfig current_ui_config_; | 280 ProxyConfig current_ui_config_; |
292 | 281 |
293 // Track changes in UseSharedProxies user preference. | 282 // Track changes in UseSharedProxies user preference. |
294 BooleanPrefMember use_shared_proxies_; | 283 BooleanPrefMember use_shared_proxies_; |
295 | 284 |
296 // Callbacks for notification when network to be viewed has been changed from | 285 // Callbacks for notification when network to be viewed has been changed from |
297 // the UI. | 286 // the UI. |
298 std::vector<base::Closure> callbacks_; | 287 std::vector<base::Closure> callbacks_; |
299 | 288 |
300 base::WeakPtrFactory<ProxyConfigServiceImpl> pointer_factory_; | 289 base::WeakPtrFactory<ProxyConfigServiceImpl> pointer_factory_; |
301 | 290 |
302 DISALLOW_COPY_AND_ASSIGN(ProxyConfigServiceImpl); | 291 DISALLOW_COPY_AND_ASSIGN(ProxyConfigServiceImpl); |
303 }; | 292 }; |
304 | 293 |
305 } // namespace chromeos | 294 } // namespace chromeos |
306 | 295 |
307 #endif // CHROME_BROWSER_CHROMEOS_PROXY_CONFIG_SERVICE_IMPL_H_ | 296 #endif // CHROME_BROWSER_CHROMEOS_PROXY_CONFIG_SERVICE_IMPL_H_ |
OLD | NEW |