Chromium Code Reviews| Index: extensions/browser/api/runtime/runtime_api.h |
| diff --git a/extensions/browser/api/runtime/runtime_api.h b/extensions/browser/api/runtime/runtime_api.h |
| index 187f7b0d7f16d0fb8e9f60cd2d45c50782e698fb..23766e72d3517864e2a33a4e4d2b03fbeac05024 100644 |
| --- a/extensions/browser/api/runtime/runtime_api.h |
| +++ b/extensions/browser/api/runtime/runtime_api.h |
| @@ -9,6 +9,8 @@ |
| #include "base/macros.h" |
| #include "base/scoped_observer.h" |
| +#include "base/time/time.h" |
| +#include "base/timer/timer.h" |
| #include "content/public/browser/notification_observer.h" |
| #include "content/public/browser/notification_registrar.h" |
| #include "extensions/browser/api/runtime/runtime_api_delegate.h" |
| @@ -49,6 +51,19 @@ class RuntimeAPI : public BrowserContextKeyedAPI, |
| public UpdateObserver, |
| public ProcessManagerObserver { |
| public: |
| + // The status of the restart on watchdog request. |
| + enum class RestartOnWatchdogStatus { |
| + // The request was made by a different extension other than the first one to |
| + // invoke the restartOnWatchdog runtime API. |
| + FAILED_NOT_FIRST_EXTENSION, |
| + |
| + // Any previously scheduled restart was successfully canceled. |
| + SUCCESS_RESTART_CANCELLED, |
| + |
| + // A restart was successfully scheduled. |
| + SUCCESS_RESTART_SCHEDULED, |
| + }; |
| + |
| static BrowserContextKeyedAPIFactory<RuntimeAPI>* GetFactoryInstance(); |
| explicit RuntimeAPI(content::BrowserContext* context); |
| @@ -65,10 +80,19 @@ class RuntimeAPI : public BrowserContextKeyedAPI, |
| void OpenURL(const GURL& uninstall_url); |
| bool GetPlatformInfo(api::runtime::PlatformInfo* info); |
| bool RestartDevice(std::string* error_message); |
| + |
| + using OnWatchdogTimeoutCallback = |
| + base::Callback<void(bool success, const std::string& message)>; |
| + RestartOnWatchdogStatus RestartDeviceOnWatchdogTimeout( |
| + const std::string& extension_id, |
| + int seconds_from_now, |
| + const OnWatchdogTimeoutCallback& callback); |
| + |
| bool OpenOptionsPage(const Extension* extension); |
| private: |
| friend class BrowserContextKeyedAPIFactory<RuntimeAPI>; |
| + friend class RestartOnWatchdogApiTest; |
| // ExtensionRegistryObserver implementation. |
| void OnExtensionLoaded(content::BrowserContext* browser_context, |
| @@ -81,6 +105,18 @@ class RuntimeAPI : public BrowserContextKeyedAPI, |
| const Extension* extension, |
| UninstallReason reason) override; |
| + // Cancels any previously scheduled restart request, invoking its associated |
| + // JS callback function stored in |current_watchdog_request_callback_|. |
| + void MaybeCancelRunningWatchdogTimer(); |
| + |
| + void ScheduleDelayedRestart(const std::string& extension_id, |
| + const base::Time& now, |
| + int seconds_from_now, |
| + const OnWatchdogTimeoutCallback& callback, |
| + std::unique_ptr<base::Value> stored_last_restart); |
| + |
| + void OnRestartWatchdogTimeout(const std::string& extension_id); |
|
Devlin
2016/05/20 17:26:32
Function docs
afakhry
2016/05/21 01:13:00
Done.
|
| + |
| // BrowserContextKeyedAPI implementation: |
| static const char* service_name() { return "RuntimeAPI"; } |
| static const bool kServiceRedirectedInIncognito = true; |
| @@ -102,6 +138,12 @@ class RuntimeAPI : public BrowserContextKeyedAPI, |
| void RemovePendingOnInstallInfoFromPref(const ExtensionId& extension_id); |
| void StorePendingOnInstallInfoToPref(const Extension* extension); |
| + void SetMinDurationBetweenRestartsForTesting(base::TimeDelta delta) { |
| + minimum_duration_between_restarts_ = delta; |
| + } |
| + |
| + void AllowNonKiostAppsInRestartOnWatchdogForTesting(); |
| + |
| std::unique_ptr<RuntimeAPIDelegate> delegate_; |
| content::BrowserContext* browser_context_; |
| @@ -118,6 +160,23 @@ class RuntimeAPI : public BrowserContextKeyedAPI, |
| ScopedObserver<ProcessManager, ProcessManagerObserver> |
| process_manager_observer_; |
| + // The ID of the first extension to call the restartOnWatchdog API. Any other |
| + // extensions to call this API after that will fail. |
| + std::string schedule_restart_first_extension_id_; |
| + |
| + // The current JS callback for the current scheduled restart which will be |
| + // invoked when that restart attempt is executed. |
| + OnWatchdogTimeoutCallback current_watchdog_request_callback_; |
|
Devlin
2016/05/20 17:26:33
Are we guaranteed to only have one of these?
afakhry
2016/05/21 01:13:00
This API is only allowed to be called by only one
|
| + |
| + // The timer that will trigger a device restart when it times out. |
| + base::OneShotTimer watchdog_timer_; |
| + |
| + // The minimum allowed duration between two successive restarts on watchdog |
| + // timer. |
| + base::TimeDelta minimum_duration_between_restarts_; |
| + |
| + base::WeakPtrFactory<RuntimeAPI> weak_ptr_factory_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(RuntimeAPI); |
| }; |
| @@ -220,6 +279,19 @@ class RuntimeRestartFunction : public UIThreadExtensionFunction { |
| ResponseAction Run() override; |
| }; |
| +class RuntimeRestartOnWatchdogFunction : public UIThreadExtensionFunction { |
| + public: |
| + DECLARE_EXTENSION_FUNCTION("runtime.restartOnWatchdog", |
|
Devlin
2016/05/20 17:26:32
I still think this is weird naming. Maybe restartA
afakhry
2016/05/21 01:13:00
I agree, the name is weird. I guess the intention
|
| + RUNTIME_RESTARTONWATCHDOG) |
| + |
| + protected: |
| + ~RuntimeRestartOnWatchdogFunction() override {} |
| + ResponseAction Run() override; |
| + |
| + private: |
| + void OnWatchdogTimeout(bool success, const std::string& message); |
| +}; |
| + |
| class RuntimeGetPlatformInfoFunction : public UIThreadExtensionFunction { |
| public: |
| DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo", |