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.
5 #ifndef APPS_APP_LIFETIME_MONITOR_H_
6 #define APPS_APP_LIFETIME_MONITOR_H_
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"
23 // Observes startup of apps and their windows and notifies observers of these
25 class AppLifetimeMonitor : public KeyedService,
26 public content::NotificationObserver,
27 public extensions::AppWindowRegistry::Observer {
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
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
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) {}
47 virtual ~Observer() {}
50 explicit AppLifetimeMonitor(content::BrowserContext* context);
51 ~AppLifetimeMonitor() override;
53 void AddObserver(Observer* observer);
54 void RemoveObserver(Observer* observer);
57 // content::NotificationObserver overrides:
58 void Observe(int type,
59 const content::NotificationSource& source,
60 const content::NotificationDetails& details) override;
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;
68 // KeyedService overrides:
69 void Shutdown() override;
71 bool HasOtherVisibleAppWindows(extensions::AppWindow* app_window) const;
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);
78 content::NotificationRegistrar registrar_;
79 content::BrowserContext* context_;
80 base::ObserverList<Observer>::Unchecked observers_;
82 DISALLOW_COPY_AND_ASSIGN(AppLifetimeMonitor);
87 #endif // APPS_APP_LIFETIME_MONITOR_H_