proxy_config_service.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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_PROXY_CONFIG_SERVICE_H_
  5. #define NET_PROXY_RESOLUTION_PROXY_CONFIG_SERVICE_H_
  6. #include <memory>
  7. #include "base/memory/ref_counted.h"
  8. #include "net/base/net_export.h"
  9. namespace base {
  10. class SequencedTaskRunner;
  11. } // namespace base
  12. namespace net {
  13. class ProxyConfigWithAnnotation;
  14. // Service for watching when the proxy settings have changed.
  15. class NET_EXPORT ProxyConfigService {
  16. public:
  17. // Indicates whether proxy configuration is valid, and if not, why.
  18. enum ConfigAvailability {
  19. // Configuration is pending, observers will be notified later.
  20. CONFIG_PENDING,
  21. // Configuration is present and valid.
  22. CONFIG_VALID,
  23. // No configuration is set.
  24. CONFIG_UNSET
  25. };
  26. // Observer for being notified when the proxy settings have changed.
  27. class NET_EXPORT Observer {
  28. public:
  29. virtual ~Observer() = default;
  30. // Notification callback that should be invoked by ProxyConfigService
  31. // implementors whenever the configuration changes. |availability| indicates
  32. // the new availability status and can be CONFIG_UNSET or CONFIG_VALID (in
  33. // which case |config| contains the configuration). Implementors must not
  34. // pass CONFIG_PENDING.
  35. virtual void OnProxyConfigChanged(const ProxyConfigWithAnnotation& config,
  36. ConfigAvailability availability) = 0;
  37. };
  38. virtual ~ProxyConfigService() = default;
  39. // Adds/Removes an observer that will be called whenever the proxy
  40. // configuration has changed.
  41. virtual void AddObserver(Observer* observer) = 0;
  42. virtual void RemoveObserver(Observer* observer) = 0;
  43. // Gets the most recent availability status. If a configuration is present,
  44. // the proxy configuration is written to |config| and CONFIG_VALID is
  45. // returned. Returns CONFIG_PENDING if it is not available yet. In this case,
  46. // it is guaranteed that subscribed observers will be notified of a change at
  47. // some point in the future once the configuration is available.
  48. // Note that to avoid re-entrancy problems, implementations should not
  49. // dispatch any change notifications from within this function.
  50. virtual ConfigAvailability GetLatestProxyConfig(
  51. ProxyConfigWithAnnotation* config) = 0;
  52. // ConfiguredProxyResolutionService will call this periodically during periods
  53. // of activity. It can be used as a signal for polling-based implementations.
  54. //
  55. // Note that this is purely used as an optimization -- polling
  56. // implementations could simply set a global timer that goes off every
  57. // X seconds at which point they check for changes. However that has
  58. // the disadvantage of doing continuous work even during idle periods.
  59. virtual void OnLazyPoll() {}
  60. // Creates a config service appropriate for this platform that fetches the
  61. // system proxy settings. |main_task_runner| is the sequence where the
  62. // consumer of the ProxyConfigService will live.
  63. static std::unique_ptr<ProxyConfigService> CreateSystemProxyConfigService(
  64. scoped_refptr<base::SequencedTaskRunner> main_task_runner);
  65. };
  66. } // namespace net
  67. #endif // NET_PROXY_RESOLUTION_PROXY_CONFIG_SERVICE_H_