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" |
| 22 | 24 |
| 23 namespace base { | 25 namespace base { |
| 24 class Version; | 26 class Version; |
| 25 } | 27 } |
| 26 | 28 |
| 27 namespace content { | 29 namespace content { |
| 28 class BrowserContext; | 30 class BrowserContext; |
| 29 } | 31 } |
| 30 | 32 |
| 33 class PrefRegistrySimple; | |
| 34 | |
| 31 namespace extensions { | 35 namespace extensions { |
| 32 | 36 |
| 33 namespace api { | 37 namespace api { |
| 34 namespace runtime { | 38 namespace runtime { |
| 35 struct PlatformInfo; | 39 struct PlatformInfo; |
| 36 } | 40 } |
| 37 } | 41 } |
| 38 | 42 |
| 39 class Extension; | 43 class Extension; |
| 40 class ExtensionHost; | 44 class ExtensionHost; |
| 41 class ExtensionRegistry; | 45 class ExtensionRegistry; |
| 42 | 46 |
| 43 // Runtime API dispatches onStartup, onInstalled, and similar events to | 47 // Runtime API dispatches onStartup, onInstalled, and similar events to |
| 44 // extensions. There is one instance shared between a browser context and | 48 // extensions. There is one instance shared between a browser context and |
| 45 // its related incognito instance. | 49 // its related incognito instance. |
| 46 class RuntimeAPI : public BrowserContextKeyedAPI, | 50 class RuntimeAPI : public BrowserContextKeyedAPI, |
| 47 public content::NotificationObserver, | 51 public content::NotificationObserver, |
| 48 public ExtensionRegistryObserver, | 52 public ExtensionRegistryObserver, |
| 49 public UpdateObserver, | 53 public UpdateObserver, |
| 50 public ProcessManagerObserver { | 54 public ProcessManagerObserver { |
| 51 public: | 55 public: |
| 56 // The status of the restart on watchdog request. | |
| 57 enum class RestartOnWatchdogStatus { | |
| 58 // The request was made by a different extension other than the first one to | |
| 59 // invoke the restartOnWatchdog runtime API. | |
| 60 FAILED_NOT_FIRST_EXTENSION, | |
| 61 | |
| 62 // Any previously scheduled restart was successfully canceled. | |
| 63 SUCCESS_RESTART_CANCELLED, | |
|
Devlin
2016/06/03 21:26:47
cancelled vs canceled. Typically, we use american
afakhry
2016/06/03 23:06:41
Done.
| |
| 64 | |
| 65 // A restart was successfully scheduled. | |
| 66 SUCCESS_RESTART_SCHEDULED, | |
| 67 }; | |
| 68 | |
| 52 static BrowserContextKeyedAPIFactory<RuntimeAPI>* GetFactoryInstance(); | 69 static BrowserContextKeyedAPIFactory<RuntimeAPI>* GetFactoryInstance(); |
| 53 | 70 |
| 71 static void RegisterPrefs(PrefRegistrySimple* registry); | |
| 72 | |
| 54 explicit RuntimeAPI(content::BrowserContext* context); | 73 explicit RuntimeAPI(content::BrowserContext* context); |
| 55 ~RuntimeAPI() override; | 74 ~RuntimeAPI() override; |
| 56 | 75 |
| 57 // content::NotificationObserver overrides: | 76 // content::NotificationObserver overrides: |
| 58 void Observe(int type, | 77 void Observe(int type, |
| 59 const content::NotificationSource& source, | 78 const content::NotificationSource& source, |
| 60 const content::NotificationDetails& details) override; | 79 const content::NotificationDetails& details) override; |
| 61 | 80 |
| 62 void ReloadExtension(const std::string& extension_id); | 81 void ReloadExtension(const std::string& extension_id); |
| 63 bool CheckForUpdates(const std::string& extension_id, | 82 bool CheckForUpdates(const std::string& extension_id, |
| 64 const RuntimeAPIDelegate::UpdateCheckCallback& callback); | 83 const RuntimeAPIDelegate::UpdateCheckCallback& callback); |
| 65 void OpenURL(const GURL& uninstall_url); | 84 void OpenURL(const GURL& uninstall_url); |
| 66 bool GetPlatformInfo(api::runtime::PlatformInfo* info); | 85 bool GetPlatformInfo(api::runtime::PlatformInfo* info); |
| 67 bool RestartDevice(std::string* error_message); | 86 bool RestartDevice(std::string* error_message); |
| 87 | |
| 88 RestartOnWatchdogStatus RestartDeviceOnWatchdogTimeout( | |
| 89 const std::string& extension_id, | |
| 90 int seconds_from_now); | |
| 91 | |
| 68 bool OpenOptionsPage(const Extension* extension); | 92 bool OpenOptionsPage(const Extension* extension); |
| 69 | 93 |
| 70 private: | 94 private: |
| 71 friend class BrowserContextKeyedAPIFactory<RuntimeAPI>; | 95 friend class BrowserContextKeyedAPIFactory<RuntimeAPI>; |
| 96 friend class RestartAfterDelayApiTest; | |
| 72 | 97 |
| 73 // ExtensionRegistryObserver implementation. | 98 // ExtensionRegistryObserver implementation. |
| 74 void OnExtensionLoaded(content::BrowserContext* browser_context, | 99 void OnExtensionLoaded(content::BrowserContext* browser_context, |
| 75 const Extension* extension) override; | 100 const Extension* extension) override; |
| 76 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context, | 101 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context, |
| 77 const Extension* extension, | 102 const Extension* extension, |
| 78 bool is_update, | 103 bool is_update, |
| 79 const std::string& old_name) override; | 104 const std::string& old_name) override; |
| 80 void OnExtensionUninstalled(content::BrowserContext* browser_context, | 105 void OnExtensionUninstalled(content::BrowserContext* browser_context, |
| 81 const Extension* extension, | 106 const Extension* extension, |
| 82 UninstallReason reason) override; | 107 UninstallReason reason) override; |
| 83 | 108 |
| 109 // Cancels any previously scheduled restart request. | |
| 110 void MaybeCancelRunningWatchdogTimer(); | |
| 111 | |
| 112 void ScheduleDelayedRestart(const base::Time& now, | |
| 113 int seconds_from_now, | |
| 114 double stored_last_restart); | |
| 115 | |
| 116 // Called when the restart watchdog timer times out so that it attempts to | |
| 117 // execute the restart request scheduled earlier. | |
| 118 void OnRestartWatchdogTimeout(); | |
| 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 AllowNonKiostAppsInRestartOnWatchdogForTesting(); | |
| 142 | |
| 143 void set_min_duration_between_restarts_for_testing(base::TimeDelta delta) { | |
| 144 minimum_duration_between_restarts_ = delta; | |
| 145 } | |
| 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 timer that will trigger a device restart when it times out. | |
| 168 base::OneShotTimer watchdog_timer_; | |
| 169 | |
| 170 // The minimum allowed duration between two successive restarts on watchdog | |
| 171 // timer. | |
| 172 base::TimeDelta minimum_duration_between_restarts_; | |
| 173 | |
| 174 base::WeakPtrFactory<RuntimeAPI> weak_ptr_factory_; | |
| 175 | |
| 121 DISALLOW_COPY_AND_ASSIGN(RuntimeAPI); | 176 DISALLOW_COPY_AND_ASSIGN(RuntimeAPI); |
| 122 }; | 177 }; |
| 123 | 178 |
| 124 template <> | 179 template <> |
| 125 void BrowserContextKeyedAPIFactory<RuntimeAPI>::DeclareFactoryDependencies(); | 180 void BrowserContextKeyedAPIFactory<RuntimeAPI>::DeclareFactoryDependencies(); |
| 126 | 181 |
| 127 class RuntimeEventRouter { | 182 class RuntimeEventRouter { |
| 128 public: | 183 public: |
| 129 // Dispatches the onStartup event to all currently-loaded extensions. | 184 // Dispatches the onStartup event to all currently-loaded extensions. |
| 130 static void DispatchOnStartupEvent(content::BrowserContext* context, | 185 static void DispatchOnStartupEvent(content::BrowserContext* context, |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 213 | 268 |
| 214 class RuntimeRestartFunction : public UIThreadExtensionFunction { | 269 class RuntimeRestartFunction : public UIThreadExtensionFunction { |
| 215 public: | 270 public: |
| 216 DECLARE_EXTENSION_FUNCTION("runtime.restart", RUNTIME_RESTART) | 271 DECLARE_EXTENSION_FUNCTION("runtime.restart", RUNTIME_RESTART) |
| 217 | 272 |
| 218 protected: | 273 protected: |
| 219 ~RuntimeRestartFunction() override {} | 274 ~RuntimeRestartFunction() override {} |
| 220 ResponseAction Run() override; | 275 ResponseAction Run() override; |
| 221 }; | 276 }; |
| 222 | 277 |
| 278 class RuntimeRestartAfterDelayFunction : public UIThreadExtensionFunction { | |
| 279 public: | |
| 280 DECLARE_EXTENSION_FUNCTION("runtime.restartAfterDelay", | |
| 281 RUNTIME_RESTARTAFTERDELAY) | |
| 282 | |
| 283 protected: | |
| 284 ~RuntimeRestartAfterDelayFunction() override {} | |
| 285 ResponseAction Run() override; | |
| 286 }; | |
| 287 | |
| 223 class RuntimeGetPlatformInfoFunction : public UIThreadExtensionFunction { | 288 class RuntimeGetPlatformInfoFunction : public UIThreadExtensionFunction { |
| 224 public: | 289 public: |
| 225 DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo", | 290 DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo", |
| 226 RUNTIME_GETPLATFORMINFO); | 291 RUNTIME_GETPLATFORMINFO); |
| 227 | 292 |
| 228 protected: | 293 protected: |
| 229 ~RuntimeGetPlatformInfoFunction() override {} | 294 ~RuntimeGetPlatformInfoFunction() override {} |
| 230 ResponseAction Run() override; | 295 ResponseAction Run() override; |
| 231 }; | 296 }; |
| 232 | 297 |
| 233 class RuntimeGetPackageDirectoryEntryFunction | 298 class RuntimeGetPackageDirectoryEntryFunction |
| 234 : public UIThreadExtensionFunction { | 299 : public UIThreadExtensionFunction { |
| 235 public: | 300 public: |
| 236 DECLARE_EXTENSION_FUNCTION("runtime.getPackageDirectoryEntry", | 301 DECLARE_EXTENSION_FUNCTION("runtime.getPackageDirectoryEntry", |
| 237 RUNTIME_GETPACKAGEDIRECTORYENTRY) | 302 RUNTIME_GETPACKAGEDIRECTORYENTRY) |
| 238 | 303 |
| 239 protected: | 304 protected: |
| 240 ~RuntimeGetPackageDirectoryEntryFunction() override {} | 305 ~RuntimeGetPackageDirectoryEntryFunction() override {} |
| 241 ResponseAction Run() override; | 306 ResponseAction Run() override; |
| 242 }; | 307 }; |
| 243 | 308 |
| 244 } // namespace extensions | 309 } // namespace extensions |
| 245 | 310 |
| 246 #endif // EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_ | 311 #endif // EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_ |
| OLD | NEW |