Fixes for bugs ##29090 "Downloads turns into zombies, if Incognito window is opened" and 30419 "Renderer crash triggers Chrome Exit event in particular use case"
BUG=29090,30419
TEST=Now the prompt will appear before last window of the type (incognito or not) is closed.
Review URL: http://codereview.chromium.org/1654011

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@44985 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/chrome/browser/browser.cc b/chrome/browser/browser.cc
index 231cc5a9..73913c2 100644
--- a/chrome/browser/browser.cc
+++ b/chrome/browser/browser.cc
@@ -3207,16 +3207,41 @@
     // RESPONSE_RECEIVED case, the user decided to go along with the closing.
     return true;
   }
-
+  // Indicated that normal (non-incognito) downloads are pending.
+  bool normal_downloads_are_present = false;
+  bool incognito_downloads_are_present = false;
   // If there are no download in-progress, our job is done.
   DownloadManager* download_manager = profile_->GetDownloadManager();
-  if (!download_manager || download_manager->in_progress_count() == 0)
+  if (profile_->IsOffTheRecord()) {
+    // Browser is incognito and so download_manager if present is for incognito
+    // downloads.
+    incognito_downloads_are_present =
+        (download_manager && download_manager->in_progress_count() != 0);
+    // Check original profile.
+    download_manager = profile_->GetOriginalProfile()->GetDownloadManager();
+  }
+
+  normal_downloads_are_present =
+      (download_manager && download_manager->in_progress_count() != 0);
+  if (!normal_downloads_are_present && !incognito_downloads_are_present)
+    return true;
+
+  if (is_attempting_to_close_browser_)
+    return true;
+
+  if ((!normal_downloads_are_present && !profile()->IsOffTheRecord()) ||
+      (!incognito_downloads_are_present && profile()->IsOffTheRecord()))
     return true;
 
   // Let's figure out if we are the last window for our profile.
   // Note that we cannot just use BrowserList::GetBrowserCount as browser
   // windows closing is delayed and the returned count might include windows
   // that are being closed.
+  // The browser allowed to be closed only if:
+  // 1. It is a regular browser and there are no regular downloads present or
+  //    this is not the last regular browser window.
+  // 2. It is an incognito browser and there are no incognito downloads present
+  //    or this is not the last incognito browser window.
   int count = 0;
   for (BrowserList::const_iterator iter = BrowserList::begin();
        iter != BrowserList::end(); ++iter) {
@@ -3224,6 +3249,15 @@
     if (*iter == this || (*iter)->is_attempting_to_close_browser_)
       continue;
 
+    // Verify that this is not the last non-incognito or incognito browser,
+    // depending on the pending downloads.
+    if (normal_downloads_are_present && !profile()->IsOffTheRecord() &&
+        (*iter)->profile()->IsOffTheRecord())
+      continue;
+    if (incognito_downloads_are_present && profile()->IsOffTheRecord() &&
+        !(*iter)->profile()->IsOffTheRecord())
+      continue;
+
     // We test the original profile, because an incognito browser window keeps
     // the original profile alive (and its DownloadManager).
     // We also need to test explicitly the profile directly so that 2 incognito