Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / components / autofill / content / renderer / password_autofill_agent.h
blobb6ab805249072b320e82ee9c9fa01dbc0b81acc1
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_PASSWORD_AUTOFILL_AGENT_H_
6 #define COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_AUTOFILL_AGENT_H_
8 #include <map>
9 #include <vector>
11 #include "base/memory/linked_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "components/autofill/core/common/password_form_fill_data.h"
14 #include "content/public/renderer/render_view_observer.h"
15 #include "third_party/WebKit/public/web/WebInputElement.h"
17 namespace blink {
18 class WebInputElement;
19 class WebKeyboardEvent;
20 class WebSecurityOrigin;
21 class WebView;
24 namespace autofill {
26 // This class is responsible for filling password forms.
27 // There is one PasswordAutofillAgent per RenderView.
28 class PasswordAutofillAgent : public content::RenderViewObserver {
29 public:
30 explicit PasswordAutofillAgent(content::RenderView* render_view);
31 virtual ~PasswordAutofillAgent();
33 // WebViewClient editor related calls forwarded by the RenderView.
34 // If they return true, it indicates the event was consumed and should not
35 // be used for any other autofill activity.
36 bool TextFieldDidEndEditing(const blink::WebInputElement& element);
37 bool TextDidChangeInTextField(const blink::WebInputElement& element);
38 bool TextFieldHandlingKeyDown(const blink::WebInputElement& element,
39 const blink::WebKeyboardEvent& event);
41 // Fills the username and password fields of this form with the given values.
42 // Returns true if the fields were filled, false otherwise.
43 bool FillSuggestion(const blink::WebNode& node,
44 const blink::WebString& username,
45 const blink::WebString& password);
47 // Previews the username and password fields of this form with the given
48 // values. Returns true if the fields were previewed, false otherwise.
49 bool PreviewSuggestion(const blink::WebNode& node,
50 const blink::WebString& username,
51 const blink::WebString& password);
53 // Clears the preview for the username and password fields, restoring both to
54 // their previous filled state. Return false if no login information was
55 // found for the form.
56 bool DidClearAutofillSelection(const blink::WebNode& node);
57 // Shows an Autofill popup with username suggestions for |element|. If
58 // |show_all| is |true|, will show all possible suggestions for that element,
59 // otherwise shows suggestions based on current value of |element|.
60 // Returns true if any suggestions were shown, false otherwise.
61 bool ShowSuggestions(const blink::WebInputElement& element, bool show_all);
63 // Called when new form controls are inserted.
64 void OnDynamicFormsSeen(blink::WebFrame* frame);
66 // Called when the user first interacts with the page after a load. This is a
67 // signal to make autofilled values of password input elements accessible to
68 // JavaScript.
69 void FirstUserGestureObserved();
71 protected:
72 virtual bool OriginCanAccessPasswordManager(
73 const blink::WebSecurityOrigin& origin);
75 private:
76 friend class PasswordAutofillAgentTest;
78 enum OtherPossibleUsernamesUsage {
79 NOTHING_TO_AUTOFILL,
80 OTHER_POSSIBLE_USERNAMES_ABSENT,
81 OTHER_POSSIBLE_USERNAMES_PRESENT,
82 OTHER_POSSIBLE_USERNAME_SHOWN,
83 OTHER_POSSIBLE_USERNAME_SELECTED,
84 OTHER_POSSIBLE_USERNAMES_MAX
87 // Ways to restrict which passwords are saved in ProvisionallySavePassword.
88 enum ProvisionallySaveRestriction {
89 RESTRICTION_NONE,
90 RESTRICTION_NON_EMPTY_PASSWORD
93 struct PasswordInfo {
94 blink::WebInputElement password_field;
95 PasswordFormFillData fill_data;
96 bool backspace_pressed_last;
97 // The user manually edited the password more recently than the username was
98 // changed.
99 bool password_was_edited_last;
100 PasswordInfo();
102 typedef std::map<blink::WebElement, PasswordInfo> LoginToPasswordInfoMap;
103 typedef std::map<blink::WebElement, blink::WebElement> PasswordToLoginMap;
104 typedef std::map<blink::WebFrame*,
105 linked_ptr<PasswordForm> > FrameToPasswordFormMap;
107 // This class keeps track of autofilled password input elements and makes sure
108 // the autofilled password value is not accessible to JavaScript code until
109 // the user interacts with the page.
110 class PasswordValueGatekeeper {
111 public:
112 PasswordValueGatekeeper();
113 ~PasswordValueGatekeeper();
115 // Call this for every autofilled password field, so that the gatekeeper
116 // protects the value accordingly.
117 void RegisterElement(blink::WebInputElement* element);
119 // Call this to notify the gatekeeper that the user interacted with the
120 // page.
121 void OnUserGesture();
123 // Call this to reset the gatekeeper on a new page navigation.
124 void Reset();
126 private:
127 // Make the value of |element| accessible to JavaScript code.
128 void ShowValue(blink::WebInputElement* element);
130 bool was_user_gesture_seen_;
131 std::vector<blink::WebInputElement> elements_;
133 DISALLOW_COPY_AND_ASSIGN(PasswordValueGatekeeper);
136 // RenderViewObserver:
137 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
138 virtual void DidStartProvisionalLoad(blink::WebLocalFrame* frame) OVERRIDE;
139 virtual void DidStartLoading() OVERRIDE;
140 virtual void DidFinishDocumentLoad(blink::WebLocalFrame* frame) OVERRIDE;
141 virtual void DidFinishLoad(blink::WebLocalFrame* frame) OVERRIDE;
142 virtual void DidStopLoading() OVERRIDE;
143 virtual void FrameDetached(blink::WebFrame* frame) OVERRIDE;
144 virtual void FrameWillClose(blink::WebFrame* frame) OVERRIDE;
145 virtual void WillSendSubmitEvent(blink::WebLocalFrame* frame,
146 const blink::WebFormElement& form) OVERRIDE;
147 virtual void WillSubmitForm(blink::WebLocalFrame* frame,
148 const blink::WebFormElement& form) OVERRIDE;
150 // RenderView IPC handlers:
151 void OnFillPasswordForm(const PasswordFormFillData& form_data);
152 void OnSetLoggingState(bool active);
154 // Scans the given frame for password forms and sends them up to the browser.
155 // If |only_visible| is true, only forms visible in the layout are sent.
156 void SendPasswordForms(blink::WebFrame* frame, bool only_visible);
158 void GetSuggestions(const PasswordFormFillData& fill_data,
159 const base::string16& input,
160 std::vector<base::string16>* suggestions,
161 std::vector<base::string16>* realms,
162 bool show_all);
164 bool ShowSuggestionPopup(const PasswordFormFillData& fill_data,
165 const blink::WebInputElement& user_input,
166 bool show_all);
168 // Attempts to fill |username_element| and |password_element| with the
169 // |fill_data|. Will use the data corresponding to the preferred username,
170 // unless the |username_element| already has a value set. In that case,
171 // attempts to fill the password matching the already filled username, if
172 // such a password exists.
173 void FillFormOnPasswordRecieved(const PasswordFormFillData& fill_data,
174 blink::WebInputElement username_element,
175 blink::WebInputElement password_element);
177 bool FillUserNameAndPassword(blink::WebInputElement* username_element,
178 blink::WebInputElement* password_element,
179 const PasswordFormFillData& fill_data,
180 bool exact_username_match,
181 bool set_selection);
183 // Fills |login_input| and |password| with the most relevant suggestion from
184 // |fill_data| and shows a popup with other suggestions.
185 void PerformInlineAutocomplete(
186 const blink::WebInputElement& username,
187 const blink::WebInputElement& password,
188 const PasswordFormFillData& fill_data);
190 // Invoked when the passed frame is closing. Gives us a chance to clear any
191 // reference we may have to elements in that frame.
192 void FrameClosing(const blink::WebFrame* frame);
194 // Finds login information for a |node| that was previously filled.
195 bool FindLoginInfo(const blink::WebNode& node,
196 blink::WebInputElement* found_input,
197 PasswordInfo** found_password);
199 // Clears the preview for the username and password fields, restoring both to
200 // their previous filled state.
201 void ClearPreview(blink::WebInputElement* username,
202 blink::WebInputElement* password);
204 // If |provisionally_saved_forms_| contains a form for |current_frame| or its
205 // children, return such frame.
206 blink::WebFrame* CurrentOrChildFrameWithSavedForms(
207 const blink::WebFrame* current_frame);
209 // Extracts a PasswordForm from |form| and saves it as
210 // |provisionally_saved_forms_[frame]|, as long as it satisfies |restriction|.
211 void ProvisionallySavePassword(blink::WebLocalFrame* frame,
212 const blink::WebFormElement& form,
213 ProvisionallySaveRestriction restriction);
215 // The logins we have filled so far with their associated info.
216 LoginToPasswordInfoMap login_to_password_info_;
217 // A (sort-of) reverse map to |login_to_password_info_|.
218 PasswordToLoginMap password_to_username_;
220 // Used for UMA stats.
221 OtherPossibleUsernamesUsage usernames_usage_;
223 // Pointer to the WebView. Used to access page scale factor.
224 blink::WebView* web_view_;
226 // Set if the user might be submitting a password form on the current page,
227 // but the submit may still fail (i.e. doesn't pass JavaScript validation).
228 FrameToPasswordFormMap provisionally_saved_forms_;
230 PasswordValueGatekeeper gatekeeper_;
232 // True indicates that user debug information should be logged.
233 bool logging_state_active_;
235 // True indicates that the username field was autofilled, false otherwise.
236 bool was_username_autofilled_;
237 // True indicates that the password field was autofilled, false otherwise.
238 bool was_password_autofilled_;
240 // Records original starting point of username element's selection range
241 // before preview.
242 int username_selection_start_;
244 // True indicates that all frames in a page have been rendered.
245 bool did_stop_loading_;
247 base::WeakPtrFactory<PasswordAutofillAgent> weak_ptr_factory_;
249 DISALLOW_COPY_AND_ASSIGN(PasswordAutofillAgent);
252 } // namespace autofill
254 #endif // COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_AUTOFILL_AGENT_H_