| 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 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 |
| 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 RestartAfterDelayStatus RestartDeviceAfterDelay( |
| 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 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 |
| 84 // BrowserContextKeyedAPI implementation: | 124 // BrowserContextKeyedAPI implementation: |
| 85 static const char* service_name() { return "RuntimeAPI"; } | 125 static const char* service_name() { return "RuntimeAPI"; } |
| 86 static const bool kServiceRedirectedInIncognito = true; | 126 static const bool kServiceRedirectedInIncognito = true; |
| 87 static const bool kServiceIsNULLWhileTesting = true; | 127 static const bool kServiceIsNULLWhileTesting = true; |
| 88 void Shutdown() override; | 128 void Shutdown() override; |
| 89 | 129 |
| 90 // extensions::UpdateObserver overrides: | 130 // extensions::UpdateObserver overrides: |
| 91 void OnAppUpdateAvailable(const Extension* extension) override; | 131 void OnAppUpdateAvailable(const Extension* extension) override; |
| 92 void OnChromeUpdateAvailable() override; | 132 void OnChromeUpdateAvailable() override; |
| 93 | 133 |
| 94 // ProcessManagerObserver implementation: | 134 // ProcessManagerObserver implementation: |
| 95 void OnBackgroundHostStartup(const Extension* extension) override; | 135 void OnBackgroundHostStartup(const Extension* extension) override; |
| 96 | 136 |
| 97 // Pref related functions that deals with info about installed extensions that | 137 // Pref related functions that deals with info about installed extensions that |
| 98 // has not been loaded yet. | 138 // has not been loaded yet. |
| 99 // Used to send chrome.runtime.onInstalled event upon loading the extensions. | 139 // Used to send chrome.runtime.onInstalled event upon loading the extensions. |
| 100 bool ReadPendingOnInstallInfoFromPref(const ExtensionId& extension_id, | 140 bool ReadPendingOnInstallInfoFromPref(const ExtensionId& extension_id, |
| 101 base::Version* previous_version); | 141 base::Version* previous_version); |
| 102 void RemovePendingOnInstallInfoFromPref(const ExtensionId& extension_id); | 142 void RemovePendingOnInstallInfoFromPref(const ExtensionId& extension_id); |
| 103 void StorePendingOnInstallInfoToPref(const Extension* extension); | 143 void StorePendingOnInstallInfoToPref(const Extension* extension); |
| 104 | 144 |
| 145 void AllowNonKioskAppsInRestartAfterDelayForTesting(); |
| 146 |
| 147 void set_min_duration_between_restarts_for_testing(base::TimeDelta delta) { |
| 148 minimum_duration_between_restarts_ = delta; |
| 149 } |
| 150 |
| 105 std::unique_ptr<RuntimeAPIDelegate> delegate_; | 151 std::unique_ptr<RuntimeAPIDelegate> delegate_; |
| 106 | 152 |
| 107 content::BrowserContext* browser_context_; | 153 content::BrowserContext* browser_context_; |
| 108 | 154 |
| 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 | |
| 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 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 |
| 121 DISALLOW_COPY_AND_ASSIGN(RuntimeAPI); | 187 DISALLOW_COPY_AND_ASSIGN(RuntimeAPI); |
| 122 }; | 188 }; |
| 123 | 189 |
| 124 template <> | 190 template <> |
| 125 void BrowserContextKeyedAPIFactory<RuntimeAPI>::DeclareFactoryDependencies(); | 191 void BrowserContextKeyedAPIFactory<RuntimeAPI>::DeclareFactoryDependencies(); |
| 126 | 192 |
| 127 class RuntimeEventRouter { | 193 class RuntimeEventRouter { |
| 128 public: | 194 public: |
| 129 // Dispatches the onStartup event to all currently-loaded extensions. | 195 // Dispatches the onStartup event to all currently-loaded extensions. |
| 130 static void DispatchOnStartupEvent(content::BrowserContext* context, | 196 static void DispatchOnStartupEvent(content::BrowserContext* context, |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 | 279 |
| 214 class RuntimeRestartFunction : public UIThreadExtensionFunction { | 280 class RuntimeRestartFunction : public UIThreadExtensionFunction { |
| 215 public: | 281 public: |
| 216 DECLARE_EXTENSION_FUNCTION("runtime.restart", RUNTIME_RESTART) | 282 DECLARE_EXTENSION_FUNCTION("runtime.restart", RUNTIME_RESTART) |
| 217 | 283 |
| 218 protected: | 284 protected: |
| 219 ~RuntimeRestartFunction() override {} | 285 ~RuntimeRestartFunction() override {} |
| 220 ResponseAction Run() override; | 286 ResponseAction Run() override; |
| 221 }; | 287 }; |
| 222 | 288 |
| 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 |
| 223 class RuntimeGetPlatformInfoFunction : public UIThreadExtensionFunction { | 299 class RuntimeGetPlatformInfoFunction : public UIThreadExtensionFunction { |
| 224 public: | 300 public: |
| 225 DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo", | 301 DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo", |
| 226 RUNTIME_GETPLATFORMINFO); | 302 RUNTIME_GETPLATFORMINFO); |
| 227 | 303 |
| 228 protected: | 304 protected: |
| 229 ~RuntimeGetPlatformInfoFunction() override {} | 305 ~RuntimeGetPlatformInfoFunction() override {} |
| 230 ResponseAction Run() override; | 306 ResponseAction Run() override; |
| 231 }; | 307 }; |
| 232 | 308 |
| 233 class RuntimeGetPackageDirectoryEntryFunction | 309 class RuntimeGetPackageDirectoryEntryFunction |
| 234 : public UIThreadExtensionFunction { | 310 : public UIThreadExtensionFunction { |
| 235 public: | 311 public: |
| 236 DECLARE_EXTENSION_FUNCTION("runtime.getPackageDirectoryEntry", | 312 DECLARE_EXTENSION_FUNCTION("runtime.getPackageDirectoryEntry", |
| 237 RUNTIME_GETPACKAGEDIRECTORYENTRY) | 313 RUNTIME_GETPACKAGEDIRECTORYENTRY) |
| 238 | 314 |
| 239 protected: | 315 protected: |
| 240 ~RuntimeGetPackageDirectoryEntryFunction() override {} | 316 ~RuntimeGetPackageDirectoryEntryFunction() override {} |
| 241 ResponseAction Run() override; | 317 ResponseAction Run() override; |
| 242 }; | 318 }; |
| 243 | 319 |
| 244 } // namespace extensions | 320 } // namespace extensions |
| 245 | 321 |
| 246 #endif // EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_ | 322 #endif // EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_ |
| OLD | NEW |