#ifndef EXTENSIONS_BROWSER_API_STORAGE_STORAGE_FRONTEND_H_
#define EXTENSIONS_BROWSER_API_STORAGE_STORAGE_FRONTEND_H_
#include <map>
#include <memory>
#include <string>
#include "base/memory/raw_ptr.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/values.h"
#include "extensions/browser/api/storage/settings_namespace.h"
#include "extensions/browser/api/storage/settings_observer.h"
#include "extensions/browser/api/storage/value_store_cache.h"
#include "extensions/browser/browser_context_keyed_api_factory.h"
namespace content {
class BrowserContext;
}
namespace value_store {
class ValueStoreFactory;
}
namespace extensions {
class StorageFrontend : public BrowserContextKeyedAPI {
public:
static StorageFrontend* Get(content::BrowserContext* context);
static std::unique_ptr<StorageFrontend> CreateForTesting(
scoped_refptr<value_store::ValueStoreFactory> storage_factory,
content::BrowserContext* context);
StorageFrontend(const StorageFrontend&) = delete;
StorageFrontend& operator=(const StorageFrontend&) = delete;
~StorageFrontend() override;
ValueStoreCache* GetValueStoreCache(
settings_namespace::Namespace settings_namespace) const;
bool IsStorageEnabled(settings_namespace::Namespace settings_namespace) const;
void RunWithStorage(scoped_refptr<const Extension> extension,
settings_namespace::Namespace settings_namespace,
ValueStoreCache::StorageCallback callback);
void DeleteStorageSoon(const std::string& extension_id,
base::OnceClosure done_callback);
SettingsChangedCallback GetObserver();
void DisableStorageForTesting(
settings_namespace::Namespace settings_namespace);
static BrowserContextKeyedAPIFactory<StorageFrontend>* GetFactoryInstance();
static const char* service_name();
static const bool kServiceRedirectedInIncognito = true;
static const bool kServiceIsNULLWhileTesting = true;
private:
friend class BrowserContextKeyedAPIFactory<StorageFrontend>;
typedef std::map<settings_namespace::Namespace, ValueStoreCache*> CacheMap;
explicit StorageFrontend(content::BrowserContext* context);
StorageFrontend(scoped_refptr<value_store::ValueStoreFactory> storage_factory,
content::BrowserContext* context);
void Init(scoped_refptr<value_store::ValueStoreFactory> storage_factory);
void OnSettingsChanged(const std::string& extension_id,
StorageAreaNamespace storage_area,
base::Value changes);
const raw_ptr<content::BrowserContext> browser_context_;
CacheMap caches_;
base::WeakPtrFactory<StorageFrontend> weak_factory_{this};
};
}
#endif