1 // Copyright 2015 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 #include "chrome/browser/ui/views/passwords/credentials_selection_view.h"
7 #include "chrome/browser/ui/passwords/manage_passwords_bubble_model.h"
8 #include "ui/base/models/simple_combobox_model.h"
9 #include "ui/base/resource/resource_bundle.h"
10 #include "ui/views/controls/button/button.h"
11 #include "ui/views/controls/combobox/combobox.h"
12 #include "ui/views/controls/label.h"
13 #include "ui/views/layout/grid_layout.h"
14 #include "ui/views/layout/layout_constants.h"
18 views::Label
* GeneratePasswordLabel(const autofill::PasswordForm
& form
) {
19 views::Label
* label
= new views::Label(form
.password_value
);
20 label
->SetFontList(ui::ResourceBundle::GetSharedInstance().GetFontList(
21 ui::ResourceBundle::SmallFont
));
22 label
->SetHorizontalAlignment(gfx::ALIGN_CENTER
);
23 label
->SetObscured(true);
27 views::Combobox
* GenerateUsernameCombobox(
28 const std::vector
<const autofill::PasswordForm
*>& forms
,
29 const base::string16
& best_matched_username
) {
30 std::vector
<base::string16
> usernames
;
31 size_t best_matched_username_index
= forms
.size();
32 for (size_t index
= 0; index
< forms
.size(); ++index
) {
33 usernames
.push_back(forms
[index
]->username_value
);
34 if (forms
[index
]->username_value
== best_matched_username
) {
35 best_matched_username_index
= index
;
39 views::Combobox
* combobox
=
40 new views::Combobox(new ui::SimpleComboboxModel(usernames
));
42 if (best_matched_username_index
< forms
.size()) {
43 combobox
->SetSelectedIndex(best_matched_username_index
);
50 CredentialsSelectionView::CredentialsSelectionView(
51 ManagePasswordsBubbleModel
* manage_passwords_bubble_model
,
52 const std::vector
<const autofill::PasswordForm
*>& password_forms
,
53 const base::string16
& best_matched_username
)
54 : password_forms_(password_forms
) {
55 DCHECK(!password_forms
.empty());
58 views::GridLayout
* layout
= new views::GridLayout(this);
59 SetLayoutManager(layout
);
62 int column_set_id
= 0;
63 views::ColumnSet
* column_set
= layout
->AddColumnSet(column_set_id
);
64 column_set
->AddColumn(views::GridLayout::FILL
, views::GridLayout::FILL
, 1,
65 views::GridLayout::FIXED
, 0, 0);
66 column_set
->AddPaddingColumn(0, views::kItemLabelSpacing
);
67 column_set
->AddColumn(views::GridLayout::FILL
, views::GridLayout::FILL
, 1,
68 views::GridLayout::FIXED
, 0, 0);
69 column_set
->AddPaddingColumn(0, views::kItemLabelSpacing
);
71 // The username combobox and password label.
72 layout
->StartRowWithPadding(0, column_set_id
, 0,
73 views::kRelatedControlVerticalSpacing
);
74 combobox_
= GenerateUsernameCombobox(
75 manage_passwords_bubble_model
->local_credentials().get(),
76 best_matched_username
);
77 layout
->AddView(combobox_
);
79 GeneratePasswordLabel(manage_passwords_bubble_model
->pending_password());
80 layout
->AddView(label
);
82 GetLayoutManager()->Layout(this);
85 const autofill::PasswordForm
*
86 CredentialsSelectionView::GetSelectedCredentials() {
87 DCHECK_EQ(password_forms_
.size(),
88 static_cast<size_t>(combobox_
->model()->GetItemCount()));
89 return password_forms_
[combobox_
->selected_index()];