OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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. | |
4 | |
5 #ifndef CHROME_RENDERER_AUTOFILL_AUTOFILL_AGENT_H_ | |
6 #define CHROME_RENDERER_AUTOFILL_AUTOFILL_AGENT_H_ | |
7 | |
8 #include <vector> | |
9 | |
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/timer.h" | |
15 #include "chrome/renderer/autofill/form_cache.h" | |
16 #include "chrome/renderer/page_click_listener.h" | |
17 #include "content/public/renderer/render_view_observer.h" | |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebAutofillClient.h" | |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormElement.h" | |
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" | |
21 | |
22 struct FormFieldData; | |
23 | |
24 namespace WebKit { | |
25 class WebNode; | |
26 class WebView; | |
27 } | |
28 | |
29 namespace autofill { | |
30 | |
31 struct WebElementDescriptor; | |
32 class PasswordAutofillManager; | |
33 | |
34 // AutofillAgent deals with Autofill related communications between WebKit and | |
35 // the browser. There is one AutofillAgent per RenderView. | |
36 // This code was originally part of RenderView. | |
37 // Note that Autofill encompasses: | |
38 // - single text field suggestions, that we usually refer to as Autocomplete, | |
39 // - password form fill, refered to as Password Autofill, and | |
40 // - entire form fill based on one field entry, referred to as Form Autofill. | |
41 | |
42 class AutofillAgent : public content::RenderViewObserver, | |
43 public PageClickListener, | |
44 public WebKit::WebAutofillClient { | |
45 public: | |
46 // PasswordAutofillManager is guaranteed to outlive AutofillAgent. | |
47 AutofillAgent(content::RenderView* render_view, | |
48 PasswordAutofillManager* password_autofill_manager); | |
49 virtual ~AutofillAgent(); | |
50 | |
51 private: | |
52 enum AutofillAction { | |
53 AUTOFILL_NONE, // No state set. | |
54 AUTOFILL_FILL, // Fill the Autofill form data. | |
55 AUTOFILL_PREVIEW, // Preview the Autofill form data. | |
56 }; | |
57 | |
58 // RenderView::Observer: | |
59 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | |
60 virtual void DidFinishDocumentLoad(WebKit::WebFrame* frame) OVERRIDE; | |
61 virtual void DidStartProvisionalLoad(WebKit::WebFrame* frame) OVERRIDE; | |
62 virtual void DidFailProvisionalLoad( | |
63 WebKit::WebFrame* frame, | |
64 const WebKit::WebURLError& error) OVERRIDE; | |
65 virtual void DidCommitProvisionalLoad(WebKit::WebFrame* frame, | |
66 bool is_new_navigation) OVERRIDE; | |
67 virtual void FrameDetached(WebKit::WebFrame* frame) OVERRIDE; | |
68 virtual void WillSubmitForm(WebKit::WebFrame* frame, | |
69 const WebKit::WebFormElement& form) OVERRIDE; | |
70 virtual void ZoomLevelChanged() OVERRIDE; | |
71 virtual void DidChangeScrollOffset(WebKit::WebFrame* frame) OVERRIDE; | |
72 | |
73 // PageClickListener: | |
74 virtual bool InputElementClicked(const WebKit::WebInputElement& element, | |
75 bool was_focused, | |
76 bool is_focused) OVERRIDE; | |
77 virtual bool InputElementLostFocus() OVERRIDE; | |
78 | |
79 // WebKit::WebAutofillClient: | |
80 virtual void didAcceptAutofillSuggestion(const WebKit::WebNode& node, | |
81 const WebKit::WebString& value, | |
82 const WebKit::WebString& label, | |
83 int item_id, | |
84 unsigned index) OVERRIDE; | |
85 virtual void didSelectAutofillSuggestion(const WebKit::WebNode& node, | |
86 const WebKit::WebString& value, | |
87 const WebKit::WebString& label, | |
88 int item_id) OVERRIDE; | |
89 virtual void didClearAutofillSelection(const WebKit::WebNode& node) OVERRIDE; | |
90 virtual void removeAutocompleteSuggestion( | |
91 const WebKit::WebString& name, | |
92 const WebKit::WebString& value) OVERRIDE; | |
93 virtual void textFieldDidEndEditing( | |
94 const WebKit::WebInputElement& element) OVERRIDE; | |
95 virtual void textFieldDidChange( | |
96 const WebKit::WebInputElement& element) OVERRIDE; | |
97 virtual void textFieldDidReceiveKeyDown( | |
98 const WebKit::WebInputElement& element, | |
99 const WebKit::WebKeyboardEvent& event) OVERRIDE; | |
100 virtual void didRequestAutocomplete( | |
101 WebKit::WebFrame* frame, | |
102 const WebKit::WebFormElement& form) OVERRIDE; | |
103 virtual void setIgnoreTextChanges(bool ignore) OVERRIDE; | |
104 | |
105 void OnSuggestionsReturned(int query_id, | |
106 const std::vector<string16>& values, | |
107 const std::vector<string16>& labels, | |
108 const std::vector<string16>& icons, | |
109 const std::vector<int>& unique_ids); | |
110 void OnFormDataFilled(int query_id, const FormData& form); | |
111 void OnFieldTypePredictionsAvailable( | |
112 const std::vector<FormDataPredictions>& forms); | |
113 | |
114 // For external Autofill selection. | |
115 void OnSetAutofillActionFill(); | |
116 void OnClearForm(); | |
117 void OnSetAutofillActionPreview(); | |
118 void OnClearPreviewedForm(); | |
119 void OnSetNodeText(const string16& value); | |
120 void OnAcceptDataListSuggestion(const string16& value); | |
121 void OnAcceptPasswordAutofillSuggestion(const string16& value); | |
122 | |
123 // Called when interactive autocomplete finishes. | |
124 void OnRequestAutocompleteResult( | |
125 WebKit::WebFormElement::AutocompleteResult result, | |
126 const FormData& form_data); | |
127 | |
128 // Called when an autocomplete request succeeds or fails with the |result|. | |
129 void FinishAutocompleteRequest( | |
130 WebKit::WebFormElement::AutocompleteResult result); | |
131 | |
132 // Called when the Autofill server hints that this page should be filled using | |
133 // Autocheckout. All the relevant form fields in |form_data| will be filled | |
134 // and then element specified by |element_descriptor| will be clicked to | |
135 // proceed to the next step of the form. | |
136 void OnFillFormsAndClick(const std::vector<FormData>& form_data, | |
137 const WebElementDescriptor& element_descriptor); | |
138 | |
139 // Called when clicking an Autocheckout proceed element fails to do anything. | |
140 void ClickFailed(); | |
141 | |
142 // Called in a posted task by textFieldDidChange() to work-around a WebKit bug | |
143 // http://bugs.webkit.org/show_bug.cgi?id=16976 | |
144 void TextFieldDidChangeImpl(const WebKit::WebInputElement& element); | |
145 | |
146 // Shows the autofill suggestions for |element|. | |
147 // This call is asynchronous and may or may not lead to the showing of a | |
148 // suggestion popup (no popup is shown if there are no available suggestions). | |
149 // |autofill_on_empty_values| specifies whether suggestions should be shown | |
150 // when |element| contains no text. | |
151 // |requires_caret_at_end| specifies whether suggestions should be shown when | |
152 // the caret is not after the last character in |element|. | |
153 // |display_warning_if_disabled| specifies whether a warning should be | |
154 // displayed to the user if Autofill has suggestions available, but cannot | |
155 // fill them because it is disabled (e.g. when trying to fill a credit card | |
156 // form on a non-secure website). | |
157 void ShowSuggestions(const WebKit::WebInputElement& element, | |
158 bool autofill_on_empty_values, | |
159 bool requires_caret_at_end, | |
160 bool display_warning_if_disabled); | |
161 | |
162 // Queries the browser for Autocomplete and Autofill suggestions for the given | |
163 // |element|. | |
164 void QueryAutofillSuggestions(const WebKit::WebInputElement& element, | |
165 bool display_warning_if_disabled); | |
166 | |
167 // Combines DataList suggestion entries with the autofill ones and show them | |
168 // to the user. | |
169 void CombineDataListEntriesAndShow(const WebKit::WebInputElement& element, | |
170 const std::vector<string16>& values, | |
171 const std::vector<string16>& labels, | |
172 const std::vector<string16>& icons, | |
173 const std::vector<int>& item_ids, | |
174 bool has_autofill_item); | |
175 | |
176 // Sets the element value to reflect the selected |suggested_value|. | |
177 void AcceptDataListSuggestion(const string16& suggested_value); | |
178 | |
179 // Queries the AutofillManager for form data for the form containing |node|. | |
180 // |value| is the current text in the field, and |unique_id| is the selected | |
181 // profile's unique ID. |action| specifies whether to Fill or Preview the | |
182 // values returned from the AutofillManager. | |
183 void FillAutofillFormData(const WebKit::WebNode& node, | |
184 int unique_id, | |
185 AutofillAction action); | |
186 | |
187 // Fills |form| and |field| with the FormData and FormField corresponding to | |
188 // |node|. Returns true if the data was found; and false otherwise. | |
189 bool FindFormAndFieldForNode( | |
190 const WebKit::WebNode& node, | |
191 FormData* form, | |
192 FormFieldData* field) WARN_UNUSED_RESULT; | |
193 | |
194 // Set |node| to display the given |value|. | |
195 void SetNodeText(const string16& value, WebKit::WebInputElement* node); | |
196 | |
197 // Hides any currently showing Autofill popups in the renderer or browser. | |
198 void HidePopups(); | |
199 | |
200 // Hides any currently showing Autofill popups in the browser only. | |
201 void HideHostPopups(); | |
202 | |
203 FormCache form_cache_; | |
204 | |
205 PasswordAutofillManager* password_autofill_manager_; // WEAK reference. | |
206 | |
207 // The ID of the last request sent for form field Autofill. Used to ignore | |
208 // out of date responses. | |
209 int autofill_query_id_; | |
210 | |
211 // The element corresponding to the last request sent for form field Autofill. | |
212 WebKit::WebInputElement element_; | |
213 | |
214 // The form element currently requesting an interactive autocomplete. | |
215 WebKit::WebFormElement in_flight_request_form_; | |
216 | |
217 // All the form elements seen in the top frame. | |
218 std::vector<WebKit::WebFormElement> form_elements_; | |
219 | |
220 // The action to take when receiving Autofill data from the AutofillManager. | |
221 AutofillAction autofill_action_; | |
222 | |
223 // Pointer to the current topmost frame. Used in autocheckout flows so | |
224 // elements can be clicked. | |
225 WebKit::WebFrame* topmost_frame_; | |
226 | |
227 // Pointer to the WebView. Used to access page scale factor. | |
228 WebKit::WebView* web_view_; | |
229 | |
230 // Should we display a warning if autofill is disabled? | |
231 bool display_warning_if_disabled_; | |
232 | |
233 // Was the query node autofilled prior to previewing the form? | |
234 bool was_query_node_autofilled_; | |
235 | |
236 // Have we already shown Autofill suggestions for the field the user is | |
237 // currently editing? Used to keep track of state for metrics logging. | |
238 bool has_shown_autofill_popup_for_current_edit_; | |
239 | |
240 // If true we just set the node text so we shouldn't show the popup. | |
241 bool did_set_node_text_; | |
242 | |
243 // Watchdog timer for clicking in Autocheckout flows. | |
244 base::OneShotTimer<AutofillAgent> click_timer_; | |
245 | |
246 // Used to signal that we need to watch for loading failures in an | |
247 // Autocheckout flow. | |
248 bool autocheckout_click_in_progress_; | |
249 | |
250 // Whether or not to ignore text changes. Useful for when we're committing | |
251 // a composition when we are defocusing the WebView and we don't want to | |
252 // trigger an autofill popup to show. | |
253 bool ignore_text_changes_; | |
254 | |
255 base::WeakPtrFactory<AutofillAgent> weak_ptr_factory_; | |
256 | |
257 friend class PasswordAutofillManagerTest; | |
258 FRIEND_TEST_ALL_PREFIXES(ChromeRenderViewTest, FillFormElement); | |
259 FRIEND_TEST_ALL_PREFIXES(ChromeRenderViewTest, SendForms); | |
260 FRIEND_TEST_ALL_PREFIXES(ChromeRenderViewTest, ShowAutofillWarning); | |
261 FRIEND_TEST_ALL_PREFIXES(PasswordAutofillManagerTest, WaitUsername); | |
262 FRIEND_TEST_ALL_PREFIXES(PasswordAutofillManagerTest, SuggestionAccept); | |
263 FRIEND_TEST_ALL_PREFIXES(PasswordAutofillManagerTest, SuggestionSelect); | |
264 | |
265 DISALLOW_COPY_AND_ASSIGN(AutofillAgent); | |
266 }; | |
267 | |
268 } // namespace autofill | |
269 | |
270 #endif // CHROME_RENDERER_AUTOFILL_AUTOFILL_AGENT_H_ | |
OLD | NEW |