Update V8 to version 4.6.48.
[chromium-blink-merge.git] / apps / app_lifetime_monitor.cc
blob6051f0c1c222f58e87d483d1af11f8667dbf4268
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"
16 namespace apps {
18 using extensions::AppWindow;
19 using extensions::AppWindowRegistry;
20 using extensions::Extension;
21 using extensions::ExtensionHost;
23 AppLifetimeMonitor::AppLifetimeMonitor(Profile* profile)
24 : profile_(profile) {
25 registrar_.Add(this,
26 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD,
27 content::NotificationService::AllSources());
28 registrar_.Add(this,
29 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
30 content::NotificationService::AllSources());
31 registrar_.Add(
32 this, chrome::NOTIFICATION_APP_TERMINATING,
33 content::NotificationService::AllSources());
35 AppWindowRegistry* app_window_registry =
36 AppWindowRegistry::Factory::GetForBrowserContext(profile_,
37 false /* create */);
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) {
55 switch (type) {
56 case extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD: {
57 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
58 const Extension* extension = host->extension();
59 if (!extension || !extension->is_platform_app())
60 return;
62 NotifyAppStart(extension->id());
63 break;
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())
70 return;
72 NotifyAppStop(extension->id());
73 break;
76 case chrome::NOTIFICATION_APP_TERMINATING: {
77 NotifyChromeTerminating();
78 break;
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,
94 bool was_hidden) {
95 if (app_window->window_type() != AppWindow::WINDOW_TYPE_DEFAULT)
96 return;
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_,
107 false /* create */);
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();
119 i != windows.end();
120 ++i) {
121 if (*i != app_window && !(*i)->is_hidden())
122 return true;
124 return false;
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());
147 } // namespace apps