From d54b3f490de80c708e335714872e44eba322342b Mon Sep 17 00:00:00 2001 From: "jackhou@chromium.org" Date: Thu, 6 Jun 2013 13:38:37 +0000 Subject: Factor out AppLifetimeMonitor. AppLifetimeMonitor listens to extension host notifications and observes ShellWindowRegistry. It sends out notifications when: - an app starts - the first shell window opens - the last shell window closes - the app stops BUG= Review URL: https://chromiumcodereview.appspot.com/16412002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@204487 0039d316-1c4b-4281-b951-d872f2087c98 --- apps/app_restore_service.cc | 85 ++++++++++++++------------------------------- 1 file changed, 27 insertions(+), 58 deletions(-) (limited to 'apps/app_restore_service.cc') diff --git a/apps/app_restore_service.cc b/apps/app_restore_service.cc index 88ac811..dd9639c 100644 --- a/apps/app_restore_service.cc +++ b/apps/app_restore_service.cc @@ -4,27 +4,24 @@ #include "apps/app_restore_service.h" +#include "apps/app_lifetime_monitor_factory.h" #include "apps/saved_files_service.h" #include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h" -#include "chrome/browser/extensions/event_router.h" #include "chrome/browser/extensions/extension_host.h" #include "chrome/browser/extensions/extension_prefs.h" #include "chrome/browser/extensions/extension_service.h" #include "chrome/browser/extensions/extension_system.h" #include "chrome/browser/extensions/platform_app_launcher.h" -#include "chrome/browser/ui/extensions/shell_window.h" #include "chrome/common/chrome_notification_types.h" #include "chrome/common/extensions/extension.h" #include "chrome/common/extensions/extension_set.h" #include "content/public/browser/notification_details.h" #include "content/public/browser/notification_service.h" -#include "content/public/browser/notification_types.h" #if defined(OS_WIN) #include "win8/util/win8_util.h" #endif -using extensions::AppEventRouter; using extensions::Extension; using extensions::ExtensionHost; using extensions::ExtensionPrefs; @@ -48,16 +45,10 @@ bool AppRestoreService::ShouldRestoreApps(bool is_browser_restart) { AppRestoreService::AppRestoreService(Profile* profile) : profile_(profile) { - registrar_.Add( - this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, - content::NotificationService::AllSources()); - registrar_.Add( - this, chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, - content::NotificationService::AllSources()); + StartObservingAppLifetime(); registrar_.Add( this, chrome::NOTIFICATION_APP_TERMINATING, content::NotificationService::AllSources()); - StartObservingShellWindows(); } void AppRestoreService::HandleStartup(bool should_restore_apps) { @@ -88,47 +79,39 @@ void AppRestoreService::Observe(int type, const content::NotificationSource& source, const content::NotificationDetails& details) { switch (type) { - case chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING: { - ExtensionHost* host = content::Details(details).ptr(); - const Extension* extension = host->extension(); - if (extension && extension->is_platform_app()) - RecordAppStart(extension->id()); - break; - } - - case chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED: { - ExtensionHost* host = content::Details(details).ptr(); - const Extension* extension = host->extension(); - if (extension && extension->is_platform_app()) - RecordAppStop(extension->id()); - break; - } - case chrome::NOTIFICATION_APP_TERMINATING: { // Stop listening to NOTIFICATION_EXTENSION_HOST_DESTROYED in particular // as all extension hosts will be destroyed as a result of shutdown. registrar_.RemoveAll(); - // Stop listening to the ShellWindowRegistry for window closes, because - // all windows will be closed as a result of shutdown. - StopObservingShellWindows(); + // We want to preserve the state when the app begins terminating, so stop + // listening to app lifetime events. + StopObservingAppLifetime(); break; } } } -void AppRestoreService::OnShellWindowAdded(ShellWindow* shell_window) { - RecordIfAppHasWindows(shell_window->extension_id()); +void AppRestoreService::OnAppStart(Profile* profile, + const std::string& app_id) { + RecordAppStart(app_id); } -void AppRestoreService::OnShellWindowIconChanged(ShellWindow* shell_window) { +void AppRestoreService::OnAppActivated(Profile* profile, + const std::string& app_id) { + RecordAppActiveState(app_id, true); } -void AppRestoreService::OnShellWindowRemoved(ShellWindow* shell_window) { - RecordIfAppHasWindows(shell_window->extension_id()); +void AppRestoreService::OnAppDeactivated(Profile* profile, + const std::string& app_id) { + RecordAppActiveState(app_id, false); +} + +void AppRestoreService::OnAppStop(Profile* profile, const std::string& app_id) { + RecordAppStop(app_id); } void AppRestoreService::Shutdown() { - StopObservingShellWindows(); + StopObservingAppLifetime(); } void AppRestoreService::RecordAppStart(const std::string& extension_id) { @@ -143,44 +126,30 @@ void AppRestoreService::RecordAppStop(const std::string& extension_id) { extension_prefs->SetExtensionRunning(extension_id, false); } -void AppRestoreService::RecordIfAppHasWindows( - const std::string& id) { +void AppRestoreService::RecordAppActiveState(const std::string& id, + bool is_active) { ExtensionService* extension_service = ExtensionSystem::Get(profile_)->extension_service(); ExtensionPrefs* extension_prefs = extension_service->extension_prefs(); // If the extension isn't running then we will already have recorded whether - // it had windows or not. + // it is active or not. if (!extension_prefs->IsExtensionRunning(id)) return; - extensions::ShellWindowRegistry* shell_window_registry = - extensions::ShellWindowRegistry::Factory::GetForProfile( - profile_, false /* create */); - if (!shell_window_registry) - return; - bool has_windows = !shell_window_registry->GetShellWindowsForApp(id).empty(); - extension_prefs->SetHasWindows(id, has_windows); + extension_prefs->SetIsActive(id, is_active); } void AppRestoreService::RestoreApp(const Extension* extension) { extensions::RestartPlatformApp(profile_, extension); } -void AppRestoreService::StartObservingShellWindows() { - extensions::ShellWindowRegistry* shell_window_registry = - extensions::ShellWindowRegistry::Factory::GetForProfile( - profile_, false /* create */); - if (shell_window_registry) - shell_window_registry->AddObserver(this); +void AppRestoreService::StartObservingAppLifetime() { + AppLifetimeMonitorFactory::GetForProfile(profile_)->AddObserver(this); } -void AppRestoreService::StopObservingShellWindows() { - extensions::ShellWindowRegistry* shell_window_registry = - extensions::ShellWindowRegistry::Factory::GetForProfile( - profile_, false /* create */); - if (shell_window_registry) - shell_window_registry->RemoveObserver(this); +void AppRestoreService::StopObservingAppLifetime() { + AppLifetimeMonitorFactory::GetForProfile(profile_)->RemoveObserver(this); } } // namespace apps -- cgit v1.1