Index: chrome/browser/background/background_mode_manager.cc |
diff --git a/chrome/browser/background/background_mode_manager.cc b/chrome/browser/background/background_mode_manager.cc |
index b1ffcd17cb0e3e4b0da6e9c18413ce006addab73..76fd5c5c84e7cdca18c0820724db01b134a0c3b8 100644 |
--- a/chrome/browser/background/background_mode_manager.cc |
+++ b/chrome/browser/background/background_mode_manager.cc |
@@ -30,6 +30,7 @@ |
#include "chrome/browser/ui/browser.h" |
#include "chrome/browser/ui/browser_commands.h" |
#include "chrome/browser/ui/browser_finder.h" |
+#include "chrome/browser/ui/browser_list.h" |
#include "chrome/browser/ui/chrome_pages.h" |
#include "chrome/browser/ui/extensions/application_launch.h" |
#include "chrome/browser/ui/host_desktop.h" |
@@ -148,6 +149,8 @@ BackgroundModeManager::BackgroundModeManager( |
in_background_mode_(false), |
keep_alive_for_startup_(false), |
keep_alive_for_test_(false), |
+ background_mode_suspended_(false), |
+ keeping_alive_(false), |
current_command_id_(0) { |
// We should never start up if there is no browser process or if we are |
// currently quitting. |
@@ -181,6 +184,14 @@ BackgroundModeManager::BackgroundModeManager( |
if (command_line->HasSwitch(switches::kKeepAliveForTest)) |
keep_alive_for_test_ = true; |
+ // If the --app-id or the --show-app-list flag is passed, then start with |
+ // background mode suspended. It will be resumed when the first browser window |
+ // is opened. |
+ if (command_line->HasSwitch(switches::kAppId) || |
+ command_line->HasSwitch(switches::kShowAppList)) { |
+ SuspendBackgroundMode(); |
benwells
2013/10/21 05:57:22
Relying on these two flags feels fragile. E.g. --l
Sam McNally
2013/10/21 06:45:35
Done.
|
+ } |
+ |
if (ShouldBeInBackgroundMode()) |
StartBackgroundMode(); |
@@ -188,6 +199,7 @@ BackgroundModeManager::BackgroundModeManager( |
// count. |
registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, |
content::NotificationService::AllSources()); |
+ BrowserList::AddObserver(this); |
} |
BackgroundModeManager::~BackgroundModeManager() { |
@@ -199,6 +211,7 @@ BackgroundModeManager::~BackgroundModeManager() { |
++it) { |
it->second->applications_->RemoveObserver(this); |
} |
+ BrowserList::RemoveObserver(this); |
// We're going away, so exit background mode (does nothing if we aren't in |
// background mode currently). This is primarily needed for unit tests, |
@@ -457,7 +470,7 @@ void BackgroundModeManager::ExecuteCommand(int command_id, int event_flags) { |
break; |
case IDC_EXIT: |
content::RecordAction(UserMetricsAction("Exit")); |
- chrome::AttemptExit(); |
+ chrome::CloseAllBrowsers(); |
break; |
case IDC_STATUS_TRAY_KEEP_CHROME_RUNNING_IN_BACKGROUND: { |
// Background mode must already be enabled (as otherwise this menu would |
@@ -502,11 +515,7 @@ void BackgroundModeManager::StartBackgroundMode() { |
// Mark ourselves as running in background mode. |
in_background_mode_ = true; |
- // Put ourselves in KeepAlive mode and create a status tray icon. |
- chrome::StartKeepAlive(); |
- |
- // Display a status icon to exit Chrome. |
- InitStatusTrayIcon(); |
+ UpdateKeepAliveAndTrayIcon(); |
content::NotificationService::current()->Notify( |
chrome::NOTIFICATION_BACKGROUND_MODE_CHANGED, |
@@ -514,22 +523,13 @@ void BackgroundModeManager::StartBackgroundMode() { |
content::Details<bool>(&in_background_mode_)); |
} |
-void BackgroundModeManager::InitStatusTrayIcon() { |
- // Only initialize status tray icons for those profiles which actually |
- // have a background app running. |
- if (ShouldBeInBackgroundMode()) |
- CreateStatusTrayIcon(); |
-} |
- |
void BackgroundModeManager::EndBackgroundMode() { |
if (!in_background_mode_) |
return; |
in_background_mode_ = false; |
- // End KeepAlive mode and blow away our status tray icon. |
- chrome::EndKeepAlive(); |
+ UpdateKeepAliveAndTrayIcon(); |
- RemoveStatusTrayIcon(); |
content::NotificationService::current()->Notify( |
chrome::NOTIFICATION_BACKGROUND_MODE_CHANGED, |
content::Source<BackgroundModeManager>(this), |
@@ -554,6 +554,36 @@ void BackgroundModeManager::DisableBackgroundMode() { |
} |
} |
+void BackgroundModeManager::SuspendBackgroundMode() { |
+ background_mode_suspended_ = true; |
+ UpdateKeepAliveAndTrayIcon(); |
+} |
+ |
+void BackgroundModeManager::ResumeBackgroundMode() { |
+ background_mode_suspended_ = false; |
+ UpdateKeepAliveAndTrayIcon(); |
+} |
+ |
+void BackgroundModeManager::UpdateKeepAliveAndTrayIcon() { |
+ if (in_background_mode_ && !background_mode_suspended_) { |
+ if (!keeping_alive_) { |
+ keeping_alive_ = true; |
+ chrome::StartKeepAlive(); |
+ } |
+ CreateStatusTrayIcon(); |
benwells
2013/10/21 05:57:22
Nit: early return instead of else.
Sam McNally
2013/10/21 06:45:35
Done.
|
+ } else { |
+ RemoveStatusTrayIcon(); |
+ if (keeping_alive_) { |
+ keeping_alive_ = false; |
+ chrome::EndKeepAlive(); |
+ } |
+ } |
+} |
+ |
+void BackgroundModeManager::OnBrowserAdded(Browser* browser) { |
+ ResumeBackgroundMode(); |
+} |
+ |
int BackgroundModeManager::GetBackgroundAppCount() const { |
int count = 0; |
// Walk the BackgroundModeData for all profiles and count the number of apps. |
@@ -584,9 +614,14 @@ void BackgroundModeManager::OnBackgroundAppInstalled( |
if (!IsBackgroundModePrefEnabled()) |
return; |
- // Check if we need a status tray icon and make one if we do (needed so we |
- // can display the app-installed notification below). |
- CreateStatusTrayIcon(); |
+ // Ensure we have a tray icon if appropriate (needed so we can display the |
+ // app-installed notification below). |
+ UpdateKeepAliveAndTrayIcon(); |
+ |
+ // If we don't have a status icon or one could not be created succesfully, |
+ // then we won't notify the user. |
+ if (!status_icon_) |
+ return; |
// Notify the user that a background app has been installed. |
if (extension) { // NULL when called by unit tests. |
@@ -643,8 +678,7 @@ void BackgroundModeManager::UpdateStatusTrayIconContextMenu() { |
// If no status icon exists, it's either because one wasn't created when |
// it should have been which can happen when extensions load after the |
// profile has already been registered with the background mode manager. |
- if (in_background_mode_ && !status_icon_) |
- CreateStatusTrayIcon(); |
+ UpdateKeepAliveAndTrayIcon(); |
benwells
2013/10/21 05:57:22
This only happened before if !status_icon, and the
Sam McNally
2013/10/21 06:45:35
Done.
|
// If we don't have a status icon or one could not be created succesfully, |
// then no need to continue the update. |