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 "components/autofill/content/renderer/form_cache.h"
16 #include "components/autofill/content/renderer/page_click_listener.h"
17 #include "components/autofill/content/renderer/page_click_tracker.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 // Thunk class for RenderViewObserver methods that haven't yet been migrated
59 // to RenderFrameObserver. Should eventually be removed.
60 // http://crbug.com/433486
61 class LegacyAutofillAgent
: public content::RenderViewObserver
{
63 LegacyAutofillAgent(content::RenderView
* render_view
, AutofillAgent
* agent
);
64 ~LegacyAutofillAgent() override
;
67 // content::RenderViewObserver:
68 void OnDestruct() override
;
69 void FocusedNodeChanged(const blink::WebNode
& node
) override
;
70 void Resized() override
;
72 AutofillAgent
* agent_
;
74 DISALLOW_COPY_AND_ASSIGN(LegacyAutofillAgent
);
76 friend class LegacyAutofillAgent
;
78 // Flags passed to ShowSuggestions.
79 struct ShowSuggestionsOptions
{
80 // All fields are default initialized to false.
81 ShowSuggestionsOptions();
83 // Specifies that suggestions should be shown when |element| contains no
85 bool autofill_on_empty_values
;
87 // Specifies that suggestions should be shown when the caret is not
88 // after the last character in the element.
89 bool requires_caret_at_end
;
91 // Specifies that a warning should be displayed to the user if Autofill has
92 // suggestions available, but cannot fill them because it is disabled (e.g.
93 // when trying to fill a credit card form on a non-secure website).
94 bool display_warning_if_disabled
;
96 // Specifies that all of <datalist> suggestions and no autofill suggestions
97 // are shown. |autofill_on_empty_values| and |requires_caret_at_end| are
98 // ignored if |datalist_only| is true.
101 // Specifies that all autofill suggestions should be shown and none should
102 // be elided because of the current value of |element| (relevant for inline
104 bool show_full_suggestion_list
;
106 // Specifies that only show a suggestions box if |element| is part of a
107 // password form, otherwise show no suggestions.
108 bool show_password_suggestions_only
;
111 // content::RenderFrameObserver:
112 bool OnMessageReceived(const IPC::Message
& message
) override
;
113 void DidCommitProvisionalLoad(bool is_new_navigation
) override
;
114 void DidFinishDocumentLoad() override
;
115 void WillSubmitForm(const blink::WebFormElement
& form
) override
;
116 void DidChangeScrollOffset() override
;
118 // Pass-throughs from LegacyAutofillAgent. These correlate with
119 // RenderViewObserver methods.
120 void FocusedNodeChanged(const blink::WebNode
& node
);
123 // PageClickListener:
124 void FormControlElementClicked(const blink::WebFormControlElement
& element
,
125 bool was_focused
) override
;
127 // blink::WebAutofillClient:
128 virtual void textFieldDidEndEditing(
129 const blink::WebInputElement
& element
);
130 virtual void textFieldDidChange(
131 const blink::WebFormControlElement
& element
);
132 virtual void textFieldDidReceiveKeyDown(
133 const blink::WebInputElement
& element
,
134 const blink::WebKeyboardEvent
& event
);
135 virtual void didRequestAutocomplete(
136 const blink::WebFormElement
& form
);
137 virtual void setIgnoreTextChanges(bool ignore
);
138 virtual void didAssociateFormControls(
139 const blink::WebVector
<blink::WebNode
>& nodes
);
140 virtual void openTextDataListChooser(const blink::WebInputElement
& element
);
141 virtual void dataListOptionsChanged(const blink::WebInputElement
& element
);
142 virtual void firstUserGestureObserved();
144 void OnFieldTypePredictionsAvailable(
145 const std::vector
<FormDataPredictions
>& forms
);
146 void OnFillForm(int query_id
, const FormData
& form
);
147 void OnFirstUserGestureObservedInTab();
149 void OnPreviewForm(int query_id
, const FormData
& form
);
151 // For external Autofill selection.
153 void OnClearPreviewedForm();
154 void OnFillFieldWithValue(const base::string16
& value
);
155 void OnPreviewFieldWithValue(const base::string16
& value
);
156 void OnAcceptDataListSuggestion(const base::string16
& value
);
157 void OnFillPasswordSuggestion(const base::string16
& username
,
158 const base::string16
& password
);
159 void OnPreviewPasswordSuggestion(const base::string16
& username
,
160 const base::string16
& password
);
162 // Called when interactive autocomplete finishes. |message| is printed to
163 // the console if non-empty.
164 void OnRequestAutocompleteResult(
165 blink::WebFormElement::AutocompleteResult result
,
166 const base::string16
& message
,
167 const FormData
& form_data
);
169 // Called when an autocomplete request succeeds or fails with the |result|.
170 void FinishAutocompleteRequest(
171 blink::WebFormElement::AutocompleteResult result
);
173 // Called in a posted task by textFieldDidChange() to work-around a WebKit bug
174 // http://bugs.webkit.org/show_bug.cgi?id=16976
175 void TextFieldDidChangeImpl(const blink::WebFormControlElement
& element
);
177 // Shows the autofill suggestions for |element|. This call is asynchronous
178 // and may or may not lead to the showing of a suggestion popup (no popup is
179 // shown if there are no available suggestions).
180 void ShowSuggestions(const blink::WebFormControlElement
& element
,
181 const ShowSuggestionsOptions
& options
);
183 // Queries the browser for Autocomplete and Autofill suggestions for the given
185 void QueryAutofillSuggestions(const blink::WebFormControlElement
& element
,
186 bool display_warning_if_disabled
,
189 // Sets the element value to reflect the selected |suggested_value|.
190 void AcceptDataListSuggestion(const base::string16
& suggested_value
);
192 // Fills |form| and |field| with the FormData and FormField corresponding to
193 // |node|. Returns true if the data was found; and false otherwise.
194 bool FindFormAndFieldForNode(
195 const blink::WebNode
& node
,
197 FormFieldData
* field
) WARN_UNUSED_RESULT
;
199 // Set |node| to display the given |value|.
200 void FillFieldWithValue(const base::string16
& value
,
201 blink::WebInputElement
* node
);
203 // Set |node| to display the given |value| as a preview. The preview is
204 // visible on screen to the user, but not visible to the page via the DOM or
206 void PreviewFieldWithValue(const base::string16
& value
,
207 blink::WebInputElement
* node
);
209 // Notifies browser of new fillable forms in |render_frame|.
212 // Hides any currently showing Autofill popup.
215 // Formerly cached forms for all frames, now only caches forms for the current
217 FormCache form_cache_
;
219 PasswordAutofillAgent
* password_autofill_agent_
; // Weak reference.
220 PasswordGenerationAgent
* password_generation_agent_
; // Weak reference.
222 // Passes through RenderViewObserver methods to |this|.
223 LegacyAutofillAgent legacy_
;
225 // Tracks clicks on the RenderViewHost, informs |this|.
226 PageClickTracker page_click_tracker_
;
228 // The ID of the last request sent for form field Autofill. Used to ignore
229 // out of date responses.
230 int autofill_query_id_
;
232 // The element corresponding to the last request sent for form field Autofill.
233 blink::WebFormControlElement element_
;
235 // The form element currently requesting an interactive autocomplete.
236 blink::WebFormElement in_flight_request_form_
;
238 // Should we display a warning if autofill is disabled?
239 bool display_warning_if_disabled_
;
241 // Was the query node autofilled prior to previewing the form?
242 bool was_query_node_autofilled_
;
244 // Have we already shown Autofill suggestions for the field the user is
245 // currently editing? Used to keep track of state for metrics logging.
246 bool has_shown_autofill_popup_for_current_edit_
;
248 // If true we just set the node text so we shouldn't show the popup.
249 bool did_set_node_text_
;
251 // Whether or not to ignore text changes. Useful for when we're committing
252 // a composition when we are defocusing the WebView and we don't want to
253 // trigger an autofill popup to show.
254 bool ignore_text_changes_
;
256 // Whether the Autofill popup is possibly visible. This is tracked as a
257 // performance improvement, so that the IPC channel isn't flooded with
258 // messages to close the Autofill popup when it can't possibly be showing.
259 bool is_popup_possibly_visible_
;
261 base::WeakPtrFactory
<AutofillAgent
> weak_ptr_factory_
;
263 DISALLOW_COPY_AND_ASSIGN(AutofillAgent
);
266 } // namespace autofill
268 #endif // COMPONENTS_AUTOFILL_CONTENT_RENDERER_AUTOFILL_AGENT_H_