shell_keep_alive_requester.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2018 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 "extensions/shell/browser/shell_keep_alive_requester.h"
  5. #include "apps/app_lifetime_monitor_factory.h"
  6. #include "components/keep_alive_registry/keep_alive_types.h"
  7. #include "components/keep_alive_registry/scoped_keep_alive.h"
  8. #include "extensions/browser/extension_prefs.h"
  9. namespace extensions {
  10. ShellKeepAliveRequester::ShellKeepAliveRequester(
  11. content::BrowserContext* browser_context) {
  12. extension_registry_observation_.Observe(
  13. ExtensionRegistry::Get(browser_context));
  14. app_lifetime_monitor_observation_.Observe(
  15. apps::AppLifetimeMonitorFactory::GetForBrowserContext(browser_context));
  16. }
  17. ShellKeepAliveRequester::~ShellKeepAliveRequester() = default;
  18. void ShellKeepAliveRequester::StartTrackingReload(const Extension* extension) {
  19. if (!extension->is_platform_app())
  20. return;
  21. // The app will be reloaded, closing its windows. Add a keep-alive to wait for
  22. // the app to unload and reload.
  23. app_reloading_keep_alives_[extension->id()] =
  24. std::make_unique<ScopedKeepAlive>(KeepAliveOrigin::APP_CONTROLLER,
  25. KeepAliveRestartOption::ENABLED);
  26. }
  27. void ShellKeepAliveRequester::StopTrackingReload(
  28. const ExtensionId& old_extension_id) {
  29. // No longer waiting for reload to complete.
  30. app_reloading_keep_alives_.erase(old_extension_id);
  31. }
  32. void ShellKeepAliveRequester::OnExtensionLoaded(
  33. content::BrowserContext* browser_context,
  34. const Extension* extension) {
  35. if (!extension->is_platform_app())
  36. return;
  37. // Add a keep-alive to wait for the app to launch its first app window, as
  38. // otherwise the Aura desktop controller may exit. The assumption is that all
  39. // apps will create a visible window. If the app doesn't, this keep-alive will
  40. // still be erased once the app's background page eventually stops.
  41. app_launching_keep_alives_[extension->id()] =
  42. std::make_unique<ScopedKeepAlive>(KeepAliveOrigin::APP_CONTROLLER,
  43. KeepAliveRestartOption::ENABLED);
  44. }
  45. void ShellKeepAliveRequester::OnExtensionUnloaded(
  46. content::BrowserContext* browser_context,
  47. const Extension* extension,
  48. UnloadedExtensionReason reason) {
  49. // There may already be a keep-alive waiting for the app to launch a window.
  50. // Remove that; another will be created if the extension successfully loads.
  51. app_launching_keep_alives_.erase(extension->id());
  52. }
  53. void ShellKeepAliveRequester::OnAppActivated(content::BrowserContext* context,
  54. const std::string& app_id) {
  55. // The app has launched its first window. The desktop controller will keep
  56. // running until all app windows close.
  57. app_launching_keep_alives_.erase(app_id);
  58. }
  59. void ShellKeepAliveRequester::OnAppStop(content::BrowserContext* context,
  60. const std::string& app_id) {
  61. // The app will still have a keep-alive if it never showed a window.
  62. app_launching_keep_alives_.erase(app_id);
  63. }
  64. } // namespace extensions