Upload upstream chromium 76.0.3809.146
[platform/framework/web/chromium-efl.git] / apps / app_lifetime_monitor.h
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
5 #ifndef APPS_APP_LIFETIME_MONITOR_H_
6 #define APPS_APP_LIFETIME_MONITOR_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "base/observer_list.h"
12 #include "components/keyed_service/core/keyed_service.h"
13 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/notification_registrar.h"
15 #include "extensions/browser/app_window/app_window_registry.h"
16
17 namespace content {
18 class BrowserContext;
19 }
20
21 namespace apps {
22
23 // Observes startup of apps and their windows and notifies observers of these
24 // events.
25 class AppLifetimeMonitor : public KeyedService,
26                            public content::NotificationObserver,
27                            public extensions::AppWindowRegistry::Observer {
28  public:
29   class Observer {
30    public:
31     // Called when the app starts running.
32     virtual void OnAppStart(content::BrowserContext* context,
33                             const std::string& app_id) {}
34     // Called when the app becomes active to the user, i.e. the first window
35     // becomes visible.
36     virtual void OnAppActivated(content::BrowserContext* context,
37                                 const std::string& app_id) {}
38     // Called when the app becomes inactive to the user, i.e. the last window is
39     // hidden or closed.
40     virtual void OnAppDeactivated(content::BrowserContext* context,
41                                   const std::string& app_id) {}
42     // Called when the app stops running.
43     virtual void OnAppStop(content::BrowserContext* context,
44                            const std::string& app_id) {}
45
46    protected:
47     virtual ~Observer() {}
48   };
49
50   explicit AppLifetimeMonitor(content::BrowserContext* context);
51   ~AppLifetimeMonitor() override;
52
53   void AddObserver(Observer* observer);
54   void RemoveObserver(Observer* observer);
55
56  private:
57   // content::NotificationObserver overrides:
58   void Observe(int type,
59                const content::NotificationSource& source,
60                const content::NotificationDetails& details) override;
61
62   // extensions::AppWindowRegistry::Observer overrides:
63   void OnAppWindowRemoved(extensions::AppWindow* app_window) override;
64   void OnAppWindowHidden(extensions::AppWindow* app_window) override;
65   void OnAppWindowShown(extensions::AppWindow* app_window,
66                         bool was_hidden) override;
67
68   // KeyedService overrides:
69   void Shutdown() override;
70
71   bool HasOtherVisibleAppWindows(extensions::AppWindow* app_window) const;
72
73   void NotifyAppStart(const std::string& app_id);
74   void NotifyAppActivated(const std::string& app_id);
75   void NotifyAppDeactivated(const std::string& app_id);
76   void NotifyAppStop(const std::string& app_id);
77
78   content::NotificationRegistrar registrar_;
79   content::BrowserContext* context_;
80   base::ObserverList<Observer>::Unchecked observers_;
81
82   DISALLOW_COPY_AND_ASSIGN(AppLifetimeMonitor);
83 };
84
85 }  // namespace apps
86
87 #endif  // APPS_APP_LIFETIME_MONITOR_H_