#include "storage/browser/quota/quota_database.h"
#include <stddef.h>
#include <stdint.h>
#include <memory>
#include <tuple>
#include <vector>
#include "base/auto_reset.h"
#include "base/containers/contains.h"
#include "base/dcheck_is_on.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/metrics/histogram_functions.h"
#include "base/sequence_checker.h"
#include "base/time/clock.h"
#include "components/services/storage/public/cpp/buckets/constants.h"
#include "components/services/storage/public/cpp/quota_error_or.h"
#include "sql/database.h"
#include "sql/error_delegate_util.h"
#include "sql/meta_table.h"
#include "sql/recovery.h"
#include "sql/statement.h"
#include "sql/transaction.h"
#include "storage/browser/quota/quota_database_migrations.h"
#include "storage/browser/quota/quota_features.h"
#include "storage/browser/quota/quota_internals.mojom.h"
#include "storage/browser/quota/special_storage_policy.h"
#include "url/gurl.h"
StorageKey;
StorageType;
namespace storage {
namespace {
static const int kDaysInTenYears = …;
const int kQuotaDatabaseCurrentSchemaVersion = …;
const int kQuotaDatabaseCompatibleVersion = …;
const char kBucketTable[] = …;
const char kBucketsTableBootstrapped[] = …;
const int kCommitIntervalMs = …;
base::Clock* g_clock_for_testing = …;
void RecordDatabaseResetHistogram(const DatabaseResetReason reason) { … }
#define BUCKETS_FIELDS_INSERTER …
void BindBucketInitParamsToInsertStatement(const BucketInitParams& params,
StorageType type,
int use_count,
const base::Time& last_accessed,
const base::Time& last_modified,
sql::Statement& statement) { … }
#define BUCKET_TABLE_ENTRY_FIELDS_SELECTOR …
mojom::BucketTableEntryPtr BucketTableEntryFromSqlStatement(
sql::Statement& statement) { … }
#define BUCKET_INFO_FIELDS_SELECTOR …
QuotaErrorOr<BucketInfo> BucketInfoFromSqlStatement(sql::Statement& statement) { … }
std::set<BucketInfo> BucketInfosFromSqlStatement(sql::Statement& statement) { … }
}
const QuotaDatabase::TableSchema QuotaDatabase::kTables[] = …;
const size_t QuotaDatabase::kTableCount = …;
const QuotaDatabase::IndexSchema QuotaDatabase::kIndexes[] = …;
const size_t QuotaDatabase::kIndexCount = …;
QuotaDatabase::QuotaDatabase(const base::FilePath& profile_path)
: … { … }
QuotaDatabase::~QuotaDatabase() { … }
constexpr char QuotaDatabase::kDatabaseName[];
QuotaErrorOr<BucketInfo> QuotaDatabase::UpdateOrCreateBucket(
const BucketInitParams& params,
int max_bucket_count) { … }
QuotaErrorOr<BucketInfo> QuotaDatabase::GetOrCreateBucketDeprecated(
const BucketInitParams& params,
StorageType type) { … }
QuotaErrorOr<BucketInfo> QuotaDatabase::CreateBucketForTesting(
const StorageKey& storage_key,
const std::string& bucket_name,
StorageType storage_type) { … }
QuotaErrorOr<BucketInfo> QuotaDatabase::GetBucket(
const StorageKey& storage_key,
const std::string& bucket_name,
blink::mojom::StorageType storage_type) { … }
QuotaErrorOr<BucketInfo> QuotaDatabase::UpdateBucketExpiration(
BucketId bucket,
const base::Time& expiration) { … }
QuotaErrorOr<BucketInfo> QuotaDatabase::UpdateBucketPersistence(
BucketId bucket,
bool persistent) { … }
QuotaErrorOr<BucketInfo> QuotaDatabase::GetBucketById(BucketId bucket_id) { … }
QuotaErrorOr<std::set<BucketInfo>> QuotaDatabase::GetBucketsForType(
StorageType type) { … }
QuotaErrorOr<std::set<BucketInfo>> QuotaDatabase::GetBucketsForHost(
const std::string& host,
StorageType type) { … }
QuotaErrorOr<std::set<BucketInfo>> QuotaDatabase::GetBucketsForStorageKey(
const StorageKey& storage_key,
StorageType type) { … }
QuotaError QuotaDatabase::SetStorageKeyLastAccessTime(
const StorageKey& storage_key,
StorageType type,
base::Time last_accessed) { … }
QuotaError QuotaDatabase::SetBucketLastAccessTime(BucketId bucket_id,
base::Time last_accessed) { … }
QuotaError QuotaDatabase::SetBucketLastModifiedTime(BucketId bucket_id,
base::Time last_modified) { … }
QuotaError QuotaDatabase::RegisterInitialStorageKeyInfo(
base::flat_map<StorageType, std::set<StorageKey>> storage_keys_by_type) { … }
QuotaErrorOr<mojom::BucketTableEntryPtr> QuotaDatabase::GetBucketInfoForTest(
BucketId bucket_id) { … }
QuotaErrorOr<mojom::BucketTableEntryPtr> QuotaDatabase::DeleteBucketData(
const BucketLocator& bucket) { … }
QuotaErrorOr<std::set<BucketLocator>> QuotaDatabase::GetBucketsForEviction(
StorageType type,
int64_t target_usage,
const std::map<BucketLocator, int64_t>& usage_map,
const std::set<BucketId>& bucket_exceptions,
SpecialStoragePolicy* special_storage_policy) { … }
QuotaErrorOr<std::set<StorageKey>> QuotaDatabase::GetStorageKeysForType(
StorageType type) { … }
QuotaErrorOr<std::set<BucketLocator>> QuotaDatabase::GetBucketsModifiedBetween(
StorageType type,
base::Time begin,
base::Time end) { … }
QuotaErrorOr<std::set<BucketInfo>> QuotaDatabase::GetExpiredBuckets(
SpecialStoragePolicy* special_storage_policy) { … }
bool QuotaDatabase::IsBootstrapped() { … }
QuotaError QuotaDatabase::SetIsBootstrapped(bool bootstrap_flag) { … }
bool QuotaDatabase::RecoverOrRaze(int error_code) { … }
QuotaError QuotaDatabase::CorruptForTesting(
base::OnceCallback<void(const base::FilePath&)> corrupter) { … }
void QuotaDatabase::SetDisabledForTesting(bool disable) { … }
base::Time QuotaDatabase::GetNow() { … }
void QuotaDatabase::SetClockForTesting(base::Clock* clock) { … }
void QuotaDatabase::SetAlreadyEvictedStaleStorageForTesting(
bool already_evicted_stale_storage) { … }
void QuotaDatabase::CommitNow() { … }
void QuotaDatabase::Commit() { … }
void QuotaDatabase::ScheduleCommit() { … }
QuotaError QuotaDatabase::EnsureOpened() { … }
void QuotaDatabase::OnSqliteError(int sqlite_error_code,
sql::Statement* statement) { … }
bool QuotaDatabase::MoveLegacyDatabase() { … }
bool QuotaDatabase::OpenDatabase() { … }
bool QuotaDatabase::EnsureDatabaseVersion() { … }
bool QuotaDatabase::CreateSchema() { … }
bool QuotaDatabase::CreateTable(const TableSchema& table) { … }
bool QuotaDatabase::CreateIndex(const IndexSchema& index) { … }
bool QuotaDatabase::ResetStorage() { … }
QuotaError QuotaDatabase::DumpBucketTable(const BucketTableCallback& callback) { … }
QuotaErrorOr<BucketInfo> QuotaDatabase::CreateBucketInternal(
const BucketInitParams& params,
StorageType type,
int max_bucket_count) { … }
void QuotaDatabase::SetDbErrorCallback(
const base::RepeatingCallback<void(int)>& db_error_callback) { … }
}