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 <algorithm> | 5 #include <algorithm> |
6 #include <string> | 6 #include <string> |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/base_paths.h" | 9 #include "base/base_paths.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "chrome/app/chrome_command_ids.h" | 14 #include "chrome/app/chrome_command_ids.h" |
15 #include "chrome/browser/background/background_application_list_model.h" | 15 #include "chrome/browser/background/background_application_list_model.h" |
16 #include "chrome/browser/background/background_mode_manager.h" | 16 #include "chrome/browser/background/background_mode_manager.h" |
17 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
18 #include "chrome/browser/browser_shutdown.h" | 18 #include "chrome/browser/browser_shutdown.h" |
19 #include "chrome/browser/extensions/extension_service.h" | 19 #include "chrome/browser/extensions/extension_service.h" |
| 20 #include "chrome/browser/lifetime/application_lifetime.h" |
20 #include "chrome/browser/prefs/pref_service.h" | 21 #include "chrome/browser/prefs/pref_service.h" |
21 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
22 #include "chrome/browser/profiles/profile_info_cache.h" | 23 #include "chrome/browser/profiles/profile_info_cache.h" |
23 #include "chrome/browser/profiles/profile_manager.h" | 24 #include "chrome/browser/profiles/profile_manager.h" |
24 #include "chrome/browser/status_icons/status_icon.h" | 25 #include "chrome/browser/status_icons/status_icon.h" |
25 #include "chrome/browser/status_icons/status_tray.h" | 26 #include "chrome/browser/status_icons/status_tray.h" |
26 #include "chrome/browser/ui/browser.h" | 27 #include "chrome/browser/ui/browser.h" |
27 #include "chrome/browser/ui/browser_finder.h" | 28 #include "chrome/browser/ui/browser_finder.h" |
28 #include "chrome/browser/ui/browser_list.h" | |
29 #include "chrome/common/chrome_constants.h" | 29 #include "chrome/common/chrome_constants.h" |
30 #include "chrome/common/chrome_notification_types.h" | 30 #include "chrome/common/chrome_notification_types.h" |
31 #include "chrome/common/chrome_switches.h" | 31 #include "chrome/common/chrome_switches.h" |
32 #include "chrome/common/extensions/extension.h" | 32 #include "chrome/common/extensions/extension.h" |
33 #include "chrome/common/extensions/extension_constants.h" | 33 #include "chrome/common/extensions/extension_constants.h" |
34 #include "chrome/common/pref_names.h" | 34 #include "chrome/common/pref_names.h" |
35 #include "content/public/browser/notification_service.h" | 35 #include "content/public/browser/notification_service.h" |
36 #include "content/public/browser/notification_types.h" | 36 #include "content/public/browser/notification_types.h" |
37 #include "content/public/browser/user_metrics.h" | 37 #include "content/public/browser/user_metrics.h" |
38 #include "grit/chromium_strings.h" | 38 #include "grit/chromium_strings.h" |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 pref_registrar_.Init(g_browser_process->local_state()); | 168 pref_registrar_.Init(g_browser_process->local_state()); |
169 pref_registrar_.Add(prefs::kBackgroundModeEnabled, this); | 169 pref_registrar_.Add(prefs::kBackgroundModeEnabled, this); |
170 } | 170 } |
171 | 171 |
172 // Keep the browser alive until extensions are done loading - this is needed | 172 // Keep the browser alive until extensions are done loading - this is needed |
173 // by the --no-startup-window flag. We want to stay alive until we load | 173 // by the --no-startup-window flag. We want to stay alive until we load |
174 // extensions, at which point we should either run in background mode (if | 174 // extensions, at which point we should either run in background mode (if |
175 // there are background apps) or exit if there are none. | 175 // there are background apps) or exit if there are none. |
176 if (command_line->HasSwitch(switches::kNoStartupWindow)) { | 176 if (command_line->HasSwitch(switches::kNoStartupWindow)) { |
177 keep_alive_for_startup_ = true; | 177 keep_alive_for_startup_ = true; |
178 BrowserList::StartKeepAlive(); | 178 browser::StartKeepAlive(); |
179 } | 179 } |
180 | 180 |
181 // If the -keep-alive-for-test flag is passed, then always keep chrome running | 181 // If the -keep-alive-for-test flag is passed, then always keep chrome running |
182 // in the background until the user explicitly terminates it, by acting as if | 182 // in the background until the user explicitly terminates it, by acting as if |
183 // we loaded a background app. | 183 // we loaded a background app. |
184 if (command_line->HasSwitch(switches::kKeepAliveForTest)) { | 184 if (command_line->HasSwitch(switches::kKeepAliveForTest)) { |
185 keep_alive_for_test_ = true; | 185 keep_alive_for_test_ = true; |
186 StartBackgroundMode(); | 186 StartBackgroundMode(); |
187 } | 187 } |
188 | 188 |
(...skipping 298 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
487 BackgroundModeData* bmd = background_mode_data_.begin()->second.get(); | 487 BackgroundModeData* bmd = background_mode_data_.begin()->second.get(); |
488 switch (command_id) { | 488 switch (command_id) { |
489 case IDC_ABOUT: | 489 case IDC_ABOUT: |
490 bmd->GetBrowserWindow()->OpenAboutChromeDialog(); | 490 bmd->GetBrowserWindow()->OpenAboutChromeDialog(); |
491 break; | 491 break; |
492 case IDC_TASK_MANAGER: | 492 case IDC_TASK_MANAGER: |
493 bmd->GetBrowserWindow()->OpenTaskManager(true); | 493 bmd->GetBrowserWindow()->OpenTaskManager(true); |
494 break; | 494 break; |
495 case IDC_EXIT: | 495 case IDC_EXIT: |
496 content::RecordAction(UserMetricsAction("Exit")); | 496 content::RecordAction(UserMetricsAction("Exit")); |
497 BrowserList::AttemptExit(); | 497 browser::AttemptExit(); |
498 break; | 498 break; |
499 case IDC_STATUS_TRAY_KEEP_CHROME_RUNNING_IN_BACKGROUND: { | 499 case IDC_STATUS_TRAY_KEEP_CHROME_RUNNING_IN_BACKGROUND: { |
500 // Background mode must already be enabled (as otherwise this menu would | 500 // Background mode must already be enabled (as otherwise this menu would |
501 // not be visible). | 501 // not be visible). |
502 DCHECK(IsBackgroundModePrefEnabled()); | 502 DCHECK(IsBackgroundModePrefEnabled()); |
503 DCHECK(BrowserList::WillKeepAlive()); | 503 DCHECK(browser::WillKeepAlive()); |
504 | 504 |
505 // Set the background mode pref to "disabled" - the resulting notification | 505 // Set the background mode pref to "disabled" - the resulting notification |
506 // will result in a call to DisableBackgroundMode(). | 506 // will result in a call to DisableBackgroundMode(). |
507 PrefService* service = g_browser_process->local_state(); | 507 PrefService* service = g_browser_process->local_state(); |
508 DCHECK(service); | 508 DCHECK(service); |
509 service->SetBoolean(prefs::kBackgroundModeEnabled, false); | 509 service->SetBoolean(prefs::kBackgroundModeEnabled, false); |
510 break; | 510 break; |
511 } | 511 } |
512 default: | 512 default: |
513 bmd->ExecuteCommand(command_id); | 513 bmd->ExecuteCommand(command_id); |
514 break; | 514 break; |
515 } | 515 } |
516 } | 516 } |
517 | 517 |
518 | 518 |
519 /////////////////////////////////////////////////////////////////////////////// | 519 /////////////////////////////////////////////////////////////////////////////// |
520 // BackgroundModeManager, private | 520 // BackgroundModeManager, private |
521 void BackgroundModeManager::EndKeepAliveForStartup() { | 521 void BackgroundModeManager::EndKeepAliveForStartup() { |
522 if (keep_alive_for_startup_) { | 522 if (keep_alive_for_startup_) { |
523 keep_alive_for_startup_ = false; | 523 keep_alive_for_startup_ = false; |
524 // We call this via the message queue to make sure we don't try to end | 524 // We call this via the message queue to make sure we don't try to end |
525 // keep-alive (which can shutdown Chrome) before the message loop has | 525 // keep-alive (which can shutdown Chrome) before the message loop has |
526 // started. | 526 // started. |
527 MessageLoop::current()->PostTask( | 527 MessageLoop::current()->PostTask( |
528 FROM_HERE, base::Bind(&BrowserList::EndKeepAlive)); | 528 FROM_HERE, base::Bind(&browser::EndKeepAlive)); |
529 } | 529 } |
530 } | 530 } |
531 | 531 |
532 void BackgroundModeManager::StartBackgroundMode() { | 532 void BackgroundModeManager::StartBackgroundMode() { |
533 // Don't bother putting ourselves in background mode if we're already there | 533 // Don't bother putting ourselves in background mode if we're already there |
534 // or if background mode is disabled. | 534 // or if background mode is disabled. |
535 if (in_background_mode_ || | 535 if (in_background_mode_ || |
536 (!IsBackgroundModePrefEnabled() && !keep_alive_for_test_)) | 536 (!IsBackgroundModePrefEnabled() && !keep_alive_for_test_)) |
537 return; | 537 return; |
538 | 538 |
539 // Mark ourselves as running in background mode. | 539 // Mark ourselves as running in background mode. |
540 in_background_mode_ = true; | 540 in_background_mode_ = true; |
541 | 541 |
542 // Put ourselves in KeepAlive mode and create a status tray icon. | 542 // Put ourselves in KeepAlive mode and create a status tray icon. |
543 BrowserList::StartKeepAlive(); | 543 browser::StartKeepAlive(); |
544 | 544 |
545 // Display a status icon to exit Chrome. | 545 // Display a status icon to exit Chrome. |
546 InitStatusTrayIcon(); | 546 InitStatusTrayIcon(); |
547 | 547 |
548 content::NotificationService::current()->Notify( | 548 content::NotificationService::current()->Notify( |
549 chrome::NOTIFICATION_BACKGROUND_MODE_CHANGED, | 549 chrome::NOTIFICATION_BACKGROUND_MODE_CHANGED, |
550 content::Source<BackgroundModeManager>(this), | 550 content::Source<BackgroundModeManager>(this), |
551 content::Details<bool>(&in_background_mode_)); | 551 content::Details<bool>(&in_background_mode_)); |
552 } | 552 } |
553 | 553 |
554 void BackgroundModeManager::InitStatusTrayIcon() { | 554 void BackgroundModeManager::InitStatusTrayIcon() { |
555 // Only initialize status tray icons for those profiles which actually | 555 // Only initialize status tray icons for those profiles which actually |
556 // have a background app running. | 556 // have a background app running. |
557 if (keep_alive_for_test_ || GetBackgroundAppCount() > 0) { | 557 if (keep_alive_for_test_ || GetBackgroundAppCount() > 0) { |
558 CreateStatusTrayIcon(); | 558 CreateStatusTrayIcon(); |
559 } | 559 } |
560 } | 560 } |
561 | 561 |
562 void BackgroundModeManager::EndBackgroundMode() { | 562 void BackgroundModeManager::EndBackgroundMode() { |
563 if (!in_background_mode_) | 563 if (!in_background_mode_) |
564 return; | 564 return; |
565 in_background_mode_ = false; | 565 in_background_mode_ = false; |
566 | 566 |
567 // End KeepAlive mode and blow away our status tray icon. | 567 // End KeepAlive mode and blow away our status tray icon. |
568 BrowserList::EndKeepAlive(); | 568 browser::EndKeepAlive(); |
569 | 569 |
570 RemoveStatusTrayIcon(); | 570 RemoveStatusTrayIcon(); |
571 content::NotificationService::current()->Notify( | 571 content::NotificationService::current()->Notify( |
572 chrome::NOTIFICATION_BACKGROUND_MODE_CHANGED, | 572 chrome::NOTIFICATION_BACKGROUND_MODE_CHANGED, |
573 content::Source<BackgroundModeManager>(this), | 573 content::Source<BackgroundModeManager>(this), |
574 content::Details<bool>(&in_background_mode_)); | 574 content::Details<bool>(&in_background_mode_)); |
575 } | 575 } |
576 | 576 |
577 void BackgroundModeManager::EnableBackgroundMode() { | 577 void BackgroundModeManager::EnableBackgroundMode() { |
578 DCHECK(IsBackgroundModePrefEnabled()); | 578 DCHECK(IsBackgroundModePrefEnabled()); |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
763 } | 763 } |
764 } | 764 } |
765 return profile_it; | 765 return profile_it; |
766 } | 766 } |
767 | 767 |
768 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const { | 768 bool BackgroundModeManager::IsBackgroundModePrefEnabled() const { |
769 PrefService* service = g_browser_process->local_state(); | 769 PrefService* service = g_browser_process->local_state(); |
770 DCHECK(service); | 770 DCHECK(service); |
771 return service->GetBoolean(prefs::kBackgroundModeEnabled); | 771 return service->GetBoolean(prefs::kBackgroundModeEnabled); |
772 } | 772 } |
OLD | NEW |