[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 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 | |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 7 | #include "content/public/browser/browser_context.h" |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 8 | #include "content/public/browser/notification_details.h" |
| 9 | #include "content/public/browser/notification_service.h" |
hashimoto | ad3c687 | 2014-08-29 09:46:57 | [diff] [blame] | 10 | #include "extensions/browser/app_window/app_window.h" |
[email protected] | 22401dc | 2014-03-21 01:38:57 | [diff] [blame] | 11 | #include "extensions/browser/extension_host.h" |
[email protected] | adf5a10 | 2014-07-31 12:44:06 | [diff] [blame] | 12 | #include "extensions/browser/notification_types.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 13 | #include "extensions/common/extension.h" |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 14 | |
| 15 | namespace apps { |
| 16 | |
hashimoto | ad3c687 | 2014-08-29 09:46:57 | [diff] [blame] | 17 | using extensions::AppWindow; |
| 18 | using extensions::AppWindowRegistry; |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 19 | using extensions::Extension; |
| 20 | using extensions::ExtensionHost; |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 21 | |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 22 | AppLifetimeMonitor::AppLifetimeMonitor(content::BrowserContext* context) |
| 23 | : context_(context) { |
[email protected] | adf5a10 | 2014-07-31 12:44:06 | [diff] [blame] | 24 | registrar_.Add(this, |
kalman | fd474fa | 2015-03-16 22:30:57 | [diff] [blame] | 25 | extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD, |
[email protected] | adf5a10 | 2014-07-31 12:44:06 | [diff] [blame] | 26 | content::NotificationService::AllSources()); |
| 27 | registrar_.Add(this, |
| 28 | extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 29 | content::NotificationService::AllSources()); |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 30 | |
[email protected] | dbb03fb | 2014-02-15 05:36:33 | [diff] [blame] | 31 | AppWindowRegistry* app_window_registry = |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 32 | AppWindowRegistry::Factory::GetForBrowserContext(context_, |
[email protected] | dbb03fb | 2014-02-15 05:36:33 | [diff] [blame] | 33 | false /* create */); |
| 34 | DCHECK(app_window_registry); |
| 35 | app_window_registry->AddObserver(this); |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 36 | } |
| 37 | |
Chris Watkins | ee8488b | 2017-11-27 04:06:56 | [diff] [blame] | 38 | AppLifetimeMonitor::~AppLifetimeMonitor() = default; |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 39 | |
| 40 | void AppLifetimeMonitor::AddObserver(Observer* observer) { |
| 41 | observers_.AddObserver(observer); |
| 42 | } |
| 43 | |
| 44 | void AppLifetimeMonitor::RemoveObserver(Observer* observer) { |
| 45 | observers_.RemoveObserver(observer); |
| 46 | } |
| 47 | |
| 48 | void AppLifetimeMonitor::Observe(int type, |
| 49 | const content::NotificationSource& source, |
| 50 | const content::NotificationDetails& details) { |
| 51 | switch (type) { |
kalman | fd474fa | 2015-03-16 22:30:57 | [diff] [blame] | 52 | case extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD: { |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 53 | 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] | adf5a10 | 2014-07-31 12:44:06 | [diff] [blame] | 62 | case extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED: { |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 63 | 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] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | |
[email protected] | 7aadf69a4 | 2014-05-15 07:15:50 | [diff] [blame] | 74 | void AppLifetimeMonitor::OnAppWindowRemoved(AppWindow* app_window) { |
jackhou | ccd7df1 | 2014-11-14 02:00:27 | [diff] [blame] | 75 | if (!HasOtherVisibleAppWindows(app_window)) |
[email protected] | 7aadf69a4 | 2014-05-15 07:15:50 | [diff] [blame] | 76 | NotifyAppDeactivated(app_window->extension_id()); |
| 77 | } |
| 78 | |
| 79 | void AppLifetimeMonitor::OnAppWindowHidden(AppWindow* app_window) { |
jackhou | ccd7df1 | 2014-11-14 02:00:27 | [diff] [blame] | 80 | if (!HasOtherVisibleAppWindows(app_window)) |
[email protected] | 7aadf69a4 | 2014-05-15 07:15:50 | [diff] [blame] | 81 | NotifyAppDeactivated(app_window->extension_id()); |
| 82 | } |
| 83 | |
jackhou | ccd7df1 | 2014-11-14 02:00:27 | [diff] [blame] | 84 | void AppLifetimeMonitor::OnAppWindowShown(AppWindow* app_window, |
| 85 | bool was_hidden) { |
[email protected] | dbb03fb | 2014-02-15 05:36:33 | [diff] [blame] | 86 | if (app_window->window_type() != AppWindow::WINDOW_TYPE_DEFAULT) |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 87 | return; |
| 88 | |
jackhou | ccd7df1 | 2014-11-14 02:00:27 | [diff] [blame] | 89 | // The app is being activated if this is the first window to become visible. |
| 90 | if (was_hidden && !HasOtherVisibleAppWindows(app_window)) { |
[email protected] | dbb03fb | 2014-02-15 05:36:33 | [diff] [blame] | 91 | NotifyAppActivated(app_window->extension_id()); |
jackhou | ccd7df1 | 2014-11-14 02:00:27 | [diff] [blame] | 92 | } |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 93 | } |
| 94 | |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 95 | void AppLifetimeMonitor::Shutdown() { |
[email protected] | dbb03fb | 2014-02-15 05:36:33 | [diff] [blame] | 96 | AppWindowRegistry* app_window_registry = |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 97 | AppWindowRegistry::Factory::GetForBrowserContext(context_, |
[email protected] | dbb03fb | 2014-02-15 05:36:33 | [diff] [blame] | 98 | false /* create */); |
| 99 | if (app_window_registry) |
| 100 | app_window_registry->RemoveObserver(this); |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 101 | } |
| 102 | |
jackhou | ccd7df1 | 2014-11-14 02:00:27 | [diff] [blame] | 103 | bool AppLifetimeMonitor::HasOtherVisibleAppWindows( |
| 104 | AppWindow* app_window) const { |
[email protected] | 7aadf69a4 | 2014-05-15 07:15:50 | [diff] [blame] | 105 | 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) { |
jackhou | ccd7df1 | 2014-11-14 02:00:27 | [diff] [blame] | 112 | if (*i != app_window && !(*i)->is_hidden()) |
[email protected] | 7aadf69a4 | 2014-05-15 07:15:50 | [diff] [blame] | 113 | return true; |
| 114 | } |
| 115 | return false; |
| 116 | } |
| 117 | |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 118 | void AppLifetimeMonitor::NotifyAppStart(const std::string& app_id) { |
ericwilligers | 01080f9 | 2016-10-17 22:55:09 | [diff] [blame] | 119 | for (auto& observer : observers_) |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 120 | observer.OnAppStart(context_, app_id); |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | void AppLifetimeMonitor::NotifyAppActivated(const std::string& app_id) { |
ericwilligers | 01080f9 | 2016-10-17 22:55:09 | [diff] [blame] | 124 | for (auto& observer : observers_) |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 125 | observer.OnAppActivated(context_, app_id); |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void AppLifetimeMonitor::NotifyAppDeactivated(const std::string& app_id) { |
ericwilligers | 01080f9 | 2016-10-17 22:55:09 | [diff] [blame] | 129 | for (auto& observer : observers_) |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 130 | observer.OnAppDeactivated(context_, app_id); |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void AppLifetimeMonitor::NotifyAppStop(const std::string& app_id) { |
ericwilligers | 01080f9 | 2016-10-17 22:55:09 | [diff] [blame] | 134 | for (auto& observer : observers_) |
michaelpg | 4d80e56 | 2017-04-04 01:48:14 | [diff] [blame] | 135 | observer.OnAppStop(context_, app_id); |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 136 | } |
| 137 | |
[email protected] | 4b7111f2 | 2013-06-18 14:22:12 | [diff] [blame] | 138 | } // namespace apps |