#ifndef STORAGE_BROWSER_QUOTA_USAGE_TRACKER_H_
#define STORAGE_BROWSER_QUOTA_USAGE_TRACKER_H_
#include <stdint.h>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>
#include "base/component_export.h"
#include "base/containers/flat_map.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/sequence_checker.h"
#include "components/services/storage/public/cpp/buckets/bucket_info.h"
#include "components/services/storage/public/cpp/buckets/bucket_locator.h"
#include "components/services/storage/public/mojom/quota_client.mojom.h"
#include "storage/browser/quota/quota_callbacks.h"
#include "storage/browser/quota/quota_client_type.h"
#include "storage/browser/quota/quota_manager_impl.h"
#include "storage/browser/quota/quota_task.h"
#include "storage/browser/quota/special_storage_policy.h"
namespace blink {
class StorageKey;
}
namespace storage {
class ClientUsageTracker;
class COMPONENT_EXPORT(STORAGE_BROWSER) UsageTracker
: public QuotaTaskObserver {
public:
UsageTracker(
QuotaManagerImpl* quota_manager_impl,
const base::flat_map<mojom::QuotaClient*, QuotaClientType>& client_types,
scoped_refptr<SpecialStoragePolicy> special_storage_policy);
UsageTracker(const UsageTracker&) = delete;
UsageTracker& operator=(const UsageTracker&) = delete;
~UsageTracker() override;
void GetGlobalUsage(UsageCallback callback);
void GetStorageKeyUsageWithBreakdown(const blink::StorageKey& storage_key,
UsageWithBreakdownCallback callback);
void GetBucketUsageWithBreakdown(const BucketLocator& bucket,
UsageWithBreakdownCallback callback);
void UpdateBucketUsageCache(QuotaClientType client_type,
const BucketLocator& bucket,
std::optional<int64_t> delta);
void DeleteBucketCache(QuotaClientType client_type,
const BucketLocator& bucket);
int64_t GetCachedUsage() const;
std::map<std::string, int64_t> GetCachedHostsUsage() const;
std::map<blink::StorageKey, int64_t> GetCachedStorageKeysUsage() const;
std::map<BucketLocator, int64_t> GetCachedBucketsUsage() const;
bool IsWorking() const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
return !global_usage_callbacks_.empty() ||
!storage_key_usage_callbacks_.empty() ||
!bucket_usage_callbacks_.empty();
}
void SetUsageCacheEnabled(QuotaClientType client_type,
const blink::StorageKey& storage_key,
bool enabled);
private:
struct AccumulateInfo;
friend class ClientUsageTracker;
void DidGetAllBuckets(QuotaErrorOr<std::set<BucketInfo>> result);
void DidGetBucketsForStorageKey(const blink::StorageKey& storage_key,
QuotaErrorOr<std::set<BucketInfo>> result);
void AccumulateClientGlobalUsage(base::OnceClosure barrier_callback,
AccumulateInfo* info,
int64_t total_usage,
int64_t unlimited_usage);
void AccumulateClientUsageWithBreakdown(base::OnceClosure barrier_callback,
AccumulateInfo* info,
QuotaClientType client,
int64_t total_usage,
int64_t unlimited_usage);
void FinallySendGlobalUsage(std::unique_ptr<AccumulateInfo> info);
void FinallySendStorageKeyUsageWithBreakdown(
std::unique_ptr<AccumulateInfo> info,
const blink::StorageKey& storage_key);
void FinallySendBucketUsageWithBreakdown(std::unique_ptr<AccumulateInfo> info,
const BucketLocator& bucket);
ClientUsageTracker& GetClient(QuotaClientType type);
SEQUENCE_CHECKER(sequence_checker_);
const raw_ptr<QuotaManagerImpl> quota_manager_impl_;
base::flat_map<QuotaClientType, std::unique_ptr<ClientUsageTracker>>
client_tracker_map_;
std::vector<UsageCallback> global_usage_callbacks_;
std::map<blink::StorageKey, std::vector<UsageWithBreakdownCallback>>
storage_key_usage_callbacks_;
std::map<BucketLocator, std::vector<UsageWithBreakdownCallback>>
bucket_usage_callbacks_;
base::WeakPtrFactory<UsageTracker> weak_factory_{this};
};
}
#endif