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 // The request came too soon after a previous restart induced by the | |
| 63 // watchdog API. It failed to be scheduled as requested, and was instead | |
| 64 // throttled. | |
| 65 FAILED_THROTTLED, | |
| 66 | |
| 67 // Any previously scheduled restart was successfully canceled. | |
| 68 SUCCESS_RESTART_CANCELED, | |
| 69 | |
| 70 // A restart was successfully scheduled. | |
| 71 SUCCESS_RESTART_SCHEDULED, | |
| 72 }; | |
| 73 | |
| 52 static BrowserContextKeyedAPIFactory<RuntimeAPI>* GetFactoryInstance(); | 74 static BrowserContextKeyedAPIFactory<RuntimeAPI>* GetFactoryInstance(); |
| 53 | 75 |
| 76 static void RegisterPrefs(PrefRegistrySimple* registry); | |
| 77 | |
| 54 explicit RuntimeAPI(content::BrowserContext* context); | 78 explicit RuntimeAPI(content::BrowserContext* context); |
| 55 ~RuntimeAPI() override; | 79 ~RuntimeAPI() override; |
| 56 | 80 |
| 57 // content::NotificationObserver overrides: | 81 // content::NotificationObserver overrides: |
| 58 void Observe(int type, | 82 void Observe(int type, |
| 59 const content::NotificationSource& source, | 83 const content::NotificationSource& source, |
| 60 const content::NotificationDetails& details) override; | 84 const content::NotificationDetails& details) override; |
| 61 | 85 |
| 62 void ReloadExtension(const std::string& extension_id); | 86 void ReloadExtension(const std::string& extension_id); |
| 63 bool CheckForUpdates(const std::string& extension_id, | 87 bool CheckForUpdates(const std::string& extension_id, |
| 64 const RuntimeAPIDelegate::UpdateCheckCallback& callback); | 88 const RuntimeAPIDelegate::UpdateCheckCallback& callback); |
| 65 void OpenURL(const GURL& uninstall_url); | 89 void OpenURL(const GURL& uninstall_url); |
| 66 bool GetPlatformInfo(api::runtime::PlatformInfo* info); | 90 bool GetPlatformInfo(api::runtime::PlatformInfo* info); |
| 67 bool RestartDevice(std::string* error_message); | 91 bool RestartDevice(std::string* error_message); |
| 92 | |
| 93 RestartOnWatchdogStatus RestartDeviceOnWatchdogTimeout( | |
| 94 const std::string& extension_id, | |
| 95 int seconds_from_now); | |
| 96 | |
| 68 bool OpenOptionsPage(const Extension* extension); | 97 bool OpenOptionsPage(const Extension* extension); |
| 69 | 98 |
| 70 private: | 99 private: |
| 71 friend class BrowserContextKeyedAPIFactory<RuntimeAPI>; | 100 friend class BrowserContextKeyedAPIFactory<RuntimeAPI>; |
| 101 friend class RestartAfterDelayApiTest; | |
| 72 | 102 |
| 73 // ExtensionRegistryObserver implementation. | 103 // ExtensionRegistryObserver implementation. |
| 74 void OnExtensionLoaded(content::BrowserContext* browser_context, | 104 void OnExtensionLoaded(content::BrowserContext* browser_context, |
| 75 const Extension* extension) override; | 105 const Extension* extension) override; |
| 76 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context, | 106 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context, |
| 77 const Extension* extension, | 107 const Extension* extension, |
| 78 bool is_update, | 108 bool is_update, |
| 79 const std::string& old_name) override; | 109 const std::string& old_name) override; |
| 80 void OnExtensionUninstalled(content::BrowserContext* browser_context, | 110 void OnExtensionUninstalled(content::BrowserContext* browser_context, |
| 81 const Extension* extension, | 111 const Extension* extension, |
| 82 UninstallReason reason) override; | 112 UninstallReason reason) override; |
| 83 | 113 |
| 114 // Cancels any previously scheduled restart request. | |
| 115 void MaybeCancelRunningWatchdogTimer(); | |
| 116 | |
| 117 RestartOnWatchdogStatus ScheduleDelayedRestart(const base::Time& now, | |
| 118 int seconds_from_now, | |
| 119 double stored_last_restart); | |
| 120 | |
| 121 // Called when the restart watchdog timer times out so that it attempts to | |
| 122 // execute the restart request scheduled earlier. | |
| 123 void OnRestartWatchdogTimeout(); | |
| 124 | |
| 125 // Called when the host device is being shutdown or restarted. | |
| 126 void OnDeviceShutdown(); | |
| 127 | |
| 84 // BrowserContextKeyedAPI implementation: | 128 // BrowserContextKeyedAPI implementation: |
| 85 static const char* service_name() { return "RuntimeAPI"; } | 129 static const char* service_name() { return "RuntimeAPI"; } |
| 86 static const bool kServiceRedirectedInIncognito = true; | 130 static const bool kServiceRedirectedInIncognito = true; |
| 87 static const bool kServiceIsNULLWhileTesting = true; | 131 static const bool kServiceIsNULLWhileTesting = true; |
| 88 void Shutdown() override; | 132 void Shutdown() override; |
| 89 | 133 |
| 90 // extensions::UpdateObserver overrides: | 134 // extensions::UpdateObserver overrides: |
| 91 void OnAppUpdateAvailable(const Extension* extension) override; | 135 void OnAppUpdateAvailable(const Extension* extension) override; |
| 92 void OnChromeUpdateAvailable() override; | 136 void OnChromeUpdateAvailable() override; |
| 93 | 137 |
| 94 // ProcessManagerObserver implementation: | 138 // ProcessManagerObserver implementation: |
| 95 void OnBackgroundHostStartup(const Extension* extension) override; | 139 void OnBackgroundHostStartup(const Extension* extension) override; |
| 96 | 140 |
| 97 // Pref related functions that deals with info about installed extensions that | 141 // Pref related functions that deals with info about installed extensions that |
| 98 // has not been loaded yet. | 142 // has not been loaded yet. |
| 99 // Used to send chrome.runtime.onInstalled event upon loading the extensions. | 143 // Used to send chrome.runtime.onInstalled event upon loading the extensions. |
| 100 bool ReadPendingOnInstallInfoFromPref(const ExtensionId& extension_id, | 144 bool ReadPendingOnInstallInfoFromPref(const ExtensionId& extension_id, |
| 101 base::Version* previous_version); | 145 base::Version* previous_version); |
| 102 void RemovePendingOnInstallInfoFromPref(const ExtensionId& extension_id); | 146 void RemovePendingOnInstallInfoFromPref(const ExtensionId& extension_id); |
| 103 void StorePendingOnInstallInfoToPref(const Extension* extension); | 147 void StorePendingOnInstallInfoToPref(const Extension* extension); |
| 104 | 148 |
| 149 void AllowNonKiostAppsInRestartOnWatchdogForTesting(); | |
| 150 | |
| 151 void set_min_duration_between_restarts_for_testing(base::TimeDelta delta) { | |
| 152 minimum_duration_between_restarts_ = delta; | |
| 153 } | |
| 154 | |
| 105 std::unique_ptr<RuntimeAPIDelegate> delegate_; | 155 std::unique_ptr<RuntimeAPIDelegate> delegate_; |
| 106 | 156 |
| 107 content::BrowserContext* browser_context_; | 157 content::BrowserContext* browser_context_; |
| 108 | 158 |
| 109 // True if we should dispatch the chrome.runtime.onInstalled event with | |
|
Devlin
2016/06/10 00:34:45
What's the reason for moving this?
afakhry
2016/06/13 14:46:00
I'm sure you know this, and probably would think i
Devlin
2016/06/13 15:54:57
Realistically, since this is a singleton per profi
| |
| 110 // reason "chrome_update" upon loading each extension. | |
| 111 bool dispatch_chrome_updated_event_; | |
| 112 | |
| 113 content::NotificationRegistrar registrar_; | 159 content::NotificationRegistrar registrar_; |
| 114 | 160 |
| 115 // Listen to extension notifications. | 161 // Listen to extension notifications. |
| 116 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> | 162 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
| 117 extension_registry_observer_; | 163 extension_registry_observer_; |
| 118 ScopedObserver<ProcessManager, ProcessManagerObserver> | 164 ScopedObserver<ProcessManager, ProcessManagerObserver> |
| 119 process_manager_observer_; | 165 process_manager_observer_; |
| 120 | 166 |
| 167 // The ID of the first extension to call the restartOnWatchdog API. Any other | |
| 168 // extensions to call this API after that will fail. | |
| 169 std::string schedule_restart_first_extension_id_; | |
| 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 // True if we should dispatch the chrome.runtime.onInstalled event with | |
| 179 // reason "chrome_update" upon loading each extension. | |
| 180 bool dispatch_chrome_updated_event_ = false; | |
|
Devlin
2016/06/10 00:34:45
Let's not mix initialization styles. Since we ini
afakhry
2016/06/13 14:46:00
Done.
| |
| 181 | |
| 182 // The currently expected restarts induced by this API. | |
| 183 // chrome.runtime.restartAfterDelay() introduces a watchdog expected restart. | |
| 184 // chrome.runtime.restart() introduces a non-watchdog expected restart. | |
| 185 bool expecting_watchdog_restart_ = false; | |
| 186 bool expecting_non_watchdog_restart_ = false; | |
| 187 | |
| 188 base::WeakPtrFactory<RuntimeAPI> weak_ptr_factory_; | |
| 189 | |
| 121 DISALLOW_COPY_AND_ASSIGN(RuntimeAPI); | 190 DISALLOW_COPY_AND_ASSIGN(RuntimeAPI); |
| 122 }; | 191 }; |
| 123 | 192 |
| 124 template <> | 193 template <> |
| 125 void BrowserContextKeyedAPIFactory<RuntimeAPI>::DeclareFactoryDependencies(); | 194 void BrowserContextKeyedAPIFactory<RuntimeAPI>::DeclareFactoryDependencies(); |
| 126 | 195 |
| 127 class RuntimeEventRouter { | 196 class RuntimeEventRouter { |
| 128 public: | 197 public: |
| 129 // Dispatches the onStartup event to all currently-loaded extensions. | 198 // Dispatches the onStartup event to all currently-loaded extensions. |
| 130 static void DispatchOnStartupEvent(content::BrowserContext* context, | 199 static void DispatchOnStartupEvent(content::BrowserContext* context, |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 213 | 282 |
| 214 class RuntimeRestartFunction : public UIThreadExtensionFunction { | 283 class RuntimeRestartFunction : public UIThreadExtensionFunction { |
| 215 public: | 284 public: |
| 216 DECLARE_EXTENSION_FUNCTION("runtime.restart", RUNTIME_RESTART) | 285 DECLARE_EXTENSION_FUNCTION("runtime.restart", RUNTIME_RESTART) |
| 217 | 286 |
| 218 protected: | 287 protected: |
| 219 ~RuntimeRestartFunction() override {} | 288 ~RuntimeRestartFunction() override {} |
| 220 ResponseAction Run() override; | 289 ResponseAction Run() override; |
| 221 }; | 290 }; |
| 222 | 291 |
| 292 class RuntimeRestartAfterDelayFunction : public UIThreadExtensionFunction { | |
| 293 public: | |
| 294 DECLARE_EXTENSION_FUNCTION("runtime.restartAfterDelay", | |
| 295 RUNTIME_RESTARTAFTERDELAY) | |
| 296 | |
| 297 protected: | |
| 298 ~RuntimeRestartAfterDelayFunction() override {} | |
| 299 ResponseAction Run() override; | |
| 300 }; | |
| 301 | |
| 223 class RuntimeGetPlatformInfoFunction : public UIThreadExtensionFunction { | 302 class RuntimeGetPlatformInfoFunction : public UIThreadExtensionFunction { |
| 224 public: | 303 public: |
| 225 DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo", | 304 DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo", |
| 226 RUNTIME_GETPLATFORMINFO); | 305 RUNTIME_GETPLATFORMINFO); |
| 227 | 306 |
| 228 protected: | 307 protected: |
| 229 ~RuntimeGetPlatformInfoFunction() override {} | 308 ~RuntimeGetPlatformInfoFunction() override {} |
| 230 ResponseAction Run() override; | 309 ResponseAction Run() override; |
| 231 }; | 310 }; |
| 232 | 311 |
| 233 class RuntimeGetPackageDirectoryEntryFunction | 312 class RuntimeGetPackageDirectoryEntryFunction |
| 234 : public UIThreadExtensionFunction { | 313 : public UIThreadExtensionFunction { |
| 235 public: | 314 public: |
| 236 DECLARE_EXTENSION_FUNCTION("runtime.getPackageDirectoryEntry", | 315 DECLARE_EXTENSION_FUNCTION("runtime.getPackageDirectoryEntry", |
| 237 RUNTIME_GETPACKAGEDIRECTORYENTRY) | 316 RUNTIME_GETPACKAGEDIRECTORYENTRY) |
| 238 | 317 |
| 239 protected: | 318 protected: |
| 240 ~RuntimeGetPackageDirectoryEntryFunction() override {} | 319 ~RuntimeGetPackageDirectoryEntryFunction() override {} |
| 241 ResponseAction Run() override; | 320 ResponseAction Run() override; |
| 242 }; | 321 }; |
| 243 | 322 |
| 244 } // namespace extensions | 323 } // namespace extensions |
| 245 | 324 |
| 246 #endif // EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_ | 325 #endif // EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_ |
| OLD | NEW |