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

Side by Side Diff: chrome/browser/chromeos/proxy_config_service_impl.h

Issue 6549007: Make CrOS proxy configuration write prefs directly. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove default request context proxy config service patch. Created 9 years, 10 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 (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #pragma once 7 #pragma once
8 8
9 #include <string> 9 #include <string>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/basictypes.h" 12 #include "base/basictypes.h"
13 #include "base/observer_list.h" 13 #include "base/observer_list.h"
14 #include "base/ref_counted.h" 14 #include "base/ref_counted.h"
15 #include "base/scoped_ptr.h" 15 #include "base/scoped_ptr.h"
16 #include "base/values.h" 16 #include "base/values.h"
17 #include "chrome/browser/chromeos/login/signed_settings.h" 17 #include "chrome/browser/chromeos/login/signed_settings.h"
18 #include "net/proxy/proxy_config.h" 18 #include "net/proxy/proxy_config.h"
19 #include "net/proxy/proxy_config_service.h" 19 #include "net/proxy/proxy_config_service.h"
20 #include "net/proxy/proxy_server.h" 20 #include "net/proxy/proxy_server.h"
21 21
22 class PrefService;
23
22 namespace chromeos { 24 namespace chromeos {
23 25
24 // Implementation of proxy config service for chromeos that: 26 // Implementation of proxy config service for chromeos that:
25 // - is RefCountedThreadSafe
26 // - is wrapped by chromeos::ProxyConfigService which implements
27 // net::ProxyConfigService interface by fowarding the methods to this class
28 // - retrieves initial system proxy configuration from cros settings persisted 27 // - retrieves initial system proxy configuration from cros settings persisted
29 // on chromeos device 28 // on chromeos device
30 // - provides network stack with latest system proxy configuration for use on
31 // IO thread
32 // - provides UI with methods to retrieve and modify system proxy configuration 29 // - provides UI with methods to retrieve and modify system proxy configuration
33 // on UI thread 30 // on UI thread
34 // - TODO(kuan): persists proxy configuration settings on chromeos device using 31 // - TODO(kuan): persists proxy configuration settings on chromeos device using
35 // cros settings 32 // cros settings
36 class ProxyConfigServiceImpl 33 class ProxyConfigServiceImpl
37 : public base::RefCountedThreadSafe<ProxyConfigServiceImpl>, 34 : public SignedSettings::Delegate<bool>,
38 public SignedSettings::Delegate<bool>,
39 public SignedSettings::Delegate<std::string> { 35 public SignedSettings::Delegate<std::string> {
40 public: 36 public:
41 // ProxyConfigServiceImpl is created on the UI thread in 37 // ProxyConfigServiceImpl lives in the profile and is accessed via
42 // chrome/browser/net/chrome_url_request_context.cc::CreateProxyConfigService 38 // Profile::GetChromeOSProxyConfigServiceImpl. It is accessed from
kuan 2011/02/22 22:20:25 i'm sure u're aware of battre's cl that places Pro
Mattias Nissler (ping if slow) 2011/03/01 15:19:20 Yes, he's actually sitting 2 desks away from me, a
43 // via ProfileImpl::GetChromeOSProxyConfigServiceImpl, and stored in Profile 39 // ProxyCrosSettingsProvider in order to read or modify the proxy
44 // as a scoped_refptr (because it's RefCountedThreadSafe). 40 // configuration via UIGetProxyConfig or UISetProxyConfigTo* respectively. The
41 // modified proxy configuration is written to local state preferences that get
42 // picked up by PrefProxyConfigService and passed to the network stack.
45 // 43 //
46 // Past that point, it can be accessed from the IO or UI threads.
47 //
48 // From the IO thread, it is accessed periodically through the wrapper class
49 // chromeos::ProxyConfigService via net::ProxyConfigService interface
50 // (GetLatestProxyConfig, AddObserver, RemoveObserver).
51 //
52 // From the UI thread, it is accessed via
53 // WebUI::GetProfile::GetChromeOSProxyConfigServiceImpl to allow user to read
54 // or modify the proxy configuration via UIGetProxyConfig or
55 // UISetProxyConfigTo* respectively.
56 // The new modified proxy config is posted to the IO thread through
57 // SetNewProxyConfig(). We then notify observers on the IO thread of the
58 // configuration change.
59
60 // In contrary to other platforms which simply use the systems' UI to allow 44 // In contrary to other platforms which simply use the systems' UI to allow
61 // users to configure proxies, we have to implement our own UI on the chromeos 45 // users to configure proxies, we have to implement our own UI on the chromeos
62 // device. This requires extra and specific UI requirements that 46 // device. This requires extra and specific UI requirements that
63 // net::ProxyConfig does not suffice. So we create an augmented analog to 47 // net::ProxyConfig does not suffice. So we create an augmented analog to
64 // net:ProxyConfig here to include and handle these UI requirements, e.g. 48 // net:ProxyConfig here to include and handle these UI requirements, e.g.
65 // - where configuration was picked up from - policy or owner 49 // - where configuration was picked up from - policy or owner
66 // - the read/write access of a proxy setting 50 // - the read/write access of a proxy setting
67 // - may add more stuff later. 51 // - may add more stuff later.
68 // This is then converted to the common net::ProxyConfig before being returned
69 // to ProxyService::GetLatestProxyConfig on the IO thread to be used on the
70 // network stack.
71 struct ProxyConfig { 52 struct ProxyConfig {
72 // Specifies if proxy config is direct, auto-detect, using pac script, 53 // Specifies if proxy config is direct, auto-detect, using pac script,
73 // single-proxy, or proxy-per-scheme. 54 // single-proxy, or proxy-per-scheme.
74 enum Mode { 55 enum Mode {
75 MODE_DIRECT, 56 MODE_DIRECT,
76 MODE_AUTO_DETECT, 57 MODE_AUTO_DETECT,
77 MODE_PAC_SCRIPT, 58 MODE_PAC_SCRIPT,
78 MODE_SINGLE_PROXY, 59 MODE_SINGLE_PROXY,
79 MODE_PROXY_PER_SCHEME, 60 MODE_PROXY_PER_SCHEME,
80 }; 61 };
(...skipping 25 matching lines...) Expand all
106 // Proxy setting for mode = single-proxy or proxy-per-scheme. 87 // Proxy setting for mode = single-proxy or proxy-per-scheme.
107 struct ManualProxy : public Setting { 88 struct ManualProxy : public Setting {
108 virtual DictionaryValue* Encode() const; 89 virtual DictionaryValue* Encode() const;
109 bool Decode(DictionaryValue* dict, net::ProxyServer::Scheme scheme); 90 bool Decode(DictionaryValue* dict, net::ProxyServer::Scheme scheme);
110 91
111 net::ProxyServer server; 92 net::ProxyServer server;
112 }; 93 };
113 94
114 ProxyConfig() : mode(MODE_DIRECT) {} 95 ProxyConfig() : mode(MODE_DIRECT) {}
115 96
116 // Converts |this| to net::ProxyConfig. 97 // Converts |this| to a proxy config dictionary understood by the pref
117 void ToNetProxyConfig(net::ProxyConfig* net_config); 98 // system. Ownership is transferred to the caller.
99 DictionaryValue* ToProxyConfigDictionary() const;
118 100
119 // Returns true if proxy config can be written by user. 101 // Returns true if proxy config can be written by user.
120 // If mode is MODE_PROXY_PER_SCHEME, |scheme| is one of "http", "https", 102 // If mode is MODE_PROXY_PER_SCHEME, |scheme| is one of "http", "https",
121 // "ftp" or "socks"; otherwise, it should be empty or will be ignored. 103 // "ftp" or "socks"; otherwise, it should be empty or will be ignored.
122 bool CanBeWrittenByUser(bool user_is_owner, const std::string& scheme); 104 bool CanBeWrittenByUser(bool user_is_owner, const std::string& scheme);
123 105
124 // Map |scheme| (one of "http", "https", "ftp" or "socks") to the correct 106 // Map |scheme| (one of "http", "https", "ftp" or "socks") to the correct
125 // ManualProxy. Returns NULL if scheme is invalid. 107 // ManualProxy. Returns NULL if scheme is invalid.
126 ManualProxy* MapSchemeToProxy(const std::string& scheme); 108 ManualProxy* MapSchemeToProxy(const std::string& scheme);
127 109
(...skipping 22 matching lines...) Expand all
150 // Set if mode is MODE_PROXY_PER_SCHEME and has socks proxy. 132 // Set if mode is MODE_PROXY_PER_SCHEME and has socks proxy.
151 ManualProxy socks_proxy; 133 ManualProxy socks_proxy;
152 134
153 // Exceptions for when not to use a proxy. 135 // Exceptions for when not to use a proxy.
154 net::ProxyBypassRules bypass_rules; 136 net::ProxyBypassRules bypass_rules;
155 137
156 private: 138 private:
157 // Encodes |manual_proxy| and adds it as value into |key_name| of |dict|. 139 // Encodes |manual_proxy| and adds it as value into |key_name| of |dict|.
158 void EncodeManualProxy(const ManualProxy& manual_proxy, 140 void EncodeManualProxy(const ManualProxy& manual_proxy,
159 DictionaryValue* dict, const char* key_name); 141 DictionaryValue* dict, const char* key_name);
142
160 // Decodes value of |key_name| in |dict| into |manual_proxy| with |scheme|; 143 // Decodes value of |key_name| in |dict| into |manual_proxy| with |scheme|;
161 // if |ok_if_absent| is true, function returns true if |key_name| doesn't 144 // if |ok_if_absent| is true, function returns true if |key_name| doesn't
162 // exist in |dict|. 145 // exist in |dict|.
163 bool DecodeManualProxy(DictionaryValue* dict, const char* key_name, 146 bool DecodeManualProxy(DictionaryValue* dict, const char* key_name,
164 bool ok_if_absent, net::ProxyServer::Scheme scheme, 147 bool ok_if_absent, net::ProxyServer::Scheme scheme,
165 ManualProxy* manual_proxy); 148 ManualProxy* manual_proxy);
149
150 // Helper function for building up the manual proxy spec. A declaration of
151 // the form <scheme>=<server> is written to spec. |spec| is in/out, it is
152 // appended to if it already contains data.
153 static void FormatProxySpec(std::string* spec,
154 const std::string& scheme,
155 const net::ProxyServer& server);
166 }; 156 };
167 157
168 // Usual constructor. 158 // Usual constructor.
169 ProxyConfigServiceImpl(); 159 explicit ProxyConfigServiceImpl(PrefService* local_state);
170 // Constructor for testing. 160 // Constructor for testing.
171 // |init_config| specifies the ProxyConfig to use for initialization. 161 // |init_config| specifies the ProxyConfig to use for initialization.
172 explicit ProxyConfigServiceImpl(const ProxyConfig& init_config); 162 ProxyConfigServiceImpl(PrefService* local_state,
163 const ProxyConfig& init_config);
173 virtual ~ProxyConfigServiceImpl(); 164 virtual ~ProxyConfigServiceImpl();
174 165
175 // Methods called on IO thread from wrapper class chromeos::ProxyConfigService
176 // as ProxyConfigService methods.
177 void AddObserver(net::ProxyConfigService::Observer* observer);
178 void RemoveObserver(net::ProxyConfigService::Observer* observer);
179 // Called from GetLatestProxyConfig.
180 bool IOGetProxyConfig(net::ProxyConfig* config);
181
182 // Called from UI thread to retrieve proxy configuration in |config|. 166 // Called from UI thread to retrieve proxy configuration in |config|.
183 void UIGetProxyConfig(ProxyConfig* config); 167 void UIGetProxyConfig(ProxyConfig* config);
184 168
185 // Called from UI thread to set flag to persist settings to device. 169 // Called from UI thread to set flag to persist settings to device.
186 // Subsequent UISet* methods will use this flag, until UI calls it again with 170 // Subsequent UISet* methods will use this flag, until UI calls it again with
187 // a different flag. 171 // a different flag.
188 void UISetPersistToDevice(bool persist) { 172 void UISetPersistToDevice(bool persist) {
189 persist_to_device_ = persist; 173 persist_to_device_ = persist;
190 } 174 }
191 175
(...skipping 14 matching lines...) Expand all
206 // Only valid for MODE_SINGLE_PROXY or MODE_PROXY_PER_SCHEME. 190 // Only valid for MODE_SINGLE_PROXY or MODE_PROXY_PER_SCHEME.
207 bool UISetProxyConfigBypassRules(const net::ProxyBypassRules& bypass_rules); 191 bool UISetProxyConfigBypassRules(const net::ProxyBypassRules& bypass_rules);
208 192
209 // Implementation for SignedSettings::Delegate 193 // Implementation for SignedSettings::Delegate
210 virtual void OnSettingsOpCompleted(SignedSettings::ReturnCode code, 194 virtual void OnSettingsOpCompleted(SignedSettings::ReturnCode code,
211 std::string value); 195 std::string value);
212 virtual void OnSettingsOpCompleted(SignedSettings::ReturnCode code, 196 virtual void OnSettingsOpCompleted(SignedSettings::ReturnCode code,
213 bool value); 197 bool value);
214 198
215 private: 199 private:
216 friend class base::RefCountedThreadSafe<ProxyConfigServiceImpl>;
217
218 // Init proxy to default config, i.e. AutoDetect. 200 // Init proxy to default config, i.e. AutoDetect.
219 // If |post_to_io_thread| is true, a task will be posted to IO thread to 201 void InitConfigToDefault();
220 // update |cached_config|.
221 void InitConfigToDefault(bool post_to_io_thread);
222 202
223 // Persists proxy config to device. 203 // Persists proxy config to device.
224 void PersistConfigToDevice(); 204 void PersistConfigToDevice();
225 205
226 // Called from UI thread from the various UISetProxyConfigTo* 206 // Called from UI thread from the various UISetProxyConfigTo*
227 // |update_to_device| is true to persist new proxy config to device. 207 // |update_to_device| is true to persist new proxy config to device.
228 void OnUISetProxyConfig(bool update_to_device); 208 void OnUISetProxyConfig(bool update_to_device);
229 209
230 // Posted from UI thread to IO thread to carry the new config information.
231 void IOSetProxyConfig(const ProxyConfig& new_config);
232
233 // Checks that method is called on BrowserThread::IO thread.
234 void CheckCurrentlyOnIOThread();
235
236 // Checks that method is called on BrowserThread::UI thread.
237 void CheckCurrentlyOnUIThread();
238
239 // Data members. 210 // Data members.
240 211
241 // True if tasks can be posted, which can only happen if constructor has 212 // True if tasks can be posted, which can only happen if constructor has
242 // completed (NewRunnableMethod cannot be created for a RefCountedThreadBase's 213 // completed (NewRunnableMethod cannot be created for a RefCountedThreadBase's
243 // method until the class's ref_count is at least one). 214 // method until the class's ref_count is at least one).
244 bool can_post_task_; 215 bool can_post_task_;
245 216
246 // True if config has been fetched from device or initialized properly. 217 // True if config has been fetched from device or initialized properly.
247 bool has_config_; 218 bool has_config_;
248 219
249 // True if settings are to be persisted to device. 220 // True if settings are to be persisted to device.
250 bool persist_to_device_; 221 bool persist_to_device_;
251 222
252 // True if there's a pending operation to store proxy setting to device. 223 // True if there's a pending operation to store proxy setting to device.
253 bool persist_to_device_pending_; 224 bool persist_to_device_pending_;
254 225
255 // Cached proxy configuration, to be converted to net::ProxyConfig and
256 // returned by IOGetProxyConfig.
257 // Initially populated from the UI thread, but afterwards only accessed from
258 // the IO thread.
259 ProxyConfig cached_config_;
260
261 // Copy of the proxy configuration kept on the UI thread of the last seen 226 // Copy of the proxy configuration kept on the UI thread of the last seen
262 // proxy config, so as to avoid posting a call to SetNewProxyConfig when we 227 // proxy config, so as to avoid posting a call to SetNewProxyConfig when we
263 // are called by UI to set new proxy but the config has not actually changed. 228 // are called by UI to set new proxy but the config has not actually changed.
264 ProxyConfig reference_config_; 229 ProxyConfig reference_config_;
265 230
231 // The pref service to store the proxy configuration to.
232 PrefService* local_state_;
233
266 // List of observers for changes in proxy config. 234 // List of observers for changes in proxy config.
267 ObserverList<net::ProxyConfigService::Observer> observers_; 235 ObserverList<net::ProxyConfigService::Observer> observers_;
268 236
269 // Operations to retrieve and store proxy setting from and to device 237 // Operations to retrieve and store proxy setting from and to device
270 // respectively. 238 // respectively.
271 scoped_refptr<SignedSettings> retrieve_property_op_; 239 scoped_refptr<SignedSettings> retrieve_property_op_;
272 scoped_refptr<SignedSettings> store_property_op_; 240 scoped_refptr<SignedSettings> store_property_op_;
273 241
274 DISALLOW_COPY_AND_ASSIGN(ProxyConfigServiceImpl); 242 DISALLOW_COPY_AND_ASSIGN(ProxyConfigServiceImpl);
275 }; 243 };
276 244
277 } // namespace chromeos 245 } // namespace chromeos
278 246
279 #endif // CHROME_BROWSER_CHROMEOS_PROXY_CONFIG_SERVICE_IMPL_H_ 247 #endif // CHROME_BROWSER_CHROMEOS_PROXY_CONFIG_SERVICE_IMPL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698