| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "apps/app_lifetime_monitor.h" | 5 #include "apps/app_lifetime_monitor.h" |
| 6 | 6 |
| 7 #include "chrome/browser/profiles/profile.h" | 7 #include "content/public/browser/browser_context.h" |
| 8 #include "content/public/browser/notification_details.h" | 8 #include "content/public/browser/notification_details.h" |
| 9 #include "content/public/browser/notification_service.h" | 9 #include "content/public/browser/notification_service.h" |
| 10 #include "extensions/browser/app_window/app_window.h" | 10 #include "extensions/browser/app_window/app_window.h" |
| 11 #include "extensions/browser/extension_host.h" | 11 #include "extensions/browser/extension_host.h" |
| 12 #include "extensions/browser/notification_types.h" | 12 #include "extensions/browser/notification_types.h" |
| 13 #include "extensions/common/extension.h" | 13 #include "extensions/common/extension.h" |
| 14 | 14 |
| 15 namespace apps { | 15 namespace apps { |
| 16 | 16 |
| 17 using extensions::AppWindow; | 17 using extensions::AppWindow; |
| 18 using extensions::AppWindowRegistry; | 18 using extensions::AppWindowRegistry; |
| 19 using extensions::Extension; | 19 using extensions::Extension; |
| 20 using extensions::ExtensionHost; | 20 using extensions::ExtensionHost; |
| 21 | 21 |
| 22 AppLifetimeMonitor::AppLifetimeMonitor(Profile* profile) | 22 AppLifetimeMonitor::AppLifetimeMonitor(content::BrowserContext* context) |
| 23 : profile_(profile) { | 23 : context_(context) { |
| 24 registrar_.Add(this, | 24 registrar_.Add(this, |
| 25 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD, | 25 extensions::NOTIFICATION_EXTENSION_HOST_DID_STOP_FIRST_LOAD, |
| 26 content::NotificationService::AllSources()); | 26 content::NotificationService::AllSources()); |
| 27 registrar_.Add(this, | 27 registrar_.Add(this, |
| 28 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 28 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 29 content::NotificationService::AllSources()); | 29 content::NotificationService::AllSources()); |
| 30 | 30 |
| 31 AppWindowRegistry* app_window_registry = | 31 AppWindowRegistry* app_window_registry = |
| 32 AppWindowRegistry::Factory::GetForBrowserContext(profile_, | 32 AppWindowRegistry::Factory::GetForBrowserContext(context_, |
| 33 false /* create */); | 33 false /* create */); |
| 34 DCHECK(app_window_registry); | 34 DCHECK(app_window_registry); |
| 35 app_window_registry->AddObserver(this); | 35 app_window_registry->AddObserver(this); |
| 36 } | 36 } |
| 37 | 37 |
| 38 AppLifetimeMonitor::~AppLifetimeMonitor() {} | 38 AppLifetimeMonitor::~AppLifetimeMonitor() {} |
| 39 | 39 |
| 40 void AppLifetimeMonitor::AddObserver(Observer* observer) { | 40 void AppLifetimeMonitor::AddObserver(Observer* observer) { |
| 41 observers_.AddObserver(observer); | 41 observers_.AddObserver(observer); |
| 42 } | 42 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 return; | 87 return; |
| 88 | 88 |
| 89 // The app is being activated if this is the first window to become visible. | 89 // The app is being activated if this is the first window to become visible. |
| 90 if (was_hidden && !HasOtherVisibleAppWindows(app_window)) { | 90 if (was_hidden && !HasOtherVisibleAppWindows(app_window)) { |
| 91 NotifyAppActivated(app_window->extension_id()); | 91 NotifyAppActivated(app_window->extension_id()); |
| 92 } | 92 } |
| 93 } | 93 } |
| 94 | 94 |
| 95 void AppLifetimeMonitor::Shutdown() { | 95 void AppLifetimeMonitor::Shutdown() { |
| 96 AppWindowRegistry* app_window_registry = | 96 AppWindowRegistry* app_window_registry = |
| 97 AppWindowRegistry::Factory::GetForBrowserContext(profile_, | 97 AppWindowRegistry::Factory::GetForBrowserContext(context_, |
| 98 false /* create */); | 98 false /* create */); |
| 99 if (app_window_registry) | 99 if (app_window_registry) |
| 100 app_window_registry->RemoveObserver(this); | 100 app_window_registry->RemoveObserver(this); |
| 101 } | 101 } |
| 102 | 102 |
| 103 bool AppLifetimeMonitor::HasOtherVisibleAppWindows( | 103 bool AppLifetimeMonitor::HasOtherVisibleAppWindows( |
| 104 AppWindow* app_window) const { | 104 AppWindow* app_window) const { |
| 105 AppWindowRegistry::AppWindowList windows = | 105 AppWindowRegistry::AppWindowList windows = |
| 106 AppWindowRegistry::Get(app_window->browser_context()) | 106 AppWindowRegistry::Get(app_window->browser_context()) |
| 107 ->GetAppWindowsForApp(app_window->extension_id()); | 107 ->GetAppWindowsForApp(app_window->extension_id()); |
| 108 | 108 |
| 109 for (AppWindowRegistry::AppWindowList::const_iterator i = windows.begin(); | 109 for (AppWindowRegistry::AppWindowList::const_iterator i = windows.begin(); |
| 110 i != windows.end(); | 110 i != windows.end(); |
| 111 ++i) { | 111 ++i) { |
| 112 if (*i != app_window && !(*i)->is_hidden()) | 112 if (*i != app_window && !(*i)->is_hidden()) |
| 113 return true; | 113 return true; |
| 114 } | 114 } |
| 115 return false; | 115 return false; |
| 116 } | 116 } |
| 117 | 117 |
| 118 void AppLifetimeMonitor::NotifyAppStart(const std::string& app_id) { | 118 void AppLifetimeMonitor::NotifyAppStart(const std::string& app_id) { |
| 119 for (auto& observer : observers_) | 119 for (auto& observer : observers_) |
| 120 observer.OnAppStart(profile_, app_id); | 120 observer.OnAppStart(context_, app_id); |
| 121 } | 121 } |
| 122 | 122 |
| 123 void AppLifetimeMonitor::NotifyAppActivated(const std::string& app_id) { | 123 void AppLifetimeMonitor::NotifyAppActivated(const std::string& app_id) { |
| 124 for (auto& observer : observers_) | 124 for (auto& observer : observers_) |
| 125 observer.OnAppActivated(profile_, app_id); | 125 observer.OnAppActivated(context_, app_id); |
| 126 } | 126 } |
| 127 | 127 |
| 128 void AppLifetimeMonitor::NotifyAppDeactivated(const std::string& app_id) { | 128 void AppLifetimeMonitor::NotifyAppDeactivated(const std::string& app_id) { |
| 129 for (auto& observer : observers_) | 129 for (auto& observer : observers_) |
| 130 observer.OnAppDeactivated(profile_, app_id); | 130 observer.OnAppDeactivated(context_, app_id); |
| 131 } | 131 } |
| 132 | 132 |
| 133 void AppLifetimeMonitor::NotifyAppStop(const std::string& app_id) { | 133 void AppLifetimeMonitor::NotifyAppStop(const std::string& app_id) { |
| 134 for (auto& observer : observers_) | 134 for (auto& observer : observers_) |
| 135 observer.OnAppStop(profile_, app_id); | 135 observer.OnAppStop(context_, app_id); |
| 136 } | 136 } |
| 137 | 137 |
| 138 } // namespace apps | 138 } // namespace apps |
| OLD | NEW |