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_CORE_COMMON_PASSWORD_FORM_H__
6 #define COMPONENTS_AUTOFILL_CORE_COMMON_PASSWORD_FORM_H__
12 #include "base/containers/scoped_ptr_map.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/time/time.h"
15 #include "components/autofill/core/common/form_data.h"
20 // The PasswordForm struct encapsulates information about a login form,
21 // which can be an HTML form or a dialog with username/password text fields.
23 // The Web Data database stores saved username/passwords and associated form
24 // metdata using a PasswordForm struct, typically one that was created from
25 // a parsed HTMLFormElement or LoginDialog, but the saved entries could have
26 // also been created by imported data from another browser.
28 // The PasswordManager implements a fuzzy-matching algorithm to compare saved
29 // PasswordForm entries against PasswordForms that were created from a parsed
30 // HTML or dialog form. As one might expect, the more data contained in one
31 // of the saved PasswordForms, the better the job the PasswordManager can do
32 // in matching it against the actual form it was saved on, and autofill
33 // accurately. But it is not always possible, especially when importing from
34 // other browsers with different data models, to copy over all the information
35 // about a particular "saved password entry" to our PasswordForm
38 // The field descriptions in the struct specification below are intended to
39 // describe which fields are not strictly required when adding a saved password
40 // entry to the database and how they can affect the matching process.
43 // Enum to keep track of what information has been sent to the server about
44 // this form regarding password generation.
45 enum GenerationUploadStatus
{
49 // Reserve a few values for future use.
53 // Enum to differentiate between HTML form based authentication, and dialogs
54 // using basic or digest schemes. Default is SCHEME_HTML. Only PasswordForms
55 // of the same Scheme will be matched/autofilled against each other.
61 SCHEME_LAST
= SCHEME_OTHER
64 // During form parsing, Chrome tries to partly understand the type of the form
65 // based on the layout of its fields. The result of this analysis helps to
66 // treat the form correctly once the low-level information is lost by
67 // converting the web form into a PasswordForm. It is only used for observed
68 // HTML forms, not for stored credentials.
70 // Forms which either do not need to be classified, or cannot be classified
73 // Login and signup forms combined in one <form>, to distinguish them from,
74 // e.g., change-password forms.
75 LAYOUT_LOGIN_AND_SIGNUP
,
76 LAYOUT_LAST
= LAYOUT_LOGIN_AND_SIGNUP
79 // The "Realm" for the sign-on. This is scheme, host, port for SCHEME_HTML.
80 // Dialog based forms also contain the HTTP realm. Android based forms will
81 // contain a string of the form "android://<hash of cert>@<package name>"
83 // The signon_realm is effectively the primary key used for retrieving
84 // data from the database, so it must not be empty.
85 std::string signon_realm
;
87 // The original "Realm" for the sign-on (scheme, host, port for SCHEME_HTML,
88 // and contains the HTTP realm for dialog-based forms). This realm is only set
89 // when two PasswordForms are matched when trying to find a login/pass pair
90 // for a site. It is only set to a non-empty value during a match of the
91 // original stored login/pass and the current observed form if all these
92 // statements are true:
93 // 1) The full signon_realm is not the same.
94 // 2) The registry controlled domain is the same. For example; example.com,
95 // m.example.com, foo.login.example.com and www.example.com would all resolve
96 // to example.com since .com is the public suffix.
97 // 3) The scheme is the same.
98 // 4) The port is the same.
99 // For example, if there exists a stored password for http://www.example.com
100 // (where .com is the public suffix) and the observed form is
101 // http://m.example.com, |original_signon_realm| must be set to
102 // http://www.example.com.
103 std::string original_signon_realm
;
105 // An origin URL consists of the scheme, host, port and path; the rest is
106 // stripped. This is the primary data used by the PasswordManager to decide
107 // (in longest matching prefix fashion) whether or not a given PasswordForm
108 // result from the database is a good fit for a particular form on a page.
109 // This should not be empty except for Android based credentials.
110 // TODO(melandory): origin should be renamed in order to be consistent with
111 // GURL definition of origin.
114 // The action target of the form; like |origin| URL consists of the scheme,
115 // host, port and path; the rest is stripped. This is the primary data used by
116 // the PasswordManager for form autofill; that is, the action of the saved
117 // credentials must match the action of the form on the page to be autofilled.
118 // If this is empty / not available, it will result in a "restricted" IE-like
119 // autofill policy, where we wait for the user to type in his username before
120 // autofilling the password. In these cases, after successful login the action
121 // URL will automatically be assigned by the PasswordManager.
123 // When parsing an HTML form, this must always be set.
126 // The name of the submit button used. Optional; only used in scoring
127 // of PasswordForm results from the database to make matches as tight as
130 // When parsing an HTML form, this must always be set.
131 base::string16 submit_element
;
133 // The name of the username input element. Optional (improves scoring).
135 // When parsing an HTML form, this must always be set.
136 base::string16 username_element
;
138 // Whether the |username_element| has an autocomplete=username attribute. This
139 // is only used in parsed HTML forms.
140 bool username_marked_by_site
;
142 // The username. Optional.
144 // When parsing an HTML form, this is typically empty unless the site
145 // has implemented some form of autofill.
146 base::string16 username_value
;
148 // This member is populated in cases where we there are multiple input
149 // elements that could possibly be the username. Used when our heuristics for
150 // determining the username are incorrect. Optional.
152 // When parsing an HTML form, this is typically empty.
153 std::vector
<base::string16
> other_possible_usernames
;
155 // The name of the input element corresponding to the current password.
156 // Optional (improves scoring).
158 // When parsing an HTML form, this will always be set, unless it is a sign-up
159 // form or a change password form that does not ask for the current password.
160 // In these two cases the |new_password_element| will always be set.
161 base::string16 password_element
;
163 // The current password. Must be non-empty for PasswordForm instances that are
164 // meant to be persisted to the password store.
166 // When parsing an HTML form, this is typically empty.
167 base::string16 password_value
;
169 // Whether the password value is the same as specified in the "value"
170 // attribute of the input element. Only used in the renderer.
171 bool password_value_is_default
;
173 // If the form was a sign-up or a change password form, the name of the input
174 // element corresponding to the new password. Optional, and not persisted.
175 base::string16 new_password_element
;
177 // The new password. Optional, and not persisted.
178 base::string16 new_password_value
;
180 // Whether the password value is the same as specified in the "value"
181 // attribute of the input element. Only used in the renderer.
182 bool new_password_value_is_default
;
184 // Whether the |new_password_element| has an autocomplete=new-password
185 // attribute. This is only used in parsed HTML forms.
186 bool new_password_marked_by_site
;
188 // Whether or not this login was saved under an HTTPS session with a valid
189 // SSL cert. We will never match or autofill a PasswordForm where
190 // ssl_valid == true with a PasswordForm where ssl_valid == false. This means
191 // passwords saved under HTTPS will never get autofilled onto an HTTP page.
192 // When importing, this should be set to true if the page URL is HTTPS, thus
193 // giving it "the benefit of the doubt" that the SSL cert was valid when it
194 // was saved. Default to false.
197 // True if this PasswordForm represents the last username/password login the
198 // user selected to log in to the site. If there is only one saved entry for
199 // the site, this will always be true, but when there are multiple entries
200 // the PasswordManager ensures that only one of them has a preferred bit set
201 // to true. Default to false.
203 // When parsing an HTML form, this is not used.
206 // When the login was saved (by chrome).
208 // When parsing an HTML form, this is not used.
209 base::Time date_created
;
211 // When the login was downloaded from the sync server. For local passwords is
214 // When parsing an HTML form, this is not used.
215 base::Time date_synced
;
217 // Tracks if the user opted to never remember passwords for this form. Default
220 // When parsing an HTML form, this is not used.
221 bool blacklisted_by_user
;
223 // Enum to differentiate between manually filled forms and forms with auto
224 // generated passwords.
228 TYPE_LAST
= TYPE_GENERATED
234 // The number of times that this username/password has been used to
235 // authenticate the user.
237 // When parsing an HTML form, this is not used.
240 // Autofill representation of this form. Used to communicate with the
241 // Autofill servers if necessary. Currently this is only used to help
242 // determine forms where we can trigger password generation.
244 // When parsing an HTML form, this is normally set.
247 // What information has been sent to the Autofill server about this form.
248 GenerationUploadStatus generation_upload_status
;
250 // These following fields are set by a website using the Credential Manager
251 // API. They will be empty and remain unused for sites which do not use that
254 // User friendly name to show in the UI.
255 base::string16 display_name
;
257 // The URL of this credential's icon, such as the user's avatar, to display
259 // TODO(msramek): This field was previously named |avatar_url|. It is still
260 // named this way in the password store backends (e.g. the avatar_url column
261 // in the SQL DB of LoginDatabase) and for the purposes of syncing
262 // (i.e in PasswordSpecificsData). Rename these occurrences.
265 // The URL of identity provider used for federated login.
268 // If true, Chrome will not return this credential to a site in response to
269 // 'navigator.credentials.request()' without user interaction.
270 // Once user selects this credential the flag is reseted.
271 bool skip_zero_click
;
273 // The layout as determined during parsing. Default value is LAYOUT_OTHER.
276 // If true, this form was parsed using Autofill predictions.
277 bool was_parsed_using_autofill_predictions
;
279 // TODO(vabr): Remove |is_alive| once http://crbug.com/486931 is fixed.
280 bool is_alive
; // Set on construction, reset on destruction.
282 // Returns true if this match was found using public suffix matching.
283 bool IsPublicSuffixMatch() const;
285 // Return true if we consider this form to be a change password form.
286 // We use only client heuristics, so it could include signup forms.
287 bool IsPossibleChangePasswordForm() const;
289 // Return true if we consider this form to be a change password form
290 // without username field. We use only client heuristics, so it could
291 // include signup forms.
292 bool IsPossibleChangePasswordFormWithoutUsername() const;
294 // Equality operators for testing.
295 bool operator==(const PasswordForm
& form
) const;
296 bool operator!=(const PasswordForm
& form
) const;
302 // True if the unique keys for the forms are the same. The unique key is
303 // (origin, username_element, username_value, password_element, signon_realm).
304 bool ArePasswordFormUniqueKeyEqual(const PasswordForm
& left
,
305 const PasswordForm
& right
);
307 // A comparator for the unique key.
308 struct LessThanUniqueKey
{
309 bool operator()(const PasswordForm
* left
, const PasswordForm
* right
) const;
312 // Map username to PasswordForm* for convenience. See password_form_manager.h.
313 typedef base::ScopedPtrMap
<base::string16
, scoped_ptr
<PasswordForm
>>
316 // Like PasswordFormMap, but with weak (not owned) pointers.
317 typedef std::map
<base::string16
, const PasswordForm
*> ConstPasswordFormMap
;
320 std::ostream
& operator<<(std::ostream
& os
, PasswordForm::Layout layout
);
321 std::ostream
& operator<<(std::ostream
& os
, const PasswordForm
& form
);
322 std::ostream
& operator<<(std::ostream
& os
, PasswordForm
* form
);
324 } // namespace autofill
326 #endif // COMPONENTS_AUTOFILL_CORE_COMMON_PASSWORD_FORM_H__