1 // Copyright 2013 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 COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_EXTERNAL_DELEGATE_H_ 6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_EXTERNAL_DELEGATE_H_ 7 8 #include <vector> 9 10 #include "base/compiler_specific.h" 11 #include "base/memory/weak_ptr.h" 12 #include "base/strings/string16.h" 13 #include "components/autofill/core/browser/autofill_popup_delegate.h" 14 #include "components/autofill/core/browser/password_autofill_manager.h" 15 #include "components/autofill/core/common/form_data.h" 16 #include "components/autofill/core/common/form_field_data.h" 17 #include "components/autofill/core/common/password_form_fill_data.h" 18 #include "ui/gfx/rect.h" 19 20 namespace gfx { 21 class Rect; 22 } 23 24 namespace content { 25 class RenderViewHost; 26 class WebContents; 27 } 28 29 namespace autofill { 30 31 class AutofillDriver; 32 class AutofillManager; 33 34 // TODO(csharp): A lot of the logic in this class is copied from autofillagent. 35 // Once Autofill is moved out of WebKit this class should be the only home for 36 // this logic. See http://crbug.com/51644 37 38 // Delegate for in-browser Autocomplete and Autofill display and selection. 39 class AutofillExternalDelegate 40 : public AutofillPopupDelegate { 41 public: 42 // Creates an AutofillExternalDelegate for the specified contents, 43 // AutofillManager, and AutofillDriver. 44 AutofillExternalDelegate(content::WebContents* web_contents, 45 AutofillManager* autofill_manager, 46 AutofillDriver* autofill_driver); 47 virtual ~AutofillExternalDelegate(); 48 49 // AutofillPopupDelegate implementation. 50 virtual void OnPopupShown(content::KeyboardListener* listener) OVERRIDE; 51 virtual void OnPopupHidden(content::KeyboardListener* listener) OVERRIDE; 52 virtual void DidSelectSuggestion(int identifier) OVERRIDE; 53 virtual void DidAcceptSuggestion(const base::string16& value, 54 int identifier) OVERRIDE; 55 virtual void RemoveSuggestion(const base::string16& value, 56 int identifier) OVERRIDE; 57 virtual void ClearPreviewedForm() OVERRIDE; 58 59 // Records and associates a query_id with web form data. Called 60 // when the renderer posts an Autofill query to the browser. |bounds| 61 // is window relative. |display_warning_if_disabled| tells us if we should 62 // display warnings (such as autofill is disabled, but had suggestions). 63 // We might not want to display the warning if a website has disabled 64 // Autocomplete because they have their own popup, and showing our popup 65 // on to of theirs would be a poor user experience. 66 virtual void OnQuery(int query_id, 67 const FormData& form, 68 const FormFieldData& field, 69 const gfx::RectF& element_bounds, 70 bool display_warning_if_disabled); 71 72 // Records query results and correctly formats them before sending them off 73 // to be displayed. Called when an Autofill query result is available. 74 virtual void OnSuggestionsReturned( 75 int query_id, 76 const std::vector<base::string16>& autofill_values, 77 const std::vector<base::string16>& autofill_labels, 78 const std::vector<base::string16>& autofill_icons, 79 const std::vector<int>& autofill_unique_ids); 80 81 // Show password suggestions in the popup. 82 void OnShowPasswordSuggestions(const std::vector<base::string16>& suggestions, 83 const std::vector<base::string16>& realms, 84 const FormFieldData& field, 85 const gfx::RectF& bounds); 86 87 // Set the data list value associated with the current field. 88 void SetCurrentDataListValues( 89 const std::vector<base::string16>& data_list_values, 90 const std::vector<base::string16>& data_list_labels); 91 92 // Inform the delegate that the text field editing has ended. This is 93 // used to help record the metrics of when a new popup is shown. 94 void DidEndTextFieldEditing(); 95 96 // Returns the delegate to its starting state by removing any page specific 97 // values or settings. 98 void Reset(); 99 100 // Inform the Password Manager of a filled form. 101 void AddPasswordFormMapping( 102 const FormFieldData& form, 103 const PasswordFormFillData& fill_data); 104 105 protected: 106 content::WebContents* web_contents() { return web_contents_; } 107 108 base::WeakPtr<AutofillExternalDelegate> GetWeakPtr(); 109 110 private: 111 // Fills the form with the Autofill data corresponding to |unique_id|. 112 // If |is_preview| is true then this is just a preview to show the user what 113 // would be selected and if |is_preview| is false then the user has selected 114 // this data. 115 void FillAutofillFormData(int unique_id, bool is_preview); 116 117 // Handle applying any Autofill warnings to the Autofill popup. 118 void ApplyAutofillWarnings(std::vector<base::string16>* autofill_values, 119 std::vector<base::string16>* autofill_labels, 120 std::vector<base::string16>* autofill_icons, 121 std::vector<int>* autofill_unique_ids); 122 123 // Handle applying any Autofill option listings to the Autofill popup. 124 // This function should only get called when there is at least one 125 // multi-field suggestion in the list of suggestions. 126 void ApplyAutofillOptions(std::vector<base::string16>* autofill_values, 127 std::vector<base::string16>* autofill_labels, 128 std::vector<base::string16>* autofill_icons, 129 std::vector<int>* autofill_unique_ids); 130 131 // Insert the data list values at the start of the given list, including 132 // any required separators. 133 void InsertDataListValues(std::vector<base::string16>* autofill_values, 134 std::vector<base::string16>* autofill_labels, 135 std::vector<base::string16>* autofill_icons, 136 std::vector<int>* autofill_unique_ids); 137 138 // The web_contents associated with this delegate. 139 content::WebContents* web_contents_; // weak; owns me. 140 AutofillManager* autofill_manager_; // weak. 141 142 // Provides driver-level context to the shared code of the component. Must 143 // outlive this object. 144 AutofillDriver* autofill_driver_; // weak 145 146 // Password Autofill manager, handles all password-related Autofilling. 147 PasswordAutofillManager password_autofill_manager_; 148 149 // The ID of the last request sent for form field Autofill. Used to ignore 150 // out of date responses. 151 int autofill_query_id_; 152 153 // The current form and field selected by Autofill. 154 FormData autofill_query_form_; 155 FormFieldData autofill_query_field_; 156 157 // The bounds of the form field that user is interacting with. 158 gfx::RectF element_bounds_; 159 160 // Should we display a warning if Autofill is disabled? 161 bool display_warning_if_disabled_; 162 163 // Does the popup include any Autofill profile or credit card suggestions? 164 bool has_autofill_suggestion_; 165 166 // Have we already shown Autofill suggestions for the field the user is 167 // currently editing? Used to keep track of state for metrics logging. 168 bool has_shown_autofill_popup_for_current_edit_; 169 170 // The RenderViewHost that this object has been registered with as a 171 // keyboard listener. 172 content::RenderViewHost* registered_keyboard_listener_with_; 173 174 // The current data list values. 175 std::vector<base::string16> data_list_values_; 176 std::vector<base::string16> data_list_labels_; 177 178 base::WeakPtrFactory<AutofillExternalDelegate> weak_ptr_factory_; 179 180 DISALLOW_COPY_AND_ASSIGN(AutofillExternalDelegate); 181 }; 182 183 } // namespace autofill 184 185 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_EXTERNAL_DELEGATE_H_ 186