File: from_gws_navigation_and_keep_alive_request_tracker.h

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (116 lines) | stat: -rw-r--r-- 4,461 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
// 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 CHROME_BROWSER_LOADER_FROM_GWS_NAVIGATION_AND_KEEP_ALIVE_REQUEST_TRACKER_H_
#define CHROME_BROWSER_LOADER_FROM_GWS_NAVIGATION_AND_KEEP_ALIVE_REQUEST_TRACKER_H_

#include <stdint.h>

#include <map>
#include <optional>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/timer/timer.h"
#include "base/unguessable_token.h"
#include "components/keyed_service/core/keyed_service.h"
#include "content/public/browser/global_routing_id.h"
#include "services/metrics/public/cpp/ukm_source_id.h"

namespace content {
class BrowserContext;
}  // namespace content

// `FromGWSNavigationAndKeepAliveRequestTracker` is a per browser context
// service that tracks navigations and fetch keepalive requests made from Google
// search result pages (SRP).
class FromGWSNavigationAndKeepAliveRequestTracker : public KeyedService {
 public:
  // DO NOT use this constructor directly. Use
  // `FromGWSNavigationAndKeepAliveRequestTrackerFactory::GetTracker` instead.
  //
  // `browser_context` is the browser context that owns `this` via
  // `KeyedService`.
  explicit FromGWSNavigationAndKeepAliveRequestTracker(
      content::BrowserContext* browser_context);
  ~FromGWSNavigationAndKeepAliveRequestTracker() override;

  // Not copyable or movable.
  FromGWSNavigationAndKeepAliveRequestTracker(
      const FromGWSNavigationAndKeepAliveRequestTracker&) = delete;
  FromGWSNavigationAndKeepAliveRequestTracker& operator=(
      const FromGWSNavigationAndKeepAliveRequestTracker&) = delete;

  // Tracks a navigation.
  //
  // `initator_rfh_id` is the ID of the RenderFrameHost that initiates the
  // navigation.
  // `category_id` is the category ID extracted from the navigation URL.
  // `ukm_source_id` is the UKM source ID of the page that initiates the
  // navigation.
  // `navigation_id` is the ID of the navigation.
  virtual void TrackNavigation(content::GlobalRenderFrameHostId initator_rfh_id,
                               int64_t category_id,
                               ukm::SourceId ukm_source_id,
                               int64_t navigation_id);

  // Tracks a keepalive request.
  //
  // `initator_rfh_id` is the ID of the RenderFrameHost that initiates the
  // request.
  // `category_id` is the category ID extracted from the request URL.
  // `ukm_source_id` is the UKM source ID of the page that initiates the
  // request.
  // `keepalive_token` is the token of the fetch keepalive request.
  virtual void TrackKeepAliveRequest(
      content::GlobalRenderFrameHostId initator_rfh_id,
      int64_t category_id,
      ukm::SourceId ukm_source_id,
      base::UnguessableToken keepalive_token);

 private:
  // Represents a category ID within a RenderFrameHost.
  struct FrameCategory {
    content::GlobalRenderFrameHostId initator_rfh_id;
    int64_t category_id;
    ukm::SourceId ukm_source_id;

    bool operator<(const FrameCategory& rhs) const;
    bool operator==(const FrameCategory& rhs) const;
  };

  // Represents a navigation and a keepalive request made from the same page.
  // Note that both of them must have the same category ID in their URLs.
  struct NavigationAndKeepAliveRequest {
    std::optional<int64_t> navigation_id = std::nullopt;
    std::optional<base::UnguessableToken> keepalive_token = std::nullopt;
  };

  void LogUkmEvent(const FrameCategory& frame_category,
                   const NavigationAndKeepAliveRequest& request);

  // Starts `cleanup_timer_` if it's not running yet.
  void StartCleanupTimer();
  // A callback to clean up unused mappings.
  void OnCleanupTimeout();

  // The `BrowserContext` that owns `this` via `KeyedService`.
  raw_ptr<content::BrowserContext> browser_context_;

  // A map from a `FrameCategory` to a list of `NavigationAndKeepAliveRequest`s
  // made from the same page.
  std::map<FrameCategory, std::vector<NavigationAndKeepAliveRequest>>
      per_frame_category_navigation_and_keepalive_requests_;

  // A timer to clean up `per_frame_category_navigation_and_keepalive_requests_`
  // periodically.
  base::RepeatingTimer cleanup_timer_;

  // Must be the last field.
  base::WeakPtrFactory<FromGWSNavigationAndKeepAliveRequestTracker>
      weak_ptr_factory_{this};
};

#endif  // CHROME_BROWSER_LOADER_FROM_GWS_NAVIGATION_AND_KEEP_ALIVE_REQUEST_TRACKER_H_