1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/download/download_danger_prompt.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/chrome_notification_types.h"
10 #include "chrome/browser/download/chrome_download_manager_delegate.h"
11 #include "chrome/browser/download/download_stats.h"
12 #include "chrome/browser/ui/tab_modal_confirm_dialog.h"
13 #include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h"
14 #include "content/public/browser/download_danger_type.h"
15 #include "content/public/browser/download_item.h"
16 #include "grit/chromium_strings.h"
17 #include "grit/generated_resources.h"
18 #include "ui/base/l10n/l10n_util.h"
22 // TODO(wittman): Create a native web contents modal dialog implementation of
23 // this dialog for non-Views platforms, to support bold formatting of the
26 // Implements DownloadDangerPrompt using a TabModalConfirmDialog.
27 class DownloadDangerPromptImpl
: public DownloadDangerPrompt
,
28 public content::DownloadItem::Observer
,
29 public TabModalConfirmDialogDelegate
{
31 DownloadDangerPromptImpl(content::DownloadItem
* item
,
32 content::WebContents
* web_contents
,
35 virtual ~DownloadDangerPromptImpl();
37 // DownloadDangerPrompt:
38 virtual void InvokeActionForTesting(Action action
) OVERRIDE
;
41 // content::DownloadItem::Observer:
42 virtual void OnDownloadUpdated(content::DownloadItem
* download
) OVERRIDE
;
44 // TabModalConfirmDialogDelegate:
45 virtual base::string16
GetTitle() OVERRIDE
;
46 virtual base::string16
GetMessage() OVERRIDE
;
47 virtual base::string16
GetAcceptButtonTitle() OVERRIDE
;
48 virtual base::string16
GetCancelButtonTitle() OVERRIDE
;
49 virtual void OnAccepted() OVERRIDE
;
50 virtual void OnCanceled() OVERRIDE
;
51 virtual void OnClosed() OVERRIDE
;
53 void RunDone(Action action
);
55 content::DownloadItem
* download_
;
59 DISALLOW_COPY_AND_ASSIGN(DownloadDangerPromptImpl
);
62 DownloadDangerPromptImpl::DownloadDangerPromptImpl(
63 content::DownloadItem
* download
,
64 content::WebContents
* web_contents
,
67 : TabModalConfirmDialogDelegate(web_contents
),
69 show_context_(show_context
),
71 DCHECK(!done_
.is_null());
72 download_
->AddObserver(this);
73 RecordOpenedDangerousConfirmDialog(download_
->GetDangerType());
76 DownloadDangerPromptImpl::~DownloadDangerPromptImpl() {
77 // |this| might be deleted without invoking any callbacks. E.g. pressing Esc
78 // on GTK or if the user navigates away from the page showing the prompt.
82 void DownloadDangerPromptImpl::InvokeActionForTesting(Action action
) {
84 case ACCEPT
: Accept(); break;
85 case CANCEL
: Cancel(); break;
93 void DownloadDangerPromptImpl::OnDownloadUpdated(
94 content::DownloadItem
* download
) {
95 // If the download is nolonger dangerous (accepted externally) or the download
96 // is in a terminal state, then the download danger prompt is no longer
98 if (!download
->IsDangerous() || download
->IsDone()) {
104 base::string16
DownloadDangerPromptImpl::GetTitle() {
106 return l10n_util::GetStringUTF16(IDS_CONFIRM_KEEP_DANGEROUS_DOWNLOAD_TITLE
);
107 switch (download_
->GetDangerType()) {
108 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL
:
109 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT
:
110 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST
:
111 case content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED
: {
112 return l10n_util::GetStringUTF16(
113 IDS_RESTORE_KEEP_DANGEROUS_DOWNLOAD_TITLE
);
116 return l10n_util::GetStringUTF16(
117 IDS_CONFIRM_KEEP_DANGEROUS_DOWNLOAD_TITLE
);
122 base::string16
DownloadDangerPromptImpl::GetMessage() {
124 switch (download_
->GetDangerType()) {
125 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE
: {
126 return l10n_util::GetStringFUTF16(
127 IDS_PROMPT_DANGEROUS_DOWNLOAD
,
128 download_
->GetFileNameToReportUser().LossyDisplayName());
130 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL
: // Fall through
131 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT
:
132 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST
: {
133 return l10n_util::GetStringFUTF16(
134 IDS_PROMPT_MALICIOUS_DOWNLOAD_CONTENT
,
135 download_
->GetFileNameToReportUser().LossyDisplayName());
137 case content::DOWNLOAD_DANGER_TYPE_UNCOMMON_CONTENT
: {
138 return l10n_util::GetStringFUTF16(
139 IDS_PROMPT_UNCOMMON_DOWNLOAD_CONTENT
,
140 download_
->GetFileNameToReportUser().LossyDisplayName());
142 case content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED
: {
143 return l10n_util::GetStringFUTF16(
144 IDS_PROMPT_DOWNLOAD_CHANGES_SETTINGS
,
145 download_
->GetFileNameToReportUser().LossyDisplayName());
147 case content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS
:
148 case content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT
:
149 case content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED
:
150 case content::DOWNLOAD_DANGER_TYPE_MAX
: {
155 switch (download_
->GetDangerType()) {
156 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL
:
157 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT
:
158 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST
: {
159 return l10n_util::GetStringUTF16(
160 IDS_PROMPT_CONFIRM_KEEP_MALICIOUS_DOWNLOAD_LEAD
) +
161 base::ASCIIToUTF16("\n\n") +
162 l10n_util::GetStringUTF16(
163 IDS_PROMPT_CONFIRM_KEEP_MALICIOUS_DOWNLOAD_BODY
);
166 return l10n_util::GetStringUTF16(
167 IDS_PROMPT_CONFIRM_KEEP_DANGEROUS_DOWNLOAD
);
172 return base::string16();
175 base::string16
DownloadDangerPromptImpl::GetAcceptButtonTitle() {
177 return l10n_util::GetStringUTF16(IDS_CONFIRM_DOWNLOAD
);
178 switch (download_
->GetDangerType()) {
179 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL
:
180 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT
:
181 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST
:
182 case content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED
: {
183 return l10n_util::GetStringUTF16(IDS_CONFIRM_DOWNLOAD_AGAIN_MALICIOUS
);
186 return l10n_util::GetStringUTF16(IDS_CONFIRM_DOWNLOAD_AGAIN
);
190 base::string16
DownloadDangerPromptImpl::GetCancelButtonTitle() {
192 return l10n_util::GetStringUTF16(IDS_CANCEL
);
193 switch (download_
->GetDangerType()) {
194 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL
:
195 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT
:
196 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_HOST
:
197 case content::DOWNLOAD_DANGER_TYPE_POTENTIALLY_UNWANTED
: {
198 return l10n_util::GetStringUTF16(IDS_CONFIRM_CANCEL_AGAIN_MALICIOUS
);
201 return l10n_util::GetStringUTF16(IDS_CANCEL
);
205 void DownloadDangerPromptImpl::OnAccepted() {
209 void DownloadDangerPromptImpl::OnCanceled() {
213 void DownloadDangerPromptImpl::OnClosed() {
217 void DownloadDangerPromptImpl::RunDone(Action action
) {
218 // Invoking the callback can cause the download item state to change or cause
219 // the constrained window to close, and |callback| refers to a member
223 if (download_
!= NULL
) {
224 download_
->RemoveObserver(this);
233 #if !(defined(OS_WIN) || defined(USE_AURA))
235 DownloadDangerPrompt
* DownloadDangerPrompt::Create(
236 content::DownloadItem
* item
,
237 content::WebContents
* web_contents
,
239 const OnDone
& done
) {
240 DownloadDangerPromptImpl
* prompt
= new DownloadDangerPromptImpl(
241 item
, web_contents
, show_context
, done
);
242 // |prompt| will be deleted when the dialog is done.
243 TabModalConfirmDialog::Create(prompt
, web_contents
);