Chromium Code Reviews| 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..a4cfd80fdea9a3837764b025893e6507eaf08b0a 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,23 @@ const char kPrefPreviousVersion[] = "previous_version"; |
| // with the equivalent Pepper API. |
| const char kPackageDirectoryPath[] = "crxfs"; |
| +// Preference key for storing the last successful restart on watchdog requests. |
| +constexpr char kPrefRestartOnWatchdogTime[] = "last_restart_on_watchdog_time"; |
| + |
| +// Error and status messages strings for the restartOnWatchdog() 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 restartOnWatchdog |
| +// 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 +167,11 @@ BrowserContextKeyedAPIFactory<RuntimeAPI>* RuntimeAPI::GetFactoryInstance() { |
| return g_factory.Pointer(); |
| } |
| +// static |
| +void RuntimeAPI::RegisterPrefs(PrefRegistrySimple* registry) { |
| + registry->RegisterDoublePref(kPrefRestartOnWatchdogTime, 0.0); |
| +} |
| + |
| template <> |
| void BrowserContextKeyedAPIFactory<RuntimeAPI>::DeclareFactoryDependencies() { |
| DependsOn(ProcessManagerFactory::GetInstance()); |
| @@ -156,7 +181,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 +349,103 @@ 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; |
| + } |
| + |
| + if (seconds_from_now == -1) { |
| + MaybeCancelRunningWatchdogTimer(); |
|
Devlin
2016/05/25 21:53:21
Given we call this in both cases (here and on line
afakhry
2016/05/26 00:18:39
Done.
|
| + 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(kPrefRestartOnWatchdogTime); |
| + |
| + 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()) |
|
Devlin
2016/05/25 21:53:22
nit: prefer
if (watchdog_timer_.IsRunning())
wat
afakhry
2016/05/26 00:18:39
Done.
|
| + return; |
| + |
| + 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); |
| + } |
| + |
| + MaybeCancelRunningWatchdogTimer(); |
| + 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(kPrefRestartOnWatchdogTime, 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 +672,38 @@ ExtensionFunction::ResponseAction RuntimeRestartFunction::Run() { |
| return RespondNow(NoArguments()); |
| } |
| +ExtensionFunction::ResponseAction RuntimeRestartOnWatchdogFunction::Run() { |
| + if (!allow_non_kiost_apps_restart_api_for_test && |
| + !ExtensionsBrowserClient::Get()->IsRunningInForcedAppMode()) { |
| + return RespondNow(Error(kErrorOnlyKioskModeAllowed)); |
| + } |
| + |
| + std::unique_ptr<api::runtime::RestartOnWatchdog::Params> params( |
| + api::runtime::RestartOnWatchdog::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() |