| 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" | |
| 14 #include "content/public/browser/notification_observer.h" | 12 #include "content/public/browser/notification_observer.h" |
| 15 #include "content/public/browser/notification_registrar.h" | 13 #include "content/public/browser/notification_registrar.h" |
| 16 #include "extensions/browser/api/runtime/runtime_api_delegate.h" | 14 #include "extensions/browser/api/runtime/runtime_api_delegate.h" |
| 17 #include "extensions/browser/browser_context_keyed_api_factory.h" | 15 #include "extensions/browser/browser_context_keyed_api_factory.h" |
| 18 #include "extensions/browser/extension_function.h" | 16 #include "extensions/browser/extension_function.h" |
| 19 #include "extensions/browser/extension_registry_observer.h" | 17 #include "extensions/browser/extension_registry_observer.h" |
| 20 #include "extensions/browser/process_manager.h" | 18 #include "extensions/browser/process_manager.h" |
| 21 #include "extensions/browser/process_manager_observer.h" | 19 #include "extensions/browser/process_manager_observer.h" |
| 22 #include "extensions/browser/update_observer.h" | 20 #include "extensions/browser/update_observer.h" |
| 23 #include "extensions/common/api/runtime.h" | 21 #include "extensions/common/api/runtime.h" |
| 24 | 22 |
| 25 namespace base { | 23 namespace base { |
| 26 class Version; | 24 class Version; |
| 27 } | 25 } |
| 28 | 26 |
| 29 namespace content { | 27 namespace content { |
| 30 class BrowserContext; | 28 class BrowserContext; |
| 31 } | 29 } |
| 32 | 30 |
| 33 class PrefRegistrySimple; | |
| 34 | |
| 35 namespace extensions { | 31 namespace extensions { |
| 36 | 32 |
| 37 namespace api { | 33 namespace api { |
| 38 namespace runtime { | 34 namespace runtime { |
| 39 struct PlatformInfo; | 35 struct PlatformInfo; |
| 40 } | 36 } |
| 41 } | 37 } |
| 42 | 38 |
| 43 class Extension; | 39 class Extension; |
| 44 class ExtensionHost; | 40 class ExtensionHost; |
| 45 class ExtensionRegistry; | 41 class ExtensionRegistry; |
| 46 | 42 |
| 47 // Runtime API dispatches onStartup, onInstalled, and similar events to | 43 // Runtime API dispatches onStartup, onInstalled, and similar events to |
| 48 // extensions. There is one instance shared between a browser context and | 44 // extensions. There is one instance shared between a browser context and |
| 49 // its related incognito instance. | 45 // its related incognito instance. |
| 50 class RuntimeAPI : public BrowserContextKeyedAPI, | 46 class RuntimeAPI : public BrowserContextKeyedAPI, |
| 51 public content::NotificationObserver, | 47 public content::NotificationObserver, |
| 52 public ExtensionRegistryObserver, | 48 public ExtensionRegistryObserver, |
| 53 public UpdateObserver, | 49 public UpdateObserver, |
| 54 public ProcessManagerObserver { | 50 public ProcessManagerObserver { |
| 55 public: | 51 public: |
| 56 // The status of the restartAfterDelay request. | |
| 57 enum class RestartAfterDelayStatus { | |
| 58 // The request was made by a different extension other than the first one to | |
| 59 // invoke the restartAfterDelay runtime API. | |
| 60 FAILED_NOT_FIRST_EXTENSION, | |
| 61 | |
| 62 // The request came too soon after a previous restart induced by the | |
| 63 // restartAfterDelay API. It failed to be scheduled as requested, and was | |
| 64 // instead 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 | |
| 74 static BrowserContextKeyedAPIFactory<RuntimeAPI>* GetFactoryInstance(); | 52 static BrowserContextKeyedAPIFactory<RuntimeAPI>* GetFactoryInstance(); |
| 75 | 53 |
| 76 static void RegisterPrefs(PrefRegistrySimple* registry); | |
| 77 | |
| 78 explicit RuntimeAPI(content::BrowserContext* context); | 54 explicit RuntimeAPI(content::BrowserContext* context); |
| 79 ~RuntimeAPI() override; | 55 ~RuntimeAPI() override; |
| 80 | 56 |
| 81 // content::NotificationObserver overrides: | 57 // content::NotificationObserver overrides: |
| 82 void Observe(int type, | 58 void Observe(int type, |
| 83 const content::NotificationSource& source, | 59 const content::NotificationSource& source, |
| 84 const content::NotificationDetails& details) override; | 60 const content::NotificationDetails& details) override; |
| 85 | 61 |
| 86 void ReloadExtension(const std::string& extension_id); | 62 void ReloadExtension(const std::string& extension_id); |
| 87 bool CheckForUpdates(const std::string& extension_id, | 63 bool CheckForUpdates(const std::string& extension_id, |
| 88 const RuntimeAPIDelegate::UpdateCheckCallback& callback); | 64 const RuntimeAPIDelegate::UpdateCheckCallback& callback); |
| 89 void OpenURL(const GURL& uninstall_url); | 65 void OpenURL(const GURL& uninstall_url); |
| 90 bool GetPlatformInfo(api::runtime::PlatformInfo* info); | 66 bool GetPlatformInfo(api::runtime::PlatformInfo* info); |
| 91 bool RestartDevice(std::string* error_message); | 67 bool RestartDevice(std::string* error_message); |
| 92 | |
| 93 RestartAfterDelayStatus RestartDeviceAfterDelay( | |
| 94 const std::string& extension_id, | |
| 95 int seconds_from_now); | |
| 96 | |
| 97 bool OpenOptionsPage(const Extension* extension); | 68 bool OpenOptionsPage(const Extension* extension); |
| 98 | 69 |
| 99 private: | 70 private: |
| 100 friend class BrowserContextKeyedAPIFactory<RuntimeAPI>; | 71 friend class BrowserContextKeyedAPIFactory<RuntimeAPI>; |
| 101 friend class RestartAfterDelayApiTest; | |
| 102 | 72 |
| 103 // ExtensionRegistryObserver implementation. | 73 // ExtensionRegistryObserver implementation. |
| 104 void OnExtensionLoaded(content::BrowserContext* browser_context, | 74 void OnExtensionLoaded(content::BrowserContext* browser_context, |
| 105 const Extension* extension) override; | 75 const Extension* extension) override; |
| 106 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context, | 76 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context, |
| 107 const Extension* extension, | 77 const Extension* extension, |
| 108 bool is_update, | 78 bool is_update, |
| 109 const std::string& old_name) override; | 79 const std::string& old_name) override; |
| 110 void OnExtensionUninstalled(content::BrowserContext* browser_context, | 80 void OnExtensionUninstalled(content::BrowserContext* browser_context, |
| 111 const Extension* extension, | 81 const Extension* extension, |
| 112 UninstallReason reason) override; | 82 UninstallReason reason) override; |
| 113 | 83 |
| 114 // Cancels any previously scheduled restart request. | |
| 115 void MaybeCancelRunningDelayedRestartTimer(); | |
| 116 | |
| 117 RestartAfterDelayStatus ScheduleDelayedRestart(const base::Time& now, | |
| 118 int seconds_from_now); | |
| 119 | |
| 120 // Called when the delayed restart timer times out so that it attempts to | |
| 121 // execute the restart request scheduled earlier. | |
| 122 void OnDelayedRestartTimerTimeout(); | |
| 123 | |
| 124 // BrowserContextKeyedAPI implementation: | 84 // BrowserContextKeyedAPI implementation: |
| 125 static const char* service_name() { return "RuntimeAPI"; } | 85 static const char* service_name() { return "RuntimeAPI"; } |
| 126 static const bool kServiceRedirectedInIncognito = true; | 86 static const bool kServiceRedirectedInIncognito = true; |
| 127 static const bool kServiceIsNULLWhileTesting = true; | 87 static const bool kServiceIsNULLWhileTesting = true; |
| 128 void Shutdown() override; | 88 void Shutdown() override; |
| 129 | 89 |
| 130 // extensions::UpdateObserver overrides: | 90 // extensions::UpdateObserver overrides: |
| 131 void OnAppUpdateAvailable(const Extension* extension) override; | 91 void OnAppUpdateAvailable(const Extension* extension) override; |
| 132 void OnChromeUpdateAvailable() override; | 92 void OnChromeUpdateAvailable() override; |
| 133 | 93 |
| 134 // ProcessManagerObserver implementation: | 94 // ProcessManagerObserver implementation: |
| 135 void OnBackgroundHostStartup(const Extension* extension) override; | 95 void OnBackgroundHostStartup(const Extension* extension) override; |
| 136 | 96 |
| 137 // Pref related functions that deals with info about installed extensions that | 97 // Pref related functions that deals with info about installed extensions that |
| 138 // has not been loaded yet. | 98 // has not been loaded yet. |
| 139 // Used to send chrome.runtime.onInstalled event upon loading the extensions. | 99 // Used to send chrome.runtime.onInstalled event upon loading the extensions. |
| 140 bool ReadPendingOnInstallInfoFromPref(const ExtensionId& extension_id, | 100 bool ReadPendingOnInstallInfoFromPref(const ExtensionId& extension_id, |
| 141 base::Version* previous_version); | 101 base::Version* previous_version); |
| 142 void RemovePendingOnInstallInfoFromPref(const ExtensionId& extension_id); | 102 void RemovePendingOnInstallInfoFromPref(const ExtensionId& extension_id); |
| 143 void StorePendingOnInstallInfoToPref(const Extension* extension); | 103 void StorePendingOnInstallInfoToPref(const Extension* extension); |
| 144 | 104 |
| 145 void AllowNonKioskAppsInRestartAfterDelayForTesting(); | |
| 146 | |
| 147 void set_min_duration_between_restarts_for_testing(base::TimeDelta delta) { | |
| 148 minimum_duration_between_restarts_ = delta; | |
| 149 } | |
| 150 | |
| 151 std::unique_ptr<RuntimeAPIDelegate> delegate_; | 105 std::unique_ptr<RuntimeAPIDelegate> delegate_; |
| 152 | 106 |
| 153 content::BrowserContext* browser_context_; | 107 content::BrowserContext* browser_context_; |
| 154 | 108 |
| 109 // True if we should dispatch the chrome.runtime.onInstalled event with |
| 110 // reason "chrome_update" upon loading each extension. |
| 111 bool dispatch_chrome_updated_event_; |
| 112 |
| 155 content::NotificationRegistrar registrar_; | 113 content::NotificationRegistrar registrar_; |
| 156 | 114 |
| 157 // Listen to extension notifications. | 115 // Listen to extension notifications. |
| 158 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> | 116 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
| 159 extension_registry_observer_; | 117 extension_registry_observer_; |
| 160 ScopedObserver<ProcessManager, ProcessManagerObserver> | 118 ScopedObserver<ProcessManager, ProcessManagerObserver> |
| 161 process_manager_observer_; | 119 process_manager_observer_; |
| 162 | 120 |
| 163 // The ID of the first extension to call the restartAfterDelay 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 restart_after_delay_timer_; | |
| 169 | |
| 170 // The minimum allowed duration between two successive restarts caused by | |
| 171 // restartAfterDelay calls. | |
| 172 base::TimeDelta minimum_duration_between_restarts_; | |
| 173 | |
| 174 // The last restart time which was a result of a successful call to | |
| 175 // chrome.runtime.restartAfterDelay(). | |
| 176 base::Time last_delayed_restart_time_; | |
| 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_; | |
| 181 | |
| 182 bool did_read_delayed_restart_preferences_; | |
| 183 bool was_last_restart_due_to_delayed_restart_api_; | |
| 184 | |
| 185 base::WeakPtrFactory<RuntimeAPI> weak_ptr_factory_; | |
| 186 | |
| 187 DISALLOW_COPY_AND_ASSIGN(RuntimeAPI); | 121 DISALLOW_COPY_AND_ASSIGN(RuntimeAPI); |
| 188 }; | 122 }; |
| 189 | 123 |
| 190 template <> | 124 template <> |
| 191 void BrowserContextKeyedAPIFactory<RuntimeAPI>::DeclareFactoryDependencies(); | 125 void BrowserContextKeyedAPIFactory<RuntimeAPI>::DeclareFactoryDependencies(); |
| 192 | 126 |
| 193 class RuntimeEventRouter { | 127 class RuntimeEventRouter { |
| 194 public: | 128 public: |
| 195 // Dispatches the onStartup event to all currently-loaded extensions. | 129 // Dispatches the onStartup event to all currently-loaded extensions. |
| 196 static void DispatchOnStartupEvent(content::BrowserContext* context, | 130 static void DispatchOnStartupEvent(content::BrowserContext* context, |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 279 | 213 |
| 280 class RuntimeRestartFunction : public UIThreadExtensionFunction { | 214 class RuntimeRestartFunction : public UIThreadExtensionFunction { |
| 281 public: | 215 public: |
| 282 DECLARE_EXTENSION_FUNCTION("runtime.restart", RUNTIME_RESTART) | 216 DECLARE_EXTENSION_FUNCTION("runtime.restart", RUNTIME_RESTART) |
| 283 | 217 |
| 284 protected: | 218 protected: |
| 285 ~RuntimeRestartFunction() override {} | 219 ~RuntimeRestartFunction() override {} |
| 286 ResponseAction Run() override; | 220 ResponseAction Run() override; |
| 287 }; | 221 }; |
| 288 | 222 |
| 289 class RuntimeRestartAfterDelayFunction : public UIThreadExtensionFunction { | |
| 290 public: | |
| 291 DECLARE_EXTENSION_FUNCTION("runtime.restartAfterDelay", | |
| 292 RUNTIME_RESTARTAFTERDELAY) | |
| 293 | |
| 294 protected: | |
| 295 ~RuntimeRestartAfterDelayFunction() override {} | |
| 296 ResponseAction Run() override; | |
| 297 }; | |
| 298 | |
| 299 class RuntimeGetPlatformInfoFunction : public UIThreadExtensionFunction { | 223 class RuntimeGetPlatformInfoFunction : public UIThreadExtensionFunction { |
| 300 public: | 224 public: |
| 301 DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo", | 225 DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo", |
| 302 RUNTIME_GETPLATFORMINFO); | 226 RUNTIME_GETPLATFORMINFO); |
| 303 | 227 |
| 304 protected: | 228 protected: |
| 305 ~RuntimeGetPlatformInfoFunction() override {} | 229 ~RuntimeGetPlatformInfoFunction() override {} |
| 306 ResponseAction Run() override; | 230 ResponseAction Run() override; |
| 307 }; | 231 }; |
| 308 | 232 |
| 309 class RuntimeGetPackageDirectoryEntryFunction | 233 class RuntimeGetPackageDirectoryEntryFunction |
| 310 : public UIThreadExtensionFunction { | 234 : public UIThreadExtensionFunction { |
| 311 public: | 235 public: |
| 312 DECLARE_EXTENSION_FUNCTION("runtime.getPackageDirectoryEntry", | 236 DECLARE_EXTENSION_FUNCTION("runtime.getPackageDirectoryEntry", |
| 313 RUNTIME_GETPACKAGEDIRECTORYENTRY) | 237 RUNTIME_GETPACKAGEDIRECTORYENTRY) |
| 314 | 238 |
| 315 protected: | 239 protected: |
| 316 ~RuntimeGetPackageDirectoryEntryFunction() override {} | 240 ~RuntimeGetPackageDirectoryEntryFunction() override {} |
| 317 ResponseAction Run() override; | 241 ResponseAction Run() override; |
| 318 }; | 242 }; |
| 319 | 243 |
| 320 } // namespace extensions | 244 } // namespace extensions |
| 321 | 245 |
| 322 #endif // EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_ | 246 #endif // EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_ |
| OLD | NEW |