Skip to content

Commit

Permalink
Add password prompt to download security view
Browse files Browse the repository at this point in the history
This CL uses the previously created DownloadBubblePasswordPromptView in
the download security view for deep scans.

Bug: 1466284
Change-Id: I6d0eb51ca78f36f44d300485818195589bc4a7b4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4808423
Commit-Queue: Daniel Rubery <drubery@chromium.org>
Reviewed-by: Lily Chen <chlily@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1187973}
  • Loading branch information
Daniel Rubery authored and Chromium LUCI CQ committed Aug 24, 2023
1 parent 717294d commit ab8878e
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "chrome/browser/download/bubble/download_bubble_prefs.h"
#include "chrome/browser/download/bubble/download_bubble_ui_controller.h"
#include "chrome/browser/download/download_item_warning_data.h"
#include "chrome/browser/safe_browsing/download_protection/download_protection_service.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/views/download/bubble/download_bubble_partial_view.h"
#include "chrome/browser/ui/views/download/bubble/download_bubble_primary_view.h"
Expand Down Expand Up @@ -128,6 +129,34 @@ void DownloadBubbleContentsView::AddSecuritySubpageWarningActionEvent(
}
}

void DownloadBubbleContentsView::ProcessDeepScanPress(
const ContentId& id,
const std::string& password) {
if (DownloadUIModel* model = GetDownloadModel(id); model) {
safe_browsing::DownloadProtectionService::UploadForConsumerDeepScanning(
model->GetDownloadItem(), password);
}
}

bool DownloadBubbleContentsView::IsEncryptedArchive(const ContentId& id) {
if (DownloadUIModel* model = GetDownloadModel(id); model) {
return DownloadItemWarningData::IsEncryptedArchive(
model->GetDownloadItem());
}

return false;
}

bool DownloadBubbleContentsView::HasPreviousIncorrectPassword(
const ContentId& id) {
if (DownloadUIModel* model = GetDownloadModel(id); model) {
return DownloadItemWarningData::HasIncorrectPassword(
model->GetDownloadItem());
}

return false;
}

void DownloadBubbleContentsView::SwitchToCurrentPage(
absl::optional<ContentId> id) {
primary_view_->SetVisible(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ class DownloadBubbleContentsView : public views::View,
void AddSecuritySubpageWarningActionEvent(
const offline_items_collection::ContentId& id,
DownloadItemWarningData::WarningAction action) override;
void ProcessDeepScanPress(const ContentId& id,
const std::string& password) override;
bool IsEncryptedArchive(const ContentId& id) override;
bool HasPreviousIncorrectPassword(const ContentId& id) override;

// Gets the row view at the given index.
DownloadBubbleRowView* GetPrimaryViewRowForTesting(size_t index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
#include "base/containers/fixed_flat_map.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/strcat.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/download/download_commands.h"
#include "chrome/browser/download/download_item_model.h"
#include "chrome/browser/download/download_ui_model.h"
#include "chrome/browser/download/offline_item_utils.h"
#include "chrome/browser/ui/layout_constants.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/download/bubble/download_bubble_password_prompt_view.h"
#include "chrome/browser/ui/views/download/bubble/download_bubble_row_view.h"
#include "chrome/browser/ui/views/download/bubble/download_toolbar_button_view.h"
#include "chrome/grit/generated_resources.h"
Expand Down Expand Up @@ -401,7 +403,7 @@ void DownloadBubbleSecurityView::UpdateSecondaryIconAndText() {
secondary_styled_label_->PreferredSizeChanged();
}

void DownloadBubbleSecurityView::AddIconAndText() {
void DownloadBubbleSecurityView::AddIconAndContents() {
const int side_margin = ChromeLayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_RELATED_CONTROL_VERTICAL);
const int icon_label_spacing = ChromeLayoutProvider::Get()->GetDistanceMetric(
Expand Down Expand Up @@ -495,6 +497,8 @@ void DownloadBubbleSecurityView::AddIconAndText() {
// paragraph spacing between them.
learn_more_link_->SetProperty(views::kMarginsKey,
gfx::Insets().set_top(kAfterParagraphSpacing));

AddPasswordPrompt(wrapper);
}

void DownloadBubbleSecurityView::AddSecondaryIconAndText() {
Expand Down Expand Up @@ -581,6 +585,23 @@ void DownloadBubbleSecurityView::AddProgressBar() {
progress_bar_->SetVisible(false);
}

void DownloadBubbleSecurityView::AddPasswordPrompt(views::View* parent) {
password_prompt_ = parent->AddChildView(
std::make_unique<DownloadBubblePasswordPromptView>());
password_prompt_->SetVisible(false);
password_prompt_->SetProperty(
views::kFlexBehaviorKey,
views::FlexSpecification(views::MinimumFlexSizeRule::kScaleToMinimum,
views::MaximumFlexSizeRule::kUnbounded,
/*adjust_height_for_width=*/false));
password_prompt_->SetProperty(
views::kMarginsKey,
gfx::Insets::VH(ChromeLayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_RELATED_CONTROL_VERTICAL),
0));
UpdatePasswordPrompt();
}

bool DownloadBubbleSecurityView::ProcessButtonClick(
DownloadCommands::Command command,
bool is_secondary_button) {
Expand All @@ -592,6 +613,12 @@ bool DownloadBubbleSecurityView::ProcessButtonClick(
return true;
}

if (command == DownloadCommands::DEEP_SCAN &&
base::FeatureList::IsEnabled(
safe_browsing::kDeepScanningEncryptedArchives)) {
return ProcessDeepScanClick();
}

// Record metrics only if we are actually processing the command.
RecordWarningActionTime(is_secondary_button);
base::UmaHistogramEnumeration(
Expand Down Expand Up @@ -679,6 +706,25 @@ void DownloadBubbleSecurityView::UpdateProgressBar() {
progress_bar_->SetValue(-1);
}

void DownloadBubbleSecurityView::UpdatePasswordPrompt() {
if (!base::FeatureList::IsEnabled(
safe_browsing::kDeepScanningEncryptedArchives)) {
return;
}

bool should_show =
danger_type_ == download::DOWNLOAD_DANGER_TYPE_PROMPT_FOR_SCANNING &&
delegate_->IsEncryptedArchive(content_id_);

DownloadBubblePasswordPromptView::State state =
delegate_->HasPreviousIncorrectPassword(content_id_)
? DownloadBubblePasswordPromptView::State::kInvalid
: DownloadBubblePasswordPromptView::State::kValid;

password_prompt_->SetVisible(should_show);
password_prompt_->SetState(state);
}

void DownloadBubbleSecurityView::ClearWideFields() {
bubble_delegate_->set_fixed_width(0);
bubble_delegate_->SetButtonLabel(ui::DIALOG_BUTTON_CANCEL, std::u16string());
Expand Down Expand Up @@ -793,6 +839,7 @@ void DownloadBubbleSecurityView::UpdateViews() {
UpdateIconAndText();
UpdateSecondaryIconAndText();
UpdateProgressBar();
UpdatePasswordPrompt();

bubble_delegate_->SizeToContents();
}
Expand Down Expand Up @@ -832,7 +879,7 @@ DownloadBubbleSecurityView::DownloadBubbleSecurityView(
SetProperty(views::kMarginsKey, GetLayoutInsets(DOWNLOAD_ROW));
}
AddHeader();
AddIconAndText();
AddIconAndContents();
AddSecondaryIconAndText();
AddProgressBar();
}
Expand Down Expand Up @@ -867,5 +914,19 @@ int DownloadBubbleSecurityView::GetMinimumLabelWidth() const {
GetLayoutInsets(DOWNLOAD_ICON).width() - icon_label_spacing;
}

bool DownloadBubbleSecurityView::ProcessDeepScanClick() {
if (delegate_->IsEncryptedArchive(content_id_) &&
password_prompt_->GetText().empty()) {
password_prompt_->SetState(
DownloadBubblePasswordPromptView::State::kInvalidEmpty);
bubble_delegate_->SizeToContents();
} else {
delegate_->ProcessDeepScanPress(
content_id_, base::UTF16ToUTF8(password_prompt_->GetText()));
}

return false;
}

BEGIN_METADATA(DownloadBubbleSecurityView, views::View)
END_METADATA
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class LabelButton;

class DownloadBubbleNavigationHandler;
class ParagraphsView;
class DownloadBubblePasswordPromptView;

class DownloadBubbleSecurityView : public views::View,
public download::DownloadItem::Observer {
Expand All @@ -47,6 +48,21 @@ class DownloadBubbleSecurityView : public views::View,
virtual void AddSecuritySubpageWarningActionEvent(
const offline_items_collection::ContentId& id,
DownloadItemWarningData::WarningAction action) = 0;

// Processes the deep scan being pressed, when the given password is
// provided.
virtual void ProcessDeepScanPress(
const offline_items_collection::ContentId& id,
const std::string& password) = 0;

// Return whether the download item is an encrypted archive.
virtual bool IsEncryptedArchive(
const offline_items_collection::ContentId& id) = 0;

// Return whether the download item has a previously provided invalid
// password.
virtual bool HasPreviousIncorrectPassword(
const offline_items_collection::ContentId& id) = 0;
};

METADATA_HEADER(DownloadBubbleSecurityView);
Expand Down Expand Up @@ -105,9 +121,10 @@ class DownloadBubbleSecurityView : public views::View,
void AddHeader();
void CloseBubble();
void OnCheckboxClicked();
void AddIconAndText();
void AddIconAndContents();
void AddSecondaryIconAndText();
void AddProgressBar();
void AddPasswordPrompt(views::View* parent);

void UpdateViews();
void UpdateHeader();
Expand All @@ -120,6 +137,7 @@ class DownloadBubbleSecurityView : public views::View,
bool has_checkbox);
void UpdateButtons();
void UpdateProgressBar();
void UpdatePasswordPrompt();

// Reset fields that increase the width of the bubble.
void ClearWideFields();
Expand All @@ -132,6 +150,10 @@ class DownloadBubbleSecurityView : public views::View,
// Minimum width for the subpage summary.
int GetMinimumLabelWidth() const;

// Deep scanning is complicated enough that this button click is separate from
// the others.
bool ProcessDeepScanClick();

// Must outlive this.
const raw_ptr<Delegate> delegate_;

Expand Down Expand Up @@ -168,6 +190,7 @@ class DownloadBubbleSecurityView : public views::View,
raw_ptr<views::StyledLabel> deep_scanning_link_ = nullptr;
raw_ptr<views::StyledLabel> learn_more_link_ = nullptr;
raw_ptr<views::ProgressBar> progress_bar_ = nullptr;
raw_ptr<DownloadBubblePasswordPromptView> password_prompt_ = nullptr;

// Records the last time this was shown or updated for a new download. Used
// for metrics.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ class MockDownloadBubbleSecurityViewDelegate
item, WarningSurface::BUBBLE_SUBPAGE, action);
}

void ProcessDeepScanPress(const ContentId&, const std::string&) override {}
bool IsEncryptedArchive(const ContentId&) override { return false; }
bool HasPreviousIncorrectPassword(const ContentId&) override { return false; }

void set_should_close(bool should_close) { should_close_ = should_close; }

private:
Expand Down

0 comments on commit ab8878e

Please sign in to comment.