1 // Copyright (c) 2012 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_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_FORM_MANAGER_H_
6 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_FORM_MANAGER_H_
11 #include "build/build_config.h"
13 #include "base/stl_util.h"
14 #include "components/autofill/core/common/password_form.h"
15 #include "components/password_manager/core/browser/password_manager_driver.h"
16 #include "components/password_manager/core/browser/password_store.h"
17 #include "components/password_manager/core/browser/password_store_consumer.h"
21 } // namespace content
23 namespace password_manager {
25 class PasswordManager;
26 class PasswordManagerClient;
28 // Per-password-form-{on-page, dialog} class responsible for interactions
29 // between a given form, the per-tab PasswordManager, and the PasswordStore.
30 class PasswordFormManager : public PasswordStoreConsumer {
32 // |password_manager| owns this object
33 // |form_on_page| is the form that may be submitted and could need login data.
34 // |ssl_valid| represents the security of the page containing observed_form,
35 // used to filter login results from database.
36 PasswordFormManager(PasswordManager* password_manager,
37 PasswordManagerClient* client,
38 PasswordManagerDriver* driver,
39 const autofill::PasswordForm& observed_form,
41 virtual ~PasswordFormManager();
43 // Flags describing the result of comparing two forms as performed by
44 // DoesMatch. Individual flags are only relevant for HTML forms, but
45 // RESULT_COMPLETE_MATCH will also be returned to indicate non-HTML forms
46 // completely matching.
47 enum MatchResultFlags {
49 RESULT_MANDATORY_ATTRIBUTES_MATCH = 1 << 0, // Bare minimum to be a match.
50 RESULT_ACTION_MATCH = 1 << 1, // Action URLs match too.
51 RESULT_COMPLETE_MATCH =
52 RESULT_MANDATORY_ATTRIBUTES_MATCH | RESULT_ACTION_MATCH
54 // Use MatchResultMask to contain combinations of MatchResultFlags values.
55 // It's a signed int rather than unsigned to avoid signed/unsigned mismatch
56 // caused by the enum values implicitly converting to signed int.
57 typedef int MatchResultMask;
59 enum OtherPossibleUsernamesAction {
60 ALLOW_OTHER_POSSIBLE_USERNAMES,
61 IGNORE_OTHER_POSSIBLE_USERNAMES
64 // Compares basic data of |observed_form_| with |form| and returns how much
65 // they match. The return value is a MatchResultMask bitmask.
66 MatchResultMask DoesManage(const autofill::PasswordForm& form) const;
68 // Retrieves potential matching logins from the database.
69 // |prompt_policy| indicates whether it's permissible to prompt the user to
70 // authorize access to locked passwords. This argument is only used on
71 // platforms that support prompting the user for access (such as Mac OS).
72 void FetchMatchingLoginsFromPasswordStore(
73 PasswordStore::AuthorizationPromptPolicy prompt_policy);
75 // Simple state-check to verify whether this object as received a callback
76 // from the PasswordStore and completed its matching phase. Note that the
77 // callback in question occurs on the same (and only) main thread from which
78 // instances of this class are ever used, but it is required since it is
79 // conceivable that a user (or ui test) could attempt to submit a login
80 // prompt before the callback has occured, which would InvokeLater a call to
81 // PasswordManager::ProvisionallySave, which would interact with this object
82 // before the db has had time to answer with matching password entries.
83 // This is intended to be a one-time check; if the return value is false the
84 // expectation is caller will give up. This clearly won't work if you put it
85 // in a loop and wait for matching to complete; you're (supposed to be) on
87 bool HasCompletedMatching();
89 // Determines if the user opted to 'never remember' passwords for this form.
92 // Used by PasswordManager to determine whether or not to display
93 // a SavePasswordBar when given the green light to save the PasswordForm
97 // Returns true if the current pending credentials were found using
98 // origin matching of the public suffix, instead of the signon realm of the
100 bool IsPendingCredentialsPublicSuffixMatch();
102 // Checks if the form is a valid password form. Forms which lack either
103 // login or password field are not considered valid.
104 bool HasValidPasswordForm();
106 // These functions are used to determine if this form has had it's password
107 // auto generated by the browser.
108 bool HasGeneratedPassword();
109 void SetHasGeneratedPassword();
111 // Determines if we need to autofill given the results of the query.
112 // Takes ownership of the elements in |result|.
113 void OnRequestDone(const std::vector<autofill::PasswordForm*>& result);
115 virtual void OnGetPasswordStoreResults(
116 const std::vector<autofill::PasswordForm*>& results) OVERRIDE;
118 // A user opted to 'never remember' passwords for this form.
119 // Blacklist it so that from now on when it is seen we ignore it.
120 // TODO: Make this private once we switch to the new UI.
121 void PermanentlyBlacklist();
123 // Sets whether the password form should use additional password
124 // authentication if available before being used for autofill.
125 void SetUseAdditionalPasswordAuthentication(
126 bool use_additional_authentication);
128 // If the user has submitted observed_form_, provisionally hold on to
129 // the submitted credentials until we are told by PasswordManager whether
130 // or not the login was successful. |action| describes how we deal with
131 // possible usernames. If |action| is ALLOW_OTHER_POSSIBLE_USERNAMES we will
132 // treat a possible usernames match as a sign that our original heuristics
133 // were wrong and that the user selected the correct username from the
135 void ProvisionallySave(const autofill::PasswordForm& credentials,
136 OtherPossibleUsernamesAction action);
138 // Handles save-as-new or update of the form managed by this manager.
139 // Note the basic data of updated_credentials must match that of
140 // observed_form_ (e.g DoesManage(pending_credentials_) == true).
141 // TODO: Make this private once we switch to the new UI.
144 // Call these if/when we know the form submission worked or failed.
145 // These routines are used to update internal statistics ("ActionsTaken").
149 // Returns the username associated with the credentials.
150 const base::string16& associated_username() const {
151 return pending_credentials_.username_value;
154 // Returns the pending credentials.
155 const autofill::PasswordForm& pending_credentials() const {
156 return pending_credentials_;
159 // Returns the best matches.
160 const autofill::PasswordFormMap& best_matches() const {
161 return best_matches_;
164 const autofill::PasswordForm* preferred_match() const {
165 return preferred_match_;
168 // Returns the realm URL for the form managed my this manager.
169 const std::string& realm() const {
170 return pending_credentials_.signon_realm;
174 friend class PasswordFormManagerTest;
176 // ManagerAction - What does the manager do with this form? Either it
177 // fills it, or it doesn't. If it doesn't fill it, that's either
178 // because it has no match, or it is blacklisted, or it is disabled
179 // via the AUTOCOMPLETE=off attribute. Note that if we don't have
180 // an exact match, we still provide candidates that the user may
183 kManagerActionNone = 0,
184 kManagerActionAutofilled,
185 kManagerActionBlacklisted,
189 // UserAction - What does the user do with this form? If he or she
190 // does nothing (either by accepting what the password manager did, or
191 // by simply (not typing anything at all), you get None. If there were
192 // multiple choices and the user selects one other than the default,
193 // you get Choose, if user selects an entry from matching against the Public
194 // Suffix List you get ChoosePslMatch, if the user types in a new value
195 // for just the password you get OverridePassword, and if the user types in a
196 // new value for the username and password you get
197 // OverrideUsernameAndPassword.
201 kUserActionChoosePslMatch,
202 kUserActionOverridePassword,
203 kUserActionOverrideUsernameAndPassword,
207 // Result - What happens to the form?
209 kSubmitResultNotSubmitted = 0,
215 // The maximum number of combinations of the three preceding enums.
216 // This is used when recording the actions taken by the form in UMA.
217 static const int kMaxNumActionsTaken = kManagerActionMax * kUserActionMax *
220 // Helper for OnGetPasswordStoreResults to determine whether or not
221 // the given result form is worth scoring.
222 bool ShouldIgnoreResult(const autofill::PasswordForm& form) const;
224 // Helper for Save in the case that best_matches.size() == 0, meaning
225 // we have no prior record of this form/username/password and the user
226 // has opted to 'Save Password'. If |reset_preferred_login| is set,
227 // the previously preferred login from |best_matches_| will be reset.
228 void SaveAsNewLogin(bool reset_preferred_login);
230 // Helper for OnGetPasswordStoreResults to score an individual result
231 // against the observed_form_.
232 int ScoreResult(const autofill::PasswordForm& form) const;
234 // Helper for Save in the case that best_matches.size() > 0, meaning
235 // we have at least one match for this form/username/password. This
236 // Updates the form managed by this object, as well as any matching forms
237 // that now need to have preferred bit changed, since updated_credentials
238 // is now implicitly 'preferred'.
241 // Check to see if |pending| corresponds to an account creation form. If we
242 // think that it does, we label it as such and upload this state to the
243 // Autofill server, so that we will trigger password generation in the future.
244 void CheckForAccountCreationForm(const autofill::PasswordForm& pending,
245 const autofill::PasswordForm& observed);
247 // Update all login matches to reflect new preferred state - preferred flag
248 // will be reset on all matched logins that different than the current
249 // |pending_credentials_|.
250 void UpdatePreferredLoginState(PasswordStore* password_store);
252 // Returns true if |username| is one of the other possible usernames for a
253 // password form in |best_matches_| and sets |pending_credentials_| to the
254 // match which had this username.
255 bool UpdatePendingCredentialsIfOtherPossibleUsername(
256 const base::string16& username);
258 // Converts the "ActionsTaken" fields into an int so they can be logged to
260 int GetActionsTaken();
262 // Remove possible_usernames that may contains sensitive information and
264 void SanitizePossibleUsernames(autofill::PasswordForm* form);
266 // Set of PasswordForms from the DB that best match the form
267 // being managed by this. Use a map instead of vector, because we most
268 // frequently require lookups by username value in IsNewLogin.
269 autofill::PasswordFormMap best_matches_;
271 // Cleans up when best_matches_ goes out of scope.
272 STLValueDeleter<autofill::PasswordFormMap> best_matches_deleter_;
274 // The PasswordForm from the page or dialog managed by |this|.
275 const autofill::PasswordForm observed_form_;
277 // The origin url path of observed_form_ tokenized, for convenience when
279 std::vector<std::string> form_path_tokens_;
281 // Stores updated credentials when the form was submitted but success is
283 autofill::PasswordForm pending_credentials_;
285 // Whether pending_credentials_ stores a new login or is an update
286 // to an existing one.
289 // Whether this form has an auto generated password.
290 bool has_generated_password_;
292 // Set if the user has selected one of the other possible usernames in
293 // |pending_credentials_|.
294 base::string16 selected_username_;
296 // PasswordManager owning this.
297 const PasswordManager* const password_manager_;
299 // Convenience pointer to entry in best_matches_ that is marked
300 // as preferred. This is only allowed to be null if there are no best matches
301 // at all, since there will always be one preferred login when there are
302 // multiple matches (when first saved, a login is marked preferred).
303 const autofill::PasswordForm* preferred_match_;
306 PRE_MATCHING_PHASE, // Have not yet invoked a GetLogins query to find
307 // matching login information from password store.
308 MATCHING_PHASE, // We've made a GetLogins request, but
309 // haven't received or finished processing result.
310 POST_MATCHING_PHASE // We've queried the DB and processed matching
312 } PasswordFormManagerState;
314 // State of matching process, used to verify that we don't call methods
315 // assuming we've already processed the request for matching logins,
316 // when we actually haven't.
317 PasswordFormManagerState state_;
319 // The client which implements embedder-specific PasswordManager operations.
320 PasswordManagerClient* client_;
322 // The driver which implements platform-specific PasswordManager operations.
323 PasswordManagerDriver* driver_;
325 // These three fields record the "ActionsTaken" by the browser and
326 // the user with this form, and the result. They are combined and
327 // recorded in UMA when the manager is destroyed.
328 ManagerAction manager_action_;
329 UserAction user_action_;
330 SubmitResult submit_result_;
332 DISALLOW_COPY_AND_ASSIGN(PasswordFormManager);
335 } // namespace password_manager
337 #endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_FORM_MANAGER_H_