File: ukm_database.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 (112 lines) | stat: -rw-r--r-- 4,840 bytes parent folder | download | duplicates (6)
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
// 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_DATABASE_UKM_DATABASE_H_
#define COMPONENTS_SEGMENTATION_PLATFORM_INTERNAL_DATABASE_UKM_DATABASE_H_

#include <cstdint>
#include <memory>
#include <string_view>
#include <vector>

#include "base/functional/callback.h"
#include "base/time/time.h"
#include "components/segmentation_platform/internal/database/ukm_types.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "services/metrics/public/mojom/ukm_interface.mojom.h"
#include "url/gurl.h"

namespace segmentation_platform {

// UKM database is a single instance for each browser process. This class will
// be used for the storage and query of UKM data, for all the segmentation
// platform service(s).
class UkmDatabase {
 public:
  UkmDatabase() = default;
  virtual ~UkmDatabase() = default;

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

  using SuccessCallback = base::OnceCallback<void(bool)>;

  // Initialize the database.
  virtual void InitDatabase(SuccessCallback callback) = 0;

  // Called once when an UKM event is added. All metrics from the event will be
  // added to the metrics table.
  virtual void StoreUkmEntry(ukm::mojom::UkmEntryPtr ukm_entry) = 0;

  // Called when URL for a source ID is updated. Only validated URLs will be
  // written to the URL table. The URL can be validated either by setting
  // |is_validated| to true when calling this method or by calling
  // OnUrlValidated() at a later point in time. Note that this method needs to
  // be called even when |is_validated| is false so that the database is able to
  // index metrics with the same |source_id| with the URL.
  virtual void UpdateUrlForUkmSource(ukm::SourceId source_id,
                                     const GURL& url,
                                     bool is_validated,
                                     const std::string& profile_id) = 0;

  // Called to validate an URL, see also UpdateUrlForUkmSource(). Safe to call
  // with unneeded URLs, since the database will only persist the URLs already
  // pending for known |source_id|s. Note that this call will not automatically
  // validate future URLs given by UpdateUrlForUkmSource(). They need to have
  // |is_validated| set to be persisted.
  virtual void OnUrlValidated(const GURL& url,
                              const std::string& profile_id) = 0;

  // Removes all the URLs from URL table and all the associated metrics in
  // metrics table, on best effort. Any new metrics added with the URL will
  // still be stored in metrics table (without URLs). If `all_urls` is true,
  // then clears all the URLs without using `urls` list. It is an optimization
  // to clear all the URLs quickly.
  virtual void RemoveUrls(const std::vector<GURL>& urls, bool all_urls) = 0;

  // Called once when a new UMA metric is to be recorded in the database.
  virtual void AddUmaMetric(const std::string& profile_id,
                            const UmaMetricEntry& row) = 0;

  // Struct responsible for storing a sql query and its bind values.
  struct CustomSqlQuery {
    CustomSqlQuery();
    CustomSqlQuery(CustomSqlQuery&&);
    CustomSqlQuery(std::string_view query,
                   const std::vector<processing::ProcessedValue>& bind_values);
    ~CustomSqlQuery();

    bool operator==(const CustomSqlQuery& rhs) const {
      return query == rhs.query && bind_values == rhs.bind_values;
    }
    CustomSqlQuery& operator=(CustomSqlQuery&&) = default;

    std::string query;
    std::vector<processing::ProcessedValue> bind_values;
  };

  using QueryList = base::flat_map<processing::FeatureIndex, CustomSqlQuery>;
  using QueryCallback =
      base::OnceCallback<void(bool success, processing::IndexedTensors)>;

  // Called to query data from the ukm database. The result is returned in the
  // |callback| as a mapping of indexed vectors of processing::ProcessedValue.
  virtual void RunReadOnlyQueries(QueryList&& queries,
                                  QueryCallback callback) = 0;

  // Removes metrics older than or equal to the given `time` from the database.
  // URLs are removed when there are no references to the metrics.
  virtual void DeleteEntriesOlderThan(base::Time time) = 0;

  // Cleans up old items from the database. Only cleans up UMA entries. UKM
  // entries still uses `DeleteEntriesOlderThan()` instead.
  virtual void CleanupItems(const std::string& profile_id,
                            std::vector<CleanupItem> cleanup_items) = 0;

  virtual void CommitTransactionForTesting() = 0;
};

}  // namespace segmentation_platform

#endif  // COMPONENTS_SEGMENTATION_PLATFORM_INTERNAL_DATABASE_UKM_DATABASE_H_