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 #ifdef 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 PasswordManagerClient;
25 class PasswordManagerDriver;
26 class PasswordManagerTest;
27 class PasswordFormManager;
28 class PrefRegistrySimple;
34 namespace user_prefs {
35 class PrefRegistrySyncable;
38 // Per-tab password manager. Handles creation and management of UI elements,
39 // receiving password form data from the renderer and managing the password
40 // database through the PasswordStore. The PasswordManager is a LoginModel
41 // for purposes of supporting HTTP authentication dialogs.
42 class PasswordManager : public LoginModel {
44 static const char kOtherPossibleUsernamesExperiment[];
46 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
48 explicit PasswordManager(PasswordManagerClient* client);
49 virtual ~PasswordManager();
51 typedef base::Callback<void(const autofill::PasswordForm&)>
52 PasswordSubmittedCallback;
54 // There is no corresponding remove function as currently all of the
55 // owners of these callbacks have sufficient lifetimes so that the callbacks
56 // should always be valid when called.
57 void AddSubmissionCallback(const PasswordSubmittedCallback& callback);
59 // Is password manager enabled for autofill password
60 bool IsPasswordManagerEnabled() const;
62 // Set password manager enabled for autofill password
63 void SetPasswordManagerEnabled(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);
97 // Handles a password form being submitted.
98 virtual void OnPasswordFormSubmitted(
99 const autofill::PasswordForm& password_form);
102 enum ProvisionalSaveFailure {
106 MATCHING_NOT_COMPLETE,
113 // Log failure for UMA. Logs additional metrics if the |form_origin|
114 // corresponds to one of the top, explicitly monitored websites.
115 void RecordFailure(ProvisionalSaveFailure failure,
116 const std::string& form_origin);
118 // Possibly set up FieldTrial for testing other possible usernames. This only
119 // happens if there are other_possible_usernames to be shown and the
120 // experiment hasn't already been initialized. We setup the experiment at
121 // such a late time because this experiment will only affect a small number
122 // of users so we want to include a larger fraction of these users than the
124 void PossiblyInitializeUsernamesExperiment(
125 const autofill::PasswordFormMap& matches) const;
127 // Returns true if we can show possible usernames to users in cases where
128 // the username for the form is ambigious.
129 bool OtherPossibleUsernamesEnabled() const;
131 // Returns true if the user needs to be prompted before a password can be
132 // saved (instead of automatically saving
133 // the password), based on inspecting the state of
134 // |provisional_save_manager_|.
135 bool ShouldPromptUserToSavePassword() const;
137 // Note about how a PasswordFormManager can transition from
138 // pending_login_managers_ to provisional_save_manager_ and the infobar.
143 // pending_login -- form submit --> provisional_save ___/
144 // ^ | \___ (update DB)
146 // |-----------<------<---------| !new
148 // When a form is "seen" on a page, a PasswordFormManager is created
149 // and stored in this collection until user navigates away from page.
151 ScopedVector<PasswordFormManager> pending_login_managers_;
153 // When the user submits a password/credential, this contains the
154 // PasswordFormManager for the form in question until we deem the login
155 // attempt to have succeeded (as in valid credentials). If it fails, we
156 // send the PasswordFormManager back to the pending_login_managers_ set.
157 // Scoped in case PasswordManager gets deleted (e.g tab closes) between the
158 // time a user submits a login form and gets to the next page.
159 scoped_ptr<PasswordFormManager> provisional_save_manager_;
161 // The embedder-level client. Must outlive this class.
162 PasswordManagerClient* const client_;
164 // The platform-level driver. Must outlive this class.
165 PasswordManagerDriver* const driver_;
167 // Set to false to disable the password manager (will no longer ask if you
168 // want to save passwords but will continue to fill passwords).
169 bool password_manager_enabled_;
171 // Observers to be notified of LoginModel events. This is mutable to allow
172 // notification in const member functions.
173 mutable ObserverList<LoginModelObserver> observers_;
175 // Callbacks to be notified when a password form has been submitted.
176 std::vector<PasswordSubmittedCallback> submission_callbacks_;
178 DISALLOW_COPY_AND_ASSIGN(PasswordManager);
181 #endif // TIZEN_AUTOFILL_SUPPORT
183 #endif // PASSWORD_MANAGER_H