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 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
69 void FirstUserGestureObserved();
72 virtual bool OriginCanAccessPasswordManager(
73 const blink::WebSecurityOrigin
& origin
);
76 friend class PasswordAutofillAgentTest
;
78 enum OtherPossibleUsernamesUsage
{
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
{
90 RESTRICTION_NON_EMPTY_PASSWORD
94 blink::WebInputElement password_field
;
95 PasswordFormFillData fill_data
;
96 bool backspace_pressed_last
;
97 PasswordInfo() : backspace_pressed_last(false) {}
99 typedef std::map
<blink::WebElement
, PasswordInfo
> LoginToPasswordInfoMap
;
100 typedef std::map
<blink::WebFrame
*,
101 linked_ptr
<PasswordForm
> > FrameToPasswordFormMap
;
103 // This class holds a vector of autofilled password input elements and makes
104 // sure the autofilled password value is not accessible to JavaScript code
105 // until the user interacts with the page.
106 class PasswordValueGatekeeper
{
108 PasswordValueGatekeeper();
109 ~PasswordValueGatekeeper();
111 // Call this for every autofilled password field, so that the gatekeeper
112 // protects the value accordingly.
113 void RegisterElement(blink::WebInputElement
* element
);
115 // Call this to notify the gatekeeper that the user interacted with the
117 void OnUserGesture();
119 // Call this to reset the gatekeeper on a new page navigation.
123 // Make the value of |element| accessible to JavaScript code.
124 void ShowValue(blink::WebInputElement
* element
);
126 bool was_user_gesture_seen_
;
127 std::vector
<blink::WebInputElement
> elements_
;
129 DISALLOW_COPY_AND_ASSIGN(PasswordValueGatekeeper
);
132 // RenderViewObserver:
133 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
134 virtual void DidStartProvisionalLoad(blink::WebLocalFrame
* frame
) OVERRIDE
;
135 virtual void DidStartLoading() OVERRIDE
;
136 virtual void DidFinishDocumentLoad(blink::WebLocalFrame
* frame
) OVERRIDE
;
137 virtual void DidFinishLoad(blink::WebLocalFrame
* frame
) OVERRIDE
;
138 virtual void DidStopLoading() OVERRIDE
;
139 virtual void FrameDetached(blink::WebFrame
* frame
) OVERRIDE
;
140 virtual void FrameWillClose(blink::WebFrame
* frame
) OVERRIDE
;
141 virtual void WillSendSubmitEvent(blink::WebLocalFrame
* frame
,
142 const blink::WebFormElement
& form
) OVERRIDE
;
143 virtual void WillSubmitForm(blink::WebLocalFrame
* frame
,
144 const blink::WebFormElement
& form
) OVERRIDE
;
146 // RenderView IPC handlers:
147 void OnFillPasswordForm(const PasswordFormFillData
& form_data
);
148 void OnSetLoggingState(bool active
);
150 // Scans the given frame for password forms and sends them up to the browser.
151 // If |only_visible| is true, only forms visible in the layout are sent.
152 void SendPasswordForms(blink::WebFrame
* frame
, bool only_visible
);
154 void GetSuggestions(const PasswordFormFillData
& fill_data
,
155 const base::string16
& input
,
156 std::vector
<base::string16
>* suggestions
,
157 std::vector
<base::string16
>* realms
,
160 bool ShowSuggestionPopup(const PasswordFormFillData
& fill_data
,
161 const blink::WebInputElement
& user_input
,
164 // Attempts to fill |username_element| and |password_element| with the
165 // |fill_data|. Will use the data corresponding to the preferred username,
166 // unless the |username_element| already has a value set. In that case,
167 // attempts to fill the password matching the already filled username, if
168 // such a password exists.
169 void FillFormOnPasswordRecieved(const PasswordFormFillData
& fill_data
,
170 blink::WebInputElement username_element
,
171 blink::WebInputElement password_element
);
173 bool FillUserNameAndPassword(blink::WebInputElement
* username_element
,
174 blink::WebInputElement
* password_element
,
175 const PasswordFormFillData
& fill_data
,
176 bool exact_username_match
,
179 // Fills |login_input| and |password| with the most relevant suggestion from
180 // |fill_data| and shows a popup with other suggestions.
181 void PerformInlineAutocomplete(
182 const blink::WebInputElement
& username
,
183 const blink::WebInputElement
& password
,
184 const PasswordFormFillData
& fill_data
);
186 // Invoked when the passed frame is closing. Gives us a chance to clear any
187 // reference we may have to elements in that frame.
188 void FrameClosing(const blink::WebFrame
* frame
);
190 // Finds login information for a |node| that was previously filled.
191 bool FindLoginInfo(const blink::WebNode
& node
,
192 blink::WebInputElement
* found_input
,
193 PasswordInfo
* found_password
);
195 // Clears the preview for the username and password fields, restoring both to
196 // their previous filled state.
197 void ClearPreview(blink::WebInputElement
* username
,
198 blink::WebInputElement
* password
);
200 // If |provisionally_saved_forms_| contains a form for |current_frame| or its
201 // children, return such frame.
202 blink::WebFrame
* CurrentOrChildFrameWithSavedForms(
203 const blink::WebFrame
* current_frame
);
205 // Extracts a PasswordForm from |form| and saves it as
206 // |provisionally_saved_forms_[frame]|, as long as it satisfies |restriction|.
207 void ProvisionallySavePassword(blink::WebLocalFrame
* frame
,
208 const blink::WebFormElement
& form
,
209 ProvisionallySaveRestriction restriction
);
211 // The logins we have filled so far with their associated info.
212 LoginToPasswordInfoMap login_to_password_info_
;
214 // Used for UMA stats.
215 OtherPossibleUsernamesUsage usernames_usage_
;
217 // Pointer to the WebView. Used to access page scale factor.
218 blink::WebView
* web_view_
;
220 // Set if the user might be submitting a password form on the current page,
221 // but the submit may still fail (i.e. doesn't pass JavaScript validation).
222 FrameToPasswordFormMap provisionally_saved_forms_
;
224 PasswordValueGatekeeper gatekeeper_
;
226 // True indicates that user debug information should be logged.
227 bool logging_state_active_
;
229 // True indicates that the username field was autofilled, false otherwise.
230 bool was_username_autofilled_
;
231 // True indicates that the password field was autofilled, false otherwise.
232 bool was_password_autofilled_
;
234 // Records original starting point of username element's selection range
236 int username_selection_start_
;
238 // True indicates that all frames in a page have been rendered.
239 bool did_stop_loading_
;
241 base::WeakPtrFactory
<PasswordAutofillAgent
> weak_ptr_factory_
;
243 DISALLOW_COPY_AND_ASSIGN(PasswordAutofillAgent
);
246 } // namespace autofill
248 #endif // COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_AUTOFILL_AGENT_H_