#ifndef CHROME_BROWSER_ASH_EXTENSIONS_INSTALL_LIMITER_H_
#define CHROME_BROWSER_ASH_EXTENSIONS_INSTALL_LIMITER_H_
#include <stdint.h>
#include <optional>
#include <set>
#include "base/containers/queue.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/timer/timer.h"
#include "chrome/browser/extensions/crx_installer.h"
#include "components/keyed_service/core/keyed_service.h"
namespace extensions {
class InstallLimiter : public KeyedService {
public:
static InstallLimiter* Get(Profile* profile);
static bool ShouldDeferInstall(int64_t app_size, const std::string& app_id);
InstallLimiter();
InstallLimiter(const InstallLimiter&) = delete;
InstallLimiter& operator=(const InstallLimiter&) = delete;
~InstallLimiter() override;
void DisableForTest();
void Add(const scoped_refptr<CrxInstaller>& installer,
const CRXFileInfo& file_info);
void OnAllExternalProvidersReady();
private:
struct DeferredInstall {
DeferredInstall(const scoped_refptr<CrxInstaller>& installer,
const CRXFileInfo& file_info);
DeferredInstall(const DeferredInstall& other);
~DeferredInstall();
const scoped_refptr<CrxInstaller> installer;
const CRXFileInfo file_info;
};
using DeferredInstallList = base::queue<DeferredInstall>;
void AddWithSize(const scoped_refptr<CrxInstaller>& installer,
const CRXFileInfo& file_info,
std::optional<int64_t> size);
void CheckAndRunDeferrredInstalls();
void RunInstall(const scoped_refptr<CrxInstaller>& installer,
const CRXFileInfo& file_info);
void OnInstallerDone(const std::optional<CrxInstallError>& error);
bool AllInstallsQueuedWithFileSize() const;
DeferredInstallList deferred_installs_;
uint32_t num_running_installs_ = 0;
base::OneShotTimer wait_timer_;
bool disabled_for_test_;
bool all_external_providers_ready_ = false;
int num_installs_waiting_for_file_size_ = 0;
base::WeakPtrFactory<InstallLimiter> weak_ptr_factory_{this};
};
}
#endif