| Index: extensions/browser/api/runtime/runtime_api.cc
|
| diff --git a/extensions/browser/api/runtime/runtime_api.cc b/extensions/browser/api/runtime/runtime_api.cc
|
| index e62805e674289632f33a1786b7a89eac8b06d478..cd63723aef33690a51991e37183430209f3cbe79 100644
|
| --- a/extensions/browser/api/runtime/runtime_api.cc
|
| +++ b/extensions/browser/api/runtime/runtime_api.cc
|
| @@ -10,8 +10,11 @@
|
| #include "base/lazy_instance.h"
|
| #include "base/logging.h"
|
| #include "base/metrics/histogram.h"
|
| +#include "base/strings/string_number_conversions.h"
|
| #include "base/values.h"
|
| #include "base/version.h"
|
| +#include "components/prefs/pref_registry_simple.h"
|
| +#include "components/prefs/pref_service.h"
|
| #include "content/public/browser/browser_context.h"
|
| #include "content/public/browser/child_process_security_policy.h"
|
| #include "content/public/browser/notification_service.h"
|
| @@ -76,6 +79,25 @@ const char kPrefPreviousVersion[] = "previous_version";
|
| // with the equivalent Pepper API.
|
| const char kPackageDirectoryPath[] = "crxfs";
|
|
|
| +// Preference key for storing the last successful restart on the watchdog timer
|
| +// timing out.
|
| +constexpr char kPrefLastRestartAfterDelayTime[] =
|
| + "last_restart_on_watchdog_time";
|
| +
|
| +// Error and status messages strings for the restartAfterDelay() API.
|
| +constexpr char kErrorInvalidArgument[] = "Invalid argument: *.";
|
| +constexpr char kErrorOnlyKioskModeAllowed[] =
|
| + "API available only for ChromeOS kiosk mode.";
|
| +constexpr char kErrorOnlyFirstExtensionAllowed[] =
|
| + "Not the first extension to call this API.";
|
| +constexpr char kErrorInvalidStatus[] = "Invalid restart request status.";
|
| +
|
| +constexpr int kMinDurationBetweenSuccessiveRestartsHours = 3;
|
| +
|
| +// This is used for browsertests, so that we can test the restartAfterDelay
|
| +// API without a kiost app.
|
| +bool allow_non_kiost_apps_restart_api_for_test = false;
|
| +
|
| void DispatchOnStartupEventImpl(BrowserContext* browser_context,
|
| const std::string& extension_id,
|
| bool first_call,
|
| @@ -147,6 +169,11 @@ BrowserContextKeyedAPIFactory<RuntimeAPI>* RuntimeAPI::GetFactoryInstance() {
|
| return g_factory.Pointer();
|
| }
|
|
|
| +// static
|
| +void RuntimeAPI::RegisterPrefs(PrefRegistrySimple* registry) {
|
| + registry->RegisterDoublePref(kPrefLastRestartAfterDelayTime, 0.0);
|
| +}
|
| +
|
| template <>
|
| void BrowserContextKeyedAPIFactory<RuntimeAPI>::DeclareFactoryDependencies() {
|
| DependsOn(ProcessManagerFactory::GetInstance());
|
| @@ -156,7 +183,10 @@ RuntimeAPI::RuntimeAPI(content::BrowserContext* context)
|
| : browser_context_(context),
|
| dispatch_chrome_updated_event_(false),
|
| extension_registry_observer_(this),
|
| - process_manager_observer_(this) {
|
| + process_manager_observer_(this),
|
| + minimum_duration_between_restarts_(base::TimeDelta::FromHours(
|
| + kMinDurationBetweenSuccessiveRestartsHours)),
|
| + weak_ptr_factory_(this) {
|
| // RuntimeAPI is redirected in incognito, so |browser_context_| is never
|
| // incognito.
|
| DCHECK(!browser_context_->IsOffTheRecord());
|
| @@ -321,10 +351,102 @@ bool RuntimeAPI::RestartDevice(std::string* error_message) {
|
| return delegate_->RestartDevice(error_message);
|
| }
|
|
|
| +RuntimeAPI::RestartOnWatchdogStatus RuntimeAPI::RestartDeviceOnWatchdogTimeout(
|
| + const std::string& extension_id,
|
| + int seconds_from_now) {
|
| + // To achieve as much accuracy as possible, record the time of the call as
|
| + // |now| here.
|
| + const base::Time now = base::Time::NowFromSystemTime();
|
| +
|
| + if (schedule_restart_first_extension_id_.empty()) {
|
| + schedule_restart_first_extension_id_ = extension_id;
|
| + } else if (extension_id != schedule_restart_first_extension_id_) {
|
| + // We only allow the first extension to call this API to call it repeatedly.
|
| + // Any other extension will fail.
|
| + return RestartOnWatchdogStatus::FAILED_NOT_FIRST_EXTENSION;
|
| + }
|
| +
|
| + MaybeCancelRunningWatchdogTimer();
|
| +
|
| + if (seconds_from_now == -1) {
|
| + // We already stopped the running timer (if any).
|
| + return RestartOnWatchdogStatus::SUCCESS_RESTART_CANCELLED;
|
| + }
|
| +
|
| + // Try to read any previous successful restart attempt time resulting from
|
| + // this API.
|
| + PrefService* pref_service =
|
| + ExtensionsBrowserClient::Get()->GetPrefServiceForContext(
|
| + browser_context_);
|
| + DCHECK(pref_service);
|
| + double last_restart_time =
|
| + pref_service->GetDouble(kPrefLastRestartAfterDelayTime);
|
| +
|
| + ScheduleDelayedRestart(now, seconds_from_now, last_restart_time);
|
| + return RestartOnWatchdogStatus::SUCCESS_RESTART_SCHEDULED;
|
| +}
|
| +
|
| bool RuntimeAPI::OpenOptionsPage(const Extension* extension) {
|
| return delegate_->OpenOptionsPage(extension);
|
| }
|
|
|
| +void RuntimeAPI::MaybeCancelRunningWatchdogTimer() {
|
| + if (watchdog_timer_.IsRunning())
|
| + watchdog_timer_.Stop();
|
| +}
|
| +
|
| +void RuntimeAPI::ScheduleDelayedRestart(const base::Time& now,
|
| + int seconds_from_now,
|
| + double stored_last_restart) {
|
| + base::TimeDelta delay_till_restart =
|
| + base::TimeDelta::FromSeconds(seconds_from_now);
|
| +
|
| + // Throttle restart requests that are received too soon successively.
|
| + base::Time last_restart_time = base::Time::FromDoubleT(stored_last_restart);
|
| + base::Time future_restart_time = now + delay_till_restart;
|
| + base::TimeDelta future_time_since_last_restart =
|
| + future_restart_time > last_restart_time
|
| + ? future_restart_time - last_restart_time
|
| + : base::TimeDelta();
|
| + if (future_time_since_last_restart < minimum_duration_between_restarts_) {
|
| + // Schedule the restart after |minimum_duration_between_restarts_| has
|
| + // passed.
|
| + delay_till_restart =
|
| + minimum_duration_between_restarts_ - (now - last_restart_time);
|
| + }
|
| +
|
| + watchdog_timer_.Start(FROM_HERE, delay_till_restart,
|
| + base::Bind(&RuntimeAPI::OnRestartWatchdogTimeout,
|
| + weak_ptr_factory_.GetWeakPtr()));
|
| +}
|
| +
|
| +void RuntimeAPI::OnRestartWatchdogTimeout() {
|
| + // We can persist "now" as the last successful restart time, assuming that the
|
| + // following restart request will succeed, since it can only fail if requested
|
| + // by non kiosk apps, and we prevent that from the beginning (unless in
|
| + // browsertests).
|
| + // This assumption is important, since once restart is requested, we might not
|
| + // have enough time to persist the data to disk.
|
| + PrefService* pref_service =
|
| + ExtensionsBrowserClient::Get()->GetPrefServiceForContext(
|
| + browser_context_);
|
| + DCHECK(pref_service);
|
| + base::Time now = base::Time::NowFromSystemTime();
|
| + pref_service->SetDouble(kPrefLastRestartAfterDelayTime, now.ToDoubleT());
|
| +
|
| + std::string error_message;
|
| + const bool success = delegate_->RestartDevice(&error_message);
|
| +
|
| + if (!success && !allow_non_kiost_apps_restart_api_for_test) {
|
| + // This is breaking our above assumption and should never be reached.
|
| + NOTREACHED();
|
| + }
|
| +}
|
| +
|
| +void RuntimeAPI::AllowNonKiostAppsInRestartOnWatchdogForTesting() {
|
| + allow_non_kiost_apps_restart_api_for_test = true;
|
| +}
|
| +
|
| ///////////////////////////////////////////////////////////////////////////////
|
|
|
| // static
|
| @@ -551,6 +673,38 @@ ExtensionFunction::ResponseAction RuntimeRestartFunction::Run() {
|
| return RespondNow(NoArguments());
|
| }
|
|
|
| +ExtensionFunction::ResponseAction RuntimeRestartAfterDelayFunction::Run() {
|
| + if (!allow_non_kiost_apps_restart_api_for_test &&
|
| + !ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode()) {
|
| + return RespondNow(Error(kErrorOnlyKioskModeAllowed));
|
| + }
|
| +
|
| + std::unique_ptr<api::runtime::RestartAfterDelay::Params> params(
|
| + api::runtime::RestartAfterDelay::Params::Create(*args_));
|
| + EXTENSION_FUNCTION_VALIDATE(params.get());
|
| + int seconds = params->seconds;
|
| +
|
| + if (seconds < -1)
|
| + return RespondNow(Error(kErrorInvalidArgument, base::IntToString(seconds)));
|
| +
|
| + RuntimeAPI::RestartOnWatchdogStatus request_status =
|
| + RuntimeAPI::GetFactoryInstance()
|
| + ->Get(browser_context())
|
| + ->RestartDeviceOnWatchdogTimeout(extension()->id(), seconds);
|
| +
|
| + switch (request_status) {
|
| + case RuntimeAPI::RestartOnWatchdogStatus::FAILED_NOT_FIRST_EXTENSION:
|
| + return RespondNow(Error(kErrorOnlyFirstExtensionAllowed));
|
| +
|
| + case RuntimeAPI::RestartOnWatchdogStatus::SUCCESS_RESTART_CANCELLED:
|
| + case RuntimeAPI::RestartOnWatchdogStatus::SUCCESS_RESTART_SCHEDULED:
|
| + return RespondNow(NoArguments());
|
| + }
|
| +
|
| + NOTREACHED();
|
| + return RespondNow(Error(kErrorInvalidStatus));
|
| +}
|
| +
|
| ExtensionFunction::ResponseAction RuntimeGetPlatformInfoFunction::Run() {
|
| runtime::PlatformInfo info;
|
| if (!RuntimeAPI::GetFactoryInstance()
|
|
|