| 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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 void Observe(int type, | 60 void Observe(int type, |
| 59 const content::NotificationSource& source, | 61 const content::NotificationSource& source, |
| 60 const content::NotificationDetails& details) override; | 62 const content::NotificationDetails& details) override; |
| 61 | 63 |
| 62 void ReloadExtension(const std::string& extension_id); | 64 void ReloadExtension(const std::string& extension_id); |
| 63 bool CheckForUpdates(const std::string& extension_id, | 65 bool CheckForUpdates(const std::string& extension_id, |
| 64 const RuntimeAPIDelegate::UpdateCheckCallback& callback); | 66 const RuntimeAPIDelegate::UpdateCheckCallback& callback); |
| 65 void OpenURL(const GURL& uninstall_url); | 67 void OpenURL(const GURL& uninstall_url); |
| 66 bool GetPlatformInfo(api::runtime::PlatformInfo* info); | 68 bool GetPlatformInfo(api::runtime::PlatformInfo* info); |
| 67 bool RestartDevice(std::string* error_message); | 69 bool RestartDevice(std::string* error_message); |
| 70 |
| 71 using OnWatchdogTimeoutCallback = |
| 72 base::Callback<void(bool success, const std::string& message)>; |
| 73 void RestartDeviceOnWatchdogTimeout( |
| 74 const std::string& extension_id, |
| 75 int seconds_from_now, |
| 76 const OnWatchdogTimeoutCallback& callback); |
| 77 |
| 68 bool OpenOptionsPage(const Extension* extension); | 78 bool OpenOptionsPage(const Extension* extension); |
| 69 | 79 |
| 70 private: | 80 private: |
| 71 friend class BrowserContextKeyedAPIFactory<RuntimeAPI>; | 81 friend class BrowserContextKeyedAPIFactory<RuntimeAPI>; |
| 82 friend class RestartOnWatchdogApiTest; |
| 72 | 83 |
| 73 // ExtensionRegistryObserver implementation. | 84 // ExtensionRegistryObserver implementation. |
| 74 void OnExtensionLoaded(content::BrowserContext* browser_context, | 85 void OnExtensionLoaded(content::BrowserContext* browser_context, |
| 75 const Extension* extension) override; | 86 const Extension* extension) override; |
| 76 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context, | 87 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context, |
| 77 const Extension* extension, | 88 const Extension* extension, |
| 78 bool is_update, | 89 bool is_update, |
| 79 const std::string& old_name) override; | 90 const std::string& old_name) override; |
| 80 void OnExtensionUninstalled(content::BrowserContext* browser_context, | 91 void OnExtensionUninstalled(content::BrowserContext* browser_context, |
| 81 const Extension* extension, | 92 const Extension* extension, |
| 82 UninstallReason reason) override; | 93 UninstallReason reason) override; |
| 83 | 94 |
| 95 // Cancels any previously scheduled restart request, invoking its associated |
| 96 // JS callback function stored in |current_watchdog_request_callback_|. |
| 97 void MaybeCancelRunningWatchdogTimer(); |
| 98 |
| 99 void ScheduleDelayedRestart(const std::string& extension_id, |
| 100 const base::Time& now, |
| 101 int seconds_from_now, |
| 102 const OnWatchdogTimeoutCallback& callback, |
| 103 std::unique_ptr<base::Value> stored_last_restart); |
| 104 |
| 105 void OnRestartWatchdogTimeout(const std::string& extension_id); |
| 106 |
| 84 // BrowserContextKeyedAPI implementation: | 107 // BrowserContextKeyedAPI implementation: |
| 85 static const char* service_name() { return "RuntimeAPI"; } | 108 static const char* service_name() { return "RuntimeAPI"; } |
| 86 static const bool kServiceRedirectedInIncognito = true; | 109 static const bool kServiceRedirectedInIncognito = true; |
| 87 static const bool kServiceIsNULLWhileTesting = true; | 110 static const bool kServiceIsNULLWhileTesting = true; |
| 88 void Shutdown() override; | 111 void Shutdown() override; |
| 89 | 112 |
| 90 // extensions::UpdateObserver overrides: | 113 // extensions::UpdateObserver overrides: |
| 91 void OnAppUpdateAvailable(const Extension* extension) override; | 114 void OnAppUpdateAvailable(const Extension* extension) override; |
| 92 void OnChromeUpdateAvailable() override; | 115 void OnChromeUpdateAvailable() override; |
| 93 | 116 |
| 94 // ProcessManagerObserver implementation: | 117 // ProcessManagerObserver implementation: |
| 95 void OnBackgroundHostStartup(const Extension* extension) override; | 118 void OnBackgroundHostStartup(const Extension* extension) override; |
| 96 | 119 |
| 97 // Pref related functions that deals with info about installed extensions that | 120 // Pref related functions that deals with info about installed extensions that |
| 98 // has not been loaded yet. | 121 // has not been loaded yet. |
| 99 // Used to send chrome.runtime.onInstalled event upon loading the extensions. | 122 // Used to send chrome.runtime.onInstalled event upon loading the extensions. |
| 100 bool ReadPendingOnInstallInfoFromPref(const ExtensionId& extension_id, | 123 bool ReadPendingOnInstallInfoFromPref(const ExtensionId& extension_id, |
| 101 base::Version* previous_version); | 124 base::Version* previous_version); |
| 102 void RemovePendingOnInstallInfoFromPref(const ExtensionId& extension_id); | 125 void RemovePendingOnInstallInfoFromPref(const ExtensionId& extension_id); |
| 103 void StorePendingOnInstallInfoToPref(const Extension* extension); | 126 void StorePendingOnInstallInfoToPref(const Extension* extension); |
| 104 | 127 |
| 128 void SetMinDurationBetweenRestartsForTesting(base::TimeDelta delta) { |
| 129 minimum_duration_between_restarts_ = delta; |
| 130 } |
| 131 |
| 105 std::unique_ptr<RuntimeAPIDelegate> delegate_; | 132 std::unique_ptr<RuntimeAPIDelegate> delegate_; |
| 106 | 133 |
| 107 content::BrowserContext* browser_context_; | 134 content::BrowserContext* browser_context_; |
| 108 | 135 |
| 109 // True if we should dispatch the chrome.runtime.onInstalled event with | 136 // True if we should dispatch the chrome.runtime.onInstalled event with |
| 110 // reason "chrome_update" upon loading each extension. | 137 // reason "chrome_update" upon loading each extension. |
| 111 bool dispatch_chrome_updated_event_; | 138 bool dispatch_chrome_updated_event_; |
| 112 | 139 |
| 113 content::NotificationRegistrar registrar_; | 140 content::NotificationRegistrar registrar_; |
| 114 | 141 |
| 115 // Listen to extension notifications. | 142 // Listen to extension notifications. |
| 116 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> | 143 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
| 117 extension_registry_observer_; | 144 extension_registry_observer_; |
| 118 ScopedObserver<ProcessManager, ProcessManagerObserver> | 145 ScopedObserver<ProcessManager, ProcessManagerObserver> |
| 119 process_manager_observer_; | 146 process_manager_observer_; |
| 120 | 147 |
| 148 // The ID of the first extension to call the restartOnWatchdog API. Any other |
| 149 // extensions to call this API after that will fail. |
| 150 std::string schedule_restart_first_extension_id_; |
| 151 |
| 152 // The current JS callback for the current scheduled restart which will be |
| 153 // invoked when that restart attempt is executed. |
| 154 OnWatchdogTimeoutCallback current_watchdog_request_callback_; |
| 155 |
| 156 // The timer that will trigger a device restart when it times out. |
| 157 base::OneShotTimer watchdog_timer_; |
| 158 |
| 159 // The minimum allowed duration between two successive restarts on watchdog |
| 160 // timer. |
| 161 base::TimeDelta minimum_duration_between_restarts_; |
| 162 |
| 163 base::WeakPtrFactory<RuntimeAPI> weak_ptr_factory_; |
| 164 |
| 121 DISALLOW_COPY_AND_ASSIGN(RuntimeAPI); | 165 DISALLOW_COPY_AND_ASSIGN(RuntimeAPI); |
| 122 }; | 166 }; |
| 123 | 167 |
| 124 template <> | 168 template <> |
| 125 void BrowserContextKeyedAPIFactory<RuntimeAPI>::DeclareFactoryDependencies(); | 169 void BrowserContextKeyedAPIFactory<RuntimeAPI>::DeclareFactoryDependencies(); |
| 126 | 170 |
| 127 class RuntimeEventRouter { | 171 class RuntimeEventRouter { |
| 128 public: | 172 public: |
| 129 // Dispatches the onStartup event to all currently-loaded extensions. | 173 // Dispatches the onStartup event to all currently-loaded extensions. |
| 130 static void DispatchOnStartupEvent(content::BrowserContext* context, | 174 static void DispatchOnStartupEvent(content::BrowserContext* context, |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 | 257 |
| 214 class RuntimeRestartFunction : public UIThreadExtensionFunction { | 258 class RuntimeRestartFunction : public UIThreadExtensionFunction { |
| 215 public: | 259 public: |
| 216 DECLARE_EXTENSION_FUNCTION("runtime.restart", RUNTIME_RESTART) | 260 DECLARE_EXTENSION_FUNCTION("runtime.restart", RUNTIME_RESTART) |
| 217 | 261 |
| 218 protected: | 262 protected: |
| 219 ~RuntimeRestartFunction() override {} | 263 ~RuntimeRestartFunction() override {} |
| 220 ResponseAction Run() override; | 264 ResponseAction Run() override; |
| 221 }; | 265 }; |
| 222 | 266 |
| 267 class RuntimeRestartOnWatchdogFunction : public UIThreadExtensionFunction { |
| 268 public: |
| 269 DECLARE_EXTENSION_FUNCTION("runtime.restartOnWatchdog", |
| 270 RUNTIME_RESTARTONWATCHDOG) |
| 271 |
| 272 protected: |
| 273 ~RuntimeRestartOnWatchdogFunction() override {} |
| 274 ResponseAction Run() override; |
| 275 |
| 276 private: |
| 277 void OnWatchdogTimeout(bool success, const std::string& message); |
| 278 }; |
| 279 |
| 223 class RuntimeGetPlatformInfoFunction : public UIThreadExtensionFunction { | 280 class RuntimeGetPlatformInfoFunction : public UIThreadExtensionFunction { |
| 224 public: | 281 public: |
| 225 DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo", | 282 DECLARE_EXTENSION_FUNCTION("runtime.getPlatformInfo", |
| 226 RUNTIME_GETPLATFORMINFO); | 283 RUNTIME_GETPLATFORMINFO); |
| 227 | 284 |
| 228 protected: | 285 protected: |
| 229 ~RuntimeGetPlatformInfoFunction() override {} | 286 ~RuntimeGetPlatformInfoFunction() override {} |
| 230 ResponseAction Run() override; | 287 ResponseAction Run() override; |
| 231 }; | 288 }; |
| 232 | 289 |
| 233 class RuntimeGetPackageDirectoryEntryFunction | 290 class RuntimeGetPackageDirectoryEntryFunction |
| 234 : public UIThreadExtensionFunction { | 291 : public UIThreadExtensionFunction { |
| 235 public: | 292 public: |
| 236 DECLARE_EXTENSION_FUNCTION("runtime.getPackageDirectoryEntry", | 293 DECLARE_EXTENSION_FUNCTION("runtime.getPackageDirectoryEntry", |
| 237 RUNTIME_GETPACKAGEDIRECTORYENTRY) | 294 RUNTIME_GETPACKAGEDIRECTORYENTRY) |
| 238 | 295 |
| 239 protected: | 296 protected: |
| 240 ~RuntimeGetPackageDirectoryEntryFunction() override {} | 297 ~RuntimeGetPackageDirectoryEntryFunction() override {} |
| 241 ResponseAction Run() override; | 298 ResponseAction Run() override; |
| 242 }; | 299 }; |
| 243 | 300 |
| 244 } // namespace extensions | 301 } // namespace extensions |
| 245 | 302 |
| 246 #endif // EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_ | 303 #endif // EXTENSIONS_BROWSER_API_RUNTIME_RUNTIME_API_H_ |
| OLD | NEW |