diff options
author | mdm@chromium.org <mdm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-17 22:19:50 +0000 |
---|---|---|
committer | mdm@chromium.org <mdm@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-05-17 22:19:50 +0000 |
commit | 573c0504ca29c38670f43da59fc1c3ab3d584006 (patch) | |
tree | dab4229da43ba611512c07a51eaf63575b60f5b8 /net/proxy/proxy_config_service_linux.h | |
parent | d231828ebc146578393f38c34161013d0c7bf161 (diff) | |
download | chromium_src-573c0504ca29c38670f43da59fc1c3ab3d584006.zip chromium_src-573c0504ca29c38670f43da59fc1c3ab3d584006.tar.gz chromium_src-573c0504ca29c38670f43da59fc1c3ab3d584006.tar.bz2 |
Linux: refactor ProxyConfigServiceLinux to use an enum of setting ids rather than gconf key names.
This will make it easier to add a third way to get proxy settings, via the new gsettings API.
BUG=80453
TEST=proxy settings still work as expected on linux
Review URL: http://codereview.chromium.org/7027002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@85686 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/proxy/proxy_config_service_linux.h')
-rw-r--r-- | net/proxy/proxy_config_service_linux.h | 174 |
1 files changed, 106 insertions, 68 deletions
diff --git a/net/proxy/proxy_config_service_linux.h b/net/proxy/proxy_config_service_linux.h index ea76c4d..363363a 100644 --- a/net/proxy/proxy_config_service_linux.h +++ b/net/proxy/proxy_config_service_linux.h @@ -23,36 +23,37 @@ namespace net { // Implementation of ProxyConfigService that retrieves the system proxy -// settings from environment variables or gconf. +// settings from environment variables, gconf, gsettings, or kioslaverc (KDE). class BASE_API ProxyConfigServiceLinux : public ProxyConfigService { public: // Forward declaration of Delegate. class Delegate; - class GConfSettingGetter { + class SettingGetter { public: // Buffer size used in some implementations of this class when reading // files. Defined here so unit tests can construct worst-case inputs. static const size_t BUFFER_SIZE = 512; - GConfSettingGetter() {} - virtual ~GConfSettingGetter() {} + SettingGetter() {} + virtual ~SettingGetter() {} - // Initializes the class: obtains a gconf client, or simulates one, in - // the concrete implementations. Returns true on success. Must be called - // before using other methods, and should be called on the thread running - // the glib main loop. - // One of |glib_default_loop| and |file_loop| will be used for gconf calls - // or reading necessary files, depending on the implementation. + // Initializes the class: obtains a gconf/gsettings client, or simulates + // one, in the concrete implementations. Returns true on success. Must be + // called before using other methods, and should be called on the thread + // running the glib main loop. + // One of |glib_default_loop| and |file_loop| will be used for + // gconf/gsettings calls or reading necessary files, depending on the + // implementation. virtual bool Init(MessageLoop* glib_default_loop, MessageLoopForIO* file_loop) = 0; - // Releases the gconf client, which clears cached directories and + // Releases the gconf/gsettings client, which clears cached directories and // stops notifications. virtual void ShutDown() = 0; - // Requests notification of gconf setting changes for proxy + // Requests notification of gconf/gsettings changes for proxy // settings. Returns true on success. virtual bool SetUpNotifications(Delegate* delegate) = 0; @@ -61,22 +62,59 @@ class BASE_API ProxyConfigServiceLinux : public ProxyConfigService { // Returns NULL if it does not matter. virtual MessageLoop* GetNotificationLoop() = 0; - // Returns the data source's name (e.g. "gconf", "KDE", "test"). - // Used only for diagnostic purposes (e.g. VLOG(1) etc.). + // Returns the data source's name (e.g. "gconf", "gsettings", "KDE", + // "test"). Used only for diagnostic purposes (e.g. VLOG(1) etc.). virtual const char* GetDataSource() = 0; - // Gets a string type value from gconf and stores it in - // result. Returns false if the key is unset or on error. Must - // only be called after a successful call to Init(), and not - // after a failed call to SetUpNotifications() or after calling - // Release(). - virtual bool GetString(const char* key, std::string* result) = 0; + // These are all the values that can be fetched. We used to just use the + // corresponding paths in gconf for these, but gconf is now obsolete and + // in the future we'll be using mostly gsettings/kioslaverc so we + // enumerate them instead to avoid unnecessary string operations. + enum Setting { + PROXY_MODE, // string + PROXY_AUTOCONF_URL, // string + PROXY_USE_HTTP_PROXY, // bool + PROXY_USE_SAME_PROXY, // bool + PROXY_USE_AUTHENTICATION, // bool + PROXY_IGNORE_HOSTS, // string list + PROXY_HTTP_HOST, // string + PROXY_HTTP_PORT, // int + PROXY_HTTPS_HOST, // string + PROXY_HTTPS_PORT, // int + PROXY_FTP_HOST, // string + PROXY_FTP_PORT, // int + PROXY_SOCKS_HOST, // string + PROXY_SOCKS_PORT, // int + }; + + // Given a PROXY_*_HOST value, return the corresponding PROXY_*_PORT value. + static Setting HostSettingToPortSetting(Setting host) { + switch (host) { + case PROXY_HTTP_HOST: + return PROXY_HTTP_PORT; + case PROXY_HTTPS_HOST: + return PROXY_HTTPS_PORT; + case PROXY_FTP_HOST: + return PROXY_FTP_PORT; + case PROXY_SOCKS_HOST: + return PROXY_SOCKS_PORT; + default: + NOTREACHED(); + return host; // placate compiler + } + } + + // Gets a string type value from the data source and stores it in + // |*result|. Returns false if the key is unset or on error. Must only be + // called after a successful call to Init(), and not after a failed call + // to SetUpNotifications() or after calling Release(). + virtual bool GetString(Setting key, std::string* result) = 0; // Same thing for a bool typed value. - virtual bool GetBoolean(const char* key, bool* result) = 0; + virtual bool GetBool(Setting key, bool* result) = 0; // Same for an int typed value. - virtual bool GetInt(const char* key, int* result) = 0; + virtual bool GetInt(Setting key, int* result) = 0; // And for a string list. - virtual bool GetStringList(const char* key, + virtual bool GetStringList(Setting key, std::vector<std::string>* result) = 0; // Returns true if the bypass list should be interpreted as a proxy @@ -88,57 +126,58 @@ class BASE_API ProxyConfigServiceLinux : public ProxyConfigService { virtual bool MatchHostsUsingSuffixMatching() = 0; private: - DISALLOW_COPY_AND_ASSIGN(GConfSettingGetter); + DISALLOW_COPY_AND_ASSIGN(SettingGetter); }; // ProxyConfigServiceLinux is created on the UI thread, and - // SetUpAndFetchInitialConfig() is immediately called to - // synchronously fetch the original configuration and set up gconf - // notifications on the UI thread. + // SetUpAndFetchInitialConfig() is immediately called to synchronously + // fetch the original configuration and set up change notifications on + // the UI thread. // // Past that point, it is accessed periodically through the // ProxyConfigService interface (GetLatestProxyConfig, AddObserver, // RemoveObserver) from the IO thread. // - // gconf change notification callbacks can occur at any time and are - // run on the UI thread. The new gconf settings are fetched on the - // UI thread, and the new resulting proxy config is posted to the IO - // thread through Delegate::SetNewProxyConfig(). We then notify - // observers on the IO thread of the configuration change. + // Setting change notification callbacks can occur at any time and are + // run on either the UI thread (gconf/gsettings) or the file thread + // (KDE). The new settings are fetched on that thread, and the resulting + // proxy config is posted to the IO thread through + // Delegate::SetNewProxyConfig(). We then notify observers on the IO + // thread of the configuration change. // // ProxyConfigServiceLinux is deleted from the IO thread. // // The substance of the ProxyConfigServiceLinux implementation is // wrapped in the Delegate ref counted class. On deleting the - // ProxyConfigServiceLinux, Delegate::OnDestroy() is posted to the - // UI thread where gconf notifications will be safely stopped before - // releasing Delegate. + // ProxyConfigServiceLinux, Delegate::OnDestroy() is posted to either + // the UI thread (gconf/gsettings) or the file thread (KDE) where change + // notifications will be safely stopped before releasing Delegate. class Delegate : public base::RefCountedThreadSafe<Delegate> { public: // Constructor receives env var getter implementation to use, and // takes ownership of it. This is the normal constructor. explicit Delegate(base::Environment* env_var_getter); - // Constructor receives gconf and env var getter implementations + // Constructor receives setting and env var getter implementations // to use, and takes ownership of them. Used for testing. - Delegate(base::Environment* env_var_getter, - GConfSettingGetter* gconf_getter); - // Synchronously obtains the proxy configuration. If gconf is - // used, also enables gconf notification for setting - // changes. gconf must only be accessed from the thread running - // the default glib main loop, and so this method must be called - // from the UI thread. The message loop for the IO thread is - // specified so that notifications can post tasks to it (and for - // assertions). The message loop for the file thread is used to - // read any files needed to determine proxy settings. + Delegate(base::Environment* env_var_getter, SettingGetter* setting_getter); + + // Synchronously obtains the proxy configuration. If gconf, + // gsettings, or kioslaverc are used, also enables notifications for + // setting changes. gconf/gsettings must only be accessed from the + // thread running the default glib main loop, and so this method + // must be called from the UI thread. The message loop for the IO + // thread is specified so that notifications can post tasks to it + // (and for assertions). The message loop for the file thread is + // used to read any files needed to determine proxy settings. void SetUpAndFetchInitialConfig(MessageLoop* glib_default_loop, MessageLoop* io_loop, MessageLoopForIO* file_loop); - // Handler for gconf change notifications: fetches a new proxy - // configuration from gconf settings, and if this config is - // different than what we had before, posts a task to have it - // stored in cached_config_. + // Handler for setting change notifications: fetches a new proxy + // configuration from settings, and if this config is different + // than what we had before, posts a task to have it stored in + // cached_config_. // Left public for simplicity. void OnCheckProxyConfigSettings(); @@ -151,7 +190,7 @@ class BASE_API ProxyConfigServiceLinux : public ProxyConfigService { // Posts a call to OnDestroy() to the UI thread. Called from // ProxyConfigServiceLinux's destructor. void PostDestroyTask(); - // Safely stops gconf notifications. Posted to the UI thread. + // Safely stops change notifications. Posted to the UI thread. void OnDestroy(); private: @@ -171,14 +210,14 @@ class BASE_API ProxyConfigServiceLinux : public ProxyConfigService { // variables were found and the configuration is valid. bool GetConfigFromEnv(ProxyConfig* config); - // Obtains host and port gconf settings and parses a proxy server + // Obtains host and port config settings and parses a proxy server // specification from it and puts it in result. Returns true if the // requested variable is defined and the value valid. - bool GetProxyFromGConf(const char* key_prefix, bool is_socks, - ProxyServer* result_server); - // Fills proxy config from gconf. Returns true if settings were found + bool GetProxyFromSettings(SettingGetter::Setting host_key, + ProxyServer* result_server); + // Fills proxy config from settings. Returns true if settings were found // and the configuration is valid. - bool GetConfigFromGConf(ProxyConfig* config); + bool GetConfigFromSettings(ProxyConfig* config); // This method is posted from the UI thread to the IO thread to // carry the new config information. @@ -188,7 +227,7 @@ class BASE_API ProxyConfigServiceLinux : public ProxyConfigService { void SetUpNotifications(); scoped_ptr<base::Environment> env_var_getter_; - scoped_ptr<GConfSettingGetter> gconf_getter_; + scoped_ptr<SettingGetter> setting_getter_; // Cached proxy configuration, to be returned by // GetLatestProxyConfig. Initially populated from the UI thread, but @@ -200,15 +239,14 @@ class BASE_API ProxyConfigServiceLinux : public ProxyConfigService { // notification but the config has not actually changed. ProxyConfig reference_config_; - // The MessageLoop for the UI thread, aka main browser thread. This - // thread is where we run the glib main loop (see - // base/message_pump_glib.h). It is the glib default loop in the - // sense that it runs the glib default context: as in the context - // where sources are added by g_timeout_add and g_idle_add, and - // returned by g_main_context_default. gconf uses glib timeouts and - // idles and possibly other callbacks that will all be dispatched on - // this thread. Since gconf is not thread safe, any use of gconf - // must be done on the thread running this loop. + // The MessageLoop for the UI thread, aka main browser thread. This thread + // is where we run the glib main loop (see base/message_pump_glib.h). It is + // the glib default loop in the sense that it runs the glib default context: + // as in the context where sources are added by g_timeout_add and + // g_idle_add, and returned by g_main_context_default. gconf uses glib + // timeouts and idles and possibly other callbacks that will all be + // dispatched on this thread. Since gconf is not thread safe, any use of + // gconf must be done on the thread running this loop. MessageLoop* glib_default_loop_; // MessageLoop for the IO thread. GetLatestProxyConfig() is called from // the thread running this loop. @@ -223,10 +261,10 @@ class BASE_API ProxyConfigServiceLinux : public ProxyConfigService { // Usual constructor ProxyConfigServiceLinux(); - // For testing: take alternate gconf and env var getter implementations. + // For testing: take alternate setting and env var getter implementations. explicit ProxyConfigServiceLinux(base::Environment* env_var_getter); ProxyConfigServiceLinux(base::Environment* env_var_getter, - GConfSettingGetter* gconf_getter); + SettingGetter* setting_getter); virtual ~ProxyConfigServiceLinux(); |