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
3 // found in the LICENSE file.
5 #import "chrome/browser/ui/cocoa/browser/password_generation_bubble_controller.h"
7 #include "base/mac/foundation_util.h"
8 #include "base/strings/sys_string_conversions.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_window.h"
11 #import "chrome/browser/ui/cocoa/info_bubble_view.h"
12 #import "chrome/browser/ui/cocoa/info_bubble_window.h"
13 #include "chrome/browser/ui/cocoa/key_equivalent_constants.h"
14 #import "chrome/browser/ui/cocoa/styled_text_field_cell.h"
15 #include "components/autofill/content/common/autofill_messages.h"
16 #include "components/autofill/core/browser/password_generator.h"
17 #include "components/autofill/core/common/password_form.h"
18 #include "components/autofill/core/common/password_generation_util.h"
19 #include "components/password_manager/core/browser/password_manager.h"
20 #include "content/public/browser/render_view_host.h"
21 #include "grit/generated_resources.h"
22 #include "grit/theme_resources.h"
23 #import "ui/base/cocoa/tracking_area.h"
24 #include "ui/base/l10n/l10n_util_mac.h"
25 #include "ui/base/resource/resource_bundle.h"
26 #include "ui/gfx/font_list.h"
30 // Size of the border in the bubble.
31 const CGFloat kBorderSize = 9.0;
33 // Visible size of the textfield.
34 const CGFloat kTextFieldHeight = 20.0;
35 const CGFloat kTextFieldWidth = 172.0;
37 // Frame padding necessary to make the textfield the correct visible size.
38 const CGFloat kTextFieldTopPadding = 3.0;
40 // Visible size of the button
41 const CGFloat kButtonWidth = 63.0;
42 const CGFloat kButtonHeight = 20.0;
44 // Padding that is added to the frame around the button to make it the
45 // correct visible size. Determined via visual inspection.
46 const CGFloat kButtonHorizontalPadding = 6.0;
47 const CGFloat kButtonVerticalPadding = 3.0;
49 // Visible size of the title.
50 const CGFloat kTitleWidth = 170.0;
51 const CGFloat kTitleHeight = 15.0;
53 // Space between the title and the textfield.
54 const CGFloat kVerticalSpacing = 13.0;
56 // Space between the textfield and the button.
57 const CGFloat kHorizontalSpacing = 7.0;
59 // We don't actually want the border to be kBorderSize on top as there is
60 // whitespace in the title text that makes it looks substantially bigger.
61 const CGFloat kTopBorderOffset = 3.0;
63 const CGFloat kIconSize = 26.0;
67 // Customized StyledTextFieldCell to display one button decoration that changes
69 @interface PasswordGenerationTextFieldCell : StyledTextFieldCell {
71 PasswordGenerationBubbleController* controller_;
73 base::scoped_nsobject<NSImage> normalImage_;
74 base::scoped_nsobject<NSImage> hoverImage_;
77 - (void)setUpWithController:(PasswordGenerationBubbleController*)controller
78 normalImage:(NSImage*)normalImage
79 hoverImage:(NSImage*)hoverImage;
80 - (void)mouseEntered:(NSEvent*)theEvent
81 inView:(PasswordGenerationTextField*)controlView;
82 - (void)mouseExited:(NSEvent*)theEvent
83 inView:(PasswordGenerationTextField*)controlView;
84 - (BOOL)mouseDown:(NSEvent*)theEvent
85 inView:(PasswordGenerationTextField*)controlView;
86 - (void)setUpTrackingAreaInRect:(NSRect)frame
87 ofView:(PasswordGenerationTextField*)controlView;
88 // Exposed for testing.
92 @implementation PasswordGenerationTextField
95 return [PasswordGenerationTextFieldCell class];
98 - (PasswordGenerationTextFieldCell*)cell {
99 return base::mac::ObjCCastStrict<PasswordGenerationTextFieldCell>(
103 - (id)initWithFrame:(NSRect)frame
104 withController:(PasswordGenerationBubbleController*)controller
105 normalImage:(NSImage*)normalImage
106 hoverImage:(NSImage*)hoverImage {
107 self = [super initWithFrame:frame];
109 PasswordGenerationTextFieldCell* cell = [self cell];
110 [cell setUpWithController:controller
111 normalImage:normalImage
112 hoverImage:hoverImage];
113 [cell setUpTrackingAreaInRect:[self bounds] ofView:self];
118 - (void)mouseEntered:(NSEvent*)theEvent {
119 [[self cell] mouseEntered:theEvent inView:self];
122 - (void)mouseExited:(NSEvent*)theEvent {
123 [[self cell] mouseExited:theEvent inView:self];
126 - (void)mouseDown:(NSEvent*)theEvent {
127 // Let the cell handle the click if it's in the decoration.
128 if (![[self cell] mouseDown:theEvent inView:self]) {
129 if ([self currentEditor]) {
130 [[self currentEditor] mouseDown:theEvent];
132 // We somehow lost focus.
133 [super mouseDown:theEvent];
138 - (void)simulateIconClick {
139 [[self cell] iconClicked];
144 @implementation PasswordGenerationTextFieldCell
146 - (void)setUpWithController:(PasswordGenerationBubbleController*)controller
147 normalImage:(NSImage*)normalImage
148 hoverImage:(NSImage*)hoverImage {
149 controller_ = controller;
151 normalImage_.reset([normalImage retain]);
152 hoverImage_.reset([hoverImage retain]);
153 [self setLineBreakMode:NSLineBreakByTruncatingTail];
154 [self setTruncatesLastVisibleLine:YES];
157 - (void)splitFrame:(NSRect*)cellFrame toIconFrame:(NSRect*)iconFrame {
158 NSDivideRect(*cellFrame, iconFrame, cellFrame,
159 kIconSize, NSMaxXEdge);
162 - (NSRect)getIconFrame:(NSRect)cellFrame {
164 [self splitFrame:&cellFrame toIconFrame:&iconFrame];
168 - (NSRect)getTextFrame:(NSRect)cellFrame {
170 [self splitFrame:&cellFrame toIconFrame:&iconFrame];
174 - (BOOL)eventIsInDecoration:(NSEvent*)theEvent
175 inView:(PasswordGenerationTextField*)controlView {
176 NSPoint mouseLocation = [controlView convertPoint:[theEvent locationInWindow]
178 NSRect cellFrame = [controlView bounds];
179 return NSMouseInRect(mouseLocation,
180 [self getIconFrame:cellFrame],
181 [controlView isFlipped]);
184 - (void)mouseEntered:(NSEvent*)theEvent
185 inView:(PasswordGenerationTextField*)controlView {
187 [controlView setNeedsDisplay:YES];
190 - (void)mouseExited:(NSEvent*)theEvent
191 inView:(PasswordGenerationTextField*)controlView {
193 [controlView setNeedsDisplay:YES];
196 - (BOOL)mouseDown:(NSEvent*)theEvent
197 inView:(PasswordGenerationTextField*)controlView {
198 if ([self eventIsInDecoration:theEvent inView:controlView]) {
205 - (void)iconClicked {
206 [controller_ regeneratePassword];
209 - (NSImage*)getImage {
215 - (NSRect)adjustFrameForFrame:(NSRect)frame {
216 // By default, there appears to be a 2 pixel gap between what is considered
217 // part of the textFrame and what is considered part of the icon.
218 // TODO(gcasto): This really should be fixed in StyledTextFieldCell, as it
219 // looks like the location bar also suffers from this issue.
220 frame.size.width += 2;
224 - (NSRect)textFrameForFrame:(NSRect)cellFrame {
225 // Baseclass insets the rect by top and bottom offsets.
226 NSRect textFrame = [super textFrameForFrame:cellFrame];
227 textFrame = [self getTextFrame:textFrame];
228 return [self adjustFrameForFrame:textFrame];
231 - (NSRect)textCursorFrameForFrame:(NSRect)cellFrame {
232 NSRect textFrame = [self getTextFrame:cellFrame];
233 return [self adjustFrameForFrame:textFrame];
236 - (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView*)controlView {
237 NSImage* image = [self getImage];
238 NSRect iconFrame = [self getIconFrame:cellFrame];
239 // Center the image in the available space. At the moment the image is
240 // slightly larger than the frame so we crop it.
241 // Offset the full difference on the left hand side since the border on the
242 // right takes up some space. Offset half the vertical difference on the
243 // bottom so that the image stays vertically centered.
244 const CGFloat xOffset = [image size].width - NSWidth(iconFrame);
245 const CGFloat yOffset = ([image size].height - (NSHeight(iconFrame))) / 2.0;
246 NSRect croppedRect = NSMakeRect(xOffset,
249 NSHeight(iconFrame));
251 [image drawInRect:iconFrame
253 operation:NSCompositeSourceOver
258 [super drawInteriorWithFrame:cellFrame inView:controlView];
261 - (void)setUpTrackingAreaInRect:(NSRect)frame
262 ofView:(PasswordGenerationTextField*)view {
263 NSRect iconFrame = [self getIconFrame:frame];
264 base::scoped_nsobject<CrTrackingArea> area(
265 [[CrTrackingArea alloc] initWithRect:iconFrame
266 options:NSTrackingMouseEnteredAndExited |
267 NSTrackingActiveAlways owner:view userInfo:nil]);
268 [view addTrackingArea:area];
271 - (CGFloat)topTextFrameOffset {
275 - (CGFloat)bottomTextFrameOffset {
279 - (CGFloat)cornerRadius {
283 - (BOOL)shouldDrawBezel {
289 @implementation PasswordGenerationBubbleController
291 @synthesize textField = textField_;
293 - (id)initWithWindow:(NSWindow*)parentWindow
294 anchoredAt:(NSPoint)point
295 renderViewHost:(content::RenderViewHost*)renderViewHost
296 passwordManager:(password_manager::PasswordManager*)passwordManager
297 usingGenerator:(autofill::PasswordGenerator*)passwordGenerator
298 forForm:(const autofill::PasswordForm&)form {
299 CGFloat width = (kBorderSize*2 +
303 CGFloat height = (kBorderSize*2 +
308 info_bubble::kBubbleArrowHeight);
309 NSRect contentRect = NSMakeRect(0, 0, width, height);
310 base::scoped_nsobject<InfoBubbleWindow> window(
311 [[InfoBubbleWindow alloc] initWithContentRect:contentRect
312 styleMask:NSBorderlessWindowMask
313 backing:NSBackingStoreBuffered
315 if (self = [super initWithWindow:window
316 parentWindow:parentWindow
318 passwordGenerator_ = passwordGenerator;
319 renderViewHost_ = renderViewHost;
320 passwordManager_ = passwordManager;
322 [[self bubble] setArrowLocation:info_bubble::kTopLeft];
323 [self performLayout];
329 - (void)performLayout {
330 NSView* contentView = [[self window] contentView];
331 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
333 textField_ = [[[PasswordGenerationTextField alloc]
334 initWithFrame:NSMakeRect(kBorderSize,
337 kTextFieldHeight + kTextFieldTopPadding)
339 normalImage:rb.GetNativeImageNamed(IDR_RELOAD_DIMMED).ToNSImage()
340 hoverImage:rb.GetNativeImageNamed(IDR_RELOAD)
341 .ToNSImage()] autorelease];
342 const gfx::FontList& smallBoldFont =
343 rb.GetFontList(ui::ResourceBundle::SmallBoldFont);
344 [textField_ setFont:smallBoldFont.GetPrimaryFont().GetNativeFont()];
346 setStringValue:base::SysUTF8ToNSString(passwordGenerator_->Generate())];
347 [textField_ setDelegate:self];
348 [contentView addSubview:textField_];
350 CGFloat buttonX = (NSMaxX([textField_ frame]) +
352 kButtonHorizontalPadding);
353 CGFloat buttonY = kBorderSize - kButtonVerticalPadding;
355 [[NSButton alloc] initWithFrame:NSMakeRect(
358 kButtonWidth + 2 * kButtonHorizontalPadding,
359 kButtonHeight + 2 * kButtonVerticalPadding)];
360 [button setBezelStyle:NSRoundedBezelStyle];
361 [button setTitle:l10n_util::GetNSString(IDS_PASSWORD_GENERATION_BUTTON_TEXT)];
362 [button setTarget:self];
363 [button setAction:@selector(fillPassword:)];
364 [contentView addSubview:button];
366 base::scoped_nsobject<NSTextField> title([[NSTextField alloc] initWithFrame:
367 NSMakeRect(kBorderSize,
368 kBorderSize + kTextFieldHeight + kVerticalSpacing,
371 [title setEditable:NO];
372 [title setBordered:NO];
373 [title setStringValue:l10n_util::GetNSString(
374 IDS_PASSWORD_GENERATION_BUBBLE_TITLE)];
375 [contentView addSubview:title];
378 - (IBAction)fillPassword:(id)sender {
379 if (renderViewHost_) {
380 renderViewHost_->Send(
381 new AutofillMsg_GeneratedPasswordAccepted(
382 renderViewHost_->GetRoutingID(),
383 base::SysNSStringToUTF16([textField_ stringValue])));
385 if (passwordManager_)
386 passwordManager_->SetFormHasGeneratedPassword(form_);
388 actions_.password_accepted = true;
392 - (void)regeneratePassword {
394 setStringValue:base::SysUTF8ToNSString(passwordGenerator_->Generate())];
395 actions_.password_regenerated = true;
398 - (void)controlTextDidChange:(NSNotification*)notification {
399 actions_.password_edited = true;
402 - (void)windowWillClose:(NSNotification*)notification {
403 autofill::password_generation::LogUserActions(actions_);
404 [super windowWillClose:notification];