Upload upstream chromium 76.0.3809.146
[platform/framework/web/chromium-efl.git] / apps / app_lifetime_monitor.cc
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 "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"
14
15 namespace apps {
16
17 using extensions::AppWindow;
18 using extensions::AppWindowRegistry;
19 using extensions::Extension;
20 using extensions::ExtensionHost;
21
22 AppLifetimeMonitor::AppLifetimeMonitor(content::BrowserContext* context)
23     : context_(context) {
24   registrar_.Add(this,
25                  extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD,
26                  content::NotificationService::AllSources());
27   registrar_.Add(this,
28                  extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
29                  content::NotificationService::AllSources());
30
31   AppWindowRegistry* app_window_registry =
32       AppWindowRegistry::Factory::GetForBrowserContext(context_,
33                                                        false /* create */);
34   DCHECK(app_window_registry);
35   app_window_registry->AddObserver(this);
36 }
37
38 AppLifetimeMonitor::~AppLifetimeMonitor() = default;
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) {
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())
56         return;
57
58       NotifyAppStart(extension->id());
59       break;
60     }
61
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())
66         return;
67
68       NotifyAppStop(extension->id());
69       break;
70     }
71   }
72 }
73
74 void AppLifetimeMonitor::OnAppWindowRemoved(AppWindow* app_window) {
75   if (!HasOtherVisibleAppWindows(app_window))
76     NotifyAppDeactivated(app_window->extension_id());
77 }
78
79 void AppLifetimeMonitor::OnAppWindowHidden(AppWindow* app_window) {
80   if (!HasOtherVisibleAppWindows(app_window))
81     NotifyAppDeactivated(app_window->extension_id());
82 }
83
84 void AppLifetimeMonitor::OnAppWindowShown(AppWindow* app_window,
85                                           bool was_hidden) {
86   if (app_window->window_type() != AppWindow::WINDOW_TYPE_DEFAULT)
87     return;
88
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());
92   }
93 }
94
95 void AppLifetimeMonitor::Shutdown() {
96   AppWindowRegistry* app_window_registry =
97       AppWindowRegistry::Factory::GetForBrowserContext(context_,
98                                                        false /* create */);
99   if (app_window_registry)
100     app_window_registry->RemoveObserver(this);
101 }
102
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());
108
109   for (AppWindowRegistry::AppWindowList::const_iterator i = windows.begin();
110        i != windows.end();
111        ++i) {
112     if (*i != app_window && !(*i)->is_hidden())
113       return true;
114   }
115   return false;
116 }
117
118 void AppLifetimeMonitor::NotifyAppStart(const std::string& app_id) {
119   for (auto& observer : observers_)
120     observer.OnAppStart(context_, app_id);
121 }
122
123 void AppLifetimeMonitor::NotifyAppActivated(const std::string& app_id) {
124   for (auto& observer : observers_)
125     observer.OnAppActivated(context_, app_id);
126 }
127
128 void AppLifetimeMonitor::NotifyAppDeactivated(const std::string& app_id) {
129   for (auto& observer : observers_)
130     observer.OnAppDeactivated(context_, app_id);
131 }
132
133 void AppLifetimeMonitor::NotifyAppStop(const std::string& app_id) {
134   for (auto& observer : observers_)
135     observer.OnAppStop(context_, app_id);
136 }
137
138 }  // namespace apps