optimization_guide_navigation_data.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 COMPONENTS_OPTIMIZATION_GUIDE_CORE_OPTIMIZATION_GUIDE_NAVIGATION_DATA_H_
  5. #define COMPONENTS_OPTIMIZATION_GUIDE_CORE_OPTIMIZATION_GUIDE_NAVIGATION_DATA_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <utility>
  9. #include "base/containers/flat_map.h"
  10. #include "base/containers/flat_set.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/time/time.h"
  13. #include "components/optimization_guide/core/optimization_guide_enums.h"
  14. #include "components/optimization_guide/proto/hints.pb.h"
  15. #include "components/optimization_guide/proto/models.pb.h"
  16. #include "third_party/abseil-cpp/absl/types/optional.h"
  17. #include "url/gurl.h"
  18. // A representation of optimization guide information related to a navigation.
  19. // Metrics will be recorded upon this object's destruction.
  20. class OptimizationGuideNavigationData {
  21. public:
  22. OptimizationGuideNavigationData(int64_t navigation_id,
  23. base::TimeTicks navigation_start);
  24. ~OptimizationGuideNavigationData();
  25. OptimizationGuideNavigationData(
  26. const OptimizationGuideNavigationData& other) = delete;
  27. OptimizationGuideNavigationData& operator=(
  28. const OptimizationGuideNavigationData&) = delete;
  29. base::WeakPtr<OptimizationGuideNavigationData> GetWeakPtr() {
  30. return weak_ptr_factory_.GetWeakPtr();
  31. }
  32. // The navigation ID of the navigation handle that this data is
  33. // associated with.
  34. int64_t navigation_id() const { return navigation_id_; }
  35. // The time of navigation start.
  36. base::TimeTicks navigation_start() const { return navigation_start_; }
  37. // The navigation URL of the navigation handle that this data is
  38. // associated with.
  39. GURL navigation_url() const { return navigation_url_; }
  40. void set_navigation_url(const GURL& navigation_url) {
  41. navigation_url_ = navigation_url;
  42. }
  43. // The optimization types that were registered at the start of the navigation.
  44. base::flat_set<optimization_guide::proto::OptimizationType>
  45. registered_optimization_types() const {
  46. return registered_optimization_types_;
  47. }
  48. void set_registered_optimization_types(
  49. base::flat_set<optimization_guide::proto::OptimizationType>
  50. registered_optimization_types) {
  51. registered_optimization_types_ = registered_optimization_types;
  52. }
  53. // The optimization targets that were registered at the start of the
  54. // navigation.
  55. base::flat_set<optimization_guide::proto::OptimizationTarget>
  56. registered_optimization_targets() const {
  57. return registered_optimization_targets_;
  58. }
  59. void set_registered_optimization_targets(
  60. base::flat_set<optimization_guide::proto::OptimizationTarget>
  61. registered_optimization_targets) {
  62. registered_optimization_targets_ = registered_optimization_targets;
  63. }
  64. // The duration between the fetch for a hint for the navigation going out to
  65. // when it was received by the client if a fetch was initiated for the
  66. // navigation.
  67. absl::optional<base::TimeDelta> hints_fetch_latency() const;
  68. void set_hints_fetch_start(base::TimeTicks hints_fetch_start) {
  69. hints_fetch_start_ = hints_fetch_start;
  70. }
  71. void set_hints_fetch_end(base::TimeTicks hints_fetch_end) {
  72. hints_fetch_end_ = hints_fetch_end;
  73. }
  74. // The status for whether a hint for the page load was attempted to be fetched
  75. // from the remote Optimization Guide Service.
  76. absl::optional<optimization_guide::RaceNavigationFetchAttemptStatus>
  77. hints_fetch_attempt_status() const {
  78. return hints_fetch_attempt_status_;
  79. }
  80. void set_hints_fetch_attempt_status(
  81. optimization_guide::RaceNavigationFetchAttemptStatus
  82. hints_fetch_attempt_status) {
  83. hints_fetch_attempt_status_ = hints_fetch_attempt_status;
  84. }
  85. private:
  86. // Records metrics based on data currently held in |this|.
  87. void RecordMetrics() const;
  88. // Records the OptimizationGuide UKM event based on data currently held in
  89. // |this|.
  90. void RecordOptimizationGuideUKM() const;
  91. // The navigation ID of the navigation handle that this data is associated
  92. // with.
  93. const int64_t navigation_id_;
  94. // The time of navigation start.
  95. const base::TimeTicks navigation_start_;
  96. // The navigation URL of the navigation handle this data is associated with.
  97. // Updated on navigation start and navigation redirects.
  98. GURL navigation_url_;
  99. // The optimization types that were registered at the start of the navigation.
  100. base::flat_set<optimization_guide::proto::OptimizationType>
  101. registered_optimization_types_;
  102. // The optimization targets that were registered at the start of the
  103. // navigation.
  104. base::flat_set<optimization_guide::proto::OptimizationTarget>
  105. registered_optimization_targets_;
  106. // The map from optimization type to the last decision made for that type.
  107. base::flat_map<optimization_guide::proto::OptimizationType,
  108. optimization_guide::OptimizationTypeDecision>
  109. optimization_type_decisions_;
  110. // The page hint for the navigation.
  111. absl::optional<std::unique_ptr<optimization_guide::proto::PageHint>>
  112. page_hint_;
  113. // The time that the hints fetch for this navigation started. Is only present
  114. // if a fetch was initiated for this navigation.
  115. absl::optional<base::TimeTicks> hints_fetch_start_;
  116. // The time that the hints fetch for the navigation ended. Is only present if
  117. // a fetch was initiated and successfully completed for this navigation.
  118. absl::optional<base::TimeTicks> hints_fetch_end_;
  119. // The status for whether a hint for the page load was attempted to be fetched
  120. // from the remote Optimization Guide Service.
  121. absl::optional<optimization_guide::RaceNavigationFetchAttemptStatus>
  122. hints_fetch_attempt_status_;
  123. // Used to get |weak_ptr_| to self on the UI thread.
  124. base::WeakPtrFactory<OptimizationGuideNavigationData> weak_ptr_factory_{this};
  125. };
  126. #endif // COMPONENTS_OPTIMIZATION_GUIDE_CORE_OPTIMIZATION_GUIDE_NAVIGATION_DATA_H_