• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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"
23 
24 namespace blink {
25 class WebNode;
26 class WebView;
27 struct WebAutocompleteParams;
28 }
29 
30 namespace autofill {
31 
32 struct FormData;
33 struct FormFieldData;
34 struct WebElementDescriptor;
35 class PasswordAutofillAgent;
36 class PasswordGenerationAgent;
37 
38 // AutofillAgent deals with Autofill related communications between WebKit and
39 // the browser.  There is one AutofillAgent per RenderView.
40 // This code was originally part of RenderView.
41 // Note that Autofill encompasses:
42 // - single text field suggestions, that we usually refer to as Autocomplete,
43 // - password form fill, refered to as Password Autofill, and
44 // - entire form fill based on one field entry, referred to as Form Autofill.
45 
46 class AutofillAgent : public content::RenderViewObserver,
47                       public PageClickListener,
48                       public blink::WebAutofillClient {
49  public:
50   // PasswordAutofillAgent is guaranteed to outlive AutofillAgent.
51   // PasswordGenerationAgent may be NULL. If it is not, then it is also
52   // guaranteed to outlive AutofillAgent.
53   AutofillAgent(content::RenderView* render_view,
54                 PasswordAutofillAgent* password_autofill_manager,
55                 PasswordGenerationAgent* password_generation_agent);
56   virtual ~AutofillAgent();
57 
58  private:
59   // content::RenderViewObserver:
60   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
61   virtual void DidFinishDocumentLoad(blink::WebLocalFrame* frame) OVERRIDE;
62   virtual void DidCommitProvisionalLoad(blink::WebLocalFrame* frame,
63                                         bool is_new_navigation) OVERRIDE;
64   virtual void FrameDetached(blink::WebFrame* frame) OVERRIDE;
65   virtual void FrameWillClose(blink::WebFrame* frame) OVERRIDE;
66   virtual void WillSubmitForm(blink::WebLocalFrame* frame,
67                               const blink::WebFormElement& form) OVERRIDE;
68   virtual void DidChangeScrollOffset(blink::WebLocalFrame* frame) OVERRIDE;
69   virtual void FocusedNodeChanged(const blink::WebNode& node) OVERRIDE;
70   virtual void OrientationChangeEvent() OVERRIDE;
71   virtual void Resized() OVERRIDE;
72 
73   // PageClickListener:
74   virtual void FormControlElementClicked(
75       const blink::WebFormControlElement& element,
76       bool was_focused) OVERRIDE;
77 
78   // blink::WebAutofillClient:
79   virtual void textFieldDidEndEditing(
80       const blink::WebInputElement& element);
81   virtual void textFieldDidChange(
82       const blink::WebFormControlElement& element);
83   virtual void textFieldDidReceiveKeyDown(
84       const blink::WebInputElement& element,
85       const blink::WebKeyboardEvent& event);
86   virtual void didRequestAutocomplete(
87       const blink::WebFormElement& form);
88   virtual void setIgnoreTextChanges(bool ignore);
89   virtual void didAssociateFormControls(
90       const blink::WebVector<blink::WebNode>& nodes);
91   virtual void openTextDataListChooser(const blink::WebInputElement& element);
92   virtual void firstUserGestureObserved();
93 
94   void OnFieldTypePredictionsAvailable(
95       const std::vector<FormDataPredictions>& forms);
96   void OnFillForm(int query_id, const FormData& form);
97   void OnPing();
98   void OnPreviewForm(int query_id, const FormData& form);
99 
100   // For external Autofill selection.
101   void OnClearForm();
102   void OnClearPreviewedForm();
103   void OnFillFieldWithValue(const base::string16& value);
104   void OnPreviewFieldWithValue(const base::string16& value);
105   void OnAcceptDataListSuggestion(const base::string16& value);
106   void OnFillPasswordSuggestion(const base::string16& username,
107                                 const base::string16& password);
108   void OnPreviewPasswordSuggestion(const base::string16& username,
109                                    const base::string16& password);
110 
111   // Called when interactive autocomplete finishes. |message| is printed to
112   // the console if non-empty.
113   void OnRequestAutocompleteResult(
114       blink::WebFormElement::AutocompleteResult result,
115       const base::string16& message,
116       const FormData& form_data);
117 
118   // Called when an autocomplete request succeeds or fails with the |result|.
119   void FinishAutocompleteRequest(
120       blink::WebFormElement::AutocompleteResult result);
121 
122   // Called in a posted task by textFieldDidChange() to work-around a WebKit bug
123   // http://bugs.webkit.org/show_bug.cgi?id=16976
124   void TextFieldDidChangeImpl(const blink::WebFormControlElement& element);
125 
126   // Shows the autofill suggestions for |element|.
127   // This call is asynchronous and may or may not lead to the showing of a
128   // suggestion popup (no popup is shown if there are no available suggestions).
129   // |autofill_on_empty_values| specifies whether suggestions should be shown
130   // when |element| contains no text.
131   // |requires_caret_at_end| specifies whether suggestions should be shown when
132   // the caret is not after the last character in |element|.
133   // |display_warning_if_disabled| specifies whether a warning should be
134   // displayed to the user if Autofill has suggestions available, but cannot
135   // fill them because it is disabled (e.g. when trying to fill a credit card
136   // form on a non-secure website).
137   // |datalist_only| specifies whether all of <datalist> suggestions and no
138   // autofill suggestions are shown. |autofill_on_empty_values| and
139   // |requires_caret_at_end| are ignored if |datalist_only| is true.
140   // |show_full_suggestion_list| specifies that all autofill suggestions should
141   // be shown and none should be elided because of the current value of
142   // |element| (relevant for inline autocomplete).
143   // |show_password_suggestions_only| specifies that only show a suggestions box
144   // if |element| is part of a password form, otherwise show no suggestions.
145   void ShowSuggestions(const blink::WebFormControlElement& element,
146                        bool autofill_on_empty_values,
147                        bool requires_caret_at_end,
148                        bool display_warning_if_disabled,
149                        bool datalist_only,
150                        bool show_full_suggestion_list,
151                        bool show_password_suggestions_only);
152 
153   // Queries the browser for Autocomplete and Autofill suggestions for the given
154   // |element|.
155   void QueryAutofillSuggestions(const blink::WebFormControlElement& element,
156                                 bool display_warning_if_disabled,
157                                 bool datalist_only);
158 
159   // Sets the element value to reflect the selected |suggested_value|.
160   void AcceptDataListSuggestion(const base::string16& suggested_value);
161 
162   // Fills |form| and |field| with the FormData and FormField corresponding to
163   // |node|. Returns true if the data was found; and false otherwise.
164   bool FindFormAndFieldForNode(
165       const blink::WebNode& node,
166       FormData* form,
167       FormFieldData* field) WARN_UNUSED_RESULT;
168 
169   // Set |node| to display the given |value|.
170   void FillFieldWithValue(const base::string16& value,
171                           blink::WebInputElement* node);
172 
173   // Set |node| to display the given |value| as a preview.  The preview is
174   // visible on screen to the user, but not visible to the page via the DOM or
175   // JavaScript.
176   void PreviewFieldWithValue(const base::string16& value,
177                              blink::WebInputElement* node);
178 
179   // Notifies browser of new fillable forms in |frame|.
180   void ProcessForms(const blink::WebLocalFrame& frame);
181 
182   // Hides any currently showing Autofill popup.
183   void HidePopup();
184 
185   FormCache form_cache_;
186 
187   PasswordAutofillAgent* password_autofill_agent_;  // Weak reference.
188   PasswordGenerationAgent* password_generation_agent_;  // Weak reference.
189 
190   // The ID of the last request sent for form field Autofill.  Used to ignore
191   // out of date responses.
192   int autofill_query_id_;
193 
194   // The element corresponding to the last request sent for form field Autofill.
195   blink::WebFormControlElement element_;
196 
197   // The form element currently requesting an interactive autocomplete.
198   blink::WebFormElement in_flight_request_form_;
199 
200   // Pointer to the WebView. Used to access page scale factor.
201   blink::WebView* web_view_;
202 
203   // Should we display a warning if autofill is disabled?
204   bool display_warning_if_disabled_;
205 
206   // Was the query node autofilled prior to previewing the form?
207   bool was_query_node_autofilled_;
208 
209   // Have we already shown Autofill suggestions for the field the user is
210   // currently editing?  Used to keep track of state for metrics logging.
211   bool has_shown_autofill_popup_for_current_edit_;
212 
213   // If true we just set the node text so we shouldn't show the popup.
214   bool did_set_node_text_;
215 
216   // Whether or not to ignore text changes.  Useful for when we're committing
217   // a composition when we are defocusing the WebView and we don't want to
218   // trigger an autofill popup to show.
219   bool ignore_text_changes_;
220 
221   // Whether the Autofill popup is possibly visible.  This is tracked as a
222   // performance improvement, so that the IPC channel isn't flooded with
223   // messages to close the Autofill popup when it can't possibly be showing.
224   bool is_popup_possibly_visible_;
225 
226   // True if a message has already been sent about forms for the main frame.
227   // When the main frame is first loaded, a message is sent even if no forms
228   // exist in the frame. Otherwise, such messages are supressed.
229   bool main_frame_processed_;
230 
231   base::WeakPtrFactory<AutofillAgent> weak_ptr_factory_;
232 
233   DISALLOW_COPY_AND_ASSIGN(AutofillAgent);
234 };
235 
236 }  // namespace autofill
237 
238 #endif  // COMPONENTS_AUTOFILL_CONTENT_RENDERER_AUTOFILL_AGENT_H_
239