summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/cocoa/passwords/account_chooser_view_controller.mm
blob: 08ebc734f3a3a0287390d807f398c27fd4010ead (plain)
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
// 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/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 "components/autofill/core/common/password_form.h"
#include "components/password_manager/core/common/credential_manager_types.h"
#include "grit/theme_resources.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/strings/grit/ui_strings.h"

@interface AccountChooserViewController () {
  NSButton* cancelButton_;  // Weak.
  NSTextView* titleView_;   //  Weak.
  base::scoped_nsobject<NSArray> credentialButtons_;
  base::scoped_nsobject<AccountAvatarFetcherManager> avatarManager_;
}
- (void)onCancelClicked:(id)sender;
- (void)onCredentialClicked:(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];
}

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

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

  // Create the views.
  // Title.
  std::pair<base::string16, gfx::Range> title_text =
      bridge_->GetDialogController()->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_ = DialogButton(l10n_util::GetNSString(IDS_APP_CANCEL));
  [cancelButton_ setTarget:self];
  [cancelButton_ setAction:@selector(onCancelClicked:)];
  [view addSubview:cancelButton_];

  // Lay out the views.
  [cancelButton_ setFrameOrigin:NSMakePoint(
      kFramePadding + width - NSWidth([cancelButton_ frame]),
      kFramePadding)];

  CGFloat curY =
      NSMaxY([cancelButton_ frame]) + 3 * kRelatedControlVerticalSpacing;
  for (CredentialItemButton* button in credentialButtons_.get()) {
    [view addSubview:button];
    [button setFrameOrigin:NSMakePoint(0, curY)];
    curY = NSMaxY([button frame]);
  }
  curY += 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)loadCredentialItems {
  base::scoped_nsobject<NSMutableArray> items([[NSMutableArray alloc] init]);
  PasswordDialogController* controller = self.bridge->GetDialogController();
  NSRect rect = NSMakeRect(0, 0, kDesiredBubbleWidth,
                           kAvatarImageSize + 2 * kVerticalAvatarMargin);
  for (const auto& form : controller->GetLocalForms()) {
    base::scoped_nsobject<CredentialItemButton> item(
        [[CredentialItemButton alloc]
              initWithFrame:rect
            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_;
}

- (NSArray*)credentialButtons {
  return credentialButtons_;
}

- (NSTextView*)titleView {
  return titleView_;
}

@end