Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/background/background_mode_manager.h" | 5 #include "chrome/browser/background/background_mode_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 78 // previously logged data. When making changes, also update histograms.xml. | 78 // previously logged data. When making changes, also update histograms.xml. |
| 79 enum MenuItem { | 79 enum MenuItem { |
| 80 MENU_ITEM_ABOUT = 0, | 80 MENU_ITEM_ABOUT = 0, |
| 81 MENU_ITEM_TASK_MANAGER = 1, | 81 MENU_ITEM_TASK_MANAGER = 1, |
| 82 MENU_ITEM_BACKGROUND_CLIENT = 2, | 82 MENU_ITEM_BACKGROUND_CLIENT = 2, |
| 83 MENU_ITEM_KEEP_RUNNING = 3, | 83 MENU_ITEM_KEEP_RUNNING = 3, |
| 84 MENU_ITEM_EXIT = 4, | 84 MENU_ITEM_EXIT = 4, |
| 85 MENU_ITEM_NUM_STATES | 85 MENU_ITEM_NUM_STATES |
| 86 }; | 86 }; |
| 87 | 87 |
| 88 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
| |
| 89 | |
| 88 void RecordMenuItemClick(MenuItem item) { | 90 void RecordMenuItemClick(MenuItem item) { |
| 89 UMA_HISTOGRAM_ENUMERATION("BackgroundMode.MenuItemClick", item, | 91 UMA_HISTOGRAM_ENUMERATION("BackgroundMode.MenuItemClick", item, |
| 90 MENU_ITEM_NUM_STATES); | 92 MENU_ITEM_NUM_STATES); |
| 91 } | 93 } |
| 92 } // namespace | 94 } // namespace |
| 93 | 95 |
| 94 BackgroundModeManager::BackgroundModeData::BackgroundModeData( | 96 BackgroundModeManager::BackgroundModeData::BackgroundModeData( |
| 95 Profile* profile, | 97 Profile* profile, |
| 96 CommandIdHandlerVector* command_id_handler_vector) | 98 CommandIdHandlerVector* command_id_handler_vector) |
| 97 : applications_(new BackgroundApplicationListModel(profile)), | 99 : applications_(new BackgroundApplicationListModel(profile)), |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 271 /////////////////////////////////////////////////////////////////////////////// | 273 /////////////////////////////////////////////////////////////////////////////// |
| 272 // BackgroundModeManager, public | 274 // BackgroundModeManager, public |
| 273 BackgroundModeManager::BackgroundModeManager( | 275 BackgroundModeManager::BackgroundModeManager( |
| 274 const base::CommandLine& command_line, | 276 const base::CommandLine& command_line, |
| 275 ProfileAttributesStorage* profile_storage) | 277 ProfileAttributesStorage* profile_storage) |
| 276 : profile_storage_(profile_storage), | 278 : profile_storage_(profile_storage), |
| 277 status_tray_(NULL), | 279 status_tray_(NULL), |
| 278 status_icon_(NULL), | 280 status_icon_(NULL), |
| 279 context_menu_(NULL), | 281 context_menu_(NULL), |
| 280 in_background_mode_(false), | 282 in_background_mode_(false), |
| 281 keep_alive_for_startup_(false), | |
| 282 keep_alive_for_test_(false), | 283 keep_alive_for_test_(false), |
| 283 background_mode_suspended_(false), | 284 background_mode_suspended_(false), |
| 284 keeping_alive_(false), | |
| 285 weak_factory_(this) { | 285 weak_factory_(this) { |
| 286 // We should never start up if there is no browser process or if we are | 286 // We should never start up if there is no browser process or if we are |
| 287 // currently quitting. | 287 // currently quitting. |
| 288 CHECK(g_browser_process != NULL); | 288 CHECK(g_browser_process != NULL); |
| 289 CHECK(!browser_shutdown::IsTryingToQuit()); | 289 CHECK(!browser_shutdown::IsTryingToQuit()); |
| 290 | 290 |
| 291 // Add self as an observer for the ProfileAttributesStorage so we know when | 291 // Add self as an observer for the ProfileAttributesStorage so we know when |
| 292 // profiles are deleted and their names change. | 292 // profiles are deleted and their names change. |
| 293 profile_storage_->AddObserver(this); | 293 profile_storage_->AddObserver(this); |
| 294 | 294 |
| 295 UMA_HISTOGRAM_BOOLEAN("BackgroundMode.OnStartup.AutoLaunchState", | 295 UMA_HISTOGRAM_BOOLEAN("BackgroundMode.OnStartup.AutoLaunchState", |
| 296 command_line.HasSwitch(switches::kNoStartupWindow)); | 296 command_line.HasSwitch(switches::kNoStartupWindow)); |
| 297 UMA_HISTOGRAM_BOOLEAN("BackgroundMode.OnStartup.IsBackgroundModePrefEnabled", | 297 UMA_HISTOGRAM_BOOLEAN("BackgroundMode.OnStartup.IsBackgroundModePrefEnabled", |
| 298 IsBackgroundModePrefEnabled()); | 298 IsBackgroundModePrefEnabled()); |
| 299 | 299 |
| 300 // Listen for the background mode preference changing. | 300 // Listen for the background mode preference changing. |
| 301 if (g_browser_process->local_state()) { // Skip for unit tests | 301 if (g_browser_process->local_state()) { // Skip for unit tests |
| 302 pref_registrar_.Init(g_browser_process->local_state()); | 302 pref_registrar_.Init(g_browser_process->local_state()); |
| 303 pref_registrar_.Add( | 303 pref_registrar_.Add( |
| 304 prefs::kBackgroundModeEnabled, | 304 prefs::kBackgroundModeEnabled, |
| 305 base::Bind(&BackgroundModeManager::OnBackgroundModeEnabledPrefChanged, | 305 base::Bind(&BackgroundModeManager::OnBackgroundModeEnabledPrefChanged, |
| 306 base::Unretained(this))); | 306 base::Unretained(this))); |
| 307 } | 307 } |
| 308 | 308 |
| 309 // Keep the browser alive until extensions are done loading - this is needed | 309 // Keep the browser alive until extensions are done loading - this is needed |
| 310 // by the --no-startup-window flag. We want to stay alive until we load | 310 // by the --no-startup-window flag. We want to stay alive until we load |
| 311 // extensions, at which point we should either run in background mode (if | 311 // extensions, at which point we should either run in background mode (if |
| 312 // there are background apps) or exit if there are none. | 312 // there are background apps) or exit if there are none. |
| 313 if (command_line.HasSwitch(switches::kNoStartupWindow)) { | 313 if (command_line.HasSwitch(switches::kNoStartupWindow)) { |
| 314 keep_alive_for_startup_ = true; | 314 keep_alive_for_startup_.reset(new ScopedKeepAlive(&g_keep_alive_options)); |
| 315 chrome::IncrementKeepAliveCount(); | |
| 316 } else { | 315 } else { |
| 317 // Otherwise, start with background mode suspended in case we're launching | 316 // Otherwise, start with background mode suspended in case we're launching |
| 318 // in a mode that doesn't open a browser window. It will be resumed when the | 317 // in a mode that doesn't open a browser window. It will be resumed when the |
| 319 // first browser window is opened. | 318 // first browser window is opened. |
| 320 SuspendBackgroundMode(); | 319 SuspendBackgroundMode(); |
| 321 } | 320 } |
| 322 | 321 |
| 323 // If the -keep-alive-for-test flag is passed, then always keep chrome running | 322 // If the -keep-alive-for-test flag is passed, then always keep chrome running |
| 324 // in the background until the user explicitly terminates it. | 323 // in the background until the user explicitly terminates it. |
| 325 if (command_line.HasSwitch(switches::kKeepAliveForTest)) | 324 if (command_line.HasSwitch(switches::kKeepAliveForTest)) |
| (...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 668 profiles::USER_MANAGER_NO_TUTORIAL, | 667 profiles::USER_MANAGER_NO_TUTORIAL, |
| 669 profiles::USER_MANAGER_SELECT_PROFILE_NO_ACTION); | 668 profiles::USER_MANAGER_SELECT_PROFILE_NO_ACTION); |
| 670 } | 669 } |
| 671 break; | 670 break; |
| 672 } | 671 } |
| 673 } | 672 } |
| 674 | 673 |
| 675 | 674 |
| 676 /////////////////////////////////////////////////////////////////////////////// | 675 /////////////////////////////////////////////////////////////////////////////// |
| 677 // BackgroundModeManager, private | 676 // BackgroundModeManager, private |
| 677 void BackgroundModeManager::ReleaseStartupKeepAlive() { | |
| 678 keep_alive_for_startup_.reset(); | |
| 679 } | |
| 680 | |
| 678 void BackgroundModeManager::DecrementKeepAliveCountForStartup() { | 681 void BackgroundModeManager::DecrementKeepAliveCountForStartup() { |
| 679 if (keep_alive_for_startup_) { | 682 if (keep_alive_for_startup_) { |
| 680 keep_alive_for_startup_ = false; | |
| 681 // We call this via the message queue to make sure we don't try to end | 683 // We call this via the message queue to make sure we don't try to end |
| 682 // keep-alive (which can shutdown Chrome) before the message loop has | 684 // keep-alive (which can shutdown Chrome) before the message loop has |
| 683 // started. | 685 // started. This object reference is safe because it's going to be kept |
| 686 // alive by the browser process until after the callback is called. | |
| 684 base::ThreadTaskRunnerHandle::Get()->PostTask( | 687 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 685 FROM_HERE, base::Bind(&chrome::DecrementKeepAliveCount)); | 688 FROM_HERE, base::Bind(&BackgroundModeManager::ReleaseStartupKeepAlive, |
| 689 base::Unretained(this))); | |
| 686 } | 690 } |
| 687 } | 691 } |
| 688 | 692 |
| 689 void BackgroundModeManager::StartBackgroundMode() { | 693 void BackgroundModeManager::StartBackgroundMode() { |
| 690 DCHECK(ShouldBeInBackgroundMode()); | 694 DCHECK(ShouldBeInBackgroundMode()); |
| 691 // Don't bother putting ourselves in background mode if we're already there | 695 // Don't bother putting ourselves in background mode if we're already there |
| 692 // or if background mode is disabled. | 696 // or if background mode is disabled. |
| 693 if (in_background_mode_) | 697 if (in_background_mode_) |
| 694 return; | 698 return; |
| 695 | 699 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 754 UpdateKeepAliveAndTrayIcon(); | 758 UpdateKeepAliveAndTrayIcon(); |
| 755 } | 759 } |
| 756 | 760 |
| 757 void BackgroundModeManager::ResumeBackgroundMode() { | 761 void BackgroundModeManager::ResumeBackgroundMode() { |
| 758 background_mode_suspended_ = false; | 762 background_mode_suspended_ = false; |
| 759 UpdateKeepAliveAndTrayIcon(); | 763 UpdateKeepAliveAndTrayIcon(); |
| 760 } | 764 } |
| 761 | 765 |
| 762 void BackgroundModeManager::UpdateKeepAliveAndTrayIcon() { | 766 void BackgroundModeManager::UpdateKeepAliveAndTrayIcon() { |
| 763 if (in_background_mode_ && !background_mode_suspended_) { | 767 if (in_background_mode_ && !background_mode_suspended_) { |
| 764 if (!keeping_alive_) { | 768 if (!keep_alive_) |
| 765 keeping_alive_ = true; | 769 keep_alive_.reset(new ScopedKeepAlive(&g_keep_alive_options)); |
| 766 chrome::IncrementKeepAliveCount(); | |
| 767 } | |
| 768 CreateStatusTrayIcon(); | 770 CreateStatusTrayIcon(); |
| 769 return; | 771 return; |
| 770 } | 772 } |
| 771 | 773 |
| 772 RemoveStatusTrayIcon(); | 774 RemoveStatusTrayIcon(); |
| 773 if (keeping_alive_) { | 775 keep_alive_.reset(); |
| 774 keeping_alive_ = false; | |
| 775 chrome::DecrementKeepAliveCount(); | |
| 776 } | |
| 777 } | 776 } |
| 778 | 777 |
| 779 void BackgroundModeManager::OnBrowserAdded(Browser* browser) { | 778 void BackgroundModeManager::OnBrowserAdded(Browser* browser) { |
| 780 ResumeBackgroundMode(); | 779 ResumeBackgroundMode(); |
| 781 } | 780 } |
| 782 | 781 |
| 783 void BackgroundModeManager::OnClientsChanged( | 782 void BackgroundModeManager::OnClientsChanged( |
| 784 Profile* profile, | 783 Profile* profile, |
| 785 const std::vector<base::string16>& new_client_names) { | 784 const std::vector<base::string16>& new_client_names) { |
| 786 DCHECK(IsBackgroundModePrefEnabled()); | 785 DCHECK(IsBackgroundModePrefEnabled()); |
| (...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1026 } | 1025 } |
| 1027 } | 1026 } |
| 1028 return profile_it; | 1027 return profile_it; |
| 1029 } | 1028 } |
| 1030 | 1029 |
| 1031 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const { | 1030 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const { |
| 1032 PrefService* service = g_browser_process->local_state(); | 1031 PrefService* service = g_browser_process->local_state(); |
| 1033 DCHECK(service); | 1032 DCHECK(service); |
| 1034 return service->GetBoolean(prefs::kBackgroundModeEnabled); | 1033 return service->GetBoolean(prefs::kBackgroundModeEnabled); |
| 1035 } | 1034 } |
| OLD | NEW |