autofill_popup_controller_impl.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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/memory/weak_ptr.h" 10#include "base/string16.h" 11#include "chrome/browser/ui/autofill/autofill_popup_controller.h" 12#include "content/public/browser/keyboard_listener.h" 13#include "ui/gfx/font.h" 14#include "ui/gfx/rect.h" 15#include "ui/gfx/rect_f.h" 16 17namespace gfx { 18class Display; 19} 20 21namespace ui { 22class KeyEvent; 23} 24 25namespace autofill { 26 27class AutofillPopupDelegate; 28class AutofillPopupView; 29 30// This class is a controller for an AutofillPopupView. It implements 31// AutofillPopupController to allow calls from AutofillPopupView. The 32// other, public functions are available to its instantiator. 33class AutofillPopupControllerImpl : public AutofillPopupController, 34 public content::KeyboardListener { 35 public: 36 // Creates a new |AutofillPopupControllerImpl|, or reuses |previous| if 37 // the construction arguments are the same. |previous| may be invalidated by 38 // this call. 39 static base::WeakPtr<AutofillPopupControllerImpl> GetOrCreate( 40 base::WeakPtr<AutofillPopupControllerImpl> previous, 41 base::WeakPtr<AutofillPopupDelegate> delegate, 42 gfx::NativeView container_view, 43 const gfx::RectF& element_bounds); 44 45 // Shows the popup, or updates the existing popup with the given values. 46 void Show(const std::vector<string16>& names, 47 const std::vector<string16>& subtexts, 48 const std::vector<string16>& icons, 49 const std::vector<int>& identifiers); 50 51 // Hides the popup and destroys the controller. This also invalidates 52 // |delegate_|. 53 virtual void Hide() OVERRIDE; 54 55 // Invoked when the view was destroyed by by someone other than this class. 56 virtual void ViewDestroyed() OVERRIDE; 57 58 // KeyboardListener implementation. 59 virtual bool HandleKeyPressEvent( 60 const content::NativeWebKeyboardEvent& event) OVERRIDE; 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 gfx::NativeView container_view, 70 const gfx::RectF& element_bounds); 71 virtual ~AutofillPopupControllerImpl(); 72 73 // AutofillPopupController implementation. 74 virtual void UpdateBoundsAndRedrawPopup() OVERRIDE; 75 virtual void MouseHovered(int x, int y) OVERRIDE; 76 virtual void MouseClicked(int x, int y) OVERRIDE; 77 virtual void MouseExitedPopup() OVERRIDE; 78 virtual void AcceptSuggestion(size_t index) OVERRIDE; 79 virtual int GetIconResourceID(const string16& resource_name) OVERRIDE; 80 virtual bool CanDelete(size_t index) const OVERRIDE; 81 virtual gfx::Rect GetRowBounds(size_t index) OVERRIDE; 82 virtual void SetPopupBounds(const gfx::Rect& bounds) OVERRIDE; 83 virtual const gfx::Rect& popup_bounds() const OVERRIDE; 84 virtual gfx::NativeView container_view() const OVERRIDE; 85 virtual const gfx::RectF& element_bounds() const OVERRIDE; 86 87 virtual const std::vector<string16>& names() const OVERRIDE; 88 virtual const std::vector<string16>& subtexts() const OVERRIDE; 89 virtual const std::vector<string16>& icons() const OVERRIDE; 90 virtual const std::vector<int>& identifiers() const OVERRIDE; 91#if !defined(OS_ANDROID) 92 virtual const gfx::Font& GetNameFontForRow(size_t index) const OVERRIDE; 93 virtual const gfx::Font& subtext_font() const OVERRIDE; 94#endif 95 virtual int selected_line() const OVERRIDE; 96 97 // Change which line is currently selected by the user. 98 void SetSelectedLine(int selected_line); 99 100 // Increase the selected line by 1, properly handling wrapping. 101 void SelectNextLine(); 102 103 // Decrease the selected line by 1, properly handling wrapping. 104 void SelectPreviousLine(); 105 106 // The user has choosen the selected line. 107 bool AcceptSelectedLine(); 108 109 // The user has removed a suggestion. 110 bool RemoveSelectedLine(); 111 112 // Convert a y-coordinate to the closest line. 113 int LineFromY(int y); 114 115 // Returns the height of a row depending on its type. 116 int GetRowHeightFromId(int identifier) const; 117 118 // Returns true if the given id refers to an element that can be accepted. 119 bool CanAccept(int id); 120 121 // Returns true if the popup still has non-options entries to show the user. 122 bool HasSuggestions(); 123 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<string16>& names, 127 const std::vector<string16>& subtexts, 128 const std::vector<string16>& icons, 129 const std::vector<int>& identifier); 130 131 AutofillPopupView* view() { return view_; } 132 133 // |view_| pass throughs (virtual for testing). 134 virtual void ShowView(); 135 virtual void InvalidateRow(size_t row); 136 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; 141 142 // Calculates the desired height of the popup based on its contents. 143 int GetDesiredPopupHeight() const; 144 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 150 151 base::WeakPtr<AutofillPopupControllerImpl> GetWeakPtr(); 152 153 private: 154 // Clear the internal state of the controller. This is needed to ensure that 155 // when the popup is reused it doesn't leak values between uses. 156 void ClearState(); 157 158 const gfx::Rect RoundedElementBounds() const; 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(); 163#endif 164 165 // A helper function to get the display closest to the given point (virtual 166 // for testing). 167 virtual gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const; 168 169 // Calculates the width of the popup and the x position of it. These values 170 // will stay on the screen. 171 std::pair<int, int> CalculatePopupXAndWidth( 172 const gfx::Display& left_display, 173 const gfx::Display& right_display, 174 int popup_required_width) const; 175 176 // Calculates the height of the popup and the y position of it. These values 177 // will stay on the screen. 178 std::pair<int, int> CalculatePopupYAndHeight( 179 const gfx::Display& top_display, 180 const gfx::Display& bottom_display, 181 int popup_required_height) const; 182 183 AutofillPopupView* view_; // Weak reference. 184 base::WeakPtr<AutofillPopupDelegate> delegate_; 185 gfx::NativeView container_view_; // Weak reference. 186 187 // The bounds of the text element that is the focus of the Autofill. 188 // These coordinates are in screen space. 189 const gfx::RectF element_bounds_; 190 191 // The bounds of the Autofill popup. 192 gfx::Rect popup_bounds_; 193 194 // The current Autofill query values. 195 std::vector<string16> names_; 196 std::vector<string16> subtexts_; 197 std::vector<string16> icons_; 198 std::vector<int> identifiers_; 199 200 // Since names_ can be elided to ensure that it fits on the screen, we need to 201 // keep an unelided copy of the names to be able to pass to the delegate. 202 std::vector<string16> full_names_; 203 204#if !defined(OS_ANDROID) 205 // The fonts for the popup text. 206 gfx::Font name_font_; 207 gfx::Font subtext_font_; 208 gfx::Font warning_font_; 209#endif 210 211 // The line that is currently selected by the user. 212 // |kNoSelection| indicates that no line is currently selected. 213 int selected_line_; 214 215 base::WeakPtrFactory<AutofillPopupControllerImpl> weak_ptr_factory_; 216}; 217 218} // namespace autofill 219 220#endif // CHROME_BROWSER_UI_AUTOFILL_AUTOFILL_POPUP_CONTROLLER_IMPL_H_ 221