Revert 290001 because it's causing test failures:
[chromium-blink-merge.git] / apps / app_lifetime_monitor.cc
blob84b6c826d3a8a32142e2bb68f1cd051982dca25a
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"
16 namespace apps {
18 using extensions::Extension;
19 using extensions::ExtensionHost;
21 AppLifetimeMonitor::AppLifetimeMonitor(Profile* profile)
22 : profile_(profile) {
23 registrar_.Add(this,
24 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
25 content::NotificationService::AllSources());
26 registrar_.Add(this,
27 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
28 content::NotificationService::AllSources());
29 registrar_.Add(
30 this, chrome::NOTIFICATION_APP_TERMINATING,
31 content::NotificationService::AllSources());
33 AppWindowRegistry* app_window_registry =
34 AppWindowRegistry::Factory::GetForBrowserContext(profile_,
35 false /* create */);
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) {
53 switch (type) {
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())
58 return;
60 NotifyAppStart(extension->id());
61 break;
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())
68 return;
70 NotifyAppStop(extension->id());
71 break;
74 case chrome::NOTIFICATION_APP_TERMINATING: {
75 NotifyChromeTerminating();
76 break;
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)
93 return;
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_,
102 false /* create */);
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();
113 i != windows.end();
114 ++i) {
115 if (!(*i)->is_hidden())
116 return true;
118 return false;
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());
141 } // namespace apps