#ifndef CHROME_BROWSER_BACKGROUND_GLIC_GLIC_BACKGROUND_MODE_MANAGER_H_
#define CHROME_BROWSER_BACKGROUND_GLIC_GLIC_BACKGROUND_MODE_MANAGER_H_
#include <map>
#include <memory>
#include "base/callback_list.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "chrome/browser/background/glic/glic_launcher_configuration.h"
#include "chrome/browser/profiles/profile_manager_observer.h"
#include "chrome/browser/profiles/profile_observer.h"
class ScopedKeepAlive;
class StatusTray;
namespace ui {
class Accelerator;
}
namespace glic {
class GlicController;
class GlicStatusIcon;
enum class HotkeyUsage {
kDefault = 0,
kCustom = 1,
kMaxValue = kCustom,
};
class GlicBackgroundModeManager : public GlicLauncherConfiguration::Observer,
public ProfileManagerObserver,
public ProfileObserver {
public:
explicit GlicBackgroundModeManager(StatusTray* status_tray);
~GlicBackgroundModeManager() override;
static GlicBackgroundModeManager* GetInstance();
void OnEnabledChanged(bool enabled) override;
void OnGlobalHotkeyChanged(ui::Accelerator hotkey) override;
void OnProfileAdded(Profile* profile) override;
void OnProfileWillBeDestroyed(Profile* profile) override;
void HandleHotkey(const ui::Accelerator& accelerator);
void Shutdown();
ui::Accelerator RegisteredHotkeyForTesting() {
return actual_registered_hotkey_;
}
bool IsInBackgroundModeForTesting() {
CHECK_EQ(static_cast<bool>(keep_alive_), static_cast<bool>(status_icon_));
return keep_alive_ != nullptr;
}
GlicStatusIcon* GetStatusIconForTesting() { return status_icon_.get(); }
void EnterBackgroundMode();
void ExitBackgroundMode();
private:
class AcceleratorRegistrar;
void EnableLaunchOnStartup(bool should_launch);
void RegisterHotkey(ui::Accelerator updated_hotkey);
void UnregisterHotkey();
void UpdateState();
bool IsEnabledInAnyLoadedProfile();
std::unique_ptr<GlicLauncherConfiguration> configuration_;
std::unique_ptr<GlicController> controller_;
std::unique_ptr<ScopedKeepAlive> keep_alive_;
raw_ptr<StatusTray, DanglingUntriaged> status_tray_;
std::unique_ptr<GlicStatusIcon> status_icon_;
bool enabled_pref_ = false;
ui::Accelerator expected_registered_hotkey_;
ui::Accelerator actual_registered_hotkey_;
std::unique_ptr<AcceleratorRegistrar> accelerator_registrar_;
std::map<Profile*, base::CallbackListSubscription>
profile_enabled_subscriptions_;
std::map<Profile*, base::CallbackListSubscription>
profile_consent_subscriptions_;
using ScopedProfileObserver =
base::ScopedObservation<Profile, ProfileObserver>;
std::map<Profile*, ScopedProfileObserver> profile_observers_;
base::WeakPtrFactory<GlicBackgroundModeManager> weak_ptr_factory_{this};
};
}
#endif