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