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 "chrome/browser/ui/login/login_model.h"
18 #include "components/autofill/core/common/password_form.h"
19 #include "components/autofill/core/common/password_form_fill_data.h"
20 #include "content/public/browser/web_contents_observer.h"
21 #include "content/public/browser/web_contents_user_data.h"
23 class PasswordManagerDelegate;
24 class PasswordManagerTest;
25 class PasswordFormManager;
27 namespace user_prefs {
28 class PrefRegistrySyncable;
31 // Per-tab password manager. Handles creation and management of UI elements,
32 // receiving password form data from the renderer and managing the password
33 // database through the PasswordStore. The PasswordManager is a LoginModel
34 // for purposes of supporting HTTP authentication dialogs.
35 class PasswordManager : public LoginModel,
36 public content::WebContentsObserver,
37 public content::WebContentsUserData<PasswordManager> {
39 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
41 static void CreateForWebContentsAndDelegate(
42 content::WebContents* contents,
43 PasswordManagerDelegate* delegate);
44 virtual ~PasswordManager();
46 typedef base::Callback<void(const autofill::PasswordForm&)>
47 PasswordSubmittedCallback;
49 // There is no corresponding remove function as currently all of the
50 // owners of these callbacks have sufficient lifetimes so that the callbacks
51 // should always be valid when called.
52 void AddSubmissionCallback(const PasswordSubmittedCallback& callback);
54 // Is saving new data for password autofill enabled for the current profile?
55 // For example, saving new data is disabled in Incognito mode, whereas filling
57 bool IsSavingEnabled() const;
59 // Called by a PasswordFormManager when it decides a form can be autofilled
61 virtual void Autofill(const autofill::PasswordForm& form_for_autofill,
62 const autofill::PasswordFormMap& best_matches,
63 const autofill::PasswordForm& preferred_match,
64 bool wait_for_username) const;
66 // LoginModel implementation.
67 virtual void AddObserver(LoginModelObserver* observer) OVERRIDE;
68 virtual void RemoveObserver(LoginModelObserver* observer) OVERRIDE;
70 // Mark this form as having a generated password.
71 void SetFormHasGeneratedPassword(const autofill::PasswordForm& form);
73 // TODO(isherman): This should not be public, but is currently being used by
74 // the LoginPrompt code.
75 // When a form is submitted, we prepare to save the password but wait
76 // until we decide the user has successfully logged in. This is step 1
77 // of 2 (see SavePassword).
78 void ProvisionallySavePassword(const autofill::PasswordForm& form);
80 // content::WebContentsObserver overrides.
81 virtual void DidNavigateMainFrame(
82 const content::LoadCommittedDetails& details,
83 const content::FrameNavigateParams& params) OVERRIDE;
84 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
86 // TODO(isherman): This should not be public, but is currently being used by
87 // the LoginPrompt code.
88 void OnPasswordFormsParsed(
89 const std::vector<autofill::PasswordForm>& forms);
90 void OnPasswordFormsRendered(
91 const std::vector<autofill::PasswordForm>& visible_forms);
94 // Subclassed for unit tests.
95 PasswordManager(content::WebContents* web_contents,
96 PasswordManagerDelegate* delegate);
98 // Handle notification that a password form was submitted.
99 virtual void OnPasswordFormSubmitted(
100 const autofill::PasswordForm& password_form);
103 friend class content::WebContentsUserData<PasswordManager>;
105 enum ProvisionalSaveFailure {
109 MATCHING_NOT_COMPLETE,
116 // Log failure for UMA. Logs additional metrics if the |form_origin|
117 // corresponds to one of the top, explicitly monitored websites.
118 void RecordFailure(ProvisionalSaveFailure failure,
119 const std::string& form_origin);
121 // Possibly set up FieldTrial for testing other possible usernames. This only
122 // happens if there are other_possible_usernames to be shown and the
123 // experiment hasn't already been initialized. We setup the experiment at
124 // such a late time because this experiment will only affect a small number
125 // of users so we want to include a larger fraction of these users than the
127 void PossiblyInitializeUsernamesExperiment(
128 const autofill::PasswordFormMap& matches) const;
130 // Returns true if we can show possible usernames to users in cases where
131 // the username for the form is ambigious.
132 bool OtherPossibleUsernamesEnabled() const;
134 // Returns true if we should show an infobar instead of automatically saving
135 // the password, based on inspecting the state of |provisional_save_manager_|.
136 bool ShouldShowSavePasswordInfoBar() const;
138 // Note about how a PasswordFormManager can transition from
139 // pending_login_managers_ to provisional_save_manager_ and the infobar.
144 // pending_login -- form submit --> provisional_save ___/
145 // ^ | \___ (update DB)
147 // |-----------<------<---------| !new
149 // When a form is "seen" on a page, a PasswordFormManager is created
150 // and stored in this collection until user navigates away from page.
152 ScopedVector<PasswordFormManager> pending_login_managers_;
154 // When the user submits a password/credential, this contains the
155 // PasswordFormManager for the form in question until we deem the login
156 // attempt to have succeeded (as in valid credentials). If it fails, we
157 // send the PasswordFormManager back to the pending_login_managers_ set.
158 // Scoped in case PasswordManager gets deleted (e.g tab closes) between the
159 // time a user submits a login form and gets to the next page.
160 scoped_ptr<PasswordFormManager> provisional_save_manager_;
162 // Our delegate for carrying out external operations. This is typically the
163 // containing WebContents.
164 PasswordManagerDelegate* const delegate_;
166 // Set to false to disable the password manager (will no longer ask if you
167 // want to save passwords but will continue to fill passwords).
168 BooleanPrefMember password_manager_enabled_;
170 // Observers to be notified of LoginModel events. This is mutable to allow
171 // notification in const member functions.
172 mutable ObserverList<LoginModelObserver> observers_;
174 // Callbacks to be notified when a password form has been submitted.
175 std::vector<PasswordSubmittedCallback> submission_callbacks_;
177 DISALLOW_COPY_AND_ASSIGN(PasswordManager);
180 #endif // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_MANAGER_H_