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_
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"
18 class WebInputElement
;
19 class WebKeyboardEvent
;
20 class WebSecurityOrigin
;
26 // This class is responsible for filling password forms.
27 // There is one PasswordAutofillAgent per RenderView.
28 class PasswordAutofillAgent
: public content::RenderViewObserver
{
30 explicit PasswordAutofillAgent(content::RenderView
* render_view
);
31 ~PasswordAutofillAgent() override
;
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
69 void FirstUserGestureObserved();
72 virtual bool OriginCanAccessPasswordManager(
73 const blink::WebSecurityOrigin
& origin
);
76 enum OtherPossibleUsernamesUsage
{
78 OTHER_POSSIBLE_USERNAMES_ABSENT
,
79 OTHER_POSSIBLE_USERNAMES_PRESENT
,
80 OTHER_POSSIBLE_USERNAME_SHOWN
,
81 OTHER_POSSIBLE_USERNAME_SELECTED
,
82 OTHER_POSSIBLE_USERNAMES_MAX
85 // Ways to restrict which passwords are saved in ProvisionallySavePassword.
86 enum ProvisionallySaveRestriction
{
88 RESTRICTION_NON_EMPTY_PASSWORD
92 blink::WebInputElement password_field
;
93 PasswordFormFillData fill_data
;
94 bool backspace_pressed_last
;
95 // The user manually edited the password more recently than the username was
97 bool password_was_edited_last
;
100 typedef std::map
<blink::WebElement
, PasswordInfo
> LoginToPasswordInfoMap
;
101 typedef std::map
<blink::WebElement
, blink::WebElement
> PasswordToLoginMap
;
102 typedef std::map
<blink::WebFrame
*,
103 linked_ptr
<PasswordForm
> > FrameToPasswordFormMap
;
105 // This class keeps track of autofilled password input elements and makes sure
106 // the autofilled password value is not accessible to JavaScript code until
107 // the user interacts with the page.
108 class PasswordValueGatekeeper
{
110 PasswordValueGatekeeper();
111 ~PasswordValueGatekeeper();
113 // Call this for every autofilled password field, so that the gatekeeper
114 // protects the value accordingly.
115 void RegisterElement(blink::WebInputElement
* element
);
117 // Call this to notify the gatekeeper that the user interacted with the
119 void OnUserGesture();
121 // Call this to reset the gatekeeper on a new page navigation.
125 // Make the value of |element| accessible to JavaScript code.
126 void ShowValue(blink::WebInputElement
* element
);
128 bool was_user_gesture_seen_
;
129 std::vector
<blink::WebInputElement
> elements_
;
131 DISALLOW_COPY_AND_ASSIGN(PasswordValueGatekeeper
);
134 // RenderViewObserver:
135 bool OnMessageReceived(const IPC::Message
& message
) override
;
136 void DidStartProvisionalLoad(blink::WebLocalFrame
* frame
) override
;
137 void DidStartLoading() override
;
138 void DidFinishDocumentLoad(blink::WebLocalFrame
* frame
) override
;
139 void DidFinishLoad(blink::WebLocalFrame
* frame
) override
;
140 void DidStopLoading() override
;
141 void FrameDetached(blink::WebFrame
* frame
) override
;
142 void FrameWillClose(blink::WebFrame
* frame
) override
;
143 void WillSendSubmitEvent(blink::WebLocalFrame
* frame
,
144 const blink::WebFormElement
& form
) override
;
145 void WillSubmitForm(blink::WebLocalFrame
* frame
,
146 const blink::WebFormElement
& form
) override
;
148 // RenderView IPC handlers:
149 void OnFillPasswordForm(const PasswordFormFillData
& form_data
);
150 void OnSetLoggingState(bool active
);
152 // Scans the given frame for password forms and sends them up to the browser.
153 // If |only_visible| is true, only forms visible in the layout are sent.
154 void SendPasswordForms(blink::WebFrame
* frame
, bool only_visible
);
156 bool ShowSuggestionPopup(const PasswordFormFillData
& fill_data
,
157 const blink::WebInputElement
& user_input
,
160 // Fills |login_input| and |password| with the most relevant suggestion from
161 // |fill_data| and shows a popup with other suggestions.
162 void PerformInlineAutocomplete(
163 const blink::WebInputElement
& username
,
164 const blink::WebInputElement
& password
,
165 const PasswordFormFillData
& fill_data
);
167 // Invoked when the passed frame is closing. Gives us a chance to clear any
168 // reference we may have to elements in that frame.
169 void FrameClosing(const blink::WebFrame
* frame
);
171 // Finds login information for a |node| that was previously filled.
172 bool FindLoginInfo(const blink::WebNode
& node
,
173 blink::WebInputElement
* found_input
,
174 PasswordInfo
** found_password
);
176 // Clears the preview for the username and password fields, restoring both to
177 // their previous filled state.
178 void ClearPreview(blink::WebInputElement
* username
,
179 blink::WebInputElement
* password
);
181 // If |provisionally_saved_forms_| contains a form for |current_frame| or its
182 // children, return such frame.
183 blink::WebFrame
* CurrentOrChildFrameWithSavedForms(
184 const blink::WebFrame
* current_frame
);
186 // Extracts a PasswordForm from |form| and saves it as
187 // |provisionally_saved_forms_[frame]|, as long as it satisfies |restriction|.
188 void ProvisionallySavePassword(blink::WebLocalFrame
* frame
,
189 const blink::WebFormElement
& form
,
190 ProvisionallySaveRestriction restriction
);
192 // The logins we have filled so far with their associated info.
193 LoginToPasswordInfoMap login_to_password_info_
;
194 // A (sort-of) reverse map to |login_to_password_info_|.
195 PasswordToLoginMap password_to_username_
;
197 // Used for UMA stats.
198 OtherPossibleUsernamesUsage usernames_usage_
;
200 // Pointer to the WebView. Used to access page scale factor.
201 blink::WebView
* web_view_
;
203 // Set if the user might be submitting a password form on the current page,
204 // but the submit may still fail (i.e. doesn't pass JavaScript validation).
205 FrameToPasswordFormMap provisionally_saved_forms_
;
207 PasswordValueGatekeeper gatekeeper_
;
209 // True indicates that user debug information should be logged.
210 bool logging_state_active_
;
212 // True indicates that the username field was autofilled, false otherwise.
213 bool was_username_autofilled_
;
214 // True indicates that the password field was autofilled, false otherwise.
215 bool was_password_autofilled_
;
217 // Records original starting point of username element's selection range
219 int username_selection_start_
;
221 // True indicates that all frames in a page have been rendered.
222 bool did_stop_loading_
;
224 base::WeakPtrFactory
<PasswordAutofillAgent
> weak_ptr_factory_
;
226 DISALLOW_COPY_AND_ASSIGN(PasswordAutofillAgent
);
229 } // namespace autofill
231 #endif // COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_AUTOFILL_AGENT_H_