autofill_popup_controller_impl.h revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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 "content/public/browser/keyboard_listener.h" 14#include "ui/gfx/font.h" 15#include "ui/gfx/rect.h" 16#include "ui/gfx/rect_f.h" 17 18namespace gfx { 19class Display; 20} 21 22namespace ui { 23class KeyEvent; 24} 25 26namespace autofill { 27 28class AutofillPopupDelegate; 29class AutofillPopupView; 30 31// This class is a controller for an AutofillPopupView. It implements 32// AutofillPopupController to allow calls from AutofillPopupView. The 33// other, public functions are available to its instantiator. 34class AutofillPopupControllerImpl : public AutofillPopupController, 35 public content::KeyboardListener { 36 public: 37 // Creates a new |AutofillPopupControllerImpl|, or reuses |previous| if 38 // the construction arguments are the same. |previous| may be invalidated by 39 // this call. 40 static base::WeakPtr<AutofillPopupControllerImpl> GetOrCreate( 41 base::WeakPtr<AutofillPopupControllerImpl> previous, 42 base::WeakPtr<AutofillPopupDelegate> delegate, 43 gfx::NativeView container_view, 44 const gfx::RectF& element_bounds, 45 base::i18n::TextDirection text_direction); 46 47 // Shows the popup, or updates the existing popup with the given values. 48 void Show(const std::vector<string16>& names, 49 const std::vector<string16>& subtexts, 50 const std::vector<string16>& icons, 51 const std::vector<int>& identifiers); 52 53 // Hides the popup and destroys the controller. This also invalidates 54 // |delegate_|. 55 virtual void Hide() OVERRIDE; 56 57 // Invoked when the view was destroyed by by someone other than this class. 58 virtual void ViewDestroyed() OVERRIDE; 59 60 // KeyboardListener implementation. 61 virtual bool HandleKeyPressEvent( 62 const content::NativeWebKeyboardEvent& event) OVERRIDE; 63 64 protected: 65 FRIEND_TEST_ALL_PREFIXES(AutofillExternalDelegateBrowserTest, 66 CloseWidgetAndNoLeaking); 67 FRIEND_TEST_ALL_PREFIXES(AutofillPopupControllerUnitTest, 68 ProperlyResetController); 69 70 AutofillPopupControllerImpl(base::WeakPtr<AutofillPopupDelegate> delegate, 71 gfx::NativeView container_view, 72 const gfx::RectF& element_bounds, 73 base::i18n::TextDirection text_direction); 74 virtual ~AutofillPopupControllerImpl(); 75 76 // AutofillPopupController implementation. 77 virtual void UpdateBoundsAndRedrawPopup() OVERRIDE; 78 virtual void MouseHovered(int x, int y) OVERRIDE; 79 virtual void MouseClicked(int x, int y) OVERRIDE; 80 virtual void MouseExitedPopup() OVERRIDE; 81 virtual void AcceptSuggestion(size_t index) OVERRIDE; 82 virtual int GetIconResourceID(const string16& resource_name) 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() const OVERRIDE; 89 virtual const gfx::RectF& element_bounds() const OVERRIDE; 90 virtual bool IsRTL() const OVERRIDE; 91 92 virtual const std::vector<string16>& names() const OVERRIDE; 93 virtual const std::vector<string16>& subtexts() const OVERRIDE; 94 virtual const std::vector<string16>& icons() const OVERRIDE; 95 virtual const std::vector<int>& identifiers() const OVERRIDE; 96#if !defined(OS_ANDROID) 97 virtual const gfx::Font& GetNameFontForRow(size_t index) const OVERRIDE; 98 virtual const gfx::Font& subtext_font() const OVERRIDE; 99#endif 100 virtual int selected_line() const OVERRIDE; 101 102 // Change which line is currently selected by the user. 103 void SetSelectedLine(int selected_line); 104 105 // Increase the selected line by 1, properly handling wrapping. 106 void SelectNextLine(); 107 108 // Decrease the selected line by 1, properly handling wrapping. 109 void SelectPreviousLine(); 110 111 // The user has choosen the selected line. 112 bool AcceptSelectedLine(); 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<string16>& names, 132 const std::vector<string16>& subtexts, 133 const std::vector<string16>& icons, 134 const std::vector<int>& identifier); 135 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 private: 159 // Clear the internal state of the controller. This is needed to ensure that 160 // when the popup is reused it doesn't leak values between uses. 161 void ClearState(); 162 163 const gfx::Rect RoundedElementBounds() const; 164#if !defined(OS_ANDROID) 165 // Calculates and sets the bounds of the popup, including placing it properly 166 // to prevent it from going off the screen. 167 void UpdatePopupBounds(); 168#endif 169 170 // A helper function to get the display closest to the given point (virtual 171 // for testing). 172 virtual gfx::Display GetDisplayNearestPoint(const gfx::Point& point) const; 173 174 // Calculates the width of the popup and the x position of it. These values 175 // will stay on the screen. 176 std::pair<int, int> CalculatePopupXAndWidth( 177 const gfx::Display& left_display, 178 const gfx::Display& right_display, 179 int popup_required_width) const; 180 181 // Calculates the height of the popup and the y position of it. These values 182 // will stay on the screen. 183 std::pair<int, int> CalculatePopupYAndHeight( 184 const gfx::Display& top_display, 185 const gfx::Display& bottom_display, 186 int popup_required_height) const; 187 188 AutofillPopupView* view_; // Weak reference. 189 base::WeakPtr<AutofillPopupDelegate> delegate_; 190 gfx::NativeView container_view_; // Weak reference. 191 192 // The bounds of the text element that is the focus of the Autofill. 193 // These coordinates are in screen space. 194 const gfx::RectF element_bounds_; 195 196 // The bounds of the Autofill popup. 197 gfx::Rect popup_bounds_; 198 199 // The text direction of the popup. 200 base::i18n::TextDirection text_direction_; 201 202 // The current Autofill query values. 203 std::vector<string16> names_; 204 std::vector<string16> subtexts_; 205 std::vector<string16> icons_; 206 std::vector<int> identifiers_; 207 208 // Since names_ can be elided to ensure that it fits on the screen, we need to 209 // keep an unelided copy of the names to be able to pass to the delegate. 210 std::vector<string16> full_names_; 211 212#if !defined(OS_ANDROID) 213 // The fonts for the popup text. 214 gfx::Font name_font_; 215 gfx::Font subtext_font_; 216 gfx::Font warning_font_; 217#endif 218 219 // The line that is currently selected by the user. 220 // |kNoSelection| indicates that no line is currently selected. 221 int selected_line_; 222 223 base::WeakPtrFactory<AutofillPopupControllerImpl> weak_ptr_factory_; 224}; 225 226} // namespace autofill 227 228#endif // CHROME_BROWSER_UI_AUTOFILL_AUTOFILL_POPUP_CONTROLLER_IMPL_H_ 229