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/rect.h"
16 #include "ui/gfx/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
<base::string16
>& names
,
42 const std::vector
<base::string16
>& subtexts
,
43 const std::vector
<base::string16
>& icons
,
44 const std::vector
<int>& identifiers
);
46 // Updates the data list values currently shown with the popup.
47 void UpdateDataListValues(const std::vector
<base::string16
>& values
,
48 const std::vector
<base::string16
>& labels
);
50 // Hides the popup and destroys the controller. This also invalidates
52 virtual void Hide() override
;
54 // Invoked when the view was destroyed by by someone other than this class.
55 virtual void ViewDestroyed() override
;
57 bool HandleKeyPressEvent(const content::NativeWebKeyboardEvent
& event
);
59 // Tells the view to capture mouse events. Must be called before |Show()|.
60 void set_hide_on_outside_click(bool hide_on_outside_click
);
63 FRIEND_TEST_ALL_PREFIXES(AutofillExternalDelegateBrowserTest
,
64 CloseWidgetAndNoLeaking
);
65 FRIEND_TEST_ALL_PREFIXES(AutofillPopupControllerUnitTest
,
66 ProperlyResetController
);
68 AutofillPopupControllerImpl(base::WeakPtr
<AutofillPopupDelegate
> delegate
,
69 content::WebContents
* web_contents
,
70 gfx::NativeView container_view
,
71 const gfx::RectF
& element_bounds
,
72 base::i18n::TextDirection text_direction
);
73 virtual ~AutofillPopupControllerImpl();
75 // AutofillPopupController implementation.
76 virtual void UpdateBoundsAndRedrawPopup() override
;
77 virtual void SetSelectionAtPoint(const gfx::Point
& point
) override
;
78 virtual bool AcceptSelectedLine() override
;
79 virtual void SelectionCleared() override
;
80 virtual void AcceptSuggestion(size_t index
) override
;
81 virtual int GetIconResourceID(
82 const base::string16
& resource_name
) const override
;
83 virtual bool CanDelete(size_t index
) const override
;
84 virtual bool IsWarning(size_t index
) const override
;
85 virtual gfx::Rect
GetRowBounds(size_t index
) override
;
86 virtual void SetPopupBounds(const gfx::Rect
& bounds
) override
;
87 virtual const gfx::Rect
& popup_bounds() const override
;
88 virtual gfx::NativeView
container_view() override
;
89 virtual const gfx::RectF
& element_bounds() const override
;
90 virtual bool IsRTL() const override
;
92 virtual const std::vector
<base::string16
>& names() const override
;
93 virtual const std::vector
<base::string16
>& subtexts() const override
;
94 virtual const std::vector
<base::string16
>& icons() const override
;
95 virtual const std::vector
<int>& identifiers() const override
;
96 #if !defined(OS_ANDROID)
97 virtual const gfx::FontList
& GetNameFontListForRow(
98 size_t index
) const override
;
99 virtual const gfx::FontList
& subtext_font_list() const override
;
101 virtual int selected_line() const override
;
103 content::WebContents
* web_contents();
105 // Change which line is currently selected by the user.
106 void SetSelectedLine(int selected_line
);
108 // Increase the selected line by 1, properly handling wrapping.
109 void SelectNextLine();
111 // Decrease the selected line by 1, properly handling wrapping.
112 void SelectPreviousLine();
114 // The user has removed a suggestion.
115 bool RemoveSelectedLine();
117 // Convert a y-coordinate to the closest line.
118 int LineFromY(int y
);
120 // Returns the height of a row depending on its type.
121 int GetRowHeightFromId(int identifier
) const;
123 // Returns true if the given id refers to an element that can be accepted.
124 bool CanAccept(int id
);
126 // Returns true if the popup still has non-options entries to show the user.
127 bool HasSuggestions();
129 // Set the Autofill entry values. Exposed to allow tests to set these values
130 // without showing the popup.
131 void SetValues(const std::vector
<base::string16
>& names
,
132 const std::vector
<base::string16
>& subtexts
,
133 const std::vector
<base::string16
>& icons
,
134 const std::vector
<int>& identifier
);
136 AutofillPopupView
* view() { return view_
; }
138 // |view_| pass throughs (virtual for testing).
139 virtual void ShowView();
140 virtual void InvalidateRow(size_t row
);
142 // Protected so tests can access.
143 #if !defined(OS_ANDROID)
144 // Calculates the desired width of the popup based on its contents.
145 int GetDesiredPopupWidth() const;
147 // Calculates the desired height of the popup based on its contents.
148 int GetDesiredPopupHeight() const;
150 // Calculate the width of the row, excluding all the text. This provides
151 // the size of the row that won't be reducible (since all the text can be
152 // elided if there isn't enough space).
153 int RowWidthWithoutText(int row
) const;
156 base::WeakPtr
<AutofillPopupControllerImpl
> GetWeakPtr();
158 // Contains common popup functionality such as popup layout. Protected for
160 scoped_ptr
<PopupControllerCommon
> controller_common_
;
163 // Clear the internal state of the controller. This is needed to ensure that
164 // when the popup is reused it doesn't leak values between uses.
167 #if !defined(OS_ANDROID)
168 // Calculates and sets the bounds of the popup, including placing it properly
169 // to prevent it from going off the screen.
170 void UpdatePopupBounds();
173 AutofillPopupView
* view_
; // Weak reference.
174 base::WeakPtr
<AutofillPopupDelegate
> delegate_
;
176 // The bounds of the Autofill popup.
177 gfx::Rect popup_bounds_
;
179 // The text direction of the popup.
180 base::i18n::TextDirection text_direction_
;
182 // The current Autofill query values.
183 std::vector
<base::string16
> names_
;
184 std::vector
<base::string16
> subtexts_
;
185 std::vector
<base::string16
> icons_
;
186 std::vector
<int> identifiers_
;
188 // Since names_ can be elided to ensure that it fits on the screen, we need to
189 // keep an unelided copy of the names to be able to pass to the delegate.
190 std::vector
<base::string16
> full_names_
;
192 #if !defined(OS_ANDROID)
193 // The fonts for the popup text.
194 gfx::FontList name_font_list_
;
195 gfx::FontList subtext_font_list_
;
196 gfx::FontList warning_font_list_
;
199 // The line that is currently selected by the user.
200 // |kNoSelection| indicates that no line is currently selected.
203 base::WeakPtrFactory
<AutofillPopupControllerImpl
> weak_ptr_factory_
;
206 } // namespace autofill
208 #endif // CHROME_BROWSER_UI_AUTOFILL_AUTOFILL_POPUP_CONTROLLER_IMPL_H_