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

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: Kill some more code. 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) 2011 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"
(...skipping 23 matching lines...) Expand all
34 using content::DownloadItem; 34 using content::DownloadItem;
35 35
36 namespace { 36 namespace {
37 37
38 // NOTE: Mac currently doesn't use this like Windows does. Mac uses this to 38 // 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 39 // 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 40 // UX to fully spec all the the behaviors of download items and truncations
41 // rules so all platforms can get inline in the future. 41 // rules so all platforms can get inline in the future.
42 const int kTextWidth = 140; // Pixels 42 const int kTextWidth = 140; // Pixels
43 43
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. 44 // The maximum width in pixels for the file name tooltip.
49 const int kToolTipMaxWidth = 900; 45 const int kToolTipMaxWidth = 900;
50 46
51 47
52 // Helper to widen a view. 48 // Helper to widen a view.
53 void WidenView(NSView* view, CGFloat widthChange) { 49 void WidenView(NSView* view, CGFloat widthChange) {
54 // If it is an NSBox, the autoresize of the contentView is the issue. 50 // If it is an NSBox, the autoresize of the contentView is the issue.
55 NSView* contentView = view; 51 NSView* contentView = view;
56 if ([view isKindOfClass:[NSBox class]]) { 52 if ([view isKindOfClass:[NSBox class]]) {
57 contentView = [(NSBox*)view contentView]; 53 contentView = [(NSBox*)view contentView];
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 [buttonTweaker_ setFrameOrigin:frameOrigin]; 154 [buttonTweaker_ setFrameOrigin:frameOrigin];
159 155
160 bridge_->LoadIcon(); 156 bridge_->LoadIcon();
161 [self updateToolTip]; 157 [self updateToolTip];
162 } 158 }
163 159
164 - (void)setStateFromDownload:(BaseDownloadItemModel*)downloadModel { 160 - (void)setStateFromDownload:(BaseDownloadItemModel*)downloadModel {
165 DCHECK_EQ(bridge_->download_model(), downloadModel); 161 DCHECK_EQ(bridge_->download_model(), downloadModel);
166 162
167 // Handle dangerous downloads. 163 // Handle dangerous downloads.
168 if (downloadModel->download()->GetSafetyState() == DownloadItem::DANGEROUS) { 164 if (downloadModel->IsDangerous()) {
169 [self setState:kDangerous]; 165 [self setState:kDangerous];
170 166
171 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); 167 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
172 NSString* dangerousWarning; 168 NSString* dangerousWarning;
173 NSString* confirmButtonTitle; 169 NSString* confirmButtonTitle;
174 NSImage* alertIcon; 170 NSImage* alertIcon;
175 171
176 // The dangerous download label, button text and icon are different under 172 dangerousWarning =
177 // different cases. 173 base::SysUTF16ToNSString(downloadModel->GetWarningText(
178 if (downloadModel->download()->GetDangerType() == 174 gfx::Font(), kTextWidth));
179 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL) { 175 confirmButtonTitle =
180 // TODO(noelutz): add support for malicious content. 176 base::SysUTF16ToNSString(downloadModel->GetWarningConfirmButtonText());
181 // Safebrowsing shows the download URL leads to malicious file. 177 if (downloadModel->IsMalicious())
182 alertIcon = rb.GetNativeImageNamed(IDR_SAFEBROWSING_WARNING); 178 alertIcon = rb.GetNativeImageNamed(IDR_SAFEBROWSING_WARNING);
183 dangerousWarning = l10n_util::GetNSStringWithFixup( 179 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); 180 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);
Nico 2012/03/02 18:11:39 It looks like this previously didn't have to creat
asanka 2012/03/02 19:14:10 Looking at PlatformFontMac, I now see how this can
asanka 2012/03/05 17:49:25 Added a persistent font_ member that is used for t
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); 181 DCHECK(alertIcon);
232 [image_ setImage:alertIcon]; 182 [image_ setImage:alertIcon];
233 DCHECK(dangerousWarning); 183 DCHECK(dangerousWarning);
234 [dangerousDownloadLabel_ setStringValue:dangerousWarning]; 184 [dangerousDownloadLabel_ setStringValue:dangerousWarning];
235 DCHECK(confirmButtonTitle); 185 DCHECK(confirmButtonTitle);
236 [dangerousDownloadConfirmButton_ setTitle:confirmButtonTitle]; 186 [dangerousDownloadConfirmButton_ setTitle:confirmButtonTitle];
237 return; 187 return;
238 } 188 }
239 189
240 // Set correct popup menu. Also, set draggable download on completion. 190 // Set correct popup menu. Also, set draggable download on completion.
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 [sender setTitle:l10n_util::GetNSStringWithFixup( 366 [sender setTitle:l10n_util::GetNSStringWithFixup(
417 IDS_DOWNLOAD_MENU_PAUSE_ITEM)]; 367 IDS_DOWNLOAD_MENU_PAUSE_ITEM)];
418 } else { 368 } else {
419 [sender setTitle:l10n_util::GetNSStringWithFixup( 369 [sender setTitle:l10n_util::GetNSStringWithFixup(
420 IDS_DOWNLOAD_MENU_RESUME_ITEM)]; 370 IDS_DOWNLOAD_MENU_RESUME_ITEM)];
421 } 371 }
422 menuBridge_->ExecuteCommand(DownloadShelfContextMenuMac::TOGGLE_PAUSE); 372 menuBridge_->ExecuteCommand(DownloadShelfContextMenuMac::TOGGLE_PAUSE);
423 } 373 }
424 374
425 @end 375 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698