blob: 0e63be8b6bdf4f85899ff28ac7b660cf85f19603 [file] [log] [blame]
[email protected]4b7111f22013-06-18 14:22:121// 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
michaelpg4d80e562017-04-04 01:48:147#include "content/public/browser/browser_context.h"
[email protected]4b7111f22013-06-18 14:22:128#include "content/public/browser/notification_details.h"
9#include "content/public/browser/notification_service.h"
hashimotoad3c6872014-08-29 09:46:5710#include "extensions/browser/app_window/app_window.h"
[email protected]22401dc2014-03-21 01:38:5711#include "extensions/browser/extension_host.h"
[email protected]adf5a102014-07-31 12:44:0612#include "extensions/browser/notification_types.h"
[email protected]e4452d32013-11-15 23:07:4113#include "extensions/common/extension.h"
[email protected]4b7111f22013-06-18 14:22:1214
15namespace apps {
16
hashimotoad3c6872014-08-29 09:46:5717using extensions::AppWindow;
18using extensions::AppWindowRegistry;
[email protected]4b7111f22013-06-18 14:22:1219using extensions::Extension;
20using extensions::ExtensionHost;
[email protected]4b7111f22013-06-18 14:22:1221
michaelpg4d80e562017-04-04 01:48:1422AppLifetimeMonitor::AppLifetimeMonitor(content::BrowserContext* context)
23 : context_(context) {
[email protected]adf5a102014-07-31 12:44:0624 registrar_.Add(this,
kalmanfd474fa2015-03-16 22:30:5725 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD,
[email protected]adf5a102014-07-31 12:44:0626 content::NotificationService::AllSources());
27 registrar_.Add(this,
28 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
29 content::NotificationService::AllSources());
[email protected]4b7111f22013-06-18 14:22:1230
[email protected]dbb03fb2014-02-15 05:36:3331 AppWindowRegistry* app_window_registry =
michaelpg4d80e562017-04-04 01:48:1432 AppWindowRegistry::Factory::GetForBrowserContext(context_,
[email protected]dbb03fb2014-02-15 05:36:3333 false /* create */);
34 DCHECK(app_window_registry);
35 app_window_registry->AddObserver(this);
[email protected]4b7111f22013-06-18 14:22:1236}
37
Chris Watkinsee8488b2017-11-27 04:06:5638AppLifetimeMonitor::~AppLifetimeMonitor() = default;
[email protected]4b7111f22013-06-18 14:22:1239
40void AppLifetimeMonitor::AddObserver(Observer* observer) {
41 observers_.AddObserver(observer);
42}
43
44void AppLifetimeMonitor::RemoveObserver(Observer* observer) {
45 observers_.RemoveObserver(observer);
46}
47
48void AppLifetimeMonitor::Observe(int type,
49 const content::NotificationSource& source,
50 const content::NotificationDetails& details) {
51 switch (type) {
kalmanfd474fa2015-03-16 22:30:5752 case extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD: {
[email protected]4b7111f22013-06-18 14:22:1253 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
54 const Extension* extension = host->extension();
55 if (!extension || !extension->is_platform_app())
56 return;
57
58 NotifyAppStart(extension->id());
59 break;
60 }
61
[email protected]adf5a102014-07-31 12:44:0662 case extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
[email protected]4b7111f22013-06-18 14:22:1263 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
64 const Extension* extension = host->extension();
65 if (!extension || !extension->is_platform_app())
66 return;
67
68 NotifyAppStop(extension->id());
69 break;
70 }
[email protected]4b7111f22013-06-18 14:22:1271 }
72}
73
[email protected]7aadf69a42014-05-15 07:15:5074void AppLifetimeMonitor::OnAppWindowRemoved(AppWindow* app_window) {
jackhouccd7df12014-11-14 02:00:2775 if (!HasOtherVisibleAppWindows(app_window))
[email protected]7aadf69a42014-05-15 07:15:5076 NotifyAppDeactivated(app_window->extension_id());
77}
78
79void AppLifetimeMonitor::OnAppWindowHidden(AppWindow* app_window) {
jackhouccd7df12014-11-14 02:00:2780 if (!HasOtherVisibleAppWindows(app_window))
[email protected]7aadf69a42014-05-15 07:15:5081 NotifyAppDeactivated(app_window->extension_id());
82}
83
jackhouccd7df12014-11-14 02:00:2784void AppLifetimeMonitor::OnAppWindowShown(AppWindow* app_window,
85 bool was_hidden) {
[email protected]dbb03fb2014-02-15 05:36:3386 if (app_window->window_type() != AppWindow::WINDOW_TYPE_DEFAULT)
[email protected]4b7111f22013-06-18 14:22:1287 return;
88
jackhouccd7df12014-11-14 02:00:2789 // The app is being activated if this is the first window to become visible.
90 if (was_hidden && !HasOtherVisibleAppWindows(app_window)) {
[email protected]dbb03fb2014-02-15 05:36:3391 NotifyAppActivated(app_window->extension_id());
jackhouccd7df12014-11-14 02:00:2792 }
[email protected]4b7111f22013-06-18 14:22:1293}
94
[email protected]4b7111f22013-06-18 14:22:1295void AppLifetimeMonitor::Shutdown() {
[email protected]dbb03fb2014-02-15 05:36:3396 AppWindowRegistry* app_window_registry =
michaelpg4d80e562017-04-04 01:48:1497 AppWindowRegistry::Factory::GetForBrowserContext(context_,
[email protected]dbb03fb2014-02-15 05:36:3398 false /* create */);
99 if (app_window_registry)
100 app_window_registry->RemoveObserver(this);
[email protected]4b7111f22013-06-18 14:22:12101}
102
jackhouccd7df12014-11-14 02:00:27103bool AppLifetimeMonitor::HasOtherVisibleAppWindows(
104 AppWindow* app_window) const {
[email protected]7aadf69a42014-05-15 07:15:50105 AppWindowRegistry::AppWindowList windows =
106 AppWindowRegistry::Get(app_window->browser_context())
107 ->GetAppWindowsForApp(app_window->extension_id());
108
109 for (AppWindowRegistry::AppWindowList::const_iterator i = windows.begin();
110 i != windows.end();
111 ++i) {
jackhouccd7df12014-11-14 02:00:27112 if (*i != app_window && !(*i)->is_hidden())
[email protected]7aadf69a42014-05-15 07:15:50113 return true;
114 }
115 return false;
116}
117
[email protected]4b7111f22013-06-18 14:22:12118void AppLifetimeMonitor::NotifyAppStart(const std::string& app_id) {
ericwilligers01080f92016-10-17 22:55:09119 for (auto& observer : observers_)
michaelpg4d80e562017-04-04 01:48:14120 observer.OnAppStart(context_, app_id);
[email protected]4b7111f22013-06-18 14:22:12121}
122
123void AppLifetimeMonitor::NotifyAppActivated(const std::string& app_id) {
ericwilligers01080f92016-10-17 22:55:09124 for (auto& observer : observers_)
michaelpg4d80e562017-04-04 01:48:14125 observer.OnAppActivated(context_, app_id);
[email protected]4b7111f22013-06-18 14:22:12126}
127
128void AppLifetimeMonitor::NotifyAppDeactivated(const std::string& app_id) {
ericwilligers01080f92016-10-17 22:55:09129 for (auto& observer : observers_)
michaelpg4d80e562017-04-04 01:48:14130 observer.OnAppDeactivated(context_, app_id);
[email protected]4b7111f22013-06-18 14:22:12131}
132
133void AppLifetimeMonitor::NotifyAppStop(const std::string& app_id) {
ericwilligers01080f92016-10-17 22:55:09134 for (auto& observer : observers_)
michaelpg4d80e562017-04-04 01:48:14135 observer.OnAppStop(context_, app_id);
[email protected]4b7111f22013-06-18 14:22:12136}
137
[email protected]4b7111f22013-06-18 14:22:12138} // namespace apps