Min Qin | 7b7cbbbf | 2018-06-04 21:49:31 | [diff] [blame] | 1 | // Copyright 2018 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. |
| 4 | |
| 5 | #include "chrome/browser/download/download_open_prompt.h" |
| 6 | |
Jeremy Chinsen | e5459643 | 2019-05-29 00:18:46 | [diff] [blame] | 7 | #include <memory> |
| 8 | #include <utility> |
| 9 | |
Min Qin | 7b7cbbbf | 2018-06-04 21:49:31 | [diff] [blame] | 10 | #include "base/callback.h" |
| 11 | #include "base/strings/utf_string_conversions.h" |
| 12 | #include "chrome/browser/ui/browser_dialogs.h" |
| 13 | #include "chrome/browser/ui/tab_modal_confirm_dialog.h" |
| 14 | #include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h" |
| 15 | #include "chrome/grit/generated_resources.h" |
| 16 | #include "content/public/browser/web_contents.h" |
| 17 | #include "ui/base/l10n/l10n_util.h" |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | // Dialog class for prompting users about opening a download. |
| 22 | class DownloadOpenConfirmationDialog : public DownloadOpenPrompt, |
| 23 | public TabModalConfirmDialogDelegate { |
| 24 | public: |
| 25 | DownloadOpenConfirmationDialog( |
| 26 | content::WebContents* web_contents, |
| 27 | const std::string& extension_name, |
| 28 | const base::FilePath& file_path, |
| 29 | DownloadOpenPrompt::OpenCallback open_callback); |
| 30 | ~DownloadOpenConfirmationDialog() override; |
| 31 | |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame^] | 32 | std::u16string GetTitle() override; |
| 33 | std::u16string GetDialogMessage() override; |
| 34 | std::u16string GetAcceptButtonTitle() override; |
| 35 | std::u16string GetCancelButtonTitle() override; |
Min Qin | 7b7cbbbf | 2018-06-04 21:49:31 | [diff] [blame] | 36 | |
| 37 | private: |
| 38 | void OnAccepted() override; |
| 39 | void OnCanceled() override; |
| 40 | void OnClosed() override; |
| 41 | |
| 42 | DownloadOpenPrompt::OpenCallback open_callback_; |
| 43 | |
| 44 | std::string extension_name_; |
| 45 | |
| 46 | base::FilePath file_path_; |
| 47 | |
| 48 | DISALLOW_COPY_AND_ASSIGN(DownloadOpenConfirmationDialog); |
| 49 | }; |
| 50 | |
| 51 | DownloadOpenConfirmationDialog::DownloadOpenConfirmationDialog( |
| 52 | content::WebContents* web_contents, |
| 53 | const std::string& extension_name, |
| 54 | const base::FilePath& file_path, |
| 55 | DownloadOpenPrompt::OpenCallback open_callback) |
| 56 | : TabModalConfirmDialogDelegate(web_contents), |
| 57 | open_callback_(std::move(open_callback)), |
| 58 | extension_name_(extension_name), |
| 59 | file_path_(file_path) { |
| 60 | chrome::RecordDialogCreation( |
| 61 | chrome::DialogIdentifier::DOWNLOAD_OPEN_CONFIRMATION); |
| 62 | } |
| 63 | |
| 64 | DownloadOpenConfirmationDialog::~DownloadOpenConfirmationDialog() = default; |
| 65 | |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame^] | 66 | std::u16string DownloadOpenConfirmationDialog::GetTitle() { |
Min Qin | 7b7cbbbf | 2018-06-04 21:49:31 | [diff] [blame] | 67 | return l10n_util::GetStringUTF16(IDS_DOWNLOAD_OPEN_CONFIRMATION_DIALOG_TITLE); |
| 68 | } |
| 69 | |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame^] | 70 | std::u16string DownloadOpenConfirmationDialog::GetDialogMessage() { |
Min Qin | 7b7cbbbf | 2018-06-04 21:49:31 | [diff] [blame] | 71 | return l10n_util::GetStringFUTF16( |
| 72 | IDS_DOWNLOAD_OPEN_CONFIRMATION_DIALOG_MESSAGE, |
| 73 | base::UTF8ToUTF16(extension_name_), |
| 74 | file_path_.BaseName().AsUTF16Unsafe()); |
| 75 | } |
| 76 | |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame^] | 77 | std::u16string DownloadOpenConfirmationDialog::GetAcceptButtonTitle() { |
Min Qin | 7b7cbbbf | 2018-06-04 21:49:31 | [diff] [blame] | 78 | return l10n_util::GetStringUTF16(IDS_CONFIRM_MESSAGEBOX_YES_BUTTON_LABEL); |
| 79 | } |
| 80 | |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame^] | 81 | std::u16string DownloadOpenConfirmationDialog::GetCancelButtonTitle() { |
Min Qin | 7b7cbbbf | 2018-06-04 21:49:31 | [diff] [blame] | 82 | return l10n_util::GetStringUTF16(IDS_CONFIRM_MESSAGEBOX_NO_BUTTON_LABEL); |
| 83 | } |
| 84 | |
| 85 | void DownloadOpenConfirmationDialog::OnAccepted() { |
| 86 | std::move(open_callback_).Run(true); |
| 87 | } |
| 88 | |
| 89 | void DownloadOpenConfirmationDialog::OnCanceled() { |
| 90 | std::move(open_callback_).Run(false); |
| 91 | } |
| 92 | |
| 93 | void DownloadOpenConfirmationDialog::OnClosed() { |
| 94 | std::move(open_callback_).Run(false); |
| 95 | } |
| 96 | |
| 97 | } // namespace |
| 98 | |
| 99 | DownloadOpenPrompt::DownloadOpenPrompt() = default; |
| 100 | |
| 101 | DownloadOpenPrompt::~DownloadOpenPrompt() = default; |
| 102 | |
| 103 | DownloadOpenPrompt* DownloadOpenPrompt::CreateDownloadOpenConfirmationDialog( |
| 104 | content::WebContents* web_contents, |
| 105 | const std::string& extension_name, |
| 106 | const base::FilePath& file_path, |
| 107 | DownloadOpenPrompt::OpenCallback open_callback) { |
Jeremy Chinsen | e5459643 | 2019-05-29 00:18:46 | [diff] [blame] | 108 | auto prompt = std::make_unique<DownloadOpenConfirmationDialog>( |
Min Qin | 7b7cbbbf | 2018-06-04 21:49:31 | [diff] [blame] | 109 | web_contents, extension_name, file_path, std::move(open_callback)); |
Jeremy Chinsen | e5459643 | 2019-05-29 00:18:46 | [diff] [blame] | 110 | DownloadOpenConfirmationDialog* prompt_observer = prompt.get(); |
| 111 | TabModalConfirmDialog::Create(std::move(prompt), web_contents); |
| 112 | return prompt_observer; |
Min Qin | 7b7cbbbf | 2018-06-04 21:49:31 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void DownloadOpenPrompt::AcceptConfirmationDialogForTesting( |
| 116 | DownloadOpenPrompt* download_open_prompt) { |
| 117 | static_cast<DownloadOpenConfirmationDialog*>(download_open_prompt)->Accept(); |
| 118 | } |