app_lifetime_monitor.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2013 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_LIFETIME_MONITOR_H_
  5. #define APPS_APP_LIFETIME_MONITOR_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/observer_list.h"
  10. #include "base/scoped_observation.h"
  11. #include "components/keyed_service/core/keyed_service.h"
  12. #include "extensions/browser/app_window/app_window_registry.h"
  13. #include "extensions/browser/extension_host_registry.h"
  14. namespace content {
  15. class BrowserContext;
  16. }
  17. namespace apps {
  18. // Observes startup of apps and their windows and notifies observers of these
  19. // events.
  20. class AppLifetimeMonitor : public KeyedService,
  21. public extensions::AppWindowRegistry::Observer,
  22. public extensions::ExtensionHostRegistry::Observer {
  23. public:
  24. class Observer {
  25. public:
  26. // Called when the app starts running.
  27. virtual void OnAppStart(content::BrowserContext* context,
  28. const std::string& app_id) {}
  29. // Called when the app becomes active to the user, i.e. the first window
  30. // becomes visible.
  31. virtual void OnAppActivated(content::BrowserContext* context,
  32. const std::string& app_id) {}
  33. // Called when the app becomes inactive to the user, i.e. the last window is
  34. // hidden or closed.
  35. virtual void OnAppDeactivated(content::BrowserContext* context,
  36. const std::string& app_id) {}
  37. // Called when the app stops running.
  38. virtual void OnAppStop(content::BrowserContext* context,
  39. const std::string& app_id) {}
  40. protected:
  41. virtual ~Observer() = default;
  42. };
  43. explicit AppLifetimeMonitor(content::BrowserContext* context);
  44. AppLifetimeMonitor(const AppLifetimeMonitor&) = delete;
  45. AppLifetimeMonitor& operator=(const AppLifetimeMonitor&) = delete;
  46. ~AppLifetimeMonitor() override;
  47. void AddObserver(Observer* observer);
  48. void RemoveObserver(Observer* observer);
  49. private:
  50. // extensions::AppWindowRegistry::Observer overrides:
  51. void OnAppWindowRemoved(extensions::AppWindow* app_window) override;
  52. void OnAppWindowHidden(extensions::AppWindow* app_window) override;
  53. void OnAppWindowShown(extensions::AppWindow* app_window,
  54. bool was_hidden) override;
  55. // extensions::ExtensionHostRegistry::Observer:
  56. void OnExtensionHostCompletedFirstLoad(
  57. content::BrowserContext* browser_context,
  58. extensions::ExtensionHost* host) override;
  59. void OnExtensionHostDestroyed(content::BrowserContext* browser_context,
  60. extensions::ExtensionHost* host) override;
  61. // KeyedService overrides:
  62. void Shutdown() override;
  63. bool HasOtherVisibleAppWindows(extensions::AppWindow* app_window) const;
  64. void NotifyAppStart(const std::string& app_id);
  65. void NotifyAppActivated(const std::string& app_id);
  66. void NotifyAppDeactivated(const std::string& app_id);
  67. void NotifyAppStop(const std::string& app_id);
  68. raw_ptr<content::BrowserContext> context_;
  69. base::ObserverList<Observer>::Unchecked observers_;
  70. base::ScopedObservation<extensions::ExtensionHostRegistry,
  71. extensions::ExtensionHostRegistry::Observer>
  72. extension_host_registry_observation_{this};
  73. };
  74. } // namespace apps
  75. #endif // APPS_APP_LIFETIME_MONITOR_H_