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.
5 #ifndef COMPONENTS_AUTOFILL_CONTENT_RENDERER_AUTOFILL_AGENT_H_
6 #define COMPONENTS_AUTOFILL_CONTENT_RENDERER_AUTOFILL_AGENT_H_
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"
27 struct WebAutocompleteParams;
34 struct WebElementDescriptor;
35 class PasswordAutofillAgent;
36 class PasswordGenerationAgent;
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.
46 class AutofillAgent : public content::RenderViewObserver,
47 public PageClickListener,
48 public blink::WebAutofillClient {
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();
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 DidChangeScrollOffset(blink::WebLocalFrame* frame) OVERRIDE;
69 virtual void FocusedNodeChanged(const blink::WebNode& node) OVERRIDE;
70 virtual void OrientationChangeEvent() OVERRIDE;
73 virtual void FormControlElementClicked(
74 const blink::WebFormControlElement& element,
75 bool was_focused) OVERRIDE;
77 // blink::WebAutofillClient:
78 virtual void textFieldDidEndEditing(
79 const blink::WebInputElement& element);
80 virtual void textFieldDidChange(
81 const blink::WebFormControlElement& element);
82 virtual void textFieldDidReceiveKeyDown(
83 const blink::WebInputElement& element,
84 const blink::WebKeyboardEvent& event);
85 virtual void didRequestAutocomplete(
86 const blink::WebFormElement& form);
87 virtual void setIgnoreTextChanges(bool ignore);
88 virtual void didAssociateFormControls(
89 const blink::WebVector<blink::WebNode>& nodes);
90 virtual void openTextDataListChooser(const blink::WebInputElement& element);
91 virtual void firstUserGestureObserved();
93 void OnFieldTypePredictionsAvailable(
94 const std::vector<FormDataPredictions>& forms);
95 void OnFillForm(int query_id, const FormData& form);
97 void OnPreviewForm(int query_id, const FormData& form);
99 // For external Autofill selection.
101 void OnClearPreviewedForm();
102 void OnFillFieldWithValue(const base::string16& value);
103 void OnPreviewFieldWithValue(const base::string16& value);
104 void OnAcceptDataListSuggestion(const base::string16& value);
105 void OnFillPasswordSuggestion(const base::string16& username,
106 const base::string16& password);
107 void OnPreviewPasswordSuggestion(const base::string16& username,
108 const base::string16& password);
110 // Called when interactive autocomplete finishes. |message| is printed to
111 // the console if non-empty.
112 void OnRequestAutocompleteResult(
113 blink::WebFormElement::AutocompleteResult result,
114 const base::string16& message,
115 const FormData& form_data);
117 // Called when an autocomplete request succeeds or fails with the |result|.
118 void FinishAutocompleteRequest(
119 blink::WebFormElement::AutocompleteResult result);
121 // Called in a posted task by textFieldDidChange() to work-around a WebKit bug
122 // http://bugs.webkit.org/show_bug.cgi?id=16976
123 void TextFieldDidChangeImpl(const blink::WebFormControlElement& element);
125 // Shows the autofill suggestions for |element|.
126 // This call is asynchronous and may or may not lead to the showing of a
127 // suggestion popup (no popup is shown if there are no available suggestions).
128 // |autofill_on_empty_values| specifies whether suggestions should be shown
129 // when |element| contains no text.
130 // |requires_caret_at_end| specifies whether suggestions should be shown when
131 // the caret is not after the last character in |element|.
132 // |display_warning_if_disabled| specifies whether a warning should be
133 // displayed to the user if Autofill has suggestions available, but cannot
134 // fill them because it is disabled (e.g. when trying to fill a credit card
135 // form on a non-secure website).
136 // |datalist_only| specifies whether all of <datalist> suggestions and no
137 // autofill suggestions are shown. |autofill_on_empty_values| and
138 // |requires_caret_at_end| are ignored if |datalist_only| is true.
139 // |show_full_suggestion_list| specifies that all autofill suggestions should
140 // be shown and none should be elided because of the current value of
141 // |element| (relevant for inline autocomplete).
142 // |show_password_suggestions_only| specifies that only show a suggestions box
143 // if |element| is part of a password form, otherwise show no suggestions.
144 void ShowSuggestions(const blink::WebFormControlElement& element,
145 bool autofill_on_empty_values,
146 bool requires_caret_at_end,
147 bool display_warning_if_disabled,
149 bool show_full_suggestion_list,
150 bool show_password_suggestions_only);
152 // Queries the browser for Autocomplete and Autofill suggestions for the given
154 void QueryAutofillSuggestions(const blink::WebFormControlElement& element,
155 bool display_warning_if_disabled,
158 // Sets the element value to reflect the selected |suggested_value|.
159 void AcceptDataListSuggestion(const base::string16& suggested_value);
161 // Fills |form| and |field| with the FormData and FormField corresponding to
162 // |node|. Returns true if the data was found; and false otherwise.
163 bool FindFormAndFieldForNode(
164 const blink::WebNode& node,
166 FormFieldData* field) WARN_UNUSED_RESULT;
168 // Set |node| to display the given |value|.
169 void FillFieldWithValue(const base::string16& value,
170 blink::WebInputElement* node);
172 // Set |node| to display the given |value| as a preview. The preview is
173 // visible on screen to the user, but not visible to the page via the DOM or
175 void PreviewFieldWithValue(const base::string16& value,
176 blink::WebInputElement* node);
178 // Notifies browser of new fillable forms in |frame|.
179 void ProcessForms(const blink::WebLocalFrame& frame);
181 // Hides any currently showing Autofill popup.
184 FormCache form_cache_;
186 PasswordAutofillAgent* password_autofill_agent_; // Weak reference.
187 PasswordGenerationAgent* password_generation_agent_; // Weak reference.
189 // The ID of the last request sent for form field Autofill. Used to ignore
190 // out of date responses.
191 int autofill_query_id_;
193 // The element corresponding to the last request sent for form field Autofill.
194 blink::WebFormControlElement element_;
196 // The form element currently requesting an interactive autocomplete.
197 blink::WebFormElement in_flight_request_form_;
199 // Pointer to the WebView. Used to access page scale factor.
200 blink::WebView* web_view_;
202 // Should we display a warning if autofill is disabled?
203 bool display_warning_if_disabled_;
205 // Was the query node autofilled prior to previewing the form?
206 bool was_query_node_autofilled_;
208 // Have we already shown Autofill suggestions for the field the user is
209 // currently editing? Used to keep track of state for metrics logging.
210 bool has_shown_autofill_popup_for_current_edit_;
212 // If true we just set the node text so we shouldn't show the popup.
213 bool did_set_node_text_;
215 // Whether or not to ignore text changes. Useful for when we're committing
216 // a composition when we are defocusing the WebView and we don't want to
217 // trigger an autofill popup to show.
218 bool ignore_text_changes_;
220 // Whether the Autofill popup is possibly visible. This is tracked as a
221 // performance improvement, so that the IPC channel isn't flooded with
222 // messages to close the Autofill popup when it can't possibly be showing.
223 bool is_popup_possibly_visible_;
225 // True if a message has already been sent about forms for the main frame.
226 // When the main frame is first loaded, a message is sent even if no forms
227 // exist in the frame. Otherwise, such messages are supressed.
228 bool main_frame_processed_;
230 base::WeakPtrFactory<AutofillAgent> weak_ptr_factory_;
232 friend class PasswordAutofillAgentTest;
233 friend class RequestAutocompleteRendererTest;
234 FRIEND_TEST_ALL_PREFIXES(AutofillRendererTest, FillFormElement);
235 FRIEND_TEST_ALL_PREFIXES(AutofillRendererTest, SendDynamicForms);
236 FRIEND_TEST_ALL_PREFIXES(AutofillRendererTest, ShowAutofillWarning);
237 FRIEND_TEST_ALL_PREFIXES(PasswordAutofillAgentTest, WaitUsername);
238 FRIEND_TEST_ALL_PREFIXES(PasswordAutofillAgentTest, SuggestionAccept);
239 FRIEND_TEST_ALL_PREFIXES(PasswordAutofillAgentTest, SuggestionSelect);
240 FRIEND_TEST_ALL_PREFIXES(
241 PasswordAutofillAgentTest,
242 PasswordAutofillTriggersOnChangeEventsWaitForUsername);
243 FRIEND_TEST_ALL_PREFIXES(PasswordAutofillAgentTest, CredentialsOnClick);
244 FRIEND_TEST_ALL_PREFIXES(RequestAutocompleteRendererTest,
245 NoCancelOnMainFrameNavigateAfterDone);
246 FRIEND_TEST_ALL_PREFIXES(RequestAutocompleteRendererTest,
247 NoCancelOnSubframeNavigateAfterDone);
248 FRIEND_TEST_ALL_PREFIXES(RequestAutocompleteRendererTest,
249 InvokingTwiceOnlyShowsOnce);
251 DISALLOW_COPY_AND_ASSIGN(AutofillAgent);
254 } // namespace autofill
256 #endif // COMPONENTS_AUTOFILL_CONTENT_RENDERER_AUTOFILL_AGENT_H_