Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Side by Side Diff: chrome/browser/ui/cocoa/download/download_item_controller.mm

Issue 9569011: Refactor dangerous download warning text generation into DownloadItemModel. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Update copyright headers Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 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/string16.h" 10 #include "base/string16.h"
11 #include "base/string_util.h" 11 #include "base/string_util.h"
(...skipping 10 matching lines...) Expand all
22 #import "chrome/browser/ui/cocoa/download/download_shelf_controller.h" 22 #import "chrome/browser/ui/cocoa/download/download_shelf_controller.h"
23 #import "chrome/browser/ui/cocoa/themed_window.h" 23 #import "chrome/browser/ui/cocoa/themed_window.h"
24 #import "chrome/browser/ui/cocoa/ui_localizer.h" 24 #import "chrome/browser/ui/cocoa/ui_localizer.h"
25 #include "content/public/browser/download_item.h" 25 #include "content/public/browser/download_item.h"
26 #include "grit/generated_resources.h" 26 #include "grit/generated_resources.h"
27 #include "grit/theme_resources.h" 27 #include "grit/theme_resources.h"
28 #include "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h" 28 #include "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h"
29 #include "ui/base/l10n/l10n_util_mac.h" 29 #include "ui/base/l10n/l10n_util_mac.h"
30 #include "ui/base/resource/resource_bundle.h" 30 #include "ui/base/resource/resource_bundle.h"
31 #include "ui/base/text/text_elider.h" 31 #include "ui/base/text/text_elider.h"
32 #include "ui/gfx/font.h"
32 #include "ui/gfx/image/image.h" 33 #include "ui/gfx/image/image.h"
33 34
34 using content::DownloadItem; 35 using content::DownloadItem;
35 36
36 namespace { 37 namespace {
37 38
38 // NOTE: Mac currently doesn't use this like Windows does. Mac uses this to 39 // NOTE: Mac currently doesn't use this like Windows does. Mac uses this to
39 // control the min size on the dangerous download text. TVL sent a query off to 40 // control the min size on the dangerous download text. TVL sent a query off to
40 // UX to fully spec all the the behaviors of download items and truncations 41 // UX to fully spec all the the behaviors of download items and truncations
41 // rules so all platforms can get inline in the future. 42 // rules so all platforms can get inline in the future.
42 const int kTextWidth = 140; // Pixels 43 const int kTextWidth = 140; // Pixels
43 44
44 // The maximum number of characters we show in a file name when displaying the
45 // dangerous download message.
46 const int kFileNameMaxLength = 20;
47
48 // The maximum width in pixels for the file name tooltip. 45 // The maximum width in pixels for the file name tooltip.
49 const int kToolTipMaxWidth = 900; 46 const int kToolTipMaxWidth = 900;
50 47
51 48
52 // Helper to widen a view. 49 // Helper to widen a view.
53 void WidenView(NSView* view, CGFloat widthChange) { 50 void WidenView(NSView* view, CGFloat widthChange) {
54 // If it is an NSBox, the autoresize of the contentView is the issue. 51 // If it is an NSBox, the autoresize of the contentView is the issue.
55 NSView* contentView = view; 52 NSView* contentView = view;
56 if ([view isKindOfClass:[NSBox class]]) { 53 if ([view isKindOfClass:[NSBox class]]) {
57 contentView = [(NSBox*)view contentView]; 54 contentView = [(NSBox*)view contentView];
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 107
111 NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter]; 108 NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
112 [defaultCenter addObserver:self 109 [defaultCenter addObserver:self
113 selector:@selector(themeDidChangeNotification:) 110 selector:@selector(themeDidChangeNotification:)
114 name:kBrowserThemeDidChangeNotification 111 name:kBrowserThemeDidChangeNotification
115 object:nil]; 112 object:nil];
116 113
117 shelf_ = shelf; 114 shelf_ = shelf;
118 state_ = kNormal; 115 state_ = kNormal;
119 creationTime_ = base::Time::Now(); 116 creationTime_ = base::Time::Now();
117 font_.reset(new gfx::Font());
120 } 118 }
121 return self; 119 return self;
122 } 120 }
123 121
124 - (void)dealloc { 122 - (void)dealloc {
125 [[NSNotificationCenter defaultCenter] removeObserver:self]; 123 [[NSNotificationCenter defaultCenter] removeObserver:self];
126 [progressView_ setController:nil]; 124 [progressView_ setController:nil];
127 [[self view] removeFromSuperview]; 125 [[self view] removeFromSuperview];
128 [super dealloc]; 126 [super dealloc];
129 } 127 }
(...skipping 28 matching lines...) Expand all
158 [buttonTweaker_ setFrameOrigin:frameOrigin]; 156 [buttonTweaker_ setFrameOrigin:frameOrigin];
159 157
160 bridge_->LoadIcon(); 158 bridge_->LoadIcon();
161 [self updateToolTip]; 159 [self updateToolTip];
162 } 160 }
163 161
164 - (void)setStateFromDownload:(BaseDownloadItemModel*)downloadModel { 162 - (void)setStateFromDownload:(BaseDownloadItemModel*)downloadModel {
165 DCHECK_EQ(bridge_->download_model(), downloadModel); 163 DCHECK_EQ(bridge_->download_model(), downloadModel);
166 164
167 // Handle dangerous downloads. 165 // Handle dangerous downloads.
168 if (downloadModel->download()->GetSafetyState() == DownloadItem::DANGEROUS) { 166 if (downloadModel->IsDangerous()) {
169 [self setState:kDangerous]; 167 [self setState:kDangerous];
170 168
171 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 169 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
172 NSString* dangerousWarning; 170 NSString* dangerousWarning;
173 NSString* confirmButtonTitle; 171 NSString* confirmButtonTitle;
174 NSImage* alertIcon; 172 NSImage* alertIcon;
175 173
176 // The dangerous download label, button text and icon are different under 174 dangerousWarning =
177 // different cases. 175 base::SysUTF16ToNSString(downloadModel->GetWarningText(
178 if (downloadModel->download()->GetDangerType() == 176 *font_, kTextWidth));
179 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL) { 177 confirmButtonTitle =
180 // TODO(noelutz): add support for malicious content. 178 base::SysUTF16ToNSString(downloadModel->GetWarningConfirmButtonText());
181 // Safebrowsing shows the download URL leads to malicious file. 179 if (downloadModel->IsMalicious())
182 alertIcon = rb.GetNativeImageNamed(IDR_SAFEBROWSING_WARNING); 180 alertIcon = rb.GetNativeImageNamed(IDR_SAFEBROWSING_WARNING);
183 dangerousWarning = l10n_util::GetNSStringWithFixup( 181 else
184 IDS_PROMPT_MALICIOUS_DOWNLOAD_URL);
185 confirmButtonTitle = l10n_util::GetNSStringWithFixup(
186 IDS_CONFIRM_DOWNLOAD);
187 } else {
188 // It's a dangerous file type (e.g.: an executable).
189 DCHECK_EQ(downloadModel->download()->GetDangerType(),
190 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE);
191 alertIcon = rb.GetNativeImageNamed(IDR_WARNING); 182 alertIcon = rb.GetNativeImageNamed(IDR_WARNING);
192 if (ChromeDownloadManagerDelegate::IsExtensionDownload(
193 downloadModel->download())) {
194 dangerousWarning = l10n_util::GetNSStringWithFixup(
195 IDS_PROMPT_DANGEROUS_DOWNLOAD_EXTENSION);
196 confirmButtonTitle = l10n_util::GetNSStringWithFixup(
197 IDS_CONTINUE_EXTENSION_DOWNLOAD);
198 } else {
199 // This basic fixup copies Windows DownloadItemView::DownloadItemView().
200
201 // Extract the file extension (if any).
202 FilePath filename(downloadModel->download()->GetTargetName());
203 FilePath::StringType extension = filename.Extension();
204
205 // Remove leading '.' from the extension
206 if (extension.length() > 0)
207 extension = extension.substr(1);
208
209 // Elide giant extensions.
210 if (extension.length() > kFileNameMaxLength / 2) {
211 string16 utf16_extension;
212 ui::ElideString(UTF8ToUTF16(extension), kFileNameMaxLength / 2,
213 &utf16_extension);
214 extension = UTF16ToUTF8(utf16_extension);
215 }
216
217 // Rebuild the filename.extension.
218 string16 rootname = UTF8ToUTF16(filename.RemoveExtension().value());
219 ui::ElideString(rootname, kFileNameMaxLength - extension.length(),
220 &rootname);
221 std::string new_filename = UTF16ToUTF8(rootname);
222 if (extension.length())
223 new_filename += std::string(".") + extension;
224
225 dangerousWarning = l10n_util::GetNSStringFWithFixup(
226 IDS_PROMPT_DANGEROUS_DOWNLOAD, UTF8ToUTF16(new_filename));
227 confirmButtonTitle =
228 l10n_util::GetNSStringWithFixup(IDS_CONFIRM_DOWNLOAD);
229 }
230 }
231 DCHECK(alertIcon); 183 DCHECK(alertIcon);
232 [image_ setImage:alertIcon]; 184 [image_ setImage:alertIcon];
233 DCHECK(dangerousWarning); 185 DCHECK(dangerousWarning);
234 [dangerousDownloadLabel_ setStringValue:dangerousWarning]; 186 [dangerousDownloadLabel_ setStringValue:dangerousWarning];
235 DCHECK(confirmButtonTitle); 187 DCHECK(confirmButtonTitle);
236 [dangerousDownloadConfirmButton_ setTitle:confirmButtonTitle]; 188 [dangerousDownloadConfirmButton_ setTitle:confirmButtonTitle];
237 return; 189 return;
238 } 190 }
239 191
240 // Set correct popup menu. Also, set draggable download on completion. 192 // Set correct popup menu. Also, set draggable download on completion.
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 DCHECK_EQ(kDangerous, state_); 239 DCHECK_EQ(kDangerous, state_);
288 return [dangerousDownloadView_ frame].size; 240 return [dangerousDownloadView_ frame].size;
289 } 241 }
290 242
291 - (DownloadItem*)download { 243 - (DownloadItem*)download {
292 return bridge_->download_model()->download(); 244 return bridge_->download_model()->download();
293 } 245 }
294 246
295 - (void)updateToolTip { 247 - (void)updateToolTip {
296 string16 elidedFilename = ui::ElideFilename( 248 string16 elidedFilename = ui::ElideFilename(
297 [self download]->GetFileNameToReportUser(), 249 [self download]->GetFileNameToReportUser(), *font_, kToolTipMaxWidth);
298 gfx::Font(), kToolTipMaxWidth);
299 [progressView_ setToolTip:base::SysUTF16ToNSString(elidedFilename)]; 250 [progressView_ setToolTip:base::SysUTF16ToNSString(elidedFilename)];
300 } 251 }
301 252
302 - (void)clearDangerousMode { 253 - (void)clearDangerousMode {
303 [self setState:kNormal]; 254 [self setState:kNormal];
304 // The state change hide the dangerouse download view and is now showing the 255 // The state change hide the dangerouse download view and is now showing the
305 // download progress view. This means the view is likely to be a different 256 // download progress view. This means the view is likely to be a different
306 // size, so trigger a shelf layout to fix up spacing. 257 // size, so trigger a shelf layout to fix up spacing.
307 [shelf_ layoutItems]; 258 [shelf_ layoutItems];
308 } 259 }
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 [sender setTitle:l10n_util::GetNSStringWithFixup( 367 [sender setTitle:l10n_util::GetNSStringWithFixup(
417 IDS_DOWNLOAD_MENU_PAUSE_ITEM)]; 368 IDS_DOWNLOAD_MENU_PAUSE_ITEM)];
418 } else { 369 } else {
419 [sender setTitle:l10n_util::GetNSStringWithFixup( 370 [sender setTitle:l10n_util::GetNSStringWithFixup(
420 IDS_DOWNLOAD_MENU_RESUME_ITEM)]; 371 IDS_DOWNLOAD_MENU_RESUME_ITEM)];
421 } 372 }
422 menuBridge_->ExecuteCommand(DownloadShelfContextMenuMac::TOGGLE_PAUSE); 373 menuBridge_->ExecuteCommand(DownloadShelfContextMenuMac::TOGGLE_PAUSE);
423 } 374 }
424 375
425 @end 376 @end
OLDNEW
« no previous file with comments | « chrome/browser/ui/cocoa/download/download_item_controller.h ('k') | chrome/browser/ui/cocoa/download/download_item_mac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698