File: account_chooser_view_controller.mm

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (309 lines) | stat: -rw-r--r-- 11,578 bytes parent folder | download
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
// Copyright 2015 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/ui/cocoa/passwords/account_chooser_view_controller.h"

#include "base/mac/foundation_util.h"
#include "base/mac/scoped_nsobject.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
#import "chrome/browser/ui/cocoa/key_equivalent_constants.h"
#import "chrome/browser/ui/cocoa/passwords/account_avatar_fetcher_manager.h"
#import "chrome/browser/ui/cocoa/passwords/credential_item_button.h"
#import "chrome/browser/ui/cocoa/passwords/passwords_bubble_utils.h"
#include "chrome/browser/ui/passwords/manage_passwords_view_utils.h"
#include "chrome/browser/ui/passwords/password_dialog_controller.h"
#include "chrome/browser/ui/passwords/password_dialog_prompts.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/grit/theme_resources.h"
#include "components/autofill/core/common/password_form.h"
#include "components/password_manager/core/common/credential_manager_types.h"
#include "skia/ext/skia_utils_mac.h"
#include "ui/base/cocoa/controls/hyperlink_text_view.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/color_palette.h"
#include "ui/gfx/image/image_skia_util_mac.h"
#include "ui/gfx/paint_vector_icon.h"
#include "ui/gfx/vector_icons_public.h"
#include "ui/strings/grit/ui_strings.h"

namespace {

// Standard height of one credential item. It can be bigger though in case the
// text needs more vertical space than the avatar.
constexpr CGFloat kCredentialHeight =
    kAvatarImageSize + 2 * kVerticalAvatarMargin;

// Maximum height of the credential list. The unit is one row height.
constexpr CGFloat kMaxHeightAccounts = 3.5;

}  // namespace

// An image view that consumes the mouse click.
@interface InfoImageView : NSImageView
@end

@implementation InfoImageView
- (void)mouseDown:(NSEvent*)theEvent {
  if (theEvent.type != NSLeftMouseDown) {
    [super mouseDown:theEvent];
  }
}
@end

namespace {

NSImageView* IconForPSL(const NSRect& parentRect, const std::string& tooltip) {
  NSImage* image = gfx::NSImageFromImageSkia(gfx::CreateVectorIcon(
      gfx::VectorIconId::INFO_OUTLINE, gfx::kChromeIconGrey));
  NSRect rect = NSMakeRect(
      base::i18n::IsRTL() ? kFramePadding
                          : NSMaxX(parentRect) - kInfoIconSize - kFramePadding,
      NSMinY(parentRect) + (NSHeight(parentRect) - kInfoIconSize) / 2,
      kInfoIconSize, kInfoIconSize);
  base::scoped_nsobject<NSImageView> icon(
      [[InfoImageView alloc] initWithFrame:rect]);
  [icon setImage:image];
  [icon setToolTip:base::SysUTF8ToNSString(tooltip)];
  return icon.autorelease();
}

}  // namespace

@interface AccountChooserViewController () {
  NSButton* cancelButton_;  // Weak.
  NSButton* signInButton_;  // Weak.
  NSTextView* titleView_;   // Weak.
  base::scoped_nsobject<NSArray> credentialButtons_;
  base::scoped_nsobject<AccountAvatarFetcherManager> avatarManager_;
}
- (void)onCancelClicked:(id)sender;
- (void)onCredentialClicked:(id)sender;
- (void)onSignInClicked:(id)sender;
- (void)loadCredentialItems;
@end

@implementation AccountChooserViewController
@synthesize bridge = bridge_;

- (instancetype)initWithBridge:(PasswordPromptBridgeInterface*)bridge {
  base::scoped_nsobject<AccountAvatarFetcherManager> avatarManager(
      [[AccountAvatarFetcherManager alloc]
           initWithRequestContext:bridge->GetRequestContext()]);
  return [self initWithBridge:bridge avatarManager:avatarManager];
}

- (NSButton*)defaultButton {
  return signInButton_;
}

- (void)loadView {
  base::scoped_nsobject<NSView> view([[NSView alloc] initWithFrame:NSZeroRect]);

  // ------------------------------------
  // |                                  |
  // | Choose an account etc etc        |
  // |                                  |
  // | ----                             |
  // | |  |  credential view            |
  // | ----                             |
  // | |  |  credential view            |
  // | ----                             |
  // |                                  |
  // |             [ Cancel ] [Sign In] |
  // ------------------------------------

  // Create the views.
  // Title.
  PasswordDialogController* controller = bridge_->GetDialogController();
  std::pair<base::string16, gfx::Range> title_text =
      controller->GetAccoutChooserTitle();
  titleView_ =
      TitleDialogLabelWithLink(title_text.first, title_text.second, self);
  // Force the text to wrap to fit in the bubble size.
  [titleView_ setVerticallyResizable:YES];
  const CGFloat width = kDesiredBubbleWidth - 2*kFramePadding;
  [titleView_ setFrameSize:NSMakeSize(width, MAXFLOAT)];
  [titleView_ sizeToFit];
  [[titleView_ textContainer] setLineFragmentPadding:0];
  [view addSubview:titleView_];

  // Credentials list.
  [self loadCredentialItems];

  // "Cancel" button.
  cancelButton_ = BiggerDialogButton(l10n_util::GetNSString(IDS_APP_CANCEL));
  [cancelButton_ setTarget:self];
  [cancelButton_ setAction:@selector(onCancelClicked:)];
  [cancelButton_ setKeyEquivalent:kKeyEquivalentEscape];
  [view addSubview:cancelButton_];

  // "Sign In" button.
  if (controller->ShouldShowSignInButton()) {
    signInButton_ = BiggerDialogButton(
        l10n_util::GetNSString(IDS_PASSWORD_MANAGER_ACCOUNT_CHOOSER_SIGN_IN));
    [signInButton_ setTarget:self];
    [signInButton_ setAction:@selector(onSignInClicked:)];
    [signInButton_ setKeyEquivalent:kKeyEquivalentReturn];
    [view addSubview:signInButton_];
  }

  // Lay out the views.
  CGFloat curX = kFramePadding + width;
  if (signInButton_) {
    curX -= NSWidth([signInButton_ frame]);
    [signInButton_ setFrameOrigin:NSMakePoint(curX, kFramePadding)];
  }
  [cancelButton_ setFrameOrigin:NSMakePoint(
      curX - NSWidth([cancelButton_ frame]),
      kFramePadding)];

  base::scoped_nsobject<NSScrollView> scrollView([[NSScrollView alloc]
      initWithFrame:NSMakeRect(0, 0, kDesiredBubbleWidth, kCredentialHeight)]);
  [scrollView
      setHasVerticalScroller:[credentialButtons_ count] > kMaxHeightAccounts
                                 ? YES
                                 : NO];
  [scrollView setBorderType:NSNoBorder];
  const CGFloat buttonWidth = [scrollView contentSize].width;
  CGFloat curY = 0;
  base::scoped_nsobject<NSView> documentView([[NSView alloc]
      initWithFrame:NSZeroRect]);
  for (CredentialItemButton* button in credentialButtons_.get()) {
    [documentView addSubview:button];
    CGFloat cellHeight = [[button cell] cellSize].height;
    [button setFrame:NSMakeRect(0, curY, buttonWidth,
                                cellHeight + 2 * kVerticalAvatarMargin)];
    if (button.passwordForm->is_public_suffix_match) {
      NSImageView* icon = IconForPSL(
          [button frame], button.passwordForm->origin.GetOrigin().spec());
      [documentView addSubview:icon];
    }
    curY = NSMaxY([button frame]);
  }
  [documentView setFrameSize:NSMakeSize(buttonWidth, curY)];
  [scrollView setDocumentView:documentView];
  [scrollView setFrameSize:NSMakeSize(
      kDesiredBubbleWidth,
      [scrollView hasVerticalScroller] ? kMaxHeightAccounts * kCredentialHeight
                                       : curY)];
  [view addSubview:scrollView];
  [documentView scrollRectToVisible:NSMakeRect(0, curY, buttonWidth, 0)];
  curY = NSMaxY([cancelButton_ frame]) + 3 * kRelatedControlVerticalSpacing;
  [scrollView setFrameOrigin:NSMakePoint(0, curY)];
  curY = NSMaxY([scrollView frame]) + 2 * kRelatedControlVerticalSpacing;

  [titleView_ setFrameOrigin:NSMakePoint(kFramePadding, curY)];

  const CGFloat frameHeight = NSMaxY([titleView_ frame]) + kFramePadding;
  [view setFrame:NSMakeRect(0, 0, kDesiredBubbleWidth, frameHeight)];
  [self setView:view];
}

- (BOOL)textView:(NSTextView*)textView
    clickedOnLink:(id)link
          atIndex:(NSUInteger)charIndex {
  if (bridge_ && bridge_->GetDialogController())
    bridge_->GetDialogController()->OnSmartLockLinkClicked();
  return YES;
}

- (void)onCancelClicked:(id)sender {
  if (bridge_)
    bridge_->PerformClose();
}

- (void)onCredentialClicked:(id)sender {
  CredentialItemButton* button =
      base::mac::ObjCCastStrict<CredentialItemButton>(sender);
  if (bridge_ && bridge_->GetDialogController()) {
    bridge_->GetDialogController()->OnChooseCredentials(*button.passwordForm,
                                                        button.credentialType);
  }
}

- (void)onSignInClicked:(id)sender {
  if (bridge_ && bridge_->GetDialogController())
    bridge_->GetDialogController()->OnSignInClicked();
}

- (void)loadCredentialItems {
  base::scoped_nsobject<NSMutableArray> items([[NSMutableArray alloc] init]);
  PasswordDialogController* controller = self.bridge->GetDialogController();
  for (const auto& form : controller->GetLocalForms()) {
    base::scoped_nsobject<CredentialItemButton> item(
        [[CredentialItemButton alloc]
              initWithFrame:NSZeroRect
            backgroundColor:[NSColor textBackgroundColor]
                 hoverColor:skia::SkColorToSRGBNSColor(kButtonHoverColor)]);
    [item setPasswordForm:form.get()];
    [item setCredentialType:password_manager::CredentialType::
                                CREDENTIAL_TYPE_PASSWORD];
    std::pair<base::string16, base::string16> labels =
        GetCredentialLabelsForAccountChooser(*form);
    if (labels.second.empty()) {
      [item setTitle:base::SysUTF16ToNSString(labels.first)];
    } else {
      NSString* text = base::SysUTF16ToNSString(
          labels.first + base::ASCIIToUTF16("\n") + labels.second);
      NSFont* font = ResourceBundle::GetSharedInstance()
                         .GetFontList(ResourceBundle::SmallFont)
                         .GetPrimaryFont()
                         .GetNativeFont();
      NSDictionary* attrsDictionary =
          [NSDictionary dictionaryWithObject:font forKey:NSFontAttributeName];
      base::scoped_nsobject<NSMutableAttributedString> attributed_string(
          [[NSMutableAttributedString alloc] initWithString:text
                                                 attributes:attrsDictionary]);
      [attributed_string beginEditing];
      [attributed_string
          addAttribute:NSForegroundColorAttributeName
                 value:skia::SkColorToSRGBNSColor(kAutoSigninTextColor)
                 range:NSMakeRange(labels.first.size() + 1,
                                   labels.second.size())];
      [attributed_string endEditing];
      [item setAttributedTitle:attributed_string];
    }
    [item setImage:[CredentialItemButton defaultAvatar]];
    if (form->icon_url.is_valid())
      [avatarManager_ fetchAvatar:form->icon_url forView:item];
    [item setTarget:self];
    [item setAction:@selector(onCredentialClicked:)];
    [items addObject:item];
  }
  credentialButtons_.reset(items.release());
}

@end

@implementation AccountChooserViewController(Testing)

- (instancetype)initWithBridge:(PasswordPromptBridgeInterface*)bridge
                 avatarManager:(AccountAvatarFetcherManager*)avatarManager {
  DCHECK(bridge);
  if (self = [super initWithNibName:nil bundle:nil]) {
    bridge_ = bridge;
    avatarManager_.reset([avatarManager retain]);
  }
  return self;
}

- (NSButton*)cancelButton {
  return cancelButton_;
}

- (NSButton*)signInButton {
  return signInButton_;
}

- (NSArray*)credentialButtons {
  return credentialButtons_;
}

- (NSTextView*)titleView {
  return titleView_;
}

@end