| 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 "chrome/browser/autofill/autofill_external_delegate.h" | 8 #include "chrome/browser/autofill/autofill_external_delegate.h" |
| 9 #include "content/public/browser/web_contents.h" | 9 #include "content/public/browser/web_contents.h" |
| 10 #include "content/public/browser/navigation_controller.h" | 10 #include "content/public/browser/navigation_controller.h" |
| 11 #include "content/public/browser/notification_service.h" | 11 #include "content/public/browser/notification_service.h" |
| 12 #include "content/public/browser/notification_source.h" | 12 #include "content/public/browser/notification_source.h" |
| 13 #include "content/public/browser/notification_types.h" | 13 #include "content/public/browser/notification_types.h" |
| 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" |
| 15 |
| 16 using WebKit::WebAutofillClient; |
| 14 | 17 |
| 15 namespace { | 18 namespace { |
| 16 | 19 |
| 17 // Used to indicate that no line is currently selected by the user. | 20 // Used to indicate that no line is currently selected by the user. |
| 18 const int kNoSelection = -1; | 21 const int kNoSelection = -1; |
| 19 | 22 |
| 20 } // end namespace | 23 } // end namespace |
| 21 | 24 |
| 22 AutofillPopupView::AutofillPopupView( | 25 AutofillPopupView::AutofillPopupView( |
| 23 content::WebContents* web_contents, | 26 content::WebContents* web_contents, |
| 24 AutofillExternalDelegate* external_delegate) | 27 AutofillExternalDelegate* external_delegate) |
| 25 : external_delegate_(external_delegate), | 28 : external_delegate_(external_delegate), |
| 26 separator_index_(0), | |
| 27 selected_line_(kNoSelection) { | 29 selected_line_(kNoSelection) { |
| 28 if (!web_contents) | 30 if (!web_contents) |
| 29 return; | 31 return; |
| 30 | 32 |
| 31 registrar_.Add(this, | 33 registrar_.Add(this, |
| 32 content::NOTIFICATION_WEB_CONTENTS_HIDDEN, | 34 content::NOTIFICATION_WEB_CONTENTS_HIDDEN, |
| 33 content::Source<content::WebContents>(web_contents)); | 35 content::Source<content::WebContents>(web_contents)); |
| 34 registrar_.Add( | 36 registrar_.Add( |
| 35 this, | 37 this, |
| 36 content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 38 content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 37 content::Source<content::NavigationController>( | 39 content::Source<content::NavigationController>( |
| 38 &(web_contents->GetController()))); | 40 &(web_contents->GetController()))); |
| 39 } | 41 } |
| 40 | 42 |
| 41 AutofillPopupView::~AutofillPopupView() {} | 43 AutofillPopupView::~AutofillPopupView() {} |
| 42 | 44 |
| 43 void AutofillPopupView::Hide() { | 45 void AutofillPopupView::Hide() { |
| 44 HideInternal(); | 46 HideInternal(); |
| 45 | 47 |
| 46 external_delegate_->ClearPreviewedForm(); | 48 external_delegate_->ClearPreviewedForm(); |
| 47 } | 49 } |
| 48 | 50 |
| 49 void AutofillPopupView::Show(const std::vector<string16>& autofill_values, | 51 void AutofillPopupView::Show(const std::vector<string16>& autofill_values, |
| 50 const std::vector<string16>& autofill_labels, | 52 const std::vector<string16>& autofill_labels, |
| 51 const std::vector<string16>& autofill_icons, | 53 const std::vector<string16>& autofill_icons, |
| 52 const std::vector<int>& autofill_unique_ids, | 54 const std::vector<int>& autofill_unique_ids) { |
| 53 int separator_index) { | |
| 54 autofill_values_ = autofill_values; | 55 autofill_values_ = autofill_values; |
| 55 autofill_labels_ = autofill_labels; | 56 autofill_labels_ = autofill_labels; |
| 56 autofill_icons_ = autofill_icons; | 57 autofill_icons_ = autofill_icons; |
| 57 autofill_unique_ids_ = autofill_unique_ids; | 58 autofill_unique_ids_ = autofill_unique_ids; |
| 58 | 59 |
| 59 separator_index_ = separator_index; | |
| 60 | |
| 61 ShowInternal(); | 60 ShowInternal(); |
| 62 } | 61 } |
| 63 | 62 |
| 64 void AutofillPopupView::SetSelectedLine(int selected_line) { | 63 void AutofillPopupView::SetSelectedLine(int selected_line) { |
| 65 if (selected_line_ == selected_line) | 64 if (selected_line_ == selected_line) |
| 66 return; | 65 return; |
| 67 | 66 |
| 68 if (selected_line_ != kNoSelection) | 67 if (selected_line_ != kNoSelection) |
| 69 InvalidateRow(selected_line_); | 68 InvalidateRow(selected_line_); |
| 70 | 69 |
| 71 if (selected_line != kNoSelection) | 70 if (selected_line != kNoSelection) |
| 72 InvalidateRow(selected_line); | 71 InvalidateRow(selected_line); |
| 73 | 72 |
| 74 selected_line_ = selected_line; | 73 selected_line_ = selected_line; |
| 75 | 74 |
| 76 if (selected_line_ != kNoSelection) { | 75 if (selected_line_ != kNoSelection) { |
| 77 external_delegate_->SelectAutofillSuggestionAtIndex( | 76 external_delegate_->SelectAutofillSuggestionAtIndex( |
| 78 autofill_unique_ids_[selected_line_], | 77 autofill_unique_ids_[selected_line_]); |
| 79 selected_line_); | |
| 80 } | 78 } |
| 81 } | 79 } |
| 82 | 80 |
| 83 void AutofillPopupView::SelectNextLine() { | 81 void AutofillPopupView::SelectNextLine() { |
| 84 int new_selected_line = selected_line_ + 1; | 82 int new_selected_line = selected_line_ + 1; |
| 85 | 83 |
| 86 if (new_selected_line == static_cast<int>(autofill_values_.size())) | 84 if (new_selected_line == static_cast<int>(autofill_values_.size())) |
| 87 new_selected_line = 0; | 85 new_selected_line = 0; |
| 88 | 86 |
| 89 SetSelectedLine(new_selected_line); | 87 SetSelectedLine(new_selected_line); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 108 return external_delegate()->DidAcceptAutofillSuggestions( | 106 return external_delegate()->DidAcceptAutofillSuggestions( |
| 109 autofill_values_[selected_line_], | 107 autofill_values_[selected_line_], |
| 110 autofill_unique_ids_[selected_line_], | 108 autofill_unique_ids_[selected_line_], |
| 111 selected_line_); | 109 selected_line_); |
| 112 } | 110 } |
| 113 | 111 |
| 114 bool AutofillPopupView::RemoveSelectedLine() { | 112 bool AutofillPopupView::RemoveSelectedLine() { |
| 115 if (selected_line_ == kNoSelection) | 113 if (selected_line_ == kNoSelection) |
| 116 return false; | 114 return false; |
| 117 | 115 |
| 118 // TODO(csharp) add removal code. | 116 DCHECK_GE(selected_line_, 0); |
| 119 return false; | 117 DCHECK_LT(selected_line_, static_cast<int>(autofill_values_.size())); |
| 118 |
| 119 if (!CanDelete(autofill_unique_ids_[selected_line_])) |
| 120 return false; |
| 121 |
| 122 if (autofill_unique_ids_[selected_line_] > 0) { |
| 123 external_delegate()->RemoveAutofillProfileOrCreditCard( |
| 124 autofill_unique_ids_[selected_line_]); |
| 125 } else { |
| 126 external_delegate()->RemoveAutocompleteEntry( |
| 127 autofill_values_[selected_line_]); |
| 128 } |
| 129 |
| 130 // Remove the deleted element. |
| 131 autofill_values_.erase(autofill_values_.begin() + selected_line_); |
| 132 autofill_labels_.erase(autofill_labels_.begin() + selected_line_); |
| 133 autofill_icons_.erase(autofill_icons_.begin() + selected_line_); |
| 134 autofill_unique_ids_.erase(autofill_unique_ids_.begin() + selected_line_); |
| 135 |
| 136 // Resize the popup. |
| 137 ResizePopup(); |
| 138 |
| 139 SetSelectedLine(kNoSelection); |
| 140 |
| 141 if (!HasAutofillEntries()) |
| 142 Hide(); |
| 143 |
| 144 return true; |
| 145 } |
| 146 |
| 147 bool AutofillPopupView::IsSeparatorIndex(int index) { |
| 148 // TODO(csharp): Use WebAutofillClient::MenuItemIDSeparator instead. |
| 149 // http://crbug.com/125001 |
| 150 return (index > 0 && autofill_unique_ids_[index - 1] >= 0 && |
| 151 autofill_unique_ids_[index] < 0); |
| 152 } |
| 153 |
| 154 bool AutofillPopupView::CanDelete(int id) { |
| 155 return id > 0 || |
| 156 id == WebAutofillClient::MenuItemIDAutocompleteEntry || |
| 157 id == WebAutofillClient::MenuItemIDPasswordEntry; |
| 158 } |
| 159 |
| 160 bool AutofillPopupView::HasAutofillEntries() { |
| 161 return autofill_values_.size() != 0 && |
| 162 (autofill_unique_ids_[0] > 0 || |
| 163 autofill_unique_ids_[0] == |
| 164 WebAutofillClient::MenuItemIDAutocompleteEntry || |
| 165 autofill_unique_ids_[0] == WebAutofillClient::MenuItemIDPasswordEntry || |
| 166 autofill_unique_ids_[0] == WebAutofillClient::MenuItemIDDataListEntry); |
| 120 } | 167 } |
| 121 | 168 |
| 122 void AutofillPopupView::Observe(int type, | 169 void AutofillPopupView::Observe(int type, |
| 123 const content::NotificationSource& source, | 170 const content::NotificationSource& source, |
| 124 const content::NotificationDetails& details) { | 171 const content::NotificationDetails& details) { |
| 125 if (type == content::NOTIFICATION_WEB_CONTENTS_HIDDEN | 172 if (type == content::NOTIFICATION_WEB_CONTENTS_HIDDEN |
| 126 || type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) | 173 || type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) |
| 127 Hide(); | 174 Hide(); |
| 128 } | 175 } |
| OLD | NEW |