app_restore_service.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #include "apps/app_restore_service.h"
  5. #include "apps/app_lifetime_monitor_factory.h"
  6. #include "apps/app_restore_service_factory.h"
  7. #include "apps/launcher.h"
  8. #include "apps/saved_files_service.h"
  9. #include "build/chromeos_buildflags.h"
  10. #include "content/public/browser/browser_context.h"
  11. #include "extensions/browser/app_window/app_window.h"
  12. #include "extensions/browser/extension_host.h"
  13. #include "extensions/browser/extension_prefs.h"
  14. #include "extensions/browser/extension_registry.h"
  15. #include "extensions/common/extension.h"
  16. #include "extensions/common/extension_set.h"
  17. using extensions::Extension;
  18. using extensions::ExtensionHost;
  19. using extensions::ExtensionPrefs;
  20. using extensions::ExtensionRegistry;
  21. namespace apps {
  22. // static
  23. bool AppRestoreService::ShouldRestoreApps(bool is_browser_restart) {
  24. bool should_restore_apps = is_browser_restart;
  25. #if BUILDFLAG(IS_CHROMEOS_ASH)
  26. // Chromeos always restarts apps, even if it was a regular shutdown.
  27. should_restore_apps = true;
  28. #endif
  29. return should_restore_apps;
  30. }
  31. AppRestoreService::AppRestoreService(content::BrowserContext* context)
  32. : context_(context) {
  33. StartObservingAppLifetime();
  34. }
  35. void AppRestoreService::HandleStartup(bool should_restore_apps) {
  36. const extensions::ExtensionSet& extensions =
  37. ExtensionRegistry::Get(context_)->enabled_extensions();
  38. ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context_);
  39. for (extensions::ExtensionSet::const_iterator it = extensions.begin();
  40. it != extensions.end(); ++it) {
  41. const Extension* extension = it->get();
  42. if (extension_prefs->IsExtensionRunning(extension->id())) {
  43. RecordAppStop(extension->id());
  44. // If we are not restoring apps (e.g., because it is a clean restart), and
  45. // the app does not have retain permission, explicitly clear the retained
  46. // entries queue.
  47. if (should_restore_apps) {
  48. RestoreApp(it->get());
  49. } else {
  50. SavedFilesService::Get(context_)->ClearQueueIfNoRetainPermission(
  51. extension);
  52. }
  53. }
  54. }
  55. }
  56. bool AppRestoreService::IsAppRestorable(const std::string& extension_id) {
  57. return ExtensionPrefs::Get(context_)->IsExtensionRunning(extension_id);
  58. }
  59. void AppRestoreService::OnApplicationTerminating() {
  60. // We want to preserve the state when the app begins terminating, so stop
  61. // listening to app lifetime events.
  62. StopObservingAppLifetime();
  63. }
  64. // static
  65. AppRestoreService* AppRestoreService::Get(content::BrowserContext* context) {
  66. return apps::AppRestoreServiceFactory::GetForBrowserContext(context);
  67. }
  68. void AppRestoreService::OnAppStart(content::BrowserContext* context,
  69. const std::string& app_id) {
  70. RecordAppStart(app_id);
  71. }
  72. void AppRestoreService::OnAppActivated(content::BrowserContext* context,
  73. const std::string& app_id) {
  74. RecordAppActiveState(app_id, true);
  75. }
  76. void AppRestoreService::OnAppDeactivated(content::BrowserContext* context,
  77. const std::string& app_id) {
  78. RecordAppActiveState(app_id, false);
  79. }
  80. void AppRestoreService::OnAppStop(content::BrowserContext* context,
  81. const std::string& app_id) {
  82. RecordAppStop(app_id);
  83. }
  84. void AppRestoreService::Shutdown() {
  85. StopObservingAppLifetime();
  86. }
  87. void AppRestoreService::RecordAppStart(const std::string& extension_id) {
  88. ExtensionPrefs::Get(context_)->SetExtensionRunning(extension_id, true);
  89. }
  90. void AppRestoreService::RecordAppStop(const std::string& extension_id) {
  91. ExtensionPrefs::Get(context_)->SetExtensionRunning(extension_id, false);
  92. }
  93. void AppRestoreService::RecordAppActiveState(const std::string& id,
  94. bool is_active) {
  95. ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(context_);
  96. // If the extension isn't running then we will already have recorded whether
  97. // it is active or not.
  98. if (!extension_prefs->IsExtensionRunning(id))
  99. return;
  100. extension_prefs->SetIsActive(id, is_active);
  101. }
  102. void AppRestoreService::RestoreApp(const Extension* extension) {
  103. RestartPlatformApp(context_, extension);
  104. }
  105. void AppRestoreService::StartObservingAppLifetime() {
  106. AppLifetimeMonitor* app_lifetime_monitor =
  107. AppLifetimeMonitorFactory::GetForBrowserContext(context_);
  108. DCHECK(app_lifetime_monitor);
  109. app_lifetime_monitor->AddObserver(this);
  110. }
  111. void AppRestoreService::StopObservingAppLifetime() {
  112. AppLifetimeMonitor* app_lifetime_monitor =
  113. AppLifetimeMonitorFactory::GetForBrowserContext(context_);
  114. // This might be NULL in tests.
  115. if (app_lifetime_monitor)
  116. app_lifetime_monitor->RemoveObserver(this);
  117. }
  118. } // namespace apps