Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(965)

Side by Side Diff: chrome/browser/chromeos/ui_proxy_config_service.cc

Issue 16398005: Extract common per-network proxy configuration functions. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Nit. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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 #include "chrome/browser/chromeos/ui_proxy_config_service.h" 5 #include "chrome/browser/chromeos/ui_proxy_config_service.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/values.h" 9 #include "base/values.h"
10 #include "chrome/browser/chromeos/cros/cros_library.h" 10 #include "chrome/browser/chromeos/net/proxy_config_handler.h"
11 #include "chrome/browser/chromeos/cros/network_library.h"
12 #include "chrome/browser/chromeos/proxy_config_service_impl.h" 11 #include "chrome/browser/chromeos/proxy_config_service_impl.h"
13 #include "chromeos/network/onc/onc_utils.h" 12 #include "chromeos/network/network_state.h"
13 #include "chromeos/network/network_state_handler.h"
14 #include "grit/generated_resources.h" 14 #include "grit/generated_resources.h"
15 #include "net/proxy/proxy_config.h" 15 #include "net/proxy/proxy_config.h"
16 16
17 namespace chromeos { 17 namespace chromeos {
18 18
19 namespace { 19 namespace {
20 20
21 const char* ModeToString(UIProxyConfig::Mode mode) { 21 const char* ModeToString(UIProxyConfig::Mode mode) {
22 switch (mode) { 22 switch (mode) {
23 case UIProxyConfig::MODE_DIRECT: 23 case UIProxyConfig::MODE_DIRECT:
24 return "direct"; 24 return "direct";
25 case UIProxyConfig::MODE_AUTO_DETECT: 25 case UIProxyConfig::MODE_AUTO_DETECT:
26 return "auto-detect"; 26 return "auto-detect";
27 case UIProxyConfig::MODE_PAC_SCRIPT: 27 case UIProxyConfig::MODE_PAC_SCRIPT:
28 return "pacurl"; 28 return "pacurl";
29 case UIProxyConfig::MODE_SINGLE_PROXY: 29 case UIProxyConfig::MODE_SINGLE_PROXY:
30 return "single-proxy"; 30 return "single-proxy";
31 case UIProxyConfig::MODE_PROXY_PER_SCHEME: 31 case UIProxyConfig::MODE_PROXY_PER_SCHEME:
32 return "proxy-per-scheme"; 32 return "proxy-per-scheme";
33 } 33 }
34 NOTREACHED() << "Unrecognized mode type"; 34 NOTREACHED() << "Unrecognized mode type";
35 return ""; 35 return "";
36 } 36 }
37 37
38 bool ParseProxyConfig(const std::string& pref_proxy_config, 38 // Writes the proxy config of |network| to |proxy_config|. Returns false if no
39 net::ProxyConfig* proxy_config) { 39 // proxy was configured for this network.
40 if (pref_proxy_config.empty()) 40 bool GetProxyConfig(const NetworkState& network,
41 net::ProxyConfig* proxy_config) {
42 scoped_ptr<ProxyConfigDictionary> proxy_dict =
43 proxy_config::GetProxyConfigForNetwork(network);
44 if (!proxy_dict)
41 return false; 45 return false;
42 46 return PrefProxyConfigTrackerImpl::PrefConfigToNetConfig(*proxy_dict,
43 scoped_ptr<base::DictionaryValue> proxy_config_dict( 47 proxy_config);
44 chromeos::onc::ReadDictionaryFromJson(pref_proxy_config));
45 if (!proxy_config_dict) {
46 LOG(WARNING) << "Failed to parse proxy config.";
47 return false;
48 }
49
50 ProxyConfigDictionary proxy_config_dict_wrapper(proxy_config_dict.get());
51 return PrefProxyConfigTrackerImpl::PrefConfigToNetConfig(
52 proxy_config_dict_wrapper,
53 proxy_config);
54 } 48 }
55 49
56 // Returns true if proxy settings of |network| are editable. 50 // Returns true if proxy settings of |network| are editable.
57 bool IsNetworkProxySettingsEditable(const Network& network) { 51 bool IsNetworkProxySettingsEditable(const NetworkState& network) {
58 onc::ONCSource source = network.ui_data().onc_source(); 52 onc::ONCSource source = network.onc_source();
59 return source != onc::ONC_SOURCE_DEVICE_POLICY && 53 return source != onc::ONC_SOURCE_DEVICE_POLICY &&
60 source != onc::ONC_SOURCE_USER_POLICY; 54 source != onc::ONC_SOURCE_USER_POLICY;
61 } 55 }
62 56
63 } // namespace 57 } // namespace
64 58
65 UIProxyConfigService::UIProxyConfigService() { 59 UIProxyConfigService::UIProxyConfigService() {
66 } 60 }
67 61
68 UIProxyConfigService::~UIProxyConfigService() { 62 UIProxyConfigService::~UIProxyConfigService() {
69 } 63 }
70 64
71 void UIProxyConfigService::SetPrefs(PrefService* pref_service) { 65 void UIProxyConfigService::SetPrefs(PrefService* pref_service) {
72 pref_service_ = pref_service; 66 pref_service_ = pref_service;
73 } 67 }
74 68
75 void UIProxyConfigService::SetCurrentNetwork( 69 void UIProxyConfigService::SetCurrentNetwork(
76 const std::string& current_network) { 70 const std::string& current_network) {
77 Network* network = NULL; 71 const NetworkState* network = NULL;
78 if (!current_network.empty()) { 72 if (!current_network.empty()) {
79 network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath( 73 network = NetworkHandler::Get()->network_state_handler()->GetNetworkState(
80 current_network); 74 current_network);
81 LOG_IF(ERROR, !network) 75 LOG_IF(ERROR, !network)
82 << "Can't find requested network " << current_network; 76 << "Can't find requested network " << current_network;
83 } 77 }
84 current_ui_network_ = current_network; 78 current_ui_network_ = current_network;
85 if (!network) { 79 if (!network) {
86 current_ui_network_.clear(); 80 current_ui_network_.clear();
87 current_ui_config_ = UIProxyConfig(); 81 current_ui_config_ = UIProxyConfig();
88 return; 82 return;
89 } 83 }
90 84
91 DetermineEffectiveConfig(*network); 85 DetermineEffectiveConfig(*network);
92 VLOG(1) << "Current ui network: " 86 VLOG(1) << "Current ui network: "
93 << network->name() 87 << network->name()
94 << ", " << ModeToString(current_ui_config_.mode) << ", " 88 << ", " << ModeToString(current_ui_config_.mode) << ", "
95 << ProxyPrefs::ConfigStateToDebugString(current_ui_config_.state) 89 << ProxyPrefs::ConfigStateToDebugString(current_ui_config_.state)
96 << ", modifiable:" << current_ui_config_.user_modifiable; 90 << ", modifiable:" << current_ui_config_.user_modifiable;
97 } 91 }
98 92
99 void UIProxyConfigService::GetProxyConfig(UIProxyConfig* config) const { 93 void UIProxyConfigService::GetProxyConfig(UIProxyConfig* config) const {
100 *config = current_ui_config_; 94 *config = current_ui_config_;
101 } 95 }
102 96
103 void UIProxyConfigService::SetProxyConfig(const UIProxyConfig& config) { 97 void UIProxyConfigService::SetProxyConfig(const UIProxyConfig& config) {
104 current_ui_config_ = config; 98 current_ui_config_ = config;
105 if (current_ui_network_.empty()) 99 if (current_ui_network_.empty())
106 return; 100 return;
107 101
108 // Update config to shill. 102 const NetworkState* network =
109 std::string value; 103 NetworkHandler::Get()->network_state_handler()->
110 if (!current_ui_config_.SerializeForNetwork(&value)) 104 GetNetworkState(current_ui_network_);
111 return;
112
113 VLOG(1) << "Set proxy for " << current_ui_network_ << " to " << value;
114 current_ui_config_.state = ProxyPrefs::CONFIG_SYSTEM;
115
116 Network* network = CrosLibrary::Get()->GetNetworkLibrary()->FindNetworkByPath(
117 current_ui_network_);
118 if (!network) { 105 if (!network) {
119 LOG(ERROR) << "Can't find requested network " << current_ui_network_; 106 LOG(ERROR) << "Can't find requested network " << current_ui_network_;
120 return; 107 return;
121 } 108 }
122 network->SetProxyConfig(value); 109
110 // Store config for this network.
111 scoped_ptr<base::DictionaryValue> proxy_config_value(
112 config.ToPrefProxyConfig());
113 ProxyConfigDictionary proxy_config_dict(proxy_config_value.get());
114
115 VLOG(1) << "Set proxy for " << current_ui_network_
116 << " to " << *proxy_config_value;
117
118 proxy_config::SetProxyConfigForNetwork(proxy_config_dict, *network);
119 current_ui_config_.state = ProxyPrefs::CONFIG_SYSTEM;
123 } 120 }
124 121
125 void UIProxyConfigService::DetermineEffectiveConfig(const Network& network) { 122 void UIProxyConfigService::DetermineEffectiveConfig(
123 const NetworkState& network) {
126 DCHECK(pref_service_); 124 DCHECK(pref_service_);
127 125
128 // Get prefs proxy config if available. 126 // Get prefs proxy config if available.
129 net::ProxyConfig pref_config; 127 net::ProxyConfig pref_config;
130 ProxyPrefs::ConfigState pref_state = ProxyConfigServiceImpl::ReadPrefConfig( 128 ProxyPrefs::ConfigState pref_state = ProxyConfigServiceImpl::ReadPrefConfig(
131 pref_service_, &pref_config); 129 pref_service_, &pref_config);
132 130
133 // Get network proxy config if available. 131 // Get network proxy config if available.
134 net::ProxyConfig network_config; 132 net::ProxyConfig network_config;
135 net::ProxyConfigService::ConfigAvailability network_availability = 133 net::ProxyConfigService::ConfigAvailability network_availability =
136 net::ProxyConfigService::CONFIG_UNSET; 134 net::ProxyConfigService::CONFIG_UNSET;
137 if (ParseProxyConfig(network.proxy_config(), &network_config)) { 135 if (chromeos::GetProxyConfig(network, &network_config)) {
138 // Network is private or shared with user using shared proxies. 136 // Network is private or shared with user using shared proxies.
139 VLOG(1) << this << ": using network proxy: " << network.proxy_config(); 137 VLOG(1) << this << ": using network proxy: " << network.proxy_config();
140 network_availability = net::ProxyConfigService::CONFIG_VALID; 138 network_availability = net::ProxyConfigService::CONFIG_VALID;
141 } 139 }
142 140
143 // Determine effective proxy config, either from prefs or network. 141 // Determine effective proxy config, either from prefs or network.
144 ProxyPrefs::ConfigState effective_config_state; 142 ProxyPrefs::ConfigState effective_config_state;
145 net::ProxyConfig effective_config; 143 net::ProxyConfig effective_config;
146 ProxyConfigServiceImpl::GetEffectiveProxyConfig( 144 ProxyConfigServiceImpl::GetEffectiveProxyConfig(
147 pref_state, pref_config, 145 pref_state, pref_config,
148 network_availability, network_config, false, 146 network_availability, network_config, false,
149 &effective_config_state, &effective_config); 147 &effective_config_state, &effective_config);
150 148
151 // Store effective proxy into |current_ui_config_|. 149 // Store effective proxy into |current_ui_config_|.
152 current_ui_config_.FromNetProxyConfig(effective_config); 150 current_ui_config_.FromNetProxyConfig(effective_config);
153 current_ui_config_.state = effective_config_state; 151 current_ui_config_.state = effective_config_state;
154 if (ProxyConfigServiceImpl::PrefPrecedes(effective_config_state)) { 152 if (ProxyConfigServiceImpl::PrefPrecedes(effective_config_state)) {
155 current_ui_config_.user_modifiable = false; 153 current_ui_config_.user_modifiable = false;
156 } else if (!IsNetworkProxySettingsEditable(network)) { 154 } else if (!IsNetworkProxySettingsEditable(network)) {
157 // TODO(xiyuan): Figure out the right way to set config state for managed 155 // TODO(xiyuan): Figure out the right way to set config state for managed
158 // network. 156 // network.
159 current_ui_config_.state = ProxyPrefs::CONFIG_POLICY; 157 current_ui_config_.state = ProxyPrefs::CONFIG_POLICY;
160 current_ui_config_.user_modifiable = false; 158 current_ui_config_.user_modifiable = false;
161 } else { 159 } else {
162 current_ui_config_.user_modifiable = 160 current_ui_config_.user_modifiable =
163 !ProxyConfigServiceImpl::IgnoreProxy(pref_service_, 161 !ProxyConfigServiceImpl::IgnoreProxy(pref_service_,
164 network.profile_path(), 162 network.profile_path(),
165 network.ui_data().onc_source()); 163 network.onc_source());
166 } 164 }
167 } 165 }
168 166
169 } // namespace chromeos 167 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/ui_proxy_config_service.h ('k') | chrome/browser/prefs/proxy_config_dictionary.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698