context_manager.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2015 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 COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_MANAGER_H_
  5. #define COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_MANAGER_H_
  6. #include <stddef.h>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/time/time.h"
  12. #include "base/values.h"
  13. #include "components/domain_reliability/beacon.h"
  14. #include "components/domain_reliability/config.h"
  15. #include "components/domain_reliability/context.h"
  16. #include "components/domain_reliability/domain_reliability_export.h"
  17. #include "url/gurl.h"
  18. namespace domain_reliability {
  19. // Owns DomainReliabilityContexts, receives Beacons and queues them in the
  20. // appropriate Context.
  21. // TODO(chlily): This class should be merged with the DomainReliabilityMonitor.
  22. class DOMAIN_RELIABILITY_EXPORT DomainReliabilityContextManager {
  23. public:
  24. DomainReliabilityContextManager(
  25. const MockableTime* time,
  26. const std::string& upload_reporter_string,
  27. DomainReliabilityContext::UploadAllowedCallback upload_allowed_callback,
  28. DomainReliabilityDispatcher* dispatcher);
  29. DomainReliabilityContextManager(const DomainReliabilityContextManager&) =
  30. delete;
  31. DomainReliabilityContextManager& operator=(
  32. const DomainReliabilityContextManager&) = delete;
  33. ~DomainReliabilityContextManager();
  34. // If |url| maps to a context added to this manager, calls |OnBeacon| on
  35. // that context with |beacon|. Otherwise, does nothing.
  36. // Prefers contexts that are an exact match for the beacon's url's hostname
  37. // over a subdomain-inclusive context for the beacon's superdomain.
  38. // If the beacon should be routed to a Google config that has not yet been
  39. // created, this will create it first.
  40. void RouteBeacon(std::unique_ptr<DomainReliabilityBeacon> beacon);
  41. // Calls |ClearBeacons| on all contexts matched by |origin_filter| added
  42. // to this manager, but leaves the contexts themselves intact. A null
  43. // |origin_filter| is interpreted as an always-true filter, indicating
  44. // complete deletion.
  45. void ClearBeacons(
  46. const base::RepeatingCallback<bool(const url::Origin&)>& origin_filter);
  47. // Creates and stores a context for the given |config|, which should be for an
  48. // origin distinct from any existing ones. Returns pointer to the inserted
  49. // context.
  50. DomainReliabilityContext* AddContextForConfig(
  51. std::unique_ptr<const DomainReliabilityConfig> config);
  52. // Removes all contexts matched by |origin_filter| from this manager
  53. // (discarding all queued beacons in the process). A null |origin_filter|
  54. // is interpreted as an always-true filter, indicating complete deletion.
  55. void RemoveContexts(
  56. const base::RepeatingCallback<bool(const url::Origin&)>& origin_filter);
  57. // Finds a context for the exact domain |host|. Otherwise returns nullptr.
  58. DomainReliabilityContext* GetContext(const std::string& host) const;
  59. // Finds a context for the parent domain of |host|, which must include
  60. // subdomains. Otherwise returns nullptr.
  61. DomainReliabilityContext* GetSuperdomainContext(
  62. const std::string& host) const;
  63. void OnNetworkChanged(base::TimeTicks now);
  64. // Called by the Monitor during initialization. Should be called exactly once.
  65. // |uploader_| needs to be set before any contexts are created.
  66. void SetUploader(DomainReliabilityUploader* uploader);
  67. size_t contexts_size_for_testing() const { return contexts_.size(); }
  68. private:
  69. // Maps the hostname to the DomainReliabilityContext for that origin.
  70. using ContextMap =
  71. std::map<std::string, std::unique_ptr<DomainReliabilityContext>>;
  72. std::unique_ptr<DomainReliabilityContext> CreateContextForConfig(
  73. std::unique_ptr<const DomainReliabilityConfig> config) const;
  74. // |time_| is owned by the Monitor and a copy of this pointer is given to
  75. // every Context.
  76. const raw_ptr<const MockableTime> time_;
  77. const std::string upload_reporter_string_;
  78. base::TimeTicks last_network_change_time_;
  79. DomainReliabilityContext::UploadAllowedCallback upload_allowed_callback_;
  80. // |dispatcher_| is owned by the Monitor and a copy of this pointer is given
  81. // to every Context.
  82. const raw_ptr<DomainReliabilityDispatcher> dispatcher_;
  83. // |uploader_| is owned by the Monitor. Expected to be non-null after
  84. // initialization because it should be set by the Monitor, as long as the
  85. // Monitor has been fully initialized. A copy of this pointer is given to
  86. // every Context.
  87. raw_ptr<DomainReliabilityUploader> uploader_ = nullptr;
  88. ContextMap contexts_;
  89. };
  90. } // namespace domain_reliability
  91. #endif // COMPONENTS_DOMAIN_RELIABILITY_CONTEXT_MANAGER_H_