From 69444cc086ef3ddf9aaecb4d6bebcb24687c6977 Mon Sep 17 00:00:00 2001
From: "jcampan@chromium.org"
 <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>
Date: Thu, 9 Apr 2009 20:40:06 +0000
Subject: This CL adds a confirmation box when closing the browser with
 in-progress downloads.

BUG=1028
TEST=Start several bug downloads, close the browser. A dialog shows up to warn you are about to lose the downloads. Try the wait and continue button. Try again with several windows, and in Incognito mode.
Review URL: http://codereview.chromium.org/62131

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@13453 0039d316-1c4b-4281-b951-d872f2087c98
---
 chrome/browser/views/frame/browser_view.cc | 76 ++++++++++++++++++++++++++++++
 chrome/browser/views/frame/browser_view.h  |  1 +
 2 files changed, 77 insertions(+)

(limited to 'chrome/browser/views')

diff --git a/chrome/browser/views/frame/browser_view.cc b/chrome/browser/views/frame/browser_view.cc
index b746436..260b891 100644
--- a/chrome/browser/views/frame/browser_view.cc
+++ b/chrome/browser/views/frame/browser_view.cc
@@ -12,6 +12,7 @@
 #include "chrome/browser/bookmarks/bookmark_utils.h"
 #include "chrome/browser/browser.h"
 #include "chrome/browser/browser_list.h"
+#include "chrome/browser/download/download_manager.h"
 #include "chrome/browser/encoding_menu_controller_delegate.h"
 #include "chrome/browser/find_bar_controller.h"
 #include "chrome/browser/view_ids.h"
@@ -53,6 +54,7 @@
 #include "chrome/common/resource_bundle.h"
 #include "chrome/common/win_util.h"
 #include "chrome/views/controls/scrollbar/native_scroll_bar.h"
+#include "chrome/views/fill_layout.h"
 #include "chrome/views/view.h"
 #include "chrome/views/widget/hwnd_notification_source.h"
 #include "chrome/views/widget/root_view.h"
@@ -191,6 +193,73 @@ class ResizeCorner : public views::View {
   DISALLOW_COPY_AND_ASSIGN(ResizeCorner);
 };
 
+////////////////////////////////////////////////////////////////////////////////
+// DownloadInProgressConfirmDialogDelegate
+
+class DownloadInProgressConfirmDialogDelegate : public views::DialogDelegate,
+                                                public views::View {
+ public:
+  explicit DownloadInProgressConfirmDialogDelegate(Browser* browser)
+      : browser_(browser) {
+    int download_count = browser->profile()->GetDownloadManager()->
+        in_progress_count();
+    label_ = new views::Label(l10n_util::GetStringF(
+        IDS_DOWNLOAD_REMOVE_CONFIRM_TITLE, download_count));
+    label_->SetMultiLine(true);
+    label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT);
+    label_->set_border(views::Border::CreateEmptyBorder(10, 10, 10, 10));
+    AddChildView(label_);
+    SetParentOwned(false);
+    SetLayoutManager(new views::FillLayout());
+  }
+
+  // View implementation:
+  virtual gfx::Size GetPreferredSize() {
+    const int kContentWidth = 400;
+    return gfx::Size(kContentWidth, label_->GetHeightForWidth(kContentWidth));
+  }
+
+  // DialogDelegate implementation:
+  virtual int GetDefaultDialogButton() const {
+    return DIALOGBUTTON_CANCEL;
+  }
+
+  virtual std::wstring GetDialogButtonLabel(DialogButton button) const {
+    if (button == DIALOGBUTTON_OK)
+      return l10n_util::GetString(IDS_DOWNLOAD_REMOVE_CONFIRM_OK_BUTTON_LABEL);
+
+    DCHECK_EQ(DIALOGBUTTON_CANCEL, button);
+    return l10n_util::GetString(
+        IDS_DOWNLOAD_REMOVE_CONFIRM_CANCEL_BUTTON_LABEL);
+  }
+
+  virtual bool Accept() {
+    browser_->InProgressDownloadResponse(true);
+    return true;
+  }
+
+  virtual bool Cancel() {
+    browser_->InProgressDownloadResponse(false);
+    return true;
+  }
+
+  virtual void DeleteDelegate() {
+    delete this;
+  }
+
+  // WindowDelegate implementation:
+  virtual bool IsModal() const { return true; }
+
+  virtual views::View* GetContentsView() {
+    return this;
+  }
+
+ private:
+  Browser* browser_;
+  views::Label* label_;
+
+  DISALLOW_COPY_AND_ASSIGN(DownloadInProgressConfirmDialogDelegate);
+};
 
 ///////////////////////////////////////////////////////////////////////////////
 // BrowserView, public:
@@ -822,6 +891,13 @@ void BrowserView::ShowNewProfileDialog() {
   NewProfileDialog::RunDialog();
 }
 
+void BrowserView::ConfirmBrowserCloseWithPendingDownloads() {
+  DownloadInProgressConfirmDialogDelegate* delegate =
+      new DownloadInProgressConfirmDialogDelegate(browser_.get());
+  views::Window::CreateChromeWindow(GetWidget()->GetNativeView(), gfx::Rect(),
+                                    delegate)->Show();
+}
+
 void BrowserView::ShowHTMLDialog(HtmlDialogUIDelegate* delegate,
                                  void* parent_window) {
   HWND parent_hwnd = reinterpret_cast<HWND>(parent_window);
diff --git a/chrome/browser/views/frame/browser_view.h b/chrome/browser/views/frame/browser_view.h
index f95c0fa..5479cdc 100644
--- a/chrome/browser/views/frame/browser_view.h
+++ b/chrome/browser/views/frame/browser_view.h
@@ -205,6 +205,7 @@ class BrowserView : public BrowserWindow,
   virtual void ShowPasswordManager();
   virtual void ShowSelectProfileDialog();
   virtual void ShowNewProfileDialog();
+  virtual void ConfirmBrowserCloseWithPendingDownloads();
   virtual void ShowHTMLDialog(HtmlDialogUIDelegate* delegate,
                               void* parent_window);
 
-- 
cgit v1.1