download_danger_prompt_views.cc revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
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"
75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/download/download_stats.h"
803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "chrome/browser/extensions/api/experience_sampling_private/experience_sampling.h"
946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chrome/browser/ui/views/constrained_window_views.h"
106e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "chrome/grit/chromium_strings.h"
116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "chrome/grit/generated_resources.h"
121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "content/public/browser/browser_thread.h"
131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "content/public/browser/download_danger_type.h"
141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "content/public/browser/download_item.h"
151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "ui/base/l10n/l10n_util.h"
165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/base/resource/resource_bundle.h"
171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "ui/views/controls/button/label_button.h"
181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "ui/views/controls/label.h"
191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "ui/views/layout/grid_layout.h"
201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "ui/views/view.h"
211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "ui/views/widget/widget.h"
221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "ui/views/window/dialog_client_view.h"
231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)#include "ui/views/window/dialog_delegate.h"
241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)using extensions::ExperienceSamplingEvent;
2603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)namespace {
281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const int kMessageWidth = 320;
301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const int kParagraphPadding = 15;
311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// Views-specific implementation of download danger prompt dialog. We use this
331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// class rather than a TabModalConfirmDialog so that we can use custom
341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// formatting on the text in the body of the dialog.
351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)class DownloadDangerPromptViews : public DownloadDangerPrompt,
361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                  public content::DownloadItem::Observer,
371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                  public views::DialogDelegate {
381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) public:
391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DownloadDangerPromptViews(content::DownloadItem* item,
401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                            bool show_context,
411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                            const OnDone& done);
421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // DownloadDangerPrompt methods:
441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual void InvokeActionForTesting(Action action) OVERRIDE;
451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // views::DialogDelegate methods:
47a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual base::string16 GetDialogButtonLabel(
48a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      ui::DialogButton button) const OVERRIDE;
49a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  virtual base::string16 GetWindowTitle() const OVERRIDE;
501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual void DeleteDelegate() OVERRIDE;
511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual ui::ModalType GetModalType() const OVERRIDE;
521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual bool Cancel() OVERRIDE;
531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual bool Accept() OVERRIDE;
541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual bool Close() OVERRIDE;
551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual views::View* GetInitiallyFocusedView() OVERRIDE;
561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual views::View* GetContentsView() OVERRIDE;
571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual views::Widget* GetWidget() OVERRIDE;
581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual const views::Widget* GetWidget() const OVERRIDE;
591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // content::DownloadItem::Observer:
611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  virtual void OnDownloadUpdated(content::DownloadItem* download) OVERRIDE;
621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles) private:
64a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 GetAcceptButtonTitle() const;
65a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 GetCancelButtonTitle() const;
661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // The message lead is separated from the main text and is bolded.
67a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 GetMessageLead() const;
68a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 GetMessageBody() const;
691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void RunDone(Action action);
701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  content::DownloadItem* download_;
721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  bool show_context_;
731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  OnDone done_;
741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
7503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  scoped_ptr<ExperienceSamplingEvent> sampling_event_;
7603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  views::View* contents_view_;
781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)};
791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)DownloadDangerPromptViews::DownloadDangerPromptViews(
811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    content::DownloadItem* item,
821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    bool show_context,
831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    const OnDone& done)
841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    : download_(item),
851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      show_context_(show_context),
861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      done_(done),
871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      contents_view_(NULL) {
881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DCHECK(!done_.is_null());
891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  download_->AddObserver(this);
901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  contents_view_ = new views::View;
921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  views::GridLayout* layout = views::GridLayout::CreatePanel(contents_view_);
941e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  contents_view_->SetLayoutManager(layout);
951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  views::ColumnSet* column_set = layout->AddColumnSet(0);
971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1,
981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                        views::GridLayout::FIXED, kMessageWidth, 0);
991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
100a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  const base::string16 message_lead = GetMessageLead();
1011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (!message_lead.empty()) {
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ui::ResourceBundle* rb = &ui::ResourceBundle::GetSharedInstance();
1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    views::Label* message_lead_label = new views::Label(
1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        message_lead, rb->GetFontList(ui::ResourceBundle::BoldFont));
1061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    message_lead_label->SetMultiLine(true);
1071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    message_lead_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
1081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    message_lead_label->SetAllowCharacterBreak(true);
1091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    layout->StartRow(0, 0);
1111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    layout->AddView(message_lead_label);
1121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    layout->AddPaddingRow(0, kParagraphPadding);
1141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
1151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  views::Label* message_body_label = new views::Label(GetMessageBody());
1171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  message_body_label->SetMultiLine(true);
1181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  message_body_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
1191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  message_body_label->SetAllowCharacterBreak(true);
1201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  layout->StartRow(0, 0);
1221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  layout->AddView(message_body_label);
1235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  RecordOpenedDangerousConfirmDialog(download_->GetDangerType());
12503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)
12603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // ExperienceSampling: A malicious download warning is being shown to the
12703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // user, so we start a new SamplingEvent and track it.
12803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  sampling_event_.reset(new ExperienceSamplingEvent(
12903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      ExperienceSamplingEvent::kDownloadDangerPrompt,
13003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      item->GetURL(),
13103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      item->GetReferrerUrl(),
13203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      item->GetBrowserContext()));
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);
16546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (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() {
17646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
1771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  delete this;
1781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)ui::ModalType DownloadDangerPromptViews::GetModalType() const {
1811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return ui::MODAL_TYPE_CHILD;
1821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)bool DownloadDangerPromptViews::Cancel() {
18546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
18603b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // ExperienceSampling: User canceled the warning.
18703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  sampling_event_->CreateUserDecisionEvent(ExperienceSamplingEvent::kDeny);
1881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  RunDone(CANCEL);
1891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return true;
1901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
1921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)bool DownloadDangerPromptViews::Accept() {
19346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
19403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // ExperienceSampling: User proceeded through the warning.
19503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  sampling_event_->CreateUserDecisionEvent(ExperienceSamplingEvent::kProceed);
1961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  RunDone(ACCEPT);
1971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return true;
1981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
1991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2001e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)bool DownloadDangerPromptViews::Close() {
20146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
20203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  // ExperienceSampling: User canceled the warning.
20303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  sampling_event_->CreateUserDecisionEvent(ExperienceSamplingEvent::kDeny);
2041e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  RunDone(DISMISS);
2051e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return true;
2061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)views::View* DownloadDangerPromptViews::GetInitiallyFocusedView() {
2091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return GetDialogClientView()->cancel_button();
2101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)views::View* DownloadDangerPromptViews::GetContentsView() {
2131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return contents_view_;
2141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)views::Widget* DownloadDangerPromptViews::GetWidget() {
2171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return contents_view_->GetWidget();
2181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)const views::Widget* DownloadDangerPromptViews::GetWidget() const {
2211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return contents_view_->GetWidget();
2221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)// content::DownloadItem::Observer:
2251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void DownloadDangerPromptViews::OnDownloadUpdated(
2261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    content::DownloadItem* download) {
2271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // If the download is nolonger dangerous (accepted externally) or the download
2281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // is in a terminal state, then the download danger prompt is no longer
2291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // necessary.
2301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (!download_->IsDangerous() || download_->IsDone()) {
2311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    RunDone(DISMISS);
2321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    Cancel();
2331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
2341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
236a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 DownloadDangerPromptViews::GetAcceptButtonTitle() const {
2371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (show_context_)
2381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_CONFIRM_DOWNLOAD);
2391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  switch (download_->GetDangerType()) {
2401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL:
2411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT:
2421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST: {
2431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_CONFIRM_DOWNLOAD_AGAIN_MALICIOUS);
2441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    }
2451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    default:
2461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_CONFIRM_DOWNLOAD_AGAIN);
2471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
2481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
250a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 DownloadDangerPromptViews::GetCancelButtonTitle() const {
2511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (show_context_)
2521e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    return l10n_util::GetStringUTF16(IDS_CANCEL);
2531e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  switch (download_->GetDangerType()) {
2541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL:
2551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT:
2561e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST: {
2571e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_CONFIRM_CANCEL_AGAIN_MALICIOUS);
2581e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    }
2591e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    default:
2601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      return l10n_util::GetStringUTF16(IDS_CANCEL);
2611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
2621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2631e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
264a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 DownloadDangerPromptViews::GetMessageLead() const {
2651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (!show_context_) {
2661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    switch (download_->GetDangerType()) {
2671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL:
2681e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT:
2691e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST:
2701e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        return l10n_util::GetStringUTF16(
2711e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            IDS_PROMPT_CONFIRM_KEEP_MALICIOUS_DOWNLOAD_LEAD);
2721e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2731e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      default:
2741e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        break;
2751e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    }
2761e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
2771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
278a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return base::string16();
2791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
2801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
281a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 DownloadDangerPromptViews::GetMessageBody() const {
2821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (show_context_) {
2831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    switch (download_->GetDangerType()) {
2841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE: {
2851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        return l10n_util::GetStringFUTF16(
2861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            IDS_PROMPT_DANGEROUS_DOWNLOAD,
2871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            download_->GetFileNameToReportUser().LossyDisplayName());
2881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      }
2895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL:  // Fall through
2901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT:
2911e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST: {
2921e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        return l10n_util::GetStringFUTF16(
2931e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            IDS_PROMPT_MALICIOUS_DOWNLOAD_CONTENT,
2941e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            download_->GetFileNameToReportUser().LossyDisplayName());
2951e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      }
2961e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT: {
2971e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        return l10n_util::GetStringFUTF16(
2981e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            IDS_PROMPT_UNCOMMON_DOWNLOAD_CONTENT,
2991e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            download_->GetFileNameToReportUser().LossyDisplayName());
3001e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      }
3011e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED: {
3021e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        return l10n_util::GetStringFUTF16(
3031e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            IDS_PROMPT_DOWNLOAD_CHANGES_SETTINGS,
3041e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            download_->GetFileNameToReportUser().LossyDisplayName());
3051e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      }
3061e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS:
3071e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT:
3081e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED:
3091e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_MAX: {
3101e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        break;
3111e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      }
3121e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    }
3131e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  } else {
3141e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    switch (download_->GetDangerType()) {
3151e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL:
3161e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT:
3171e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST: {
3181e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        return l10n_util::GetStringUTF16(
3191e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            IDS_PROMPT_CONFIRM_KEEP_MALICIOUS_DOWNLOAD_BODY);
3201e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      }
3211e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      default: {
3221e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)        return l10n_util::GetStringUTF16(
3231e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)            IDS_PROMPT_CONFIRM_KEEP_DANGEROUS_DOWNLOAD);
3241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      }
3251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    }
3261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
3271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  NOTREACHED();
328a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return base::string16();
3291e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
3301e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
3311e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)void DownloadDangerPromptViews::RunDone(Action action) {
3321e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Invoking the callback can cause the download item state to change or cause
3331e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // the window to close, and |callback| refers to a member variable.
3341e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  OnDone done = done_;
3351e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  done_.Reset();
3361e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (download_ != NULL) {
3371e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    download_->RemoveObserver(this);
3381e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    download_ = NULL;
3391e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  }
3401e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  if (!done.is_null())
3411e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    done.Run(action);
3421e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
3431e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
3441e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}  // namespace
3451e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
3461e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)DownloadDangerPrompt* DownloadDangerPrompt::Create(
3471e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    content::DownloadItem* item,
3481e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    content::WebContents* web_contents,
3491e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    bool show_context,
3501e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)    const OnDone& done) {
3511e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  DownloadDangerPromptViews* download_danger_prompt =
35246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      new DownloadDangerPromptViews(item, show_context, done);
35346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  ShowWebModalDialogViews(download_danger_prompt, web_contents);
3541e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  return download_danger_prompt;
3551e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)}
356