Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "chrome/browser/ui/cocoa/download/download_item_controller.h" | 5 #import "chrome/browser/ui/cocoa/download/download_item_controller.h" |
| 6 | 6 |
| 7 #include "base/mac/bundle_locations.h" | 7 #include "base/mac/bundle_locations.h" |
| 8 #include "base/mac/mac_util.h" | 8 #include "base/mac/mac_util.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/strings/sys_string_conversions.h" | 12 #include "base/strings/sys_string_conversions.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/browser/download/chrome_download_manager_delegate.h" | 14 #include "chrome/browser/download/chrome_download_manager_delegate.h" |
| 15 #include "chrome/browser/download/download_item_model.h" | 15 #include "chrome/browser/download/download_item_model.h" |
| 16 #include "chrome/browser/download/download_shelf_context_menu.h" | 16 #include "chrome/browser/download/download_shelf_context_menu.h" |
| 17 #import "chrome/browser/themes/theme_properties.h" | 17 #import "chrome/browser/themes/theme_properties.h" |
| 18 #import "chrome/browser/themes/theme_service.h" | 18 #import "chrome/browser/themes/theme_service.h" |
| 19 #import "chrome/browser/ui/cocoa/download/download_item_button.h" | 19 #import "chrome/browser/ui/cocoa/download/download_item_button.h" |
| 20 #import "chrome/browser/ui/cocoa/download/download_item_cell.h" | 20 #import "chrome/browser/ui/cocoa/download/download_item_cell.h" |
| 21 #include "chrome/browser/ui/cocoa/download/download_item_mac.h" | 21 #include "chrome/browser/ui/cocoa/download/download_item_mac.h" |
| 22 #import "chrome/browser/ui/cocoa/download/download_shelf_context_menu_controller .h" | |
| 22 #import "chrome/browser/ui/cocoa/download/download_shelf_controller.h" | 23 #import "chrome/browser/ui/cocoa/download/download_shelf_controller.h" |
| 23 #import "chrome/browser/ui/cocoa/themed_window.h" | 24 #import "chrome/browser/ui/cocoa/themed_window.h" |
| 24 #import "chrome/browser/ui/cocoa/ui_localizer.h" | 25 #import "chrome/browser/ui/cocoa/ui_localizer.h" |
| 25 #include "content/public/browser/download_item.h" | 26 #include "content/public/browser/download_item.h" |
| 26 #include "content/public/browser/page_navigator.h" | 27 #include "content/public/browser/page_navigator.h" |
| 27 #include "grit/generated_resources.h" | 28 #include "grit/generated_resources.h" |
| 28 #include "grit/theme_resources.h" | 29 #include "grit/theme_resources.h" |
| 29 #include "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" | 30 #include "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" |
| 30 #include "ui/base/l10n/l10n_util_mac.h" | 31 #include "ui/base/l10n/l10n_util_mac.h" |
| 31 #include "ui/base/resource/resource_bundle.h" | 32 #include "ui/base/resource/resource_bundle.h" |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 118 - (void)dealloc { | 119 - (void)dealloc { |
| 119 [[NSNotificationCenter defaultCenter] removeObserver:self]; | 120 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 120 [progressView_ setController:nil]; | 121 [progressView_ setController:nil]; |
| 121 [[self view] removeFromSuperview]; | 122 [[self view] removeFromSuperview]; |
| 122 [super dealloc]; | 123 [super dealloc]; |
| 123 } | 124 } |
| 124 | 125 |
| 125 - (void)awakeFromNib { | 126 - (void)awakeFromNib { |
| 126 [progressView_ setController:self]; | 127 [progressView_ setController:self]; |
| 127 | 128 |
| 128 [self setStateFromDownload:bridge_->download_model()]; | |
| 129 | |
| 130 GTMUILocalizerAndLayoutTweaker* localizerAndLayoutTweaker = | 129 GTMUILocalizerAndLayoutTweaker* localizerAndLayoutTweaker = |
| 131 [[[GTMUILocalizerAndLayoutTweaker alloc] init] autorelease]; | 130 [[[GTMUILocalizerAndLayoutTweaker alloc] init] autorelease]; |
| 132 [localizerAndLayoutTweaker applyLocalizer:localizer_ tweakingUI:[self view]]; | 131 [localizerAndLayoutTweaker applyLocalizer:localizer_ tweakingUI:[self view]]; |
| 133 | 132 |
| 134 // The strings are based on the download item's name, sizing tweaks have to be | 133 [self setStateFromDownload:bridge_->download_model()]; |
| 135 // manually done. | |
| 136 DCHECK(buttonTweaker_ != nil); | |
| 137 CGFloat widthChange = [buttonTweaker_ changedWidth]; | |
| 138 // If it's a dangerous download, size the two lines so the text/filename | |
| 139 // is always visible. | |
| 140 if ([self isDangerousMode]) { | |
| 141 widthChange += | |
| 142 [GTMUILocalizerAndLayoutTweaker | |
| 143 sizeToFitFixedHeightTextField:dangerousDownloadLabel_ | |
| 144 minWidth:kTextWidth]; | |
| 145 } | |
| 146 // Grow the parent views | |
| 147 WidenView([self view], widthChange); | |
| 148 WidenView(dangerousDownloadView_, widthChange); | |
| 149 // Slide the two buttons over. | |
| 150 NSPoint frameOrigin = [buttonTweaker_ frame].origin; | |
| 151 frameOrigin.x += widthChange; | |
| 152 [buttonTweaker_ setFrameOrigin:frameOrigin]; | |
| 153 | 134 |
| 154 bridge_->LoadIcon(); | 135 bridge_->LoadIcon(); |
| 155 [self updateToolTip]; | 136 [self updateToolTip]; |
| 156 } | 137 } |
| 157 | 138 |
| 139 - (void)showDangerousWarning:(DownloadItemModel*)downloadModel { | |
| 140 // The transition from safe -> dangerous should only happen once. The code | |
| 141 // assumes that the danger type of the download doesn't change once it's set. | |
| 142 if ([self isDangerousMode]) | |
| 143 return; | |
| 144 | |
| 145 [self setState:kDangerous]; | |
| 146 | |
| 147 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 148 NSImage* alertIcon; | |
| 149 | |
| 150 NSString* dangerousWarning = base::SysUTF16ToNSString( | |
| 151 downloadModel->GetWarningText(*font_list_, kTextWidth)); | |
| 152 DCHECK(dangerousWarning); | |
| 153 [dangerousDownloadLabel_ setStringValue:dangerousWarning]; | |
| 154 CGFloat labelWidthChange = | |
| 155 [GTMUILocalizerAndLayoutTweaker | |
| 156 sizeToFitFixedHeightTextField:dangerousDownloadLabel_ | |
| 157 minWidth:kTextWidth]; | |
| 158 CGFloat buttonWidthChange = 0.0; | |
| 159 | |
| 160 if (downloadModel->MightBeMalicious()) { | |
| 161 alertIcon = rb.GetNativeImageNamed(IDR_SAFEBROWSING_WARNING).ToNSImage(); | |
| 162 buttonWidthChange = [maliciousButtonTweaker_ changedWidth]; | |
| 163 | |
| 164 // Move the buttons to account for the change in label size. | |
| 165 NSPoint frameOrigin = [maliciousButtonTweaker_ frame].origin; | |
| 166 frameOrigin.x += labelWidthChange; | |
| 167 [maliciousButtonTweaker_ setFrameOrigin:frameOrigin]; | |
| 168 | |
| 169 [dangerousButtonTweaker_ setHidden:YES]; | |
| 170 [maliciousButtonTweaker_ setHidden:NO]; | |
| 171 } else { | |
| 172 alertIcon = rb.GetNativeImageNamed(IDR_WARNING).ToNSImage(); | |
| 173 buttonWidthChange = [dangerousButtonTweaker_ changedWidth]; | |
| 174 | |
| 175 // The text on the confirm button can change depending on the type of the | |
| 176 // download. | |
| 177 NSString* confirmButtonTitle = | |
| 178 base::SysUTF16ToNSString(downloadModel->GetWarningConfirmButtonText()); | |
| 179 DCHECK(confirmButtonTitle); | |
| 180 [dangerousDownloadConfirmButton_ setTitle:confirmButtonTitle]; | |
| 181 | |
| 182 // Move the button to account for the change in label size. | |
| 183 NSPoint frameOrigin = [dangerousButtonTweaker_ frame].origin; | |
| 184 frameOrigin.x += labelWidthChange; | |
| 185 [dangerousButtonTweaker_ setFrameOrigin:frameOrigin]; | |
| 186 | |
| 187 [dangerousButtonTweaker_ setHidden:NO]; | |
| 188 [maliciousButtonTweaker_ setHidden:YES]; | |
| 189 } | |
| 190 DCHECK(alertIcon); | |
| 191 [image_ setImage:alertIcon]; | |
| 192 | |
| 193 // Grow the parent views | |
| 194 WidenView([self view], labelWidthChange + buttonWidthChange); | |
| 195 WidenView(dangerousDownloadView_, labelWidthChange + buttonWidthChange); | |
| 196 } | |
| 197 | |
| 158 - (void)setStateFromDownload:(DownloadItemModel*)downloadModel { | 198 - (void)setStateFromDownload:(DownloadItemModel*)downloadModel { |
| 159 DCHECK_EQ([self download], downloadModel->download()); | 199 DCHECK_EQ([self download], downloadModel->download()); |
| 160 | 200 |
| 161 // Handle dangerous downloads. | 201 // Handle dangerous downloads. |
| 162 if (downloadModel->IsDangerous()) { | 202 if (downloadModel->IsDangerous()) { |
| 163 [self setState:kDangerous]; | 203 [self showDangerousWarning:downloadModel]; |
| 164 | |
| 165 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
| 166 NSString* dangerousWarning; | |
| 167 NSString* confirmButtonTitle; | |
| 168 NSImage* alertIcon; | |
| 169 | |
| 170 dangerousWarning = | |
| 171 base::SysUTF16ToNSString(downloadModel->GetWarningText( | |
| 172 *font_list_, kTextWidth)); | |
| 173 confirmButtonTitle = | |
| 174 base::SysUTF16ToNSString(downloadModel->GetWarningConfirmButtonText()); | |
| 175 if (downloadModel->MightBeMalicious()) | |
| 176 alertIcon = rb.GetNativeImageNamed(IDR_SAFEBROWSING_WARNING).ToNSImage(); | |
| 177 else | |
| 178 alertIcon = rb.GetNativeImageNamed(IDR_WARNING).ToNSImage(); | |
| 179 DCHECK(alertIcon); | |
| 180 [image_ setImage:alertIcon]; | |
| 181 DCHECK(dangerousWarning); | |
| 182 [dangerousDownloadLabel_ setStringValue:dangerousWarning]; | |
| 183 DCHECK(confirmButtonTitle); | |
| 184 [dangerousDownloadConfirmButton_ setTitle:confirmButtonTitle]; | |
| 185 return; | 204 return; |
| 186 } | 205 } |
| 187 | 206 |
| 188 // Set path to draggable download on completion. | 207 // Set path to draggable download on completion. |
| 189 if (downloadModel->download()->GetState() == DownloadItem::COMPLETE) | 208 if (downloadModel->download()->GetState() == DownloadItem::COMPLETE) |
| 190 [progressView_ setDownload:downloadModel->download()->GetTargetFilePath()]; | 209 [progressView_ setDownload:downloadModel->download()->GetTargetFilePath()]; |
| 191 | 210 |
| 192 [cell_ setStateFromDownload:downloadModel]; | 211 [cell_ setStateFromDownload:downloadModel]; |
| 193 } | 212 } |
| 194 | 213 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 300 } | 319 } |
| 301 | 320 |
| 302 - (IBAction)discardDownload:(id)sender { | 321 - (IBAction)discardDownload:(id)sender { |
| 303 UMA_HISTOGRAM_LONG_TIMES("clickjacking.discard_download", | 322 UMA_HISTOGRAM_LONG_TIMES("clickjacking.discard_download", |
| 304 base::Time::Now() - creationTime_); | 323 base::Time::Now() - creationTime_); |
| 305 DownloadItem* download = bridge_->download_model()->download(); | 324 DownloadItem* download = bridge_->download_model()->download(); |
| 306 download->Remove(); | 325 download->Remove(); |
| 307 // WARNING: we are deleted at this point. Don't access 'this'. | 326 // WARNING: we are deleted at this point. Don't access 'this'. |
| 308 } | 327 } |
| 309 | 328 |
| 329 - (IBAction)dismissMaliciousDownload:(id)sender { | |
| 330 [self remove]; | |
| 331 // WARNING: we are deleted at this point. | |
| 332 } | |
| 333 | |
| 334 - (IBAction)showContextMenu:(id)sender { | |
| 335 base::scoped_nsobject<DownloadShelfContextMenuController> menuController( | |
| 336 [[DownloadShelfContextMenuController alloc] | |
| 337 initWithItemController:self | |
| 338 withDelegate:nil]); | |
| 339 [NSMenu popUpContextMenu:[menuController menu] | |
| 340 withEvent:[NSApp currentEvent] | |
| 341 forView:[self view]]; | |
|
Nico
2013/11/02 00:13:51
Does "forView:sender" make a visual difference? If
asanka
2013/11/02 05:41:36
The behavior with "[self view]" appeared correct.
asanka
2013/11/04 16:53:47
Tried both ways, and there was no difference.
| |
| 342 } | |
| 343 | |
| 310 @end | 344 @end |
| OLD | NEW |