app_restore_service.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef APPS_APP_RESTORE_SERVICE_H_
  5. #define APPS_APP_RESTORE_SERVICE_H_
  6. #include <string>
  7. #include <vector>
  8. #include "apps/app_lifetime_monitor.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "components/keyed_service/core/keyed_service.h"
  11. #include "extensions/browser/app_window/app_window_registry.h"
  12. namespace extensions {
  13. class Extension;
  14. }
  15. namespace content {
  16. class BrowserContext;
  17. }
  18. namespace apps {
  19. // Tracks what apps need to be restarted when the browser restarts.
  20. class AppRestoreService : public KeyedService,
  21. public AppLifetimeMonitor::Observer {
  22. public:
  23. // Returns true if apps should be restored on the current platform, given
  24. // whether this new browser process launched due to a restart.
  25. static bool ShouldRestoreApps(bool is_browser_restart);
  26. explicit AppRestoreService(content::BrowserContext* context);
  27. AppRestoreService(const AppRestoreService&) = delete;
  28. AppRestoreService& operator=(const AppRestoreService&) = delete;
  29. // Restart apps that need to be restarted and clear the "running" preference
  30. // from apps to prevent them being restarted in subsequent restarts.
  31. void HandleStartup(bool should_restore_apps);
  32. // Returns whether this extension is running or, at startup, whether it was
  33. // running when Chrome was last terminated.
  34. bool IsAppRestorable(const std::string& extension_id);
  35. // Called to notify that the application has begun to exit.
  36. void OnApplicationTerminating();
  37. static AppRestoreService* Get(content::BrowserContext* context);
  38. private:
  39. // AppLifetimeMonitor::Observer.
  40. void OnAppStart(content::BrowserContext* context,
  41. const std::string& app_id) override;
  42. void OnAppActivated(content::BrowserContext* context,
  43. const std::string& app_id) override;
  44. void OnAppDeactivated(content::BrowserContext* context,
  45. const std::string& app_id) override;
  46. void OnAppStop(content::BrowserContext* context,
  47. const std::string& app_id) override;
  48. // KeyedService.
  49. void Shutdown() override;
  50. void RecordAppStart(const std::string& extension_id);
  51. void RecordAppStop(const std::string& extension_id);
  52. void RecordAppActiveState(const std::string& id, bool is_active);
  53. void RestoreApp(const extensions::Extension* extension);
  54. void StartObservingAppLifetime();
  55. void StopObservingAppLifetime();
  56. raw_ptr<content::BrowserContext> context_;
  57. };
  58. } // namespace apps
  59. #endif // APPS_APP_RESTORE_SERVICE_H_