Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(469)

Side by Side Diff: chrome/browser/background/background_mode_manager.cc

Issue 1708343002: Add ScopedKeepAlive to c/b/lifetime (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use singleton instead of browserprocess Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
271 /////////////////////////////////////////////////////////////////////////////// 271 ///////////////////////////////////////////////////////////////////////////////
272 // BackgroundModeManager, public 272 // BackgroundModeManager, public
273 BackgroundModeManager::BackgroundModeManager( 273 BackgroundModeManager::BackgroundModeManager(
274 const base::CommandLine& command_line, 274 const base::CommandLine& command_line,
275 ProfileAttributesStorage* profile_storage) 275 ProfileAttributesStorage* profile_storage)
276 : profile_storage_(profile_storage), 276 : profile_storage_(profile_storage),
277 status_tray_(NULL), 277 status_tray_(NULL),
278 status_icon_(NULL), 278 status_icon_(NULL),
279 context_menu_(NULL), 279 context_menu_(NULL),
280 in_background_mode_(false), 280 in_background_mode_(false),
281 keep_alive_for_startup_(false),
282 keep_alive_for_test_(false), 281 keep_alive_for_test_(false),
283 background_mode_suspended_(false), 282 background_mode_suspended_(false),
284 keeping_alive_(false),
285 weak_factory_(this) { 283 weak_factory_(this) {
286 // We should never start up if there is no browser process or if we are 284 // We should never start up if there is no browser process or if we are
287 // currently quitting. 285 // currently quitting.
288 CHECK(g_browser_process != NULL); 286 CHECK(g_browser_process != NULL);
289 CHECK(!browser_shutdown::IsTryingToQuit()); 287 CHECK(!browser_shutdown::IsTryingToQuit());
290 288
291 // Add self as an observer for the ProfileAttributesStorage so we know when 289 // Add self as an observer for the ProfileAttributesStorage so we know when
292 // profiles are deleted and their names change. 290 // profiles are deleted and their names change.
293 profile_storage_->AddObserver(this); 291 profile_storage_->AddObserver(this);
294 292
295 UMA_HISTOGRAM_BOOLEAN("BackgroundMode.OnStartup.AutoLaunchState", 293 UMA_HISTOGRAM_BOOLEAN("BackgroundMode.OnStartup.AutoLaunchState",
296 command_line.HasSwitch(switches::kNoStartupWindow)); 294 command_line.HasSwitch(switches::kNoStartupWindow));
297 UMA_HISTOGRAM_BOOLEAN("BackgroundMode.OnStartup.IsBackgroundModePrefEnabled", 295 UMA_HISTOGRAM_BOOLEAN("BackgroundMode.OnStartup.IsBackgroundModePrefEnabled",
298 IsBackgroundModePrefEnabled()); 296 IsBackgroundModePrefEnabled());
299 297
300 // Listen for the background mode preference changing. 298 // Listen for the background mode preference changing.
301 if (g_browser_process->local_state()) { // Skip for unit tests 299 if (g_browser_process->local_state()) { // Skip for unit tests
302 pref_registrar_.Init(g_browser_process->local_state()); 300 pref_registrar_.Init(g_browser_process->local_state());
303 pref_registrar_.Add( 301 pref_registrar_.Add(
304 prefs::kBackgroundModeEnabled, 302 prefs::kBackgroundModeEnabled,
305 base::Bind(&BackgroundModeManager::OnBackgroundModeEnabledPrefChanged, 303 base::Bind(&BackgroundModeManager::OnBackgroundModeEnabledPrefChanged,
306 base::Unretained(this))); 304 base::Unretained(this)));
307 } 305 }
308 306
309 // Keep the browser alive until extensions are done loading - this is needed 307 // 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 308 // 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 309 // extensions, at which point we should either run in background mode (if
312 // there are background apps) or exit if there are none. 310 // there are background apps) or exit if there are none.
313 if (command_line.HasSwitch(switches::kNoStartupWindow)) { 311 if (command_line.HasSwitch(switches::kNoStartupWindow)) {
314 keep_alive_for_startup_ = true; 312 keep_alive_for_startup_.reset(new ScopedKeepAlive("BackgroundForStartup"));
315 chrome::IncrementKeepAliveCount();
316 } else { 313 } else {
317 // Otherwise, start with background mode suspended in case we're launching 314 // 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 315 // in a mode that doesn't open a browser window. It will be resumed when the
319 // first browser window is opened. 316 // first browser window is opened.
320 SuspendBackgroundMode(); 317 SuspendBackgroundMode();
321 } 318 }
322 319
323 // If the -keep-alive-for-test flag is passed, then always keep chrome running 320 // If the -keep-alive-for-test flag is passed, then always keep chrome running
324 // in the background until the user explicitly terminates it. 321 // in the background until the user explicitly terminates it.
325 if (command_line.HasSwitch(switches::kKeepAliveForTest)) 322 if (command_line.HasSwitch(switches::kKeepAliveForTest))
(...skipping 342 matching lines...) Expand 10 before | Expand all | Expand 10 after
668 profiles::USER_MANAGER_NO_TUTORIAL, 665 profiles::USER_MANAGER_NO_TUTORIAL,
669 profiles::USER_MANAGER_SELECT_PROFILE_NO_ACTION); 666 profiles::USER_MANAGER_SELECT_PROFILE_NO_ACTION);
670 } 667 }
671 break; 668 break;
672 } 669 }
673 } 670 }
674 671
675 672
676 /////////////////////////////////////////////////////////////////////////////// 673 ///////////////////////////////////////////////////////////////////////////////
677 // BackgroundModeManager, private 674 // BackgroundModeManager, private
675 void BackgroundModeManager::ReleaseStartupKeepAlive() {
676 keep_alive_for_startup_.reset();
677 }
678
678 void BackgroundModeManager::DecrementKeepAliveCountForStartup() { 679 void BackgroundModeManager::DecrementKeepAliveCountForStartup() {
679 if (keep_alive_for_startup_) { 680 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 681 // 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 682 // keep-alive (which can shutdown Chrome) before the message loop has
683 // started. 683 // started.
684 base::ThreadTaskRunnerHandle::Get()->PostTask( 684 base::ThreadTaskRunnerHandle::Get()->PostTask(
685 FROM_HERE, base::Bind(&chrome::DecrementKeepAliveCount)); 685 FROM_HERE, base::Bind(&BackgroundModeManager::ReleaseStartupKeepAlive,
686 base::Unretained(this)));
686 } 687 }
687 } 688 }
688 689
689 void BackgroundModeManager::StartBackgroundMode() { 690 void BackgroundModeManager::StartBackgroundMode() {
690 DCHECK(ShouldBeInBackgroundMode()); 691 DCHECK(ShouldBeInBackgroundMode());
691 // Don't bother putting ourselves in background mode if we're already there 692 // Don't bother putting ourselves in background mode if we're already there
692 // or if background mode is disabled. 693 // or if background mode is disabled.
693 if (in_background_mode_) 694 if (in_background_mode_)
694 return; 695 return;
695 696
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
754 UpdateKeepAliveAndTrayIcon(); 755 UpdateKeepAliveAndTrayIcon();
755 } 756 }
756 757
757 void BackgroundModeManager::ResumeBackgroundMode() { 758 void BackgroundModeManager::ResumeBackgroundMode() {
758 background_mode_suspended_ = false; 759 background_mode_suspended_ = false;
759 UpdateKeepAliveAndTrayIcon(); 760 UpdateKeepAliveAndTrayIcon();
760 } 761 }
761 762
762 void BackgroundModeManager::UpdateKeepAliveAndTrayIcon() { 763 void BackgroundModeManager::UpdateKeepAliveAndTrayIcon() {
763 if (in_background_mode_ && !background_mode_suspended_) { 764 if (in_background_mode_ && !background_mode_suspended_) {
764 if (!keeping_alive_) { 765 if (!keep_alive_)
765 keeping_alive_ = true; 766 keep_alive_.reset(new ScopedKeepAlive(
Bernhard Bauer 2016/02/19 17:39:28 Nit: braces
dgn 2016/02/19 18:04:28 Done.
766 chrome::IncrementKeepAliveCount(); 767 "Background", OptimizationOptions::RESTART_ALLOWED));
767 }
768 CreateStatusTrayIcon(); 768 CreateStatusTrayIcon();
769 return; 769 return;
770 } 770 }
771 771
772 RemoveStatusTrayIcon(); 772 RemoveStatusTrayIcon();
773 if (keeping_alive_) { 773 if (keep_alive_)
774 keeping_alive_ = false; 774 keep_alive_.reset();
Bernhard Bauer 2016/02/19 17:39:28 You can unconditionally reset it; it will be a no-
dgn 2016/02/19 18:04:28 Done.
775 chrome::DecrementKeepAliveCount();
776 }
777 } 775 }
778 776
779 void BackgroundModeManager::OnBrowserAdded(Browser* browser) { 777 void BackgroundModeManager::OnBrowserAdded(Browser* browser) {
780 ResumeBackgroundMode(); 778 ResumeBackgroundMode();
781 } 779 }
782 780
783 void BackgroundModeManager::OnClientsChanged( 781 void BackgroundModeManager::OnClientsChanged(
784 Profile* profile, 782 Profile* profile,
785 const std::vector<base::string16>& new_client_names) { 783 const std::vector<base::string16>& new_client_names) {
786 DCHECK(IsBackgroundModePrefEnabled()); 784 DCHECK(IsBackgroundModePrefEnabled());
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
1026 } 1024 }
1027 } 1025 }
1028 return profile_it; 1026 return profile_it;
1029 } 1027 }
1030 1028
1031 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const { 1029 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const {
1032 PrefService* service = g_browser_process->local_state(); 1030 PrefService* service = g_browser_process->local_state();
1033 DCHECK(service); 1031 DCHECK(service);
1034 return service->GetBoolean(prefs::kBackgroundModeEnabled); 1032 return service->GetBoolean(prefs::kBackgroundModeEnabled);
1035 } 1033 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698