Change Browser::OpenApplication() to handle having no browser windows open.

Update Browser::OpenApplication() to create a new browser window if there are
no browser windows open for the profile.

Updated BackgroundModeManager and AppControllerMac to properly respect the
launch preference set by the user.


BUG=96075
TEST=Close browser window, open application via context menu, make sure there's just one tab.


Review URL: http://codereview.chromium.org/8059037

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@103314 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/app_controller_mac.mm b/chrome/browser/app_controller_mac.mm
index 8320b6f..4acf0c6 100644
--- a/chrome/browser/app_controller_mac.mm
+++ b/chrome/browser/app_controller_mac.mm
@@ -15,6 +15,7 @@
 #include "base/utf_string_conversions.h"
 #include "chrome/app/chrome_command_ids.h"
 #include "chrome/browser/background/background_application_list_model.h"
+#include "chrome/browser/background/background_mode_manager.h"
 #include "chrome/browser/browser_process.h"
 #include "chrome/browser/browser_shutdown.h"
 #include "chrome/browser/command_updater.h"
@@ -903,13 +904,8 @@
   BackgroundApplicationListModel applications(profile);
   DCHECK(tag >= 0 &&
          tag < static_cast<int>(applications.size()));
-  Browser* browser = BrowserList::GetLastActive();
-  if (!browser) {
-    Browser::OpenEmptyWindow(profile);
-    browser = BrowserList::GetLastActive();
-  }
   const Extension* extension = applications.GetExtension(tag);
-  browser->OpenApplicationTab(profile, extension, NEW_FOREGROUND_TAB);
+  BackgroundModeManager::LaunchBackgroundApplication(profile, extension);
 }
 
 // Same as |-commandDispatch:|, but executes commands using a disposition
diff --git a/chrome/browser/background/background_mode_manager.cc b/chrome/browser/background/background_mode_manager.cc
index 6355c44..0f67dbf 100644
--- a/chrome/browser/background/background_mode_manager.cc
+++ b/chrome/browser/background/background_mode_manager.cc
@@ -22,6 +22,7 @@
 #include "chrome/common/chrome_notification_types.h"
 #include "chrome/common/chrome_switches.h"
 #include "chrome/common/extensions/extension.h"
+#include "chrome/common/extensions/extension_constants.h"
 #include "chrome/common/pref_names.h"
 #include "content/browser/user_metrics.h"
 #include "content/common/content_notification_types.h"
@@ -73,9 +74,9 @@
       // Do nothing. This is just a label.
       break;
     default:
-      Browser* browser = GetBrowserWindow();
+      // Launch the app associated with this item.
       const Extension* extension = applications_->GetExtension(item);
-      browser->OpenApplicationTab(profile_, extension, NEW_FOREGROUND_TAB);
+      BackgroundModeManager::LaunchBackgroundApplication(profile_, extension);
       break;
   }
 }
@@ -228,6 +229,18 @@
     UpdateStatusTrayIconContextMenu();
 }
 
+// static
+void BackgroundModeManager::LaunchBackgroundApplication(
+    Profile* profile,
+    const Extension* extension) {
+  ExtensionService* service = profile->GetExtensionService();
+  extension_misc::LaunchContainer launch_container =
+      service->extension_prefs()->GetLaunchContainer(
+          extension, ExtensionPrefs::LAUNCH_REGULAR);
+  Browser::OpenApplication(profile, extension, launch_container,
+                           NEW_FOREGROUND_TAB);
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 //  BackgroundModeManager, NotificationObserver overrides
 void BackgroundModeManager::Observe(int type,
diff --git a/chrome/browser/background/background_mode_manager.h b/chrome/browser/background/background_mode_manager.h
index 75bb8af..832c5ad 100644
--- a/chrome/browser/background/background_mode_manager.h
+++ b/chrome/browser/background/background_mode_manager.h
@@ -52,6 +52,9 @@
 
   virtual void RegisterProfile(Profile* profile);
 
+  static void LaunchBackgroundApplication(Profile* profile,
+                                          const Extension* extension);
+
  private:
   friend class TestBackgroundModeManager;
   friend class BackgroundModeManagerTest;
diff --git a/chrome/browser/ui/browser.cc b/chrome/browser/ui/browser.cc
index 00baca8..894cdc9 100644
--- a/chrome/browser/ui/browser.cc
+++ b/chrome/browser/ui/browser.cc
@@ -684,8 +684,13 @@
                                          WindowOpenDisposition disposition) {
   Browser* browser = BrowserList::FindTabbedBrowser(profile, false);
   TabContents* contents = NULL;
-  if (!browser)
-    return contents;
+  if (!browser) {
+    // No browser for this profile, need to open a new one.
+    browser = Browser::Create(profile);
+    browser->window()->Show();
+    // There's no current tab in this browser window, so add a new one.
+    disposition = NEW_FOREGROUND_TAB;
+  }
 
   // Check the prefs for overridden mode.
   ExtensionService* extension_service = profile->GetExtensionService();