File: ukm_url_table.cc

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 (128 lines) | stat: -rw-r--r-- 4,377 bytes parent folder | download | duplicates (8)
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
// 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.

#include "components/segmentation_platform/internal/database/ukm_url_table.h"

#include <utility>

#include "base/containers/span.h"
#include "base/hash/md5.h"
#include "base/logging.h"
#include "base/numerics/byte_conversions.h"
#include "components/database_utils/url_converter.h"
#include "sql/database.h"
#include "sql/statement.h"

namespace segmentation_platform {

UkmUrlTable::UkmUrlTable(sql::Database* db) : db_(db) {
  DETACH_FROM_SEQUENCE(sequence_checker_);
  DCHECK(db_);
}

UkmUrlTable::~UkmUrlTable() = default;

// static
std::string UkmUrlTable::GetDatabaseUrlString(const GURL& url) {
  return database_utils::GurlToDatabaseUrl(url);
}

// static
UrlId UkmUrlTable::GenerateUrlId(const GURL& url) {
  // Converts the 8-byte prefix of an MD5 hash into a int64_t value. This
  // hashing scheme is architecture dependent.
  std::string db_url = GetDatabaseUrlString(url);
  base::MD5Digest digest;
  base::MD5Sum(base::as_byte_span(db_url), &digest);
  return UrlId::FromUnsafeValue(
      base::I64FromLittleEndian(base::span(digest.a).first<8u>()));
}

bool UkmUrlTable::InitTable() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (db_->DoesTableExist(kTableName)) {
    if (!db_->DoesColumnExist(kTableName, "profile_id")) {
      // Old versions don't have the profile_id column, we modify the table to
      // add that field.
      return db_->Execute(
          "ALTER TABLE urls "
          "ADD COLUMN profile_id TEXT");
    }
    return true;
  }

  static constexpr char kCreateTableQuery[] =
      // clang-format off
      "CREATE TABLE urls("
        "url_id INTEGER PRIMARY KEY NOT NULL,"
        "url TEXT NOT NULL,"
        "last_timestamp INTEGER NOT NULL,"
        "counter INTEGER,"
        "title TEXT,"
        "profile_id TEXT)";
  // clang-format on
  return db_->Execute(kCreateTableQuery);
}

bool UkmUrlTable::IsUrlInTable(UrlId url_id) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  static constexpr char kGetUrlQuery[] = "SELECT 1 FROM urls WHERE url_id=?";
  sql::Statement statement(
      db_->GetCachedStatement(SQL_FROM_HERE, kGetUrlQuery));
  statement.BindInt64(0, url_id.GetUnsafeValue());
  return statement.Step();
}

bool UkmUrlTable::WriteUrl(const GURL& url,
                           UrlId url_id,
                           base::Time timestamp,
                           const std::string& profile_id) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  static constexpr char kWriteQuery[] =
      "INSERT INTO urls(url_id,url,last_timestamp, profile_id) VALUES(?,?,?,?)";
  sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kWriteQuery));
  statement.BindInt64(0, url_id.GetUnsafeValue());
  statement.BindString(1, database_utils::GurlToDatabaseUrl(url));
  statement.BindTime(2, timestamp);
  statement.BindString(3, profile_id);
  return statement.Run();
}

bool UkmUrlTable::UpdateUrlTimestamp(UrlId url_id, base::Time timestamp) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  static constexpr char kWriteQuery[] =
      "UPDATE urls SET last_timestamp=? WHERE url_id=?";
  sql::Statement statement(db_->GetCachedStatement(SQL_FROM_HERE, kWriteQuery));
  statement.BindTime(0, timestamp);
  statement.BindInt64(1, url_id.GetUnsafeValue());
  return statement.Run();
}

bool UkmUrlTable::RemoveUrls(const std::vector<UrlId>& urls) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  static constexpr char kDeleteQuery[] = "DELETE FROM urls WHERE url_id=?";
  sql::Statement statement(
      db_->GetCachedStatement(SQL_FROM_HERE, kDeleteQuery));
  bool success = true;
  for (UrlId url_id : urls) {
    statement.Reset(/*clear_bound_vars=*/true);
    statement.BindInt64(0, url_id.GetUnsafeValue());
    if (!statement.Run())
      success = false;
  }
  return success;
}

bool UkmUrlTable::DeleteUrlsBeforeTimestamp(base::Time time) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  // Delete the metrics.
  static constexpr char kDeleteoldEntries[] =
      "DELETE FROM urls WHERE last_timestamp<=?";
  sql::Statement statement(
      db_->GetCachedStatement(SQL_FROM_HERE, kDeleteoldEntries));
  statement.BindTime(0, time);
  return statement.Run();
}

}  // namespace segmentation_platform