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 "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "content/public/browser/notification_details.h"
10 #include "content/public/browser/notification_service.h"
11 #include "extensions/browser/app_window/app_window.h"
12 #include "extensions/browser/extension_host.h"
13 #include "extensions/browser/notification_types.h"
14 #include "extensions/common/extension.h"
18 using extensions::AppWindow
;
19 using extensions::AppWindowRegistry
;
20 using extensions::Extension
;
21 using extensions::ExtensionHost
;
23 AppLifetimeMonitor::AppLifetimeMonitor(Profile
* profile
)
26 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING
,
27 content::NotificationService::AllSources());
29 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED
,
30 content::NotificationService::AllSources());
32 this, chrome::NOTIFICATION_APP_TERMINATING
,
33 content::NotificationService::AllSources());
35 AppWindowRegistry
* app_window_registry
=
36 AppWindowRegistry::Factory::GetForBrowserContext(profile_
,
38 DCHECK(app_window_registry
);
39 app_window_registry
->AddObserver(this);
42 AppLifetimeMonitor::~AppLifetimeMonitor() {}
44 void AppLifetimeMonitor::AddObserver(Observer
* observer
) {
45 observers_
.AddObserver(observer
);
48 void AppLifetimeMonitor::RemoveObserver(Observer
* observer
) {
49 observers_
.RemoveObserver(observer
);
52 void AppLifetimeMonitor::Observe(int type
,
53 const content::NotificationSource
& source
,
54 const content::NotificationDetails
& details
) {
56 case extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING
: {
57 ExtensionHost
* host
= content::Details
<ExtensionHost
>(details
).ptr();
58 const Extension
* extension
= host
->extension();
59 if (!extension
|| !extension
->is_platform_app())
62 NotifyAppStart(extension
->id());
66 case extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED
: {
67 ExtensionHost
* host
= content::Details
<ExtensionHost
>(details
).ptr();
68 const Extension
* extension
= host
->extension();
69 if (!extension
|| !extension
->is_platform_app())
72 NotifyAppStop(extension
->id());
76 case chrome::NOTIFICATION_APP_TERMINATING
: {
77 NotifyChromeTerminating();
83 void AppLifetimeMonitor::OnAppWindowRemoved(AppWindow
* app_window
) {
84 if (!HasOtherVisibleAppWindows(app_window
))
85 NotifyAppDeactivated(app_window
->extension_id());
88 void AppLifetimeMonitor::OnAppWindowHidden(AppWindow
* app_window
) {
89 if (!HasOtherVisibleAppWindows(app_window
))
90 NotifyAppDeactivated(app_window
->extension_id());
93 void AppLifetimeMonitor::OnAppWindowShown(AppWindow
* app_window
,
95 if (app_window
->window_type() != AppWindow::WINDOW_TYPE_DEFAULT
)
98 // The app is being activated if this is the first window to become visible.
99 if (was_hidden
&& !HasOtherVisibleAppWindows(app_window
)) {
100 NotifyAppActivated(app_window
->extension_id());
104 void AppLifetimeMonitor::Shutdown() {
105 AppWindowRegistry
* app_window_registry
=
106 AppWindowRegistry::Factory::GetForBrowserContext(profile_
,
108 if (app_window_registry
)
109 app_window_registry
->RemoveObserver(this);
112 bool AppLifetimeMonitor::HasOtherVisibleAppWindows(
113 AppWindow
* app_window
) const {
114 AppWindowRegistry::AppWindowList windows
=
115 AppWindowRegistry::Get(app_window
->browser_context())
116 ->GetAppWindowsForApp(app_window
->extension_id());
118 for (AppWindowRegistry::AppWindowList::const_iterator i
= windows
.begin();
121 if (*i
!= app_window
&& !(*i
)->is_hidden())
127 void AppLifetimeMonitor::NotifyAppStart(const std::string
& app_id
) {
128 FOR_EACH_OBSERVER(Observer
, observers_
, OnAppStart(profile_
, app_id
));
131 void AppLifetimeMonitor::NotifyAppActivated(const std::string
& app_id
) {
132 FOR_EACH_OBSERVER(Observer
, observers_
, OnAppActivated(profile_
, app_id
));
135 void AppLifetimeMonitor::NotifyAppDeactivated(const std::string
& app_id
) {
136 FOR_EACH_OBSERVER(Observer
, observers_
, OnAppDeactivated(profile_
, app_id
));
139 void AppLifetimeMonitor::NotifyAppStop(const std::string
& app_id
) {
140 FOR_EACH_OBSERVER(Observer
, observers_
, OnAppStop(profile_
, app_id
));
143 void AppLifetimeMonitor::NotifyChromeTerminating() {
144 FOR_EACH_OBSERVER(Observer
, observers_
, OnChromeTerminating());