1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef APPS_APP_LIFETIME_MONITOR_H_
6 #define APPS_APP_LIFETIME_MONITOR_H_
11 #include "base/memory/raw_ptr.h"
12 #include "base/observer_list.h"
13 #include "base/scoped_observation.h"
14 #include "components/keyed_service/core/keyed_service.h"
15 #include "extensions/browser/app_window/app_window_registry.h"
16 #include "extensions/browser/extension_host_registry.h"
24 // Observes startup of apps and their windows and notifies observers of these
26 class AppLifetimeMonitor : public KeyedService,
27 public extensions::AppWindowRegistry::Observer,
28 public extensions::ExtensionHostRegistry::Observer {
32 // Called when the app starts running.
33 virtual void OnAppStart(content::BrowserContext* context,
34 const std::string& app_id) {}
35 // Called when the app becomes active to the user, i.e. the first window
37 virtual void OnAppActivated(content::BrowserContext* context,
38 const std::string& app_id) {}
39 // Called when the app becomes inactive to the user, i.e. the last window is
41 virtual void OnAppDeactivated(content::BrowserContext* context,
42 const std::string& app_id) {}
43 // Called when the app stops running.
44 virtual void OnAppStop(content::BrowserContext* context,
45 const std::string& app_id) {}
48 virtual ~Observer() = default;
51 explicit AppLifetimeMonitor(content::BrowserContext* context);
52 AppLifetimeMonitor(const AppLifetimeMonitor&) = delete;
53 AppLifetimeMonitor& operator=(const AppLifetimeMonitor&) = delete;
54 ~AppLifetimeMonitor() override;
56 void AddObserver(Observer* observer);
57 void RemoveObserver(Observer* observer);
60 // extensions::AppWindowRegistry::Observer overrides:
61 void OnAppWindowRemoved(extensions::AppWindow* app_window) override;
62 void OnAppWindowHidden(extensions::AppWindow* app_window) override;
63 void OnAppWindowShown(extensions::AppWindow* app_window,
64 bool was_hidden) override;
66 // extensions::ExtensionHostRegistry::Observer:
67 void OnExtensionHostCompletedFirstLoad(
68 content::BrowserContext* browser_context,
69 extensions::ExtensionHost* host) override;
70 void OnExtensionHostDestroyed(content::BrowserContext* browser_context,
71 extensions::ExtensionHost* host) override;
73 // KeyedService overrides:
74 void Shutdown() override;
76 bool HasOtherVisibleAppWindows(extensions::AppWindow* app_window) const;
78 void NotifyAppStart(const std::string& app_id);
79 void NotifyAppActivated(const std::string& app_id);
80 void NotifyAppDeactivated(const std::string& app_id);
81 void NotifyAppStop(const std::string& app_id);
83 raw_ptr<content::BrowserContext> context_;
84 base::ObserverList<Observer>::Unchecked observers_;
85 base::ScopedObservation<extensions::ExtensionHostRegistry,
86 extensions::ExtensionHostRegistry::Observer>
87 extension_host_registry_observation_{this};
92 #endif // APPS_APP_LIFETIME_MONITOR_H_