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 CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_FORM_MANAGER_H_
6 #define CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_FORM_MANAGER_H_
11 #include "build/build_config.h"
13 #include "base/stl_util.h"
14 #include "chrome/browser/password_manager/password_manager_driver.h"
15 #include "components/autofill/core/common/password_form.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 class PasswordManager
;
24 class PasswordManagerClient
;
26 // Per-password-form-{on-page, dialog} class responsible for interactions
27 // between a given form, the per-tab PasswordManager, and the PasswordStore.
28 class PasswordFormManager
: public PasswordStoreConsumer
{
30 // |password_manager| owns this object
31 // |form_on_page| is the form that may be submitted and could need login data.
32 // |ssl_valid| represents the security of the page containing observed_form,
33 // used to filter login results from database.
34 PasswordFormManager(PasswordManager
* password_manager
,
35 PasswordManagerClient
* client
,
36 PasswordManagerDriver
* driver
,
37 const autofill::PasswordForm
& observed_form
,
39 virtual ~PasswordFormManager();
42 ACTION_MATCH_REQUIRED
,
43 ACTION_MATCH_NOT_REQUIRED
46 enum OtherPossibleUsernamesAction
{
47 ALLOW_OTHER_POSSIBLE_USERNAMES
,
48 IGNORE_OTHER_POSSIBLE_USERNAMES
51 // Compare basic data of observed_form_ with argument. Only check the action
52 // URL when action match is required.
53 bool DoesManage(const autofill::PasswordForm
& form
,
54 ActionMatch action_match
) const;
56 // Retrieves potential matching logins from the database.
57 // |prompt_policy| indicates whether it's permissible to prompt the user to
58 // authorize access to locked passwords. This argument is only used on
59 // platforms that support prompting the user for access (such as Mac OS).
60 void FetchMatchingLoginsFromPasswordStore(
61 PasswordStore::AuthorizationPromptPolicy prompt_policy
);
63 // Simple state-check to verify whether this object as received a callback
64 // from the PasswordStore and completed its matching phase. Note that the
65 // callback in question occurs on the same (and only) main thread from which
66 // instances of this class are ever used, but it is required since it is
67 // conceivable that a user (or ui test) could attempt to submit a login
68 // prompt before the callback has occured, which would InvokeLater a call to
69 // PasswordManager::ProvisionallySave, which would interact with this object
70 // before the db has had time to answer with matching password entries.
71 // This is intended to be a one-time check; if the return value is false the
72 // expectation is caller will give up. This clearly won't work if you put it
73 // in a loop and wait for matching to complete; you're (supposed to be) on
75 bool HasCompletedMatching();
77 // Determines if the user opted to 'never remember' passwords for this form.
80 // Used by PasswordManager to determine whether or not to display
81 // a SavePasswordBar when given the green light to save the PasswordForm
85 // Returns true if the current pending credentials were found using
86 // origin matching of the public suffix, instead of the signon realm of the
88 bool IsPendingCredentialsPublicSuffixMatch();
90 // Checks if the form is a valid password form. Forms which lack either
91 // login or password field are not considered valid.
92 bool HasValidPasswordForm();
94 // These functions are used to determine if this form has had it's password
95 // auto generated by the browser.
96 bool HasGeneratedPassword();
97 void SetHasGeneratedPassword();
99 // Determines if we need to autofill given the results of the query.
100 // Takes ownership of the elements in |result|.
101 void OnRequestDone(const std::vector
<autofill::PasswordForm
*>& result
);
103 virtual void OnGetPasswordStoreResults(
104 const std::vector
<autofill::PasswordForm
*>& results
) OVERRIDE
;
106 // A user opted to 'never remember' passwords for this form.
107 // Blacklist it so that from now on when it is seen we ignore it.
108 // TODO: Make this private once we switch to the new UI.
109 void PermanentlyBlacklist();
111 // Sets whether the password form should use additional password
112 // authentication if available before being used for autofill.
113 void SetUseAdditionalPasswordAuthentication(
114 bool use_additional_authentication
);
116 // If the user has submitted observed_form_, provisionally hold on to
117 // the submitted credentials until we are told by PasswordManager whether
118 // or not the login was successful. |action| describes how we deal with
119 // possible usernames. If |action| is ALLOW_OTHER_POSSIBLE_USERNAMES we will
120 // treat a possible usernames match as a sign that our original heuristics
121 // were wrong and that the user selected the correct username from the
123 void ProvisionallySave(const autofill::PasswordForm
& credentials
,
124 OtherPossibleUsernamesAction action
);
126 // Handles save-as-new or update of the form managed by this manager.
127 // Note the basic data of updated_credentials must match that of
128 // observed_form_ (e.g DoesManage(pending_credentials_) == true).
129 // TODO: Make this private once we switch to the new UI.
132 // Call these if/when we know the form submission worked or failed.
133 // These routines are used to update internal statistics ("ActionsTaken").
137 // Returns the username associated with the credentials.
138 const base::string16
& associated_username() const {
139 return pending_credentials_
.username_value
;
142 // Returns the pending credentials.
143 const autofill::PasswordForm
pending_credentials() const {
144 return pending_credentials_
;
147 // Returns the best matches.
148 const autofill::PasswordFormMap
best_matches() const {
149 return best_matches_
;
152 // Returns the realm URL for the form managed my this manager.
153 const std::string
& realm() const {
154 return pending_credentials_
.signon_realm
;
158 friend class PasswordFormManagerTest
;
160 // ManagerAction - What does the manager do with this form? Either it
161 // fills it, or it doesn't. If it doesn't fill it, that's either
162 // because it has no match, or it is blacklisted, or it is disabled
163 // via the AUTOCOMPLETE=off attribute. Note that if we don't have
164 // an exact match, we still provide candidates that the user may
167 kManagerActionNone
= 0,
168 kManagerActionAutofilled
,
169 kManagerActionBlacklisted
,
173 // UserAction - What does the user do with this form? If he or she
174 // does nothing (either by accepting what the password manager did, or
175 // by simply (not typing anything at all), you get None. If there were
176 // multiple choices and the user selects one other than the default,
177 // you get Choose, if user selects an entry from matching against the Public
178 // Suffix List you get ChoosePslMatch, and if the user types in a new value,
183 kUserActionChoosePslMatch
,
188 // Result - What happens to the form?
190 kSubmitResultNotSubmitted
= 0,
196 // The maximum number of combinations of the three preceding enums.
197 // This is used when recording the actions taken by the form in UMA.
198 static const int kMaxNumActionsTaken
= kManagerActionMax
* kUserActionMax
*
201 // Helper for OnGetPasswordStoreResults to determine whether or not
202 // the given result form is worth scoring.
203 bool IgnoreResult(const autofill::PasswordForm
& form
) const;
205 // Helper for Save in the case that best_matches.size() == 0, meaning
206 // we have no prior record of this form/username/password and the user
207 // has opted to 'Save Password'. If |reset_preferred_login| is set,
208 // the previously preferred login from |best_matches_| will be reset.
209 void SaveAsNewLogin(bool reset_preferred_login
);
211 // Helper for OnGetPasswordStoreResults to score an individual result
212 // against the observed_form_.
213 int ScoreResult(const autofill::PasswordForm
& form
) const;
215 // Helper for Save in the case that best_matches.size() > 0, meaning
216 // we have at least one match for this form/username/password. This
217 // Updates the form managed by this object, as well as any matching forms
218 // that now need to have preferred bit changed, since updated_credentials
219 // is now implicitly 'preferred'.
222 // Check to see if |pending| corresponds to an account creation form. If we
223 // think that it does, we label it as such and upload this state to the
224 // Autofill server, so that we will trigger password generation in the future.
225 void CheckForAccountCreationForm(const autofill::PasswordForm
& pending
,
226 const autofill::PasswordForm
& observed
);
228 // Update all login matches to reflect new preferred state - preferred flag
229 // will be reset on all matched logins that different than the current
230 // |pending_credentials_|.
231 void UpdatePreferredLoginState(PasswordStore
* password_store
);
233 // Returns true if |username| is one of the other possible usernames for a
234 // password form in |best_matches_| and sets |pending_credentials_| to the
235 // match which had this username.
236 bool UpdatePendingCredentialsIfOtherPossibleUsername(
237 const base::string16
& username
);
239 // Converts the "ActionsTaken" fields into an int so they can be logged to
241 int GetActionsTaken();
243 // Remove possible_usernames that may contains sensitive information and
245 void SanitizePossibleUsernames(autofill::PasswordForm
* form
);
247 // Set of PasswordForms from the DB that best match the form
248 // being managed by this. Use a map instead of vector, because we most
249 // frequently require lookups by username value in IsNewLogin.
250 autofill::PasswordFormMap best_matches_
;
252 // Cleans up when best_matches_ goes out of scope.
253 STLValueDeleter
<autofill::PasswordFormMap
> best_matches_deleter_
;
255 // The PasswordForm from the page or dialog managed by this.
256 autofill::PasswordForm observed_form_
;
258 // The origin url path of observed_form_ tokenized, for convenience when
260 std::vector
<std::string
> form_path_tokens_
;
262 // Stores updated credentials when the form was submitted but success is
264 autofill::PasswordForm pending_credentials_
;
266 // Whether pending_credentials_ stores a new login or is an update
267 // to an existing one.
270 // Whether this form has an auto generated password.
271 bool has_generated_password_
;
273 // Set if the user has selected one of the other possible usernames in
274 // |pending_credentials_|.
275 base::string16 selected_username_
;
277 // PasswordManager owning this.
278 const PasswordManager
* const password_manager_
;
280 // Convenience pointer to entry in best_matches_ that is marked
281 // as preferred. This is only allowed to be null if there are no best matches
282 // at all, since there will always be one preferred login when there are
283 // multiple matches (when first saved, a login is marked preferred).
284 const autofill::PasswordForm
* preferred_match_
;
287 PRE_MATCHING_PHASE
, // Have not yet invoked a GetLogins query to find
288 // matching login information from password store.
289 MATCHING_PHASE
, // We've made a GetLogins request, but
290 // haven't received or finished processing result.
291 POST_MATCHING_PHASE
// We've queried the DB and processed matching
293 } PasswordFormManagerState
;
295 // State of matching process, used to verify that we don't call methods
296 // assuming we've already processed the request for matching logins,
297 // when we actually haven't.
298 PasswordFormManagerState state_
;
300 // The client which implements embedder-specific PasswordManager operations.
301 PasswordManagerClient
* client_
;
303 // The driver which implements platform-specific PasswordManager operations.
304 PasswordManagerDriver
* driver_
;
306 // These three fields record the "ActionsTaken" by the browser and
307 // the user with this form, and the result. They are combined and
308 // recorded in UMA when the manager is destroyed.
309 ManagerAction manager_action_
;
310 UserAction user_action_
;
311 SubmitResult submit_result_
;
313 DISALLOW_COPY_AND_ASSIGN(PasswordFormManager
);
315 #endif // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_FORM_MANAGER_H_