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 #ifndef CHROME_BROWSER_UI_AUTOFILL_AUTOFILL_POPUP_CONTROLLER_IMPL_H_
6 #define CHROME_BROWSER_UI_AUTOFILL_AUTOFILL_POPUP_CONTROLLER_IMPL_H_
8 #include "base/gtest_prod_util.h"
9 #include "base/i18n/rtl.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/strings/string16.h"
12 #include "chrome/browser/ui/autofill/autofill_popup_controller.h"
13 #include "chrome/browser/ui/autofill/popup_controller_common.h"
14 #include "ui/gfx/font_list.h"
15 #include "ui/gfx/geometry/rect.h"
16 #include "ui/gfx/geometry/rect_f.h"
20 class AutofillPopupDelegate
;
21 class AutofillPopupView
;
23 // This class is a controller for an AutofillPopupView. It implements
24 // AutofillPopupController to allow calls from AutofillPopupView. The
25 // other, public functions are available to its instantiator.
26 class AutofillPopupControllerImpl
: public AutofillPopupController
{
28 // Creates a new |AutofillPopupControllerImpl|, or reuses |previous| if the
29 // construction arguments are the same. |previous| may be invalidated by this
30 // call. The controller will listen for keyboard input routed to
31 // |web_contents| while the popup is showing, unless |web_contents| is NULL.
32 static base::WeakPtr
<AutofillPopupControllerImpl
> GetOrCreate(
33 base::WeakPtr
<AutofillPopupControllerImpl
> previous
,
34 base::WeakPtr
<AutofillPopupDelegate
> delegate
,
35 content::WebContents
* web_contents
,
36 gfx::NativeView container_view
,
37 const gfx::RectF
& element_bounds
,
38 base::i18n::TextDirection text_direction
);
40 // Shows the popup, or updates the existing popup with the given values.
41 void Show(const std::vector
<autofill::Suggestion
>& suggestions
);
43 // Updates the data list values currently shown with the popup.
44 void UpdateDataListValues(const std::vector
<base::string16
>& values
,
45 const std::vector
<base::string16
>& labels
);
47 // Hides the popup and destroys the controller. This also invalidates
51 // Invoked when the view was destroyed by by someone other than this class.
52 void ViewDestroyed() override
;
54 bool HandleKeyPressEvent(const content::NativeWebKeyboardEvent
& event
);
56 // Tells the view to capture mouse events. Must be called before |Show()|.
57 void set_hide_on_outside_click(bool hide_on_outside_click
);
60 FRIEND_TEST_ALL_PREFIXES(AutofillExternalDelegateBrowserTest
,
61 CloseWidgetAndNoLeaking
);
62 FRIEND_TEST_ALL_PREFIXES(AutofillPopupControllerUnitTest
,
63 ProperlyResetController
);
65 AutofillPopupControllerImpl(base::WeakPtr
<AutofillPopupDelegate
> delegate
,
66 content::WebContents
* web_contents
,
67 gfx::NativeView container_view
,
68 const gfx::RectF
& element_bounds
,
69 base::i18n::TextDirection text_direction
);
70 ~AutofillPopupControllerImpl() override
;
72 // AutofillPopupController implementation.
73 void UpdateBoundsAndRedrawPopup() override
;
74 void SetSelectionAtPoint(const gfx::Point
& point
) override
;
75 bool AcceptSelectedLine() override
;
76 void SelectionCleared() override
;
77 void AcceptSuggestion(size_t index
) override
;
78 int GetIconResourceID(const base::string16
& resource_name
) const override
;
79 bool CanDelete(size_t index
) const override
;
80 bool IsWarning(size_t index
) const override
;
81 gfx::Rect
GetRowBounds(size_t index
) override
;
82 void SetPopupBounds(const gfx::Rect
& bounds
) override
;
83 const gfx::Rect
& popup_bounds() const override
;
84 gfx::NativeView
container_view() override
;
85 const gfx::RectF
& element_bounds() const override
;
86 bool IsRTL() const override
;
88 size_t GetLineCount() const override
;
89 const autofill::Suggestion
& GetSuggestionAt(size_t row
) const override
;
90 const base::string16
& GetElidedValueAt(size_t row
) const override
;
91 const base::string16
& GetElidedLabelAt(size_t row
) const override
;
92 #if !defined(OS_ANDROID)
93 const gfx::FontList
& GetValueFontListForRow(size_t index
) const override
;
94 const gfx::FontList
& GetLabelFontList() const override
;
96 int selected_line() const override
;
98 content::WebContents
* web_contents();
100 // Change which line is currently selected by the user.
101 void SetSelectedLine(int selected_line
);
103 // Increase the selected line by 1, properly handling wrapping.
104 void SelectNextLine();
106 // Decrease the selected line by 1, properly handling wrapping.
107 void SelectPreviousLine();
109 // The user has removed a suggestion.
110 bool RemoveSelectedLine();
112 // Convert a y-coordinate to the closest line.
113 int LineFromY(int y
);
115 // Returns the height of a row depending on its type.
116 int GetRowHeightFromId(int identifier
) const;
118 // Returns true if the given id refers to an element that can be accepted.
119 bool CanAccept(int id
);
121 // Returns true if the popup still has non-options entries to show the user.
122 bool HasSuggestions();
124 // Set the Autofill entry values. Exposed to allow tests to set these values
125 // without showing the popup.
126 void SetValues(const std::vector
<autofill::Suggestion
>& suggestions
);
128 AutofillPopupView
* view() { return view_
; }
130 // |view_| pass throughs (virtual for testing).
131 virtual void ShowView();
132 virtual void InvalidateRow(size_t row
);
134 // Protected so tests can access.
135 #if !defined(OS_ANDROID)
136 // Calculates the desired width of the popup based on its contents.
137 int GetDesiredPopupWidth() const;
139 // Calculates the desired height of the popup based on its contents.
140 int GetDesiredPopupHeight() const;
142 // Calculate the width of the row, excluding all the text. This provides
143 // the size of the row that won't be reducible (since all the text can be
144 // elided if there isn't enough space).
145 int RowWidthWithoutText(int row
) const;
148 base::WeakPtr
<AutofillPopupControllerImpl
> GetWeakPtr();
150 // Contains common popup functionality such as popup layout. Protected for
152 scoped_ptr
<PopupControllerCommon
> controller_common_
;
155 // Clear the internal state of the controller. This is needed to ensure that
156 // when the popup is reused it doesn't leak values between uses.
159 #if !defined(OS_ANDROID)
160 // Calculates and sets the bounds of the popup, including placing it properly
161 // to prevent it from going off the screen.
162 void UpdatePopupBounds();
165 AutofillPopupView
* view_
; // Weak reference.
166 base::WeakPtr
<AutofillPopupDelegate
> delegate_
;
168 // The bounds of the Autofill popup.
169 gfx::Rect popup_bounds_
;
171 // The text direction of the popup.
172 base::i18n::TextDirection text_direction_
;
174 // The current Autofill query values.
175 std::vector
<autofill::Suggestion
> suggestions_
;
177 // Elided values and labels corresponding to the suggestions_ vector to
178 // ensure that it fits on the screen.
179 std::vector
<base::string16
> elided_values_
;
180 std::vector
<base::string16
> elided_labels_
;
182 #if !defined(OS_ANDROID)
183 // The fonts for the popup text.
184 gfx::FontList value_font_list_
;
185 gfx::FontList label_font_list_
;
186 gfx::FontList warning_font_list_
;
187 gfx::FontList title_font_list_
;
190 // The line that is currently selected by the user.
191 // |kNoSelection| indicates that no line is currently selected.
194 base::WeakPtrFactory
<AutofillPopupControllerImpl
> weak_ptr_factory_
;
197 } // namespace autofill
199 #endif // CHROME_BROWSER_UI_AUTOFILL_AUTOFILL_POPUP_CONTROLLER_IMPL_H_