Chromium Code Reviews| Index: chrome/browser/background/background_mode_manager.cc |
| diff --git a/chrome/browser/background/background_mode_manager.cc b/chrome/browser/background/background_mode_manager.cc |
| index 694f91057a64f4952800d09b07290f16d32f8487..4a4cfedd826a355a5b29443b41677b138b288a9d 100644 |
| --- a/chrome/browser/background/background_mode_manager.cc |
| +++ b/chrome/browser/background/background_mode_manager.cc |
| @@ -85,6 +85,8 @@ enum MenuItem { |
| MENU_ITEM_NUM_STATES |
| }; |
| +static const KeepAliveOptions g_keep_alive_options = {"Background"}; |
|
Bernhard Bauer
2016/02/22 18:19:38
The static isn't necessary (outside of a class "st
dgn
2016/02/22 19:07:19
Thanks, I didn't know that about const. Done
|
| + |
| void RecordMenuItemClick(MenuItem item) { |
| UMA_HISTOGRAM_ENUMERATION("BackgroundMode.MenuItemClick", item, |
| MENU_ITEM_NUM_STATES); |
| @@ -278,10 +280,8 @@ BackgroundModeManager::BackgroundModeManager( |
| status_icon_(NULL), |
| context_menu_(NULL), |
| in_background_mode_(false), |
| - keep_alive_for_startup_(false), |
| keep_alive_for_test_(false), |
| background_mode_suspended_(false), |
| - keeping_alive_(false), |
| weak_factory_(this) { |
| // We should never start up if there is no browser process or if we are |
| // currently quitting. |
| @@ -311,8 +311,7 @@ BackgroundModeManager::BackgroundModeManager( |
| // extensions, at which point we should either run in background mode (if |
| // there are background apps) or exit if there are none. |
| if (command_line.HasSwitch(switches::kNoStartupWindow)) { |
| - keep_alive_for_startup_ = true; |
| - chrome::IncrementKeepAliveCount(); |
| + keep_alive_for_startup_.reset(new ScopedKeepAlive(&g_keep_alive_options)); |
| } else { |
| // Otherwise, start with background mode suspended in case we're launching |
| // in a mode that doesn't open a browser window. It will be resumed when the |
| @@ -675,14 +674,19 @@ void BackgroundModeManager::ExecuteCommand(int command_id, int event_flags) { |
| /////////////////////////////////////////////////////////////////////////////// |
| // BackgroundModeManager, private |
| +void BackgroundModeManager::ReleaseStartupKeepAlive() { |
| + keep_alive_for_startup_.reset(); |
| +} |
| + |
| void BackgroundModeManager::DecrementKeepAliveCountForStartup() { |
| if (keep_alive_for_startup_) { |
| - keep_alive_for_startup_ = false; |
| // We call this via the message queue to make sure we don't try to end |
| // keep-alive (which can shutdown Chrome) before the message loop has |
| - // started. |
| + // started. This object reference is safe because it's going to be kept |
| + // alive by the browser process until after the callback is called. |
| base::ThreadTaskRunnerHandle::Get()->PostTask( |
| - FROM_HERE, base::Bind(&chrome::DecrementKeepAliveCount)); |
| + FROM_HERE, base::Bind(&BackgroundModeManager::ReleaseStartupKeepAlive, |
| + base::Unretained(this))); |
| } |
| } |
| @@ -761,19 +765,14 @@ void BackgroundModeManager::ResumeBackgroundMode() { |
| void BackgroundModeManager::UpdateKeepAliveAndTrayIcon() { |
| if (in_background_mode_ && !background_mode_suspended_) { |
| - if (!keeping_alive_) { |
| - keeping_alive_ = true; |
| - chrome::IncrementKeepAliveCount(); |
| - } |
| + if (!keep_alive_) |
| + keep_alive_.reset(new ScopedKeepAlive(&g_keep_alive_options)); |
| CreateStatusTrayIcon(); |
| return; |
| } |
| RemoveStatusTrayIcon(); |
| - if (keeping_alive_) { |
| - keeping_alive_ = false; |
| - chrome::DecrementKeepAliveCount(); |
| - } |
| + keep_alive_.reset(); |
| } |
| void BackgroundModeManager::OnBrowserAdded(Browser* browser) { |