#ifndef EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_
#define EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_
#include <memory>
#include <optional>
#include <string>
#include "base/memory/raw_ptr.h"
#include "base/scoped_observation.h"
#include "base/time/time.h"
#include "base/timer/timer.h"
#include "base/values.h"
#include "extensions/browser/api/runtime/runtime_api_delegate.h"
#include "extensions/browser/browser_context_keyed_api_factory.h"
#include "extensions/browser/events/lazy_event_dispatch_util.h"
#include "extensions/browser/extension_function.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_registry_observer.h"
#include "extensions/browser/lazy_context_task_queue.h"
#include "extensions/browser/process_manager.h"
#include "extensions/browser/process_manager_observer.h"
#include "extensions/browser/update_observer.h"
#include "extensions/buildflags/buildflags.h"
#include "extensions/common/api/runtime.h"
#include "extensions/common/extension_id.h"
static_assert(BUILDFLAG(ENABLE_EXTENSIONS_CORE));
namespace base {
class Version;
}
namespace content {
class BrowserContext;
}
class PrefRegistrySimple;
namespace extensions {
namespace api::runtime {
struct PlatformInfo;
}
class Extension;
class ExtensionRegistry;
class RuntimeAPI : public BrowserContextKeyedAPI,
public ExtensionRegistryObserver,
public UpdateObserver,
public ProcessManagerObserver,
public LazyEventDispatchUtil::Observer {
public:
enum class RestartAfterDelayStatus {
FAILED_NOT_FIRST_EXTENSION,
FAILED_THROTTLED,
SUCCESS_RESTART_CANCELED,
SUCCESS_RESTART_SCHEDULED,
};
static constexpr int kFastReloadCount = 5;
static constexpr int kUnpackedFastReloadCount = 30;
static BrowserContextKeyedAPIFactory<RuntimeAPI>* GetFactoryInstance();
static void RegisterPrefs(PrefRegistrySimple* registry);
explicit RuntimeAPI(content::BrowserContext* context);
RuntimeAPI(const RuntimeAPI&) = delete;
RuntimeAPI& operator=(const RuntimeAPI&) = delete;
~RuntimeAPI() override;
void ReloadExtension(const ExtensionId& extension_id);
bool CheckForUpdates(const ExtensionId& extension_id,
RuntimeAPIDelegate::UpdateCheckCallback callback);
void OpenURL(const GURL& uninstall_url);
bool GetPlatformInfo(api::runtime::PlatformInfo* info);
bool RestartDevice(std::string* error_message);
RestartAfterDelayStatus RestartDeviceAfterDelay(
const ExtensionId& extension_id,
int seconds_from_now);
bool OpenOptionsPage(const Extension* extension,
content::BrowserContext* browser_context);
private:
friend class BrowserContextKeyedAPIFactory<RuntimeAPI>;
friend class RestartAfterDelayApiTest;
void OnExtensionLoaded(content::BrowserContext* browser_context,
const Extension* extension) override;
void OnExtensionUninstalled(content::BrowserContext* browser_context,
const Extension* extension,
UninstallReason reason) override;
void OnExtensionInstalledAndLoaded(
content::BrowserContext* browser_context,
const Extension* extension,
const base::Version& previous_version) override;
void MaybeCancelRunningDelayedRestartTimer();
void OnExtensionsReady();
RestartAfterDelayStatus ScheduleDelayedRestart(const base::Time& now,
int seconds_from_now);
void OnDelayedRestartTimerTimeout();
static const char* service_name() { return "RuntimeAPI"; }
static const bool kServiceRedirectedInIncognito = true;
static const bool kServiceIsNULLWhileTesting = true;
void Shutdown() override;
void OnAppUpdateAvailable(const Extension& extension) override;
void OnChromeUpdateAvailable() override;
void OnBackgroundHostStartup(const Extension* extension) override;
void AllowNonKioskAppsInRestartAfterDelayForTesting();
void set_min_duration_between_restarts_for_testing(base::TimeDelta delta) {
minimum_duration_between_restarts_ = delta;
}
std::unique_ptr<RuntimeAPIDelegate> delegate_;
raw_ptr<content::BrowserContext> browser_context_;
base::ScopedObservation<ExtensionRegistry, ExtensionRegistryObserver>
extension_registry_observation_{this};
base::ScopedObservation<ProcessManager, ProcessManagerObserver>
process_manager_observation_{this};
std::string schedule_restart_first_extension_id_;
base::OneShotTimer restart_after_delay_timer_;
base::TimeDelta minimum_duration_between_restarts_;
base::Time last_delayed_restart_time_;
bool dispatch_chrome_updated_event_;
bool did_read_delayed_restart_preferences_;
bool was_last_restart_due_to_delayed_restart_api_;
base::WeakPtrFactory<RuntimeAPI> weak_ptr_factory_{this};
};
template <>
void BrowserContextKeyedAPIFactory<RuntimeAPI>::DeclareFactoryDependencies();
class RuntimeEventRouter {
public:
static void DispatchOnStartupEvent(content::BrowserContext* context,
const ExtensionId& extension_id);
static void DispatchOnInstalledEvent(void* context_id,
const ExtensionId& extension_id,
const base::Version& old_version,
bool chrome_updated);
static void DispatchOnUpdateAvailableEvent(content::BrowserContext* context,
const ExtensionId& extension_id,
const base::Value::Dict* manifest);
static void DispatchOnBrowserUpdateAvailableEvent(
content::BrowserContext* context);
static void DispatchOnRestartRequiredEvent(
content::BrowserContext* context,
const std::string& app_id,
api::runtime::OnRestartRequiredReason reason);
static void OnExtensionUninstalled(content::BrowserContext* context,
const ExtensionId& extension_id,
UninstallReason reason);
};
class RuntimeGetBackgroundPageFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("runtime.getBackgroundPage",
RUNTIME_GETBACKGROUNDPAGE)
protected:
~RuntimeGetBackgroundPageFunction() override = default;
ResponseAction Run() override;
private:
void OnPageLoaded(
std::unique_ptr<LazyContextTaskQueue::ContextInfo> context_info);
};
class RuntimeOpenOptionsPageFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("runtime.openOptionsPage", RUNTIME_OPENOPTIONSPAGE)
protected:
~RuntimeOpenOptionsPageFunction() override = default;
ResponseAction Run() override;
};
class RuntimeSetUninstallURLFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("runtime.setUninstallURL", RUNTIME_SETUNINSTALLURL)
protected:
~RuntimeSetUninstallURLFunction() override = default;
ResponseAction Run() override;
};
class RuntimeReloadFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("runtime.reload", RUNTIME_RELOAD)
protected:
~RuntimeReloadFunction() override = default;
ResponseAction Run() override;
};
class RuntimeRequestUpdateCheckFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("runtime.requestUpdateCheck",
RUNTIME_REQUESTUPDATECHECK)
protected:
~RuntimeRequestUpdateCheckFunction() override = default;
ResponseAction Run() override;
private:
void CheckComplete(const RuntimeAPIDelegate::UpdateCheckResult& result);
};
class RuntimeRestartFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("runtime.restart", RUNTIME_RESTART)
protected:
~RuntimeRestartFunction() override = default;
ResponseAction Run() override;
};
class RuntimeRestartAfterDelayFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("runtime.restartAfterDelay",
RUNTIME_RESTARTAFTERDELAY)
protected:
~RuntimeRestartAfterDelayFunction() override = default;
ResponseAction Run() override;
};
class RuntimeGetPlatformInfoFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo", RUNTIME_GETPLATFORMINFO)
protected:
~RuntimeGetPlatformInfoFunction() override = default;
ResponseAction Run() override;
};
class RuntimeGetPackageDirectoryEntryFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("runtime.getPackageDirectoryEntry",
RUNTIME_GETPACKAGEDIRECTORYENTRY)
protected:
~RuntimeGetPackageDirectoryEntryFunction() override = default;
ResponseAction Run() override;
};
class RuntimeGetContextsFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("runtime.getContexts", RUNTIME_GETCONTEXTS)
RuntimeGetContextsFunction();
RuntimeGetContextsFunction(const RuntimeGetContextsFunction&) = delete;
RuntimeGetContextsFunction& operator=(const RuntimeGetContextsFunction&) =
delete;
private:
~RuntimeGetContextsFunction() override;
ResponseAction Run() override;
std::optional<api::runtime::ExtensionContext> GetWorkerContext();
std::vector<api::runtime::ExtensionContext> GetFrameContexts();
int GetTabId(content::WebContents& web_contents);
int GetFrameId(content::RenderFrameHost& host);
int GetWindowId(content::WebContents& web_contents);
};
}
#endif