OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include "chrome/browser/ui/views/passwords/credentials_selection_view.h" | |
6 | |
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" | |
15 | |
16 namespace { | |
17 | |
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); | |
24 return label; | |
25 } | |
26 | |
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; | |
36 } | |
37 } | |
38 | |
39 views::Combobox* combobox = | |
40 new views::Combobox(new ui::SimpleComboboxModel(usernames)); | |
41 | |
42 if (best_matched_username_index < forms.size()) { | |
43 combobox->SetSelectedIndex(best_matched_username_index); | |
44 } | |
45 return combobox; | |
46 } | |
47 | |
48 } // namespace | |
49 | |
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()); | |
56 | |
57 // Layout. | |
58 views::GridLayout* layout = new views::GridLayout(this); | |
59 SetLayoutManager(layout); | |
60 | |
61 // ColumnSet. | |
62 int column_set_id = 0; | |
63 views::ColumnSet* column_set = layout->AddColumnSet(column_set_id); | |
64 column_set->AddPaddingColumn(0, views::kItemLabelSpacing); | |
65 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, | |
66 views::GridLayout::FIXED, 0, 0); | |
67 column_set->AddPaddingColumn(0, views::kItemLabelSpacing); | |
68 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, | |
69 views::GridLayout::FIXED, 0, 0); | |
70 column_set->AddPaddingColumn(0, views::kItemLabelSpacing); | |
71 | |
72 // The username combobox and password label. | |
73 layout->StartRowWithPadding(0, column_set_id, 0, | |
74 views::kRelatedControlVerticalSpacing); | |
75 combobox_ = GenerateUsernameCombobox( | |
76 manage_passwords_bubble_model->local_credentials().get(), | |
77 best_matched_username); | |
78 layout->AddView(combobox_); | |
79 views::Label* label = | |
vasilii
2015/08/05 15:42:55
I'd get rid of |label| to avoid memory leaks
dvadym
2015/08/05 16:40:43
As far as I understand |label| will be removed on
vasilii
2015/08/05 17:34:08
Yes, I meant to add the view directly. The variabl
| |
80 GeneratePasswordLabel(manage_passwords_bubble_model->pending_password()); | |
81 layout->AddView(label); | |
82 | |
83 GetLayoutManager()->Layout(this); | |
84 } | |
85 | |
86 const autofill::PasswordForm* | |
87 CredentialsSelectionView::GetSelectedCredentials() { | |
88 DCHECK_EQ(password_forms_.size(), | |
89 static_cast<size_t>(combobox_->model()->GetItemCount())); | |
90 return password_forms_[combobox_->selected_index()]; | |
91 } | |
OLD | NEW |