polling_proxy_config_service.h 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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
  3. // found in the LICENSE file.
  4. #ifndef NET_PROXY_RESOLUTION_POLLING_PROXY_CONFIG_SERVICE_H_
  5. #define NET_PROXY_RESOLUTION_POLLING_PROXY_CONFIG_SERVICE_H_
  6. #include "base/compiler_specific.h"
  7. #include "base/memory/ref_counted.h"
  8. #include "base/time/time.h"
  9. #include "net/base/net_export.h"
  10. #include "net/proxy_resolution/proxy_config_service.h"
  11. #include "net/traffic_annotation/network_traffic_annotation.h"
  12. namespace net {
  13. // PollingProxyConfigService is a base class for creating ProxyConfigService
  14. // implementations that use polling to notice when settings have change.
  15. //
  16. // It runs code to get the current proxy settings on a background worker
  17. // thread, and notifies registered observers when the value changes.
  18. class NET_EXPORT_PRIVATE PollingProxyConfigService : public ProxyConfigService {
  19. public:
  20. PollingProxyConfigService(const PollingProxyConfigService&) = delete;
  21. PollingProxyConfigService& operator=(const PollingProxyConfigService&) =
  22. delete;
  23. // ProxyConfigService implementation:
  24. void AddObserver(Observer* observer) override;
  25. void RemoveObserver(Observer* observer) override;
  26. ConfigAvailability GetLatestProxyConfig(
  27. ProxyConfigWithAnnotation* config) override;
  28. void OnLazyPoll() override;
  29. protected:
  30. // Function for retrieving the current proxy configuration.
  31. // Implementors must be threadsafe as the function will be invoked from
  32. // worker threads.
  33. typedef void (*GetConfigFunction)(NetworkTrafficAnnotationTag,
  34. ProxyConfigWithAnnotation*);
  35. // Creates a polling-based ProxyConfigService which will test for new
  36. // settings at most every |poll_interval| time by calling |get_config_func|
  37. // on a worker thread.
  38. PollingProxyConfigService(
  39. base::TimeDelta poll_interval,
  40. GetConfigFunction get_config_func,
  41. const NetworkTrafficAnnotationTag& traffic_annotation);
  42. ~PollingProxyConfigService() override;
  43. // Polls for changes by posting a task to the worker pool.
  44. void CheckForChangesNow();
  45. private:
  46. class Core;
  47. scoped_refptr<Core> core_;
  48. };
  49. } // namespace net
  50. #endif // NET_PROXY_RESOLUTION_POLLING_PROXY_CONFIG_SERVICE_H_