1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
|
// Copyright (c) 2009 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.
#import "chrome/browser/cocoa/download_item_controller.h"
#include "app/gfx/text_elider.h"
#include "app/l10n_util_mac.h"
#include "app/resource_bundle.h"
#include "base/mac_util.h"
#include "base/sys_string_conversions.h"
#import "chrome/browser/cocoa/download_item_cell.h"
#include "chrome/browser/cocoa/download_item_mac.h"
#import "chrome/browser/cocoa/download_shelf_controller.h"
#import "chrome/browser/cocoa/ui_localizer.h"
#include "chrome/browser/download/download_item_model.h"
#include "chrome/browser/download/download_shelf.h"
#include "chrome/browser/download/download_util.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#import "third_party/GTM/AppKit/GTMTheme.h"
#include "third_party/GTM/AppKit/GTMUILocalizerAndLayoutTweaker.h"
static const int kTextWidth = 140; // Pixels
namespace {
// Helper to widen a view.
void WidenView(NSView* view, CGFloat widthChange) {
// If it is an NSBox, the autoresize of the contentView is the issue.
NSView* contentView = view;
if ([view isKindOfClass:[NSBox class]]) {
contentView = [(NSBox*)view contentView];
}
BOOL autoresizesSubviews = [contentView autoresizesSubviews];
if (autoresizesSubviews) {
[contentView setAutoresizesSubviews:NO];
}
NSRect frame = [view frame];
frame.size.width += widthChange;
[view setFrame:frame];
if (autoresizesSubviews) {
[contentView setAutoresizesSubviews:YES];
}
}
} // namespace
// A class for the chromium-side part of the download shelf context menu.
class DownloadShelfContextMenuMac : public DownloadShelfContextMenu {
public:
DownloadShelfContextMenuMac(BaseDownloadItemModel* model)
: DownloadShelfContextMenu(model) { }
using DownloadShelfContextMenu::ExecuteItemCommand;
using DownloadShelfContextMenu::ItemIsChecked;
using DownloadShelfContextMenu::IsItemCommandEnabled;
using DownloadShelfContextMenu::SHOW_IN_FOLDER;
using DownloadShelfContextMenu::OPEN_WHEN_COMPLETE;
using DownloadShelfContextMenu::ALWAYS_OPEN_TYPE;
using DownloadShelfContextMenu::CANCEL;
};
@interface DownloadItemController (Private)
- (void)themeDidChangeNotification:(NSNotification*)aNotification;
- (void)updateTheme:(GTMTheme*)theme;
- (void)setState:(DownoadItemState)state;
@end
// Implementation of DownloadItemController
@implementation DownloadItemController
- (id)initWithModel:(BaseDownloadItemModel*)downloadModel
shelf:(DownloadShelfController*)shelf {
if ((self = [super initWithNibName:@"DownloadItem"
bundle:mac_util::MainAppBundle()])) {
// Must be called before [self view], so that bridge_ is set in awakeFromNib
bridge_.reset(new DownloadItemMac(downloadModel, self));
menuBridge_.reset(new DownloadShelfContextMenuMac(downloadModel));
NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self
selector:@selector(themeDidChangeNotification:)
name:kGTMThemeDidChangeNotification
object:nil];
shelf_ = shelf;
state_ = kNormal;
creationTime_ = base::Time::Now();
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[self view] removeFromSuperview];
[super dealloc];
}
- (void)awakeFromNib {
[self setStateFromDownload:bridge_->download_model()];
GTMUILocalizerAndLayoutTweaker* localizerAndLayoutTweaker =
[[[GTMUILocalizerAndLayoutTweaker alloc] init] autorelease];
[localizerAndLayoutTweaker applyLocalizer:localizer_ tweakingUI:[self view]];
// Since the shelf keeps laying out views as more items are added, relying on
// the WidthBaseTweaker to resize the dangerous download part does not work.
DCHECK(buttonTweaker_ != nil);
CGFloat widthChange = [buttonTweaker_ changedWidth];
// Grow the parent views
WidenView([self view], widthChange);
WidenView(dangerousDownloadView_, widthChange);
// Slide the two buttons over.
NSPoint frameOrigin = [buttonTweaker_ frame].origin;
frameOrigin.x += widthChange;
[buttonTweaker_ setFrameOrigin:frameOrigin];
ResourceBundle& rb = ResourceBundle::GetSharedInstance();
NSImage* alertIcon = rb.GetNSImageNamed(IDR_WARNING);
DCHECK(alertIcon);
[image_ setImage:alertIcon];
bridge_->LoadIcon();
}
- (void)setStateFromDownload:(BaseDownloadItemModel*)downloadModel {
DCHECK_EQ(bridge_->download_model(), downloadModel);
// Handle dangerous downloads.
if (downloadModel->download()->safety_state() == DownloadItem::DANGEROUS) {
[self setState:kDangerous];
NSString* dangerousWarning;
NSString* confirmButtonTitle;
// The dangerous download label and button text are different for an
// extension file.
if (DownloadManager::IsExtensionInstall(downloadModel->download())) {
dangerousWarning = l10n_util::GetNSStringWithFixup(
IDS_PROMPT_DANGEROUS_DOWNLOAD_EXTENSION);
confirmButtonTitle = l10n_util::GetNSStringWithFixup(
IDS_CONTINUE_EXTENSION_DOWNLOAD);
} else {
NSFont* font = [dangerousDownloadLabel_ font];
gfx::Font chromeFont = gfx::Font::CreateFont(
base::SysNSStringToWide([font fontName]), [font pointSize]);
string16 elidedFilename = WideToUTF16(ElideFilename(
downloadModel->download()->original_name(), chromeFont, kTextWidth));
dangerousWarning = l10n_util::GetNSStringFWithFixup(
IDS_PROMPT_DANGEROUS_DOWNLOAD, elidedFilename);
confirmButtonTitle = l10n_util::GetNSStringWithFixup(IDS_SAVE_DOWNLOAD);
}
[dangerousDownloadLabel_ setStringValue:dangerousWarning];
[dangerousDownloadConfirmButton_ setTitle:confirmButtonTitle];
return;
}
// Set the correct popup menu.
if (downloadModel->download()->state() == DownloadItem::COMPLETE)
currentMenu_ = completeDownloadMenu_;
else
currentMenu_ = activeDownloadMenu_;
[progressView_ setMenu:currentMenu_]; // for context menu
[cell_ setStateFromDownload:downloadModel];
}
- (void)setIcon:(NSImage*)icon {
[cell_ setImage:icon];
}
- (void)remove {
// We are deleted after this!
[shelf_ remove:self];
}
- (void)updateVisibility:(id)sender {
if ([[self view] window])
[self updateTheme:[[self view] gtm_theme]];
// TODO(thakis): Make this prettier, by fading the items out or overlaying
// the partial visible one with a horizontal alpha gradient -- crbug.com/17830
NSView* view = [self view];
NSRect containerFrame = [[view superview] frame];
[view setHidden:(NSMaxX([view frame]) > NSWidth(containerFrame))];
}
- (IBAction)handleButtonClick:(id)sender {
if ([cell_ isButtonPartPressed]) {
DownloadItem* download = bridge_->download_model()->download();
if (download->state() == DownloadItem::IN_PROGRESS)
download->set_open_when_complete(!download->open_when_complete());
else if (download->state() == DownloadItem::COMPLETE)
download_util::OpenDownload(download);
} else {
// Hold a reference to ourselves in case the download completes and we
// represent a file that's auto-removed (e.g. a theme).
scoped_nsobject<DownloadItemController> ref([self retain]);
[NSMenu popUpContextMenu:currentMenu_
withEvent:[NSApp currentEvent]
forView:progressView_];
}
}
- (NSSize)preferredSize {
if (state_ == kNormal)
return [progressView_ frame].size;
DCHECK_EQ(kDangerous, state_);
return [dangerousDownloadView_ frame].size;
}
- (DownloadItem*)download {
return bridge_->download_model()->download();
}
- (void)clearDangerousMode {
[self setState:kNormal];
}
- (BOOL)isDangerousMode {
return state_ == kDangerous;
}
- (void)setState:(DownoadItemState)state {
if (state_ == state)
return;
state_ = state;
if (state_ == kNormal) {
[progressView_ setHidden:NO];
[dangerousDownloadView_ setHidden:YES];
} else {
DCHECK_EQ(kDangerous, state_);
[progressView_ setHidden:YES];
[dangerousDownloadView_ setHidden:NO];
}
[shelf_ layoutItems];
}
// Called after the current theme has changed.
- (void)themeDidChangeNotification:(NSNotification*)aNotification {
GTMTheme* theme = [aNotification object];
[self updateTheme:theme];
}
// Adapt appearance to the current theme. Called after theme changes and before
// this is shown for the first time.
- (void)updateTheme:(GTMTheme*)theme {
NSColor* color = [theme textColorForStyle:GTMThemeStyleTabBarSelected
state:GTMThemeStateActiveWindow];
[dangerousDownloadLabel_ setTextColor:color];
}
- (IBAction)saveDownload:(id)sender {
// The user has confirmed a dangerous download. We record how quickly the
// user did this to detect whether we're being clickjacked.
UMA_HISTOGRAM_LONG_TIMES("clickjacking.save_download",
base::Time::Now() - creationTime_);
// This will change the state and notify us.
bridge_->download_model()->download()->manager()->DangerousDownloadValidated(
bridge_->download_model()->download());
}
- (IBAction)discardDownload:(id)sender {
UMA_HISTOGRAM_LONG_TIMES("clickjacking.discard_download",
base::Time::Now() - creationTime_);
if (bridge_->download_model()->download()->state() ==
DownloadItem::IN_PROGRESS)
bridge_->download_model()->download()->Cancel(true);
bridge_->download_model()->download()->Remove(true);
// WARNING: we are deleted at this point. Don't access 'this'.
}
// Sets the enabled and checked state of a particular menu item for this
// download. We translate the NSMenuItem selection to menu selections understood
// by the non platform specific download context menu.
- (BOOL)validateMenuItem:(NSMenuItem *)item {
SEL action = [item action];
int actionId = 0;
if (action == @selector(handleOpen:)) {
actionId = DownloadShelfContextMenuMac::OPEN_WHEN_COMPLETE;
} else if (action == @selector(handleAlwaysOpen:)) {
actionId = DownloadShelfContextMenuMac::ALWAYS_OPEN_TYPE;
} else if (action == @selector(handleReveal:)) {
actionId = DownloadShelfContextMenuMac::SHOW_IN_FOLDER;
} else if (action == @selector(handleCancel:)) {
actionId = DownloadShelfContextMenuMac::CANCEL;
} else {
NOTREACHED();
return YES;
}
if (menuBridge_->ItemIsChecked(actionId))
[item setState:NSOnState];
else
[item setState:NSOffState];
return menuBridge_->IsItemCommandEnabled(actionId) ? YES : NO;
}
- (IBAction)handleOpen:(id)sender {
menuBridge_->ExecuteItemCommand(
DownloadShelfContextMenuMac::OPEN_WHEN_COMPLETE);
}
- (IBAction)handleAlwaysOpen:(id)sender {
menuBridge_->ExecuteItemCommand(
DownloadShelfContextMenuMac::ALWAYS_OPEN_TYPE);
}
- (IBAction)handleReveal:(id)sender {
menuBridge_->ExecuteItemCommand(DownloadShelfContextMenuMac::SHOW_IN_FOLDER);
}
- (IBAction)handleCancel:(id)sender {
menuBridge_->ExecuteItemCommand(DownloadShelfContextMenuMac::CANCEL);
}
@end
|