download_danger_prompt_views.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
11e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
21e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
31e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// found in the LICENSE file.
41e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
51e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "base/compiler_specific.h"
61e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/download/download_danger_prompt.h"
71e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "chrome/browser/ui/views/constrained_window_views.h"
81e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "components/web_modal/web_contents_modal_dialog_host.h"
91e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "components/web_modal/web_contents_modal_dialog_manager.h"
101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "content/public/browser/browser_thread.h"
121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "content/public/browser/download_danger_type.h"
131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "content/public/browser/download_item.h"
141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "content/public/browser/web_contents.h"
151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "grit/chromium_strings.h"
161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "grit/generated_resources.h"
171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "ui/views/controls/button/label_button.h"
191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "ui/views/controls/label.h"
201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "ui/views/layout/grid_layout.h"
211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "ui/views/view.h"
221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "ui/views/widget/widget.h"
231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "ui/views/window/dialog_client_view.h"
241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "ui/views/window/dialog_delegate.h"
251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)using content::BrowserThread;
271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)using web_modal::WebContentsModalDialogManager;
281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)using web_modal::WebContentsModalDialogManagerDelegate;
291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)namespace {
311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const int kMessageWidth = 320;
331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const int kParagraphPadding = 15;
341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Views-specific implementation of download danger prompt dialog. We use this
361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// class rather than a TabModalConfirmDialog so that we can use custom
371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// formatting on the text in the body of the dialog.
381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)class DownloadDangerPromptViews : public DownloadDangerPrompt,
391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                  public content::DownloadItem::Observer,
401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                  public views::DialogDelegate {
411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) public:
421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DownloadDangerPromptViews(content::DownloadItem* item,
431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                            content::WebContents* web_contents,
441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                            bool show_context,
451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                            const OnDone& done);
461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // DownloadDangerPrompt methods:
481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual void InvokeActionForTesting(Action action) OVERRIDE;
491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // views::DialogDelegate methods:
51a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual base::string16 GetDialogButtonLabel(
52a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      ui::DialogButton button) const OVERRIDE;
53a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual base::string16 GetWindowTitle() const OVERRIDE;
541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual void DeleteDelegate() OVERRIDE;
551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual ui::ModalType GetModalType() const OVERRIDE;
561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual bool Cancel() OVERRIDE;
571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual bool Accept() OVERRIDE;
581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual bool Close() OVERRIDE;
591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // TODO(wittman): Remove this override once we move to the new style frame
601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // view on all dialogs.
611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual views::NonClientFrameView* CreateNonClientFrameView(
621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      views::Widget* widget) OVERRIDE;
631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual views::View* GetInitiallyFocusedView() OVERRIDE;
641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual views::View* GetContentsView() OVERRIDE;
651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual views::Widget* GetWidget() OVERRIDE;
661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual const views::Widget* GetWidget() const OVERRIDE;
671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // content::DownloadItem::Observer:
691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE;
701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) private:
72a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 GetAcceptButtonTitle() const;
73a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 GetCancelButtonTitle() const;
741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // The message lead is separated from the main text and is bolded.
75a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 GetMessageLead() const;
76a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 GetMessageBody() const;
771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void RunDone(Action action);
781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  content::DownloadItem* download_;
801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  content::WebContents* web_contents_;
811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  bool show_context_;
821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  OnDone done_;
831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  views::View* contents_view_;
851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)};
861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)DownloadDangerPromptViews::DownloadDangerPromptViews(
881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    content::DownloadItem* item,
891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    content::WebContents* web_contents,
901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    bool show_context,
911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    const OnDone& done)
921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    : download_(item),
931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      web_contents_(web_contents),
941e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      show_context_(show_context),
951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      done_(done),
961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      contents_view_(NULL) {
971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK(!done_.is_null());
981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  download_->AddObserver(this);
991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1001e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  contents_view_ = new views::View;
1011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  views::GridLayout* layout = views::GridLayout::CreatePanel(contents_view_);
1031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  contents_view_->SetLayoutManager(layout);
1041e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1051e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  views::ColumnSet* column_set = layout->AddColumnSet(0);
1061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
1071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                        views::GridLayout::FIXED, kMessageWidth, 0);
1081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
109a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  const base::string16 message_lead = GetMessageLead();
1101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (!message_lead.empty()) {
1121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    views::Label* message_lead_label = new views::Label(message_lead);
1131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    message_lead_label->SetMultiLine(true);
1141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    message_lead_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
1151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    message_lead_label->SetAllowCharacterBreak(true);
1161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    gfx::FontList font_list(gfx::Font().DeriveFont(0, gfx::Font::BOLD));
1181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    message_lead_label->SetFontList(font_list);
1191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    layout->StartRow(0, 0);
1211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    layout->AddView(message_lead_label);
1221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    layout->AddPaddingRow(0, kParagraphPadding);
1241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
1251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  views::Label* message_body_label = new views::Label(GetMessageBody());
1271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  message_body_label->SetMultiLine(true);
1281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  message_body_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
1291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  message_body_label->SetAllowCharacterBreak(true);
1301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  layout->StartRow(0, 0);
1321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  layout->AddView(message_body_label);
1331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// DownloadDangerPrompt methods:
1361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void DownloadDangerPromptViews::InvokeActionForTesting(Action action) {
1371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  switch (action) {
1381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case ACCEPT:
1391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      Accept();
1401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      break;
1411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case CANCEL:
1431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case DISMISS:
1441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      Cancel();
1451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      break;
1461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    default:
1481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      NOTREACHED();
1491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      break;
1501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
1511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// views::DialogDelegate methods:
154a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 DownloadDangerPromptViews::GetDialogButtonLabel(
1551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    ui::DialogButton button) const {
1561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  switch (button) {
1571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case ui::DIALOG_BUTTON_OK:
1581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return GetAcceptButtonTitle();
1591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case ui::DIALOG_BUTTON_CANCEL:
1611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return GetCancelButtonTitle();
1621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    default:
1641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return DialogDelegate::GetDialogButtonLabel(button);
1651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  };
1661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
168a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 DownloadDangerPromptViews::GetWindowTitle() const {
1691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (show_context_)
1701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_CONFIRM_KEEP_DANGEROUS_DOWNLOAD_TITLE);
1711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  else
1721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_RESTORE_KEEP_DANGEROUS_DOWNLOAD_TITLE);
1731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void DownloadDangerPromptViews::DeleteDelegate() {
1761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  delete this;
1791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)ui::ModalType DownloadDangerPromptViews::GetModalType() const {
1821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#if defined(USE_ASH)
1831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return ui::MODAL_TYPE_CHILD;
1841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#else
1851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return views::WidgetDelegate::GetModalType();
1861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#endif
1871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)bool DownloadDangerPromptViews::Cancel() {
1901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  RunDone(CANCEL);
1931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return true;
1941e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)bool DownloadDangerPromptViews::Accept() {
1971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
1981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  RunDone(ACCEPT);
2001e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return true;
2011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)bool DownloadDangerPromptViews::Close() {
2041e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
2051e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  RunDone(DISMISS);
2071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return true;
2081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// TODO(wittman): Remove this override once we move to the new style frame
2111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// view on all dialogs.
2121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)views::NonClientFrameView* DownloadDangerPromptViews::CreateNonClientFrameView(
2131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    views::Widget* widget) {
2141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return CreateConstrainedStyleNonClientFrameView(
215f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      widget, web_contents_->GetBrowserContext());
2161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)views::View* DownloadDangerPromptViews::GetInitiallyFocusedView() {
2191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return GetDialogClientView()->cancel_button();
2201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)views::View* DownloadDangerPromptViews::GetContentsView() {
2231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return contents_view_;
2241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)views::Widget* DownloadDangerPromptViews::GetWidget() {
2271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return contents_view_->GetWidget();
2281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const views::Widget* DownloadDangerPromptViews::GetWidget() const {
2311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return contents_view_->GetWidget();
2321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// content::DownloadItem::Observer:
2351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void DownloadDangerPromptViews::OnDownloadUpdated(
2361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    content::DownloadItem* download) {
2371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // If the download is nolonger dangerous (accepted externally) or the download
2381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // is in a terminal state, then the download danger prompt is no longer
2391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // necessary.
2401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (!download_->IsDangerous() || download_->IsDone()) {
2411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    RunDone(DISMISS);
2421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    Cancel();
2431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
2441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
246a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 DownloadDangerPromptViews::GetAcceptButtonTitle() const {
2471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (show_context_)
2481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_CONFIRM_DOWNLOAD);
2491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  switch (download_->GetDangerType()) {
2501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL:
2511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT:
2521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST: {
2531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_CONFIRM_DOWNLOAD_AGAIN_MALICIOUS);
2541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    }
2551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    default:
2561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_CONFIRM_DOWNLOAD_AGAIN);
2571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
2581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
260a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 DownloadDangerPromptViews::GetCancelButtonTitle() const {
2611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (show_context_)
2621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_CANCEL);
2631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  switch (download_->GetDangerType()) {
2641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL:
2651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT:
2661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST: {
2671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_CONFIRM_CANCEL_AGAIN_MALICIOUS);
2681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    }
2691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    default:
2701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_CANCEL);
2711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
2721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
274a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 DownloadDangerPromptViews::GetMessageLead() const {
2751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (!show_context_) {
2761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    switch (download_->GetDangerType()) {
2771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL:
2781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT:
2791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST:
2801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        return l10n_util::GetStringUTF16(
2811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            IDS_PROMPT_CONFIRM_KEEP_MALICIOUS_DOWNLOAD_LEAD);
2821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      default:
2841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        break;
2851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    }
2861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
2871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
288a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return base::string16();
2891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
291a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 DownloadDangerPromptViews::GetMessageBody() const {
2921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (show_context_) {
2931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    switch (download_->GetDangerType()) {
2941e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE: {
2951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        return l10n_util::GetStringFUTF16(
2961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            IDS_PROMPT_DANGEROUS_DOWNLOAD,
2971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            download_->GetFileNameToReportUser().LossyDisplayName());
2981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      }
2991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL: // Fall through
3001e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT:
3011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST: {
3021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        return l10n_util::GetStringFUTF16(
3031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            IDS_PROMPT_MALICIOUS_DOWNLOAD_CONTENT,
3041e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            download_->GetFileNameToReportUser().LossyDisplayName());
3051e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      }
3061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT: {
3071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        return l10n_util::GetStringFUTF16(
3081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            IDS_PROMPT_UNCOMMON_DOWNLOAD_CONTENT,
3091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            download_->GetFileNameToReportUser().LossyDisplayName());
3101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      }
3111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED: {
3121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        return l10n_util::GetStringFUTF16(
3131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            IDS_PROMPT_DOWNLOAD_CHANGES_SETTINGS,
3141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            download_->GetFileNameToReportUser().LossyDisplayName());
3151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      }
3161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS:
3171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT:
3181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED:
3191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_MAX: {
3201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        break;
3211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      }
3221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    }
3231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  } else {
3241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    switch (download_->GetDangerType()) {
3251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL:
3261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT:
3271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST: {
3281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        return l10n_util::GetStringUTF16(
3291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            IDS_PROMPT_CONFIRM_KEEP_MALICIOUS_DOWNLOAD_BODY);
3301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      }
3311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      default: {
3321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        return l10n_util::GetStringUTF16(
3331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            IDS_PROMPT_CONFIRM_KEEP_DANGEROUS_DOWNLOAD);
3341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      }
3351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    }
3361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
3371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  NOTREACHED();
338a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return base::string16();
3391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
3401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
3411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void DownloadDangerPromptViews::RunDone(Action action) {
3421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Invoking the callback can cause the download item state to change or cause
3431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // the window to close, and |callback| refers to a member variable.
3441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  OnDone done = done_;
3451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  done_.Reset();
3461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (download_ != NULL) {
3471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    download_->RemoveObserver(this);
3481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    download_ = NULL;
3491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
3501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (!done.is_null())
3511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    done.Run(action);
3521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
3531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
3541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}  // namespace
3551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
3561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)DownloadDangerPrompt* DownloadDangerPrompt::Create(
3571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    content::DownloadItem* item,
3581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    content::WebContents* web_contents,
3591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    bool show_context,
3601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    const OnDone& done) {
3611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DownloadDangerPromptViews* download_danger_prompt =
3621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      new DownloadDangerPromptViews(item, web_contents, show_context, done);
3631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
3641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  WebContentsModalDialogManager* web_contents_modal_dialog_manager =
3651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      WebContentsModalDialogManager::FromWebContents(web_contents);
3661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  WebContentsModalDialogManagerDelegate* modal_delegate =
3671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      web_contents_modal_dialog_manager->delegate();
3681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  CHECK(modal_delegate);
3691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  views::Widget* dialog = views::Widget::CreateWindowAsFramelessChild(
3701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      download_danger_prompt,
3711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      modal_delegate->GetWebContentsModalDialogHost()->GetHostView());
3721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  web_contents_modal_dialog_manager->ShowDialog(dialog->GetNativeView());
3731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
3741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return download_danger_prompt;
3751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
376