File: page_load_tracker.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (611 lines) | stat: -rw-r--r-- 27,150 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_PAGE_LOAD_METRICS_BROWSER_PAGE_LOAD_TRACKER_H_
#define COMPONENTS_PAGE_LOAD_METRICS_BROWSER_PAGE_LOAD_TRACKER_H_

#include <memory>
#include <optional>
#include <vector>

#include "base/containers/flat_map.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "components/page_load_metrics/browser/observers/core/largest_contentful_paint_handler.h"
#include "components/page_load_metrics/browser/page_load_metrics_observer.h"
#include "components/page_load_metrics/browser/page_load_metrics_observer_delegate.h"
#include "components/page_load_metrics/browser/page_load_metrics_update_dispatcher.h"
#include "components/page_load_metrics/browser/resource_tracker.h"
#include "components/page_load_metrics/common/page_end_reason.h"
#include "components/page_load_metrics/common/page_load_metrics.mojom.h"
#include "components/page_load_metrics/common/page_load_timing.h"
#include "content/public/browser/global_request_id.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/web_contents_observer.h"
#include "net/cookies/canonical_cookie.h"
#include "services/metrics/public/cpp/ukm_source.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "third_party/blink/public/common/performance/performance_timeline_constants.h"
#include "ui/base/scoped_visibility_tracker.h"
#include "ui/gfx/geometry/size.h"

class GURL;

namespace blink {
class WebInputEvent;
}  // namespace blink

namespace content {
class NavigationHandle;
class WebContents;
}  // namespace content

namespace page_load_metrics {

struct MemoryUpdate;
class PageLoadMetricsEmbedderInterface;

namespace internal {

// These values are persisted to logs. Entries should not be renumbered and
// numeric values should never be reused.
enum class PageLoadPrerenderEvent {
  kNavigationInPrerenderedMainFrame = 0,
  kPrerenderActivationNavigation = 1,
  kMaxValue = kPrerenderActivationNavigation,
};

enum class PageLoadTrackerPageType {
  kPrimaryPage = 0,
  kPrerenderPage = 1,
  kFencedFramesPage = 2,
  kPreviewPrimaryPage = 3,  // Primary page in the preview mode
  kMaxValue = kPreviewPrimaryPage,
};

extern const char kErrorEvents[];
extern const char kPageLoadPrerender2Event[];

}  // namespace internal

// These errors are internal to the page_load_metrics subsystem and do not
// reflect actual errors that occur during a page load.
//
// If you add elements to this enum, make sure you update the enum
// value in histograms.xml. Only add elements to the end to prevent
// inconsistencies between versions.
enum InternalErrorLoadEvent {
  // A timing IPC was sent from the renderer that did not line up with previous
  // data we've received (i.e. navigation start is different or the timing
  // struct is somehow invalid). This error can only occur once the IPC is
  // vetted in other ways (see other errors). This error is deprecated as it has
  // been replaced by the more detailed ERR_BAD_TIMING_IPC_* error codes.
  DEPRECATED_ERR_BAD_TIMING_IPC,

  // The following IPCs are not mutually exclusive.
  //
  // We received an IPC when we weren't tracking a committed load. This will
  // often happen if we get an IPC from a bad URL scheme (that is, the renderer
  // sent us an IPC from a navigation we don't care about).
  ERR_IPC_WITH_NO_RELEVANT_LOAD,

  // Received a notification from a frame that has been navigated away from.
  ERR_IPC_FROM_WRONG_FRAME,

  // We received an IPC even through the last committed url from the browser
  // was not http/s. This can happen with the renderer sending IPCs for the
  // new tab page. This will often come paired with
  // ERR_IPC_WITH_NO_RELEVANT_LOAD.
  ERR_IPC_FROM_BAD_URL_SCHEME,

  // If we track a navigation, but the renderer sends us no IPCs. This could
  // occur if the browser filters loads less aggressively than the renderer.
  ERR_NO_IPCS_RECEIVED,

  // Tracks frequency with which we record an end time that occurred before
  // navigation start. This is expected to happen in some cases (see comments in
  // cc file for details). We use this error counter to understand how often it
  // happens.
  ERR_END_BEFORE_NAVIGATION_START,

  // A new navigation triggers abort updates in multiple trackers in
  // |aborted_provisional_loads_|, when usually there should only be one (the
  // navigation that just aborted because of this one). If this happens, the
  // latest aborted load is used to track the chain size.
  ERR_NAVIGATION_SIGNALS_MULIPLE_ABORTED_LOADS,

  // Received user input without a relevant load. This error type is deprecated,
  // as it is valid to receive user input without a relevant load. We leave the
  // enum value here since it's also used in histogram recording, so it's
  // important that we not re-use this enum entry for a different value.
  DEPRECATED_ERR_USER_INPUT_WITH_NO_RELEVANT_LOAD,

  // A TimeTicks value in the browser process has value less than
  // navigation_start_. This could happen if navigation_start_ was computed in
  // renderer process and the system clock has inter process time tick skew.
  ERR_INTER_PROCESS_TIME_TICK_SKEW,

  // At the time a PageLoadTracker was destroyed, we had received neither a
  // commit nor a failed provisional load.
  ERR_NO_COMMIT_OR_FAILED_PROVISIONAL_LOAD,

  // No page load end time was recorded for this page load.
  ERR_NO_PAGE_LOAD_END_TIME,

  // Received a timing update from a subframe (deprecated).
  DEPRECATED_ERR_TIMING_IPC_FROM_SUBFRAME,

  // A timing IPC was sent from the renderer that contained timing data which
  // was inconsistent with our timing data for the currently committed load.
  ERR_BAD_TIMING_IPC_INVALID_TIMING_DESCENDENT,

  // A timing IPC was sent from the renderer that contained loading behavior
  // data which was inconsistent with our loading behavior data for the
  // currently committed load.
  ERR_BAD_TIMING_IPC_INVALID_BEHAVIOR_DESCENDENT,

  // A timing IPC was sent from the renderer that contained invalid timing data
  // (e.g. out of order timings, or other issues).
  ERR_BAD_TIMING_IPC_INVALID_TIMING,

  // We received a navigation start for a child frame that is before the
  // navigation start of the main frame.
  ERR_SUBFRAME_NAVIGATION_START_BEFORE_MAIN_FRAME,

  // We received an IPC from a subframe when we weren't tracking a committed
  // load. We expect this error to happen, and track it so we can understand how
  // frequently this case is encountered.
  ERR_SUBFRAME_IPC_WITH_NO_RELEVANT_LOAD,

  // We received browser-process reported metrics when we weren't tracking a
  // committed load. We expect this error to happen, and track it so we can
  // understand how frequently this case is encountered.
  ERR_BROWSER_USAGE_WITH_NO_RELEVANT_LOAD,

  // Add values before this final count.
  ERR_LAST_ENTRY,
};

// NOTE: these functions are shared by page_load_tracker.cc and
// metrics_web_contents_observer.cc. They are declared here to allow both files
// to access them.
void RecordInternalError(InternalErrorLoadEvent event);
PageEndReason EndReasonForPageTransition(ui::PageTransition transition);
bool IsNavigationUserInitiated(content::NavigationHandle* handle);

// This class tracks a given page load, starting from navigation start /
// provisional load, until a new navigation commits or the navigation fails.
// MetricsWebContentsObserver manages a set of provisional PageLoadTrackers, as
// well as a committed PageLoadTracker.
class PageLoadTracker : public PageLoadMetricsUpdateDispatcher::Client,
                        public PageLoadMetricsObserverDelegate {
 public:
  // Caller must guarantee that the `embedder_interface` pointer outlives this
  // class. The PageLoadTracker must not hold on to `navigation_handle` beyond
  // the scope of the constructor.
  PageLoadTracker(bool in_foreground,
                  PageLoadMetricsEmbedderInterface* embedder_interface,
                  const GURL& currently_committed_url,
                  bool is_first_navigation_in_web_contents,
                  content::NavigationHandle* navigation_handle,
                  UserInitiatedInfo user_initiated_info,
                  ukm::SourceId source_id,
                  base::WeakPtr<PageLoadTracker> parent_tracker);

  PageLoadTracker(const PageLoadTracker&) = delete;
  PageLoadTracker& operator=(const PageLoadTracker&) = delete;

  ~PageLoadTracker() override;

  // PageLoadMetricsUpdateDispatcher::Client implementation:
  bool IsPageMainFrame(content::RenderFrameHost* rfh) const override;
  void OnTimingChanged() override;
  void OnPageInputTimingChanged(uint64_t num_interactions) override;
  void OnSubFrameTimingChanged(content::RenderFrameHost* rfh,
                               const mojom::PageLoadTiming& timing) override;
  void OnSubFrameInputTimingChanged(
      content::RenderFrameHost* rfh,
      const mojom::InputTiming& input_timing_delta) override;
  void OnPageRenderDataChanged(const mojom::FrameRenderDataUpdate& render_data,
                               bool is_main_frame) override;
  void OnSubFrameRenderDataChanged(
      content::RenderFrameHost* rfh,
      const mojom::FrameRenderDataUpdate& render_data) override;
  void OnMainFrameMetadataChanged() override;
  void OnSubframeMetadataChanged(content::RenderFrameHost* rfh,
                                 const mojom::FrameMetadata& metadata) override;
  void OnSoftNavigationChanged(
      const mojom::SoftNavigationMetrics& soft_navigation_metrics) override;
  void UpdateFeaturesUsage(
      content::RenderFrameHost* rfh,
      const std::vector<blink::UseCounterFeature>& new_features) override;
  void UpdateResourceDataUse(
      content::RenderFrameHost* rfh,
      const std::vector<mojom::ResourceDataUpdatePtr>& resources) override;
  void UpdateFrameCpuTiming(content::RenderFrameHost* rfh,
                            const mojom::CpuTiming& timing) override;
  void OnMainFrameIntersectionRectChanged(
      content::RenderFrameHost* rfh,
      const gfx::Rect& main_frame_intersection_rect) override;
  void OnMainFrameViewportRectChanged(
      const gfx::Rect& main_frame_viewport_rect) override;
  void OnMainFrameImageAdRectsChanged(
      const base::flat_map<int, gfx::Rect>& main_frame_image_ad_rects) override;
  void SetUpSharedMemoryForUkms(
      base::ReadOnlySharedMemoryRegion smoothness_memory,
      base::ReadOnlySharedMemoryRegion dropped_frames_memory) override;

  // PageLoadMetricsObserverDelegate implementation:
  content::WebContents* GetWebContents() const override;
  base::TimeTicks GetNavigationStart() const override;
  std::optional<base::TimeDelta> GetTimeToFirstBackground() const override;
  std::optional<base::TimeDelta> GetTimeToFirstForeground() const override;
  PrerenderingState GetPrerenderingState() const override;
  std::optional<base::TimeDelta> GetActivationStart() const override;
  const BackForwardCacheRestore& GetBackForwardCacheRestore(
      size_t index) const override;
  bool StartedInForeground() const override;
  PageVisibility GetVisibilityAtActivation() const override;
  bool WasPrerenderedThenActivatedInForeground() const override;
  const UserInitiatedInfo& GetUserInitiatedInfo() const override;
  const GURL& GetUrl() const override;
  const GURL& GetStartUrl() const override;
  bool DidCommit() const override;
  PageEndReason GetPageEndReason() const override;
  const UserInitiatedInfo& GetPageEndUserInitiatedInfo() const override;
  std::optional<base::TimeDelta> GetTimeToPageEnd() const override;
  const base::TimeTicks& GetPageEndTime() const override;
  const mojom::FrameMetadata& GetMainFrameMetadata() const override;
  const mojom::FrameMetadata& GetSubframeMetadata() const override;
  const PageRenderData& GetPageRenderData() const override;
  const NormalizedCLSData& GetNormalizedCLSData(
      BfcacheStrategy bfcache_strategy) const override;
  const NormalizedCLSData& GetSoftNavigationIntervalNormalizedCLSData()
      const override;
  const ResponsivenessMetricsNormalization&
  GetResponsivenessMetricsNormalization() const override;
  const ResponsivenessMetricsNormalization&
  GetSoftNavigationIntervalResponsivenessMetricsNormalization() const override;
  const mojom::InputTiming& GetPageInputTiming() const override;
  const std::optional<blink::SubresourceLoadMetrics>&
  GetSubresourceLoadMetrics() const override;
  const PageRenderData& GetMainFrameRenderData() const override;
  const ui::ScopedVisibilityTracker& GetVisibilityTracker() const override;
  const ResourceTracker& GetResourceTracker() const override;
  const LargestContentfulPaintHandler& GetLargestContentfulPaintHandler()
      const override;
  const LargestContentfulPaintHandler&
  GetExperimentalLargestContentfulPaintHandler() const override;
  ukm::SourceId GetPageUkmSourceId() const override;
  mojom::SoftNavigationMetrics& GetSoftNavigationMetrics() const override;
  ukm::SourceId GetUkmSourceIdForSoftNavigation() const override;
  ukm::SourceId GetPreviousUkmSourceIdForSoftNavigation() const override;
  bool IsFirstNavigationInWebContents() const override;
  bool IsOriginVisit() const override;
  bool IsTerminalVisit() const override;
  bool ShouldObserveScheme(std::string_view scheme) const override;
  int64_t GetNavigationId() const override;

  // The following methods are called on navigation related events.
  //
  // Called only on main frames.
  void DidUpdateNavigationHandleTiming(
      content::NavigationHandle* navigation_handle);
  void Redirect(content::NavigationHandle* navigation_handle);
  void Commit(content::NavigationHandle* navigation_handle);
  void DidCommitSameDocumentNavigation(
      content::NavigationHandle* navigation_handle);
  void DidInternalNavigationAbort(content::NavigationHandle* navigation_handle);
  void FailedProvisionalLoad(content::NavigationHandle* navigation_handle,
                             base::TimeTicks failed_load_time);
  // Called only on subframes.
  void DidFinishSubFrameNavigation(
      content::NavigationHandle* navigation_handle);
  // Called on main and sub-frames.
  void ReadyToCommitNavigation(content::NavigationHandle* navigation_handle);
  void WillProcessNavigationResponse(
      content::NavigationHandle* navigation_handle);
  void PageHidden();
  void PageShown();
  void RenderFrameDeleted(content::RenderFrameHost* rfh);
  void FrameTreeNodeDeleted(content::FrameTreeNodeId frame_tree_node_id);

  void OnInputEvent(const blink::WebInputEvent& event);

  // Flush any buffered metrics, as part of the metrics subsystem persisting
  // metrics as the application goes into the background. The application may be
  // killed at any time after this method is invoked without further
  // notification.
  void FlushMetricsOnAppEnterBackground();

  // Replaces the |visibility_tracker_| for testing, which can mock a clock.
  void SetVisibilityTrackerForTesting(
      const ui::ScopedVisibilityTracker& tracker) {
    visibility_tracker_ = tracker;
  }

  void OnLoadedResource(
      const ExtraRequestCompleteInfo& extra_request_complete_info);

  void FrameReceivedUserActivation(content::RenderFrameHost* rfh);
  void FrameDisplayStateChanged(content::RenderFrameHost* render_frame_host,
                                bool is_display_none);
  void FrameSizeChanged(content::RenderFrameHost* render_frame_host,
                        const gfx::Size& frame_size);

  void OnCookiesRead(
      const GURL& url,
      const GURL& first_party_url,
      bool blocked_by_policy,
      bool is_ad_tagged,
      const net::CookieSettingOverrides& cookie_setting_overrides,
      bool is_partitioned_access);

  void OnCookieChange(
      const GURL& url,
      const GURL& first_party_url,
      const net::CanonicalCookie& cookie,
      bool blocked_by_policy,
      bool is_ad_tagged,
      const net::CookieSettingOverrides& cookie_setting_overrides,
      bool is_partitioned_access);

  void OnStorageAccessed(const GURL& url,
                         const GURL& first_party_url,
                         bool blocked_by_policy,
                         StorageType access_type);

  // Signals that we should stop tracking metrics for the associated page load.
  // We may stop tracking a page load if it doesn't meet the criteria for
  // tracking metrics in DidFinishNavigation.
  void StopTracking();

  void AddObserver(std::unique_ptr<PageLoadMetricsObserverInterface> observer);
  base::WeakPtr<PageLoadMetricsObserverInterface> FindObserver(
      char const* name);

  // If the user performs some abort-like action while we are tracking this page
  // load, notify the tracker. Note that we may not classify this as an abort if
  // we've already performed a first paint.
  // is_certainly_browser_timestamp signifies if the timestamp passed is taken
  // in the
  // browser process or not. We need this to possibly clamp browser timestamp on
  // a machine with inter process time tick skew.
  void NotifyPageEnd(PageEndReason page_end_reason,
                     UserInitiatedInfo user_initiated_info,
                     base::TimeTicks timestamp,
                     bool is_certainly_browser_timestamp);
  void UpdatePageEnd(PageEndReason page_end_reason,
                     UserInitiatedInfo user_initiated_info,
                     base::TimeTicks timestamp,
                     bool is_certainly_browser_timestamp);

  // This method returns true if this page load has been aborted with type of
  // END_OTHER, and the |abort_cause_time| is within a sufficiently close
  // delta to when it was aborted. Note that only provisional loads can be
  // aborted with END_OTHER. While this heuristic is coarse, it works better
  // and is simpler than other feasible methods. See https://goo.gl/WKRG98.
  bool IsLikelyProvisionalAbort(base::TimeTicks abort_cause_time) const;

  bool did_commit() const { return did_commit_; }
  const GURL& url() const { return url_; }

  base::TimeTicks navigation_start() const { return navigation_start_; }

  UserInitiatedInfo user_initiated_info() const { return user_initiated_info_; }

  PageLoadMetricsUpdateDispatcher* metrics_update_dispatcher() {
    return &metrics_update_dispatcher_;
  }

  // Whether this PageLoadTracker has a navigation GlobalRequestID that matches
  // the given request_id. This method will return false before
  // WillProcessNavigationResponse has been invoked, as PageLoadTracker doesn't
  // know its GlobalRequestID until WillProcessNavigationResponse has been
  // invoked.
  bool HasMatchingNavigationRequestID(
      const content::GlobalRequestID& request_id) const;

  // Invoked when a media element starts playing.
  void MediaStartedPlaying(
      const content::WebContentsObserver::MediaPlayerInfo& video_type,
      content::RenderFrameHost* render_frame_host);

  void OnPrefetchLikely();

  void OnEnterBackForwardCache();
  void OnRestoreFromBackForwardCache(
      content::NavigationHandle* navigation_handle);

  // Called when the page tracked was just activated after being prerendered.
  void DidActivatePrerenderedPage(content::NavigationHandle* navigation_handle);

  // Called when the previewed page was activated for the tab promotion.
  void DidActivatePreviewedPage(base::TimeTicks activation_time);

  // Called when V8 per-frame memory usage updates are available.
  void OnV8MemoryChanged(const std::vector<MemoryUpdate>& memory_updates);

  // Called when a `SharedStorageWorkletHost` is created.
  void OnSharedStorageWorkletHostCreated();

  // Called when `sharedStorage.selectURL()` is called for some frame on the
  // page tracked.
  void OnSharedStorageSelectURLCalled();

  // Called when a Fledge auction completes.
  void OnAdAuctionComplete(bool is_server_auction,
                           bool is_on_device_auction,
                           content::AuctionResult result);

  // Checks if this tracker is for outermost pages.
  bool IsOutermostTracker() const { return !parent_tracker_; }

  void UpdateMetrics(content::RenderFrameHost* render_frame_host,
                     mojom::PageLoadTimingPtr new_timing,
                     mojom::FrameMetadataPtr new_metadata,
                     const std::vector<blink::UseCounterFeature>& new_features,
                     const std::vector<mojom::ResourceDataUpdatePtr>& resources,
                     mojom::FrameRenderDataUpdatePtr render_data,
                     mojom::CpuTimingPtr new_cpu_timing,
                     mojom::InputTimingPtr input_timing_delta,
                     const std::optional<blink::SubresourceLoadMetrics>&
                         subresource_load_metrics,
                     mojom::SoftNavigationMetricsPtr soft_navigation_metrics);

  void AddCustomUserTimings(
      std::vector<mojom::CustomUserTimingMarkPtr> custom_timings);

  // Sets RenderFrameHost for the main frame of the page this tracker instance
  // is bound. This is called on moving the tracker to the active / inactive
  // tracker list after the provisional load is committed.
  void SetPageMainFrame(content::RenderFrameHost* rfh);

  // Records the fact link navigation from the tracking page happens.
  void RecordLinkNavigation();

  // Gets a bound ukm::SourceId without any check for testing.
  ukm::SourceId GetPageUkmSourceIdForTesting() const { return source_id_; }

  // Obtains a weak pointer for this instance.
  base::WeakPtr<PageLoadTracker> GetWeakPtr();

 private:
  // This function converts a TimeTicks value taken in the browser process
  // to navigation_start_ if:
  // - base::TimeTicks is not comparable across processes because the clock
  // is not system wide monotonic.
  // - *event_time < navigation_start_
  void ClampBrowserTimestampIfInterProcessTimeTickSkew(
      base::TimeTicks* event_time);

  void UpdatePageEndInternal(PageEndReason page_end_reason,
                             UserInitiatedInfo user_initiated_info,
                             base::TimeTicks timestamp,
                             bool is_certainly_browser_timestamp);

  // Given a |time|, returns the duration between |navigation_start_| and
  // |time|. |time| must be greater than or equal to |navigation_start_|.
  // Returns nullopt if and only if the |time| passed is nullopt.
  std::optional<base::TimeDelta> DurationSinceNavigationStartForTime(
      const std::optional<base::TimeTicks>& time) const;

  using InvokeCallback =
      base::RepeatingCallback<PageLoadMetricsObserver::ObservePolicy(
          PageLoadMetricsObserverInterface*)>;
  void InvokeAndPruneObservers(const char* trace_name,
                               InvokeCallback callback,
                               bool permit_forwarding);

  // Whether we stopped tracking this navigation after it was initiated. We may
  // stop tracking a navigation if it doesn't meet the criteria for tracking
  // metrics in DidFinishNavigation.
  bool did_stop_tracking_;

  int64_t navigation_id_;

  // The navigation start in TimeTicks, not the wall time reported by Blink.
  const base::TimeTicks navigation_start_;

  // The most recent URL of this page load. Updated at navigation start, upon
  // redirection, and at commit time.
  GURL url_;

  // The start URL for this page load (before redirects).
  GURL start_url_;

  ui::ScopedVisibilityTracker visibility_tracker_;

  // Whether this page load committed.
  bool did_commit_;

  std::unique_ptr<FailedProvisionalLoadInfo> failed_provisional_load_info_;

  // Will be END_NONE if we have not ended this load yet. Otherwise will
  // be the first page end reason encountered.
  PageEndReason page_end_reason_;

  // Whether the page end cause for this page load was user initiated. For
  // example, if this page load was ended by a new navigation, this field tracks
  // whether that new navigation was user-initiated. This field is only useful
  // if this page load's end reason is a value other than END_NONE. Note that
  // this value is currently experimental, and is subject to change. In
  // particular, this field is never set to true for some page end reasons, such
  // as stop and close, since we don't yet have sufficient instrumentation to
  // know if a stop or close was caused by a user action.
  UserInitiatedInfo page_end_user_initiated_info_;

  base::TimeTicks page_end_time_;

  // We record separate metrics for events that occur after a background,
  // because metrics like layout/paint are delayed artificially
  // when they occur in the background.
  std::optional<base::TimeTicks> first_background_time_;
  std::optional<base::TimeTicks> first_foreground_time_;
  std::vector<BackForwardCacheRestore> back_forward_cache_restores_;
  const bool started_in_foreground_;
  PrerenderingState prerendering_state_ = PrerenderingState::kNoPrerendering;
  // Holds the page's visibility at activation.
  PageVisibility visibility_at_activation_ = PageVisibility::kNotInitialized;
  std::optional<base::TimeDelta> activation_start_ = std::nullopt;

  mojom::PageLoadTimingPtr last_dispatched_merged_page_timing_;

  std::optional<content::GlobalRequestID> navigation_request_id_;

  // Whether this page load was user initiated.
  UserInitiatedInfo user_initiated_info_;

  // Keeps track of actively loading resources on the page.
  ResourceTracker resource_tracker_;

  // Interface to chrome features. Must outlive the class.
  const raw_ptr<PageLoadMetricsEmbedderInterface> embedder_interface_;

  // Holds active PageLoadMetricsObserverInterface inheritances' instances bound
  // to the tracking page.
  std::vector<std::unique_ptr<PageLoadMetricsObserverInterface>> observers_;

  // Observer's name pointer to instance map. Can be raw_ptr as the instance is
  // owned `observers` above, and is removed from the map on destruction.
  base::flat_map<const char*, raw_ptr<PageLoadMetricsObserverInterface>>
      observers_map_;

  PageLoadMetricsUpdateDispatcher metrics_update_dispatcher_;

  ukm::SourceId source_id_;

  const raw_ptr<content::WebContents> web_contents_;

  // ID of the RenderFrameHost for the main frame of the page that this tracker
  // instance is bound.
  content::GlobalRenderFrameHostId page_main_frame_id_;

  const bool is_first_navigation_in_web_contents_;
  const bool is_origin_visit_;
  bool is_terminal_visit_ = true;

  page_load_metrics::LargestContentfulPaintHandler
      largest_contentful_paint_handler_;
  page_load_metrics::LargestContentfulPaintHandler
      experimental_largest_contentful_paint_handler_;

  mojom::SoftNavigationMetricsPtr soft_navigation_metrics_;

  GURL potential_soft_navigation_url_;

  ukm::SourceId potential_soft_navigation_source_id_ = ukm::kInvalidSourceId;
  ukm::SourceId previous_soft_navigation_source_id_ = ukm::kInvalidSourceId;

  const internal::PageLoadTrackerPageType page_type_;

  const base::WeakPtr<PageLoadTracker> parent_tracker_;

  base::WeakPtrFactory<PageLoadTracker> weak_factory_{this};
};

}  // namespace page_load_metrics

#endif  // COMPONENTS_PAGE_LOAD_METRICS_BROWSER_PAGE_LOAD_TRACKER_H_