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_
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/time/time.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_frame_observer.h"
19 #include "content/public/renderer/render_view_observer.h"
20 #include "third_party/WebKit/public/web/WebAutofillClient.h"
21 #include "third_party/WebKit/public/web/WebFormControlElement.h"
22 #include "third_party/WebKit/public/web/WebFormElement.h"
23 #include "third_party/WebKit/public/web/WebInputElement.h"
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 RenderFrame.
40 // Note that Autofill encompasses:
41 // - single text field suggestions, that we usually refer to as Autocomplete,
42 // - password form fill, refered to as Password Autofill, and
43 // - entire form fill based on one field entry, referred to as Form Autofill.
45 class AutofillAgent
: public content::RenderFrameObserver
,
46 public PageClickListener
,
47 public blink::WebAutofillClient
{
49 // PasswordAutofillAgent is guaranteed to outlive AutofillAgent.
50 // PasswordGenerationAgent may be NULL. If it is not, then it is also
51 // guaranteed to outlive AutofillAgent.
52 AutofillAgent(content::RenderFrame
* render_frame
,
53 PasswordAutofillAgent
* password_autofill_manager
,
54 PasswordGenerationAgent
* password_generation_agent
);
55 virtual ~AutofillAgent();
58 // Functor used as a simplified comparison function for FormData.
59 struct FormDataCompare
{
60 bool operator()(const FormData
& lhs
, const FormData
& rhs
) const;
63 // Thunk class for RenderViewObserver methods that haven't yet been migrated
64 // to RenderFrameObserver. Should eventually be removed.
65 // http://crbug.com/433486
66 class LegacyAutofillAgent
: public content::RenderViewObserver
{
68 LegacyAutofillAgent(content::RenderView
* render_view
, AutofillAgent
* agent
);
69 ~LegacyAutofillAgent() override
;
72 // content::RenderViewObserver:
73 void OnDestruct() override
;
74 void FocusChangeComplete() override
;
76 AutofillAgent
* agent_
;
78 DISALLOW_COPY_AND_ASSIGN(LegacyAutofillAgent
);
80 friend class LegacyAutofillAgent
;
82 // Flags passed to ShowSuggestions.
83 struct ShowSuggestionsOptions
{
84 // All fields are default initialized to false.
85 ShowSuggestionsOptions();
87 // Specifies that suggestions should be shown when |element| contains no
89 bool autofill_on_empty_values
;
91 // Specifies that suggestions should be shown when the caret is not
92 // after the last character in the element.
93 bool requires_caret_at_end
;
95 // Specifies that all of <datalist> suggestions and no autofill suggestions
96 // are shown. |autofill_on_empty_values| and |requires_caret_at_end| are
97 // ignored if |datalist_only| is true.
100 // Specifies that all autofill suggestions should be shown and none should
101 // be elided because of the current value of |element| (relevant for inline
103 bool show_full_suggestion_list
;
105 // Specifies that only show a suggestions box if |element| is part of a
106 // password form, otherwise show no suggestions.
107 bool show_password_suggestions_only
;
110 // content::RenderFrameObserver:
111 bool OnMessageReceived(const IPC::Message
& message
) override
;
112 void DidCommitProvisionalLoad(bool is_new_navigation
,
113 bool is_same_page_navigation
) override
;
114 void DidFinishDocumentLoad() override
;
115 void WillSendSubmitEvent(const blink::WebFormElement
& form
) override
;
116 void WillSubmitForm(const blink::WebFormElement
& form
) override
;
117 void DidChangeScrollOffset() override
;
118 void FocusedNodeChanged(const blink::WebNode
& node
) override
;
120 // Pass-through from LegacyAutofillAgent. This correlates with the
121 // RenderViewObserver method.
122 void FocusChangeComplete();
124 // PageClickListener:
125 void FormControlElementClicked(const blink::WebFormControlElement
& element
,
126 bool was_focused
) override
;
128 // blink::WebAutofillClient:
129 virtual void textFieldDidEndEditing(
130 const blink::WebInputElement
& element
);
131 virtual void textFieldDidChange(
132 const blink::WebFormControlElement
& element
);
133 virtual void textFieldDidReceiveKeyDown(
134 const blink::WebInputElement
& element
,
135 const blink::WebKeyboardEvent
& event
);
136 virtual void didRequestAutocomplete(
137 const blink::WebFormElement
& form
);
138 virtual void setIgnoreTextChanges(bool ignore
);
139 virtual void didAssociateFormControls(
140 const blink::WebVector
<blink::WebNode
>& nodes
);
141 virtual void openTextDataListChooser(const blink::WebInputElement
& element
);
142 virtual void dataListOptionsChanged(const blink::WebInputElement
& element
);
143 virtual void firstUserGestureObserved();
144 virtual void ajaxSucceeded();
146 void OnFieldTypePredictionsAvailable(
147 const std::vector
<FormDataPredictions
>& forms
);
148 void OnFillForm(int query_id
, const FormData
& form
);
149 void OnFirstUserGestureObservedInTab();
151 void OnPreviewForm(int query_id
, const FormData
& form
);
153 // For external Autofill selection.
155 void OnClearPreviewedForm();
156 void OnFillFieldWithValue(const base::string16
& value
);
157 void OnPreviewFieldWithValue(const base::string16
& value
);
158 void OnAcceptDataListSuggestion(const base::string16
& value
);
159 void OnFillPasswordSuggestion(const base::string16
& username
,
160 const base::string16
& password
);
161 void OnPreviewPasswordSuggestion(const base::string16
& username
,
162 const base::string16
& password
);
164 // Called when interactive autocomplete finishes. |message| is printed to
165 // the console if non-empty.
166 void OnRequestAutocompleteResult(
167 blink::WebFormElement::AutocompleteResult result
,
168 const base::string16
& message
,
169 const FormData
& form_data
);
171 // Called when an autocomplete request succeeds or fails with the |result|.
172 void FinishAutocompleteRequest(
173 blink::WebFormElement::AutocompleteResult result
);
175 // Called in a posted task by textFieldDidChange() to work-around a WebKit bug
176 // http://bugs.webkit.org/show_bug.cgi?id=16976
177 void TextFieldDidChangeImpl(const blink::WebFormControlElement
& element
);
179 // Shows the autofill suggestions for |element|. This call is asynchronous
180 // and may or may not lead to the showing of a suggestion popup (no popup is
181 // shown if there are no available suggestions).
182 void ShowSuggestions(const blink::WebFormControlElement
& element
,
183 const ShowSuggestionsOptions
& options
);
185 // Queries the browser for Autocomplete and Autofill suggestions for the given
187 void QueryAutofillSuggestions(const blink::WebFormControlElement
& element
,
190 // Sets the element value to reflect the selected |suggested_value|.
191 void AcceptDataListSuggestion(const base::string16
& suggested_value
);
193 // Fills |form| and |field| with the FormData and FormField corresponding to
194 // |node|. Returns true if the data was found; and false otherwise.
195 bool FindFormAndFieldForNode(
196 const blink::WebNode
& node
,
198 FormFieldData
* field
) WARN_UNUSED_RESULT
;
200 // Set |node| to display the given |value|.
201 void FillFieldWithValue(const base::string16
& value
,
202 blink::WebInputElement
* node
);
204 // Set |node| to display the given |value| as a preview. The preview is
205 // visible on screen to the user, but not visible to the page via the DOM or
207 void PreviewFieldWithValue(const base::string16
& value
,
208 blink::WebInputElement
* node
);
210 // Notifies browser of new fillable forms in |render_frame|.
213 // Sends a message to the browser that the form is about to be submitted,
214 // only if the particular message has not been previously submitted for the
215 // form in the current frame.
216 // Additionally, depending on |send_submitted_event| a message is sent to the
217 // browser that the form was submitted.
218 void SendFormEvents(const blink::WebFormElement
& form
,
219 bool send_submitted_event
);
221 // Hides any currently showing Autofill popup.
224 // Returns true if the text field change is due to a user gesture. Can be
225 // overriden in tests.
226 virtual bool IsUserGesture() const;
228 // Formerly cached forms for all frames, now only caches forms for the current
230 FormCache form_cache_
;
232 // Keeps track of the forms for which a "will submit" message has been sent in
233 // this frame's current load. We use a simplified comparison function.
234 std::set
<FormData
, FormDataCompare
> submitted_forms_
;
236 PasswordAutofillAgent
* password_autofill_agent_
; // Weak reference.
237 PasswordGenerationAgent
* password_generation_agent_
; // Weak reference.
239 // Passes through RenderViewObserver methods to |this|.
240 LegacyAutofillAgent legacy_
;
242 // The ID of the last request sent for form field Autofill. Used to ignore
243 // out of date responses.
244 int autofill_query_id_
;
246 // The element corresponding to the last request sent for form field Autofill.
247 blink::WebFormControlElement element_
;
249 // The form element currently requesting an interactive autocomplete.
250 blink::WebFormElement in_flight_request_form_
;
252 // Was the query node autofilled prior to previewing the form?
253 bool was_query_node_autofilled_
;
255 // Have we already shown Autofill suggestions for the field the user is
256 // currently editing? Used to keep track of state for metrics logging.
257 bool has_shown_autofill_popup_for_current_edit_
;
259 // Whether or not to ignore text changes. Useful for when we're committing
260 // a composition when we are defocusing the WebView and we don't want to
261 // trigger an autofill popup to show.
262 bool ignore_text_changes_
;
264 // Whether the Autofill popup is possibly visible. This is tracked as a
265 // performance improvement, so that the IPC channel isn't flooded with
266 // messages to close the Autofill popup when it can't possibly be showing.
267 bool is_popup_possibly_visible_
;
269 // If the generation popup is possibly visible. This is tracked to prevent
270 // generation UI from displaying at the same time as password manager UI.
271 // This is needed because generation is shown on field focus vs. field click
272 // for the password manager. TODO(gcasto): Have both UIs show on focus.
273 bool is_generation_popup_possibly_visible_
;
275 base::WeakPtrFactory
<AutofillAgent
> weak_ptr_factory_
;
277 DISALLOW_COPY_AND_ASSIGN(AutofillAgent
);
280 } // namespace autofill
282 #endif // COMPONENTS_AUTOFILL_CONTENT_RENDERER_AUTOFILL_AGENT_H_