| 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/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/browser/ui/autofill/autofill_popup_delegate.h" | 12 #include "chrome/browser/ui/autofill/autofill_popup_delegate.h" |
| 13 #include "chrome/browser/ui/autofill/autofill_popup_view.h" | 13 #include "chrome/browser/ui/autofill/autofill_popup_view.h" |
| 14 #include "content/public/browser/native_web_keyboard_event.h" | 14 #include "content/public/browser/native_web_keyboard_event.h" |
| 15 #include "grit/webkit_resources.h" | 15 #include "grit/webkit_resources.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" |
| 17 #include "ui/base/events/event.h" | 17 #include "ui/base/events/event.h" |
| 18 #include "ui/base/text/text_elider.h" | 18 #include "ui/base/text/text_elider.h" |
| 19 #include "ui/gfx/display.h" | 19 #include "ui/gfx/display.h" |
| 20 #include "ui/gfx/rect_conversions.h" | 20 #include "ui/gfx/rect_conversions.h" |
| 21 #include "ui/gfx/screen.h" | 21 #include "ui/gfx/screen.h" |
| 22 #include "ui/gfx/vector2d.h" | 22 #include "ui/gfx/vector2d.h" |
| 23 | 23 |
| 24 using base::WeakPtr; |
| 24 using WebKit::WebAutofillClient; | 25 using WebKit::WebAutofillClient; |
| 25 | 26 |
| 26 namespace { | 27 namespace { |
| 27 | 28 |
| 28 // Used to indicate that no line is currently selected by the user. | 29 // Used to indicate that no line is currently selected by the user. |
| 29 const int kNoSelection = -1; | 30 const int kNoSelection = -1; |
| 30 | 31 |
| 31 // Size difference between name and subtext in pixels. | 32 // Size difference between name and subtext in pixels. |
| 32 const int kLabelFontSizeDelta = -2; | 33 const int kLabelFontSizeDelta = -2; |
| 33 | 34 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 64 { "genericCC", IDR_AUTOFILL_CC_GENERIC }, | 65 { "genericCC", IDR_AUTOFILL_CC_GENERIC }, |
| 65 { "jcbCC", IDR_AUTOFILL_CC_JCB }, | 66 { "jcbCC", IDR_AUTOFILL_CC_JCB }, |
| 66 { "masterCardCC", IDR_AUTOFILL_CC_MASTERCARD }, | 67 { "masterCardCC", IDR_AUTOFILL_CC_MASTERCARD }, |
| 67 { "soloCC", IDR_AUTOFILL_CC_SOLO }, | 68 { "soloCC", IDR_AUTOFILL_CC_SOLO }, |
| 68 { "visaCC", IDR_AUTOFILL_CC_VISA }, | 69 { "visaCC", IDR_AUTOFILL_CC_VISA }, |
| 69 }; | 70 }; |
| 70 | 71 |
| 71 } // end namespace | 72 } // end namespace |
| 72 | 73 |
| 73 // static | 74 // static |
| 74 AutofillPopupControllerImpl* AutofillPopupControllerImpl::GetOrCreate( | 75 WeakPtr<AutofillPopupControllerImpl> AutofillPopupControllerImpl::GetOrCreate( |
| 75 AutofillPopupControllerImpl* previous, | 76 WeakPtr<AutofillPopupControllerImpl> previous, |
| 76 AutofillPopupDelegate* delegate, | 77 AutofillPopupDelegate* delegate, |
| 77 gfx::NativeView container_view, | 78 gfx::NativeView container_view, |
| 78 const gfx::RectF& element_bounds) { | 79 const gfx::RectF& element_bounds) { |
| 79 DCHECK(!previous || previous->delegate_ == delegate); | 80 DCHECK(!previous || previous->delegate_ == delegate); |
| 80 | 81 |
| 81 if (previous && | 82 if (previous && |
| 82 previous->container_view() == container_view && | 83 previous->container_view() == container_view && |
| 83 previous->element_bounds() == element_bounds) { | 84 previous->element_bounds() == element_bounds) { |
| 84 return previous; | 85 return previous; |
| 85 } | 86 } |
| 86 | 87 |
| 87 if (previous) | 88 if (previous) |
| 88 previous->Hide(); | 89 previous->Hide(); |
| 89 | 90 |
| 90 return new AutofillPopupControllerImpl( | 91 AutofillPopupControllerImpl* controller = |
| 91 delegate, container_view, element_bounds); | 92 new AutofillPopupControllerImpl(delegate, container_view, element_bounds); |
| 93 return controller->GetWeakPtr(); |
| 92 } | 94 } |
| 93 | 95 |
| 94 AutofillPopupControllerImpl::AutofillPopupControllerImpl( | 96 AutofillPopupControllerImpl::AutofillPopupControllerImpl( |
| 95 AutofillPopupDelegate* delegate, | 97 AutofillPopupDelegate* delegate, |
| 96 gfx::NativeView container_view, | 98 gfx::NativeView container_view, |
| 97 const gfx::RectF& element_bounds) | 99 const gfx::RectF& element_bounds) |
| 98 : view_(NULL), | 100 : view_(NULL), |
| 99 delegate_(delegate), | 101 delegate_(delegate), |
| 100 container_view_(container_view), | 102 container_view_(container_view), |
| 101 element_bounds_(element_bounds), | 103 element_bounds_(element_bounds), |
| 102 selected_line_(kNoSelection), | 104 selected_line_(kNoSelection), |
| 103 delete_icon_hovered_(false), | 105 delete_icon_hovered_(false), |
| 104 is_hiding_(false), | 106 is_hiding_(false), |
| 105 inform_delegate_of_destruction_(true) { | 107 inform_delegate_of_destruction_(true), |
| 108 weak_ptr_factory_(this) { |
| 106 #if !defined(OS_ANDROID) | 109 #if !defined(OS_ANDROID) |
| 107 subtext_font_ = name_font_.DeriveFont(kLabelFontSizeDelta); | 110 subtext_font_ = name_font_.DeriveFont(kLabelFontSizeDelta); |
| 108 #endif | 111 #endif |
| 109 } | 112 } |
| 110 | 113 |
| 111 AutofillPopupControllerImpl::~AutofillPopupControllerImpl() { | 114 AutofillPopupControllerImpl::~AutofillPopupControllerImpl() {} |
| 112 if (inform_delegate_of_destruction_) | |
| 113 delegate_->ControllerDestroyed(); | |
| 114 } | |
| 115 | 115 |
| 116 void AutofillPopupControllerImpl::Show( | 116 void AutofillPopupControllerImpl::Show( |
| 117 const std::vector<string16>& names, | 117 const std::vector<string16>& names, |
| 118 const std::vector<string16>& subtexts, | 118 const std::vector<string16>& subtexts, |
| 119 const std::vector<string16>& icons, | 119 const std::vector<string16>& icons, |
| 120 const std::vector<int>& identifiers) { | 120 const std::vector<int>& identifiers) { |
| 121 names_ = names; | 121 names_ = names; |
| 122 full_names_ = names; | 122 full_names_ = names; |
| 123 subtexts_ = subtexts; | 123 subtexts_ = subtexts; |
| 124 icons_ = icons; | 124 icons_ = icons; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 ui::ELIDE_AT_END); | 158 ui::ELIDE_AT_END); |
| 159 } | 159 } |
| 160 #endif | 160 #endif |
| 161 | 161 |
| 162 if (!view_) { | 162 if (!view_) { |
| 163 view_ = AutofillPopupView::Create(this); | 163 view_ = AutofillPopupView::Create(this); |
| 164 ShowView(); | 164 ShowView(); |
| 165 } else { | 165 } else { |
| 166 UpdateBoundsAndRedrawPopup(); | 166 UpdateBoundsAndRedrawPopup(); |
| 167 } | 167 } |
| 168 |
| 169 delegate_->OnPopupShown(this); |
| 168 } | 170 } |
| 169 | 171 |
| 170 void AutofillPopupControllerImpl::Hide() { | 172 void AutofillPopupControllerImpl::Hide() { |
| 171 inform_delegate_of_destruction_ = false; | 173 inform_delegate_of_destruction_ = false; |
| 172 HideInternal(); | 174 HideInternal(); |
| 173 } | 175 } |
| 174 | 176 |
| 175 bool AutofillPopupControllerImpl::HandleKeyPressEvent( | 177 bool AutofillPopupControllerImpl::HandleKeyPressEvent( |
| 176 const content::NativeWebKeyboardEvent& event) { | 178 const content::NativeWebKeyboardEvent& event) { |
| 177 switch (event.windowsKeyCode) { | 179 switch (event.windowsKeyCode) { |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 326 return delete_icon_hovered_; | 328 return delete_icon_hovered_; |
| 327 } | 329 } |
| 328 | 330 |
| 329 void AutofillPopupControllerImpl::HideInternal() { | 331 void AutofillPopupControllerImpl::HideInternal() { |
| 330 if (is_hiding_) | 332 if (is_hiding_) |
| 331 return; | 333 return; |
| 332 is_hiding_ = true; | 334 is_hiding_ = true; |
| 333 | 335 |
| 334 SetSelectedLine(kNoSelection); | 336 SetSelectedLine(kNoSelection); |
| 335 | 337 |
| 338 if (inform_delegate_of_destruction_) |
| 339 delegate_->OnPopupHidden(this); |
| 340 |
| 336 if (view_) | 341 if (view_) |
| 337 view_->Hide(); | 342 view_->Hide(); |
| 338 else | 343 else |
| 339 delete this; | 344 delete this; |
| 340 } | 345 } |
| 341 | 346 |
| 342 void AutofillPopupControllerImpl::SetSelectedLine(int selected_line) { | 347 void AutofillPopupControllerImpl::SetSelectedLine(int selected_line) { |
| 343 if (selected_line_ == selected_line) | 348 if (selected_line_ == selected_line) |
| 344 return; | 349 return; |
| 345 | 350 |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 573 std::pair<int, int> popup_y_and_height = CalculatePopupYAndHeight( | 578 std::pair<int, int> popup_y_and_height = CalculatePopupYAndHeight( |
| 574 top_left_display, bottom_right_display, popup_height); | 579 top_left_display, bottom_right_display, popup_height); |
| 575 | 580 |
| 576 popup_bounds_ = gfx::Rect(popup_x_and_width.first, | 581 popup_bounds_ = gfx::Rect(popup_x_and_width.first, |
| 577 popup_y_and_height.first, | 582 popup_y_and_height.first, |
| 578 popup_x_and_width.second, | 583 popup_x_and_width.second, |
| 579 popup_y_and_height.second); | 584 popup_y_and_height.second); |
| 580 } | 585 } |
| 581 #endif // !defined(OS_ANDROID) | 586 #endif // !defined(OS_ANDROID) |
| 582 | 587 |
| 588 WeakPtr<AutofillPopupControllerImpl> AutofillPopupControllerImpl::GetWeakPtr() { |
| 589 return weak_ptr_factory_.GetWeakPtr(); |
| 590 } |
| 591 |
| 583 const gfx::Rect AutofillPopupControllerImpl::RoundedElementBounds() const { | 592 const gfx::Rect AutofillPopupControllerImpl::RoundedElementBounds() const { |
| 584 return gfx::ToNearestRect(element_bounds_); | 593 return gfx::ToNearestRect(element_bounds_); |
| 585 } | 594 } |
| 586 | 595 |
| 587 gfx::Display AutofillPopupControllerImpl::GetDisplayNearestPoint( | 596 gfx::Display AutofillPopupControllerImpl::GetDisplayNearestPoint( |
| 588 const gfx::Point& point) const { | 597 const gfx::Point& point) const { |
| 589 return gfx::Screen::GetScreenFor(container_view())->GetDisplayNearestPoint( | 598 return gfx::Screen::GetScreenFor(container_view())->GetDisplayNearestPoint( |
| 590 point); | 599 point); |
| 591 } | 600 } |
| 592 | 601 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 647 if (bottom_available >= popup_required_height || | 656 if (bottom_available >= popup_required_height || |
| 648 bottom_available >= top_available) { | 657 bottom_available >= top_available) { |
| 649 // The popup can appear below the field. | 658 // The popup can appear below the field. |
| 650 return std::make_pair(bottom_growth_start, popup_required_height); | 659 return std::make_pair(bottom_growth_start, popup_required_height); |
| 651 } else { | 660 } else { |
| 652 // The popup must appear above the field. | 661 // The popup must appear above the field. |
| 653 return std::make_pair(top_growth_end - popup_required_height, | 662 return std::make_pair(top_growth_end - popup_required_height, |
| 654 popup_required_height); | 663 popup_required_height); |
| 655 } | 664 } |
| 656 } | 665 } |
| OLD | NEW |