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 "content/public/browser/render_widget_host.h"
14 #include "ui/gfx/font_list.h"
15 #include "ui/gfx/rect.h"
16 #include "ui/gfx/rect_f.h"
19 struct NativeWebKeyboardEvent
;
34 class AutofillPopupDelegate
;
35 class AutofillPopupView
;
37 // This class is a controller for an AutofillPopupView. It implements
38 // AutofillPopupController to allow calls from AutofillPopupView. The
39 // other, public functions are available to its instantiator.
40 class AutofillPopupControllerImpl
: public AutofillPopupController
{
42 // Creates a new |AutofillPopupControllerImpl|, or reuses |previous| if the
43 // construction arguments are the same. |previous| may be invalidated by this
44 // call. The controller will listen for keyboard input routed to
45 // |web_contents| while the popup is showing, unless |web_contents| is NULL.
46 static base::WeakPtr
<AutofillPopupControllerImpl
> GetOrCreate(
47 base::WeakPtr
<AutofillPopupControllerImpl
> previous
,
48 base::WeakPtr
<AutofillPopupDelegate
> delegate
,
49 content::WebContents
* web_contents
,
50 gfx::NativeView container_view
,
51 const gfx::RectF
& element_bounds
,
52 base::i18n::TextDirection text_direction
);
54 // Shows the popup, or updates the existing popup with the given values.
55 void Show(const std::vector
<base::string16
>& names
,
56 const std::vector
<base::string16
>& subtexts
,
57 const std::vector
<base::string16
>& icons
,
58 const std::vector
<int>& identifiers
);
60 // Updates the data list values currently shown with the popup.
61 void UpdateDataListValues(const std::vector
<base::string16
>& values
,
62 const std::vector
<base::string16
>& labels
);
64 // Hides the popup and destroys the controller. This also invalidates
66 virtual void Hide() OVERRIDE
;
68 // Invoked when the view was destroyed by by someone other than this class.
69 virtual void ViewDestroyed() OVERRIDE
;
71 bool HandleKeyPressEvent(const content::NativeWebKeyboardEvent
& event
);
73 // Tells the view to capture mouse events. Must be called before |Show()|.
74 void set_hide_on_outside_click(bool hide_on_outside_click
);
77 FRIEND_TEST_ALL_PREFIXES(AutofillExternalDelegateBrowserTest
,
78 CloseWidgetAndNoLeaking
);
79 FRIEND_TEST_ALL_PREFIXES(AutofillPopupControllerUnitTest
,
80 ProperlyResetController
);
82 AutofillPopupControllerImpl(base::WeakPtr
<AutofillPopupDelegate
> delegate
,
83 content::WebContents
* web_contents
,
84 gfx::NativeView container_view
,
85 const gfx::RectF
& element_bounds
,
86 base::i18n::TextDirection text_direction
);
87 virtual ~AutofillPopupControllerImpl();
89 // AutofillPopupController implementation.
90 virtual void UpdateBoundsAndRedrawPopup() OVERRIDE
;
91 virtual void LineSelectedAtPoint(int x
, int y
) OVERRIDE
;
92 virtual void LineAcceptedAtPoint(int x
, int y
) OVERRIDE
;
93 virtual void SelectionCleared() OVERRIDE
;
94 virtual bool ShouldRepostEvent(const ui::MouseEvent
& event
) OVERRIDE
;
95 virtual void AcceptSuggestion(size_t index
) OVERRIDE
;
96 virtual int GetIconResourceID(
97 const base::string16
& resource_name
) const OVERRIDE
;
98 virtual bool CanDelete(size_t index
) const OVERRIDE
;
99 virtual bool IsWarning(size_t index
) const OVERRIDE
;
100 virtual gfx::Rect
GetRowBounds(size_t index
) OVERRIDE
;
101 virtual void SetPopupBounds(const gfx::Rect
& bounds
) OVERRIDE
;
102 virtual const gfx::Rect
& popup_bounds() const OVERRIDE
;
103 virtual gfx::NativeView
container_view() const OVERRIDE
;
104 virtual const gfx::RectF
& element_bounds() const OVERRIDE
;
105 virtual bool IsRTL() const OVERRIDE
;
107 virtual const std::vector
<base::string16
>& names() const OVERRIDE
;
108 virtual const std::vector
<base::string16
>& subtexts() const OVERRIDE
;
109 virtual const std::vector
<base::string16
>& icons() const OVERRIDE
;
110 virtual const std::vector
<int>& identifiers() const OVERRIDE
;
111 #if !defined(OS_ANDROID)
112 virtual const gfx::FontList
& GetNameFontListForRow(
113 size_t index
) const OVERRIDE
;
114 virtual const gfx::FontList
& subtext_font_list() const OVERRIDE
;
116 virtual int selected_line() const OVERRIDE
;
117 virtual bool hide_on_outside_click() const OVERRIDE
;
119 // Change which line is currently selected by the user.
120 void SetSelectedLine(int selected_line
);
122 // Increase the selected line by 1, properly handling wrapping.
123 void SelectNextLine();
125 // Decrease the selected line by 1, properly handling wrapping.
126 void SelectPreviousLine();
128 // The user has choosen the selected line.
129 bool AcceptSelectedLine();
131 // The user has removed a suggestion.
132 bool RemoveSelectedLine();
134 // Convert a y-coordinate to the closest line.
135 int LineFromY(int y
);
137 // Returns the height of a row depending on its type.
138 int GetRowHeightFromId(int identifier
) const;
140 // Returns true if the given id refers to an element that can be accepted.
141 bool CanAccept(int id
);
143 // Returns true if the popup still has non-options entries to show the user.
144 bool HasSuggestions();
146 // Set the Autofill entry values. Exposed to allow tests to set these values
147 // without showing the popup.
148 void SetValues(const std::vector
<base::string16
>& names
,
149 const std::vector
<base::string16
>& subtexts
,
150 const std::vector
<base::string16
>& icons
,
151 const std::vector
<int>& identifier
);
153 AutofillPopupView
* view() { return view_
; }
155 // |view_| pass throughs (virtual for testing).
156 virtual void ShowView();
157 virtual void InvalidateRow(size_t row
);
159 // Protected so tests can access.
160 #if !defined(OS_ANDROID)
161 // Calculates the desired width of the popup based on its contents.
162 int GetDesiredPopupWidth() const;
164 // Calculates the desired height of the popup based on its contents.
165 int GetDesiredPopupHeight() const;
167 // Calculate the width of the row, excluding all the text. This provides
168 // the size of the row that won't be reducible (since all the text can be
169 // elided if there isn't enough space).
170 int RowWidthWithoutText(int row
) const;
173 base::WeakPtr
<AutofillPopupControllerImpl
> GetWeakPtr();
176 // Clear the internal state of the controller. This is needed to ensure that
177 // when the popup is reused it doesn't leak values between uses.
180 const gfx::Rect
RoundedElementBounds() const;
181 #if !defined(OS_ANDROID)
182 // Calculates and sets the bounds of the popup, including placing it properly
183 // to prevent it from going off the screen.
184 void UpdatePopupBounds();
187 // A helper function to get the display closest to the given point (virtual
189 virtual gfx::Display
GetDisplayNearestPoint(const gfx::Point
& point
) const;
191 // Calculates the width of the popup and the x position of it. These values
192 // will stay on the screen.
193 std::pair
<int, int> CalculatePopupXAndWidth(
194 const gfx::Display
& left_display
,
195 const gfx::Display
& right_display
,
196 int popup_required_width
) const;
198 // Calculates the height of the popup and the y position of it. These values
199 // will stay on the screen.
200 std::pair
<int, int> CalculatePopupYAndHeight(
201 const gfx::Display
& top_display
,
202 const gfx::Display
& bottom_display
,
203 int popup_required_height
) const;
205 AutofillPopupView
* view_
; // Weak reference.
206 base::WeakPtr
<AutofillPopupDelegate
> delegate_
;
208 // The WebContents in which this object should listen for keyboard events
209 // while showing the popup. Can be NULL, in which case this object will not
210 // listen for keyboard events.
211 content::WebContents
* web_contents_
;
213 gfx::NativeView container_view_
; // Weak reference.
215 // The bounds of the text element that is the focus of the Autofill.
216 // These coordinates are in screen space.
217 const gfx::RectF element_bounds_
;
219 // The bounds of the Autofill popup.
220 gfx::Rect popup_bounds_
;
222 // The text direction of the popup.
223 base::i18n::TextDirection text_direction_
;
225 // The RenderViewHost that this object has registered its keyboard press
227 content::RenderViewHost
* registered_key_press_event_callback_with_
;
229 // The current Autofill query values.
230 std::vector
<base::string16
> names_
;
231 std::vector
<base::string16
> subtexts_
;
232 std::vector
<base::string16
> icons_
;
233 std::vector
<int> identifiers_
;
235 // Since names_ can be elided to ensure that it fits on the screen, we need to
236 // keep an unelided copy of the names to be able to pass to the delegate.
237 std::vector
<base::string16
> full_names_
;
239 #if !defined(OS_ANDROID)
240 // The fonts for the popup text.
241 gfx::FontList name_font_list_
;
242 gfx::FontList subtext_font_list_
;
243 gfx::FontList warning_font_list_
;
246 // The line that is currently selected by the user.
247 // |kNoSelection| indicates that no line is currently selected.
250 // Whether the popup view should hide on mouse presses outside of it.
251 bool hide_on_outside_click_
;
253 content::RenderWidgetHost::KeyPressEventCallback key_press_event_callback_
;
255 base::WeakPtrFactory
<AutofillPopupControllerImpl
> weak_ptr_factory_
;
258 } // namespace autofill
260 #endif // CHROME_BROWSER_UI_AUTOFILL_AUTOFILL_POPUP_CONTROLLER_IMPL_H_