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 "content/public/browser/browser_context.h"
8 #include "content/public/browser/notification_details.h"
9 #include "content/public/browser/notification_service.h"
10 #include "extensions/browser/app_window/app_window.h"
11 #include "extensions/browser/extension_host.h"
12 #include "extensions/browser/notification_types.h"
13 #include "extensions/common/extension.h"
17 using extensions::AppWindow;
18 using extensions::AppWindowRegistry;
19 using extensions::Extension;
20 using extensions::ExtensionHost;
22 AppLifetimeMonitor::AppLifetimeMonitor(content::BrowserContext* context)
25 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD,
26 content::NotificationService::AllSources());
28 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
29 content::NotificationService::AllSources());
31 AppWindowRegistry* app_window_registry =
32 AppWindowRegistry::Factory::GetForBrowserContext(context_,
34 DCHECK(app_window_registry);
35 app_window_registry->AddObserver(this);
38 AppLifetimeMonitor::~AppLifetimeMonitor() = default;
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 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD: {
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 extensions::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());
74 void AppLifetimeMonitor::OnAppWindowRemoved(AppWindow* app_window) {
75 if (!HasOtherVisibleAppWindows(app_window))
76 NotifyAppDeactivated(app_window->extension_id());
79 void AppLifetimeMonitor::OnAppWindowHidden(AppWindow* app_window) {
80 if (!HasOtherVisibleAppWindows(app_window))
81 NotifyAppDeactivated(app_window->extension_id());
84 void AppLifetimeMonitor::OnAppWindowShown(AppWindow* app_window,
86 if (app_window->window_type() != AppWindow::WINDOW_TYPE_DEFAULT)
89 // The app is being activated if this is the first window to become visible.
90 if (was_hidden && !HasOtherVisibleAppWindows(app_window)) {
91 NotifyAppActivated(app_window->extension_id());
95 void AppLifetimeMonitor::Shutdown() {
96 AppWindowRegistry* app_window_registry =
97 AppWindowRegistry::Factory::GetForBrowserContext(context_,
99 if (app_window_registry)
100 app_window_registry->RemoveObserver(this);
103 bool AppLifetimeMonitor::HasOtherVisibleAppWindows(
104 AppWindow* app_window) const {
105 AppWindowRegistry::AppWindowList windows =
106 AppWindowRegistry::Get(app_window->browser_context())
107 ->GetAppWindowsForApp(app_window->extension_id());
109 for (AppWindowRegistry::AppWindowList::const_iterator i = windows.begin();
112 if (*i != app_window && !(*i)->is_hidden())
118 void AppLifetimeMonitor::NotifyAppStart(const std::string& app_id) {
119 for (auto& observer : observers_)
120 observer.OnAppStart(context_, app_id);
123 void AppLifetimeMonitor::NotifyAppActivated(const std::string& app_id) {
124 for (auto& observer : observers_)
125 observer.OnAppActivated(context_, app_id);
128 void AppLifetimeMonitor::NotifyAppDeactivated(const std::string& app_id) {
129 for (auto& observer : observers_)
130 observer.OnAppDeactivated(context_, app_id);
133 void AppLifetimeMonitor::NotifyAppStop(const std::string& app_id) {
134 for (auto& observer : observers_)
135 observer.OnAppStop(context_, app_id);