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_MANAGER_H_
6 #define CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_MANAGER_H_
10 #include "base/callback.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/observer_list.h"
14 #include "base/prefs/pref_member.h"
15 #include "base/stl_util.h"
16 #include "chrome/browser/password_manager/password_form_manager.h"
17 #include "components/autofill/core/common/password_form.h"
18 #include "components/autofill/core/common/password_form_fill_data.h"
19 #include "components/password_manager/core/browser/login_model.h"
21 class PasswordManagerClient
;
22 class PasswordManagerDriver
;
23 class PasswordManagerTest
;
24 class PasswordFormManager
;
25 class PrefRegistrySimple
;
31 namespace user_prefs
{
32 class PrefRegistrySyncable
;
35 // Per-tab password manager. Handles creation and management of UI elements,
36 // receiving password form data from the renderer and managing the password
37 // database through the PasswordStore. The PasswordManager is a LoginModel
38 // for purposes of supporting HTTP authentication dialogs.
39 class PasswordManager
: public LoginModel
{
41 static const char kOtherPossibleUsernamesExperiment
[];
43 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
45 static void RegisterLocalPrefs(PrefRegistrySimple
* registry
);
47 explicit PasswordManager(PasswordManagerClient
* client
);
48 virtual ~PasswordManager();
50 typedef base::Callback
<void(const autofill::PasswordForm
&)>
51 PasswordSubmittedCallback
;
53 // There is no corresponding remove function as currently all of the
54 // owners of these callbacks have sufficient lifetimes so that the callbacks
55 // should always be valid when called.
56 void AddSubmissionCallback(const PasswordSubmittedCallback
& callback
);
58 // Is saving new data for password autofill enabled for the current profile?
59 // For example, saving new data is disabled in Incognito mode, whereas filling
61 bool IsSavingEnabled() const;
63 // Called by a PasswordFormManager when it decides a form can be autofilled
65 virtual void Autofill(const autofill::PasswordForm
& form_for_autofill
,
66 const autofill::PasswordFormMap
& best_matches
,
67 const autofill::PasswordForm
& preferred_match
,
68 bool wait_for_username
) const;
70 // LoginModel implementation.
71 virtual void AddObserver(LoginModelObserver
* observer
) OVERRIDE
;
72 virtual void RemoveObserver(LoginModelObserver
* observer
) OVERRIDE
;
74 // Mark this form as having a generated password.
75 void SetFormHasGeneratedPassword(const autofill::PasswordForm
& form
);
77 // TODO(isherman): This should not be public, but is currently being used by
78 // the LoginPrompt code.
79 // When a form is submitted, we prepare to save the password but wait
80 // until we decide the user has successfully logged in. This is step 1
81 // of 2 (see SavePassword).
82 void ProvisionallySavePassword(const autofill::PasswordForm
& form
);
84 // Should be called when the user navigates the main frame.
85 void DidNavigateMainFrame(bool is_in_page
);
87 // Handles password forms being parsed.
88 void OnPasswordFormsParsed(
89 const std::vector
<autofill::PasswordForm
>& forms
);
91 // Handles password forms being rendered.
92 void OnPasswordFormsRendered(
93 const std::vector
<autofill::PasswordForm
>& visible_forms
);
95 // Handles a password form being submitted.
96 virtual void OnPasswordFormSubmitted(
97 const autofill::PasswordForm
& password_form
);
100 enum ProvisionalSaveFailure
{
104 MATCHING_NOT_COMPLETE
,
111 // Log failure for UMA. Logs additional metrics if the |form_origin|
112 // corresponds to one of the top, explicitly monitored websites.
113 void RecordFailure(ProvisionalSaveFailure failure
,
114 const std::string
& form_origin
);
116 // Possibly set up FieldTrial for testing other possible usernames. This only
117 // happens if there are other_possible_usernames to be shown and the
118 // experiment hasn't already been initialized. We setup the experiment at
119 // such a late time because this experiment will only affect a small number
120 // of users so we want to include a larger fraction of these users than the
122 void PossiblyInitializeUsernamesExperiment(
123 const autofill::PasswordFormMap
& matches
) const;
125 // Returns true if we can show possible usernames to users in cases where
126 // the username for the form is ambigious.
127 bool OtherPossibleUsernamesEnabled() const;
129 // Returns true if the user needs to be prompted before a password can be
130 // saved (instead of automatically saving
131 // the password), based on inspecting the state of
132 // |provisional_save_manager_|.
133 bool ShouldPromptUserToSavePassword() const;
135 // Note about how a PasswordFormManager can transition from
136 // pending_login_managers_ to provisional_save_manager_ and the infobar.
141 // pending_login -- form submit --> provisional_save ___/
142 // ^ | \___ (update DB)
144 // |-----------<------<---------| !new
146 // When a form is "seen" on a page, a PasswordFormManager is created
147 // and stored in this collection until user navigates away from page.
149 ScopedVector
<PasswordFormManager
> pending_login_managers_
;
151 // When the user submits a password/credential, this contains the
152 // PasswordFormManager for the form in question until we deem the login
153 // attempt to have succeeded (as in valid credentials). If it fails, we
154 // send the PasswordFormManager back to the pending_login_managers_ set.
155 // Scoped in case PasswordManager gets deleted (e.g tab closes) between the
156 // time a user submits a login form and gets to the next page.
157 scoped_ptr
<PasswordFormManager
> provisional_save_manager_
;
159 // The embedder-level client. Must outlive this class.
160 PasswordManagerClient
* const client_
;
162 // The platform-level driver. Must outlive this class.
163 PasswordManagerDriver
* const driver_
;
165 // Set to false to disable the password manager (will no longer ask if you
166 // want to save passwords but will continue to fill passwords).
167 BooleanPrefMember password_manager_enabled_
;
169 // Observers to be notified of LoginModel events. This is mutable to allow
170 // notification in const member functions.
171 mutable ObserverList
<LoginModelObserver
> observers_
;
173 // Callbacks to be notified when a password form has been submitted.
174 std::vector
<PasswordSubmittedCallback
> submission_callbacks_
;
176 DISALLOW_COPY_AND_ASSIGN(PasswordManager
);
179 #endif // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_MANAGER_H_