Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / passwords / manage_passwords_state.h
blobf05f0dbf5a8eac1c28cbba8b2fe1d9091726ea72
1 // Copyright 2015 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_UI_PASSWORDS_MANAGE_PASSWORDS_STATE_H_
6 #define CHROME_BROWSER_UI_PASSWORDS_MANAGE_PASSWORDS_STATE_H_
8 #include "base/callback.h"
9 #include "base/macros.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/scoped_vector.h"
12 #include "components/autofill/core/common/password_form.h"
13 #include "components/password_manager/core/browser/password_store_change.h"
14 #include "components/password_manager/core/common/password_manager_ui.h"
15 #include "url/gurl.h"
17 namespace password_manager {
18 struct CredentialInfo;
19 class PasswordFormManager;
20 class PasswordManagerClient;
24 // ManagePasswordsState keeps the current state for ManagePasswordsUIController
25 // as well as up-to-date data for this state.
26 class ManagePasswordsState {
27 public:
28 using CredentialsCallback =
29 base::Callback<void(const password_manager::CredentialInfo&)>;
31 ManagePasswordsState();
32 ~ManagePasswordsState();
34 // The embedder of this class has to set the client for logging.
35 void set_client(password_manager::PasswordManagerClient* client) {
36 client_ = client;
39 // The methods below discard the current state/data of the object and move it
40 // to the specified state.
42 // Move to PENDING_PASSWORD_STATE.
43 void OnPendingPassword(
44 scoped_ptr<password_manager::PasswordFormManager> form_manager);
46 // Move to PENDING_PASSWORD_UPDATE_STATE.
47 void OnUpdatePassword(
48 scoped_ptr<password_manager::PasswordFormManager> form_manager);
50 // Move to CREDENTIAL_REQUEST_STATE.
51 void OnRequestCredentials(
52 ScopedVector<autofill::PasswordForm> local_credentials,
53 ScopedVector<autofill::PasswordForm> federated_credentials,
54 const GURL& origin);
56 // Move to AUTO_SIGNIN_STATE. |local_forms| can't be empty.
57 void OnAutoSignin(ScopedVector<autofill::PasswordForm> local_forms);
59 // Move to CONFIRMATION_STATE.
60 void OnAutomaticPasswordSave(
61 scoped_ptr<password_manager::PasswordFormManager> form_manager);
63 // Move to MANAGE_STATE or INACTIVE_STATE for PSL matched passwords.
64 void OnPasswordAutofilled(const autofill::PasswordFormMap& password_form_map);
66 // Move to INACTIVE_STATE.
67 void OnInactive();
69 // Moves the object to |state| without resetting the internal data. Allowed:
70 // * -> MANAGE_STATE
71 void TransitionToState(password_manager::ui::State state);
73 // Updates the internal state applying |changes|.
74 void ProcessLoginsChanged(
75 const password_manager::PasswordStoreChangeList& changes);
77 password_manager::ui::State state() const { return state_; }
78 const GURL& origin() const { return origin_; }
79 password_manager::PasswordFormManager* form_manager() const {
80 return form_manager_.get();
82 const CredentialsCallback& credentials_callback() {
83 return credentials_callback_;
85 void set_credentials_callback(const CredentialsCallback& callback) {
86 credentials_callback_ = callback;
89 // Current local forms. ManagePasswordsState is responsible for the forms.
90 const std::vector<const autofill::PasswordForm*>& GetCurrentForms() const {
91 return form_manager_ ? current_forms_weak_ : local_credentials_forms_.get();
94 // Current federated forms.
95 const std::vector<const autofill::PasswordForm*>&
96 federated_credentials_forms() const {
97 return federated_credentials_forms_.get();
100 private:
101 // Removes all the PasswordForms stored in this object.
102 void ClearData();
104 // Add |form| to the internal state.
105 void AddForm(const autofill::PasswordForm& form);
106 // Updates |form| in the internal state.
107 bool UpdateForm(const autofill::PasswordForm& form);
108 // Removes |form| from the internal state.
109 void DeleteForm(const autofill::PasswordForm& form);
111 void SetState(password_manager::ui::State state);
113 // The origin of the current page. It's used to determine which PasswordStore
114 // changes are applicable to the internal state.
115 GURL origin_;
117 // Contains the password that was submitted.
118 scoped_ptr<password_manager::PasswordFormManager> form_manager_;
120 // Weak references to the passwords for the current status. The hard pointers
121 // are scattered between |form_manager_| and |local_credentials_forms_|. If
122 // |form_manager_| is nullptr then all the forms are stored in
123 // |local_credentials_forms_|. |current_forms_weak_| remains empty.
124 std::vector<const autofill::PasswordForm*> current_forms_weak_;
126 // If |form_manager_| is nullptr then |local_credentials_forms_| contains all
127 // the current forms. Otherwise, it's a container for the new forms coming
128 // from the PasswordStore.
129 ScopedVector<const autofill::PasswordForm> local_credentials_forms_;
131 // Federated credentials for the CREDENTIAL_REQUEST_STATE.
132 ScopedVector<const autofill::PasswordForm> federated_credentials_forms_;
134 // A callback to be invoked when user selects a credential.
135 CredentialsCallback credentials_callback_;
137 // The current state of the password manager UI.
138 password_manager::ui::State state_;
140 // The client used for logging.
141 password_manager::PasswordManagerClient* client_;
143 DISALLOW_COPY_AND_ASSIGN(ManagePasswordsState);
146 #endif // CHROME_BROWSER_UI_PASSWORDS_MANAGE_PASSWORDS_STATE_H_