| 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/ui/autofill/autofill_popup_controller_impl.h" | 5 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 11 #include "base/i18n/rtl.h" |
| 11 #include "base/logging.h" | 12 #include "base/logging.h" |
| 12 #include "base/macros.h" | 13 #include "base/macros.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 14 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 15 #include "chrome/browser/ui/autofill/autofill_popup_view.h" | 16 #include "chrome/browser/ui/autofill/autofill_popup_view.h" |
| 17 #include "components/autofill/content/browser/content_autofill_driver.h" |
| 16 #include "components/autofill/core/browser/autofill_popup_delegate.h" | 18 #include "components/autofill/core/browser/autofill_popup_delegate.h" |
| 17 #include "components/autofill/core/browser/popup_item_ids.h" | 19 #include "components/autofill/core/browser/popup_item_ids.h" |
| 18 #include "components/autofill/core/browser/suggestion.h" | 20 #include "components/autofill/core/browser/suggestion.h" |
| 19 #include "content/public/browser/native_web_keyboard_event.h" | 21 #include "content/public/browser/native_web_keyboard_event.h" |
| 20 #include "ui/events/event.h" | 22 #include "ui/events/event.h" |
| 21 #include "ui/gfx/canvas.h" | 23 #include "ui/gfx/canvas.h" |
| 22 #include "ui/gfx/text_elider.h" | 24 #include "ui/gfx/text_elider.h" |
| 23 #include "ui/gfx/text_utils.h" | 25 #include "ui/gfx/text_utils.h" |
| 24 | 26 |
| 25 using base::WeakPtr; | 27 using base::WeakPtr; |
| 26 | 28 |
| 27 namespace autofill { | 29 namespace autofill { |
| 28 namespace { | 30 namespace { |
| 29 | 31 |
| 30 // Used to indicate that no line is currently selected by the user. | 32 // Used to indicate that no line is currently selected by the user. |
| 31 const int kNoSelection = -1; | 33 const int kNoSelection = -1; |
| 32 | 34 |
| 33 } // namespace | 35 } // namespace |
| 34 | 36 |
| 35 // static | 37 // static |
| 36 WeakPtr<AutofillPopupControllerImpl> AutofillPopupControllerImpl::GetOrCreate( | 38 WeakPtr<AutofillPopupControllerImpl> AutofillPopupControllerImpl::GetOrCreate( |
| 37 WeakPtr<AutofillPopupControllerImpl> previous, | 39 WeakPtr<AutofillPopupControllerImpl> previous, |
| 38 WeakPtr<AutofillPopupDelegate> delegate, | 40 WeakPtr<AutofillPopupDelegate> delegate, |
| 39 content::WebContents* web_contents, | 41 content::WebContents* web_contents, |
| 40 gfx::NativeView container_view, | 42 gfx::NativeView container_view, |
| 41 const gfx::RectF& element_bounds, | 43 const gfx::RectF& element_bounds, |
| 42 base::i18n::TextDirection text_direction) { | 44 base::i18n::TextDirection text_direction) { |
| 43 if (previous.get() && previous->web_contents() == web_contents && | 45 if (previous.get() && previous->delegate_.get() == delegate.get() && |
| 44 previous->delegate_.get() == delegate.get() && | |
| 45 previous->container_view() == container_view && | 46 previous->container_view() == container_view && |
| 46 previous->element_bounds() == element_bounds) { | 47 previous->element_bounds() == element_bounds) { |
| 47 previous->ClearState(); | 48 previous->ClearState(); |
| 48 return previous; | 49 return previous; |
| 49 } | 50 } |
| 50 | 51 |
| 51 if (previous.get()) | 52 if (previous.get()) |
| 52 previous->Hide(); | 53 previous->Hide(); |
| 53 | 54 |
| 54 AutofillPopupControllerImpl* controller = | 55 AutofillPopupControllerImpl* controller = |
| 55 new AutofillPopupControllerImpl( | 56 new AutofillPopupControllerImpl( |
| 56 delegate, web_contents, container_view, element_bounds, | 57 delegate, web_contents, container_view, element_bounds, |
| 57 text_direction); | 58 text_direction); |
| 58 return controller->GetWeakPtr(); | 59 return controller->GetWeakPtr(); |
| 59 } | 60 } |
| 60 | 61 |
| 61 AutofillPopupControllerImpl::AutofillPopupControllerImpl( | 62 AutofillPopupControllerImpl::AutofillPopupControllerImpl( |
| 62 base::WeakPtr<AutofillPopupDelegate> delegate, | 63 base::WeakPtr<AutofillPopupDelegate> delegate, |
| 63 content::WebContents* web_contents, | 64 content::WebContents* web_contents, |
| 64 gfx::NativeView container_view, | 65 gfx::NativeView container_view, |
| 65 const gfx::RectF& element_bounds, | 66 const gfx::RectF& element_bounds, |
| 66 base::i18n::TextDirection text_direction) | 67 base::i18n::TextDirection text_direction) |
| 67 : controller_common_(new PopupControllerCommon(element_bounds, | 68 : controller_common_(element_bounds, text_direction, container_view), |
| 68 text_direction, | |
| 69 container_view, | |
| 70 web_contents)), | |
| 71 view_(NULL), | 69 view_(NULL), |
| 72 layout_model_(this, delegate->IsCreditCardPopup()), | 70 layout_model_(this, delegate->IsCreditCardPopup()), |
| 73 delegate_(delegate), | 71 delegate_(delegate), |
| 74 weak_ptr_factory_(this) { | 72 weak_ptr_factory_(this) { |
| 75 ClearState(); | 73 ClearState(); |
| 76 controller_common_->SetKeyPressCallback( | |
| 77 base::Bind(&AutofillPopupControllerImpl::HandleKeyPressEvent, | |
| 78 base::Unretained(this))); | |
| 79 } | 74 } |
| 80 | 75 |
| 81 AutofillPopupControllerImpl::~AutofillPopupControllerImpl() {} | 76 AutofillPopupControllerImpl::~AutofillPopupControllerImpl() {} |
| 82 | 77 |
| 83 void AutofillPopupControllerImpl::Show( | 78 void AutofillPopupControllerImpl::Show( |
| 84 const std::vector<autofill::Suggestion>& suggestions) { | 79 const std::vector<autofill::Suggestion>& suggestions) { |
| 85 SetValues(suggestions); | 80 SetValues(suggestions); |
| 86 DCHECK_EQ(suggestions_.size(), elided_values_.size()); | 81 DCHECK_EQ(suggestions_.size(), elided_values_.size()); |
| 87 DCHECK_EQ(suggestions_.size(), elided_labels_.size()); | 82 DCHECK_EQ(suggestions_.size(), elided_labels_.size()); |
| 88 | 83 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 112 i, layout_model_.GetAvailableWidthForRow(i, has_label)); | 107 i, layout_model_.GetAvailableWidthForRow(i, has_label)); |
| 113 } | 108 } |
| 114 #endif | 109 #endif |
| 115 | 110 |
| 116 if (just_created) { | 111 if (just_created) { |
| 117 ShowView(); | 112 ShowView(); |
| 118 } else { | 113 } else { |
| 119 UpdateBoundsAndRedrawPopup(); | 114 UpdateBoundsAndRedrawPopup(); |
| 120 } | 115 } |
| 121 | 116 |
| 122 controller_common_->RegisterKeyPressCallback(); | 117 static_cast<ContentAutofillDriver*>(delegate_->GetAutofillDriver()) |
| 118 ->RegisterKeyPressHandler( |
| 119 base::Bind(&AutofillPopupControllerImpl::HandleKeyPressEvent, |
| 120 base::Unretained(this))); |
| 123 delegate_->OnPopupShown(); | 121 delegate_->OnPopupShown(); |
| 124 | 122 |
| 125 DCHECK_EQ(suggestions_.size(), elided_values_.size()); | 123 DCHECK_EQ(suggestions_.size(), elided_values_.size()); |
| 126 DCHECK_EQ(suggestions_.size(), elided_labels_.size()); | 124 DCHECK_EQ(suggestions_.size(), elided_labels_.size()); |
| 127 } | 125 } |
| 128 | 126 |
| 129 void AutofillPopupControllerImpl::UpdateDataListValues( | 127 void AutofillPopupControllerImpl::UpdateDataListValues( |
| 130 const std::vector<base::string16>& values, | 128 const std::vector<base::string16>& values, |
| 131 const std::vector<base::string16>& labels) { | 129 const std::vector<base::string16>& labels) { |
| 132 DCHECK_EQ(suggestions_.size(), elided_values_.size()); | 130 DCHECK_EQ(suggestions_.size(), elided_values_.size()); |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 elided_values_[i] = values[i]; | 182 elided_values_[i] = values[i]; |
| 185 elided_labels_[i] = labels[i]; | 183 elided_labels_[i] = labels[i]; |
| 186 } | 184 } |
| 187 | 185 |
| 188 UpdateBoundsAndRedrawPopup(); | 186 UpdateBoundsAndRedrawPopup(); |
| 189 DCHECK_EQ(suggestions_.size(), elided_values_.size()); | 187 DCHECK_EQ(suggestions_.size(), elided_values_.size()); |
| 190 DCHECK_EQ(suggestions_.size(), elided_labels_.size()); | 188 DCHECK_EQ(suggestions_.size(), elided_labels_.size()); |
| 191 } | 189 } |
| 192 | 190 |
| 193 void AutofillPopupControllerImpl::Hide() { | 191 void AutofillPopupControllerImpl::Hide() { |
| 194 controller_common_->RemoveKeyPressCallback(); | 192 if (delegate_) { |
| 195 if (delegate_) | |
| 196 delegate_->OnPopupHidden(); | 193 delegate_->OnPopupHidden(); |
| 194 static_cast<ContentAutofillDriver*>(delegate_->GetAutofillDriver()) |
| 195 ->RemoveKeyPressHandler(); |
| 196 } |
| 197 | 197 |
| 198 if (view_) | 198 if (view_) |
| 199 view_->Hide(); | 199 view_->Hide(); |
| 200 | 200 |
| 201 delete this; | 201 delete this; |
| 202 } | 202 } |
| 203 | 203 |
| 204 void AutofillPopupControllerImpl::ViewDestroyed() { | 204 void AutofillPopupControllerImpl::ViewDestroyed() { |
| 205 // The view has already been destroyed so clear the reference to it. | 205 // The view has already been destroyed so clear the reference to it. |
| 206 view_ = NULL; | 206 view_ = NULL; |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 void AutofillPopupControllerImpl::AcceptSuggestion(size_t index) { | 283 void AutofillPopupControllerImpl::AcceptSuggestion(size_t index) { |
| 284 const autofill::Suggestion& suggestion = suggestions_[index]; | 284 const autofill::Suggestion& suggestion = suggestions_[index]; |
| 285 delegate_->DidAcceptSuggestion(suggestion.value, suggestion.frontend_id, | 285 delegate_->DidAcceptSuggestion(suggestion.value, suggestion.frontend_id, |
| 286 index); | 286 index); |
| 287 } | 287 } |
| 288 | 288 |
| 289 gfx::Rect AutofillPopupControllerImpl::popup_bounds() const { | 289 gfx::Rect AutofillPopupControllerImpl::popup_bounds() const { |
| 290 return layout_model_.popup_bounds(); | 290 return layout_model_.popup_bounds(); |
| 291 } | 291 } |
| 292 | 292 |
| 293 content::WebContents* AutofillPopupControllerImpl::web_contents() { | |
| 294 return controller_common_->web_contents(); | |
| 295 } | |
| 296 | |
| 297 gfx::NativeView AutofillPopupControllerImpl::container_view() { | 293 gfx::NativeView AutofillPopupControllerImpl::container_view() { |
| 298 return controller_common_->container_view(); | 294 return controller_common_.container_view; |
| 299 } | 295 } |
| 300 | 296 |
| 301 const gfx::RectF& AutofillPopupControllerImpl::element_bounds() const { | 297 const gfx::RectF& AutofillPopupControllerImpl::element_bounds() const { |
| 302 return controller_common_->element_bounds(); | 298 return controller_common_.element_bounds; |
| 303 } | 299 } |
| 304 | 300 |
| 305 bool AutofillPopupControllerImpl::IsRTL() const { | 301 bool AutofillPopupControllerImpl::IsRTL() const { |
| 306 return controller_common_->is_rtl(); | 302 return controller_common_.text_direction == base::i18n::RIGHT_TO_LEFT; |
| 307 } | 303 } |
| 308 | 304 |
| 309 const std::vector<autofill::Suggestion> | 305 const std::vector<autofill::Suggestion> |
| 310 AutofillPopupControllerImpl::GetSuggestions() { | 306 AutofillPopupControllerImpl::GetSuggestions() { |
| 311 return suggestions_; | 307 return suggestions_; |
| 312 } | 308 } |
| 313 | 309 |
| 314 #if !defined(OS_ANDROID) | 310 #if !defined(OS_ANDROID) |
| 315 int AutofillPopupControllerImpl::GetElidedValueWidthForRow(size_t row) { | 311 int AutofillPopupControllerImpl::GetElidedValueWidthForRow(size_t row) { |
| 316 return gfx::GetStringWidth(GetElidedValueAt(row), | 312 return gfx::GetStringWidth(GetElidedValueAt(row), |
| (...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 528 // Don't clear view_, because otherwise the popup will have to get regenerated | 524 // Don't clear view_, because otherwise the popup will have to get regenerated |
| 529 // and this will cause flickering. | 525 // and this will cause flickering. |
| 530 suggestions_.clear(); | 526 suggestions_.clear(); |
| 531 elided_values_.clear(); | 527 elided_values_.clear(); |
| 532 elided_labels_.clear(); | 528 elided_labels_.clear(); |
| 533 | 529 |
| 534 selected_line_ = kNoSelection; | 530 selected_line_ = kNoSelection; |
| 535 } | 531 } |
| 536 | 532 |
| 537 } // namespace autofill | 533 } // namespace autofill |
| OLD | NEW |