system_dns_config_change_notifier.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright 2019 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_DNS_SYSTEM_DNS_CONFIG_CHANGE_NOTIFIER_H_
  5. #define NET_DNS_SYSTEM_DNS_CONFIG_CHANGE_NOTIFIER_H_
  6. #include <memory>
  7. #include "base/memory/scoped_refptr.h"
  8. #include "base/task/sequenced_task_runner.h"
  9. #include "net/base/net_export.h"
  10. #include "net/dns/dns_config.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. namespace net {
  13. class DnsConfigService;
  14. // Notifier that can be subscribed to to listen for changes to system DNS
  15. // configuration. Expected to only be used internally to HostResolverManager and
  16. // NetworkChangeNotifier. Other classes are expected to subscribe to
  17. // NetworkChangeNotifier::AddDNSObserver() to subscribe to listen to both system
  18. // config changes and configuration applied on top by Chrome.
  19. //
  20. // This class is thread and sequence safe except that RemoveObserver() must be
  21. // called on the same sequence as the matched AddObserver() call.
  22. //
  23. // TODO(crbug.com/971411): Use this class in HostResolverManager.
  24. class NET_EXPORT_PRIVATE SystemDnsConfigChangeNotifier {
  25. public:
  26. class Observer {
  27. public:
  28. // Called on loading new config, including the initial read once the first
  29. // valid config has been read. If a config read encounters errors or an
  30. // invalid config is read, will be invoked with |absl::nullopt|. Only
  31. // invoked when |config| changes.
  32. virtual void OnSystemDnsConfigChanged(absl::optional<DnsConfig> config) = 0;
  33. };
  34. SystemDnsConfigChangeNotifier();
  35. // Alternate constructor allowing specifying the underlying DnsConfigService.
  36. // |dns_config_service| will only be interacted with and destroyed using
  37. // |task_runner|. As required by DnsConfigService, blocking I/O may be
  38. // performed on |task_runner|, so it must support blocking (i.e.
  39. // base::MayBlock).
  40. //
  41. // |dns_config_service| may be null if system DNS config is disabled for the
  42. // current platform. Calls against the created object will noop, and no
  43. // notifications will ever be sent.
  44. SystemDnsConfigChangeNotifier(
  45. scoped_refptr<base::SequencedTaskRunner> task_runner,
  46. std::unique_ptr<DnsConfigService> dns_config_service);
  47. SystemDnsConfigChangeNotifier(const SystemDnsConfigChangeNotifier&) = delete;
  48. SystemDnsConfigChangeNotifier& operator=(
  49. const SystemDnsConfigChangeNotifier&) = delete;
  50. ~SystemDnsConfigChangeNotifier();
  51. // An added Observer will receive notifications on the sequence where
  52. // AddObserver() was called. If the config has been successfully read before
  53. // calling this method, a notification will be sent for that current config
  54. // before any other notifications.
  55. void AddObserver(Observer* observer);
  56. // In order to ensure notifications immediately stop on calling
  57. // RemoveObserver(), must be called on the same sequence where the associated
  58. // AddObserver() was called.
  59. void RemoveObserver(Observer* observer);
  60. // Triggers invalidation and re-read of the current configuration (followed by
  61. // notifications to registered Observers). For use only on platforms
  62. // expecting network-stack-external notifications of DNS config changes.
  63. void RefreshConfig();
  64. void SetDnsConfigServiceForTesting(
  65. std::unique_ptr<DnsConfigService> dns_config_service);
  66. private:
  67. class Core;
  68. std::unique_ptr<Core, base::OnTaskRunnerDeleter> core_;
  69. };
  70. } // namespace net
  71. #endif // NET_DNS_SYSTEM_DNS_CONFIG_CHANGE_NOTIFIER_H_