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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/autofill/autofill_popup_controller_impl.h"
10 #include "base/logging.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/ui/autofill/autofill_popup_view.h"
13 #include "chrome/browser/ui/autofill/popup_constants.h"
14 #include "components/autofill/core/browser/autofill_popup_delegate.h"
15 #include "components/autofill/core/browser/popup_item_ids.h"
16 #include "components/autofill/core/browser/suggestion.h"
17 #include "content/public/browser/native_web_keyboard_event.h"
18 #include "grit/components_scaled_resources.h"
19 #include "ui/base/resource/resource_bundle.h"
20 #include "ui/events/event.h"
21 #include "ui/gfx/geometry/rect_conversions.h"
22 #include "ui/gfx/geometry/vector2d.h"
23 #include "ui/gfx/screen.h"
24 #include "ui/gfx/text_elider.h"
25 #include "ui/gfx/text_utils.h"
32 // Used to indicate that no line is currently selected by the user.
33 const int kNoSelection
= -1;
35 // The vertical height of each row in pixels.
36 const size_t kRowHeight
= 24;
38 // The vertical height of a separator in pixels.
39 const size_t kSeparatorHeight
= 1;
41 #if !defined(OS_ANDROID)
42 // Size difference between name and label in pixels.
43 const int kLabelFontSizeDelta
= -2;
45 const size_t kNamePadding
= AutofillPopupView::kNamePadding
;
46 const size_t kIconPadding
= AutofillPopupView::kIconPadding
;
47 const size_t kEndPadding
= AutofillPopupView::kEndPadding
;
55 const DataResource kDataResources
[] = {
56 { "americanExpressCC", IDR_AUTOFILL_CC_AMEX
},
57 { "dinersCC", IDR_AUTOFILL_CC_GENERIC
},
58 { "discoverCC", IDR_AUTOFILL_CC_DISCOVER
},
59 { "genericCC", IDR_AUTOFILL_CC_GENERIC
},
60 { "jcbCC", IDR_AUTOFILL_CC_GENERIC
},
61 { "masterCardCC", IDR_AUTOFILL_CC_MASTERCARD
},
62 { "visaCC", IDR_AUTOFILL_CC_VISA
},
63 { "scanCreditCardIcon", IDR_AUTOFILL_CC_SCAN_NEW
},
64 #if defined(OS_MACOSX) && !defined(OS_IOS)
65 { "macContactsIcon", IDR_AUTOFILL_MAC_CONTACTS_ICON
},
66 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
72 WeakPtr
<AutofillPopupControllerImpl
> AutofillPopupControllerImpl::GetOrCreate(
73 WeakPtr
<AutofillPopupControllerImpl
> previous
,
74 WeakPtr
<AutofillPopupDelegate
> delegate
,
75 content::WebContents
* web_contents
,
76 gfx::NativeView container_view
,
77 const gfx::RectF
& element_bounds
,
78 base::i18n::TextDirection text_direction
) {
79 if (previous
.get() && previous
->web_contents() == web_contents
&&
80 previous
->delegate_
.get() == delegate
.get() &&
81 previous
->container_view() == container_view
&&
82 previous
->element_bounds() == element_bounds
) {
83 previous
->ClearState();
90 AutofillPopupControllerImpl
* controller
=
91 new AutofillPopupControllerImpl(
92 delegate
, web_contents
, container_view
, element_bounds
,
94 return controller
->GetWeakPtr();
97 AutofillPopupControllerImpl::AutofillPopupControllerImpl(
98 base::WeakPtr
<AutofillPopupDelegate
> delegate
,
99 content::WebContents
* web_contents
,
100 gfx::NativeView container_view
,
101 const gfx::RectF
& element_bounds
,
102 base::i18n::TextDirection text_direction
)
103 : controller_common_(new PopupControllerCommon(element_bounds
,
109 weak_ptr_factory_(this) {
111 controller_common_
->SetKeyPressCallback(
112 base::Bind(&AutofillPopupControllerImpl::HandleKeyPressEvent
,
113 base::Unretained(this)));
114 #if !defined(OS_ANDROID)
115 label_font_list_
= value_font_list_
.DeriveWithSizeDelta(kLabelFontSizeDelta
);
116 title_font_list_
= value_font_list_
.DeriveWithStyle(gfx::Font::BOLD
);
117 #if defined(OS_MACOSX)
118 // There is no italic version of the system font.
119 warning_font_list_
= value_font_list_
;
121 warning_font_list_
= value_font_list_
.DeriveWithStyle(gfx::Font::ITALIC
);
126 AutofillPopupControllerImpl::~AutofillPopupControllerImpl() {}
128 void AutofillPopupControllerImpl::Show(
129 const std::vector
<autofill::Suggestion
>& suggestions
) {
130 SetValues(suggestions
);
131 DCHECK_EQ(suggestions_
.size(), elided_values_
.size());
132 DCHECK_EQ(suggestions_
.size(), elided_labels_
.size());
134 #if !defined(OS_ANDROID)
135 // Android displays the long text with ellipsis using the view attributes.
138 int popup_width
= popup_bounds().width();
140 // Elide the name and label strings so that the popup fits in the available
142 for (size_t i
= 0; i
< suggestions_
.size(); ++i
) {
144 gfx::GetStringWidth(suggestions_
[i
].value
, GetValueFontListForRow(i
));
146 gfx::GetStringWidth(suggestions_
[i
].label
, GetLabelFontList());
147 int total_text_length
= value_width
+ label_width
;
149 // The line can have no strings if it represents a UI element, such as
151 if (total_text_length
== 0)
154 int available_width
= popup_width
- RowWidthWithoutText(i
);
156 // Each field receives space in proportion to its length.
157 int value_size
= available_width
* value_width
/ total_text_length
;
158 elided_values_
[i
] = gfx::ElideText(suggestions_
[i
].value
,
159 GetValueFontListForRow(i
),
160 value_size
, gfx::ELIDE_TAIL
);
162 int label_size
= available_width
* label_width
/ total_text_length
;
163 elided_labels_
[i
] = gfx::ElideText(suggestions_
[i
].label
,
165 label_size
, gfx::ELIDE_TAIL
);
170 view_
= AutofillPopupView::Create(this);
172 // It is possible to fail to create the popup, in this case
173 // treat the popup as hiding right away.
181 UpdateBoundsAndRedrawPopup();
184 controller_common_
->RegisterKeyPressCallback();
185 delegate_
->OnPopupShown();
187 DCHECK_EQ(suggestions_
.size(), elided_values_
.size());
188 DCHECK_EQ(suggestions_
.size(), elided_labels_
.size());
191 void AutofillPopupControllerImpl::UpdateDataListValues(
192 const std::vector
<base::string16
>& values
,
193 const std::vector
<base::string16
>& labels
) {
194 DCHECK_EQ(suggestions_
.size(), elided_values_
.size());
195 DCHECK_EQ(suggestions_
.size(), elided_labels_
.size());
197 // Remove all the old data list values, which should always be at the top of
198 // the list if they are present.
199 while (!suggestions_
.empty() &&
200 suggestions_
[0].frontend_id
== POPUP_ITEM_ID_DATALIST_ENTRY
) {
201 suggestions_
.erase(suggestions_
.begin());
202 elided_values_
.erase(elided_values_
.begin());
203 elided_labels_
.erase(elided_labels_
.begin());
206 // If there are no new data list values, exit (clearing the separator if there
208 if (values
.empty()) {
209 if (!suggestions_
.empty() &&
210 suggestions_
[0].frontend_id
== POPUP_ITEM_ID_SEPARATOR
) {
211 suggestions_
.erase(suggestions_
.begin());
212 elided_values_
.erase(elided_values_
.begin());
213 elided_labels_
.erase(elided_labels_
.begin());
216 // The popup contents have changed, so either update the bounds or hide it.
217 if (HasSuggestions())
218 UpdateBoundsAndRedrawPopup();
225 // Add a separator if there are any other values.
226 if (!suggestions_
.empty() &&
227 suggestions_
[0].frontend_id
!= POPUP_ITEM_ID_SEPARATOR
) {
228 suggestions_
.insert(suggestions_
.begin(), autofill::Suggestion());
229 suggestions_
[0].frontend_id
= POPUP_ITEM_ID_SEPARATOR
;
230 elided_values_
.insert(elided_values_
.begin(), base::string16());
231 elided_labels_
.insert(elided_labels_
.begin(), base::string16());
234 // Prepend the parameters to the suggestions we already have.
235 suggestions_
.insert(suggestions_
.begin(), values
.size(), Suggestion());
236 elided_values_
.insert(elided_values_
.begin(), values
.size(),
238 elided_labels_
.insert(elided_labels_
.begin(), values
.size(),
240 for (size_t i
= 0; i
< values
.size(); i
++) {
241 suggestions_
[i
].value
= values
[i
];
242 suggestions_
[i
].label
= labels
[i
];
243 suggestions_
[i
].frontend_id
= POPUP_ITEM_ID_DATALIST_ENTRY
;
245 // TODO(brettw) it looks like these should be elided.
246 elided_values_
[i
] = values
[i
];
247 elided_labels_
[i
] = labels
[i
];
250 UpdateBoundsAndRedrawPopup();
251 DCHECK_EQ(suggestions_
.size(), elided_values_
.size());
252 DCHECK_EQ(suggestions_
.size(), elided_labels_
.size());
255 void AutofillPopupControllerImpl::Hide() {
256 controller_common_
->RemoveKeyPressCallback();
258 delegate_
->OnPopupHidden();
266 void AutofillPopupControllerImpl::ViewDestroyed() {
267 // The view has already been destroyed so clear the reference to it.
273 bool AutofillPopupControllerImpl::HandleKeyPressEvent(
274 const content::NativeWebKeyboardEvent
& event
) {
275 switch (event
.windowsKeyCode
) {
277 SelectPreviousLine();
282 case ui::VKEY_PRIOR
: // Page up.
283 // Set no line and then select the next line in case the first line is not
285 SetSelectedLine(kNoSelection
);
288 case ui::VKEY_NEXT
: // Page down.
289 SetSelectedLine(GetLineCount() - 1);
291 case ui::VKEY_ESCAPE
:
294 case ui::VKEY_DELETE
:
295 return (event
.modifiers
& content::NativeWebKeyboardEvent::ShiftKey
) &&
296 RemoveSelectedLine();
298 // A tab press should cause the selected line to be accepted, but still
299 // return false so the tab key press propagates and changes the cursor
301 AcceptSelectedLine();
303 case ui::VKEY_RETURN
:
304 return AcceptSelectedLine();
310 void AutofillPopupControllerImpl::UpdateBoundsAndRedrawPopup() {
311 #if !defined(OS_ANDROID)
312 // TODO(csharp): Since UpdatePopupBounds can change the position of the popup,
313 // the popup could end up jumping from above the element to below it.
314 // It is unclear if it is better to keep the popup where it was, or if it
315 // should try and move to its desired position.
319 view_
->UpdateBoundsAndRedrawPopup();
322 void AutofillPopupControllerImpl::SetSelectionAtPoint(const gfx::Point
& point
) {
323 SetSelectedLine(LineFromY(point
.y()));
326 bool AutofillPopupControllerImpl::AcceptSelectedLine() {
327 if (selected_line_
== kNoSelection
)
330 DCHECK_GE(selected_line_
, 0);
331 DCHECK_LT(selected_line_
, static_cast<int>(GetLineCount()));
333 if (!CanAccept(suggestions_
[selected_line_
].frontend_id
))
336 AcceptSuggestion(selected_line_
);
340 void AutofillPopupControllerImpl::SelectionCleared() {
341 SetSelectedLine(kNoSelection
);
344 void AutofillPopupControllerImpl::AcceptSuggestion(size_t index
) {
345 const autofill::Suggestion
& suggestion
= suggestions_
[index
];
346 delegate_
->DidAcceptSuggestion(suggestion
.value
, suggestion
.frontend_id
);
349 int AutofillPopupControllerImpl::GetIconResourceID(
350 const base::string16
& resource_name
) const {
351 for (size_t i
= 0; i
< arraysize(kDataResources
); ++i
) {
352 if (resource_name
== base::ASCIIToUTF16(kDataResources
[i
].name
))
353 return kDataResources
[i
].id
;
359 bool AutofillPopupControllerImpl::IsWarning(size_t index
) const {
360 return suggestions_
[index
].frontend_id
== POPUP_ITEM_ID_WARNING_MESSAGE
;
363 gfx::Rect
AutofillPopupControllerImpl::GetRowBounds(size_t index
) {
364 int top
= kPopupBorderThickness
;
365 for (size_t i
= 0; i
< index
; ++i
) {
366 top
+= GetRowHeightFromId(suggestions_
[i
].frontend_id
);
370 kPopupBorderThickness
,
372 popup_bounds_
.width() - 2 * kPopupBorderThickness
,
373 GetRowHeightFromId(suggestions_
[index
].frontend_id
));
376 void AutofillPopupControllerImpl::SetPopupBounds(const gfx::Rect
& bounds
) {
377 popup_bounds_
= bounds
;
378 UpdateBoundsAndRedrawPopup();
381 const gfx::Rect
& AutofillPopupControllerImpl::popup_bounds() const {
382 return popup_bounds_
;
385 content::WebContents
* AutofillPopupControllerImpl::web_contents() {
386 return controller_common_
->web_contents();
389 gfx::NativeView
AutofillPopupControllerImpl::container_view() {
390 return controller_common_
->container_view();
393 const gfx::RectF
& AutofillPopupControllerImpl::element_bounds() const {
394 return controller_common_
->element_bounds();
397 bool AutofillPopupControllerImpl::IsRTL() const {
398 return controller_common_
->is_rtl();
401 size_t AutofillPopupControllerImpl::GetLineCount() const {
402 return suggestions_
.size();
405 const autofill::Suggestion
& AutofillPopupControllerImpl::GetSuggestionAt(
407 return suggestions_
[row
];
410 const base::string16
& AutofillPopupControllerImpl::GetElidedValueAt(
412 return elided_values_
[row
];
415 const base::string16
& AutofillPopupControllerImpl::GetElidedLabelAt(
417 return elided_labels_
[row
];
420 bool AutofillPopupControllerImpl::GetRemovalConfirmationText(
422 base::string16
* title
,
423 base::string16
* body
) {
424 return delegate_
->GetDeletionConfirmationText(
425 suggestions_
[list_index
].value
, suggestions_
[list_index
].frontend_id
,
429 bool AutofillPopupControllerImpl::RemoveSuggestion(int list_index
) {
430 if (!delegate_
->RemoveSuggestion(suggestions_
[list_index
].value
,
431 suggestions_
[list_index
].frontend_id
)) {
435 // Remove the deleted element.
436 suggestions_
.erase(suggestions_
.begin() + list_index
);
437 elided_values_
.erase(elided_values_
.begin() + list_index
);
438 elided_labels_
.erase(elided_labels_
.begin() + list_index
);
440 SetSelectedLine(kNoSelection
);
442 if (HasSuggestions()) {
443 delegate_
->ClearPreviewedForm();
444 UpdateBoundsAndRedrawPopup();
452 #if !defined(OS_ANDROID)
453 const gfx::FontList
& AutofillPopupControllerImpl::GetValueFontListForRow(
454 size_t index
) const {
455 if (suggestions_
[index
].frontend_id
== POPUP_ITEM_ID_WARNING_MESSAGE
)
456 return warning_font_list_
;
458 if (suggestions_
[index
].frontend_id
== POPUP_ITEM_ID_TITLE
)
459 return title_font_list_
;
461 return value_font_list_
;
464 const gfx::FontList
& AutofillPopupControllerImpl::GetLabelFontList() const {
465 return label_font_list_
;
469 int AutofillPopupControllerImpl::selected_line() const {
470 return selected_line_
;
473 void AutofillPopupControllerImpl::SetSelectedLine(int selected_line
) {
474 if (selected_line_
== selected_line
)
477 if (selected_line_
!= kNoSelection
&&
478 static_cast<size_t>(selected_line_
) < suggestions_
.size())
479 InvalidateRow(selected_line_
);
481 if (selected_line
!= kNoSelection
) {
482 InvalidateRow(selected_line
);
484 if (!CanAccept(suggestions_
[selected_line
].frontend_id
))
485 selected_line
= kNoSelection
;
488 selected_line_
= selected_line
;
490 if (selected_line_
!= kNoSelection
) {
491 delegate_
->DidSelectSuggestion(suggestions_
[selected_line_
].value
,
492 suggestions_
[selected_line_
].frontend_id
);
494 delegate_
->ClearPreviewedForm();
498 void AutofillPopupControllerImpl::SelectNextLine() {
499 int new_selected_line
= selected_line_
+ 1;
501 // Skip over any lines that can't be selected.
502 while (static_cast<size_t>(new_selected_line
) < GetLineCount() &&
503 !CanAccept(suggestions_
[new_selected_line
].frontend_id
)) {
507 if (new_selected_line
>= static_cast<int>(GetLineCount()))
508 new_selected_line
= 0;
510 SetSelectedLine(new_selected_line
);
513 void AutofillPopupControllerImpl::SelectPreviousLine() {
514 int new_selected_line
= selected_line_
- 1;
516 // Skip over any lines that can't be selected.
517 while (new_selected_line
> kNoSelection
&&
518 !CanAccept(GetSuggestionAt(new_selected_line
).frontend_id
)) {
522 if (new_selected_line
<= kNoSelection
)
523 new_selected_line
= GetLineCount() - 1;
525 SetSelectedLine(new_selected_line
);
528 bool AutofillPopupControllerImpl::RemoveSelectedLine() {
529 if (selected_line_
== kNoSelection
)
532 DCHECK_GE(selected_line_
, 0);
533 DCHECK_LT(selected_line_
, static_cast<int>(GetLineCount()));
534 return RemoveSuggestion(selected_line_
);
537 int AutofillPopupControllerImpl::LineFromY(int y
) {
538 int current_height
= kPopupBorderThickness
;
540 for (size_t i
= 0; i
< suggestions_
.size(); ++i
) {
541 current_height
+= GetRowHeightFromId(suggestions_
[i
].frontend_id
);
543 if (y
<= current_height
)
547 // The y value goes beyond the popup so stop the selection at the last line.
548 return GetLineCount() - 1;
551 int AutofillPopupControllerImpl::GetRowHeightFromId(int identifier
) const {
552 if (identifier
== POPUP_ITEM_ID_SEPARATOR
)
553 return kSeparatorHeight
;
558 bool AutofillPopupControllerImpl::CanAccept(int id
) {
559 return id
!= POPUP_ITEM_ID_SEPARATOR
&& id
!= POPUP_ITEM_ID_WARNING_MESSAGE
&&
560 id
!= POPUP_ITEM_ID_TITLE
;
563 bool AutofillPopupControllerImpl::HasSuggestions() {
564 if (suggestions_
.empty())
566 int id
= suggestions_
[0].frontend_id
;
568 id
== POPUP_ITEM_ID_AUTOCOMPLETE_ENTRY
||
569 id
== POPUP_ITEM_ID_PASSWORD_ENTRY
||
570 id
== POPUP_ITEM_ID_DATALIST_ENTRY
||
571 id
== POPUP_ITEM_ID_MAC_ACCESS_CONTACTS
||
572 id
== POPUP_ITEM_ID_SCAN_CREDIT_CARD
;
575 void AutofillPopupControllerImpl::SetValues(
576 const std::vector
<autofill::Suggestion
>& suggestions
) {
577 suggestions_
= suggestions
;
578 elided_values_
.resize(suggestions
.size());
579 elided_labels_
.resize(suggestions
.size());
580 for (size_t i
= 0; i
< suggestions
.size(); i
++) {
581 elided_values_
[i
] = suggestions
[i
].value
;
582 elided_labels_
[i
] = suggestions
[i
].label
;
586 void AutofillPopupControllerImpl::ShowView() {
590 void AutofillPopupControllerImpl::InvalidateRow(size_t row
) {
592 DCHECK(row
< suggestions_
.size());
593 view_
->InvalidateRow(row
);
596 #if !defined(OS_ANDROID)
597 int AutofillPopupControllerImpl::GetDesiredPopupWidth() const {
598 int popup_width
= controller_common_
->RoundedElementBounds().width();
599 for (size_t i
= 0; i
< GetLineCount(); ++i
) {
601 gfx::GetStringWidth(GetElidedValueAt(i
), value_font_list_
) +
602 gfx::GetStringWidth(GetElidedLabelAt(i
), label_font_list_
) +
603 RowWidthWithoutText(i
);
605 popup_width
= std::max(popup_width
, row_size
);
611 int AutofillPopupControllerImpl::GetDesiredPopupHeight() const {
612 int popup_height
= 2 * kPopupBorderThickness
;
614 for (size_t i
= 0; i
< suggestions_
.size(); ++i
) {
615 popup_height
+= GetRowHeightFromId(suggestions_
[i
].frontend_id
);
621 int AutofillPopupControllerImpl::RowWidthWithoutText(int row
) const {
622 int row_size
= kEndPadding
;
624 if (!elided_labels_
[row
].empty())
625 row_size
+= kNamePadding
;
627 // Add the Autofill icon size, if required.
628 const base::string16
& icon
= suggestions_
[row
].icon
;
630 int icon_width
= ui::ResourceBundle::GetSharedInstance().GetImageNamed(
631 GetIconResourceID(icon
)).Width();
632 row_size
+= icon_width
+ kIconPadding
;
635 // Add the padding at the end.
636 row_size
+= kEndPadding
;
638 // Add room for the popup border.
639 row_size
+= 2 * kPopupBorderThickness
;
644 void AutofillPopupControllerImpl::UpdatePopupBounds() {
645 int popup_width
= GetDesiredPopupWidth();
646 int popup_height
= GetDesiredPopupHeight();
648 popup_bounds_
= controller_common_
->GetPopupBounds(popup_width
, popup_height
);
650 #endif // !defined(OS_ANDROID)
652 WeakPtr
<AutofillPopupControllerImpl
> AutofillPopupControllerImpl::GetWeakPtr() {
653 return weak_ptr_factory_
.GetWeakPtr();
656 void AutofillPopupControllerImpl::ClearState() {
657 // Don't clear view_, because otherwise the popup will have to get regenerated
658 // and this will cause flickering.
660 popup_bounds_
= gfx::Rect();
662 suggestions_
.clear();
663 elided_values_
.clear();
664 elided_labels_
.clear();
666 selected_line_
= kNoSelection
;
669 } // namespace autofill