| 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 NET_PROXY_PROXY_CONFIG_SERVICE_LINUX_H_ | 5 #ifndef NET_PROXY_PROXY_CONFIG_SERVICE_LINUX_H_ |
| 6 #define NET_PROXY_PROXY_CONFIG_SERVICE_LINUX_H_ | 6 #define NET_PROXY_PROXY_CONFIG_SERVICE_LINUX_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 virtual bool BypassListIsReversed() = 0; | 132 virtual bool BypassListIsReversed() = 0; |
| 133 | 133 |
| 134 // Returns true if the bypass rules should be interpreted as | 134 // Returns true if the bypass rules should be interpreted as |
| 135 // suffix-matching rules. | 135 // suffix-matching rules. |
| 136 virtual bool MatchHostsUsingSuffixMatching() = 0; | 136 virtual bool MatchHostsUsingSuffixMatching() = 0; |
| 137 | 137 |
| 138 private: | 138 private: |
| 139 DISALLOW_COPY_AND_ASSIGN(SettingGetter); | 139 DISALLOW_COPY_AND_ASSIGN(SettingGetter); |
| 140 }; | 140 }; |
| 141 | 141 |
| 142 // ProxyConfigServiceLinux is created on the UI thread, and | 142 // ProxyConfigServiceLinux is created on the IO thread, and begins |
| 143 // SetUpAndFetchInitialConfig() is immediately called to synchronously | 143 // asynchronously on the UI thread fetching the original configuration |
| 144 // fetch the original configuration and set up change notifications on | 144 // and setting up change notifications. |
| 145 // the UI thread. | |
| 146 // | 145 // |
| 147 // Past that point, it is accessed periodically through the | 146 // Past that point, it is accessed periodically through the |
| 148 // ProxyConfigService interface (GetLatestProxyConfig, AddObserver, | 147 // ProxyConfigService interface (GetLatestProxyConfig, AddObserver, |
| 149 // RemoveObserver) from the IO thread. | 148 // RemoveObserver) from the IO thread. |
| 150 // | 149 // |
| 151 // Setting change notification callbacks can occur at any time and are | 150 // Setting change notification callbacks can occur at any time and are |
| 152 // run on either the UI thread (gconf/gsettings) or the file thread | 151 // run on either the UI thread (gconf/gsettings) or the file thread |
| 153 // (KDE). The new settings are fetched on that thread, and the resulting | 152 // (KDE). The new settings are fetched on that thread, and the resulting |
| 154 // proxy config is posted to the IO thread through | 153 // proxy config is posted to the IO thread through |
| 155 // Delegate::SetNewProxyConfig(). We then notify observers on the IO | 154 // Delegate::SetNewProxyConfig(). We then notify observers on the IO |
| 156 // thread of the configuration change. | 155 // thread of the configuration change. |
| 157 // | 156 // |
| 158 // ProxyConfigServiceLinux is deleted from the IO thread. | 157 // ProxyConfigServiceLinux is deleted from the IO thread but may require |
| 158 // prior shutdown performed on other threads via StartTearDown(). |
| 159 // This prior shutdown safely stops change notifications. |
| 159 // | 160 // |
| 160 // The substance of the ProxyConfigServiceLinux implementation is | 161 // The substance of the ProxyConfigServiceLinux implementation is |
| 161 // wrapped in the Delegate ref counted class. On deleting the | 162 // wrapped in the Delegate ref counted class. Destruction of the Delegate |
| 162 // ProxyConfigServiceLinux, Delegate::OnDestroy() is posted to either | 163 // is commenced by StartTearDown() which calls Delegate::OnDestroy() on |
| 163 // the UI thread (gconf/gsettings) or the file thread (KDE) where change | 164 // the UI thread (gconf/gsettings) or posts it to the file thread (KDE) where |
| 164 // notifications will be safely stopped before releasing Delegate. | 165 // change notifications will be safely stopped before releasing Delegate. |
| 165 | 166 |
| 166 class Delegate : public base::RefCountedThreadSafe<Delegate> { | 167 class Delegate : public base::RefCountedThreadSafe<Delegate> { |
| 167 public: | 168 public: |
| 168 // Constructor receives env var getter implementation to use, and | 169 // Constructor receives env var getter implementation to use, and |
| 169 // takes ownership of it. This is the normal constructor. | 170 // takes ownership of it. This is the normal constructor. |
| 170 explicit Delegate(base::Environment* env_var_getter); | 171 explicit Delegate(base::Environment* env_var_getter); |
| 171 // Constructor receives setting and env var getter implementations | 172 // Constructor receives setting and env var getter implementations |
| 172 // to use, and takes ownership of them. Used for testing. | 173 // to use, and takes ownership of them. Used for testing. |
| 173 Delegate(base::Environment* env_var_getter, SettingGetter* setting_getter); | 174 Delegate(base::Environment* env_var_getter, SettingGetter* setting_getter); |
| 174 | 175 |
| 175 // Synchronously obtains the proxy configuration. If gconf, | 176 // Posts SetUpAndFetchInitialConfigOnUIThread() to UI thread. Called |
| 176 // gsettings, or kioslaverc are used, also enables notifications for | 177 // from the IO thread. |
| 177 // setting changes. gconf/gsettings must only be accessed from the | |
| 178 // thread running the default glib main loop, and so this method | |
| 179 // must be called from the UI thread. The message loop for the IO | |
| 180 // thread is specified so that notifications can post tasks to it | |
| 181 // (and for assertions). The message loop for the file thread is | |
| 182 // used to read any files needed to determine proxy settings. | |
| 183 void SetUpAndFetchInitialConfig( | 178 void SetUpAndFetchInitialConfig( |
| 184 base::SingleThreadTaskRunner* glib_thread_task_runner, | 179 base::SingleThreadTaskRunner* glib_thread_task_runner, |
| 185 base::SingleThreadTaskRunner* io_thread_task_runner, | 180 base::SingleThreadTaskRunner* io_thread_task_runner, |
| 186 MessageLoopForIO* file_loop); | 181 MessageLoopForIO* file_loop); |
| 187 | 182 |
| 188 // Handler for setting change notifications: fetches a new proxy | 183 // Handler for setting change notifications: fetches a new proxy |
| 189 // configuration from settings, and if this config is different | 184 // configuration from settings, and if this config is different |
| 190 // than what we had before, posts a task to have it stored in | 185 // than what we had before, posts a task to have it stored in |
| 191 // cached_config_. | 186 // cached_config_. |
| 192 // Left public for simplicity. | 187 // Left public for simplicity. |
| 193 void OnCheckProxyConfigSettings(); | 188 void OnCheckProxyConfigSettings(); |
| 194 | 189 |
| 195 // Called from IO thread. | 190 // Called from IO thread. |
| 196 void AddObserver(Observer* observer); | 191 void AddObserver(Observer* observer); |
| 197 void RemoveObserver(Observer* observer); | 192 void RemoveObserver(Observer* observer); |
| 198 ProxyConfigService::ConfigAvailability GetLatestProxyConfig( | 193 ProxyConfigService::ConfigAvailability GetLatestProxyConfig( |
| 199 ProxyConfig* config); | 194 ProxyConfig* config); |
| 200 | 195 |
| 201 // Posts a call to OnDestroy() to the UI or FILE thread, depending on the | 196 // Posts a call to OnDestroy() to the UI or FILE thread, depending on the |
| 202 // setting getter in use. Called from ProxyConfigServiceLinux's destructor. | 197 // setting getter in use. Called from |
| 198 // ProxyConfigServiceLinux::StartTearDown(). |
| 203 void PostDestroyTask(); | 199 void PostDestroyTask(); |
| 204 // Safely stops change notifications. Posted to either the UI or FILE | 200 // Safely stops change notifications. Posted to either the UI or FILE |
| 205 // thread, depending on the setting getter in use. | 201 // thread, depending on the setting getter in use. |
| 206 void OnDestroy(); | 202 void OnDestroy(); |
| 207 | 203 |
| 208 private: | 204 private: |
| 209 friend class base::RefCountedThreadSafe<Delegate>; | 205 friend class base::RefCountedThreadSafe<Delegate>; |
| 210 | 206 |
| 211 ~Delegate(); | 207 ~Delegate(); |
| 212 | 208 |
| 209 // Synchronously obtains the proxy configuration. If gconf, |
| 210 // gsettings, or kioslaverc are used, also enables notifications for |
| 211 // setting changes. gconf/gsettings must only be accessed from the |
| 212 // thread running the default glib main loop, and so this method |
| 213 // must be called from the UI thread. |
| 214 void SetUpAndFetchInitialConfigOnUIThread( |
| 215 MessageLoopForIO* file_loop); |
| 216 |
| 213 // Obtains an environment variable's value. Parses a proxy server | 217 // Obtains an environment variable's value. Parses a proxy server |
| 214 // specification from it and puts it in result. Returns true if the | 218 // specification from it and puts it in result. Returns true if the |
| 215 // requested variable is defined and the value valid. | 219 // requested variable is defined and the value valid. |
| 216 bool GetProxyFromEnvVarForScheme(const char* variable, | 220 bool GetProxyFromEnvVarForScheme(const char* variable, |
| 217 ProxyServer::Scheme scheme, | 221 ProxyServer::Scheme scheme, |
| 218 ProxyServer* result_server); | 222 ProxyServer* result_server); |
| 219 // As above but with scheme set to HTTP, for convenience. | 223 // As above but with scheme set to HTTP, for convenience. |
| 220 bool GetProxyFromEnvVar(const char* variable, ProxyServer* result_server); | 224 bool GetProxyFromEnvVar(const char* variable, ProxyServer* result_server); |
| 221 // Fills proxy config from environment variables. Returns true if | 225 // Fills proxy config from environment variables. Returns true if |
| 222 // variables were found and the configuration is valid. | 226 // variables were found and the configuration is valid. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 234 // This method is posted from the UI thread to the IO thread to | 238 // This method is posted from the UI thread to the IO thread to |
| 235 // carry the new config information. | 239 // carry the new config information. |
| 236 void SetNewProxyConfig(const ProxyConfig& new_config); | 240 void SetNewProxyConfig(const ProxyConfig& new_config); |
| 237 | 241 |
| 238 // This method is run on the getter's notification thread. | 242 // This method is run on the getter's notification thread. |
| 239 void SetUpNotifications(); | 243 void SetUpNotifications(); |
| 240 | 244 |
| 241 scoped_ptr<base::Environment> env_var_getter_; | 245 scoped_ptr<base::Environment> env_var_getter_; |
| 242 scoped_ptr<SettingGetter> setting_getter_; | 246 scoped_ptr<SettingGetter> setting_getter_; |
| 243 | 247 |
| 248 // Has initialization completed and it's safe to read cached_config_. |
| 249 bool finished_initialization_; |
| 250 |
| 244 // Cached proxy configuration, to be returned by | 251 // Cached proxy configuration, to be returned by |
| 245 // GetLatestProxyConfig. Initially populated from the UI thread, but | 252 // GetLatestProxyConfig. Only accessed from the IO thread. |
| 246 // afterwards only accessed from the IO thread. | |
| 247 ProxyConfig cached_config_; | 253 ProxyConfig cached_config_; |
| 248 | 254 |
| 249 // A copy kept on the UI thread of the last seen proxy config, so as | 255 // A copy kept on the UI thread of the last seen proxy config, so as |
| 250 // to avoid posting a call to SetNewProxyConfig when we get a | 256 // to avoid posting a call to SetNewProxyConfig when we get a |
| 251 // notification but the config has not actually changed. | 257 // notification but the config has not actually changed. |
| 252 ProxyConfig reference_config_; | 258 ProxyConfig reference_config_; |
| 253 | 259 |
| 254 // The task runner for the glib thread, aka main browser thread. This thread | 260 // The task runner for the glib thread, aka main browser thread. This thread |
| 255 // is where we run the glib main loop (see base/message_pump_glib.h). It is | 261 // is where we run the glib main loop (see base/message_pump_glib.h). It is |
| 256 // the glib default loop in the sense that it runs the glib default context: | 262 // the glib default loop in the sense that it runs the glib default context: |
| 257 // as in the context where sources are added by g_timeout_add and | 263 // as in the context where sources are added by g_timeout_add and |
| 258 // g_idle_add, and returned by g_main_context_default. gconf uses glib | 264 // g_idle_add, and returned by g_main_context_default. gconf uses glib |
| 259 // timeouts and idles and possibly other callbacks that will all be | 265 // timeouts and idles and possibly other callbacks that will all be |
| 260 // dispatched on this thread. Since gconf is not thread safe, any use of | 266 // dispatched on this thread. Since gconf is not thread safe, any use of |
| 261 // gconf must be done on the thread running this loop. | 267 // gconf must be done on the thread running this loop. |
| 262 scoped_refptr<base::SingleThreadTaskRunner> glib_thread_task_runner_; | 268 scoped_refptr<base::SingleThreadTaskRunner> glib_thread_task_runner_; |
| 263 // Task runner for the IO thread. GetLatestProxyConfig() is called from | 269 // Task runner for the IO thread. GetLatestProxyConfig() is called from |
| 264 // the thread running this loop. | 270 // the thread running this loop. |
| 265 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner_; | 271 scoped_refptr<base::SingleThreadTaskRunner> io_thread_task_runner_; |
| 266 | 272 |
| 267 ObserverList<Observer> observers_; | 273 ObserverList<Observer> observers_; |
| 268 | 274 |
| 269 DISALLOW_COPY_AND_ASSIGN(Delegate); | 275 DISALLOW_COPY_AND_ASSIGN(Delegate); |
| 270 }; | 276 }; |
| 271 | 277 |
| 272 // Thin wrapper shell around Delegate. | 278 // Thin wrapper shell around Delegate. |
| 273 | 279 |
| 274 // Usual constructor | 280 // Usual constructor |
| 275 ProxyConfigServiceLinux(); | 281 ProxyConfigServiceLinux( |
| 282 base::SingleThreadTaskRunner* glib_thread_task_runner, |
| 283 base::SingleThreadTaskRunner* io_thread_task_runner, |
| 284 MessageLoopForIO* file_loop); |
| 276 // For testing: take alternate setting and env var getter implementations. | 285 // For testing: take alternate setting and env var getter implementations. |
| 277 explicit ProxyConfigServiceLinux(base::Environment* env_var_getter); | 286 explicit ProxyConfigServiceLinux(base::Environment* env_var_getter); |
| 278 ProxyConfigServiceLinux(base::Environment* env_var_getter, | 287 ProxyConfigServiceLinux(base::Environment* env_var_getter, |
| 279 SettingGetter* setting_getter); | 288 SettingGetter* setting_getter); |
| 280 | 289 |
| 281 virtual ~ProxyConfigServiceLinux(); | 290 virtual ~ProxyConfigServiceLinux(); |
| 282 | 291 |
| 292 // This is only public for testing. |
| 283 void SetupAndFetchInitialConfig( | 293 void SetupAndFetchInitialConfig( |
| 284 base::SingleThreadTaskRunner* glib_thread_task_runner, | 294 base::SingleThreadTaskRunner* glib_thread_task_runner, |
| 285 base::SingleThreadTaskRunner* io_thread_task_runner, | 295 base::SingleThreadTaskRunner* io_thread_task_runner, |
| 286 MessageLoopForIO* file_loop) { | 296 MessageLoopForIO* file_loop) { |
| 287 delegate_->SetUpAndFetchInitialConfig(glib_thread_task_runner, | 297 delegate_->SetUpAndFetchInitialConfig(glib_thread_task_runner, |
| 288 io_thread_task_runner, file_loop); | 298 io_thread_task_runner, file_loop); |
| 289 } | 299 } |
| 290 void OnCheckProxyConfigSettings() { | 300 void OnCheckProxyConfigSettings() { |
| 291 delegate_->OnCheckProxyConfigSettings(); | 301 delegate_->OnCheckProxyConfigSettings(); |
| 292 } | 302 } |
| 293 | 303 |
| 294 // ProxyConfigService methods: | 304 // ProxyConfigService methods: |
| 295 // Called from IO thread. | 305 // Called from IO thread. |
| 296 virtual void AddObserver(Observer* observer) OVERRIDE; | 306 virtual void AddObserver(Observer* observer) OVERRIDE; |
| 297 virtual void RemoveObserver(Observer* observer) OVERRIDE; | 307 virtual void RemoveObserver(Observer* observer) OVERRIDE; |
| 298 virtual ProxyConfigService::ConfigAvailability GetLatestProxyConfig( | 308 virtual ProxyConfigService::ConfigAvailability GetLatestProxyConfig( |
| 299 ProxyConfig* config) OVERRIDE; | 309 ProxyConfig* config) OVERRIDE; |
| 300 | 310 // Called from Glib thread. |
| 311 virtual void StartTearDown() OVERRIDE; |
| 301 private: | 312 private: |
| 302 scoped_refptr<Delegate> delegate_; | 313 scoped_refptr<Delegate> delegate_; |
| 303 | 314 |
| 304 DISALLOW_COPY_AND_ASSIGN(ProxyConfigServiceLinux); | 315 DISALLOW_COPY_AND_ASSIGN(ProxyConfigServiceLinux); |
| 305 }; | 316 }; |
| 306 | 317 |
| 307 } // namespace net | 318 } // namespace net |
| 308 | 319 |
| 309 #endif // NET_PROXY_PROXY_CONFIG_SERVICE_LINUX_H_ | 320 #endif // NET_PROXY_PROXY_CONFIG_SERVICE_LINUX_H_ |
| OLD | NEW |