File: btm_short_visit_observer.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 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 (91 lines) | stat: -rw-r--r-- 3,600 bytes parent folder | download | duplicates (5)
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_BTM_BTM_SHORT_VISIT_OBSERVER_H_
#define CONTENT_BROWSER_BTM_BTM_SHORT_VISIT_OBSERVER_H_

#include <optional>
#include <string>

#include "base/containers/unique_ptr_adapters.h"
#include "base/memory/raw_ref.h"
#include "base/memory/weak_ptr.h"
#include "base/time/time.h"
#include "content/common/content_export.h"
#include "content/public/browser/web_contents_observer.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "ui/base/page_transition_types.h"

namespace base {
class Clock;
}

namespace content {

class BtmState;

// Emits UKM metrics for pages visited only for a short time.
class CONTENT_EXPORT BtmShortVisitObserver
    : public content::WebContentsObserver {
 public:
  // See the definition in the .cc file for a description.
  class AsyncMetricsState;

  explicit BtmShortVisitObserver(content::WebContents* web_contents);
  ~BtmShortVisitObserver() override;

  static const base::Clock* SetDefaultClockForTesting(const base::Clock* clock);

  // WebContentsObserver overrides:
  void DidStartNavigation(
      content::NavigationHandle* navigation_handle) override;
  void DidFinishNavigation(
      content::NavigationHandle* navigation_handle) override;
  void DidGetUserInteraction(const blink::WebInputEvent& event) override;
  void OnCookiesAccessed(RenderFrameHost* render_frame_host,
                         const CookieAccessDetails& details) override;
  void OnCookiesAccessed(NavigationHandle* navigation_handle,
                         const CookieAccessDetails& details) override;
  void NotifyStorageAccessed(RenderFrameHost* render_frame_host,
                             blink::mojom::StorageTypeAccessed storage_type,
                             bool blocked) override;

 private:
  // Called with the result of reading the BTM state for a page.
  void StoreLastInteraction(base::WeakPtr<AsyncMetricsState> metrics_state,
                            const BtmState& btm_state);

  // For determining the length of page visits; can be overridden in tests.
  const raw_ref<const base::Clock> clock_;
  // The eTLD+1 of the page visited before the currently-committed page.
  std::optional<std::string> prev_site_;
  // The UKM source id of the page visited before the current page.
  ukm::SourceId prev_source_id_ = ukm::kInvalidSourceId;
  // Several properties of the navigation that led to the current page.
  struct {
    bool was_renderer_initiated = false;
    bool had_user_gesture = false;
    ui::PageTransition page_transition = ui::PAGE_TRANSITION_LINK;
  } last_navigation;
  // Whether the current page has received a keydown event.
  bool had_keydown_event_ = false;
  // Whether the current page has accessed storage (cookies, localStorage, etc).
  bool page_accessed_storage_ = false;
  // The time the current page committed.
  base::Time last_committed_at_;
  // The source ID of the current page -- used in DidFinishNavigation() to emit
  // an event for the previous page.
  ukm::SourceId page_source_id_;
  // Metrics state for the current page.
  std::unique_ptr<AsyncMetricsState> current_page_metrics_;
  // Metrics state for previously-visited pages that we're still waiting for BTM
  // state for.
  std::set<std::unique_ptr<AsyncMetricsState>, base::UniquePtrComparator>
      pending_metrics_;
  base::WeakPtrFactory<BtmShortVisitObserver> weak_factory_{this};
};

}  // namespace content

#endif  // CONTENT_BROWSER_BTM_BTM_SHORT_VISIT_OBSERVER_H_