diff options
author | jackhou@chromium.org <jackhou@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-06-18 14:22:12 +0000 |
---|---|---|
committer | jackhou@chromium.org <jackhou@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-06-18 14:22:12 +0000 |
commit | 4b7111f2c1f379da798835b9a7f7985138542d84 (patch) | |
tree | 38e6e22dad15dcf948a0eac1c83bac50998a97f4 /apps/app_restore_service.cc | |
parent | 42265601854dfe527c6fd2522d4c9b6c15752273 (diff) | |
download | chromium_src-4b7111f2c1f379da798835b9a7f7985138542d84.zip chromium_src-4b7111f2c1f379da798835b9a7f7985138542d84.tar.gz chromium_src-4b7111f2c1f379da798835b9a7f7985138542d84.tar.bz2 |
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=
Committed: https://src.chromium.org/viewvc/chrome?view=rev&revision=204487
Review URL: https://chromiumcodereview.appspot.com/16412002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@206988 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'apps/app_restore_service.cc')
-rw-r--r-- | apps/app_restore_service.cc | 108 |
1 files changed, 34 insertions, 74 deletions
diff --git a/apps/app_restore_service.cc b/apps/app_restore_service.cc index f5cf21c..210ccf3 100644 --- a/apps/app_restore_service.cc +++ b/apps/app_restore_service.cc @@ -4,28 +4,22 @@ #include "apps/app_restore_service.h" +#include "apps/app_lifetime_monitor_factory.h" #include "apps/app_restore_service_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; @@ -49,16 +43,7 @@ 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()); - registrar_.Add( - this, chrome::NOTIFICATION_APP_TERMINATING, - content::NotificationService::AllSources()); - StartObservingShellWindows(); + StartObservingAppLifetime(); } void AppRestoreService::HandleStartup(bool should_restore_apps) { @@ -95,51 +80,33 @@ AppRestoreService* AppRestoreService::Get(Profile* profile) { return apps::AppRestoreServiceFactory::GetForProfile(profile); } -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<ExtensionHost>(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<ExtensionHost>(details).ptr(); - const Extension* extension = host->extension(); - if (extension && extension->is_platform_app()) - RecordAppStop(extension->id()); - break; - } +void AppRestoreService::OnAppStart(Profile* profile, + const std::string& app_id) { + RecordAppStart(app_id); +} - 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(); - break; - } - } +void AppRestoreService::OnAppActivated(Profile* profile, + const std::string& app_id) { + RecordAppActiveState(app_id, true); } -void AppRestoreService::OnShellWindowAdded(ShellWindow* shell_window) { - RecordIfAppHasWindows(shell_window->extension_id()); +void AppRestoreService::OnAppDeactivated(Profile* profile, + const std::string& app_id) { + RecordAppActiveState(app_id, false); } -void AppRestoreService::OnShellWindowIconChanged(ShellWindow* shell_window) { +void AppRestoreService::OnAppStop(Profile* profile, const std::string& app_id) { + RecordAppStop(app_id); } -void AppRestoreService::OnShellWindowRemoved(ShellWindow* shell_window) { - RecordIfAppHasWindows(shell_window->extension_id()); +void AppRestoreService::OnChromeTerminating() { + // We want to preserve the state when the app begins terminating, so stop + // listening to app lifetime events. + StopObservingAppLifetime(); } void AppRestoreService::Shutdown() { - StopObservingShellWindows(); + StopObservingAppLifetime(); } void AppRestoreService::RecordAppStart(const std::string& extension_id) { @@ -154,44 +121,37 @@ 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() { + AppLifetimeMonitor* app_lifetime_monitor = + AppLifetimeMonitorFactory::GetForProfile(profile_); + DCHECK(app_lifetime_monitor); + app_lifetime_monitor->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() { + AppLifetimeMonitor* app_lifetime_monitor = + AppLifetimeMonitorFactory::GetForProfile(profile_); + // This might be NULL in tests. + if (app_lifetime_monitor) + app_lifetime_monitor->RemoveObserver(this); } } // namespace apps |