Commit fcfdd3fc authored by Elly Fong-Jones's avatar Elly Fong-Jones Committed by Commit Bot

cbuiv: migrate CryptoModulePasswordDialogView to new callbacks

This change removes CryptoModulePasswordDialogView::{Accept,Cancel} and
replaces them with explicit callbacks. It also changes the behavior
(hence the separate CL): the old code used to explicitly clear the
password entry field's text on the closure path. I can't find any good
reason why this would be done, and views::TextField has never guaranteed
that setting the text will cause the old text to be securely erased from
memory. In any case, if we want that behavior, it should happen in
//ui/views, not here.

Bug: 1011446
Change-Id: I3b81438511cf8eb3e52241543024ec081c467636
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2062887
Commit-Queue: Elly Fong-Jones <ellyjones@chromium.org>
Reviewed-by: default avatarPeter Kasting <pkasting@chromium.org>
Cr-Commit-Position: refs/heads/master@{#743149}
parent 5ef8d0fb
...@@ -28,6 +28,13 @@ CryptoModulePasswordDialogView::CryptoModulePasswordDialogView( ...@@ -28,6 +28,13 @@ CryptoModulePasswordDialogView::CryptoModulePasswordDialogView(
DialogDelegate::set_button_label( DialogDelegate::set_button_label(
ui::DIALOG_BUTTON_OK, ui::DIALOG_BUTTON_OK,
l10n_util::GetStringUTF16(IDS_CRYPTO_MODULE_AUTH_DIALOG_OK_BUTTON_LABEL)); l10n_util::GetStringUTF16(IDS_CRYPTO_MODULE_AUTH_DIALOG_OK_BUTTON_LABEL));
DialogDelegate::set_accept_callback(base::BindOnce(
[](CryptoModulePasswordDialogView* dialog) {
dialog->callback_.Run(
base::UTF16ToUTF8(dialog->password_entry_->GetText()));
},
base::Unretained(this)));
DialogDelegate::set_cancel_callback(base::BindOnce(callback_, std::string()));
set_margins(ChromeLayoutProvider::Get()->GetDialogInsetsForContentType( set_margins(ChromeLayoutProvider::Get()->GetDialogInsetsForContentType(
views::TEXT, views::CONTROL)); views::TEXT, views::CONTROL));
Init(hostname, slot_name, reason); Init(hostname, slot_name, reason);
...@@ -52,20 +59,6 @@ base::string16 CryptoModulePasswordDialogView::GetWindowTitle() const { ...@@ -52,20 +59,6 @@ base::string16 CryptoModulePasswordDialogView::GetWindowTitle() const {
return l10n_util::GetStringUTF16(IDS_CRYPTO_MODULE_AUTH_DIALOG_TITLE); return l10n_util::GetStringUTF16(IDS_CRYPTO_MODULE_AUTH_DIALOG_TITLE);
} }
bool CryptoModulePasswordDialogView::Cancel() {
callback_.Run(std::string());
const base::string16 empty;
password_entry_->SetText(empty);
return true;
}
bool CryptoModulePasswordDialogView::Accept() {
callback_.Run(base::UTF16ToUTF8(password_entry_->GetText()));
const base::string16 empty;
password_entry_->SetText(empty);
return true;
}
void CryptoModulePasswordDialogView::ContentsChanged( void CryptoModulePasswordDialogView::ContentsChanged(
views::Textfield* sender, views::Textfield* sender,
const base::string16& new_contents) { const base::string16& new_contents) {
......
...@@ -29,17 +29,16 @@ class CryptoModulePasswordDialogView : public views::DialogDelegateView, ...@@ -29,17 +29,16 @@ class CryptoModulePasswordDialogView : public views::DialogDelegateView,
~CryptoModulePasswordDialogView() override; ~CryptoModulePasswordDialogView() override;
private: private:
FRIEND_TEST_ALL_PREFIXES(CryptoModulePasswordDialogViewTest, TestAccept); FRIEND_TEST_ALL_PREFIXES(CryptoModulePasswordDialogViewTest,
AcceptUsesPassword);
FRIEND_TEST_ALL_PREFIXES(CryptoModulePasswordDialogViewTest,
CancelDoesntUsePassword);
// views::WidgetDelegate: // views::WidgetDelegate:
views::View* GetInitiallyFocusedView() override; views::View* GetInitiallyFocusedView() override;
ui::ModalType GetModalType() const override; ui::ModalType GetModalType() const override;
base::string16 GetWindowTitle() const override; base::string16 GetWindowTitle() const override;
// views::DialogDelegate:
bool Cancel() override;
bool Accept() override;
// views::TextfieldController: // views::TextfieldController:
void ContentsChanged(views::Textfield* sender, void ContentsChanged(views::Textfield* sender,
const base::string16& new_contents) override; const base::string16& new_contents) override;
......
...@@ -6,43 +6,47 @@ ...@@ -6,43 +6,47 @@
#include <string> #include <string>
#include "base/bind.h"
#include "base/strings/utf_string_conversions.h" #include "base/strings/utf_string_conversions.h"
#include "base/test/bind_test_util.h"
#include "chrome/browser/ui/crypto_module_password_dialog.h" #include "chrome/browser/ui/crypto_module_password_dialog.h"
#include "chrome/test/views/chrome_views_test_base.h" #include "chrome/test/views/chrome_views_test_base.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#include "ui/views/controls/textfield/textfield.h" #include "ui/views/controls/textfield/textfield.h"
#include "ui/views/test/views_test_base.h" #include "ui/views/test/views_test_base.h"
class CryptoModulePasswordDialogViewTest : public ChromeViewsTestBase { using CryptoModulePasswordDialogViewTest = ChromeViewsTestBase;
public:
CryptoModulePasswordDialogViewTest() {} std::unique_ptr<CryptoModulePasswordDialogView> CreateCryptoDialog(
~CryptoModulePasswordDialogViewTest() override {} const CryptoModulePasswordCallback& callback) {
return std::make_unique<CryptoModulePasswordDialogView>(
void Capture(const std::string& text) { "slot", kCryptoModulePasswordCertEnrollment, "server", callback);
text_ = text; }
}
TEST_F(CryptoModulePasswordDialogViewTest, AcceptUsesPassword) {
void CreateCryptoDialog(const CryptoModulePasswordCallback& callback) { std::string password;
dialog_ = std::make_unique<CryptoModulePasswordDialogView>( auto dialog = CreateCryptoDialog(base::BindLambdaForTesting(
"slot", kCryptoModulePasswordCertEnrollment, "server", callback); [&](const std::string& text) { password = text; }));
} EXPECT_EQ(dialog->password_entry_, dialog->GetInitiallyFocusedView());
EXPECT_TRUE(dialog->GetModalType() != ui::MODAL_TYPE_NONE);
std::string text_;
std::unique_ptr<CryptoModulePasswordDialogView> dialog_; const std::string kPassword = "diAl0g";
}; dialog->password_entry_->SetText(base::ASCIIToUTF16(kPassword));
EXPECT_TRUE(dialog->Accept());
TEST_F(CryptoModulePasswordDialogViewTest, TestAccept) { EXPECT_EQ(kPassword, password);
CryptoModulePasswordCallback cb( }
base::Bind(&CryptoModulePasswordDialogViewTest::Capture,
base::Unretained(this))); TEST_F(CryptoModulePasswordDialogViewTest, CancelDoesntUsePassword) {
CreateCryptoDialog(cb); std::string password;
EXPECT_EQ(dialog_->password_entry_, dialog_->GetInitiallyFocusedView()); bool callback_run = false;
EXPECT_TRUE(dialog_->GetModalType() != ui::MODAL_TYPE_NONE); auto dialog = CreateCryptoDialog(
base::BindLambdaForTesting([&](const std::string& text) {
callback_run = true;
password = text;
}));
const std::string kPassword = "diAl0g"; const std::string kPassword = "diAl0g";
dialog_->password_entry_->SetText(base::ASCIIToUTF16(kPassword)); dialog->password_entry_->SetText(base::ASCIIToUTF16(kPassword));
EXPECT_TRUE(dialog_->Accept()); EXPECT_TRUE(dialog->Cancel());
EXPECT_EQ(kPassword, text_); EXPECT_TRUE(callback_run);
const base::string16 empty; EXPECT_EQ("", password);
EXPECT_EQ(empty, dialog_->password_entry_->GetText());
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment