diff options
author | asanka@chromium.org <asanka@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-06 02:49:12 +0000 |
---|---|---|
committer | asanka@chromium.org <asanka@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-06 02:49:12 +0000 |
commit | 9ffabfb5e3e3e3de923d60a4ad4d1337b6895345 (patch) | |
tree | 275a04d47b5af436751e52935d37d280817c28ee /chrome/browser/ui | |
parent | d5fbd900ab784c2fcc73e62b5496106487da2909 (diff) | |
download | chromium_src-9ffabfb5e3e3e3de923d60a4ad4d1337b6895345.zip chromium_src-9ffabfb5e3e3e3de923d60a4ad4d1337b6895345.tar.gz chromium_src-9ffabfb5e3e3e3de923d60a4ad4d1337b6895345.tar.bz2 |
Refactor dangerous download warning text generation into DownloadItemModel.
Previously, we had multiple copies of the logic for generating warning text for a dangerous or malicious download. Move all of these into DownloadModel. As an additional benefit, we no longer require UI code to interpret DownloadItem::GetDangerType().
BUG=116551
TEST=Start a dangerous, malicious, and extension download. Check whether the warning text is correct, and any embedded filename is elided if necessary.
Review URL: http://codereview.chromium.org/9569011
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@125082 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/ui')
6 files changed, 45 insertions, 199 deletions
diff --git a/chrome/browser/ui/cocoa/download/download_item_controller.h b/chrome/browser/ui/cocoa/download/download_item_controller.h index d06cb14..1a64dcc 100644 --- a/chrome/browser/ui/cocoa/download/download_item_controller.h +++ b/chrome/browser/ui/cocoa/download/download_item_controller.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -20,6 +20,10 @@ namespace content { class DownloadItem; } +namespace gfx { +class Font; +} + // A controller class that manages one download item. @interface DownloadItemController : NSViewController { @@ -56,6 +60,9 @@ class DownloadItem; // The time at which this view was created. base::Time creationTime_; + // Default font to use for text metrics. + scoped_ptr<gfx::Font> font_; + // The state of this item. enum DownoadItemState { kNormal, diff --git a/chrome/browser/ui/cocoa/download/download_item_controller.mm b/chrome/browser/ui/cocoa/download/download_item_controller.mm index aa896e5..faac76d 100644 --- a/chrome/browser/ui/cocoa/download/download_item_controller.mm +++ b/chrome/browser/ui/cocoa/download/download_item_controller.mm @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -29,6 +29,7 @@ #include "ui/base/l10n/l10n_util_mac.h" #include "ui/base/resource/resource_bundle.h" #include "ui/base/text/text_elider.h" +#include "ui/gfx/font.h" #include "ui/gfx/image/image.h" using content::DownloadItem; @@ -41,10 +42,6 @@ namespace { // rules so all platforms can get inline in the future. const int kTextWidth = 140; // Pixels -// The maximum number of characters we show in a file name when displaying the -// dangerous download message. -const int kFileNameMaxLength = 20; - // The maximum width in pixels for the file name tooltip. const int kToolTipMaxWidth = 900; @@ -117,6 +114,7 @@ class DownloadShelfContextMenuMac : public DownloadShelfContextMenu { shelf_ = shelf; state_ = kNormal; creationTime_ = base::Time::Now(); + font_.reset(new gfx::Font()); } return self; } @@ -165,7 +163,7 @@ class DownloadShelfContextMenuMac : public DownloadShelfContextMenu { DCHECK_EQ(bridge_->download_model(), downloadModel); // Handle dangerous downloads. - if (downloadModel->download()->GetSafetyState() == DownloadItem::DANGEROUS) { + if (downloadModel->IsDangerous()) { [self setState:kDangerous]; ResourceBundle& rb = ResourceBundle::GetSharedInstance(); @@ -173,61 +171,15 @@ class DownloadShelfContextMenuMac : public DownloadShelfContextMenu { NSString* confirmButtonTitle; NSImage* alertIcon; - // The dangerous download label, button text and icon are different under - // different cases. - if (downloadModel->download()->GetDangerType() == - content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL) { - // TODO(noelutz): add support for malicious content. - // Safebrowsing shows the download URL leads to malicious file. + dangerousWarning = + base::SysUTF16ToNSString(downloadModel->GetWarningText( + *font_, kTextWidth)); + confirmButtonTitle = + base::SysUTF16ToNSString(downloadModel->GetWarningConfirmButtonText()); + if (downloadModel->IsMalicious()) alertIcon = rb.GetNativeImageNamed(IDR_SAFEBROWSING_WARNING); - dangerousWarning = l10n_util::GetNSStringWithFixup( - IDS_PROMPT_MALICIOUS_DOWNLOAD_URL); - confirmButtonTitle = l10n_util::GetNSStringWithFixup( - IDS_CONFIRM_DOWNLOAD); - } else { - // It's a dangerous file type (e.g.: an executable). - DCHECK_EQ(downloadModel->download()->GetDangerType(), - content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); + else alertIcon = rb.GetNativeImageNamed(IDR_WARNING); - if (ChromeDownloadManagerDelegate::IsExtensionDownload( - downloadModel->download())) { - dangerousWarning = l10n_util::GetNSStringWithFixup( - IDS_PROMPT_DANGEROUS_DOWNLOAD_EXTENSION); - confirmButtonTitle = l10n_util::GetNSStringWithFixup( - IDS_CONTINUE_EXTENSION_DOWNLOAD); - } else { - // This basic fixup copies Windows DownloadItemView::DownloadItemView(). - - // Extract the file extension (if any). - FilePath filename(downloadModel->download()->GetTargetName()); - FilePath::StringType extension = filename.Extension(); - - // Remove leading '.' from the extension - if (extension.length() > 0) - extension = extension.substr(1); - - // Elide giant extensions. - if (extension.length() > kFileNameMaxLength / 2) { - string16 utf16_extension; - ui::ElideString(UTF8ToUTF16(extension), kFileNameMaxLength / 2, - &utf16_extension); - extension = UTF16ToUTF8(utf16_extension); - } - - // Rebuild the filename.extension. - string16 rootname = UTF8ToUTF16(filename.RemoveExtension().value()); - ui::ElideString(rootname, kFileNameMaxLength - extension.length(), - &rootname); - std::string new_filename = UTF16ToUTF8(rootname); - if (extension.length()) - new_filename += std::string(".") + extension; - - dangerousWarning = l10n_util::GetNSStringFWithFixup( - IDS_PROMPT_DANGEROUS_DOWNLOAD, UTF8ToUTF16(new_filename)); - confirmButtonTitle = - l10n_util::GetNSStringWithFixup(IDS_CONFIRM_DOWNLOAD); - } - } DCHECK(alertIcon); [image_ setImage:alertIcon]; DCHECK(dangerousWarning); @@ -294,8 +246,7 @@ class DownloadShelfContextMenuMac : public DownloadShelfContextMenu { - (void)updateToolTip { string16 elidedFilename = ui::ElideFilename( - [self download]->GetFileNameToReportUser(), - gfx::Font(), kToolTipMaxWidth); + [self download]->GetFileNameToReportUser(), *font_, kToolTipMaxWidth); [progressView_ setToolTip:base::SysUTF16ToNSString(elidedFilename)]; } diff --git a/chrome/browser/ui/cocoa/download/download_item_mac.mm b/chrome/browser/ui/cocoa/download/download_item_mac.mm index 8322196..ebe669cd 100644 --- a/chrome/browser/ui/cocoa/download/download_item_mac.mm +++ b/chrome/browser/ui/cocoa/download/download_item_mac.mm @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -30,8 +30,7 @@ DownloadItemMac::~DownloadItemMac() { void DownloadItemMac::OnDownloadUpdated(content::DownloadItem* download) { DCHECK_EQ(download, download_model_->download()); - if ([item_controller_ isDangerousMode] && - download->GetSafetyState() == DownloadItem::DANGEROUS_BUT_VALIDATED) { + if ([item_controller_ isDangerousMode] && !download_model_->IsDangerous()) { // We have been approved. [item_controller_ clearDangerousMode]; } diff --git a/chrome/browser/ui/gtk/download/download_item_gtk.cc b/chrome/browser/ui/gtk/download/download_item_gtk.cc index d360572..f69f5ae 100644 --- a/chrome/browser/ui/gtk/download/download_item_gtk.cc +++ b/chrome/browser/ui/gtk/download/download_item_gtk.cc @@ -193,7 +193,7 @@ DownloadItemGtk::DownloadItemGtk(DownloadShelfGtk* parent_shelf, new_item_animation_->SetSlideDuration(kNewItemAnimationDurationMs); gtk_widget_show_all(hbox_.get()); - if (IsDangerous()) { + if (download_model_->IsDangerous()) { // Hide the download item components for now. gtk_widget_set_no_show_all(body_.get(), TRUE); gtk_widget_set_no_show_all(menu_button_, TRUE); @@ -233,11 +233,7 @@ DownloadItemGtk::DownloadItemGtk(DownloadShelfGtk* parent_shelf, // Create the ok button. GtkWidget* dangerous_accept = gtk_button_new_with_label( - l10n_util::GetStringUTF8( - ChromeDownloadManagerDelegate::IsExtensionDownload( - download_model->download()) ? - IDS_CONTINUE_EXTENSION_DOWNLOAD : - IDS_CONFIRM_DOWNLOAD).c_str()); + UTF16ToUTF8(download_model_->GetWarningConfirmButtonText()).c_str()); g_signal_connect(dangerous_accept, "clicked", G_CALLBACK(OnDangerousAcceptThunk), this); gtk_util::CenterWidgetInHBox(dangerous_hbox_.get(), dangerous_accept, false, @@ -263,7 +259,7 @@ DownloadItemGtk::DownloadItemGtk(DownloadShelfGtk* parent_shelf, theme_service_->InitThemesFor(this); // Set the initial width of the widget to be animated. - if (IsDangerous()) { + if (download_model_->IsDangerous()) { gtk_widget_set_size_request(dangerous_hbox_.get(), dangerous_hbox_start_width_, -1); } else { @@ -301,8 +297,7 @@ DownloadItemGtk::~DownloadItemGtk() { void DownloadItemGtk::OnDownloadUpdated(DownloadItem* download) { DCHECK_EQ(download, get_download()); - if (dangerous_prompt_ != NULL && - download->GetSafetyState() == DownloadItem::DANGEROUS_BUT_VALIDATED) { + if (dangerous_prompt_ != NULL && !download_model_->IsDangerous()) { // We have been approved. gtk_widget_set_no_show_all(body_.get(), FALSE); gtk_widget_set_no_show_all(menu_button_, FALSE); @@ -375,7 +370,7 @@ void DownloadItemGtk::AnimationProgressed(const ui::Animation* animation) { gtk_widget_queue_draw(progress_area_.get()); } else { DCHECK(animation == new_item_animation_.get()); - if (IsDangerous()) { + if (download_model_->IsDangerous()) { int progress = static_cast<int>((dangerous_hbox_full_width_ - dangerous_hbox_start_width_) * animation->GetCurrentValue()); @@ -425,10 +420,6 @@ DownloadItem* DownloadItemGtk::get_download() { return download_model_->download(); } -bool DownloadItemGtk::IsDangerous() { - return get_download()->GetSafetyState() == DownloadItem::DANGEROUS; -} - // Download progress animation functions. void DownloadItemGtk::UpdateDownloadProgress() { @@ -566,31 +557,8 @@ void DownloadItemGtk::UpdateDangerWarning() { // We create |dangerous_warning| as a wide string so we can more easily // calculate its length in characters. - string16 dangerous_warning; - - // The dangerous download label text is different for different cases. - if (get_download()->GetDangerType() == - content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL) { - // TODO(noelutz): handle malicious content warning. - // Safebrowsing shows the download URL leads to malicious file. - dangerous_warning = - l10n_util::GetStringUTF16(IDS_PROMPT_MALICIOUS_DOWNLOAD_URL); - } else { - // It's a dangerous file type (e.g.: an executable). - DCHECK(get_download()->GetDangerType() == - content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); - if (ChromeDownloadManagerDelegate::IsExtensionDownload(get_download())) { - dangerous_warning = - l10n_util::GetStringUTF16(IDS_PROMPT_DANGEROUS_DOWNLOAD_EXTENSION); - } else { - string16 elided_filename = ui::ElideFilename( - get_download()->GetTargetName(), gfx::Font(), kTextWidth); - dangerous_warning = - l10n_util::GetStringFUTF16(IDS_PROMPT_DANGEROUS_DOWNLOAD, - elided_filename); - } - } - + string16 dangerous_warning = + download_model_->GetWarningText(gfx::Font(), kTextWidth); if (theme_service_->UsingNativeTheme()) { gtk_util::SetLabelColor(dangerous_label_, NULL); } else { @@ -637,17 +605,15 @@ void DownloadItemGtk::UpdateDangerWarning() { void DownloadItemGtk::UpdateDangerIcon() { if (theme_service_->UsingNativeTheme()) { - const char* stock = get_download()->GetDangerType() == - content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL ? - GTK_STOCK_DIALOG_ERROR : GTK_STOCK_DIALOG_WARNING; + const char* stock = download_model_->IsMalicious() ? + GTK_STOCK_DIALOG_ERROR : GTK_STOCK_DIALOG_WARNING; gtk_image_set_from_stock( GTK_IMAGE(dangerous_image_), stock, GTK_ICON_SIZE_SMALL_TOOLBAR); } else { // Set the warning icon. ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); - int pixbuf_id = get_download()->GetDangerType() == - content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL ? - IDR_SAFEBROWSING_WARNING : IDR_WARNING; + int pixbuf_id = download_model_->IsMalicious() ? + IDR_SAFEBROWSING_WARNING : IDR_WARNING; GdkPixbuf* download_pixbuf = rb.GetNativeImageNamed(pixbuf_id); gtk_image_set_from_pixbuf(GTK_IMAGE(dangerous_image_), download_pixbuf); } @@ -729,7 +695,7 @@ gboolean DownloadItemGtk::OnHboxExpose(GtkWidget* widget, GdkEventExpose* e) { int width = allocation.width - border_width * 2; int height = allocation.height - border_width * 2; - if (IsDangerous()) { + if (download_model_->IsDangerous()) { // Draw a simple frame around the area when we're displaying the warning. gtk_paint_shadow(gtk_widget_get_style(widget), gtk_widget_get_window(widget), diff --git a/chrome/browser/ui/gtk/download/download_item_gtk.h b/chrome/browser/ui/gtk/download/download_item_gtk.h index bc0cfd3..b9be8a3 100644 --- a/chrome/browser/ui/gtk/download/download_item_gtk.h +++ b/chrome/browser/ui/gtk/download/download_item_gtk.h @@ -73,9 +73,6 @@ class DownloadItemGtk : public content::DownloadItem::Observer, private: friend class DownloadShelfContextMenuGtk; - // Returns true IFF the download is dangerous and unconfirmed. - bool IsDangerous(); - // Functions for controlling the progress animation. // Repaint the download progress. void UpdateDownloadProgress(); diff --git a/chrome/browser/ui/views/download/download_item_view.cc b/chrome/browser/ui/views/download/download_item_view.cc index 0f94520..2a41b5d 100644 --- a/chrome/browser/ui/views/download/download_item_view.cc +++ b/chrome/browser/ui/views/download/download_item_view.cc @@ -49,10 +49,6 @@ static const int kVerticalPadding = 3; // Pixels static const int kVerticalTextSpacer = 2; // Pixels static const int kVerticalTextPadding = 2; // Pixels -// The maximum number of characters we show in a file name when displaying the -// dangerous download message. -static const int kFileNameMaxLength = 20; - // We add some padding before the left image so that the progress animation icon // hides the corners of the left image. static const int kLeftPadding = 0; // Pixels. @@ -205,7 +201,7 @@ DownloadItemView::DownloadItemView(DownloadItem* download, UpdateDropDownButtonPosition(); - if (download->GetSafetyState() == DownloadItem::DANGEROUS) + if (model_->IsDangerous()) ShowWarningDialog(); UpdateAccessibleName(); @@ -255,12 +251,10 @@ void DownloadItemView::OnExtractIconComplete(IconManager::Handle handle, void DownloadItemView::OnDownloadUpdated(DownloadItem* download) { DCHECK(download == download_); - if (IsShowingWarningDialog() && - download->GetSafetyState() == DownloadItem::DANGEROUS_BUT_VALIDATED) { + if (IsShowingWarningDialog() && !model_->IsDangerous()) { // We have been approved. ClearWarningDialog(); - } else if (!IsShowingWarningDialog() && - download->GetSafetyState() == DownloadItem::DANGEROUS) { + } else if (!IsShowingWarningDialog() && model_->IsDangerous()) { ShowWarningDialog(); // Force the shelf to layout again as our size has changed. parent_->Layout(); @@ -573,7 +567,7 @@ void DownloadItemView::ShowContextMenu(const gfx::Point& p, void DownloadItemView::GetAccessibleState(ui::AccessibleViewState* state) { state->name = accessible_name_; state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON; - if (download_->GetSafetyState() == DownloadItem::DANGEROUS) { + if (model_->IsDangerous()) { state->state = ui::AccessibilityTypes::STATE_UNAVAILABLE; } else { state->state = ui::AccessibilityTypes::STATE_HASPOPUP; @@ -1004,24 +998,14 @@ void DownloadItemView::ClearWarningDialog() { void DownloadItemView::ShowWarningDialog() { DCHECK(mode_ != DANGEROUS_MODE && mode_ != MALICIOUS_MODE); - if (download_->GetDangerType() == - content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL || - download_->GetDangerType() == - content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT) { - mode_ = MALICIOUS_MODE; - } else { - DCHECK(download_->GetDangerType() == - content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); - mode_ = DANGEROUS_MODE; - } + mode_ = ((model_->IsMalicious()) ? MALICIOUS_MODE : DANGEROUS_MODE); + body_state_ = NORMAL; drop_down_state_ = NORMAL; tooltip_text_.clear(); if (mode_ == DANGEROUS_MODE) { - save_button_ = new views::NativeTextButton(this, - l10n_util::GetStringUTF16( - ChromeDownloadManagerDelegate::IsExtensionDownload(download_) ? - IDS_CONTINUE_EXTENSION_DOWNLOAD : IDS_CONFIRM_DOWNLOAD)); + save_button_ = new views::NativeTextButton( + this, model_->GetWarningConfirmButtonText()); save_button_->set_ignore_minimum_size(true); AddChildView(save_button_); } @@ -1030,74 +1014,16 @@ void DownloadItemView::ShowWarningDialog() { discard_button_->set_ignore_minimum_size(true); AddChildView(discard_button_); - // Ensure the file name is not too long. - - // Extract the file extension (if any). - FilePath filename(download_->GetTargetName()); -#if defined(OS_POSIX) - string16 extension = WideToUTF16(base::SysNativeMBToWide( - filename.Extension())); -#else - string16 extension = filename.Extension(); -#endif - - // Remove leading '.' - if (extension.length() > 0) - extension = extension.substr(1); -#if defined(OS_POSIX) - string16 rootname = WideToUTF16(base::SysNativeMBToWide( - filename.RemoveExtension().value())); -#else - string16 rootname = filename.RemoveExtension().value(); -#endif - - // Elide giant extensions (this shouldn't currently be hit, but might - // in future, should we ever notice unsafe giant extensions). - if (extension.length() > kFileNameMaxLength / 2) - ui::ElideString(extension, kFileNameMaxLength / 2, &extension); - ResourceBundle& rb = ResourceBundle::GetSharedInstance(); - // The dangerous download label text and icon are different - // under different cases. + // The dangerous download label text and icon are different under + // different cases. if (mode_ == MALICIOUS_MODE) { warning_icon_ = rb.GetBitmapNamed(IDR_SAFEBROWSING_WARNING); } else { - DCHECK(download_->GetDangerType() == - content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE); // The download file has dangerous file type (e.g.: an executable). warning_icon_ = rb.GetBitmapNamed(IDR_WARNING); } - string16 dangerous_label; - if (download_->GetDangerType() == - content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL) { - // Safebrowsing shows the download URL or content leads to malicious file. - dangerous_label = l10n_util::GetStringUTF16( - IDS_PROMPT_MALICIOUS_DOWNLOAD_URL); - } else if (download_->GetDangerType() == - content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE && - ChromeDownloadManagerDelegate::IsExtensionDownload(download_)) { - dangerous_label = - l10n_util::GetStringUTF16(IDS_PROMPT_DANGEROUS_DOWNLOAD_EXTENSION); - } else { - // The download file has dangerous file type (e.g.: an executable) or the - // file content is known to be malicious. - DCHECK(download_->GetDangerType() == - content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE || - download_->GetDangerType() == - content::DOWNLOAD_DANGER_TYPE_DANGEROUS_CONTENT); - ui::ElideString(rootname, - kFileNameMaxLength - extension.length(), - &rootname); - string16 filename = rootname + ASCIIToUTF16(".") + extension; - filename = base::i18n::GetDisplayStringInLTRDirectionality(filename); - dangerous_label = l10n_util::GetStringFUTF16( - download_->GetDangerType() == - content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE ? - IDS_PROMPT_DANGEROUS_DOWNLOAD : - IDS_PROMPT_MALICIOUS_DOWNLOAD_CONTENT, - filename); - } - + string16 dangerous_label = model_->GetWarningText(font_, kTextWidth); dangerous_download_label_ = new views::Label(dangerous_label); dangerous_download_label_->SetMultiLine(true); dangerous_download_label_->SetHorizontalAlignment(views::Label::ALIGN_LEFT); |