1 // Copyright 2013 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 CHROMEOS_NETWORK_PROXY_UI_PROXY_CONFIG_SERVICE_H_
6 #define CHROMEOS_NETWORK_PROXY_UI_PROXY_CONFIG_SERVICE_H_
7 
8 #include <string>
9 
10 #include "base/component_export.h"
11 #include "base/macros.h"
12 #include "components/prefs/pref_change_registrar.h"
13 #include "components/proxy_config/proxy_prefs.h"
14 
15 class PrefService;
16 
17 namespace base {
18 class Value;
19 }
20 
21 namespace chromeos {
22 
23 class NetworkState;
24 class NetworkStateHandler;
25 class NetworkProfileHandler;
26 
27 // This class provides an interface to the UI for getting a network proxy
28 // configuration.
29 // NOTE: This class must be rebuilt with the primary user's profile prefs when
30 // the primary user logs in.
31 // ALSO NOTE: The provided PrefService instances are used both to retrieve proxy
32 // configurations set by an extension, and for ONC policy information associated
33 // with a network. (Per-network proxy configurations are stored in Shill,
34 // but ONC policy configuration is stored in PrefService).
COMPONENT_EXPORT(CHROMEOS_NETWORK)35 class COMPONENT_EXPORT(CHROMEOS_NETWORK) UIProxyConfigService {
36  public:
37   // |local_state_prefs| must not be null. |profile_prefs| can be
38   // null if there is no logged in user, in which case only the local state
39   // (device) prefs will be used. See note above.
40   UIProxyConfigService(PrefService* profile_prefs,
41                        PrefService* local_state_prefs,
42                        NetworkStateHandler* network_state_handler,
43                        NetworkProfileHandler* network_profile_handler);
44   ~UIProxyConfigService();
45 
46   // Generates ONC dictionary for proxy settings enforced for the network, and
47   // writes them to |proxy_settings|. The proxy settings that will be written to
48   // |proxy_settings| will be one of the following (in order of preference):
49   // * A proxy enforced by a user policy (provided by kProxy prefence).
50   // * A proxy set by an extension in the active PrefService (also provided by
51   //   kProxy pref).
52   // * A proxy set by an ONC policy associated with |network_guid|.
53   //
54   // |proxy_settings| is expected to be a dictionary value containing ONC proxy
55   // settings, and will generally contain the proxy settings reported by shill
56   // (which will have user set per-network proxy settings, if they are
57   // available).
58   //
59   // Returns whether |proxy_settings| have been changed.
60   bool MergeEnforcedProxyConfig(const std::string& network_guid,
61                                 base::Value* proxy_settings);
62 
63   // Returns true if there is a default network and it has a proxy configuration
64   // with mode == MODE_FIXED_SERVERS.
65   bool HasDefaultNetworkProxyConfigured();
66 
67   // Returns the ProxyMode for |network| using |local_state_prefs_|. Proxies
68   // configured by policy or extensions are not being considered. The returned
69   // result is used to display a privacy warning to the user which in the
70   // context of managed networks is not helpful (see https://crbug.com/1130566).
71   ProxyPrefs::ProxyMode ProxyModeForNetwork(const NetworkState* network);
72 
73  private:
74   void OnPreferenceChanged(const std::string& pref_name);
75 
76   // GUID of network used for current_ui_config_.
77   std::string current_ui_network_guid_;
78 
79   PrefService* profile_prefs_;  // unowned
80   PrefChangeRegistrar profile_registrar_;
81 
82   PrefService* local_state_prefs_;  // unowned
83   PrefChangeRegistrar local_state_registrar_;
84 
85   NetworkStateHandler* network_state_handler_;      // unowned
86   NetworkProfileHandler* network_profile_handler_;  // unowned
87 
88   DISALLOW_COPY_AND_ASSIGN(UIProxyConfigService);
89 };
90 
91 }  // namespace chromeos
92 
93 #endif  // CHROMEOS_NETWORK_PROXY_UI_PROXY_CONFIG_SERVICE_H_
94