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"
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 RenderView.
39 // This code was originally part of RenderView.
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::RenderViewObserver
,
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::RenderView
* render_view
,
53 PasswordAutofillAgent
* password_autofill_manager
,
54 PasswordGenerationAgent
* password_generation_agent
);
55 virtual ~AutofillAgent();
58 // content::RenderViewObserver:
59 bool OnMessageReceived(const IPC::Message
& message
) override
;
60 void DidFinishDocumentLoad(blink::WebLocalFrame
* frame
) override
;
61 void DidCommitProvisionalLoad(blink::WebLocalFrame
* frame
,
62 bool is_new_navigation
) override
;
63 void FrameDetached(blink::WebFrame
* frame
) override
;
64 void FrameWillClose(blink::WebFrame
* frame
) override
;
65 void WillSubmitForm(blink::WebLocalFrame
* frame
,
66 const blink::WebFormElement
& form
) override
;
67 void DidChangeScrollOffset(blink::WebLocalFrame
* frame
) override
;
68 void FocusedNodeChanged(const blink::WebNode
& node
) override
;
69 void OrientationChangeEvent() override
;
70 void Resized() override
;
73 void FormControlElementClicked(const blink::WebFormControlElement
& element
,
74 bool was_focused
) override
;
76 // blink::WebAutofillClient:
77 virtual void textFieldDidEndEditing(
78 const blink::WebInputElement
& element
);
79 virtual void textFieldDidChange(
80 const blink::WebFormControlElement
& element
);
81 virtual void textFieldDidReceiveKeyDown(
82 const blink::WebInputElement
& element
,
83 const blink::WebKeyboardEvent
& event
);
84 virtual void didRequestAutocomplete(
85 const blink::WebFormElement
& form
);
86 virtual void setIgnoreTextChanges(bool ignore
);
87 virtual void didAssociateFormControls(
88 const blink::WebVector
<blink::WebNode
>& nodes
);
89 virtual void openTextDataListChooser(const blink::WebInputElement
& element
);
90 virtual void firstUserGestureObserved();
92 void OnFieldTypePredictionsAvailable(
93 const std::vector
<FormDataPredictions
>& forms
);
94 void OnFillForm(int query_id
, const FormData
& form
);
96 void OnPreviewForm(int query_id
, const FormData
& form
);
98 // For external Autofill selection.
100 void OnClearPreviewedForm();
101 void OnFillFieldWithValue(const base::string16
& value
);
102 void OnPreviewFieldWithValue(const base::string16
& value
);
103 void OnAcceptDataListSuggestion(const base::string16
& value
);
104 void OnFillPasswordSuggestion(const base::string16
& username
,
105 const base::string16
& password
);
106 void OnPreviewPasswordSuggestion(const base::string16
& username
,
107 const base::string16
& password
);
109 // Called when interactive autocomplete finishes. |message| is printed to
110 // the console if non-empty.
111 void OnRequestAutocompleteResult(
112 blink::WebFormElement::AutocompleteResult result
,
113 const base::string16
& message
,
114 const FormData
& form_data
);
116 // Called when an autocomplete request succeeds or fails with the |result|.
117 void FinishAutocompleteRequest(
118 blink::WebFormElement::AutocompleteResult result
);
120 // Called in a posted task by textFieldDidChange() to work-around a WebKit bug
121 // http://bugs.webkit.org/show_bug.cgi?id=16976
122 void TextFieldDidChangeImpl(const blink::WebFormControlElement
& element
);
124 // Shows the autofill suggestions for |element|.
125 // This call is asynchronous and may or may not lead to the showing of a
126 // suggestion popup (no popup is shown if there are no available suggestions).
127 // |autofill_on_empty_values| specifies whether suggestions should be shown
128 // when |element| contains no text.
129 // |requires_caret_at_end| specifies whether suggestions should be shown when
130 // the caret is not after the last character in |element|.
131 // |display_warning_if_disabled| specifies whether a warning should be
132 // displayed to the user if Autofill has suggestions available, but cannot
133 // fill them because it is disabled (e.g. when trying to fill a credit card
134 // form on a non-secure website).
135 // |datalist_only| specifies whether all of <datalist> suggestions and no
136 // autofill suggestions are shown. |autofill_on_empty_values| and
137 // |requires_caret_at_end| are ignored if |datalist_only| is true.
138 // |show_full_suggestion_list| specifies that all autofill suggestions should
139 // be shown and none should be elided because of the current value of
140 // |element| (relevant for inline autocomplete).
141 // |show_password_suggestions_only| specifies that only show a suggestions box
142 // if |element| is part of a password form, otherwise show no suggestions.
143 void ShowSuggestions(const blink::WebFormControlElement
& element
,
144 bool autofill_on_empty_values
,
145 bool requires_caret_at_end
,
146 bool display_warning_if_disabled
,
148 bool show_full_suggestion_list
,
149 bool show_password_suggestions_only
);
151 // Queries the browser for Autocomplete and Autofill suggestions for the given
153 void QueryAutofillSuggestions(const blink::WebFormControlElement
& element
,
154 bool display_warning_if_disabled
,
157 // Sets the element value to reflect the selected |suggested_value|.
158 void AcceptDataListSuggestion(const base::string16
& suggested_value
);
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
,
165 FormFieldData
* field
) WARN_UNUSED_RESULT
;
167 // Set |node| to display the given |value|.
168 void FillFieldWithValue(const base::string16
& value
,
169 blink::WebInputElement
* node
);
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
174 void PreviewFieldWithValue(const base::string16
& value
,
175 blink::WebInputElement
* node
);
177 // Notifies browser of new fillable forms in |frame|.
178 void ProcessForms(const blink::WebLocalFrame
& frame
);
180 // Hides any currently showing Autofill popup.
183 FormCache form_cache_
;
185 PasswordAutofillAgent
* password_autofill_agent_
; // Weak reference.
186 PasswordGenerationAgent
* password_generation_agent_
; // Weak reference.
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_
;
192 // The element corresponding to the last request sent for form field Autofill.
193 blink::WebFormControlElement element_
;
195 // The form element currently requesting an interactive autocomplete.
196 blink::WebFormElement in_flight_request_form_
;
198 // Pointer to the WebView. Used to access page scale factor.
199 blink::WebView
* web_view_
;
201 // Should we display a warning if autofill is disabled?
202 bool display_warning_if_disabled_
;
204 // Was the query node autofilled prior to previewing the form?
205 bool was_query_node_autofilled_
;
207 // Have we already shown Autofill suggestions for the field the user is
208 // currently editing? Used to keep track of state for metrics logging.
209 bool has_shown_autofill_popup_for_current_edit_
;
211 // If true we just set the node text so we shouldn't show the popup.
212 bool did_set_node_text_
;
214 // Whether or not to ignore text changes. Useful for when we're committing
215 // a composition when we are defocusing the WebView and we don't want to
216 // trigger an autofill popup to show.
217 bool ignore_text_changes_
;
219 // Whether the Autofill popup is possibly visible. This is tracked as a
220 // performance improvement, so that the IPC channel isn't flooded with
221 // messages to close the Autofill popup when it can't possibly be showing.
222 bool is_popup_possibly_visible_
;
224 // True if a message has already been sent about forms for the main frame.
225 // When the main frame is first loaded, a message is sent even if no forms
226 // exist in the frame. Otherwise, such messages are supressed.
227 bool main_frame_processed_
;
229 base::WeakPtrFactory
<AutofillAgent
> weak_ptr_factory_
;
231 DISALLOW_COPY_AND_ASSIGN(AutofillAgent
);
234 } // namespace autofill
236 #endif // COMPONENTS_AUTOFILL_CONTENT_RENDERER_AUTOFILL_AGENT_H_