Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / 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 "apps/app_window.h"
8 #include "chrome/browser/chrome_notification_types.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "content/public/browser/notification_details.h"
11 #include "content/public/browser/notification_service.h"
12 #include "extensions/browser/extension_host.h"
13 #include "extensions/browser/notification_types.h"
14 #include "extensions/common/extension.h"
15
16 namespace apps {
17
18 using extensions::Extension;
19 using extensions::ExtensionHost;
20
21 AppLifetimeMonitor::AppLifetimeMonitor(Profile* profile)
22     : profile_(profile) {
23   registrar_.Add(this,
24                  extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
25                  content::NotificationService::AllSources());
26   registrar_.Add(this,
27                  extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
28                  content::NotificationService::AllSources());
29   registrar_.Add(
30       this, chrome::NOTIFICATION_APP_TERMINATING,
31       content::NotificationService::AllSources());
32
33   AppWindowRegistry* app_window_registry =
34       AppWindowRegistry::Factory::GetForBrowserContext(profile_,
35                                                        false /* create */);
36   DCHECK(app_window_registry);
37   app_window_registry->AddObserver(this);
38 }
39
40 AppLifetimeMonitor::~AppLifetimeMonitor() {}
41
42 void AppLifetimeMonitor::AddObserver(Observer* observer) {
43   observers_.AddObserver(observer);
44 }
45
46 void AppLifetimeMonitor::RemoveObserver(Observer* observer) {
47   observers_.RemoveObserver(observer);
48 }
49
50 void AppLifetimeMonitor::Observe(int type,
51                                 const content::NotificationSource& source,
52                                 const content::NotificationDetails& details) {
53   switch (type) {
54     case extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: {
55       ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
56       const Extension* extension = host->extension();
57       if (!extension || !extension->is_platform_app())
58         return;
59
60       NotifyAppStart(extension->id());
61       break;
62     }
63
64     case extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED: {
65       ExtensionHost* host = content::Details<ExtensionHost>(details).ptr();
66       const Extension* extension = host->extension();
67       if (!extension || !extension->is_platform_app())
68         return;
69
70       NotifyAppStop(extension->id());
71       break;
72     }
73
74     case chrome::NOTIFICATION_APP_TERMINATING: {
75       NotifyChromeTerminating();
76       break;
77     }
78   }
79 }
80
81 void AppLifetimeMonitor::OnAppWindowRemoved(AppWindow* app_window) {
82   if (!HasVisibleAppWindows(app_window))
83     NotifyAppDeactivated(app_window->extension_id());
84 }
85
86 void AppLifetimeMonitor::OnAppWindowHidden(AppWindow* app_window) {
87   if (!HasVisibleAppWindows(app_window))
88     NotifyAppDeactivated(app_window->extension_id());
89 }
90
91 void AppLifetimeMonitor::OnAppWindowShown(AppWindow* app_window) {
92   if (app_window->window_type() != AppWindow::WINDOW_TYPE_DEFAULT)
93     return;
94
95   if (HasVisibleAppWindows(app_window))
96     NotifyAppActivated(app_window->extension_id());
97 }
98
99 void AppLifetimeMonitor::Shutdown() {
100   AppWindowRegistry* app_window_registry =
101       AppWindowRegistry::Factory::GetForBrowserContext(profile_,
102                                                        false /* create */);
103   if (app_window_registry)
104     app_window_registry->RemoveObserver(this);
105 }
106
107 bool AppLifetimeMonitor::HasVisibleAppWindows(AppWindow* app_window) const {
108   AppWindowRegistry::AppWindowList windows =
109       AppWindowRegistry::Get(app_window->browser_context())
110           ->GetAppWindowsForApp(app_window->extension_id());
111
112   for (AppWindowRegistry::AppWindowList::const_iterator i = windows.begin();
113        i != windows.end();
114        ++i) {
115     if (!(*i)->is_hidden())
116       return true;
117   }
118   return false;
119 }
120
121 void AppLifetimeMonitor::NotifyAppStart(const std::string& app_id) {
122   FOR_EACH_OBSERVER(Observer, observers_, OnAppStart(profile_, app_id));
123 }
124
125 void AppLifetimeMonitor::NotifyAppActivated(const std::string& app_id) {
126   FOR_EACH_OBSERVER(Observer, observers_, OnAppActivated(profile_, app_id));
127 }
128
129 void AppLifetimeMonitor::NotifyAppDeactivated(const std::string& app_id) {
130   FOR_EACH_OBSERVER(Observer, observers_, OnAppDeactivated(profile_, app_id));
131 }
132
133 void AppLifetimeMonitor::NotifyAppStop(const std::string& app_id) {
134   FOR_EACH_OBSERVER(Observer, observers_, OnAppStop(profile_, app_id));
135 }
136
137 void AppLifetimeMonitor::NotifyChromeTerminating() {
138   FOR_EACH_OBSERVER(Observer, observers_, OnChromeTerminating());
139 }
140
141 }  // namespace apps