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/shell_window.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/extensions/extension_host.h"
10 #include "chrome/common/extensions/extension.h"
11 #include "content/public/browser/notification_details.h"
12 #include "content/public/browser/notification_service.h"
16 using extensions::Extension
;
17 using extensions::ExtensionHost
;
19 AppLifetimeMonitor::AppLifetimeMonitor(Profile
* profile
)
22 this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING
,
23 content::NotificationService::AllSources());
25 this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED
,
26 content::NotificationService::AllSources());
28 this, chrome::NOTIFICATION_APP_TERMINATING
,
29 content::NotificationService::AllSources());
31 ShellWindowRegistry
* shell_window_registry
=
32 ShellWindowRegistry::Factory::GetForProfile(
33 profile_
, false /* create */);
34 DCHECK(shell_window_registry
);
35 shell_window_registry
->AddObserver(this);
38 AppLifetimeMonitor::~AppLifetimeMonitor() {}
40 void AppLifetimeMonitor::AddObserver(Observer
* observer
) {
41 observers_
.AddObserver(observer
);
44 void AppLifetimeMonitor::RemoveObserver(Observer
* observer
) {
45 observers_
.RemoveObserver(observer
);
48 void AppLifetimeMonitor::Observe(int type
,
49 const content::NotificationSource
& source
,
50 const content::NotificationDetails
& details
) {
52 case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING
: {
53 ExtensionHost
* host
= content::Details
<ExtensionHost
>(details
).ptr();
54 const Extension
* extension
= host
->extension();
55 if (!extension
|| !extension
->is_platform_app())
58 NotifyAppStart(extension
->id());
62 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED
: {
63 ExtensionHost
* host
= content::Details
<ExtensionHost
>(details
).ptr();
64 const Extension
* extension
= host
->extension();
65 if (!extension
|| !extension
->is_platform_app())
68 NotifyAppStop(extension
->id());
72 case chrome::NOTIFICATION_APP_TERMINATING
: {
73 NotifyChromeTerminating();
79 void AppLifetimeMonitor::OnShellWindowAdded(ShellWindow
* shell_window
) {
80 if (shell_window
->window_type() != ShellWindow::WINDOW_TYPE_DEFAULT
)
83 ShellWindowRegistry::ShellWindowList windows
=
84 ShellWindowRegistry::Get(shell_window
->profile())->
85 GetShellWindowsForApp(shell_window
->extension_id());
86 if (windows
.size() == 1)
87 NotifyAppActivated(shell_window
->extension_id());
90 void AppLifetimeMonitor::OnShellWindowIconChanged(ShellWindow
* shell_window
) {}
92 void AppLifetimeMonitor::OnShellWindowRemoved(ShellWindow
* shell_window
) {
93 ShellWindowRegistry::ShellWindowList windows
=
94 ShellWindowRegistry::Get(shell_window
->profile())->
95 GetShellWindowsForApp(shell_window
->extension_id());
97 NotifyAppDeactivated(shell_window
->extension_id());
100 void AppLifetimeMonitor::Shutdown() {
101 ShellWindowRegistry
* shell_window_registry
=
102 ShellWindowRegistry::Factory::GetForProfile(
103 profile_
, false /* create */);
104 if (shell_window_registry
)
105 shell_window_registry
->RemoveObserver(this);
108 void AppLifetimeMonitor::NotifyAppStart(const std::string
& app_id
) {
109 FOR_EACH_OBSERVER(Observer
, observers_
, OnAppStart(profile_
, app_id
));
112 void AppLifetimeMonitor::NotifyAppActivated(const std::string
& app_id
) {
113 FOR_EACH_OBSERVER(Observer
, observers_
, OnAppActivated(profile_
, app_id
));
116 void AppLifetimeMonitor::NotifyAppDeactivated(const std::string
& app_id
) {
117 FOR_EACH_OBSERVER(Observer
, observers_
, OnAppDeactivated(profile_
, app_id
));
120 void AppLifetimeMonitor::NotifyAppStop(const std::string
& app_id
) {
121 FOR_EACH_OBSERVER(Observer
, observers_
, OnAppStop(profile_
, app_id
));
124 void AppLifetimeMonitor::NotifyChromeTerminating() {
125 FOR_EACH_OBSERVER(Observer
, observers_
, OnChromeTerminating());