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. 4 5 #ifndef CHROME_BROWSER_UI_PASSWORDS_MANAGE_PASSWORDS_STATE_H_ 6 #define CHROME_BROWSER_UI_PASSWORDS_MANAGE_PASSWORDS_STATE_H_ 7 8 #include <memory> 9 #include <vector> 10 11 #include "base/callback.h" 12 #include "base/macros.h" 13 #include "base/strings/string16.h" 14 #include "components/password_manager/core/browser/password_form.h" 15 #include "components/password_manager/core/browser/password_store_change.h" 16 #include "components/password_manager/core/common/credential_manager_types.h" 17 #include "components/password_manager/core/common/password_manager_ui.h" 18 #include "url/gurl.h" 19 20 namespace password_manager { 21 class PasswordFormManagerForUI; 22 class PasswordManagerClient; 23 } // namespace password_manager 24 25 // ManagePasswordsState keeps the current state for ManagePasswordsUIController 26 // as well as up-to-date data for this state. 27 class ManagePasswordsState { 28 public: 29 using CredentialsCallback = 30 base::OnceCallback<void(const password_manager::PasswordForm*)>; 31 32 ManagePasswordsState(); 33 ~ManagePasswordsState(); 34 35 // The embedder of this class has to set the client for logging. set_client(password_manager::PasswordManagerClient * client)36 void set_client(password_manager::PasswordManagerClient* client) { 37 client_ = client; 38 } 39 client()40 password_manager::PasswordManagerClient* client() { return client_; } 41 42 // The methods below discard the current state/data of the object and move it 43 // to the specified state. 44 45 // Move to PENDING_PASSWORD_STATE. 46 void OnPendingPassword( 47 std::unique_ptr<password_manager::PasswordFormManagerForUI> form_manager); 48 49 // Move to PENDING_PASSWORD_UPDATE_STATE. 50 void OnUpdatePassword( 51 std::unique_ptr<password_manager::PasswordFormManagerForUI> form_manager); 52 53 // Move to CREDENTIAL_REQUEST_STATE. 54 void OnRequestCredentials( 55 std::vector<std::unique_ptr<password_manager::PasswordForm>> 56 local_credentials, 57 const url::Origin& origin); 58 59 // Move to AUTO_SIGNIN_STATE. |local_forms| can't be empty. 60 void OnAutoSignin( 61 std::vector<std::unique_ptr<password_manager::PasswordForm>> local_forms, 62 const url::Origin& origin); 63 64 // Move to CONFIRMATION_STATE. 65 void OnAutomaticPasswordSave( 66 std::unique_ptr<password_manager::PasswordFormManagerForUI> form_manager); 67 68 // Move to MANAGE_STATE or INACTIVE_STATE for PSL matched passwords. 69 // |password_forms| contains best matches from the password store for the 70 // form which was autofilled, |origin| is an origin of the form which was 71 // autofilled. In addition, |federated_matches|, if not null, contains stored 72 // federated credentials to show to the user as well. 73 void OnPasswordAutofilled( 74 const std::vector<const password_manager::PasswordForm*>& password_forms, 75 url::Origin origin, 76 const std::vector<const password_manager::PasswordForm*>* 77 federated_matches); 78 79 // Move to INACTIVE_STATE. 80 void OnInactive(); 81 82 // Move to CAN_MOVE_PASSWORD_TO_ACCOUNT_STATE. Triggers a bubble to move the 83 // just submitted form to the user's account store. 84 void OnPasswordMovable( 85 std::unique_ptr<password_manager::PasswordFormManagerForUI> form_to_move); 86 87 // Moves the object to |state| without resetting the internal data. Allowed: 88 // * -> MANAGE_STATE 89 // * -> PASSWORD_UPDATED_* 90 void TransitionToState(password_manager::ui::State state); 91 92 // Updates the internal state applying |changes|. 93 void ProcessLoginsChanged( 94 const password_manager::PasswordStoreChangeList& changes); 95 96 void ProcessUnsyncedCredentialsWillBeDeleted( 97 std::vector<password_manager::PasswordForm> unsynced_credentials); 98 99 // Called when the user chooses a credential. |form| is passed to the 100 // credentials callback. Method should be called in the 101 // CREDENTIAL_REQUEST_STATE state. 102 void ChooseCredential(const password_manager::PasswordForm* form); 103 state()104 password_manager::ui::State state() const { return state_; } unsynced_credentials()105 const std::vector<password_manager::PasswordForm>& unsynced_credentials() 106 const { 107 return unsynced_credentials_; 108 } origin()109 const url::Origin& origin() const { return origin_; } form_manager()110 password_manager::PasswordFormManagerForUI* form_manager() const { 111 return form_manager_.get(); 112 } credentials_callback()113 const CredentialsCallback& credentials_callback() { 114 return credentials_callback_; 115 } set_credentials_callback(CredentialsCallback callback)116 void set_credentials_callback(CredentialsCallback callback) { 117 credentials_callback_ = std::move(callback); 118 } 119 auth_for_account_storage_opt_in_failed()120 bool auth_for_account_storage_opt_in_failed() const { 121 return auth_for_account_storage_opt_in_failed_; 122 } set_auth_for_account_storage_opt_in_failed(bool failed)123 void set_auth_for_account_storage_opt_in_failed(bool failed) { 124 auth_for_account_storage_opt_in_failed_ = failed; 125 } 126 127 // Current local forms. ManagePasswordsState is responsible for the forms. 128 const std::vector<std::unique_ptr<password_manager::PasswordForm>>& GetCurrentForms()129 GetCurrentForms() const { 130 return local_credentials_forms_; 131 } 132 133 private: 134 // Removes all the PasswordForms stored in this object. 135 void ClearData(); 136 137 // Adds |form| to the internal state if it's relevant. 138 bool AddForm(const password_manager::PasswordForm& form); 139 140 void SetState(password_manager::ui::State state); 141 142 // The origin of the current page for which the state is stored. It's used to 143 // determine which PasswordStore changes are applicable to the internal state. 144 url::Origin origin_; 145 146 // Contains the password that was submitted. 147 std::unique_ptr<password_manager::PasswordFormManagerForUI> form_manager_; 148 149 // Contains all the current forms. 150 std::vector<std::unique_ptr<password_manager::PasswordForm>> 151 local_credentials_forms_; 152 153 // Contains any non synced credentials. 154 std::vector<password_manager::PasswordForm> unsynced_credentials_; 155 156 // A callback to be invoked when user selects a credential. 157 CredentialsCallback credentials_callback_; 158 159 // The current state of the password manager UI. 160 password_manager::ui::State state_; 161 162 // The client used for logging. 163 password_manager::PasswordManagerClient* client_; 164 165 // Whether the last attempt to authenticate to opt-in using password account 166 // storage failed. 167 bool auth_for_account_storage_opt_in_failed_ = false; 168 169 DISALLOW_COPY_AND_ASSIGN(ManagePasswordsState); 170 }; 171 172 #endif // CHROME_BROWSER_UI_PASSWORDS_MANAGE_PASSWORDS_STATE_H_ 173