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.
4
5 #include "apps/app_lifetime_monitor.h"
6
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/browser/profiles/profile.h"
11 #include "content/public/browser/notification_details.h"
12 #include "content/public/browser/notification_service.h"
13 #include "extensions/common/extension.h"
14
15 namespace apps {
16
17 using extensions::Extension;
18 using extensions::ExtensionHost;
19
AppLifetimeMonitor(Profile * profile)20 AppLifetimeMonitor::AppLifetimeMonitor(Profile* profile)
21 : profile_(profile) {
22 registrar_.Add(
23 this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
24 content::NotificationService::AllSources());
25 registrar_.Add(
26 this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED,
27 content::NotificationService::AllSources());
28 registrar_.Add(
29 this, chrome::NOTIFICATION_APP_TERMINATING,
30 content::NotificationService::AllSources());
31
32 ShellWindowRegistry* shell_window_registry =
33 ShellWindowRegistry::Factory::GetForBrowserContext(
34 profile_, false /* create */);
35 DCHECK(shell_window_registry);
36 shell_window_registry->AddObserver(this);
37 }
38
~AppLifetimeMonitor()39 AppLifetimeMonitor::~AppLifetimeMonitor() {}
40
AddObserver(Observer * observer)41 void AppLifetimeMonitor::AddObserver(Observer* observer) {
42 observers_.AddObserver(observer);
43 }
44
RemoveObserver(Observer * observer)45 void AppLifetimeMonitor::RemoveObserver(Observer* observer) {
46 observers_.RemoveObserver(observer);
47 }
48
Observe(int type,const content::NotificationSource & source,const content::NotificationDetails & details)49 void AppLifetimeMonitor::Observe(int type,
50 const content::NotificationSource& source,
51 const content::NotificationDetails& details) {
52 switch (type) {
53 case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: {
54 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
55 const Extension* extension = host->extension();
56 if (!extension || !extension->is_platform_app())
57 return;
58
59 NotifyAppStart(extension->id());
60 break;
61 }
62
63 case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
64 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
65 const Extension* extension = host->extension();
66 if (!extension || !extension->is_platform_app())
67 return;
68
69 NotifyAppStop(extension->id());
70 break;
71 }
72
73 case chrome::NOTIFICATION_APP_TERMINATING: {
74 NotifyChromeTerminating();
75 break;
76 }
77 }
78 }
79
OnShellWindowAdded(ShellWindow * shell_window)80 void AppLifetimeMonitor::OnShellWindowAdded(ShellWindow* shell_window) {
81 if (shell_window->window_type() != ShellWindow::WINDOW_TYPE_DEFAULT)
82 return;
83
84 ShellWindowRegistry::ShellWindowList windows =
85 ShellWindowRegistry::Get(shell_window->profile())->
86 GetShellWindowsForApp(shell_window->extension_id());
87 if (windows.size() == 1)
88 NotifyAppActivated(shell_window->extension_id());
89 }
90
OnShellWindowIconChanged(ShellWindow * shell_window)91 void AppLifetimeMonitor::OnShellWindowIconChanged(ShellWindow* shell_window) {}
92
OnShellWindowRemoved(ShellWindow * shell_window)93 void AppLifetimeMonitor::OnShellWindowRemoved(ShellWindow* shell_window) {
94 ShellWindowRegistry::ShellWindowList windows =
95 ShellWindowRegistry::Get(shell_window->profile())->
96 GetShellWindowsForApp(shell_window->extension_id());
97 if (windows.empty())
98 NotifyAppDeactivated(shell_window->extension_id());
99 }
100
Shutdown()101 void AppLifetimeMonitor::Shutdown() {
102 ShellWindowRegistry* shell_window_registry =
103 ShellWindowRegistry::Factory::GetForBrowserContext(
104 profile_, false /* create */);
105 if (shell_window_registry)
106 shell_window_registry->RemoveObserver(this);
107 }
108
NotifyAppStart(const std::string & app_id)109 void AppLifetimeMonitor::NotifyAppStart(const std::string& app_id) {
110 FOR_EACH_OBSERVER(Observer, observers_, OnAppStart(profile_, app_id));
111 }
112
NotifyAppActivated(const std::string & app_id)113 void AppLifetimeMonitor::NotifyAppActivated(const std::string& app_id) {
114 FOR_EACH_OBSERVER(Observer, observers_, OnAppActivated(profile_, app_id));
115 }
116
NotifyAppDeactivated(const std::string & app_id)117 void AppLifetimeMonitor::NotifyAppDeactivated(const std::string& app_id) {
118 FOR_EACH_OBSERVER(Observer, observers_, OnAppDeactivated(profile_, app_id));
119 }
120
NotifyAppStop(const std::string & app_id)121 void AppLifetimeMonitor::NotifyAppStop(const std::string& app_id) {
122 FOR_EACH_OBSERVER(Observer, observers_, OnAppStop(profile_, app_id));
123 }
124
NotifyChromeTerminating()125 void AppLifetimeMonitor::NotifyChromeTerminating() {
126 FOR_EACH_OBSERVER(Observer, observers_, OnChromeTerminating());
127 }
128
129 } // namespace apps
130