test_dns_config_service.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. #include "net/dns/test_dns_config_service.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/check.h"
  8. #include "base/files/file_path.h"
  9. #include "net/dns/dns_hosts.h"
  10. #include "third_party/abseil-cpp/absl/types/optional.h"
  11. namespace net {
  12. TestDnsConfigService::TestDnsConfigService()
  13. : DnsConfigService(base::FilePath::StringPieceType() /* hosts_file_path */,
  14. absl::nullopt /* config_change_delay */) {}
  15. TestDnsConfigService::~TestDnsConfigService() = default;
  16. bool TestDnsConfigService::StartWatching() {
  17. return true;
  18. }
  19. void TestDnsConfigService::RefreshConfig() {
  20. DCHECK(config_for_refresh_);
  21. InvalidateConfig();
  22. InvalidateHosts();
  23. OnConfigRead(config_for_refresh_.value());
  24. OnHostsRead(config_for_refresh_.value().hosts);
  25. config_for_refresh_ = absl::nullopt;
  26. }
  27. HostsReadingTestDnsConfigService::HostsReadingTestDnsConfigService(
  28. HostsParserFactory hosts_parser_factory)
  29. : hosts_reader_(
  30. std::make_unique<HostsReader>(*this,
  31. std::move(hosts_parser_factory))) {}
  32. HostsReadingTestDnsConfigService::~HostsReadingTestDnsConfigService() = default;
  33. void HostsReadingTestDnsConfigService::ReadHostsNow() {
  34. hosts_reader_->WorkNow();
  35. }
  36. bool HostsReadingTestDnsConfigService::StartWatching() {
  37. watcher_->Watch();
  38. return true;
  39. }
  40. HostsReadingTestDnsConfigService::HostsReader::HostsReader(
  41. TestDnsConfigService& service,
  42. HostsParserFactory hosts_parser_factory)
  43. : DnsConfigService::HostsReader(
  44. /*hosts_file_path=*/base::FilePath::StringPieceType(),
  45. service),
  46. hosts_parser_factory_(std::move(hosts_parser_factory)) {}
  47. HostsReadingTestDnsConfigService::HostsReader::~HostsReader() = default;
  48. std::unique_ptr<SerialWorker::WorkItem>
  49. HostsReadingTestDnsConfigService::HostsReader::CreateWorkItem() {
  50. return std::make_unique<WorkItem>(hosts_parser_factory_.Run());
  51. }
  52. HostsReadingTestDnsConfigService::Watcher::Watcher(DnsConfigService& service)
  53. : DnsConfigService::Watcher(service) {}
  54. HostsReadingTestDnsConfigService::Watcher::~Watcher() = default;
  55. void HostsReadingTestDnsConfigService::Watcher::TriggerHostsChangeNotification(
  56. bool success) {
  57. CHECK(watch_started_);
  58. OnHostsChanged(success);
  59. }
  60. bool HostsReadingTestDnsConfigService::Watcher::Watch() {
  61. watch_started_ = true;
  62. return true;
  63. }
  64. } // namespace net