autofill_popup_controller_impl.h revision effb81e5f8246d0db0270817048dc992db66e9fb
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 18namespace autofill { 19 20class AutofillPopupDelegate; 21class 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. 26class 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 bool ShouldRepostEvent(const ui::MouseEvent& event) OVERRIDE; 81 virtual bool ShouldHideOnOutsideClick() const OVERRIDE; 82 virtual void AcceptSuggestion(size_t index) OVERRIDE; 83 virtual int GetIconResourceID( 84 const base::string16& resource_name) const OVERRIDE; 85 virtual bool CanDelete(size_t index) const OVERRIDE; 86 virtual bool IsWarning(size_t index) const OVERRIDE; 87 virtual gfx::Rect GetRowBounds(size_t index) OVERRIDE; 88 virtual void SetPopupBounds(const gfx::Rect& bounds) OVERRIDE; 89 virtual const gfx::Rect& popup_bounds() const OVERRIDE; 90 virtual gfx::NativeView container_view() OVERRIDE; 91 virtual const gfx::RectF& element_bounds() const OVERRIDE; 92 virtual bool IsRTL() const OVERRIDE; 93 94 virtual const std::vector<base::string16>& names() const OVERRIDE; 95 virtual const std::vector<base::string16>& subtexts() const OVERRIDE; 96 virtual const std::vector<base::string16>& icons() const OVERRIDE; 97 virtual const std::vector<int>& identifiers() const OVERRIDE; 98#if !defined(OS_ANDROID) 99 virtual const gfx::FontList& GetNameFontListForRow( 100 size_t index) const OVERRIDE; 101 virtual const gfx::FontList& subtext_font_list() const OVERRIDE; 102#endif 103 virtual int selected_line() const OVERRIDE; 104 105 content::WebContents* web_contents(); 106 107 // Change which line is currently selected by the user. 108 void SetSelectedLine(int selected_line); 109 110 // Increase the selected line by 1, properly handling wrapping. 111 void SelectNextLine(); 112 113 // Decrease the selected line by 1, properly handling wrapping. 114 void SelectPreviousLine(); 115 116 // The user has removed a suggestion. 117 bool RemoveSelectedLine(); 118 119 // Convert a y-coordinate to the closest line. 120 int LineFromY(int y); 121 122 // Returns the height of a row depending on its type. 123 int GetRowHeightFromId(int identifier) const; 124 125 // Returns true if the given id refers to an element that can be accepted. 126 bool CanAccept(int id); 127 128 // Returns true if the popup still has non-options entries to show the user. 129 bool HasSuggestions(); 130 131 // Set the Autofill entry values. Exposed to allow tests to set these values 132 // without showing the popup. 133 void SetValues(const std::vector<base::string16>& names, 134 const std::vector<base::string16>& subtexts, 135 const std::vector<base::string16>& icons, 136 const std::vector<int>& identifier); 137 138 AutofillPopupView* view() { return view_; } 139 140 // |view_| pass throughs (virtual for testing). 141 virtual void ShowView(); 142 virtual void InvalidateRow(size_t row); 143 144 // Protected so tests can access. 145#if !defined(OS_ANDROID) 146 // Calculates the desired width of the popup based on its contents. 147 int GetDesiredPopupWidth() const; 148 149 // Calculates the desired height of the popup based on its contents. 150 int GetDesiredPopupHeight() const; 151 152 // Calculate the width of the row, excluding all the text. This provides 153 // the size of the row that won't be reducible (since all the text can be 154 // elided if there isn't enough space). 155 int RowWidthWithoutText(int row) const; 156#endif 157 158 base::WeakPtr<AutofillPopupControllerImpl> GetWeakPtr(); 159 160 // Contains common popup functionality such as popup layout. Protected for 161 // testing. 162 scoped_ptr<PopupControllerCommon> controller_common_; 163 164 private: 165 // Clear the internal state of the controller. This is needed to ensure that 166 // when the popup is reused it doesn't leak values between uses. 167 void ClearState(); 168 169#if !defined(OS_ANDROID) 170 // Calculates and sets the bounds of the popup, including placing it properly 171 // to prevent it from going off the screen. 172 void UpdatePopupBounds(); 173#endif 174 175 AutofillPopupView* view_; // Weak reference. 176 base::WeakPtr<AutofillPopupDelegate> delegate_; 177 178 // The bounds of the Autofill popup. 179 gfx::Rect popup_bounds_; 180 181 // The text direction of the popup. 182 base::i18n::TextDirection text_direction_; 183 184 // The current Autofill query values. 185 std::vector<base::string16> names_; 186 std::vector<base::string16> subtexts_; 187 std::vector<base::string16> icons_; 188 std::vector<int> identifiers_; 189 190 // Since names_ can be elided to ensure that it fits on the screen, we need to 191 // keep an unelided copy of the names to be able to pass to the delegate. 192 std::vector<base::string16> full_names_; 193 194#if !defined(OS_ANDROID) 195 // The fonts for the popup text. 196 gfx::FontList name_font_list_; 197 gfx::FontList subtext_font_list_; 198 gfx::FontList warning_font_list_; 199#endif 200 201 // The line that is currently selected by the user. 202 // |kNoSelection| indicates that no line is currently selected. 203 int selected_line_; 204 205 // Whether the popup view should hide on mouse presses outside of it. 206 bool hide_on_outside_click_; 207 208 base::WeakPtrFactory<AutofillPopupControllerImpl> weak_ptr_factory_; 209}; 210 211} // namespace autofill 212 213#endif // CHROME_BROWSER_UI_AUTOFILL_AUTOFILL_POPUP_CONTROLLER_IMPL_H_ 214