| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/cocoa/download_item_controller.h" | 5 #import "chrome/browser/cocoa/download_item_controller.h" |
| 6 | 6 |
| 7 #include "app/gfx/text_elider.h" |
| 8 #include "app/l10n_util_mac.h" |
| 7 #include "base/mac_util.h" | 9 #include "base/mac_util.h" |
| 10 #include "base/sys_string_conversions.h" |
| 8 #import "chrome/browser/cocoa/download_item_cell.h" | 11 #import "chrome/browser/cocoa/download_item_cell.h" |
| 9 #include "chrome/browser/cocoa/download_item_mac.h" | 12 #include "chrome/browser/cocoa/download_item_mac.h" |
| 13 #import "chrome/browser/cocoa/download_shelf_controller.h" |
| 10 #include "chrome/browser/download/download_item_model.h" | 14 #include "chrome/browser/download/download_item_model.h" |
| 11 #include "chrome/browser/download/download_shelf.h" | 15 #include "chrome/browser/download/download_shelf.h" |
| 12 #include "chrome/browser/download/download_util.h" | 16 #include "chrome/browser/download/download_util.h" |
| 17 #include "grit/generated_resources.h" |
| 13 | 18 |
| 19 static const int kTextWidth = 140; // Pixels |
| 14 | 20 |
| 15 // A class for the chromium-side part of the download shelf context menu. | 21 // A class for the chromium-side part of the download shelf context menu. |
| 16 | 22 |
| 17 class DownloadShelfContextMenuMac : public DownloadShelfContextMenu { | 23 class DownloadShelfContextMenuMac : public DownloadShelfContextMenu { |
| 18 public: | 24 public: |
| 19 DownloadShelfContextMenuMac(BaseDownloadItemModel* model) | 25 DownloadShelfContextMenuMac(BaseDownloadItemModel* model) |
| 20 : DownloadShelfContextMenu(model) { } | 26 : DownloadShelfContextMenu(model) { } |
| 21 | 27 |
| 22 using DownloadShelfContextMenu::ExecuteItemCommand; | 28 using DownloadShelfContextMenu::ExecuteItemCommand; |
| 23 using DownloadShelfContextMenu::ItemIsChecked; | 29 using DownloadShelfContextMenu::ItemIsChecked; |
| 24 using DownloadShelfContextMenu::IsItemCommandEnabled; | 30 using DownloadShelfContextMenu::IsItemCommandEnabled; |
| 25 | 31 |
| 26 using DownloadShelfContextMenu::SHOW_IN_FOLDER; | 32 using DownloadShelfContextMenu::SHOW_IN_FOLDER; |
| 27 using DownloadShelfContextMenu::OPEN_WHEN_COMPLETE; | 33 using DownloadShelfContextMenu::OPEN_WHEN_COMPLETE; |
| 28 using DownloadShelfContextMenu::ALWAYS_OPEN_TYPE; | 34 using DownloadShelfContextMenu::ALWAYS_OPEN_TYPE; |
| 29 using DownloadShelfContextMenu::CANCEL; | 35 using DownloadShelfContextMenu::CANCEL; |
| 30 using DownloadShelfContextMenu::REMOVE_ITEM; | 36 using DownloadShelfContextMenu::REMOVE_ITEM; |
| 31 }; | 37 }; |
| 32 | 38 |
| 39 @interface DownloadItemController (Private) |
| 40 - (void)setState:(DownoadItemState)state; |
| 41 @end |
| 33 | 42 |
| 34 // Implementation of DownloadItemController | 43 // Implementation of DownloadItemController |
| 35 | 44 |
| 36 @implementation DownloadItemController | 45 @implementation DownloadItemController |
| 37 | 46 |
| 38 - (id)initWithFrame:(NSRect)frameRect | 47 - (id)initWithModel:(BaseDownloadItemModel*)downloadModel |
| 39 model:(BaseDownloadItemModel*)downloadModel | |
| 40 shelf:(DownloadShelfController*)shelf { | 48 shelf:(DownloadShelfController*)shelf { |
| 41 if ((self = [super initWithNibName:@"DownloadItem" | 49 if ((self = [super initWithNibName:@"DownloadItem" |
| 42 bundle:mac_util::MainAppBundle()])) { | 50 bundle:mac_util::MainAppBundle()])) { |
| 43 // Must be called before [self view], so that bridge_ is set in awakeFromNib | 51 // Must be called before [self view], so that bridge_ is set in awakeFromNib |
| 44 bridge_.reset(new DownloadItemMac(downloadModel, self)); | 52 bridge_.reset(new DownloadItemMac(downloadModel, self)); |
| 45 menuBridge_.reset(new DownloadShelfContextMenuMac(downloadModel)); | 53 menuBridge_.reset(new DownloadShelfContextMenuMac(downloadModel)); |
| 46 | 54 |
| 47 shelf_ = shelf; | 55 shelf_ = shelf; |
| 48 | 56 state_ = kNormal; |
| 49 [[self view] setFrame:frameRect]; | 57 creationTime_ = base::Time::Now(); |
| 50 } | 58 } |
| 51 return self; | 59 return self; |
| 52 } | 60 } |
| 53 | 61 |
| 54 - (void)awakeFromNib { | 62 - (void)awakeFromNib { |
| 55 [self setStateFromDownload:bridge_->download_model()]; | 63 [self setStateFromDownload:bridge_->download_model()]; |
| 56 bridge_->LoadIcon(); | 64 bridge_->LoadIcon(); |
| 57 } | 65 } |
| 58 | 66 |
| 59 - (void)setStateFromDownload:(BaseDownloadItemModel*)downloadModel { | 67 - (void)setStateFromDownload:(BaseDownloadItemModel*)downloadModel { |
| 60 // TODO(thakis): handling of dangerous downloads -- crbug.com/14667 | 68 DCHECK_EQ(bridge_->download_model(), downloadModel); |
| 69 |
| 70 // Handle dangerous downloads. |
| 71 if (downloadModel->download()->safety_state() == DownloadItem::DANGEROUS) { |
| 72 [self setState:kDangerous]; |
| 73 |
| 74 // Set label. |
| 75 NSFont* font = [dangerousDownloadLabel_ font]; |
| 76 gfx::Font fontChr = gfx::Font::CreateFont( |
| 77 base::SysNSStringToWide([font fontName]), [font pointSize]); |
| 78 string16 elidedFilename = WideToUTF16(ElideFilename( |
| 79 downloadModel->download()->original_name(), fontChr, kTextWidth)); |
| 80 NSString* dangerousWarning = |
| 81 l10n_util::GetNSStringFWithFixup(IDS_PROMPT_DANGEROUS_DOWNLOAD, |
| 82 elidedFilename); |
| 83 [dangerousDownloadLabel_ setStringValue:dangerousWarning]; |
| 84 return; |
| 85 } |
| 61 | 86 |
| 62 // Set the correct popup menu. | 87 // Set the correct popup menu. |
| 63 if (downloadModel->download()->state() == DownloadItem::COMPLETE) | 88 if (downloadModel->download()->state() == DownloadItem::COMPLETE) |
| 64 currentMenu_ = completeDownloadMenu_; | 89 currentMenu_ = completeDownloadMenu_; |
| 65 else | 90 else |
| 66 currentMenu_ = activeDownloadMenu_; | 91 currentMenu_ = activeDownloadMenu_; |
| 67 | 92 |
| 68 [progressView_ setMenu:currentMenu_]; // for context menu | 93 [progressView_ setMenu:currentMenu_]; // for context menu |
| 69 [cell_ setStateFromDownload:downloadModel]; | 94 [cell_ setStateFromDownload:downloadModel]; |
| 70 } | 95 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 82 // TODO(thakis): Make this prettier, by fading the items out or overlaying | 107 // TODO(thakis): Make this prettier, by fading the items out or overlaying |
| 83 // the partial visible one with a horizontal alpha gradient -- crbug.com/17830 | 108 // the partial visible one with a horizontal alpha gradient -- crbug.com/17830 |
| 84 NSView* view = [self view]; | 109 NSView* view = [self view]; |
| 85 NSRect containerFrame = [[view superview] frame]; | 110 NSRect containerFrame = [[view superview] frame]; |
| 86 [view setHidden:(NSMaxX([view frame]) > NSWidth(containerFrame))]; | 111 [view setHidden:(NSMaxX([view frame]) > NSWidth(containerFrame))]; |
| 87 } | 112 } |
| 88 | 113 |
| 89 - (IBAction)handleButtonClick:(id)sender { | 114 - (IBAction)handleButtonClick:(id)sender { |
| 90 if ([cell_ isButtonPartPressed]) { | 115 if ([cell_ isButtonPartPressed]) { |
| 91 DownloadItem* download = bridge_->download_model()->download(); | 116 DownloadItem* download = bridge_->download_model()->download(); |
| 92 if (download->state() == DownloadItem::IN_PROGRESS) { | 117 if (download->state() == DownloadItem::IN_PROGRESS) |
| 93 download->set_open_when_complete(!download->open_when_complete()); | 118 download->set_open_when_complete(!download->open_when_complete()); |
| 94 } else if (download->state() == DownloadItem::COMPLETE) { | 119 else if (download->state() == DownloadItem::COMPLETE) |
| 95 download_util::OpenDownload(download); | 120 download_util::OpenDownload(download); |
| 96 } | |
| 97 } else { | 121 } else { |
| 98 // TODO(thakis): Align menu nicely with left view edge | |
| 99 [NSMenu popUpContextMenu:currentMenu_ | 122 [NSMenu popUpContextMenu:currentMenu_ |
| 100 withEvent:[NSApp currentEvent] | 123 withEvent:[NSApp currentEvent] |
| 101 forView:progressView_]; | 124 forView:progressView_]; |
| 102 } | 125 } |
| 103 } | 126 } |
| 104 | 127 |
| 128 - (NSSize)preferredSize { |
| 129 if (state_ == kNormal) |
| 130 return [progressView_ frame].size; |
| 131 DCHECK_EQ(kDangerous, state_); |
| 132 return [dangerousDownloadView_ frame].size; |
| 133 } |
| 134 |
| 135 - (void)clearDangerousMode { |
| 136 [self setState:kNormal]; |
| 137 } |
| 138 |
| 139 - (BOOL)isDangerousMode { |
| 140 return state_ == kDangerous; |
| 141 } |
| 142 |
| 143 - (void)setState:(DownoadItemState)state { |
| 144 if (state_ == state) |
| 145 return; |
| 146 state_ = state; |
| 147 if (state_ == kNormal) { |
| 148 [progressView_ setHidden:NO]; |
| 149 [dangerousDownloadView_ setHidden:YES]; |
| 150 } else { |
| 151 DCHECK_EQ(kDangerous, state_); |
| 152 [progressView_ setHidden:YES]; |
| 153 [dangerousDownloadView_ setHidden:NO]; |
| 154 } |
| 155 [shelf_ layoutItems]; |
| 156 } |
| 157 |
| 158 - (IBAction)saveDownload:(id)sender { |
| 159 // The user has confirmed a dangerous download. We record how quickly the |
| 160 // user did this to detect whether we're being clickjacked. |
| 161 UMA_HISTOGRAM_LONG_TIMES("clickjacking.save_download", |
| 162 base::Time::Now() - creationTime_); |
| 163 // This will change the state and notify us. |
| 164 bridge_->download_model()->download()->manager()->DangerousDownloadValidated( |
| 165 bridge_->download_model()->download()); |
| 166 } |
| 167 |
| 168 - (IBAction)discardDownload:(id)sender { |
| 169 UMA_HISTOGRAM_LONG_TIMES("clickjacking.discard_download", |
| 170 base::Time::Now() - creationTime_); |
| 171 if (bridge_->download_model()->download()->state() == |
| 172 DownloadItem::IN_PROGRESS) |
| 173 bridge_->download_model()->download()->Cancel(true); |
| 174 bridge_->download_model()->download()->Remove(true); |
| 175 // WARNING: we are deleted at this point. Don't access 'this'. |
| 176 } |
| 177 |
| 178 |
| 105 // Sets the enabled and checked state of a particular menu item for this | 179 // Sets the enabled and checked state of a particular menu item for this |
| 106 // download. We translate the NSMenuItem selection to menu selections understood | 180 // download. We translate the NSMenuItem selection to menu selections understood |
| 107 // by the non platform specific download context menu. | 181 // by the non platform specific download context menu. |
| 108 - (BOOL)validateMenuItem:(NSMenuItem *)item { | 182 - (BOOL)validateMenuItem:(NSMenuItem *)item { |
| 109 SEL action = [item action]; | 183 SEL action = [item action]; |
| 110 | 184 |
| 111 int actionId = 0; | 185 int actionId = 0; |
| 112 if (action == @selector(handleOpen:)) { | 186 if (action == @selector(handleOpen:)) { |
| 113 actionId = DownloadShelfContextMenuMac::OPEN_WHEN_COMPLETE; | 187 actionId = DownloadShelfContextMenuMac::OPEN_WHEN_COMPLETE; |
| 114 } else if (action == @selector(handleAlwaysOpen:)) { | 188 } else if (action == @selector(handleAlwaysOpen:)) { |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 | 222 |
| 149 - (IBAction)handleRemove:(id)sender { | 223 - (IBAction)handleRemove:(id)sender { |
| 150 menuBridge_->ExecuteItemCommand(DownloadShelfContextMenuMac::REMOVE_ITEM); | 224 menuBridge_->ExecuteItemCommand(DownloadShelfContextMenuMac::REMOVE_ITEM); |
| 151 } | 225 } |
| 152 | 226 |
| 153 - (IBAction)handleCancel:(id)sender { | 227 - (IBAction)handleCancel:(id)sender { |
| 154 menuBridge_->ExecuteItemCommand(DownloadShelfContextMenuMac::CANCEL); | 228 menuBridge_->ExecuteItemCommand(DownloadShelfContextMenuMac::CANCEL); |
| 155 } | 229 } |
| 156 | 230 |
| 157 @end | 231 @end |
| OLD | NEW |