Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_ |
| 6 #define EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_ | 6 #define EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/scoped_observer.h" | 11 #include "base/scoped_observer.h" |
| 12 #include "base/time/time.h" | |
| 13 #include "base/timer/timer.h" | |
| 12 #include "content/public/browser/notification_observer.h" | 14 #include "content/public/browser/notification_observer.h" |
| 13 #include "content/public/browser/notification_registrar.h" | 15 #include "content/public/browser/notification_registrar.h" |
| 14 #include "extensions/browser/api/runtime/runtime_api_delegate.h" | 16 #include "extensions/browser/api/runtime/runtime_api_delegate.h" |
| 15 #include "extensions/browser/browser_context_keyed_api_factory.h" | 17 #include "extensions/browser/browser_context_keyed_api_factory.h" |
| 16 #include "extensions/browser/extension_function.h" | 18 #include "extensions/browser/extension_function.h" |
| 17 #include "extensions/browser/extension_registry_observer.h" | 19 #include "extensions/browser/extension_registry_observer.h" |
| 18 #include "extensions/browser/process_manager.h" | 20 #include "extensions/browser/process_manager.h" |
| 19 #include "extensions/browser/process_manager_observer.h" | 21 #include "extensions/browser/process_manager_observer.h" |
| 20 #include "extensions/browser/update_observer.h" | 22 #include "extensions/browser/update_observer.h" |
| 21 #include "extensions/common/api/runtime.h" | 23 #include "extensions/common/api/runtime.h" |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 42 | 44 |
| 43 // Runtime API dispatches onStartup, onInstalled, and similar events to | 45 // Runtime API dispatches onStartup, onInstalled, and similar events to |
| 44 // extensions. There is one instance shared between a browser context and | 46 // extensions. There is one instance shared between a browser context and |
| 45 // its related incognito instance. | 47 // its related incognito instance. |
| 46 class RuntimeAPI : public BrowserContextKeyedAPI, | 48 class RuntimeAPI : public BrowserContextKeyedAPI, |
| 47 public content::NotificationObserver, | 49 public content::NotificationObserver, |
| 48 public ExtensionRegistryObserver, | 50 public ExtensionRegistryObserver, |
| 49 public UpdateObserver, | 51 public UpdateObserver, |
| 50 public ProcessManagerObserver { | 52 public ProcessManagerObserver { |
| 51 public: | 53 public: |
| 54 // The status of the restart on watchdog request. | |
| 55 enum class RestartOnWatchdogStatus { | |
| 56 // The request was made by a different extension other than the first one to | |
| 57 // invoke the restartOnWatchdog runtime API. | |
| 58 FAILED_NOT_FIRST_EXTENSION, | |
| 59 | |
| 60 // Any previously scheduled restart was successfully canceled. | |
| 61 SUCCESS_RESTART_CANCELLED, | |
| 62 | |
| 63 // A restart was successfully scheduled. | |
| 64 SUCCESS_RESTART_SCHEDULED, | |
| 65 }; | |
| 66 | |
| 52 static BrowserContextKeyedAPIFactory<RuntimeAPI>* GetFactoryInstance(); | 67 static BrowserContextKeyedAPIFactory<RuntimeAPI>* GetFactoryInstance(); |
| 53 | 68 |
| 54 explicit RuntimeAPI(content::BrowserContext* context); | 69 explicit RuntimeAPI(content::BrowserContext* context); |
| 55 ~RuntimeAPI() override; | 70 ~RuntimeAPI() override; |
| 56 | 71 |
| 57 // content::NotificationObserver overrides: | 72 // content::NotificationObserver overrides: |
| 58 void Observe(int type, | 73 void Observe(int type, |
| 59 const content::NotificationSource& source, | 74 const content::NotificationSource& source, |
| 60 const content::NotificationDetails& details) override; | 75 const content::NotificationDetails& details) override; |
| 61 | 76 |
| 62 void ReloadExtension(const std::string& extension_id); | 77 void ReloadExtension(const std::string& extension_id); |
| 63 bool CheckForUpdates(const std::string& extension_id, | 78 bool CheckForUpdates(const std::string& extension_id, |
| 64 const RuntimeAPIDelegate::UpdateCheckCallback& callback); | 79 const RuntimeAPIDelegate::UpdateCheckCallback& callback); |
| 65 void OpenURL(const GURL& uninstall_url); | 80 void OpenURL(const GURL& uninstall_url); |
| 66 bool GetPlatformInfo(api::runtime::PlatformInfo* info); | 81 bool GetPlatformInfo(api::runtime::PlatformInfo* info); |
| 67 bool RestartDevice(std::string* error_message); | 82 bool RestartDevice(std::string* error_message); |
| 83 | |
| 84 using OnWatchdogTimeoutCallback = | |
| 85 base::Callback<void(bool success, const std::string& message)>; | |
| 86 RestartOnWatchdogStatus RestartDeviceOnWatchdogTimeout( | |
| 87 const std::string& extension_id, | |
| 88 int seconds_from_now, | |
| 89 const OnWatchdogTimeoutCallback& callback); | |
| 90 | |
| 68 bool OpenOptionsPage(const Extension* extension); | 91 bool OpenOptionsPage(const Extension* extension); |
| 69 | 92 |
| 70 private: | 93 private: |
| 71 friend class BrowserContextKeyedAPIFactory<RuntimeAPI>; | 94 friend class BrowserContextKeyedAPIFactory<RuntimeAPI>; |
| 95 friend class RestartOnWatchdogApiTest; | |
| 72 | 96 |
| 73 // ExtensionRegistryObserver implementation. | 97 // ExtensionRegistryObserver implementation. |
| 74 void OnExtensionLoaded(content::BrowserContext* browser_context, | 98 void OnExtensionLoaded(content::BrowserContext* browser_context, |
| 75 const Extension* extension) override; | 99 const Extension* extension) override; |
| 76 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context, | 100 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context, |
| 77 const Extension* extension, | 101 const Extension* extension, |
| 78 bool is_update, | 102 bool is_update, |
| 79 const std::string& old_name) override; | 103 const std::string& old_name) override; |
| 80 void OnExtensionUninstalled(content::BrowserContext* browser_context, | 104 void OnExtensionUninstalled(content::BrowserContext* browser_context, |
| 81 const Extension* extension, | 105 const Extension* extension, |
| 82 UninstallReason reason) override; | 106 UninstallReason reason) override; |
| 83 | 107 |
| 108 // Cancels any previously scheduled restart request, invoking its associated | |
| 109 // JS callback function stored in |current_watchdog_request_callback_|. | |
| 110 void MaybeCancelRunningWatchdogTimer(); | |
| 111 | |
| 112 void ScheduleDelayedRestart(const std::string& extension_id, | |
| 113 const base::Time& now, | |
| 114 int seconds_from_now, | |
| 115 const OnWatchdogTimeoutCallback& callback, | |
| 116 std::unique_ptr<base::Value> stored_last_restart); | |
| 117 | |
| 118 void OnRestartWatchdogTimeout(const std::string& extension_id); | |
|
Devlin
2016/05/20 17:26:32
Function docs
afakhry
2016/05/21 01:13:00
Done.
| |
| 119 | |
| 84 // BrowserContextKeyedAPI implementation: | 120 // BrowserContextKeyedAPI implementation: |
| 85 static const char* service_name() { return "RuntimeAPI"; } | 121 static const char* service_name() { return "RuntimeAPI"; } |
| 86 static const bool kServiceRedirectedInIncognito = true; | 122 static const bool kServiceRedirectedInIncognito = true; |
| 87 static const bool kServiceIsNULLWhileTesting = true; | 123 static const bool kServiceIsNULLWhileTesting = true; |
| 88 void Shutdown() override; | 124 void Shutdown() override; |
| 89 | 125 |
| 90 // extensions::UpdateObserver overrides: | 126 // extensions::UpdateObserver overrides: |
| 91 void OnAppUpdateAvailable(const Extension* extension) override; | 127 void OnAppUpdateAvailable(const Extension* extension) override; |
| 92 void OnChromeUpdateAvailable() override; | 128 void OnChromeUpdateAvailable() override; |
| 93 | 129 |
| 94 // ProcessManagerObserver implementation: | 130 // ProcessManagerObserver implementation: |
| 95 void OnBackgroundHostStartup(const Extension* extension) override; | 131 void OnBackgroundHostStartup(const Extension* extension) override; |
| 96 | 132 |
| 97 // Pref related functions that deals with info about installed extensions that | 133 // Pref related functions that deals with info about installed extensions that |
| 98 // has not been loaded yet. | 134 // has not been loaded yet. |
| 99 // Used to send chrome.runtime.onInstalled event upon loading the extensions. | 135 // Used to send chrome.runtime.onInstalled event upon loading the extensions. |
| 100 bool ReadPendingOnInstallInfoFromPref(const ExtensionId& extension_id, | 136 bool ReadPendingOnInstallInfoFromPref(const ExtensionId& extension_id, |
| 101 base::Version* previous_version); | 137 base::Version* previous_version); |
| 102 void RemovePendingOnInstallInfoFromPref(const ExtensionId& extension_id); | 138 void RemovePendingOnInstallInfoFromPref(const ExtensionId& extension_id); |
| 103 void StorePendingOnInstallInfoToPref(const Extension* extension); | 139 void StorePendingOnInstallInfoToPref(const Extension* extension); |
| 104 | 140 |
| 141 void SetMinDurationBetweenRestartsForTesting(base::TimeDelta delta) { | |
| 142 minimum_duration_between_restarts_ = delta; | |
| 143 } | |
| 144 | |
| 145 void AllowNonKiostAppsInRestartOnWatchdogForTesting(); | |
| 146 | |
| 105 std::unique_ptr<RuntimeAPIDelegate> delegate_; | 147 std::unique_ptr<RuntimeAPIDelegate> delegate_; |
| 106 | 148 |
| 107 content::BrowserContext* browser_context_; | 149 content::BrowserContext* browser_context_; |
| 108 | 150 |
| 109 // True if we should dispatch the chrome.runtime.onInstalled event with | 151 // True if we should dispatch the chrome.runtime.onInstalled event with |
| 110 // reason "chrome_update" upon loading each extension. | 152 // reason "chrome_update" upon loading each extension. |
| 111 bool dispatch_chrome_updated_event_; | 153 bool dispatch_chrome_updated_event_; |
| 112 | 154 |
| 113 content::NotificationRegistrar registrar_; | 155 content::NotificationRegistrar registrar_; |
| 114 | 156 |
| 115 // Listen to extension notifications. | 157 // Listen to extension notifications. |
| 116 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> | 158 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
| 117 extension_registry_observer_; | 159 extension_registry_observer_; |
| 118 ScopedObserver<ProcessManager, ProcessManagerObserver> | 160 ScopedObserver<ProcessManager, ProcessManagerObserver> |
| 119 process_manager_observer_; | 161 process_manager_observer_; |
| 120 | 162 |
| 163 // The ID of the first extension to call the restartOnWatchdog API. Any other | |
| 164 // extensions to call this API after that will fail. | |
| 165 std::string schedule_restart_first_extension_id_; | |
| 166 | |
| 167 // The current JS callback for the current scheduled restart which will be | |
| 168 // invoked when that restart attempt is executed. | |
| 169 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
| |
| 170 | |
| 171 // The timer that will trigger a device restart when it times out. | |
| 172 base::OneShotTimer watchdog_timer_; | |
| 173 | |
| 174 // The minimum allowed duration between two successive restarts on watchdog | |
| 175 // timer. | |
| 176 base::TimeDelta minimum_duration_between_restarts_; | |
| 177 | |
| 178 base::WeakPtrFactory<RuntimeAPI> weak_ptr_factory_; | |
| 179 | |
| 121 DISALLOW_COPY_AND_ASSIGN(RuntimeAPI); | 180 DISALLOW_COPY_AND_ASSIGN(RuntimeAPI); |
| 122 }; | 181 }; |
| 123 | 182 |
| 124 template <> | 183 template <> |
| 125 void BrowserContextKeyedAPIFactory<RuntimeAPI>::DeclareFactoryDependencies(); | 184 void BrowserContextKeyedAPIFactory<RuntimeAPI>::DeclareFactoryDependencies(); |
| 126 | 185 |
| 127 class RuntimeEventRouter { | 186 class RuntimeEventRouter { |
| 128 public: | 187 public: |
| 129 // Dispatches the onStartup event to all currently-loaded extensions. | 188 // Dispatches the onStartup event to all currently-loaded extensions. |
| 130 static void DispatchOnStartupEvent(content::BrowserContext* context, | 189 static void DispatchOnStartupEvent(content::BrowserContext* context, |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 213 | 272 |
| 214 class RuntimeRestartFunction : public UIThreadExtensionFunction { | 273 class RuntimeRestartFunction : public UIThreadExtensionFunction { |
| 215 public: | 274 public: |
| 216 DECLARE_EXTENSION_FUNCTION("runtime.restart", RUNTIME_RESTART) | 275 DECLARE_EXTENSION_FUNCTION("runtime.restart", RUNTIME_RESTART) |
| 217 | 276 |
| 218 protected: | 277 protected: |
| 219 ~RuntimeRestartFunction() override {} | 278 ~RuntimeRestartFunction() override {} |
| 220 ResponseAction Run() override; | 279 ResponseAction Run() override; |
| 221 }; | 280 }; |
| 222 | 281 |
| 282 class RuntimeRestartOnWatchdogFunction : public UIThreadExtensionFunction { | |
| 283 public: | |
| 284 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
| |
| 285 RUNTIME_RESTARTONWATCHDOG) | |
| 286 | |
| 287 protected: | |
| 288 ~RuntimeRestartOnWatchdogFunction() override {} | |
| 289 ResponseAction Run() override; | |
| 290 | |
| 291 private: | |
| 292 void OnWatchdogTimeout(bool success, const std::string& message); | |
| 293 }; | |
| 294 | |
| 223 class RuntimeGetPlatformInfoFunction : public UIThreadExtensionFunction { | 295 class RuntimeGetPlatformInfoFunction : public UIThreadExtensionFunction { |
| 224 public: | 296 public: |
| 225 DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo", | 297 DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo", |
| 226 RUNTIME_GETPLATFORMINFO); | 298 RUNTIME_GETPLATFORMINFO); |
| 227 | 299 |
| 228 protected: | 300 protected: |
| 229 ~RuntimeGetPlatformInfoFunction() override {} | 301 ~RuntimeGetPlatformInfoFunction() override {} |
| 230 ResponseAction Run() override; | 302 ResponseAction Run() override; |
| 231 }; | 303 }; |
| 232 | 304 |
| 233 class RuntimeGetPackageDirectoryEntryFunction | 305 class RuntimeGetPackageDirectoryEntryFunction |
| 234 : public UIThreadExtensionFunction { | 306 : public UIThreadExtensionFunction { |
| 235 public: | 307 public: |
| 236 DECLARE_EXTENSION_FUNCTION("runtime.getPackageDirectoryEntry", | 308 DECLARE_EXTENSION_FUNCTION("runtime.getPackageDirectoryEntry", |
| 237 RUNTIME_GETPACKAGEDIRECTORYENTRY) | 309 RUNTIME_GETPACKAGEDIRECTORYENTRY) |
| 238 | 310 |
| 239 protected: | 311 protected: |
| 240 ~RuntimeGetPackageDirectoryEntryFunction() override {} | 312 ~RuntimeGetPackageDirectoryEntryFunction() override {} |
| 241 ResponseAction Run() override; | 313 ResponseAction Run() override; |
| 242 }; | 314 }; |
| 243 | 315 |
| 244 } // namespace extensions | 316 } // namespace extensions |
| 245 | 317 |
| 246 #endif // EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_ | 318 #endif // EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_ |
| OLD | NEW |