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/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "components/autofill/core/common/password_form_fill_data.h"
15 #include "content/public/renderer/render_frame_observer.h"
16 #include "content/public/renderer/render_view_observer.h"
17 #include "third_party/WebKit/public/web/WebInputElement.h"
20 class WebInputElement
;
21 class WebKeyboardEvent
;
22 class WebSecurityOrigin
;
27 // This class is responsible for filling password forms.
28 class PasswordAutofillAgent
: public content::RenderFrameObserver
{
30 explicit PasswordAutofillAgent(content::RenderFrame
* render_frame
);
31 ~PasswordAutofillAgent() override
;
33 // WebFrameClient editor related calls forwarded by AutofillAgent.
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
);
58 // Shows an Autofill popup with username suggestions for |element|. If
59 // |show_all| is |true|, will show all possible suggestions for that element,
60 // otherwise shows suggestions based on current value of |element|.
61 // Returns true if any suggestions were shown, false otherwise.
62 bool ShowSuggestions(const blink::WebInputElement
& element
, bool show_all
);
64 // Called when new form controls are inserted.
65 void OnDynamicFormsSeen();
67 // Called when the user first interacts with the page after a load. This is a
68 // signal to make autofilled values of password input elements accessible to
70 void FirstUserGestureObserved();
73 virtual bool OriginCanAccessPasswordManager(
74 const blink::WebSecurityOrigin
& origin
);
77 enum OtherPossibleUsernamesUsage
{
79 OTHER_POSSIBLE_USERNAMES_ABSENT
,
80 OTHER_POSSIBLE_USERNAMES_PRESENT
,
81 OTHER_POSSIBLE_USERNAME_SHOWN
,
82 OTHER_POSSIBLE_USERNAME_SELECTED
,
83 OTHER_POSSIBLE_USERNAMES_MAX
86 // Ways to restrict which passwords are saved in ProvisionallySavePassword.
87 enum ProvisionallySaveRestriction
{
89 RESTRICTION_NON_EMPTY_PASSWORD
93 blink::WebInputElement password_field
;
94 PasswordFormFillData fill_data
;
95 bool backspace_pressed_last
;
96 // The user manually edited the password more recently than the username was
98 bool password_was_edited_last
;
101 typedef std::map
<blink::WebInputElement
, PasswordInfo
> LoginToPasswordInfoMap
;
102 typedef std::map
<blink::WebElement
, int> LoginToPasswordInfoKeyMap
;
103 typedef std::map
<blink::WebInputElement
, blink::WebInputElement
>
106 // This class keeps track of autofilled password input elements and makes sure
107 // the autofilled password value is not accessible to JavaScript code until
108 // the user interacts with the page.
109 class PasswordValueGatekeeper
{
111 PasswordValueGatekeeper();
112 ~PasswordValueGatekeeper();
114 // Call this for every autofilled password field, so that the gatekeeper
115 // protects the value accordingly.
116 void RegisterElement(blink::WebInputElement
* element
);
118 // Call this to notify the gatekeeper that the user interacted with the
120 void OnUserGesture();
122 // Call this to reset the gatekeeper on a new page navigation.
126 // Make the value of |element| accessible to JavaScript code.
127 void ShowValue(blink::WebInputElement
* element
);
129 bool was_user_gesture_seen_
;
130 std::vector
<blink::WebInputElement
> elements_
;
132 DISALLOW_COPY_AND_ASSIGN(PasswordValueGatekeeper
);
135 // Thunk class for RenderViewObserver methods that haven't yet been migrated
136 // to RenderFrameObserver. Should eventually be removed.
137 // http://crbug.com/433486
138 class LegacyPasswordAutofillAgent
: public content::RenderViewObserver
{
140 LegacyPasswordAutofillAgent(content::RenderView
* render_view
,
141 PasswordAutofillAgent
* agent
);
142 ~LegacyPasswordAutofillAgent() override
;
144 // RenderViewObserver:
145 void OnDestruct() override
;
146 void DidStartLoading() override
;
147 void DidStopLoading() override
;
148 void DidStartProvisionalLoad(blink::WebLocalFrame
* frame
) override
;
151 PasswordAutofillAgent
* agent_
;
153 DISALLOW_COPY_AND_ASSIGN(LegacyPasswordAutofillAgent
);
155 friend class LegacyPasswordAutofillAgent
;
157 // RenderFrameObserver:
158 bool OnMessageReceived(const IPC::Message
& message
) override
;
159 void DidFinishDocumentLoad() override
;
160 void DidFinishLoad() override
;
161 void FrameDetached() override
;
162 void FrameWillClose() override
;
163 void DidCommitProvisionalLoad(bool is_new_navigation
,
164 bool is_same_page_navigation
) override
;
165 void WillSendSubmitEvent(const blink::WebFormElement
& form
) override
;
166 void WillSubmitForm(const blink::WebFormElement
& form
) override
;
168 // Legacy RenderViewObserver:
169 void DidStartLoading();
170 void DidStopLoading();
171 void LegacyDidStartProvisionalLoad(blink::WebLocalFrame
* frame
);
173 // RenderView IPC handlers:
174 void OnFillPasswordForm(int key
, const PasswordFormFillData
& form_data
);
175 void OnSetLoggingState(bool active
);
177 // Scans the given frame for password forms and sends them up to the browser.
178 // If |only_visible| is true, only forms visible in the layout are sent.
179 void SendPasswordForms(bool only_visible
);
181 // Instructs the browser to show a pop-up suggesting which credentials could
182 // be filled. |show_in_password_field| should indicate whether the pop-up is
183 // to be shown on the password field instead of on the username field. If the
184 // username exists, it should be passed as |user_input|. If there is no
185 // username, pass the password field in |user_input|. In the latter case, no
186 // username value will be shown in the pop-up.
187 bool ShowSuggestionPopup(const PasswordFormFillData
& fill_data
,
188 const blink::WebInputElement
& user_input
,
190 bool show_on_password_field
);
192 // Finds the PasswordInfo that corresponds to the passed in element. The
193 // passed in element can be either a username element or a password element.
194 // If a PasswordInfo was found, returns |true| and also assigns the
195 // corresponding username WebInputElement and PasswordInfo into
196 // username_element and pasword_info, respectively. Note, that
197 // |username_element->isNull()| can be true for forms without a username
199 bool FindPasswordInfoForElement(
200 const blink::WebInputElement
& element
,
201 const blink::WebInputElement
** username_element
,
202 PasswordInfo
** password_info
);
204 // Fills |login_input| and |password| with the most relevant suggestion from
205 // |fill_data| and shows a popup with other suggestions.
206 void PerformInlineAutocomplete(
207 const blink::WebInputElement
& username
,
208 const blink::WebInputElement
& password
,
209 const PasswordFormFillData
& fill_data
);
211 // Invoked when the frame is closing.
214 // Finds login information for a |node| that was previously filled.
215 bool FindLoginInfo(const blink::WebNode
& node
,
216 blink::WebInputElement
* found_input
,
217 PasswordInfo
** found_password
);
219 // Clears the preview for the username and password fields, restoring both to
220 // their previous filled state.
221 void ClearPreview(blink::WebInputElement
* username
,
222 blink::WebInputElement
* password
);
224 // Extracts a PasswordForm from |form| and saves it as
225 // |provisionally_saved_form_|, as long as it satisfies |restriction|.
226 void ProvisionallySavePassword(const blink::WebFormElement
& form
,
227 ProvisionallySaveRestriction restriction
);
229 // Passes through |RenderViewObserver| method to |this|.
230 LegacyPasswordAutofillAgent legacy_
;
232 // The logins we have filled so far with their associated info.
233 LoginToPasswordInfoMap login_to_password_info_
;
234 // And the keys under which PasswordAutofillManager can find the same info.
235 LoginToPasswordInfoKeyMap login_to_password_info_key_
;
236 // A (sort-of) reverse map to |login_to_password_info_|.
237 PasswordToLoginMap password_to_username_
;
239 // Used for UMA stats.
240 OtherPossibleUsernamesUsage usernames_usage_
;
242 // Set if the user might be submitting a password form on the current page,
243 // but the submit may still fail (i.e. doesn't pass JavaScript validation).
244 scoped_ptr
<PasswordForm
> provisionally_saved_form_
;
246 // Contains the most recent text that user typed in input elements. Used for
247 // storing username/password before JavaScript changes them.
248 std::map
<const blink::WebInputElement
, blink::WebString
>
249 user_modified_elements_
;
251 PasswordValueGatekeeper gatekeeper_
;
253 // True indicates that user debug information should be logged.
254 bool logging_state_active_
;
256 // True indicates that the username field was autofilled, false otherwise.
257 bool was_username_autofilled_
;
258 // True indicates that the password field was autofilled, false otherwise.
259 bool was_password_autofilled_
;
261 // Records original starting point of username element's selection range
263 int username_selection_start_
;
265 // True indicates that all frames in a page have been rendered.
266 bool did_stop_loading_
;
268 // True indicates that there is a command line flag to enable showing of
269 // save password prompt on in-page navigations.
270 bool save_password_on_in_page_navigation_
;
272 base::WeakPtrFactory
<PasswordAutofillAgent
> weak_ptr_factory_
;
274 DISALLOW_COPY_AND_ASSIGN(PasswordAutofillAgent
);
277 } // namespace autofill
279 #endif // COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_AUTOFILL_AGENT_H_