OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_AUTOFILL_AGENT_H_ | 5 #ifndef COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_AUTOFILL_AGENT_H_ |
6 #define COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_AUTOFILL_AGENT_H_ | 6 #define COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_AUTOFILL_AGENT_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/memory/linked_ptr.h" | 11 #include "base/gtest_prod_util.h" |
| 12 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
13 #include "components/autofill/core/common/password_form_fill_data.h" | 14 #include "components/autofill/core/common/password_form_fill_data.h" |
| 15 #include "content/public/renderer/render_frame_observer.h" |
14 #include "content/public/renderer/render_view_observer.h" | 16 #include "content/public/renderer/render_view_observer.h" |
15 #include "third_party/WebKit/public/web/WebInputElement.h" | 17 #include "third_party/WebKit/public/web/WebInputElement.h" |
16 | 18 |
17 namespace blink { | 19 namespace blink { |
18 class WebInputElement; | 20 class WebInputElement; |
19 class WebKeyboardEvent; | 21 class WebKeyboardEvent; |
20 class WebSecurityOrigin; | 22 class WebSecurityOrigin; |
21 class WebView; | 23 class WebView; |
22 } | 24 } |
23 | 25 |
24 namespace autofill { | 26 namespace autofill { |
25 | 27 |
26 // This class is responsible for filling password forms. | 28 // This class is responsible for filling password forms. |
27 // There is one PasswordAutofillAgent per RenderView. | 29 class PasswordAutofillAgent : public content::RenderFrameObserver { |
28 class PasswordAutofillAgent : public content::RenderViewObserver { | |
29 public: | 30 public: |
30 explicit PasswordAutofillAgent(content::RenderView* render_view); | 31 explicit PasswordAutofillAgent(content::RenderFrame* render_frame); |
31 ~PasswordAutofillAgent() override; | 32 ~PasswordAutofillAgent() override; |
32 | 33 |
33 // WebViewClient editor related calls forwarded by the RenderView. | 34 // WebFrameClient editor related calls forwarded by AutofillAgent. |
34 // If they return true, it indicates the event was consumed and should not | 35 // If they return true, it indicates the event was consumed and should not |
35 // be used for any other autofill activity. | 36 // be used for any other autofill activity. |
36 bool TextFieldDidEndEditing(const blink::WebInputElement& element); | 37 bool TextFieldDidEndEditing(const blink::WebInputElement& element); |
37 bool TextDidChangeInTextField(const blink::WebInputElement& element); | 38 bool TextDidChangeInTextField(const blink::WebInputElement& element); |
38 bool TextFieldHandlingKeyDown(const blink::WebInputElement& element, | 39 bool TextFieldHandlingKeyDown(const blink::WebInputElement& element, |
39 const blink::WebKeyboardEvent& event); | 40 const blink::WebKeyboardEvent& event); |
40 | 41 |
41 // Fills the username and password fields of this form with the given values. | 42 // Fills the username and password fields of this form with the given values. |
42 // Returns true if the fields were filled, false otherwise. | 43 // Returns true if the fields were filled, false otherwise. |
43 bool FillSuggestion(const blink::WebNode& node, | 44 bool FillSuggestion(const blink::WebNode& node, |
44 const blink::WebString& username, | 45 const blink::WebString& username, |
45 const blink::WebString& password); | 46 const blink::WebString& password); |
46 | 47 |
47 // Previews the username and password fields of this form with the given | 48 // Previews the username and password fields of this form with the given |
48 // values. Returns true if the fields were previewed, false otherwise. | 49 // values. Returns true if the fields were previewed, false otherwise. |
49 bool PreviewSuggestion(const blink::WebNode& node, | 50 bool PreviewSuggestion(const blink::WebNode& node, |
50 const blink::WebString& username, | 51 const blink::WebString& username, |
51 const blink::WebString& password); | 52 const blink::WebString& password); |
52 | 53 |
53 // Clears the preview for the username and password fields, restoring both to | 54 // 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 // their previous filled state. Return false if no login information was |
55 // found for the form. | 56 // found for the form. |
56 bool DidClearAutofillSelection(const blink::WebNode& node); | 57 bool DidClearAutofillSelection(const blink::WebNode& node); |
| 58 |
57 // Shows an Autofill popup with username suggestions for |element|. If | 59 // Shows an Autofill popup with username suggestions for |element|. If |
58 // |show_all| is |true|, will show all possible suggestions for that element, | 60 // |show_all| is |true|, will show all possible suggestions for that element, |
59 // otherwise shows suggestions based on current value of |element|. | 61 // otherwise shows suggestions based on current value of |element|. |
60 // Returns true if any suggestions were shown, false otherwise. | 62 // Returns true if any suggestions were shown, false otherwise. |
61 bool ShowSuggestions(const blink::WebInputElement& element, bool show_all); | 63 bool ShowSuggestions(const blink::WebInputElement& element, bool show_all); |
62 | 64 |
63 // Called when new form controls are inserted. | 65 // Called when new form controls are inserted. |
64 void OnDynamicFormsSeen(blink::WebFrame* frame); | 66 void OnDynamicFormsSeen(); |
65 | 67 |
66 // Called when the user first interacts with the page after a load. This is a | 68 // 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 // signal to make autofilled values of password input elements accessible to |
68 // JavaScript. | 70 // JavaScript. |
69 void FirstUserGestureObserved(); | 71 void FirstUserGestureObserved(); |
70 | 72 |
71 protected: | 73 protected: |
72 virtual bool OriginCanAccessPasswordManager( | 74 virtual bool OriginCanAccessPasswordManager( |
73 const blink::WebSecurityOrigin& origin); | 75 const blink::WebSecurityOrigin& origin); |
74 | 76 |
(...skipping 18 matching lines...) Expand all Loading... |
93 PasswordFormFillData fill_data; | 95 PasswordFormFillData fill_data; |
94 bool backspace_pressed_last; | 96 bool backspace_pressed_last; |
95 // The user manually edited the password more recently than the username was | 97 // The user manually edited the password more recently than the username was |
96 // changed. | 98 // changed. |
97 bool password_was_edited_last; | 99 bool password_was_edited_last; |
98 PasswordInfo(); | 100 PasswordInfo(); |
99 }; | 101 }; |
100 typedef std::map<blink::WebElement, PasswordInfo> LoginToPasswordInfoMap; | 102 typedef std::map<blink::WebElement, PasswordInfo> LoginToPasswordInfoMap; |
101 typedef std::map<blink::WebElement, int> LoginToPasswordInfoKeyMap; | 103 typedef std::map<blink::WebElement, int> LoginToPasswordInfoKeyMap; |
102 typedef std::map<blink::WebElement, blink::WebElement> PasswordToLoginMap; | 104 typedef std::map<blink::WebElement, blink::WebElement> PasswordToLoginMap; |
103 typedef std::map<blink::WebFrame*, | |
104 linked_ptr<PasswordForm> > FrameToPasswordFormMap; | |
105 | 105 |
106 // This class keeps track of autofilled password input elements and makes sure | 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 | 107 // the autofilled password value is not accessible to JavaScript code until |
108 // the user interacts with the page. | 108 // the user interacts with the page. |
109 class PasswordValueGatekeeper { | 109 class PasswordValueGatekeeper { |
110 public: | 110 public: |
111 PasswordValueGatekeeper(); | 111 PasswordValueGatekeeper(); |
112 ~PasswordValueGatekeeper(); | 112 ~PasswordValueGatekeeper(); |
113 | 113 |
114 // Call this for every autofilled password field, so that the gatekeeper | 114 // Call this for every autofilled password field, so that the gatekeeper |
(...skipping 10 matching lines...) Expand all Loading... |
125 private: | 125 private: |
126 // Make the value of |element| accessible to JavaScript code. | 126 // Make the value of |element| accessible to JavaScript code. |
127 void ShowValue(blink::WebInputElement* element); | 127 void ShowValue(blink::WebInputElement* element); |
128 | 128 |
129 bool was_user_gesture_seen_; | 129 bool was_user_gesture_seen_; |
130 std::vector<blink::WebInputElement> elements_; | 130 std::vector<blink::WebInputElement> elements_; |
131 | 131 |
132 DISALLOW_COPY_AND_ASSIGN(PasswordValueGatekeeper); | 132 DISALLOW_COPY_AND_ASSIGN(PasswordValueGatekeeper); |
133 }; | 133 }; |
134 | 134 |
135 // RenderViewObserver: | 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 { |
| 139 public: |
| 140 LegacyPasswordAutofillAgent(content::RenderView* render_view, |
| 141 PasswordAutofillAgent* agent); |
| 142 ~LegacyPasswordAutofillAgent() override; |
| 143 |
| 144 // RenderViewObserver: |
| 145 void OnDestruct() override; |
| 146 void DidStartLoading() override; |
| 147 void DidStopLoading() override; |
| 148 void DidStartProvisionalLoad(blink::WebLocalFrame* frame) override; |
| 149 void FrameDetached(blink::WebFrame* frame) override; |
| 150 void WillSendSubmitEvent(blink::WebLocalFrame* frame, |
| 151 const blink::WebFormElement& form) override; |
| 152 void WillSubmitForm(blink::WebLocalFrame* frame, |
| 153 const blink::WebFormElement& form) override; |
| 154 |
| 155 private: |
| 156 PasswordAutofillAgent* agent_; |
| 157 |
| 158 DISALLOW_COPY_AND_ASSIGN(LegacyPasswordAutofillAgent); |
| 159 }; |
| 160 friend class LegacyPasswordAutofillAgent; |
| 161 FRIEND_TEST_ALL_PREFIXES( |
| 162 PasswordAutofillAgentTest, |
| 163 RememberLastNonEmptyUsernameAndPasswordOnSubmit_ScriptCleared); |
| 164 FRIEND_TEST_ALL_PREFIXES( |
| 165 PasswordAutofillAgentTest, |
| 166 RememberLastNonEmptyUsernameAndPasswordOnSubmit_UserCleared); |
| 167 FRIEND_TEST_ALL_PREFIXES( |
| 168 PasswordAutofillAgentTest, |
| 169 RememberLastNonEmptyUsernameAndPasswordOnSubmit_New); |
| 170 |
| 171 // RenderFrameObserver: |
136 bool OnMessageReceived(const IPC::Message& message) override; | 172 bool OnMessageReceived(const IPC::Message& message) override; |
137 void DidStartProvisionalLoad(blink::WebLocalFrame* frame) override; | 173 void DidFinishDocumentLoad() override; |
138 void DidStartLoading() override; | 174 void DidFinishLoad() override; |
139 void DidFinishDocumentLoad(blink::WebLocalFrame* frame) override; | 175 void FrameWillClose() override; |
140 void DidFinishLoad(blink::WebLocalFrame* frame) override; | 176 |
141 void DidStopLoading() override; | 177 // Legacy RenderViewObserver: |
142 void FrameDetached(blink::WebFrame* frame) override; | 178 void DidStartLoading(); |
143 void FrameWillClose(blink::WebFrame* frame) override; | 179 void DidStopLoading(); |
| 180 void LegacyDidStartProvisionalLoad(blink::WebLocalFrame* frame); |
| 181 void FrameDetached(blink::WebFrame* frame); |
144 void WillSendSubmitEvent(blink::WebLocalFrame* frame, | 182 void WillSendSubmitEvent(blink::WebLocalFrame* frame, |
145 const blink::WebFormElement& form) override; | 183 const blink::WebFormElement& form); |
146 void WillSubmitForm(blink::WebLocalFrame* frame, | 184 void WillSubmitForm(blink::WebLocalFrame* frame, |
147 const blink::WebFormElement& form) override; | 185 const blink::WebFormElement& form); |
148 | 186 |
149 // RenderView IPC handlers: | 187 // RenderView IPC handlers: |
150 void OnFillPasswordForm(int key, const PasswordFormFillData& form_data); | 188 void OnFillPasswordForm(int key, const PasswordFormFillData& form_data); |
151 void OnSetLoggingState(bool active); | 189 void OnSetLoggingState(bool active); |
152 | 190 |
153 // Scans the given frame for password forms and sends them up to the browser. | 191 // Scans the given frame for password forms and sends them up to the browser. |
154 // If |only_visible| is true, only forms visible in the layout are sent. | 192 // If |only_visible| is true, only forms visible in the layout are sent. |
155 void SendPasswordForms(blink::WebFrame* frame, bool only_visible); | 193 void SendPasswordForms(bool only_visible); |
156 | 194 |
157 bool ShowSuggestionPopup(const PasswordFormFillData& fill_data, | 195 bool ShowSuggestionPopup(const PasswordFormFillData& fill_data, |
158 const blink::WebInputElement& user_input, | 196 const blink::WebInputElement& user_input, |
159 bool show_all); | 197 bool show_all); |
160 | 198 |
161 // Fills |login_input| and |password| with the most relevant suggestion from | 199 // Fills |login_input| and |password| with the most relevant suggestion from |
162 // |fill_data| and shows a popup with other suggestions. | 200 // |fill_data| and shows a popup with other suggestions. |
163 void PerformInlineAutocomplete( | 201 void PerformInlineAutocomplete( |
164 const blink::WebInputElement& username, | 202 const blink::WebInputElement& username, |
165 const blink::WebInputElement& password, | 203 const blink::WebInputElement& password, |
166 const PasswordFormFillData& fill_data); | 204 const PasswordFormFillData& fill_data); |
167 | 205 |
168 // Invoked when the passed frame is closing. Gives us a chance to clear any | 206 // Invoked when the frame is closing. |
169 // reference we may have to elements in that frame. | 207 void FrameClosing(); |
170 void FrameClosing(const blink::WebFrame* frame); | |
171 | 208 |
172 // Finds login information for a |node| that was previously filled. | 209 // Finds login information for a |node| that was previously filled. |
173 bool FindLoginInfo(const blink::WebNode& node, | 210 bool FindLoginInfo(const blink::WebNode& node, |
174 blink::WebInputElement* found_input, | 211 blink::WebInputElement* found_input, |
175 PasswordInfo** found_password); | 212 PasswordInfo** found_password); |
176 | 213 |
177 // Clears the preview for the username and password fields, restoring both to | 214 // Clears the preview for the username and password fields, restoring both to |
178 // their previous filled state. | 215 // their previous filled state. |
179 void ClearPreview(blink::WebInputElement* username, | 216 void ClearPreview(blink::WebInputElement* username, |
180 blink::WebInputElement* password); | 217 blink::WebInputElement* password); |
181 | 218 |
182 // If |provisionally_saved_forms_| contains a form for |current_frame| or its | 219 // Extracts a PasswordForm from |form| and saves it as |
183 // children, return such frame. | 220 // |provisionally_saved_form_|, as long as it satisfies |restriction|. |
184 blink::WebFrame* CurrentOrChildFrameWithSavedForms( | 221 void ProvisionallySavePassword(const blink::WebFormElement& form, |
185 const blink::WebFrame* current_frame); | 222 ProvisionallySaveRestriction restriction); |
186 | 223 |
187 // Extracts a PasswordForm from |form| and saves it as | 224 // Passes through |RenderViewObserver| method to |this|. |
188 // |provisionally_saved_forms_[frame]|, as long as it satisfies |restriction|. | 225 LegacyPasswordAutofillAgent legacy_; |
189 void ProvisionallySavePassword(blink::WebLocalFrame* frame, | |
190 const blink::WebFormElement& form, | |
191 ProvisionallySaveRestriction restriction); | |
192 | 226 |
193 // The logins we have filled so far with their associated info. | 227 // The logins we have filled so far with their associated info. |
194 LoginToPasswordInfoMap login_to_password_info_; | 228 LoginToPasswordInfoMap login_to_password_info_; |
195 // And the keys under which PasswordAutofillManager can find the same info. | 229 // And the keys under which PasswordAutofillManager can find the same info. |
196 LoginToPasswordInfoKeyMap login_to_password_info_key_; | 230 LoginToPasswordInfoKeyMap login_to_password_info_key_; |
197 // A (sort-of) reverse map to |login_to_password_info_|. | 231 // A (sort-of) reverse map to |login_to_password_info_|. |
198 PasswordToLoginMap password_to_username_; | 232 PasswordToLoginMap password_to_username_; |
199 | 233 |
200 // Used for UMA stats. | 234 // Used for UMA stats. |
201 OtherPossibleUsernamesUsage usernames_usage_; | 235 OtherPossibleUsernamesUsage usernames_usage_; |
202 | 236 |
203 // Pointer to the WebView. Used to access page scale factor. | 237 // Pointer to the WebView. Used to access page scale factor. |
204 blink::WebView* web_view_; | 238 blink::WebView* web_view_; |
205 | 239 |
206 // Set if the user might be submitting a password form on the current page, | 240 // Set if the user might be submitting a password form on the current page, |
207 // but the submit may still fail (i.e. doesn't pass JavaScript validation). | 241 // but the submit may still fail (i.e. doesn't pass JavaScript validation). |
208 FrameToPasswordFormMap provisionally_saved_forms_; | 242 scoped_ptr<PasswordForm> provisionally_saved_form_; |
209 | 243 |
210 PasswordValueGatekeeper gatekeeper_; | 244 PasswordValueGatekeeper gatekeeper_; |
211 | 245 |
212 // True indicates that user debug information should be logged. | 246 // True indicates that user debug information should be logged. |
213 bool logging_state_active_; | 247 bool logging_state_active_; |
214 | 248 |
215 // True indicates that the username field was autofilled, false otherwise. | 249 // True indicates that the username field was autofilled, false otherwise. |
216 bool was_username_autofilled_; | 250 bool was_username_autofilled_; |
217 // True indicates that the password field was autofilled, false otherwise. | 251 // True indicates that the password field was autofilled, false otherwise. |
218 bool was_password_autofilled_; | 252 bool was_password_autofilled_; |
219 | 253 |
220 // Records original starting point of username element's selection range | 254 // Records original starting point of username element's selection range |
221 // before preview. | 255 // before preview. |
222 int username_selection_start_; | 256 int username_selection_start_; |
223 | 257 |
224 // True indicates that all frames in a page have been rendered. | 258 // True indicates that all frames in a page have been rendered. |
225 bool did_stop_loading_; | 259 bool did_stop_loading_; |
226 | 260 |
227 base::WeakPtrFactory<PasswordAutofillAgent> weak_ptr_factory_; | 261 base::WeakPtrFactory<PasswordAutofillAgent> weak_ptr_factory_; |
228 | 262 |
229 DISALLOW_COPY_AND_ASSIGN(PasswordAutofillAgent); | 263 DISALLOW_COPY_AND_ASSIGN(PasswordAutofillAgent); |
230 }; | 264 }; |
231 | 265 |
232 } // namespace autofill | 266 } // namespace autofill |
233 | 267 |
234 #endif // COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_AUTOFILL_AGENT_H_ | 268 #endif // COMPONENTS_AUTOFILL_CONTENT_RENDERER_PASSWORD_AUTOFILL_AGENT_H_ |
OLD | NEW |