page_timing_metrics_sender.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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_PAGE_LOAD_METRICS_RENDERER_PAGE_TIMING_METRICS_SENDER_H_
  5. #define COMPONENTS_PAGE_LOAD_METRICS_RENDERER_PAGE_TIMING_METRICS_SENDER_H_
  6. #include <memory>
  7. #include "base/containers/flat_set.h"
  8. #include "base/containers/small_map.h"
  9. #include "components/page_load_metrics/common/page_load_timing.h"
  10. #include "components/page_load_metrics/renderer/page_resource_data_use.h"
  11. #include "components/page_load_metrics/renderer/page_timing_metadata_recorder.h"
  12. #include "services/network/public/mojom/url_response_head.mojom-forward.h"
  13. #include "third_party/blink/public/common/loader/loading_behavior_flag.h"
  14. #include "third_party/blink/public/common/responsiveness_metrics/user_interaction_latency.h"
  15. #include "third_party/blink/public/common/use_counter/use_counter_feature_tracker.h"
  16. #include "third_party/blink/public/web/web_local_frame_client.h"
  17. class GURL;
  18. namespace base {
  19. class OneShotTimer;
  20. } // namespace base
  21. namespace network {
  22. struct URLLoaderCompletionStatus;
  23. } // namespace network
  24. namespace page_load_metrics {
  25. class PageTimingSender;
  26. // PageTimingMetricsSender is responsible for sending page load timing metrics
  27. // over IPC. PageTimingMetricsSender may coalesce sent IPCs in order to
  28. // minimize IPC contention.
  29. class PageTimingMetricsSender {
  30. public:
  31. PageTimingMetricsSender(std::unique_ptr<PageTimingSender> sender,
  32. std::unique_ptr<base::OneShotTimer> timer,
  33. mojom::PageLoadTimingPtr initial_timing,
  34. const PageTimingMetadataRecorder::MonotonicTiming&
  35. initial_monotonic_timing,
  36. std::unique_ptr<PageResourceDataUse> initial_request);
  37. PageTimingMetricsSender(const PageTimingMetricsSender&) = delete;
  38. PageTimingMetricsSender& operator=(const PageTimingMetricsSender&) = delete;
  39. ~PageTimingMetricsSender();
  40. void DidObserveLoadingBehavior(blink::LoadingBehaviorFlag behavior);
  41. void DidObserveNewFeatureUsage(const blink::UseCounterFeature& feature);
  42. void DidObserveSoftNavigation(uint32_t count);
  43. void DidObserveLayoutShift(double score, bool after_input_or_scroll);
  44. void DidObserveLayoutNg(uint32_t all_block_count,
  45. uint32_t ng_block_count,
  46. uint32_t all_call_count,
  47. uint32_t ng_call_count);
  48. void DidObserveMobileFriendlinessChanged(const blink::MobileFriendliness&);
  49. void DidStartResponse(const url::SchemeHostPort& final_response_url,
  50. int resource_id,
  51. const network::mojom::URLResponseHead& response_head,
  52. network::mojom::RequestDestination request_destination);
  53. void DidReceiveTransferSizeUpdate(int resource_id, int received_data_length);
  54. void DidCompleteResponse(int resource_id,
  55. const network::URLLoaderCompletionStatus& status);
  56. void DidCancelResponse(int resource_id);
  57. void DidLoadResourceFromMemoryCache(const GURL& response_url,
  58. int request_id,
  59. int64_t encoded_body_length,
  60. const std::string& mime_type);
  61. void OnMainFrameIntersectionChanged(
  62. const gfx::Rect& main_frame_intersection_rect);
  63. void OnMainFrameViewportRectangleChanged(
  64. const gfx::Rect& main_frame_viewport_rect);
  65. void DidObserveInputDelay(base::TimeDelta input_delay);
  66. void DidObserveUserInteraction(base::TimeDelta max_event_duration,
  67. blink::UserInteractionType interaction_type);
  68. // Updates the timing information. Buffers |timing| to be sent over mojo
  69. // sometime 'soon'.
  70. void Update(
  71. mojom::PageLoadTimingPtr timing,
  72. const PageTimingMetadataRecorder::MonotonicTiming& monotonic_timing);
  73. // Sends any queued timing data immediately and stops the send timer.
  74. void SendLatest();
  75. // Updates the PageLoadMetrics::CpuTiming data and starts the send timer.
  76. void UpdateCpuTiming(base::TimeDelta task_time);
  77. void UpdateResourceMetadata(int resource_id,
  78. bool is_ad_resource,
  79. bool is_main_frame_resource,
  80. bool completed_before_fcp);
  81. void SetUpSmoothnessReporting(base::ReadOnlySharedMemoryRegion shared_memory);
  82. void InitiateUserInteractionTiming();
  83. protected:
  84. base::OneShotTimer* timer() const { return timer_.get(); }
  85. private:
  86. void EnsureSendTimer(bool urgent = false);
  87. void SendNow();
  88. void ClearNewFeatures();
  89. std::unique_ptr<PageTimingSender> sender_;
  90. std::unique_ptr<base::OneShotTimer> timer_;
  91. mojom::PageLoadTimingPtr last_timing_;
  92. mojom::CpuTimingPtr last_cpu_timing_;
  93. mojom::InputTimingPtr input_timing_delta_;
  94. absl::optional<blink::MobileFriendliness> mobile_friendliness_;
  95. // The the sender keep track of metadata as it comes in, because the sender is
  96. // scoped to a single committed load.
  97. mojom::FrameMetadataPtr metadata_;
  98. // A list of newly observed features during page load, to be sent to the
  99. // browser.
  100. std::vector<blink::UseCounterFeature> new_features_;
  101. mojom::FrameRenderDataUpdate render_data_;
  102. blink::UseCounterFeatureTracker feature_tracker_;
  103. uint32_t soft_navigation_count_ = 0;
  104. bool have_sent_ipc_ = false;
  105. // The page's resources that are currently loading, or were completed after
  106. // the last timing update.
  107. base::small_map<std::map<int, std::unique_ptr<PageResourceDataUse>>, 16>
  108. page_resource_data_use_;
  109. // Set of all resources that have completed or received a transfer
  110. // size update since the last timimg update.
  111. base::flat_set<PageResourceDataUse*> modified_resources_;
  112. // Field trial for alternating page timing metrics sender buffer timer delay.
  113. // https://crbug.com/847269.
  114. int buffer_timer_delay_ms_;
  115. // Responsible for recording sampling profiler metadata corresponding to page
  116. // timing.
  117. PageTimingMetadataRecorder metadata_recorder_;
  118. };
  119. } // namespace page_load_metrics
  120. #endif // COMPONENTS_PAGE_LOAD_METRICS_RENDERER_PAGE_TIMING_METRICS_SENDER_H_