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 #include "apps/app_lifetime_monitor.h"
7 #include "apps/app_window.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "content/public/browser/notification_details.h"
11 #include "content/public/browser/notification_service.h"
12 #include "extensions/browser/extension_host.h"
13 #include "extensions/browser/notification_types.h"
14 #include "extensions/common/extension.h"
18 using extensions::Extension;
19 using extensions::ExtensionHost;
21 AppLifetimeMonitor::AppLifetimeMonitor(Profile* profile)
24 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
25 content::NotificationService::AllSources());
27 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
28 content::NotificationService::AllSources());
30 this, chrome::NOTIFICATION_APP_TERMINATING,
31 content::NotificationService::AllSources());
33 AppWindowRegistry* app_window_registry =
34 AppWindowRegistry::Factory::GetForBrowserContext(profile_,
36 DCHECK(app_window_registry);
37 app_window_registry->AddObserver(this);
40 AppLifetimeMonitor::~AppLifetimeMonitor() {}
42 void AppLifetimeMonitor::AddObserver(Observer* observer) {
43 observers_.AddObserver(observer);
46 void AppLifetimeMonitor::RemoveObserver(Observer* observer) {
47 observers_.RemoveObserver(observer);
50 void AppLifetimeMonitor::Observe(int type,
51 const content::NotificationSource& source,
52 const content::NotificationDetails& details) {
54 case extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: {
55 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
56 const Extension* extension = host->extension();
57 if (!extension || !extension->is_platform_app())
60 NotifyAppStart(extension->id());
64 case extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
65 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
66 const Extension* extension = host->extension();
67 if (!extension || !extension->is_platform_app())
70 NotifyAppStop(extension->id());
74 case chrome::NOTIFICATION_APP_TERMINATING: {
75 NotifyChromeTerminating();
81 void AppLifetimeMonitor::OnAppWindowRemoved(AppWindow* app_window) {
82 if (!HasVisibleAppWindows(app_window))
83 NotifyAppDeactivated(app_window->extension_id());
86 void AppLifetimeMonitor::OnAppWindowHidden(AppWindow* app_window) {
87 if (!HasVisibleAppWindows(app_window))
88 NotifyAppDeactivated(app_window->extension_id());
91 void AppLifetimeMonitor::OnAppWindowShown(AppWindow* app_window) {
92 if (app_window->window_type() != AppWindow::WINDOW_TYPE_DEFAULT)
95 if (HasVisibleAppWindows(app_window))
96 NotifyAppActivated(app_window->extension_id());
99 void AppLifetimeMonitor::Shutdown() {
100 AppWindowRegistry* app_window_registry =
101 AppWindowRegistry::Factory::GetForBrowserContext(profile_,
103 if (app_window_registry)
104 app_window_registry->RemoveObserver(this);
107 bool AppLifetimeMonitor::HasVisibleAppWindows(AppWindow* app_window) const {
108 AppWindowRegistry::AppWindowList windows =
109 AppWindowRegistry::Get(app_window->browser_context())
110 ->GetAppWindowsForApp(app_window->extension_id());
112 for (AppWindowRegistry::AppWindowList::const_iterator i = windows.begin();
115 if (!(*i)->is_hidden())
121 void AppLifetimeMonitor::NotifyAppStart(const std::string& app_id) {
122 FOR_EACH_OBSERVER(Observer, observers_, OnAppStart(profile_, app_id));
125 void AppLifetimeMonitor::NotifyAppActivated(const std::string& app_id) {
126 FOR_EACH_OBSERVER(Observer, observers_, OnAppActivated(profile_, app_id));
129 void AppLifetimeMonitor::NotifyAppDeactivated(const std::string& app_id) {
130 FOR_EACH_OBSERVER(Observer, observers_, OnAppDeactivated(profile_, app_id));
133 void AppLifetimeMonitor::NotifyAppStop(const std::string& app_id) {
134 FOR_EACH_OBSERVER(Observer, observers_, OnAppStop(profile_, app_id));
137 void AppLifetimeMonitor::NotifyChromeTerminating() {
138 FOR_EACH_OBSERVER(Observer, observers_, OnChromeTerminating());