autofill_agent.h revision 23730a6e56a168d1879203e4b3819bb36e3d8f1f
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.
4
5#ifndef COMPONENTS_AUTOFILL_CONTENT_RENDERER_AUTOFILL_AGENT_H_
6#define COMPONENTS_AUTOFILL_CONTENT_RENDERER_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/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 "components/autofill/core/common/forms_seen_state.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/WebFormElement.h"
22#include "third_party/WebKit/public/web/WebInputElement.h"
23
24namespace blink {
25class WebNode;
26class WebView;
27}
28
29namespace autofill {
30
31struct FormData;
32struct FormFieldData;
33struct WebElementDescriptor;
34class PasswordAutofillAgent;
35class PasswordGenerationAgent;
36
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.
44
45class AutofillAgent : public content::RenderViewObserver,
46                      public PageClickListener,
47                      public blink::WebAutofillClient {
48 public:
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();
56
57 private:
58  // content::RenderViewObserver:
59  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
60  virtual void DidFinishDocumentLoad(blink::WebFrame* frame) OVERRIDE;
61  virtual void FrameDetached(blink::WebFrame* frame) OVERRIDE;
62  virtual void FrameWillClose(blink::WebFrame* frame) OVERRIDE;
63  virtual void WillSubmitForm(blink::WebFrame* frame,
64                              const blink::WebFormElement& form) OVERRIDE;
65  virtual void ZoomLevelChanged() OVERRIDE;
66  virtual void DidChangeScrollOffset(blink::WebFrame* frame) OVERRIDE;
67  virtual void FocusedNodeChanged(const blink::WebNode& node) OVERRIDE;
68  virtual void OrientationChangeEvent(int orientation) OVERRIDE;
69
70  // PageClickListener:
71  virtual void InputElementClicked(const blink::WebInputElement& element,
72                                   bool was_focused,
73                                   bool is_focused) OVERRIDE;
74  virtual void InputElementLostFocus() OVERRIDE;
75
76  // blink::WebAutofillClient:
77  virtual void textFieldDidEndEditing(
78      const blink::WebInputElement& element) OVERRIDE;
79  virtual void textFieldDidChange(
80      const blink::WebInputElement& element) OVERRIDE;
81  virtual void textFieldDidReceiveKeyDown(
82      const blink::WebInputElement& element,
83      const blink::WebKeyboardEvent& event) OVERRIDE;
84  virtual void didRequestAutocomplete(
85      blink::WebFrame* frame,
86      const blink::WebFormElement& form) OVERRIDE;
87  virtual void setIgnoreTextChanges(bool ignore) OVERRIDE;
88  virtual void didAssociateFormControls(
89      const blink::WebVector<blink::WebNode>& nodes) OVERRIDE;
90  virtual void openTextDataListChooser(const blink::WebInputElement& element);
91
92  void OnFieldTypePredictionsAvailable(
93      const std::vector<FormDataPredictions>& forms);
94  void OnFillForm(int query_id, const FormData& form);
95  void OnPreviewForm(int query_id, const FormData& form);
96
97  // For external Autofill selection.
98  void OnClearForm();
99  void OnClearPreviewedForm();
100  void OnFillFieldWithValue(const base::string16& value);
101  void OnPreviewFieldWithValue(const base::string16& value);
102  void OnAcceptDataListSuggestion(const base::string16& value);
103  void OnAcceptPasswordAutofillSuggestion(const base::string16& username);
104
105  // Called when interactive autocomplete finishes.
106  void OnRequestAutocompleteResult(
107      blink::WebFormElement::AutocompleteResult result,
108      const FormData& form_data);
109
110  // Called when an autocomplete request succeeds or fails with the |result|.
111  void FinishAutocompleteRequest(
112      blink::WebFormElement::AutocompleteResult result);
113
114  // Called in a posted task by textFieldDidChange() to work-around a WebKit bug
115  // http://bugs.webkit.org/show_bug.cgi?id=16976
116  void TextFieldDidChangeImpl(const blink::WebInputElement& element);
117
118  // Shows the autofill suggestions for |element|.
119  // This call is asynchronous and may or may not lead to the showing of a
120  // suggestion popup (no popup is shown if there are no available suggestions).
121  // |autofill_on_empty_values| specifies whether suggestions should be shown
122  // when |element| contains no text.
123  // |requires_caret_at_end| specifies whether suggestions should be shown when
124  // the caret is not after the last character in |element|.
125  // |display_warning_if_disabled| specifies whether a warning should be
126  // displayed to the user if Autofill has suggestions available, but cannot
127  // fill them because it is disabled (e.g. when trying to fill a credit card
128  // form on a non-secure website).
129  // |datalist_only| specifies whether all of <datalist> suggestions and no
130  // autofill suggestions are shown. |autofill_on_empty_values| and
131  // |requires_caret_at_end| are ignored if |datalist_only| is true.
132  void ShowSuggestions(const blink::WebInputElement& element,
133                       bool autofill_on_empty_values,
134                       bool requires_caret_at_end,
135                       bool display_warning_if_disabled,
136                       bool datalist_only);
137
138  // Queries the browser for Autocomplete and Autofill suggestions for the given
139  // |element|.
140  void QueryAutofillSuggestions(const blink::WebInputElement& element,
141                                bool display_warning_if_disabled,
142                                bool datalist_only);
143
144  // Sets the element value to reflect the selected |suggested_value|.
145  void AcceptDataListSuggestion(const base::string16& suggested_value);
146
147  // Fills |form| and |field| with the FormData and FormField corresponding to
148  // |node|. Returns true if the data was found; and false otherwise.
149  bool FindFormAndFieldForNode(
150      const blink::WebNode& node,
151      FormData* form,
152      FormFieldData* field) WARN_UNUSED_RESULT;
153
154  // Set |node| to display the given |value|.
155  void FillFieldWithValue(const base::string16& value,
156                          blink::WebInputElement* node);
157
158  // Set |node| to display the given |value| as a preview.  The preview is
159  // visible on screen to the user, but not visible to the page via the DOM or
160  // JavaScript.
161  void PreviewFieldWithValue(const base::string16& value,
162                             blink::WebInputElement* node);
163
164  // Hides any currently showing Autofill popup.
165  void HidePopup();
166
167  FormCache form_cache_;
168
169  PasswordAutofillAgent* password_autofill_agent_;  // Weak reference.
170  PasswordGenerationAgent* password_generation_agent_;  // Weak reference.
171
172  // The ID of the last request sent for form field Autofill.  Used to ignore
173  // out of date responses.
174  int autofill_query_id_;
175
176  // The element corresponding to the last request sent for form field Autofill.
177  blink::WebInputElement element_;
178
179  // The form element currently requesting an interactive autocomplete.
180  blink::WebFormElement in_flight_request_form_;
181
182  // All the form elements seen in the top frame.
183  std::vector<blink::WebFormElement> form_elements_;
184
185  // Pointer to the WebView. Used to access page scale factor.
186  blink::WebView* web_view_;
187
188  // Should we display a warning if autofill is disabled?
189  bool display_warning_if_disabled_;
190
191  // Was the query node autofilled prior to previewing the form?
192  bool was_query_node_autofilled_;
193
194  // Have we already shown Autofill suggestions for the field the user is
195  // currently editing?  Used to keep track of state for metrics logging.
196  bool has_shown_autofill_popup_for_current_edit_;
197
198  // If true we just set the node text so we shouldn't show the popup.
199  bool did_set_node_text_;
200
201  // Whether or not new forms/fields have been dynamically added
202  // since the last loaded forms were sent to the browser process.
203  bool has_new_forms_for_browser_;
204
205  // Whether or not to ignore text changes.  Useful for when we're committing
206  // a composition when we are defocusing the WebView and we don't want to
207  // trigger an autofill popup to show.
208  bool ignore_text_changes_;
209
210  // Whether the Autofill popup is possibly visible.  This is tracked as a
211  // performance improvement, so that the IPC channel isn't flooded with
212  // messages to close the Autofill popup when it can't possibly be showing.
213  bool is_popup_possibly_visible_;
214
215  // Timestamp of first time forms are seen.
216  base::TimeTicks forms_seen_timestamp_;
217
218  base::WeakPtrFactory<AutofillAgent> weak_ptr_factory_;
219
220  friend class PasswordAutofillAgentTest;
221  friend class RequestAutocompleteRendererTest;
222  FRIEND_TEST_ALL_PREFIXES(AutofillRendererTest, FillFormElement);
223  FRIEND_TEST_ALL_PREFIXES(AutofillRendererTest, SendDynamicForms);
224  FRIEND_TEST_ALL_PREFIXES(AutofillRendererTest, ShowAutofillWarning);
225  FRIEND_TEST_ALL_PREFIXES(PasswordAutofillAgentTest, WaitUsername);
226  FRIEND_TEST_ALL_PREFIXES(PasswordAutofillAgentTest, SuggestionAccept);
227  FRIEND_TEST_ALL_PREFIXES(PasswordAutofillAgentTest, SuggestionSelect);
228  FRIEND_TEST_ALL_PREFIXES(
229      PasswordAutofillAgentTest,
230      PasswordAutofillTriggersOnChangeEventsWaitForUsername);
231  FRIEND_TEST_ALL_PREFIXES(RequestAutocompleteRendererTest,
232                           NoCancelOnMainFrameNavigateAfterDone);
233  FRIEND_TEST_ALL_PREFIXES(RequestAutocompleteRendererTest,
234                           NoCancelOnSubframeNavigateAfterDone);
235  FRIEND_TEST_ALL_PREFIXES(RequestAutocompleteRendererTest,
236                           InvokingTwiceOnlyShowsOnce);
237
238  DISALLOW_COPY_AND_ASSIGN(AutofillAgent);
239};
240
241}  // namespace autofill
242
243#endif  // COMPONENTS_AUTOFILL_CONTENT_RENDERER_AUTOFILL_AGENT_H_
244