File: url_signal_handler.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 (121 lines) | stat: -rw-r--r-- 4,470 bytes parent folder | download | duplicates (10)
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
// Copyright 2022 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_SEGMENTATION_PLATFORM_INTERNAL_SIGNALS_URL_SIGNAL_HANDLER_H_
#define COMPONENTS_SEGMENTATION_PLATFORM_INTERNAL_SIGNALS_URL_SIGNAL_HANDLER_H_

#include "base/containers/flat_set.h"

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation_traits.h"
#include "base/sequence_checker.h"
#include "services/metrics/public/cpp/ukm_source_id.h"

class GURL;

namespace segmentation_platform {

class UkmDatabase;

// The URL signals from various sources will go through this signal handler
// before being written to the database.
class UrlSignalHandler {
 public:
  using FindCallback = base::OnceCallback<void(bool, const std::string&)>;

  // Delegate class, usually one per each HistoryService, used for checking if
  // an URL is part of the history database on-demand.
  class HistoryDelegate {
   public:
    // Fast check in local cache for the given URL. This method is optional,
    // only used for optimization. Always return false is a valid
    // implementation.
    virtual bool FastCheckUrl(const GURL& url);
    // Query the history database to check if the |url| is part of the database.
    virtual void FindUrlInHistory(const GURL& url, FindCallback callback) = 0;
    // Getters for getting profile id.
    virtual const std::string& profile_id() = 0;

    virtual ~HistoryDelegate() = default;
  };

  explicit UrlSignalHandler(UkmDatabase* ukm_database);
  ~UrlSignalHandler();

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

  // Called by UKM observer when source URL for the |source_id| is updated.
  void OnUkmSourceUpdated(ukm::SourceId source_id,
                          const std::vector<GURL>& urls);

  // Called by history service when a visit is added for the |url|. This
  // notification should mean that the |url| will be stored in history database
  // URL table until it is removed by OnUrlsRemovedFromHistory().
  void OnHistoryVisit(const GURL& url, const std::string& profile_id);

  // Called when |urls| are removed from the history database.
  void OnUrlsRemovedFromHistory(const std::vector<GURL>& urls, bool all_urls);

  // Add/Remove history delegates.
  void AddHistoryDelegate(HistoryDelegate* history_delegate);
  void RemoveHistoryDelegate(HistoryDelegate* history_delegate);

 private:
  // Checks each history delegate if the |url| is in the database.
  void CheckHistoryForUrl(const GURL& url, FindCallback callback);

  // Used as a callback to get result from the delegate. |delegates_checked| is
  // used to store the list of delegates already checked, to continue checking
  // remaining delegates.
  void ContinueCheckingHistory(
      const GURL& url,
      std::unique_ptr<base::flat_set<HistoryDelegate*>> delegates_checked,
      FindCallback callback,
      bool found,
      const std::string& profile_id);

  // Called when finished checking all the history delegates.
  void OnCheckedHistory(ukm::SourceId source_id,
                        const GURL& url,
                        bool in_history,
                        const std::string& profile_id);

  raw_ptr<UkmDatabase> ukm_database_;

  base::flat_set<raw_ptr<HistoryDelegate>> history_delegates_;
  // When true, the handler only does a fast check and skips the database
  // checks. It is possible to miss URLs in UKM database if this was true.
  bool skip_history_db_check_ = false;

  SEQUENCE_CHECKER(sequence_checker_);

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

}  // namespace segmentation_platform

namespace base {

template <>
struct ScopedObservationTraits<
    segmentation_platform::UrlSignalHandler,
    segmentation_platform::UrlSignalHandler::HistoryDelegate> {
  static void AddObserver(
      segmentation_platform::UrlSignalHandler* source,
      segmentation_platform::UrlSignalHandler::HistoryDelegate* observer) {
    source->AddHistoryDelegate(observer);
  }
  static void RemoveObserver(
      segmentation_platform::UrlSignalHandler* source,
      segmentation_platform::UrlSignalHandler::HistoryDelegate* observer) {
    source->RemoveHistoryDelegate(observer);
  }
};

}  // namespace base

#endif  // COMPONENTS_SEGMENTATION_PLATFORM_INTERNAL_SIGNALS_URL_SIGNAL_HANDLER_H_