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
|
// 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_BACKEND_H_
#define COMPONENTS_SEGMENTATION_PLATFORM_INTERNAL_DATABASE_UKM_DATABASE_BACKEND_H_
#include <cstdint>
#include <memory>
#include <vector>
#include "base/containers/flat_map.h"
#include "base/files/file_path.h"
#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/task/sequenced_task_runner.h"
#include "components/segmentation_platform/internal/database/ukm_database.h"
#include "components/segmentation_platform/internal/database/ukm_metrics_table.h"
#include "components/segmentation_platform/internal/database/ukm_types.h"
#include "components/segmentation_platform/internal/database/ukm_url_table.h"
#include "components/segmentation_platform/internal/database/uma_metrics_table.h"
#include "services/metrics/public/cpp/ukm_source_id.h"
#include "services/metrics/public/mojom/ukm_interface.mojom.h"
#include "sql/database.h"
#include "sql/transaction.h"
#include "url/gurl.h"
namespace segmentation_platform {
// Database backend class that handles various tables in the SQL database. Runs
// in database task runner.
class UkmDatabaseBackend : public UkmDatabase {
public:
UkmDatabaseBackend(
const base::FilePath& database_path,
bool in_memory,
scoped_refptr<base::SequencedTaskRunner> callback_task_runner);
~UkmDatabaseBackend() override;
UkmDatabaseBackend(const UkmDatabaseBackend&) = delete;
UkmDatabaseBackend& operator=(const UkmDatabaseBackend&) = delete;
// UkmDatabase implementation. All callbacks will be posted to
// |callback_task_runner|.
void InitDatabase(SuccessCallback callback) override;
void StoreUkmEntry(ukm::mojom::UkmEntryPtr ukm_entry) override;
void UpdateUrlForUkmSource(ukm::SourceId source_id,
const GURL& url,
bool is_validated,
const std::string& profile_id) override;
void OnUrlValidated(const GURL& url, const std::string& profile_id) override;
void RemoveUrls(const std::vector<GURL>& urls, bool all_urls) override;
void AddUmaMetric(const std::string& profile_id,
const UmaMetricEntry& row) override;
void RunReadOnlyQueries(QueryList&& queries, QueryCallback callback) override;
void DeleteEntriesOlderThan(base::Time time) override;
void CleanupItems(const std::string& profile_id,
std::vector<CleanupItem> cleanup_items) override;
void CommitTransactionForTesting() override;
sql::Database& db() { return db_; }
UkmUrlTable& url_table_for_testing() {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return url_table_;
}
bool has_transaction_for_testing() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return !!current_transaction_;
}
void RollbackTransactionForTesting();
base::WeakPtr<UkmDatabaseBackend> GetWeakPtr() {
return weak_factory_.GetWeakPtr();
}
private:
// Helper to delete all URLs from database.
void DeleteAllUrls();
// Tracks changes in the current transaction and commits when over a limit.
void TrackChangesInTransaction(int change_count);
// Commit current transaction and begin a new one.
void RestartTransaction();
const base::FilePath database_path_;
const bool in_memory_;
scoped_refptr<base::SequencedTaskRunner> callback_task_runner_
GUARDED_BY_CONTEXT(sequence_checker_);
sql::Database db_ GUARDED_BY_CONTEXT(sequence_checker_);
int change_count_ GUARDED_BY_CONTEXT(sequence_checker_){0};
std::unique_ptr<sql::Transaction> current_transaction_
GUARDED_BY_CONTEXT(sequence_checker_);
UkmMetricsTable metrics_table_ GUARDED_BY_CONTEXT(sequence_checker_);
UkmUrlTable url_table_ GUARDED_BY_CONTEXT(sequence_checker_);
UmaMetricsTable uma_metrics_table_ GUARDED_BY_CONTEXT(sequence_checker_);
enum class Status { CREATED, INIT_FAILED, INIT_SUCCESS };
Status status_ = Status::CREATED;
// Map from source ID to URL. When URL updates are sent before metrics, this
// map is used to set URL ID to the metrics rows. This is an in-memory cache
// which lives during the current session. UKM does not reuse source ID across
// sessions.
// TODO(ssid): This map should be cleaned and not grow forever.
base::flat_map<ukm::SourceId, UrlId> source_to_url_
GUARDED_BY_CONTEXT(sequence_checker_);
// List of URL IDs that are needed by UKM metrics, but are not yet validated.
// Used to verify that URLs are needed in the UKM database when a
// OnUrlValidated() is called. The map is kept around only for the current
// session, so we might miss validation calls that happen across sessions.
// But, usually history updates within the session.
base::flat_set<UrlId> urls_not_validated_
GUARDED_BY_CONTEXT(sequence_checker_);
SEQUENCE_CHECKER(sequence_checker_);
base::WeakPtrFactory<UkmDatabaseBackend> weak_factory_{this};
};
} // namespace segmentation_platform
#endif // COMPONENTS_SEGMENTATION_PLATFORM_INTERNAL_DATABASE_UKM_DATABASE_BACKEND_H_
|