| 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
| 22 #include "base/thread_task_runner_handle.h" | 22 #include "base/thread_task_runner_handle.h" |
| 23 #include "chrome/app/chrome_command_ids.h" | 23 #include "chrome/app/chrome_command_ids.h" |
| 24 #include "chrome/browser/background/background_application_list_model.h" | 24 #include "chrome/browser/background/background_application_list_model.h" |
| 25 #include "chrome/browser/background/background_trigger.h" | 25 #include "chrome/browser/background/background_trigger.h" |
| 26 #include "chrome/browser/browser_process.h" | 26 #include "chrome/browser/browser_process.h" |
| 27 #include "chrome/browser/browser_shutdown.h" | 27 #include "chrome/browser/browser_shutdown.h" |
| 28 #include "chrome/browser/chrome_notification_types.h" | 28 #include "chrome/browser/chrome_notification_types.h" |
| 29 #include "chrome/browser/extensions/extension_service.h" | 29 #include "chrome/browser/extensions/extension_service.h" |
| 30 #include "chrome/browser/lifetime/application_lifetime.h" | 30 #include "chrome/browser/lifetime/application_lifetime.h" |
| 31 #include "chrome/browser/lifetime/browser_keep_alive.h" |
| 31 #include "chrome/browser/profiles/profile.h" | 32 #include "chrome/browser/profiles/profile.h" |
| 32 #include "chrome/browser/profiles/profile_info_cache.h" | 33 #include "chrome/browser/profiles/profile_info_cache.h" |
| 33 #include "chrome/browser/profiles/profile_manager.h" | 34 #include "chrome/browser/profiles/profile_manager.h" |
| 34 #include "chrome/browser/status_icons/status_icon.h" | 35 #include "chrome/browser/status_icons/status_icon.h" |
| 35 #include "chrome/browser/status_icons/status_tray.h" | 36 #include "chrome/browser/status_icons/status_tray.h" |
| 36 #include "chrome/browser/ui/browser.h" | 37 #include "chrome/browser/ui/browser.h" |
| 37 #include "chrome/browser/ui/browser_commands.h" | 38 #include "chrome/browser/ui/browser_commands.h" |
| 38 #include "chrome/browser/ui/browser_dialogs.h" | 39 #include "chrome/browser/ui/browser_dialogs.h" |
| 39 #include "chrome/browser/ui/browser_finder.h" | 40 #include "chrome/browser/ui/browser_finder.h" |
| 40 #include "chrome/browser/ui/browser_list.h" | 41 #include "chrome/browser/ui/browser_list.h" |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 336 base::Bind(&BackgroundModeManager::OnBackgroundModeEnabledPrefChanged, | 337 base::Bind(&BackgroundModeManager::OnBackgroundModeEnabledPrefChanged, |
| 337 base::Unretained(this))); | 338 base::Unretained(this))); |
| 338 } | 339 } |
| 339 | 340 |
| 340 // Keep the browser alive until extensions are done loading - this is needed | 341 // Keep the browser alive until extensions are done loading - this is needed |
| 341 // by the --no-startup-window flag. We want to stay alive until we load | 342 // by the --no-startup-window flag. We want to stay alive until we load |
| 342 // extensions, at which point we should either run in background mode (if | 343 // extensions, at which point we should either run in background mode (if |
| 343 // there are background apps) or exit if there are none. | 344 // there are background apps) or exit if there are none. |
| 344 if (command_line.HasSwitch(switches::kNoStartupWindow)) { | 345 if (command_line.HasSwitch(switches::kNoStartupWindow)) { |
| 345 keep_alive_for_startup_ = true; | 346 keep_alive_for_startup_ = true; |
| 346 chrome::IncrementKeepAliveCount(); | 347 browser_lifetime::IncrementKeepAliveCount(); |
| 347 } else { | 348 } else { |
| 348 // Otherwise, start with background mode suspended in case we're launching | 349 // Otherwise, start with background mode suspended in case we're launching |
| 349 // in a mode that doesn't open a browser window. It will be resumed when the | 350 // in a mode that doesn't open a browser window. It will be resumed when the |
| 350 // first browser window is opened. | 351 // first browser window is opened. |
| 351 SuspendBackgroundMode(); | 352 SuspendBackgroundMode(); |
| 352 } | 353 } |
| 353 | 354 |
| 354 // If the -keep-alive-for-test flag is passed, then always keep chrome running | 355 // If the -keep-alive-for-test flag is passed, then always keep chrome running |
| 355 // in the background until the user explicitly terminates it. | 356 // in the background until the user explicitly terminates it. |
| 356 if (command_line.HasSwitch(switches::kKeepAliveForTest)) | 357 if (command_line.HasSwitch(switches::kKeepAliveForTest)) |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 673 browser_watcher::ExitFunnel::RecordSingleEvent( | 674 browser_watcher::ExitFunnel::RecordSingleEvent( |
| 674 chrome::kBrowserExitCodesRegistryPath, L"TraybarExit"); | 675 chrome::kBrowserExitCodesRegistryPath, L"TraybarExit"); |
| 675 #endif | 676 #endif |
| 676 content::RecordAction(UserMetricsAction("Exit")); | 677 content::RecordAction(UserMetricsAction("Exit")); |
| 677 chrome::CloseAllBrowsers(); | 678 chrome::CloseAllBrowsers(); |
| 678 break; | 679 break; |
| 679 case IDC_STATUS_TRAY_KEEP_CHROME_RUNNING_IN_BACKGROUND: { | 680 case IDC_STATUS_TRAY_KEEP_CHROME_RUNNING_IN_BACKGROUND: { |
| 680 // Background mode must already be enabled (as otherwise this menu would | 681 // Background mode must already be enabled (as otherwise this menu would |
| 681 // not be visible). | 682 // not be visible). |
| 682 DCHECK(IsBackgroundModePrefEnabled()); | 683 DCHECK(IsBackgroundModePrefEnabled()); |
| 683 DCHECK(chrome::WillKeepAlive()); | 684 DCHECK(browser_lifetime::WillKeepAlive()); |
| 684 | 685 |
| 685 RecordMenuItemClick(MENU_ITEM_KEEP_RUNNING); | 686 RecordMenuItemClick(MENU_ITEM_KEEP_RUNNING); |
| 686 | 687 |
| 687 // Set the background mode pref to "disabled" - the resulting notification | 688 // Set the background mode pref to "disabled" - the resulting notification |
| 688 // will result in a call to DisableBackgroundMode(). | 689 // will result in a call to DisableBackgroundMode(). |
| 689 PrefService* service = g_browser_process->local_state(); | 690 PrefService* service = g_browser_process->local_state(); |
| 690 DCHECK(service); | 691 DCHECK(service); |
| 691 service->SetBoolean(prefs::kBackgroundModeEnabled, false); | 692 service->SetBoolean(prefs::kBackgroundModeEnabled, false); |
| 692 break; | 693 break; |
| 693 } | 694 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 706 | 707 |
| 707 /////////////////////////////////////////////////////////////////////////////// | 708 /////////////////////////////////////////////////////////////////////////////// |
| 708 // BackgroundModeManager, private | 709 // BackgroundModeManager, private |
| 709 void BackgroundModeManager::DecrementKeepAliveCountForStartup() { | 710 void BackgroundModeManager::DecrementKeepAliveCountForStartup() { |
| 710 if (keep_alive_for_startup_) { | 711 if (keep_alive_for_startup_) { |
| 711 keep_alive_for_startup_ = false; | 712 keep_alive_for_startup_ = false; |
| 712 // We call this via the message queue to make sure we don't try to end | 713 // We call this via the message queue to make sure we don't try to end |
| 713 // keep-alive (which can shutdown Chrome) before the message loop has | 714 // keep-alive (which can shutdown Chrome) before the message loop has |
| 714 // started. | 715 // started. |
| 715 base::ThreadTaskRunnerHandle::Get()->PostTask( | 716 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 716 FROM_HERE, base::Bind(&chrome::DecrementKeepAliveCount)); | 717 FROM_HERE, base::Bind(&browser_lifetime::DecrementKeepAliveCount)); |
| 717 } | 718 } |
| 718 } | 719 } |
| 719 | 720 |
| 720 void BackgroundModeManager::StartBackgroundMode() { | 721 void BackgroundModeManager::StartBackgroundMode() { |
| 721 DCHECK(ShouldBeInBackgroundMode()); | 722 DCHECK(ShouldBeInBackgroundMode()); |
| 722 // Don't bother putting ourselves in background mode if we're already there | 723 // Don't bother putting ourselves in background mode if we're already there |
| 723 // or if background mode is disabled. | 724 // or if background mode is disabled. |
| 724 if (in_background_mode_) | 725 if (in_background_mode_) |
| 725 return; | 726 return; |
| 726 | 727 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 787 | 788 |
| 788 void BackgroundModeManager::ResumeBackgroundMode() { | 789 void BackgroundModeManager::ResumeBackgroundMode() { |
| 789 background_mode_suspended_ = false; | 790 background_mode_suspended_ = false; |
| 790 UpdateKeepAliveAndTrayIcon(); | 791 UpdateKeepAliveAndTrayIcon(); |
| 791 } | 792 } |
| 792 | 793 |
| 793 void BackgroundModeManager::UpdateKeepAliveAndTrayIcon() { | 794 void BackgroundModeManager::UpdateKeepAliveAndTrayIcon() { |
| 794 if (in_background_mode_ && !background_mode_suspended_) { | 795 if (in_background_mode_ && !background_mode_suspended_) { |
| 795 if (!keeping_alive_) { | 796 if (!keeping_alive_) { |
| 796 keeping_alive_ = true; | 797 keeping_alive_ = true; |
| 797 chrome::IncrementKeepAliveCount(); | 798 browser_lifetime::IncrementKeepAliveCount(); |
| 798 | 799 |
| 799 #if defined(OS_WIN) | 800 #if defined(OS_WIN) |
| 800 browser_watcher::ExitFunnel::RecordSingleEvent( | 801 browser_watcher::ExitFunnel::RecordSingleEvent( |
| 801 chrome::kBrowserExitCodesRegistryPath, L"BackgroundOn"); | 802 chrome::kBrowserExitCodesRegistryPath, L"BackgroundOn"); |
| 802 #endif | 803 #endif |
| 803 } | 804 } |
| 804 CreateStatusTrayIcon(); | 805 CreateStatusTrayIcon(); |
| 805 return; | 806 return; |
| 806 } | 807 } |
| 807 | 808 |
| 808 RemoveStatusTrayIcon(); | 809 RemoveStatusTrayIcon(); |
| 809 if (keeping_alive_) { | 810 if (keeping_alive_) { |
| 810 keeping_alive_ = false; | 811 keeping_alive_ = false; |
| 811 chrome::DecrementKeepAliveCount(); | 812 browser_lifetime::DecrementKeepAliveCount(); |
| 812 | 813 |
| 813 #if defined(OS_WIN) | 814 #if defined(OS_WIN) |
| 814 browser_watcher::ExitFunnel::RecordSingleEvent( | 815 browser_watcher::ExitFunnel::RecordSingleEvent( |
| 815 chrome::kBrowserExitCodesRegistryPath, L"BackgroundOff"); | 816 chrome::kBrowserExitCodesRegistryPath, L"BackgroundOff"); |
| 816 #endif | 817 #endif |
| 817 } | 818 } |
| 818 } | 819 } |
| 819 | 820 |
| 820 void BackgroundModeManager::OnBrowserAdded(Browser* browser) { | 821 void BackgroundModeManager::OnBrowserAdded(Browser* browser) { |
| 821 ResumeBackgroundMode(); | 822 ResumeBackgroundMode(); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 860 OnBackgroundClientInstalled(name); | 861 OnBackgroundClientInstalled(name); |
| 861 } | 862 } |
| 862 } | 863 } |
| 863 | 864 |
| 864 int BackgroundModeManager::GetBackgroundClientCount() const { | 865 int BackgroundModeManager::GetBackgroundClientCount() const { |
| 865 int count = 0; | 866 int count = 0; |
| 866 // Walk the BackgroundModeData for all profiles and count the number of | 867 // Walk the BackgroundModeData for all profiles and count the number of |
| 867 // clients. | 868 // clients. |
| 868 for (const auto& it : background_mode_data_) | 869 for (const auto& it : background_mode_data_) |
| 869 count += it.second->GetBackgroundClientCount(); | 870 count += it.second->GetBackgroundClientCount(); |
| 870 DCHECK(count >= 0); | 871 DCHECK_GE(count, 0); |
| 871 return count; | 872 return count; |
| 872 } | 873 } |
| 873 | 874 |
| 874 int BackgroundModeManager::GetBackgroundClientCountForProfile( | 875 int BackgroundModeManager::GetBackgroundClientCountForProfile( |
| 875 Profile* const profile) const { | 876 Profile* const profile) const { |
| 876 BackgroundModeManager::BackgroundModeData* bmd = | 877 BackgroundModeManager::BackgroundModeData* bmd = |
| 877 GetBackgroundModeData(profile); | 878 GetBackgroundModeData(profile); |
| 878 if (!bmd) | 879 if (!bmd) |
| 879 return 0; | 880 return 0; |
| 880 return bmd->GetBackgroundClientCount(); | 881 return bmd->GetBackgroundClientCount(); |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1043 } | 1044 } |
| 1044 } | 1045 } |
| 1045 return profile_it; | 1046 return profile_it; |
| 1046 } | 1047 } |
| 1047 | 1048 |
| 1048 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const { | 1049 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const { |
| 1049 PrefService* service = g_browser_process->local_state(); | 1050 PrefService* service = g_browser_process->local_state(); |
| 1050 DCHECK(service); | 1051 DCHECK(service); |
| 1051 return service->GetBoolean(prefs::kBackgroundModeEnabled); | 1052 return service->GetBoolean(prefs::kBackgroundModeEnabled); |
| 1052 } | 1053 } |
| OLD | NEW |