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 "content/public/renderer/render_frame_observer.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"
33 struct WebElementDescriptor
;
34 class PasswordAutofillAgent
;
35 class PasswordGenerationAgent
;
37 // AutofillAgent deals with Autofill related communications between WebKit and
38 // the browser. There is one AutofillAgent per RenderFrame.
39 // Note that Autofill encompasses:
40 // - single text field suggestions, that we usually refer to as Autocomplete,
41 // - password form fill, refered to as Password Autofill, and
42 // - entire form fill based on one field entry, referred to as Form Autofill.
44 class AutofillAgent
: public content::RenderFrameObserver
,
45 public PageClickListener
,
46 public blink::WebAutofillClient
{
48 // PasswordAutofillAgent is guaranteed to outlive AutofillAgent.
49 // PasswordGenerationAgent may be NULL. If it is not, then it is also
50 // guaranteed to outlive AutofillAgent.
51 AutofillAgent(content::RenderFrame
* render_frame
,
52 PasswordAutofillAgent
* password_autofill_manager
,
53 PasswordGenerationAgent
* password_generation_agent
);
54 virtual ~AutofillAgent();
57 // Thunk class for RenderViewObserver methods that haven't yet been migrated
58 // to RenderFrameObserver. Should eventually be removed.
59 // http://crbug.com/433486
60 class LegacyAutofillAgent
: public content::RenderViewObserver
{
62 LegacyAutofillAgent(content::RenderView
* render_view
, AutofillAgent
* agent
);
63 ~LegacyAutofillAgent() override
;
66 // content::RenderViewObserver:
67 void OnDestruct() override
;
68 void FocusChangeComplete() override
;
70 AutofillAgent
* agent_
;
72 DISALLOW_COPY_AND_ASSIGN(LegacyAutofillAgent
);
74 friend class LegacyAutofillAgent
;
76 // Flags passed to ShowSuggestions.
77 struct ShowSuggestionsOptions
{
78 // All fields are default initialized to false.
79 ShowSuggestionsOptions();
81 // Specifies that suggestions should be shown when |element| contains no
83 bool autofill_on_empty_values
;
85 // Specifies that suggestions should be shown when the caret is not
86 // after the last character in the element.
87 bool requires_caret_at_end
;
89 // Specifies that a warning should be displayed to the user if Autofill has
90 // suggestions available, but cannot fill them because it is disabled (e.g.
91 // when trying to fill a credit card form on a non-secure website).
92 bool display_warning_if_disabled
;
94 // Specifies that all of <datalist> suggestions and no autofill suggestions
95 // are shown. |autofill_on_empty_values| and |requires_caret_at_end| are
96 // ignored if |datalist_only| is true.
99 // Specifies that all autofill suggestions should be shown and none should
100 // be elided because of the current value of |element| (relevant for inline
102 bool show_full_suggestion_list
;
104 // Specifies that only show a suggestions box if |element| is part of a
105 // password form, otherwise show no suggestions.
106 bool show_password_suggestions_only
;
109 // content::RenderFrameObserver:
110 bool OnMessageReceived(const IPC::Message
& message
) override
;
111 void DidCommitProvisionalLoad(bool is_new_navigation
,
112 bool is_same_page_navigation
) override
;
113 void DidFinishDocumentLoad() override
;
114 void WillSubmitForm(const blink::WebFormElement
& form
) override
;
115 void DidChangeScrollOffset() override
;
116 void FocusedNodeChanged(const blink::WebNode
& node
) override
;
118 // Pass-through from LegacyAutofillAgent. This correlates with the
119 // RenderViewObserver method.
120 void FocusChangeComplete();
122 // PageClickListener:
123 void FormControlElementClicked(const blink::WebFormControlElement
& element
,
124 bool was_focused
) override
;
126 // blink::WebAutofillClient:
127 virtual void textFieldDidEndEditing(
128 const blink::WebInputElement
& element
);
129 virtual void textFieldDidChange(
130 const blink::WebFormControlElement
& element
);
131 virtual void textFieldDidReceiveKeyDown(
132 const blink::WebInputElement
& element
,
133 const blink::WebKeyboardEvent
& event
);
134 virtual void didRequestAutocomplete(
135 const blink::WebFormElement
& form
);
136 virtual void setIgnoreTextChanges(bool ignore
);
137 virtual void didAssociateFormControls(
138 const blink::WebVector
<blink::WebNode
>& nodes
);
139 virtual void openTextDataListChooser(const blink::WebInputElement
& element
);
140 virtual void dataListOptionsChanged(const blink::WebInputElement
& element
);
141 virtual void firstUserGestureObserved();
143 void OnFieldTypePredictionsAvailable(
144 const std::vector
<FormDataPredictions
>& forms
);
145 void OnFillForm(int query_id
, const FormData
& form
);
146 void OnFirstUserGestureObservedInTab();
148 void OnPreviewForm(int query_id
, const FormData
& form
);
150 // For external Autofill selection.
152 void OnClearPreviewedForm();
153 void OnFillFieldWithValue(const base::string16
& value
);
154 void OnPreviewFieldWithValue(const base::string16
& value
);
155 void OnAcceptDataListSuggestion(const base::string16
& value
);
156 void OnFillPasswordSuggestion(const base::string16
& username
,
157 const base::string16
& password
);
158 void OnPreviewPasswordSuggestion(const base::string16
& username
,
159 const base::string16
& password
);
161 // Called when interactive autocomplete finishes. |message| is printed to
162 // the console if non-empty.
163 void OnRequestAutocompleteResult(
164 blink::WebFormElement::AutocompleteResult result
,
165 const base::string16
& message
,
166 const FormData
& form_data
);
168 // Called when an autocomplete request succeeds or fails with the |result|.
169 void FinishAutocompleteRequest(
170 blink::WebFormElement::AutocompleteResult result
);
172 // Called in a posted task by textFieldDidChange() to work-around a WebKit bug
173 // http://bugs.webkit.org/show_bug.cgi?id=16976
174 void TextFieldDidChangeImpl(const blink::WebFormControlElement
& element
);
176 // Shows the autofill suggestions for |element|. This call is asynchronous
177 // and may or may not lead to the showing of a suggestion popup (no popup is
178 // shown if there are no available suggestions).
179 void ShowSuggestions(const blink::WebFormControlElement
& element
,
180 const ShowSuggestionsOptions
& options
);
182 // Queries the browser for Autocomplete and Autofill suggestions for the given
184 void QueryAutofillSuggestions(const blink::WebFormControlElement
& element
,
185 bool display_warning_if_disabled
,
188 // Sets the element value to reflect the selected |suggested_value|.
189 void AcceptDataListSuggestion(const base::string16
& suggested_value
);
191 // Fills |form| and |field| with the FormData and FormField corresponding to
192 // |node|. Returns true if the data was found; and false otherwise.
193 bool FindFormAndFieldForNode(
194 const blink::WebNode
& node
,
196 FormFieldData
* field
) WARN_UNUSED_RESULT
;
198 // Set |node| to display the given |value|.
199 void FillFieldWithValue(const base::string16
& value
,
200 blink::WebInputElement
* node
);
202 // Set |node| to display the given |value| as a preview. The preview is
203 // visible on screen to the user, but not visible to the page via the DOM or
205 void PreviewFieldWithValue(const base::string16
& value
,
206 blink::WebInputElement
* node
);
208 // Notifies browser of new fillable forms in |render_frame|.
211 // Hides any currently showing Autofill popup.
214 // Formerly cached forms for all frames, now only caches forms for the current
216 FormCache form_cache_
;
218 PasswordAutofillAgent
* password_autofill_agent_
; // Weak reference.
219 PasswordGenerationAgent
* password_generation_agent_
; // Weak reference.
221 // Passes through RenderViewObserver methods to |this|.
222 LegacyAutofillAgent legacy_
;
224 // The ID of the last request sent for form field Autofill. Used to ignore
225 // out of date responses.
226 int autofill_query_id_
;
228 // The element corresponding to the last request sent for form field Autofill.
229 blink::WebFormControlElement element_
;
231 // The form element currently requesting an interactive autocomplete.
232 blink::WebFormElement in_flight_request_form_
;
234 // Should we display a warning if autofill is disabled?
235 bool display_warning_if_disabled_
;
237 // Was the query node autofilled prior to previewing the form?
238 bool was_query_node_autofilled_
;
240 // Have we already shown Autofill suggestions for the field the user is
241 // currently editing? Used to keep track of state for metrics logging.
242 bool has_shown_autofill_popup_for_current_edit_
;
244 // If true we just set the node text so we shouldn't show the popup.
245 bool did_set_node_text_
;
247 // Whether or not to ignore text changes. Useful for when we're committing
248 // a composition when we are defocusing the WebView and we don't want to
249 // trigger an autofill popup to show.
250 bool ignore_text_changes_
;
252 // Whether the Autofill popup is possibly visible. This is tracked as a
253 // performance improvement, so that the IPC channel isn't flooded with
254 // messages to close the Autofill popup when it can't possibly be showing.
255 bool is_popup_possibly_visible_
;
257 base::WeakPtrFactory
<AutofillAgent
> weak_ptr_factory_
;
259 DISALLOW_COPY_AND_ASSIGN(AutofillAgent
);
262 } // namespace autofill
264 #endif // COMPONENTS_AUTOFILL_CONTENT_RENDERER_AUTOFILL_AGENT_H_