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_types.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 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 /////////////////////////////////////////////////////////////////////////////// | 272 /////////////////////////////////////////////////////////////////////////////// |
272 // BackgroundModeManager, public | 273 // BackgroundModeManager, public |
273 BackgroundModeManager::BackgroundModeManager( | 274 BackgroundModeManager::BackgroundModeManager( |
274 const base::CommandLine& command_line, | 275 const base::CommandLine& command_line, |
275 ProfileAttributesStorage* profile_storage) | 276 ProfileAttributesStorage* profile_storage) |
276 : profile_storage_(profile_storage), | 277 : profile_storage_(profile_storage), |
277 status_tray_(NULL), | 278 status_tray_(NULL), |
278 status_icon_(NULL), | 279 status_icon_(NULL), |
279 context_menu_(NULL), | 280 context_menu_(NULL), |
280 in_background_mode_(false), | 281 in_background_mode_(false), |
281 keep_alive_for_startup_(false), | |
282 keep_alive_for_test_(false), | 282 keep_alive_for_test_(false), |
283 background_mode_suspended_(false), | 283 background_mode_suspended_(false), |
284 keeping_alive_(false), | |
285 weak_factory_(this) { | 284 weak_factory_(this) { |
286 // We should never start up if there is no browser process or if we are | 285 // We should never start up if there is no browser process or if we are |
287 // currently quitting. | 286 // currently quitting. |
288 CHECK(g_browser_process != NULL); | 287 CHECK(g_browser_process != NULL); |
289 CHECK(!browser_shutdown::IsTryingToQuit()); | 288 CHECK(!browser_shutdown::IsTryingToQuit()); |
290 | 289 |
291 // Add self as an observer for the ProfileAttributesStorage so we know when | 290 // Add self as an observer for the ProfileAttributesStorage so we know when |
292 // profiles are deleted and their names change. | 291 // profiles are deleted and their names change. |
293 profile_storage_->AddObserver(this); | 292 profile_storage_->AddObserver(this); |
294 | 293 |
295 UMA_HISTOGRAM_BOOLEAN("BackgroundMode.OnStartup.AutoLaunchState", | 294 UMA_HISTOGRAM_BOOLEAN("BackgroundMode.OnStartup.AutoLaunchState", |
296 command_line.HasSwitch(switches::kNoStartupWindow)); | 295 command_line.HasSwitch(switches::kNoStartupWindow)); |
297 UMA_HISTOGRAM_BOOLEAN("BackgroundMode.OnStartup.IsBackgroundModePrefEnabled", | 296 UMA_HISTOGRAM_BOOLEAN("BackgroundMode.OnStartup.IsBackgroundModePrefEnabled", |
298 IsBackgroundModePrefEnabled()); | 297 IsBackgroundModePrefEnabled()); |
299 | 298 |
300 // Listen for the background mode preference changing. | 299 // Listen for the background mode preference changing. |
301 if (g_browser_process->local_state()) { // Skip for unit tests | 300 if (g_browser_process->local_state()) { // Skip for unit tests |
302 pref_registrar_.Init(g_browser_process->local_state()); | 301 pref_registrar_.Init(g_browser_process->local_state()); |
303 pref_registrar_.Add( | 302 pref_registrar_.Add( |
304 prefs::kBackgroundModeEnabled, | 303 prefs::kBackgroundModeEnabled, |
305 base::Bind(&BackgroundModeManager::OnBackgroundModeEnabledPrefChanged, | 304 base::Bind(&BackgroundModeManager::OnBackgroundModeEnabledPrefChanged, |
306 base::Unretained(this))); | 305 base::Unretained(this))); |
307 } | 306 } |
308 | 307 |
309 // Keep the browser alive until extensions are done loading - this is needed | 308 // 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 | 309 // 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 | 310 // extensions, at which point we should either run in background mode (if |
312 // there are background apps) or exit if there are none. | 311 // there are background apps) or exit if there are none. |
313 if (command_line.HasSwitch(switches::kNoStartupWindow)) { | 312 if (command_line.HasSwitch(switches::kNoStartupWindow)) { |
314 keep_alive_for_startup_ = true; | 313 keep_alive_for_startup_.reset( |
315 chrome::IncrementKeepAliveCount(); | 314 new ScopedKeepAlive(KeepAliveOrigin::BACKGROUND_MODE_MANAGER)); |
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)) |
326 keep_alive_for_test_ = true; | 325 keep_alive_for_test_ = true; |
327 | 326 |
328 if (ShouldBeInBackgroundMode()) | 327 if (ShouldBeInBackgroundMode()) |
329 StartBackgroundMode(); | 328 StartBackgroundMode(); |
330 | 329 |
331 // Listen for the application shutting down so we can decrement our KeepAlive | 330 // Listen for the application shutting down so we can release our KeepAlive. |
332 // count. | |
333 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, | 331 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, |
334 content::NotificationService::AllSources()); | 332 content::NotificationService::AllSources()); |
335 BrowserList::AddObserver(this); | 333 BrowserList::AddObserver(this); |
336 } | 334 } |
337 | 335 |
338 BackgroundModeManager::~BackgroundModeManager() { | 336 BackgroundModeManager::~BackgroundModeManager() { |
339 // Remove ourselves from the application observer list (only needed by unit | 337 // Remove ourselves from the application observer list (only needed by unit |
340 // tests since APP_TERMINATING is what does this in a real running system). | 338 // tests since APP_TERMINATING is what does this in a real running system). |
341 for (const auto& it : background_mode_data_) | 339 for (const auto& it : background_mode_data_) |
342 it.second->applications_->RemoveObserver(this); | 340 it.second->applications_->RemoveObserver(this); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
461 /////////////////////////////////////////////////////////////////////////////// | 459 /////////////////////////////////////////////////////////////////////////////// |
462 // BackgroundModeManager, content::NotificationObserver overrides | 460 // BackgroundModeManager, content::NotificationObserver overrides |
463 void BackgroundModeManager::Observe( | 461 void BackgroundModeManager::Observe( |
464 int type, | 462 int type, |
465 const content::NotificationSource& source, | 463 const content::NotificationSource& source, |
466 const content::NotificationDetails& details) { | 464 const content::NotificationDetails& details) { |
467 switch (type) { | 465 switch (type) { |
468 case chrome::NOTIFICATION_APP_TERMINATING: | 466 case chrome::NOTIFICATION_APP_TERMINATING: |
469 // Make sure we aren't still keeping the app alive (only happens if we | 467 // Make sure we aren't still keeping the app alive (only happens if we |
470 // don't receive an EXTENSIONS_READY notification for some reason). | 468 // don't receive an EXTENSIONS_READY notification for some reason). |
471 DecrementKeepAliveCountForStartup(); | 469 ReleaseStartupKeepAlive(); |
472 // Performing an explicit shutdown, so exit background mode (does nothing | 470 // Performing an explicit shutdown, so exit background mode (does nothing |
473 // if we aren't in background mode currently). | 471 // if we aren't in background mode currently). |
474 EndBackgroundMode(); | 472 EndBackgroundMode(); |
475 // Shutting down, so don't listen for any more notifications so we don't | 473 // Shutting down, so don't listen for any more notifications so we don't |
476 // try to re-enter/exit background mode again. | 474 // try to re-enter/exit background mode again. |
477 registrar_.RemoveAll(); | 475 registrar_.RemoveAll(); |
478 for (const auto& it : background_mode_data_) | 476 for (const auto& it : background_mode_data_) |
479 it.second->applications_->RemoveObserver(this); | 477 it.second->applications_->RemoveObserver(this); |
480 break; | 478 break; |
481 default: | 479 default: |
482 NOTREACHED(); | 480 NOTREACHED(); |
483 break; | 481 break; |
484 } | 482 } |
485 } | 483 } |
486 | 484 |
487 void BackgroundModeManager::OnExtensionsReady(Profile* profile) { | 485 void BackgroundModeManager::OnExtensionsReady(Profile* profile) { |
488 BackgroundModeManager::BackgroundModeData* bmd = | 486 BackgroundModeManager::BackgroundModeData* bmd = |
489 GetBackgroundModeData(profile); | 487 GetBackgroundModeData(profile); |
490 if (bmd) { | 488 if (bmd) { |
491 UMA_HISTOGRAM_COUNTS_100("BackgroundMode.BackgroundApplicationsCount", | 489 UMA_HISTOGRAM_COUNTS_100("BackgroundMode.BackgroundApplicationsCount", |
492 bmd->applications_->size()); | 490 bmd->applications_->size()); |
493 } | 491 } |
494 // Extensions are loaded, so we don't need to manually keep the browser | 492 // Extensions are loaded, so we don't need to manually keep the browser |
495 // process alive any more when running in no-startup-window mode. | 493 // process alive any more when running in no-startup-window mode. |
496 DecrementKeepAliveCountForStartup(); | 494 ReleaseStartupKeepAlive(); |
497 } | 495 } |
498 | 496 |
499 void BackgroundModeManager::OnBackgroundModeEnabledPrefChanged() { | 497 void BackgroundModeManager::OnBackgroundModeEnabledPrefChanged() { |
500 bool enabled = IsBackgroundModePrefEnabled(); | 498 bool enabled = IsBackgroundModePrefEnabled(); |
501 UMA_HISTOGRAM_BOOLEAN("BackgroundMode.BackgroundModeEnabledPrefChanged", | 499 UMA_HISTOGRAM_BOOLEAN("BackgroundMode.BackgroundModeEnabledPrefChanged", |
502 enabled); | 500 enabled); |
503 if (enabled) | 501 if (enabled) |
504 EnableBackgroundMode(); | 502 EnableBackgroundMode(); |
505 else | 503 else |
506 DisableBackgroundMode(); | 504 DisableBackgroundMode(); |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
668 profiles::USER_MANAGER_NO_TUTORIAL, | 666 profiles::USER_MANAGER_NO_TUTORIAL, |
669 profiles::USER_MANAGER_SELECT_PROFILE_NO_ACTION); | 667 profiles::USER_MANAGER_SELECT_PROFILE_NO_ACTION); |
670 } | 668 } |
671 break; | 669 break; |
672 } | 670 } |
673 } | 671 } |
674 | 672 |
675 | 673 |
676 /////////////////////////////////////////////////////////////////////////////// | 674 /////////////////////////////////////////////////////////////////////////////// |
677 // BackgroundModeManager, private | 675 // BackgroundModeManager, private |
678 void BackgroundModeManager::DecrementKeepAliveCountForStartup() { | 676 void BackgroundModeManager::ReleaseStartupKeepAliveCallback() { |
| 677 keep_alive_for_startup_.reset(); |
| 678 } |
| 679 |
| 680 void BackgroundModeManager::ReleaseStartupKeepAlive() { |
679 if (keep_alive_for_startup_) { | 681 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 | 682 // 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 | 683 // keep-alive (which can shutdown Chrome) before the message loop has |
683 // started. | 684 // started. This object reference is safe because it's going to be kept |
| 685 // alive by the browser process until after the callback is called. |
684 base::ThreadTaskRunnerHandle::Get()->PostTask( | 686 base::ThreadTaskRunnerHandle::Get()->PostTask( |
685 FROM_HERE, base::Bind(&chrome::DecrementKeepAliveCount)); | 687 FROM_HERE, |
| 688 base::Bind(&BackgroundModeManager::ReleaseStartupKeepAliveCallback, |
| 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( |
766 chrome::IncrementKeepAliveCount(); | 770 new ScopedKeepAlive(KeepAliveOrigin::BACKGROUND_MODE_MANAGER)); |
767 } | 771 } |
768 CreateStatusTrayIcon(); | 772 CreateStatusTrayIcon(); |
769 return; | 773 return; |
770 } | 774 } |
771 | 775 |
772 RemoveStatusTrayIcon(); | 776 RemoveStatusTrayIcon(); |
773 if (keeping_alive_) { | 777 keep_alive_.reset(); |
774 keeping_alive_ = false; | |
775 chrome::DecrementKeepAliveCount(); | |
776 } | |
777 } | 778 } |
778 | 779 |
779 void BackgroundModeManager::OnBrowserAdded(Browser* browser) { | 780 void BackgroundModeManager::OnBrowserAdded(Browser* browser) { |
780 ResumeBackgroundMode(); | 781 ResumeBackgroundMode(); |
781 } | 782 } |
782 | 783 |
783 void BackgroundModeManager::OnClientsChanged( | 784 void BackgroundModeManager::OnClientsChanged( |
784 Profile* profile, | 785 Profile* profile, |
785 const std::vector<base::string16>& new_client_names) { | 786 const std::vector<base::string16>& new_client_names) { |
786 DCHECK(IsBackgroundModePrefEnabled()); | 787 DCHECK(IsBackgroundModePrefEnabled()); |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1026 } | 1027 } |
1027 } | 1028 } |
1028 return profile_it; | 1029 return profile_it; |
1029 } | 1030 } |
1030 | 1031 |
1031 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const { | 1032 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const { |
1032 PrefService* service = g_browser_process->local_state(); | 1033 PrefService* service = g_browser_process->local_state(); |
1033 DCHECK(service); | 1034 DCHECK(service); |
1034 return service->GetBoolean(prefs::kBackgroundModeEnabled); | 1035 return service->GetBoolean(prefs::kBackgroundModeEnabled); |
1035 } | 1036 } |
OLD | NEW |