• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef CHROME_BROWSER_UI_AUTOFILL_AUTOFILL_POPUP_CONTROLLER_IMPL_H_
6 #define CHROME_BROWSER_UI_AUTOFILL_AUTOFILL_POPUP_CONTROLLER_IMPL_H_
7 
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"
17 
18 namespace autofill {
19 
20 class AutofillPopupDelegate;
21 class AutofillPopupView;
22 
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);
39 
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);
45 
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);
49 
50   // Hides the popup and destroys the controller. This also invalidates
51   // |delegate_|.
52   virtual void Hide() OVERRIDE;
53 
54   // Invoked when the view was destroyed by by someone other than this class.
55   virtual void ViewDestroyed() OVERRIDE;
56 
57   bool HandleKeyPressEvent(const content::NativeWebKeyboardEvent& event);
58 
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);
61 
62  protected:
63   FRIEND_TEST_ALL_PREFIXES(AutofillExternalDelegateBrowserTest,
64                            CloseWidgetAndNoLeaking);
65   FRIEND_TEST_ALL_PREFIXES(AutofillPopupControllerUnitTest,
66                            ProperlyResetController);
67 
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();
74 
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;
91 
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;
100 #endif
101   virtual int selected_line() const OVERRIDE;
102 
103   content::WebContents* web_contents();
104 
105   // Change which line is currently selected by the user.
106   void SetSelectedLine(int selected_line);
107 
108   // Increase the selected line by 1, properly handling wrapping.
109   void SelectNextLine();
110 
111   // Decrease the selected line by 1, properly handling wrapping.
112   void SelectPreviousLine();
113 
114   // The user has removed a suggestion.
115   bool RemoveSelectedLine();
116 
117   // Convert a y-coordinate to the closest line.
118   int LineFromY(int y);
119 
120   // Returns the height of a row depending on its type.
121   int GetRowHeightFromId(int identifier) const;
122 
123   // Returns true if the given id refers to an element that can be accepted.
124   bool CanAccept(int id);
125 
126   // Returns true if the popup still has non-options entries to show the user.
127   bool HasSuggestions();
128 
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);
135 
view()136   AutofillPopupView* view() { return view_; }
137 
138   // |view_| pass throughs (virtual for testing).
139   virtual void ShowView();
140   virtual void InvalidateRow(size_t row);
141 
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;
146 
147   // Calculates the desired height of the popup based on its contents.
148   int GetDesiredPopupHeight() const;
149 
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;
154 #endif
155 
156   base::WeakPtr<AutofillPopupControllerImpl> GetWeakPtr();
157 
158   // Contains common popup functionality such as popup layout. Protected for
159   // testing.
160   scoped_ptr<PopupControllerCommon> controller_common_;
161 
162  private:
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.
165   void ClearState();
166 
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();
171 #endif
172 
173   AutofillPopupView* view_;  // Weak reference.
174   base::WeakPtr<AutofillPopupDelegate> delegate_;
175 
176   // The bounds of the Autofill popup.
177   gfx::Rect popup_bounds_;
178 
179   // The text direction of the popup.
180   base::i18n::TextDirection text_direction_;
181 
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_;
187 
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_;
191 
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_;
197 #endif
198 
199   // The line that is currently selected by the user.
200   // |kNoSelection| indicates that no line is currently selected.
201   int selected_line_;
202 
203   base::WeakPtrFactory<AutofillPopupControllerImpl> weak_ptr_factory_;
204 };
205 
206 }  // namespace autofill
207 
208 #endif  // CHROME_BROWSER_UI_AUTOFILL_AUTOFILL_POPUP_CONTROLLER_IMPL_H_
209