ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / ui / autofill / autofill_popup_controller_impl.h
blob1bb9f6d29148fc23d3d6db780b800c1488312480
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"
18 namespace autofill {
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 {
27 public:
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
48 // |delegate_|.
49 void Hide() override;
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);
59 protected:
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 IsWarning(size_t index) const override;
80 gfx::Rect GetRowBounds(size_t index) override;
81 void SetPopupBounds(const gfx::Rect& bounds) override;
82 const gfx::Rect& popup_bounds() const override;
83 gfx::NativeView container_view() override;
84 const gfx::RectF& element_bounds() const override;
85 bool IsRTL() const override;
87 size_t GetLineCount() const override;
88 const autofill::Suggestion& GetSuggestionAt(size_t row) const override;
89 const base::string16& GetElidedValueAt(size_t row) const override;
90 const base::string16& GetElidedLabelAt(size_t row) const override;
91 bool GetRemovalConfirmationText(int list_index,
92 base::string16* title,
93 base::string16* body) override;
94 bool RemoveSuggestion(int list_index) override;
95 #if !defined(OS_ANDROID)
96 const gfx::FontList& GetValueFontListForRow(size_t index) const override;
97 const gfx::FontList& GetLabelFontList() const override;
98 #endif
99 int selected_line() const override;
101 content::WebContents* web_contents();
103 // Change which line is currently selected by the user.
104 void SetSelectedLine(int selected_line);
106 // Increase the selected line by 1, properly handling wrapping.
107 void SelectNextLine();
109 // Decrease the selected line by 1, properly handling wrapping.
110 void SelectPreviousLine();
112 // The user has removed a suggestion.
113 bool RemoveSelectedLine();
115 // Convert a y-coordinate to the closest line.
116 int LineFromY(int y);
118 // Returns the height of a row depending on its type.
119 int GetRowHeightFromId(int identifier) const;
121 // Returns true if the given id refers to an element that can be accepted.
122 bool CanAccept(int id);
124 // Returns true if the popup still has non-options entries to show the user.
125 bool HasSuggestions();
127 // Set the Autofill entry values. Exposed to allow tests to set these values
128 // without showing the popup.
129 void SetValues(const std::vector<autofill::Suggestion>& suggestions);
131 AutofillPopupView* view() { return view_; }
133 // |view_| pass throughs (virtual for testing).
134 virtual void ShowView();
135 virtual void InvalidateRow(size_t row);
137 // Protected so tests can access.
138 #if !defined(OS_ANDROID)
139 // Calculates the desired width of the popup based on its contents.
140 int GetDesiredPopupWidth() const;
142 // Calculates the desired height of the popup based on its contents.
143 int GetDesiredPopupHeight() const;
145 // Calculate the width of the row, excluding all the text. This provides
146 // the size of the row that won't be reducible (since all the text can be
147 // elided if there isn't enough space).
148 int RowWidthWithoutText(int row) const;
149 #endif
151 base::WeakPtr<AutofillPopupControllerImpl> GetWeakPtr();
153 // Contains common popup functionality such as popup layout. Protected for
154 // testing.
155 scoped_ptr<PopupControllerCommon> controller_common_;
157 private:
158 // Clear the internal state of the controller. This is needed to ensure that
159 // when the popup is reused it doesn't leak values between uses.
160 void ClearState();
162 #if !defined(OS_ANDROID)
163 // Calculates and sets the bounds of the popup, including placing it properly
164 // to prevent it from going off the screen.
165 void UpdatePopupBounds();
166 #endif
168 AutofillPopupView* view_; // Weak reference.
169 base::WeakPtr<AutofillPopupDelegate> delegate_;
171 // The bounds of the Autofill popup.
172 gfx::Rect popup_bounds_;
174 // The text direction of the popup.
175 base::i18n::TextDirection text_direction_;
177 // The current Autofill query values.
178 std::vector<autofill::Suggestion> suggestions_;
180 // Elided values and labels corresponding to the suggestions_ vector to
181 // ensure that it fits on the screen.
182 std::vector<base::string16> elided_values_;
183 std::vector<base::string16> elided_labels_;
185 #if !defined(OS_ANDROID)
186 // The fonts for the popup text.
187 gfx::FontList value_font_list_;
188 gfx::FontList label_font_list_;
189 gfx::FontList warning_font_list_;
190 gfx::FontList title_font_list_;
191 #endif
193 // The line that is currently selected by the user.
194 // |kNoSelection| indicates that no line is currently selected.
195 int selected_line_;
197 base::WeakPtrFactory<AutofillPopupControllerImpl> weak_ptr_factory_;
200 } // namespace autofill
202 #endif // CHROME_BROWSER_UI_AUTOFILL_AUTOFILL_POPUP_CONTROLLER_IMPL_H_