#ifndef CHROME_BROWSER_ASH_APPS_APK_WEB_APP_SERVICE_H_
#define CHROME_BROWSER_ASH_APPS_APK_WEB_APP_SERVICE_H_
#include <string>
#include <vector>
#include "base/functional/callback.h"
#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/scoped_observation.h"
#include "chrome/browser/ash/app_list/arc/arc_app_list_prefs.h"
#include "chrome/browser/ash/apps/apk_web_app_installer.h"
#include "chromeos/ash/experiences/arc/mojom/app.mojom-forward.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/services/app_service/public/cpp/app_registry_cache.h"
#include "components/webapps/browser/uninstall_result_code.h"
#include "components/webapps/common/web_app_id.h"
class ArcAppListPrefs;
class Profile;
class GURL;
namespace user_prefs {
class PrefRegistrySyncable;
}
namespace webapps {
enum class InstallResultCode;
enum class UninstallResultCode;
}
namespace apps {
class PromiseAppServiceTest;
}
namespace ash {
class ApkWebAppService : public KeyedService,
public ApkWebAppInstaller::Owner,
public ArcAppListPrefs::Observer,
public apps::AppRegistryCache::Observer {
public:
class Delegate {
public:
using WebAppInstallCallback = base::OnceCallback<void(
const webapps::AppId& web_app_id,
bool is_web_only_twa,
const std::optional<std::string> sha256_fingerprint,
webapps::InstallResultCode code)>;
using WebAppUninstallCallback =
base::OnceCallback<void(webapps::UninstallResultCode code)>;
virtual ~Delegate();
virtual void MaybeUninstallPackageInArc(
const std::string& package_name) = 0;
};
static ApkWebAppService* Get(Profile* profile);
static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
explicit ApkWebAppService(Profile* profile, Delegate* test_delegate);
ApkWebAppService(const ApkWebAppService&) = delete;
ApkWebAppService& operator=(const ApkWebAppService&) = delete;
~ApkWebAppService() override;
bool IsWebOnlyTwa(const webapps::AppId& app_id);
bool IsWebAppInstalledFromArc(const webapps::AppId& web_app_id);
bool IsWebAppShellPackage(const std::string& package_name);
std::optional<std::string> GetPackageNameForWebApp(
const webapps::AppId& app_id,
bool include_installing_apks = false);
std::optional<std::string> GetPackageNameForWebApp(const GURL& url);
std::optional<std::string> GetWebAppIdForPackageName(
const std::string& package_name);
std::optional<std::string> GetCertificateSha256Fingerprint(
const webapps::AppId& app_id);
void AddInstallingWebApkPackageName(const std::string& app_id,
const std::string& package_name);
using WebAppCallbackForTesting =
base::OnceCallback<void(const std::string& package_name,
const webapps::AppId& web_app_id)>;
void SetWebAppInstalledCallbackForTesting(
WebAppCallbackForTesting web_app_installed_callback);
void SetWebAppUninstalledCallbackForTesting(
WebAppCallbackForTesting web_app_uninstalled_callback);
private:
friend class apps::PromiseAppServiceTest;
Delegate& GetDelegate() {
return test_delegate_ ? *test_delegate_ : *real_delegate_;
}
void MaybeInstallWebApp(const std::string& package_name,
arc::mojom::WebAppInfoPtr web_app_info);
void MaybeUninstallWebApp(const webapps::AppId& web_app_id);
void MaybeUninstallArcPackage(const std::string& package_name);
void UpdateShelfPin(const std::string& package_name,
const arc::mojom::WebAppInfoPtr& web_app_info);
void Shutdown() override;
void OnPackageInstalled(
const arc::mojom::ArcPackageInfo& package_info) override;
void OnPackageRemoved(const std::string& package_name,
bool uninstalled) override;
void OnPackageListInitialRefreshed() override;
void OnArcAppListPrefsDestroyed() override;
void OnAppUpdate(const apps::AppUpdate& update) override;
void OnAppTypeInitialized(apps::AppType app_type) override;
void OnAppRegistryCacheWillBeDestroyed(
apps::AppRegistryCache* cache) override;
void MaybeRemoveArcPackageForWebApp(const webapps::AppId& web_app_id);
void OnDidGetWebAppIcon(const std::string& package_name,
arc::mojom::WebAppInfoPtr web_app_info,
arc::mojom::RawIconPngDataPtr icon);
void OnDidFinishInstall(const std::string& package_name,
const webapps::AppId& web_app_id,
bool is_web_only_twa,
const std::optional<std::string> sha256_fingerprint,
webapps::InstallResultCode code);
void OnDidRemoveInstallSource(const webapps::AppId& app_id,
webapps::UninstallResultCode code);
void UpdatePackageInfo(const std::string& app_id,
const arc::mojom::WebAppInfoPtr& web_app_info);
const base::Value::Dict& WebAppToApks() const;
void SyncArcAndWebApps();
void RemoveObsoletePrefValues(const webapps::AppId& web_app_id);
void RemoveInstallingWebApkPackageName(const std::string& app_id);
WebAppCallbackForTesting web_app_installed_callback_;
WebAppCallbackForTesting web_app_uninstalled_callback_;
raw_ptr<Profile> profile_;
raw_ptr<ArcAppListPrefs, DanglingUntriaged> arc_app_list_prefs_;
std::unique_ptr<Delegate> real_delegate_;
raw_ptr<Delegate> test_delegate_;
bool arc_initialized_ = false;
std::map<std::string, std::string> currently_installing_apks_;
base::ScopedObservation<apps::AppRegistryCache,
apps::AppRegistryCache::Observer>
app_registry_cache_observer_{this};
base::ScopedObservation<ArcAppListPrefs, ArcAppListPrefs::Observer>
arc_app_list_prefs_observer_{this};
base::WeakPtrFactory<ApkWebAppService> weak_ptr_factory_{this};
};
}
#endif