1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Copyright 2014 Samsung Electronics. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
6 #ifndef PASSWORD_MANAGER_H
7 #define PASSWORD_MANAGER_H
9 #if defined(TIZEN_AUTOFILL_SUPPORT)
13 #include "base/callback.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/scoped_vector.h"
16 #include "base/observer_list.h"
17 #include "base/prefs/pref_member.h"
18 #include "base/stl_util.h"
19 #include "browser/password_manager/password_form_manager.h"
20 #include "components/autofill/core/common/password_form.h"
21 #include "components/autofill/core/common/password_form_fill_data.h"
22 #include "components/password_manager/core/browser/login_model.h"
24 class PrefRegistrySimple;
30 namespace user_prefs {
31 class PrefRegistrySyncable;
34 // Per-tab password manager. Handles creation and management of UI elements,
35 // receiving password form data from the renderer and managing the password
36 // database through the PasswordStore. The PasswordManager is a LoginModel
37 // for purposes of supporting HTTP authentication dialogs.
38 namespace password_manager {
39 class PasswordManager : public LoginModel {
41 static const char kOtherPossibleUsernamesExperiment[];
43 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
45 explicit PasswordManager(PasswordManagerClient* client);
46 virtual ~PasswordManager();
48 typedef base::Callback<void(const autofill::PasswordForm&)>
49 PasswordSubmittedCallback;
51 // There is no corresponding remove function as currently all of the
52 // owners of these callbacks have sufficient lifetimes so that the callbacks
53 // should always be valid when called.
54 void AddSubmissionCallback(const PasswordSubmittedCallback& callback);
56 // Is password manager enabled for autofill password
57 bool IsPasswordManagerSavingEnabled() const;
59 // Set password manager enabled for autofill password
60 void SetPasswordManagerSavingEnabled(bool enabled);
62 bool IsPasswordManagerFillingEnabled() const;
63 void SetPasswordManagerFillingEnabled(bool enabled);
65 // Called by a PasswordFormManager when it decides a form can be autofilled
67 virtual void Autofill(const autofill::PasswordForm& form_for_autofill,
68 const autofill::PasswordFormMap& best_matches,
69 const autofill::PasswordForm& preferred_match,
70 bool wait_for_username) const;
72 // LoginModel implementation.
73 virtual void AddObserver(LoginModelObserver* observer) override;
74 virtual void RemoveObserver(LoginModelObserver* observer) override;
76 // Mark this form as having a generated password.
77 void SetFormHasGeneratedPassword(const autofill::PasswordForm& form);
79 // TODO(isherman): This should not be public, but is currently being used by
80 // the LoginPrompt code.
81 // When a form is submitted, we prepare to save the password but wait
82 // until we decide the user has successfully logged in. This is step 1
83 // of 2 (see SavePassword).
84 void ProvisionallySavePassword(const autofill::PasswordForm& form);
86 // Should be called when the user navigates the main frame.
87 void DidNavigateMainFrame(bool is_in_page);
89 // Handles password forms being parsed.
90 void OnPasswordFormsParsed(
91 const std::vector<autofill::PasswordForm>& forms);
93 // Handles password forms being rendered.
94 void OnPasswordFormsRendered(
95 const std::vector<autofill::PasswordForm>& visible_forms,
96 bool did_stop_loading);
98 // Handles a password form being submitted.
99 virtual void OnPasswordFormSubmitted(
100 const autofill::PasswordForm& password_form);
103 enum ProvisionalSaveFailure {
107 MATCHING_NOT_COMPLETE,
114 // Log failure for UMA. Logs additional metrics if the |form_origin|
115 // corresponds to one of the top, explicitly monitored websites.
116 void RecordFailure(ProvisionalSaveFailure failure,
117 const std::string& form_origin);
119 // Possibly set up FieldTrial for testing other possible usernames. This only
120 // happens if there are other_possible_usernames to be shown and the
121 // experiment hasn't already been initialized. We setup the experiment at
122 // such a late time because this experiment will only affect a small number
123 // of users so we want to include a larger fraction of these users than the
125 void PossiblyInitializeUsernamesExperiment(
126 const autofill::PasswordFormMap& matches) const;
128 // Returns true if we can show possible usernames to users in cases where
129 // the username for the form is ambigious.
130 bool OtherPossibleUsernamesEnabled() const;
132 // Returns true if the user needs to be prompted before a password can be
133 // saved (instead of automatically saving
134 // the password), based on inspecting the state of
135 // |provisional_save_manager_|.
136 bool ShouldPromptUserToSavePassword() 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 // The embedder-level client. Must outlive this class.
163 PasswordManagerClient* const client_;
165 // The platform-level driver. Must outlive this class.
166 PasswordManagerDriver* const driver_;
168 // Set to false to disable the password manager (will no longer ask if you
169 // want to save passwords but will continue to fill passwords).
170 bool password_manager_saving_enabled_;
172 bool password_manager_filling_enabled_;
174 // Observers to be notified of LoginModel events. This is mutable to allow
175 // notification in const member functions.
176 mutable base::ObserverList<LoginModelObserver> observers_;
178 // Callbacks to be notified when a password form has been submitted.
179 std::vector<PasswordSubmittedCallback> submission_callbacks_;
181 DISALLOW_COPY_AND_ASSIGN(PasswordManager);
185 #endif // TIZEN_AUTOFILL_SUPPORT
187 #endif // PASSWORD_MANAGER_H