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_CONTENT_RENDERER_AUTOFILL_AGENT_H_ 6 #define COMPONENTS_AUTOFILL_CONTENT_RENDERER_AUTOFILL_AGENT_H_ 7 8 #include <vector> 9 10 #include "base/basictypes.h" 11 #include "base/compiler_specific.h" 12 #include "base/gtest_prod_util.h" 13 #include "base/memory/weak_ptr.h" 14 #include "base/time/time.h" 15 #include "base/timer/timer.h" 16 #include "components/autofill/content/renderer/form_cache.h" 17 #include "components/autofill/content/renderer/page_click_listener.h" 18 #include "content/public/renderer/render_view_observer.h" 19 #include "third_party/WebKit/public/web/WebAutofillClient.h" 20 #include "third_party/WebKit/public/web/WebFormControlElement.h" 21 #include "third_party/WebKit/public/web/WebFormElement.h" 22 #include "third_party/WebKit/public/web/WebInputElement.h" 23 24 namespace blink { 25 class WebNode; 26 class WebView; 27 struct WebAutocompleteParams; 28 } 29 30 namespace autofill { 31 32 struct FormData; 33 struct FormFieldData; 34 struct WebElementDescriptor; 35 class PasswordAutofillAgent; 36 class PasswordGenerationAgent; 37 38 // AutofillAgent deals with Autofill related communications between WebKit and 39 // the browser. There is one AutofillAgent per RenderView. 40 // This code was originally part of RenderView. 41 // Note that Autofill encompasses: 42 // - single text field suggestions, that we usually refer to as Autocomplete, 43 // - password form fill, refered to as Password Autofill, and 44 // - entire form fill based on one field entry, referred to as Form Autofill. 45 46 class AutofillAgent : public content::RenderViewObserver, 47 public PageClickListener, 48 public blink::WebAutofillClient { 49 public: 50 // PasswordAutofillAgent is guaranteed to outlive AutofillAgent. 51 // PasswordGenerationAgent may be NULL. If it is not, then it is also 52 // guaranteed to outlive AutofillAgent. 53 AutofillAgent(content::RenderView* render_view, 54 PasswordAutofillAgent* password_autofill_manager, 55 PasswordGenerationAgent* password_generation_agent); 56 virtual ~AutofillAgent(); 57 58 private: 59 // content::RenderViewObserver: 60 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 61 virtual void DidFinishDocumentLoad(blink::WebLocalFrame* frame) OVERRIDE; 62 virtual void DidCommitProvisionalLoad(blink::WebLocalFrame* frame, 63 bool is_new_navigation) OVERRIDE; 64 virtual void FrameDetached(blink::WebFrame* frame) OVERRIDE; 65 virtual void FrameWillClose(blink::WebFrame* frame) OVERRIDE; 66 virtual void WillSubmitForm(blink::WebLocalFrame* frame, 67 const blink::WebFormElement& form) OVERRIDE; 68 virtual void ZoomLevelChanged() OVERRIDE; 69 virtual void DidChangeScrollOffset(blink::WebLocalFrame* frame) OVERRIDE; 70 virtual void FocusedNodeChanged(const blink::WebNode& node) OVERRIDE; 71 virtual void OrientationChangeEvent() OVERRIDE; 72 73 // PageClickListener: 74 virtual void FormControlElementClicked( 75 const blink::WebFormControlElement& element, 76 bool was_focused) OVERRIDE; 77 virtual void FormControlElementLostFocus() OVERRIDE; 78 79 // blink::WebAutofillClient: 80 virtual void textFieldDidEndEditing( 81 const blink::WebInputElement& element); 82 virtual void textFieldDidChange( 83 const blink::WebFormControlElement& element); 84 virtual void textFieldDidReceiveKeyDown( 85 const blink::WebInputElement& element, 86 const blink::WebKeyboardEvent& event); 87 // TODO(estade): remove. 88 virtual void didRequestAutocomplete( 89 const blink::WebFormElement& form, 90 const blink::WebAutocompleteParams& details); 91 virtual void didRequestAutocomplete( 92 const blink::WebFormElement& form); 93 virtual void setIgnoreTextChanges(bool ignore); 94 virtual void didAssociateFormControls( 95 const blink::WebVector<blink::WebNode>& nodes); 96 virtual void openTextDataListChooser(const blink::WebInputElement& element); 97 virtual void firstUserGestureObserved(); 98 99 void OnFieldTypePredictionsAvailable( 100 const std::vector<FormDataPredictions>& forms); 101 void OnFillForm(int query_id, const FormData& form); 102 void OnPing(); 103 void OnPreviewForm(int query_id, const FormData& form); 104 105 // For external Autofill selection. 106 void OnClearForm(); 107 void OnClearPreviewedForm(); 108 void OnFillFieldWithValue(const base::string16& value); 109 void OnPreviewFieldWithValue(const base::string16& value); 110 void OnAcceptDataListSuggestion(const base::string16& value); 111 void OnFillPasswordSuggestion(const base::string16& username, 112 const base::string16& password); 113 void OnPreviewPasswordSuggestion(const base::string16& username, 114 const base::string16& password); 115 116 // Called when interactive autocomplete finishes. |message| is printed to 117 // the console if non-empty. 118 void OnRequestAutocompleteResult( 119 blink::WebFormElement::AutocompleteResult result, 120 const base::string16& message, 121 const FormData& form_data); 122 123 // Called when an autocomplete request succeeds or fails with the |result|. 124 void FinishAutocompleteRequest( 125 blink::WebFormElement::AutocompleteResult result); 126 127 // Called in a posted task by textFieldDidChange() to work-around a WebKit bug 128 // http://bugs.webkit.org/show_bug.cgi?id=16976 129 void TextFieldDidChangeImpl(const blink::WebFormControlElement& element); 130 131 // Shows the autofill suggestions for |element|. 132 // This call is asynchronous and may or may not lead to the showing of a 133 // suggestion popup (no popup is shown if there are no available suggestions). 134 // |autofill_on_empty_values| specifies whether suggestions should be shown 135 // when |element| contains no text. 136 // |requires_caret_at_end| specifies whether suggestions should be shown when 137 // the caret is not after the last character in |element|. 138 // |display_warning_if_disabled| specifies whether a warning should be 139 // displayed to the user if Autofill has suggestions available, but cannot 140 // fill them because it is disabled (e.g. when trying to fill a credit card 141 // form on a non-secure website). 142 // |datalist_only| specifies whether all of <datalist> suggestions and no 143 // autofill suggestions are shown. |autofill_on_empty_values| and 144 // |requires_caret_at_end| are ignored if |datalist_only| is true. 145 void ShowSuggestions(const blink::WebFormControlElement& element, 146 bool autofill_on_empty_values, 147 bool requires_caret_at_end, 148 bool display_warning_if_disabled, 149 bool datalist_only); 150 151 // Queries the browser for Autocomplete and Autofill suggestions for the given 152 // |element|. 153 void QueryAutofillSuggestions(const blink::WebFormControlElement& element, 154 bool display_warning_if_disabled, 155 bool datalist_only); 156 157 // Sets the element value to reflect the selected |suggested_value|. 158 void AcceptDataListSuggestion(const base::string16& suggested_value); 159 160 // Fills |form| and |field| with the FormData and FormField corresponding to 161 // |node|. Returns true if the data was found; and false otherwise. 162 bool FindFormAndFieldForNode( 163 const blink::WebNode& node, 164 FormData* form, 165 FormFieldData* field) WARN_UNUSED_RESULT; 166 167 // Set |node| to display the given |value|. 168 void FillFieldWithValue(const base::string16& value, 169 blink::WebInputElement* node); 170 171 // Set |node| to display the given |value| as a preview. The preview is 172 // visible on screen to the user, but not visible to the page via the DOM or 173 // JavaScript. 174 void PreviewFieldWithValue(const base::string16& value, 175 blink::WebInputElement* node); 176 177 // Notifies browser of new fillable forms in |frame|. 178 void ProcessForms(const blink::WebLocalFrame& frame); 179 180 // Hides any currently showing Autofill popup. 181 void HidePopup(); 182 183 FormCache form_cache_; 184 185 PasswordAutofillAgent* password_autofill_agent_; // Weak reference. 186 PasswordGenerationAgent* password_generation_agent_; // Weak reference. 187 188 // The ID of the last request sent for form field Autofill. Used to ignore 189 // out of date responses. 190 int autofill_query_id_; 191 192 // The element corresponding to the last request sent for form field Autofill. 193 blink::WebFormControlElement element_; 194 195 // The form element currently requesting an interactive autocomplete. 196 blink::WebFormElement in_flight_request_form_; 197 198 // All the form elements seen in the top frame. 199 std::vector<blink::WebFormElement> form_elements_; 200 201 // Pointer to the WebView. Used to access page scale factor. 202 blink::WebView* web_view_; 203 204 // Should we display a warning if autofill is disabled? 205 bool display_warning_if_disabled_; 206 207 // Was the query node autofilled prior to previewing the form? 208 bool was_query_node_autofilled_; 209 210 // Have we already shown Autofill suggestions for the field the user is 211 // currently editing? Used to keep track of state for metrics logging. 212 bool has_shown_autofill_popup_for_current_edit_; 213 214 // If true we just set the node text so we shouldn't show the popup. 215 bool did_set_node_text_; 216 217 // Whether or not new forms/fields have been dynamically added 218 // since the last loaded forms were sent to the browser process. 219 bool has_new_forms_for_browser_; 220 221 // Whether or not to ignore text changes. Useful for when we're committing 222 // a composition when we are defocusing the WebView and we don't want to 223 // trigger an autofill popup to show. 224 bool ignore_text_changes_; 225 226 // Whether the Autofill popup is possibly visible. This is tracked as a 227 // performance improvement, so that the IPC channel isn't flooded with 228 // messages to close the Autofill popup when it can't possibly be showing. 229 bool is_popup_possibly_visible_; 230 231 // True if a message has already been sent about forms for the main frame. 232 // When the main frame is first loaded, a message is sent even if no forms 233 // exist in the frame. Otherwise, such messages are supressed. 234 bool main_frame_processed_; 235 236 base::WeakPtrFactory<AutofillAgent> weak_ptr_factory_; 237 238 friend class PasswordAutofillAgentTest; 239 friend class RequestAutocompleteRendererTest; 240 FRIEND_TEST_ALL_PREFIXES(AutofillRendererTest, FillFormElement); 241 FRIEND_TEST_ALL_PREFIXES(AutofillRendererTest, SendDynamicForms); 242 FRIEND_TEST_ALL_PREFIXES(AutofillRendererTest, ShowAutofillWarning); 243 FRIEND_TEST_ALL_PREFIXES(PasswordAutofillAgentTest, WaitUsername); 244 FRIEND_TEST_ALL_PREFIXES(PasswordAutofillAgentTest, SuggestionAccept); 245 FRIEND_TEST_ALL_PREFIXES(PasswordAutofillAgentTest, SuggestionSelect); 246 FRIEND_TEST_ALL_PREFIXES( 247 PasswordAutofillAgentTest, 248 PasswordAutofillTriggersOnChangeEventsWaitForUsername); 249 FRIEND_TEST_ALL_PREFIXES(RequestAutocompleteRendererTest, 250 NoCancelOnMainFrameNavigateAfterDone); 251 FRIEND_TEST_ALL_PREFIXES(RequestAutocompleteRendererTest, 252 NoCancelOnSubframeNavigateAfterDone); 253 FRIEND_TEST_ALL_PREFIXES(RequestAutocompleteRendererTest, 254 InvokingTwiceOnlyShowsOnce); 255 256 DISALLOW_COPY_AND_ASSIGN(AutofillAgent); 257 }; 258 259 } // namespace autofill 260 261 #endif // COMPONENTS_AUTOFILL_CONTENT_RENDERER_AUTOFILL_AGENT_H_ 262