#ifndef CHROME_BROWSER_EXTENSIONS_CHROME_APP_ICON_SERVICE_H_
#define CHROME_BROWSER_EXTENSIONS_CHROME_APP_ICON_SERVICE_H_
#include <map>
#include <memory>
#include <set>
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "build/chromeos_buildflags.h"
#include "components/keyed_service/core/keyed_service.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_registry_observer.h"
#include "extensions/buildflags/buildflags.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "chrome/browser/ui/ash/shelf/shelf_extension_app_updater.h"
#endif
static_assert(BUILDFLAG(ENABLE_EXTENSIONS_CORE));
namespace content {
class BrowserContext;
}
namespace gfx {
class ImageSkia;
class Size;
}
namespace extensions {
class ChromeAppIcon;
class ChromeAppIconDelegate;
class ChromeAppIconService : public KeyedService,
#if BUILDFLAG(IS_CHROMEOS)
public ShelfAppUpdater::Delegate,
#endif
public ExtensionRegistryObserver {
public:
using ResizeFunction =
base::RepeatingCallback<void(const gfx::Size&, gfx::ImageSkia*)>;
explicit ChromeAppIconService(content::BrowserContext* context);
ChromeAppIconService(const ChromeAppIconService&) = delete;
ChromeAppIconService& operator=(const ChromeAppIconService&) = delete;
~ChromeAppIconService() override;
static ChromeAppIconService* Get(content::BrowserContext* context);
std::unique_ptr<ChromeAppIcon> CreateIcon(
ChromeAppIconDelegate* delegate,
const std::string& app_id,
int resource_size_in_dip,
const ResizeFunction& resize_function);
std::unique_ptr<ChromeAppIcon> CreateIcon(ChromeAppIconDelegate* delegate,
const std::string& app_id,
int resource_size_in_dip);
void Shutdown() override;
private:
class Updater;
using IconMap =
std::map<std::string, std::set<raw_ptr<ChromeAppIcon, SetExperimental>>>;
void OnIconDestroyed(ChromeAppIcon* icon);
void OnAppUpdated(const std::string& app_id);
void OnExtensionLoaded(content::BrowserContext* browser_context,
const Extension* extension) override;
void OnExtensionUnloaded(content::BrowserContext* browser_context,
const Extension* extension,
UnloadedExtensionReason reason) override;
#if BUILDFLAG(IS_CHROMEOS)
void OnAppUpdated(content::BrowserContext* browser_context,
const std::string& app_id,
bool reload_icon) override;
#endif
raw_ptr<content::BrowserContext> context_;
#if BUILDFLAG(IS_CHROMEOS)
std::unique_ptr<ShelfExtensionAppUpdater> app_updater_;
#endif
void MaybeCleanupIconSet(const std::string& app_id);
IconMap icon_map_;
base::ScopedObservation<ExtensionRegistry, ExtensionRegistryObserver>
observation_{this};
base::WeakPtrFactory<ChromeAppIconService> weak_ptr_factory_{this};
};
}
#endif