summaryrefslogtreecommitdiffstats
path: root/chrome/browser/background_mode_manager.cc
diff options
context:
space:
mode:
authoratwilson@chromium.org <atwilson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-08-28 06:13:30 +0000
committeratwilson@chromium.org <atwilson@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-08-28 06:13:30 +0000
commitcc7352f88ee0cb7f66be4b1d76dc288fff923fca (patch)
tree781ced2ed1f9e160ccd68d78acdc5ad38d9711f5 /chrome/browser/background_mode_manager.cc
parent50245555c0f5f0dedd0b2aed1ce6a8b347c42a63 (diff)
downloadchromium_src-cc7352f88ee0cb7f66be4b1d76dc288fff923fca.zip
chromium_src-cc7352f88ee0cb7f66be4b1d76dc288fff923fca.tar.gz
chromium_src-cc7352f88ee0cb7f66be4b1d76dc288fff923fca.tar.bz2
Disable background mode when associated pref changes.
BUG=53173 TEST=new BackgroundModeManager unit tests Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=57642 Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=57749 Review URL: http://codereview.chromium.org/3205008 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@57778 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/background_mode_manager.cc')
-rw-r--r--chrome/browser/background_mode_manager.cc67
1 files changed, 54 insertions, 13 deletions
diff --git a/chrome/browser/background_mode_manager.cc b/chrome/browser/background_mode_manager.cc
index 22060c0..263876a 100644
--- a/chrome/browser/background_mode_manager.cc
+++ b/chrome/browser/background_mode_manager.cc
@@ -42,15 +42,16 @@ const wchar_t* kBackgroundModeRegistrySubkey =
const wchar_t* kBackgroundModeRegistryKeyName = L"chromium";
#endif
-BackgroundModeManager::BackgroundModeManager(Profile* profile)
+BackgroundModeManager::BackgroundModeManager(Profile* profile,
+ CommandLine* command_line)
: profile_(profile),
background_app_count_(0),
+ in_background_mode_(false),
status_tray_(NULL),
status_icon_(NULL) {
- // If background mode is globally disabled, just exit - don't listen for
+ // If background mode is disabled, just exit - don't listen for
// any notifications.
- if (!CommandLine::ForCurrentProcess()->HasSwitch(
- switches::kEnableBackgroundMode))
+ if (!command_line->HasSwitch(switches::kEnableBackgroundMode))
return;
// If the -keep-alive-for-test flag is passed, then always keep chrome running
@@ -84,13 +85,20 @@ BackgroundModeManager::BackgroundModeManager(Profile* profile)
registrar_.Add(this, NotificationType::APP_TERMINATING,
NotificationService::AllSources());
-
+ // Listen for changes to the background mode preference.
+ profile_->GetPrefs()->AddPrefObserver(prefs::kBackgroundModeEnabled, this);
}
BackgroundModeManager::~BackgroundModeManager() {
- // If we're going away, remove our status tray icon so we don't get any events
- // from it.
- RemoveStatusTrayIcon();
+ // We're going away, so exit background mode (does nothing if we aren't in
+ // background mode currently). This is primarily needed for unit tests,
+ // because in an actual running system we'd get an APP_TERMINATING
+ // notification before being destroyed.
+ EndBackgroundMode();
+ // Manually remove our pref observer so we don't get notified for prefs
+ // changes (have to do it manually because we can't use the registrar for
+ // prefs notifications).
+ profile_->GetPrefs()->RemovePrefObserver(prefs::kBackgroundModeEnabled, this);
}
bool BackgroundModeManager::IsBackgroundModeEnabled() {
@@ -136,14 +144,18 @@ void BackgroundModeManager::Observe(NotificationType type,
OnBackgroundAppUninstalled();
break;
case NotificationType::APP_TERMINATING:
- // Performing an explicit shutdown, so exit background mode if we were in
- // background mode.
- if (background_app_count_ > 0 && IsBackgroundModeEnabled())
- EndBackgroundMode();
+ // Performing an explicit shutdown, so exit background mode (does nothing
+ // if we aren't in background mode currently).
+ EndBackgroundMode();
// Shutting down, so don't listen for any more notifications so we don't
// try to re-enter/exit background mode again.
registrar_.RemoveAll();
break;
+ case NotificationType::PREF_CHANGED:
+ DCHECK(0 == Details<std::string>(details).ptr()->compare(
+ prefs::kBackgroundModeEnabled));
+ OnBackgroundModePrefChanged();
+ break;
default:
NOTREACHED();
break;
@@ -154,6 +166,24 @@ bool BackgroundModeManager::IsBackgroundApp(Extension* extension) {
return extension->HasApiPermission(Extension::kBackgroundPermission);
}
+
+void BackgroundModeManager::OnBackgroundModePrefChanged() {
+ // Background mode has been enabled/disabled in preferences, so update our
+ // state accordingly.
+ if (IsBackgroundModeEnabled() && !in_background_mode_ &&
+ background_app_count_ > 0) {
+ // We should be in background mode, but we're not, so switch to background
+ // mode.
+ EnableLaunchOnStartup(true);
+ StartBackgroundMode();
+ }
+ if (!IsBackgroundModeEnabled() && in_background_mode_) {
+ // We're in background mode, but we shouldn't be any longer.
+ EnableLaunchOnStartup(false);
+ EndBackgroundMode();
+ }
+}
+
void BackgroundModeManager::OnBackgroundAppLoaded() {
// When a background app loads, increment our count and also enable
// KeepAlive mode if the preference is set.
@@ -163,6 +193,13 @@ void BackgroundModeManager::OnBackgroundAppLoaded() {
}
void BackgroundModeManager::StartBackgroundMode() {
+ // Don't bother putting ourselves in background mode if we're already there.
+ if (in_background_mode_)
+ return;
+
+ // Mark ourselves as running in background mode.
+ in_background_mode_ = true;
+
// Put ourselves in KeepAlive mode and create a status tray icon.
BrowserList::StartKeepAlive();
@@ -174,12 +211,16 @@ void BackgroundModeManager::OnBackgroundAppUnloaded() {
// When a background app unloads, decrement our count and also end
// KeepAlive mode if appropriate.
background_app_count_--;
- DCHECK(background_app_count_ == 0);
+ DCHECK(background_app_count_ >= 0);
if (background_app_count_ == 0 && IsBackgroundModeEnabled())
EndBackgroundMode();
}
void BackgroundModeManager::EndBackgroundMode() {
+ if (!in_background_mode_)
+ return;
+ in_background_mode_ = false;
+
// End KeepAlive mode and blow away our status tray icon.
BrowserList::EndKeepAlive();
RemoveStatusTrayIcon();