| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/autofill/autofill_popup_view.h" | 5 #include "chrome/browser/autofill/autofill_popup_view.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 9 #include "chrome/browser/autofill/autofill_external_delegate.h" | 9 #include "chrome/browser/autofill/autofill_external_delegate.h" |
| 10 #include "content/public/browser/web_contents.h" | 10 #include "content/public/browser/web_contents.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 { "soloCC", IDR_AUTOFILL_CC_SOLO }, | 37 { "soloCC", IDR_AUTOFILL_CC_SOLO }, |
| 38 { "visaCC", IDR_AUTOFILL_CC_VISA }, | 38 { "visaCC", IDR_AUTOFILL_CC_VISA }, |
| 39 }; | 39 }; |
| 40 | 40 |
| 41 } // end namespace | 41 } // end namespace |
| 42 | 42 |
| 43 AutofillPopupView::AutofillPopupView( | 43 AutofillPopupView::AutofillPopupView( |
| 44 content::WebContents* web_contents, | 44 content::WebContents* web_contents, |
| 45 AutofillExternalDelegate* external_delegate) | 45 AutofillExternalDelegate* external_delegate) |
| 46 : external_delegate_(external_delegate), | 46 : external_delegate_(external_delegate), |
| 47 row_count_(0), |
| 47 selected_line_(kNoSelection) { | 48 selected_line_(kNoSelection) { |
| 48 if (!web_contents) | 49 if (!web_contents) |
| 49 return; | 50 return; |
| 50 | 51 |
| 51 registrar_.Add(this, | 52 registrar_.Add(this, |
| 52 content::NOTIFICATION_WEB_CONTENTS_HIDDEN, | 53 content::NOTIFICATION_WEB_CONTENTS_HIDDEN, |
| 53 content::Source<content::WebContents>(web_contents)); | 54 content::Source<content::WebContents>(web_contents)); |
| 54 registrar_.Add( | 55 registrar_.Add( |
| 55 this, | 56 this, |
| 56 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 57 content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 68 | 69 |
| 69 void AutofillPopupView::Show(const std::vector<string16>& autofill_values, | 70 void AutofillPopupView::Show(const std::vector<string16>& autofill_values, |
| 70 const std::vector<string16>& autofill_labels, | 71 const std::vector<string16>& autofill_labels, |
| 71 const std::vector<string16>& autofill_icons, | 72 const std::vector<string16>& autofill_icons, |
| 72 const std::vector<int>& autofill_unique_ids) { | 73 const std::vector<int>& autofill_unique_ids) { |
| 73 autofill_values_ = autofill_values; | 74 autofill_values_ = autofill_values; |
| 74 autofill_labels_ = autofill_labels; | 75 autofill_labels_ = autofill_labels; |
| 75 autofill_icons_ = autofill_icons; | 76 autofill_icons_ = autofill_icons; |
| 76 autofill_unique_ids_ = autofill_unique_ids; | 77 autofill_unique_ids_ = autofill_unique_ids; |
| 77 | 78 |
| 79 |
| 80 // Calculate the number of rows visible in the popup. |
| 81 row_count_ = 0; |
| 82 for (size_t i = 0; i < autofill_unique_ids.size(); ++i) { |
| 83 if (autofill_unique_ids[i] != WebAutofillClient::MenuItemIDSeparator) |
| 84 ++row_count_; |
| 85 } |
| 86 |
| 87 // Generate the row to index mapping. |
| 88 size_t index = 0; |
| 89 row_to_index_mapping_.clear(); |
| 90 for (int row = 0; row < row_count_; ++row, ++index) { |
| 91 // The separators aren't valid rows so skip them. |
| 92 while (index < autofill_unique_ids.size() && |
| 93 (autofill_unique_ids[index] == |
| 94 WebAutofillClient::MenuItemIDSeparator)) { |
| 95 ++index; |
| 96 } |
| 97 |
| 98 row_to_index_mapping_.push_back(index); |
| 99 } |
| 100 |
| 78 ShowInternal(); | 101 ShowInternal(); |
| 79 } | 102 } |
| 80 | 103 |
| 81 void AutofillPopupView::SetSelectedLine(int selected_line) { | 104 void AutofillPopupView::SetSelectedLine(int selected_line) { |
| 82 if (selected_line_ == selected_line) | 105 if (selected_line_ == selected_line) |
| 83 return; | 106 return; |
| 84 | 107 |
| 85 if (selected_line_ != kNoSelection) | 108 if (selected_line_ != kNoSelection) |
| 86 InvalidateRow(selected_line_); | 109 InvalidateRow(selected_line_); |
| 87 | 110 |
| 88 if (selected_line != kNoSelection) | 111 if (selected_line != kNoSelection) |
| 89 InvalidateRow(selected_line); | 112 InvalidateRow(selected_line); |
| 90 | 113 |
| 91 selected_line_ = selected_line; | 114 selected_line_ = selected_line; |
| 92 | 115 |
| 93 if (selected_line_ != kNoSelection) { | 116 if (selected_line_ != kNoSelection) { |
| 94 external_delegate_->SelectAutofillSuggestionAtIndex( | 117 external_delegate_->SelectAutofillSuggestionAtIndex( |
| 95 autofill_unique_ids_[selected_line_]); | 118 autofill_unique_ids_[selected_line_]); |
| 96 } | 119 } |
| 97 } | 120 } |
| 98 | 121 |
| 99 void AutofillPopupView::SelectNextLine() { | 122 void AutofillPopupView::SelectNextLine() { |
| 100 int new_selected_line = selected_line_ + 1; | 123 int new_selected_line = selected_line_ + 1; |
| 101 | 124 |
| 102 if (new_selected_line == static_cast<int>(autofill_values_.size())) | 125 if (new_selected_line == static_cast<int>(row_count())) |
| 103 new_selected_line = 0; | 126 new_selected_line = 0; |
| 104 | 127 |
| 105 SetSelectedLine(new_selected_line); | 128 SetSelectedLine(new_selected_line); |
| 106 } | 129 } |
| 107 | 130 |
| 108 void AutofillPopupView::SelectPreviousLine() { | 131 void AutofillPopupView::SelectPreviousLine() { |
| 109 int new_selected_line = selected_line_ - 1; | 132 int new_selected_line = selected_line_ - 1; |
| 110 | 133 |
| 111 if (new_selected_line <= kNoSelection) | 134 if (new_selected_line <= kNoSelection) |
| 112 new_selected_line = autofill_values_.size() - 1; | 135 new_selected_line = row_count() - 1; |
| 113 | 136 |
| 114 SetSelectedLine(new_selected_line); | 137 SetSelectedLine(new_selected_line); |
| 115 } | 138 } |
| 116 | 139 |
| 117 bool AutofillPopupView::AcceptSelectedLine() { | 140 bool AutofillPopupView::AcceptSelectedLine() { |
| 118 if (selected_line_ == kNoSelection) | 141 if (selected_line_ == kNoSelection) |
| 119 return false; | 142 return false; |
| 120 | 143 |
| 121 DCHECK_GE(selected_line_, 0); | 144 DCHECK_GE(selected_line_, 0); |
| 122 DCHECK_LT(selected_line_, static_cast<int>(autofill_values_.size())); | 145 DCHECK_LT(selected_line_, static_cast<int>(row_count())); |
| 123 | 146 |
| 124 return external_delegate()->DidAcceptAutofillSuggestions( | 147 return external_delegate()->DidAcceptAutofillSuggestions( |
| 125 autofill_values_[selected_line_], | 148 autofill_values_[RowToIndex(selected_line_)], |
| 126 autofill_unique_ids_[selected_line_], | 149 autofill_unique_ids_[RowToIndex(selected_line_)], |
| 127 selected_line_); | 150 selected_line_); |
| 128 } | 151 } |
| 129 | 152 |
| 130 bool AutofillPopupView::RemoveSelectedLine() { | 153 bool AutofillPopupView::RemoveSelectedLine() { |
| 131 if (selected_line_ == kNoSelection) | 154 if (selected_line_ == kNoSelection) |
| 132 return false; | 155 return false; |
| 133 | 156 |
| 134 DCHECK_GE(selected_line_, 0); | 157 DCHECK_GE(selected_line_, 0); |
| 135 DCHECK_LT(selected_line_, static_cast<int>(autofill_values_.size())); | 158 DCHECK_LT(selected_line_, static_cast<int>(row_count())); |
| 136 | 159 |
| 137 if (!CanDelete(autofill_unique_ids_[selected_line_])) | 160 int index = RowToIndex(selected_line_); |
| 161 |
| 162 if (!CanDelete(autofill_unique_ids_[index])) |
| 138 return false; | 163 return false; |
| 139 | 164 |
| 140 if (autofill_unique_ids_[selected_line_] > 0) { | 165 if (autofill_unique_ids_[index] > 0) { |
| 141 external_delegate()->RemoveAutofillProfileOrCreditCard( | 166 external_delegate()->RemoveAutofillProfileOrCreditCard( |
| 142 autofill_unique_ids_[selected_line_]); | 167 autofill_unique_ids_[index]); |
| 143 } else { | 168 } else { |
| 144 external_delegate()->RemoveAutocompleteEntry( | 169 external_delegate()->RemoveAutocompleteEntry( |
| 145 autofill_values_[selected_line_]); | 170 autofill_values_[index]); |
| 146 } | 171 } |
| 147 | 172 |
| 148 // Remove the deleted element. | 173 // Remove the deleted element. |
| 149 autofill_values_.erase(autofill_values_.begin() + selected_line_); | 174 autofill_values_.erase(autofill_values_.begin() + index); |
| 150 autofill_labels_.erase(autofill_labels_.begin() + selected_line_); | 175 autofill_labels_.erase(autofill_labels_.begin() + index); |
| 151 autofill_icons_.erase(autofill_icons_.begin() + selected_line_); | 176 autofill_icons_.erase(autofill_icons_.begin() + index); |
| 152 autofill_unique_ids_.erase(autofill_unique_ids_.begin() + selected_line_); | 177 autofill_unique_ids_.erase(autofill_unique_ids_.begin() + index); |
| 153 | 178 |
| 154 // Resize the popup. | 179 // Resize the popup. |
| 155 ResizePopup(); | 180 ResizePopup(); |
| 156 | 181 |
| 157 SetSelectedLine(kNoSelection); | 182 SetSelectedLine(kNoSelection); |
| 158 | 183 |
| 159 if (!HasAutofillEntries()) | 184 if (!HasAutofillEntries()) |
| 160 Hide(); | 185 Hide(); |
| 161 | 186 |
| 162 return true; | 187 return true; |
| 163 } | 188 } |
| 164 | 189 |
| 165 bool AutofillPopupView::IsSeparatorIndex(int index) { | |
| 166 // TODO(csharp): Use WebAutofillClient::MenuItemIDSeparator instead. | |
| 167 // http://crbug.com/125001 | |
| 168 return (index > 0 && autofill_unique_ids_[index - 1] >= 0 && | |
| 169 autofill_unique_ids_[index] < 0); | |
| 170 } | |
| 171 | |
| 172 int AutofillPopupView::GetIconResourceID(const string16& resource_name) { | 190 int AutofillPopupView::GetIconResourceID(const string16& resource_name) { |
| 173 for (size_t i = 0; i < arraysize(kDataResources); ++i) { | 191 for (size_t i = 0; i < arraysize(kDataResources); ++i) { |
| 174 if (resource_name == ASCIIToUTF16(kDataResources[i].name)) | 192 if (resource_name == ASCIIToUTF16(kDataResources[i].name)) |
| 175 return kDataResources[i].id; | 193 return kDataResources[i].id; |
| 176 } | 194 } |
| 177 | 195 |
| 178 return -1; | 196 return -1; |
| 179 } | 197 } |
| 180 | 198 |
| 199 int AutofillPopupView::RowToIndex(size_t row) { |
| 200 if (row_to_index_mapping_.size() > row) |
| 201 return row_to_index_mapping_[row]; |
| 202 |
| 203 return -1; |
| 204 } |
| 205 |
| 181 bool AutofillPopupView::CanDelete(int id) { | 206 bool AutofillPopupView::CanDelete(int id) { |
| 182 return id > 0 || | 207 return id > 0 || |
| 183 id == WebAutofillClient::MenuItemIDAutocompleteEntry || | 208 id == WebAutofillClient::MenuItemIDAutocompleteEntry || |
| 184 id == WebAutofillClient::MenuItemIDPasswordEntry; | 209 id == WebAutofillClient::MenuItemIDPasswordEntry; |
| 185 } | 210 } |
| 186 | 211 |
| 187 bool AutofillPopupView::HasAutofillEntries() { | 212 bool AutofillPopupView::HasAutofillEntries() { |
| 188 return autofill_values_.size() != 0 && | 213 return autofill_values_.size() != 0 && |
| 189 (autofill_unique_ids_[0] > 0 || | 214 (autofill_unique_ids_[0] > 0 || |
| 190 autofill_unique_ids_[0] == | 215 autofill_unique_ids_[0] == |
| 191 WebAutofillClient::MenuItemIDAutocompleteEntry || | 216 WebAutofillClient::MenuItemIDAutocompleteEntry || |
| 192 autofill_unique_ids_[0] == WebAutofillClient::MenuItemIDPasswordEntry || | 217 autofill_unique_ids_[0] == WebAutofillClient::MenuItemIDPasswordEntry || |
| 193 autofill_unique_ids_[0] == WebAutofillClient::MenuItemIDDataListEntry); | 218 autofill_unique_ids_[0] == WebAutofillClient::MenuItemIDDataListEntry); |
| 194 } | 219 } |
| 195 | 220 |
| 196 void AutofillPopupView::Observe(int type, | 221 void AutofillPopupView::Observe(int type, |
| 197 const content::NotificationSource& source, | 222 const content::NotificationSource& source, |
| 198 const content::NotificationDetails& details) { | 223 const content::NotificationDetails& details) { |
| 199 if (type == content::NOTIFICATION_WEB_CONTENTS_HIDDEN | 224 if (type == content::NOTIFICATION_WEB_CONTENTS_HIDDEN |
| 200 || type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) | 225 || type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) |
| 201 Hide(); | 226 Hide(); |
| 202 } | 227 } |
| OLD | NEW |