Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / components / autofill / content / renderer / password_autofill_agent.h
blob9e82bcd73bcb3020350ee07e0bca65c4d9db958d
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/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "components/autofill/core/common/form_data_predictions.h"
15 #include "components/autofill/core/common/password_form_field_prediction_map.h"
16 #include "components/autofill/core/common/password_form_fill_data.h"
17 #include "content/public/renderer/render_frame_observer.h"
18 #include "content/public/renderer/render_view_observer.h"
19 #include "third_party/WebKit/public/web/WebInputElement.h"
21 namespace blink {
22 class WebInputElement;
23 class WebKeyboardEvent;
24 class WebSecurityOrigin;
27 namespace autofill {
29 // This class is responsible for filling password forms.
30 class PasswordAutofillAgent : public content::RenderFrameObserver {
31 public:
32 explicit PasswordAutofillAgent(content::RenderFrame* render_frame);
33 ~PasswordAutofillAgent() override;
35 // WebFrameClient editor related calls forwarded by AutofillAgent.
36 // If they return true, it indicates the event was consumed and should not
37 // be used for any other autofill activity.
38 bool TextFieldDidEndEditing(const blink::WebInputElement& element);
39 bool TextDidChangeInTextField(const blink::WebInputElement& element);
40 bool TextFieldHandlingKeyDown(const blink::WebInputElement& element,
41 const blink::WebKeyboardEvent& event);
43 // Fills the username and password fields of this form with the given values.
44 // Returns true if the fields were filled, false otherwise.
45 bool FillSuggestion(const blink::WebNode& node,
46 const blink::WebString& username,
47 const blink::WebString& password);
49 // Previews the username and password fields of this form with the given
50 // values. Returns true if the fields were previewed, false otherwise.
51 bool PreviewSuggestion(const blink::WebNode& node,
52 const blink::WebString& username,
53 const blink::WebString& password);
55 // Clears the preview for the username and password fields, restoring both to
56 // their previous filled state. Return false if no login information was
57 // found for the form.
58 bool DidClearAutofillSelection(const blink::WebNode& node);
60 // Shows an Autofill popup with username suggestions for |element|. If
61 // |show_all| is |true|, will show all possible suggestions for that element,
62 // otherwise shows suggestions based on current value of |element|.
63 // Returns true if any suggestions were shown, false otherwise.
64 bool ShowSuggestions(const blink::WebInputElement& element, bool show_all);
66 // Called when new form controls are inserted.
67 void OnDynamicFormsSeen();
69 // Called when an XHR has succesfully completed. Used to determine if
70 // a form has been submitted by XHR without navigation.
71 void XHRSucceeded();
73 // Called when the user first interacts with the page after a load. This is a
74 // signal to make autofilled values of password input elements accessible to
75 // JavaScript.
76 void FirstUserGestureObserved();
78 protected:
79 virtual bool OriginCanAccessPasswordManager(
80 const blink::WebSecurityOrigin& origin);
82 private:
83 enum OtherPossibleUsernamesUsage {
84 NOTHING_TO_AUTOFILL,
85 OTHER_POSSIBLE_USERNAMES_ABSENT,
86 OTHER_POSSIBLE_USERNAMES_PRESENT,
87 OTHER_POSSIBLE_USERNAME_SHOWN,
88 OTHER_POSSIBLE_USERNAME_SELECTED,
89 OTHER_POSSIBLE_USERNAMES_MAX
92 // Ways to restrict which passwords are saved in ProvisionallySavePassword.
93 enum ProvisionallySaveRestriction {
94 RESTRICTION_NONE,
95 RESTRICTION_NON_EMPTY_PASSWORD
98 struct PasswordInfo {
99 blink::WebInputElement password_field;
100 PasswordFormFillData fill_data;
101 bool backspace_pressed_last;
102 // The user manually edited the password more recently than the username was
103 // changed.
104 bool password_was_edited_last;
105 PasswordInfo();
107 typedef std::map<blink::WebInputElement, PasswordInfo> LoginToPasswordInfoMap;
108 typedef std::map<blink::WebElement, int> LoginToPasswordInfoKeyMap;
109 typedef std::map<blink::WebInputElement, blink::WebInputElement>
110 PasswordToLoginMap;
111 using FormsPredictionsMap =
112 std::map<autofill::FormData, autofill::PasswordFormFieldPredictionMap>;
114 // This class keeps track of autofilled password input elements and makes sure
115 // the autofilled password value is not accessible to JavaScript code until
116 // the user interacts with the page.
117 class PasswordValueGatekeeper {
118 public:
119 PasswordValueGatekeeper();
120 ~PasswordValueGatekeeper();
122 // Call this for every autofilled password field, so that the gatekeeper
123 // protects the value accordingly.
124 void RegisterElement(blink::WebInputElement* element);
126 // Call this to notify the gatekeeper that the user interacted with the
127 // page.
128 void OnUserGesture();
130 // Call this to reset the gatekeeper on a new page navigation.
131 void Reset();
133 private:
134 // Make the value of |element| accessible to JavaScript code.
135 void ShowValue(blink::WebInputElement* element);
137 bool was_user_gesture_seen_;
138 std::vector<blink::WebInputElement> elements_;
140 DISALLOW_COPY_AND_ASSIGN(PasswordValueGatekeeper);
143 // Thunk class for RenderViewObserver methods that haven't yet been migrated
144 // to RenderFrameObserver. Should eventually be removed.
145 // http://crbug.com/433486
146 class LegacyPasswordAutofillAgent : public content::RenderViewObserver {
147 public:
148 LegacyPasswordAutofillAgent(content::RenderView* render_view,
149 PasswordAutofillAgent* agent);
150 ~LegacyPasswordAutofillAgent() override;
152 // RenderViewObserver:
153 void OnDestruct() override;
154 void DidStartLoading() override;
155 void DidStopLoading() override;
156 void DidStartProvisionalLoad(blink::WebLocalFrame* frame) override;
158 private:
159 PasswordAutofillAgent* agent_;
161 DISALLOW_COPY_AND_ASSIGN(LegacyPasswordAutofillAgent);
163 friend class LegacyPasswordAutofillAgent;
165 // RenderFrameObserver:
166 bool OnMessageReceived(const IPC::Message& message) override;
167 void DidFinishDocumentLoad() override;
168 void DidFinishLoad() override;
169 void FrameDetached() override;
170 void FrameWillClose() override;
171 void DidCommitProvisionalLoad(bool is_new_navigation,
172 bool is_same_page_navigation) override;
173 void WillSendSubmitEvent(const blink::WebFormElement& form) override;
174 void WillSubmitForm(const blink::WebFormElement& form) override;
176 // Legacy RenderViewObserver:
177 void DidStartLoading();
178 void DidStopLoading();
179 void LegacyDidStartProvisionalLoad(blink::WebLocalFrame* frame);
181 // RenderView IPC handlers:
182 void OnFillPasswordForm(int key, const PasswordFormFillData& form_data);
183 void OnSetLoggingState(bool active);
184 void OnAutofillUsernameAndPasswordDataReceived(
185 const FormsPredictionsMap& predictions);
187 // Scans the given frame for password forms and sends them up to the browser.
188 // If |only_visible| is true, only forms visible in the layout are sent.
189 void SendPasswordForms(bool only_visible);
191 // Instructs the browser to show a pop-up suggesting which credentials could
192 // be filled. |show_in_password_field| should indicate whether the pop-up is
193 // to be shown on the password field instead of on the username field. If the
194 // username exists, it should be passed as |user_input|. If there is no
195 // username, pass the password field in |user_input|. In the latter case, no
196 // username value will be shown in the pop-up.
197 bool ShowSuggestionPopup(const PasswordFormFillData& fill_data,
198 const blink::WebInputElement& user_input,
199 bool show_all,
200 bool show_on_password_field);
202 // Finds the PasswordInfo that corresponds to the passed in element. The
203 // passed in element can be either a username element or a password element.
204 // If a PasswordInfo was found, returns |true| and also assigns the
205 // corresponding username WebInputElement and PasswordInfo into
206 // username_element and pasword_info, respectively. Note, that
207 // |username_element->isNull()| can be true for forms without a username
208 // field.
209 bool FindPasswordInfoForElement(
210 const blink::WebInputElement& element,
211 const blink::WebInputElement** username_element,
212 PasswordInfo** password_info);
214 // Fills |login_input| and |password| with the most relevant suggestion from
215 // |fill_data| and shows a popup with other suggestions.
216 void PerformInlineAutocomplete(
217 const blink::WebInputElement& username,
218 const blink::WebInputElement& password,
219 const PasswordFormFillData& fill_data);
221 // Invoked when the frame is closing.
222 void FrameClosing();
224 // Finds login information for a |node| that was previously filled.
225 bool FindLoginInfo(const blink::WebNode& node,
226 blink::WebInputElement* found_input,
227 PasswordInfo** found_password);
229 // Clears the preview for the username and password fields, restoring both to
230 // their previous filled state.
231 void ClearPreview(blink::WebInputElement* username,
232 blink::WebInputElement* password);
234 // Extracts a PasswordForm from |form| and saves it as
235 // |provisionally_saved_form_|, as long as it satisfies |restriction|.
236 void ProvisionallySavePassword(const blink::WebFormElement& form,
237 ProvisionallySaveRestriction restriction);
239 // Returns true if |provisionally_saved_form_| has enough information that
240 // it is likely filled out.
241 bool ProvisionallySavedPasswordIsValid();
243 // Passes through |RenderViewObserver| method to |this|.
244 LegacyPasswordAutofillAgent legacy_;
246 // The logins we have filled so far with their associated info.
247 LoginToPasswordInfoMap login_to_password_info_;
248 // And the keys under which PasswordAutofillManager can find the same info.
249 LoginToPasswordInfoKeyMap login_to_password_info_key_;
250 // A (sort-of) reverse map to |login_to_password_info_|.
251 PasswordToLoginMap password_to_username_;
253 // Used for UMA stats.
254 OtherPossibleUsernamesUsage usernames_usage_;
256 // Set if the user might be submitting a password form on the current page,
257 // but the submit may still fail (i.e. doesn't pass JavaScript validation).
258 scoped_ptr<PasswordForm> provisionally_saved_form_;
260 // Contains the most recent text that user typed or PasswordManager autofilled
261 // in input elements. Used for storing username/password before JavaScript
262 // changes them.
263 std::map<const blink::WebInputElement, blink::WebString>
264 nonscript_modified_values_;
266 PasswordValueGatekeeper gatekeeper_;
268 // True indicates that user debug information should be logged.
269 bool logging_state_active_;
271 // True indicates that the username field was autofilled, false otherwise.
272 bool was_username_autofilled_;
273 // True indicates that the password field was autofilled, false otherwise.
274 bool was_password_autofilled_;
276 // Records original starting point of username element's selection range
277 // before preview.
278 int username_selection_start_;
280 // True indicates that all frames in a page have been rendered.
281 bool did_stop_loading_;
283 // True indicates that there is a command line flag to enable showing of
284 // save password prompt on in-page navigations.
285 bool save_password_on_in_page_navigation_;
287 // Contains server predictions for username, password and/or new password
288 // fields for individual forms.
289 FormsPredictionsMap form_predictions_;
291 base::WeakPtrFactory<PasswordAutofillAgent> weak_ptr_factory_;
293 DISALLOW_COPY_AND_ASSIGN(PasswordAutofillAgent);
296 } // namespace autofill
298 #endif // COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_AUTOFILL_AGENT_H_