| 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 <stddef.h> | |
| 8 | |
| 9 #include "chrome/browser/ui/passwords/manage_passwords_bubble_model.h" | |
| 10 #include "components/password_manager/core/browser/password_manager_metrics_util
.h" | |
| 11 #include "ui/base/models/simple_combobox_model.h" | |
| 12 #include "ui/base/resource/resource_bundle.h" | |
| 13 #include "ui/views/controls/button/button.h" | |
| 14 #include "ui/views/controls/combobox/combobox.h" | |
| 15 #include "ui/views/controls/label.h" | |
| 16 #include "ui/views/layout/grid_layout.h" | |
| 17 #include "ui/views/layout/layout_constants.h" | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 views::Label* GeneratePasswordLabel(const autofill::PasswordForm& form) { | |
| 22 views::Label* label = new views::Label(form.password_value); | |
| 23 label->SetFontList(ui::ResourceBundle::GetSharedInstance().GetFontList( | |
| 24 ui::ResourceBundle::SmallFont)); | |
| 25 label->SetHorizontalAlignment(gfx::ALIGN_CENTER); | |
| 26 label->SetObscured(true); | |
| 27 return label; | |
| 28 } | |
| 29 | |
| 30 } // namespace | |
| 31 | |
| 32 CredentialsSelectionView::CredentialsSelectionView( | |
| 33 ManagePasswordsBubbleModel* manage_passwords_bubble_model) | |
| 34 : password_forms_(&manage_passwords_bubble_model->local_credentials()), | |
| 35 default_index_(0), | |
| 36 is_default_best_match_(false), | |
| 37 is_default_preferred_(false), | |
| 38 action_reported_(false) { | |
| 39 DCHECK(!password_forms_->empty()); | |
| 40 | |
| 41 // Layout. | |
| 42 views::GridLayout* layout = new views::GridLayout(this); | |
| 43 SetLayoutManager(layout); | |
| 44 | |
| 45 // ColumnSet. | |
| 46 int column_set_id = 0; | |
| 47 views::ColumnSet* column_set = layout->AddColumnSet(column_set_id); | |
| 48 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, | |
| 49 views::GridLayout::FIXED, 0, 0); | |
| 50 column_set->AddPaddingColumn(0, views::kItemLabelSpacing); | |
| 51 column_set->AddColumn(views::GridLayout::FILL, views::GridLayout::FILL, 1, | |
| 52 views::GridLayout::FIXED, 0, 0); | |
| 53 column_set->AddPaddingColumn(0, views::kItemLabelSpacing); | |
| 54 | |
| 55 // The username combobox and password label. | |
| 56 layout->StartRowWithPadding(0, column_set_id, 0, | |
| 57 views::kRelatedControlVerticalSpacing); | |
| 58 GenerateUsernameCombobox( | |
| 59 manage_passwords_bubble_model->pending_password().username_value); | |
| 60 layout->AddView(combobox_.get()); | |
| 61 views::Label* label = | |
| 62 GeneratePasswordLabel(manage_passwords_bubble_model->pending_password()); | |
| 63 layout->AddView(label); | |
| 64 | |
| 65 GetLayoutManager()->Layout(this); | |
| 66 } | |
| 67 | |
| 68 CredentialsSelectionView::~CredentialsSelectionView() { | |
| 69 ReportUserActionOnce(true, -1); | |
| 70 // |combobox_| has a pointer to |combobox_model_|, so |combobox_| should be | |
| 71 // deleted before deleting of |combobox_model_|. To ensure this, let's delete | |
| 72 // it now. | |
| 73 combobox_.reset(); | |
| 74 } | |
| 75 | |
| 76 const autofill::PasswordForm* | |
| 77 CredentialsSelectionView::GetSelectedCredentials() { | |
| 78 DCHECK_EQ(password_forms_->size(), | |
| 79 static_cast<size_t>(combobox_->model()->GetItemCount())); | |
| 80 ReportUserActionOnce(false, combobox_->selected_index()); | |
| 81 return &password_forms_->at(combobox_->selected_index()); | |
| 82 } | |
| 83 | |
| 84 void CredentialsSelectionView::GenerateUsernameCombobox( | |
| 85 const base::string16& best_matched_username) { | |
| 86 std::vector<base::string16> usernames; | |
| 87 size_t best_matched_username_index = password_forms_->size(); | |
| 88 size_t preferred_form_index = password_forms_->size(); | |
| 89 for (size_t index = 0; index < password_forms_->size(); ++index) { | |
| 90 usernames.push_back(password_forms_->at(index).username_value); | |
| 91 if (password_forms_->at(index).username_value == best_matched_username) { | |
| 92 best_matched_username_index = index; | |
| 93 } | |
| 94 if (password_forms_->at(index).preferred) { | |
| 95 preferred_form_index = index; | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 combobox_model_.reset(new ui::SimpleComboboxModel(usernames)); | |
| 100 combobox_.reset(new views::Combobox(combobox_model_.get())); | |
| 101 | |
| 102 if (best_matched_username_index < password_forms_->size()) { | |
| 103 is_default_best_match_ = true; | |
| 104 default_index_ = best_matched_username_index; | |
| 105 combobox_->SetSelectedIndex(best_matched_username_index); | |
| 106 } else if (preferred_form_index < password_forms_->size()) { | |
| 107 is_default_preferred_ = true; | |
| 108 default_index_ = preferred_form_index; | |
| 109 combobox_->SetSelectedIndex(preferred_form_index); | |
| 110 } | |
| 111 } | |
| 112 | |
| 113 void CredentialsSelectionView::ReportUserActionOnce(bool was_update_rejected, | |
| 114 int selected_index) { | |
| 115 if (action_reported_) | |
| 116 return; | |
| 117 password_manager::metrics_util::MultiAccountUpdateBubbleUserAction action; | |
| 118 if (was_update_rejected) { | |
| 119 if (is_default_best_match_) { | |
| 120 action = password_manager::metrics_util:: | |
| 121 DEFAULT_ACCOUNT_MATCHED_BY_PASSWORD_USER_REJECTED_UPDATE; | |
| 122 } else if (is_default_preferred_) { | |
| 123 action = password_manager::metrics_util:: | |
| 124 DEFAULT_ACCOUNT_PREFERRED_USER_REJECTED_UPDATE; | |
| 125 } else { | |
| 126 action = password_manager::metrics_util:: | |
| 127 DEFAULT_ACCOUNT_FIRST_USER_REJECTED_UPDATE; | |
| 128 } | |
| 129 } else if (selected_index == default_index_) { | |
| 130 if (is_default_best_match_) { | |
| 131 action = password_manager::metrics_util:: | |
| 132 DEFAULT_ACCOUNT_MATCHED_BY_PASSWORD_USER_NOT_CHANGED; | |
| 133 } else if (is_default_preferred_) { | |
| 134 action = password_manager::metrics_util:: | |
| 135 DEFAULT_ACCOUNT_PREFERRED_USER_NOT_CHANGED; | |
| 136 } else { | |
| 137 action = password_manager::metrics_util:: | |
| 138 DEFAULT_ACCOUNT_FIRST_USER_NOT_CHANGED; | |
| 139 } | |
| 140 } else { | |
| 141 if (is_default_best_match_) { | |
| 142 action = password_manager::metrics_util:: | |
| 143 DEFAULT_ACCOUNT_MATCHED_BY_PASSWORD_USER_CHANGED; | |
| 144 } else if (is_default_preferred_) { | |
| 145 action = password_manager::metrics_util:: | |
| 146 DEFAULT_ACCOUNT_PREFERRED_USER_CHANGED; | |
| 147 } else { | |
| 148 action = | |
| 149 password_manager::metrics_util::DEFAULT_ACCOUNT_FIRST_USER_CHANGED; | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 password_manager::metrics_util::LogMultiAccountUpdateBubbleUserAction(action); | |
| 154 action_reported_ = true; | |
| 155 } | |
| OLD | NEW |