| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_UI_PASSWORDS_MANAGE_PASSWORDS_UI_CONTROLLER_H_ | 5 #ifndef CHROME_BROWSER_UI_PASSWORDS_MANAGE_PASSWORDS_UI_CONTROLLER_H_ |
| 6 #define CHROME_BROWSER_UI_PASSWORDS_MANAGE_PASSWORDS_UI_CONTROLLER_H_ | 6 #define CHROME_BROWSER_UI_PASSWORDS_MANAGE_PASSWORDS_UI_CONTROLLER_H_ |
| 7 | 7 |
| 8 #include "base/memory/scoped_vector.h" | 8 #include "base/memory/scoped_vector.h" |
| 9 #include "base/timer/elapsed_timer.h" | 9 #include "base/timer/elapsed_timer.h" |
| 10 #include "chrome/browser/ui/passwords/manage_passwords_state.h" |
| 10 #include "components/autofill/core/common/password_form.h" | 11 #include "components/autofill/core/common/password_form.h" |
| 11 #include "components/password_manager/core/browser/password_store.h" | 12 #include "components/password_manager/core/browser/password_store.h" |
| 12 #include "components/password_manager/core/common/password_manager_ui.h" | 13 #include "components/password_manager/core/common/password_manager_ui.h" |
| 13 #include "content/public/browser/web_contents_observer.h" | 14 #include "content/public/browser/web_contents_observer.h" |
| 14 #include "content/public/browser/web_contents_user_data.h" | 15 #include "content/public/browser/web_contents_user_data.h" |
| 15 | 16 |
| 16 namespace content { | 17 namespace content { |
| 17 class WebContents; | 18 class WebContents; |
| 18 } | 19 } |
| 19 | 20 |
| 20 namespace password_manager { | 21 namespace password_manager { |
| 21 enum class CredentialType : unsigned int; | 22 enum class CredentialType : unsigned int; |
| 22 struct CredentialInfo; | 23 struct CredentialInfo; |
| 23 class PasswordFormManager; | 24 class PasswordFormManager; |
| 24 } | 25 } |
| 25 | 26 |
| 26 class ManagePasswordsIcon; | 27 class ManagePasswordsIcon; |
| 27 | 28 |
| 28 // Per-tab class to control the Omnibox password icon and bubble. | 29 // Per-tab class to control the Omnibox password icon and bubble. |
| 29 class ManagePasswordsUIController | 30 class ManagePasswordsUIController |
| 30 : public content::WebContentsObserver, | 31 : public content::WebContentsObserver, |
| 31 public content::WebContentsUserData<ManagePasswordsUIController>, | 32 public content::WebContentsUserData<ManagePasswordsUIController>, |
| 32 public password_manager::PasswordStore::Observer { | 33 public password_manager::PasswordStore::Observer { |
| 33 public: | 34 public: |
| 34 ~ManagePasswordsUIController() override; | 35 ~ManagePasswordsUIController() override; |
| 35 | 36 |
| 36 // Called when the user submits a form containing login information, so we | 37 // Called when the user submits a form containing login information, so we |
| 37 // can handle later requests to save or blacklist that login information. | 38 // can handle later requests to save or blacklist that login information. |
| 38 // This stores the provided object in form_manager_ and triggers the UI to | 39 // This stores the provided object and triggers the UI to prompt the user |
| 39 // prompt the user about whether they would like to save the password. | 40 // about whether they would like to save the password. |
| 40 void OnPasswordSubmitted( | 41 void OnPasswordSubmitted( |
| 41 scoped_ptr<password_manager::PasswordFormManager> form_manager); | 42 scoped_ptr<password_manager::PasswordFormManager> form_manager); |
| 42 | 43 |
| 43 // Called when the site asks user to choose from credentials. This triggers | 44 // Called when the site asks user to choose from credentials. This triggers |
| 44 // the UI to prompt the user. |local_credentials| and |federated_credentials| | 45 // the UI to prompt the user. |local_credentials| and |federated_credentials| |
| 45 // shouldn't both be empty. | 46 // shouldn't both be empty. |
| 46 bool OnChooseCredentials( | 47 bool OnChooseCredentials( |
| 47 ScopedVector<autofill::PasswordForm> local_credentials, | 48 ScopedVector<autofill::PasswordForm> local_credentials, |
| 48 ScopedVector<autofill::PasswordForm> federated_credentials, | 49 ScopedVector<autofill::PasswordForm> federated_credentials, |
| 49 const GURL& origin, | 50 const GURL& origin, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 const autofill::PasswordForm& form, | 86 const autofill::PasswordForm& form, |
| 86 password_manager::CredentialType credential_type); | 87 password_manager::CredentialType credential_type); |
| 87 | 88 |
| 88 // Called from the model when the user chooses to never save passwords; passes | 89 // Called from the model when the user chooses to never save passwords; passes |
| 89 // the action off to the FormManager. The controller MUST be in a pending | 90 // the action off to the FormManager. The controller MUST be in a pending |
| 90 // state, and WILL be in BLACKLIST_STATE after this method executes. | 91 // state, and WILL be in BLACKLIST_STATE after this method executes. |
| 91 virtual void NeverSavePassword(); | 92 virtual void NeverSavePassword(); |
| 92 | 93 |
| 93 // Called from the model when the user chooses to unblacklist the site. The | 94 // Called from the model when the user chooses to unblacklist the site. The |
| 94 // controller MUST be in BLACKLIST_STATE, and WILL be in MANAGE_STATE after | 95 // controller MUST be in BLACKLIST_STATE, and WILL be in MANAGE_STATE after |
| 95 // this method executes. | 96 // this method executes. The method removes the first form of |
| 97 // GetCurrentForms() which should be the blacklisted one. |
| 96 virtual void UnblacklistSite(); | 98 virtual void UnblacklistSite(); |
| 97 | 99 |
| 98 // Open a new tab, pointing to the password manager settings page. | 100 // Open a new tab, pointing to the password manager settings page. |
| 99 virtual void NavigateToPasswordManagerSettingsPage(); | 101 virtual void NavigateToPasswordManagerSettingsPage(); |
| 100 | 102 |
| 101 virtual const autofill::PasswordForm& PendingPassword() const; | 103 virtual const autofill::PasswordForm& PendingPassword() const; |
| 102 | 104 |
| 103 // Set the state of the Omnibox icon, and possibly show the associated bubble | 105 // Set the state of the Omnibox icon, and possibly show the associated bubble |
| 104 // without user interaction. | 106 // without user interaction. |
| 105 virtual void UpdateIconAndBubbleState(ManagePasswordsIcon* icon); | 107 virtual void UpdateIconAndBubbleState(ManagePasswordsIcon* icon); |
| 106 | 108 |
| 107 // Called from the model when the bubble is displayed. | 109 // Called from the model when the bubble is displayed. |
| 108 void OnBubbleShown(); | 110 void OnBubbleShown(); |
| 109 | 111 |
| 110 // Called from the model when the bubble is hidden. | 112 // Called from the model when the bubble is hidden. |
| 111 void OnBubbleHidden(); | 113 void OnBubbleHidden(); |
| 112 | 114 |
| 113 password_manager::ui::State state() const { return state_; } | 115 password_manager::ui::State state() const { return passwords_data_.state(); } |
| 114 | |
| 115 ScopedVector<autofill::PasswordForm>& federated_credentials_forms() { | |
| 116 return federated_credentials_forms_; | |
| 117 } | |
| 118 | |
| 119 ScopedVector<autofill::PasswordForm>& local_credentials_forms() { | |
| 120 return local_credentials_forms_; | |
| 121 } | |
| 122 | 116 |
| 123 // True if a password is sitting around, waiting for a user to decide whether | 117 // True if a password is sitting around, waiting for a user to decide whether |
| 124 // or not to save it. | 118 // or not to save it. |
| 125 bool PasswordPendingUserDecision() const; | 119 bool PasswordPendingUserDecision() const { |
| 126 | 120 return state() == password_manager::ui::PENDING_PASSWORD_STATE; |
| 127 const autofill::ConstPasswordFormMap& best_matches() const { | |
| 128 return password_form_map_; | |
| 129 } | 121 } |
| 130 | 122 |
| 131 const GURL& origin() const { return origin_; } | 123 const GURL& origin() const { return passwords_data_.origin(); } |
| 132 | 124 |
| 133 bool IsAutomaticallyOpeningBubble() const { return should_pop_up_bubble_; } | 125 bool IsAutomaticallyOpeningBubble() const { return should_pop_up_bubble_; } |
| 134 | 126 |
| 127 // Current local forms. |
| 128 const std::vector<const autofill::PasswordForm*>& GetCurrentForms() const { |
| 129 return passwords_data_.GetCurrentForms(); |
| 130 } |
| 131 |
| 132 // Current federated forms. |
| 133 const std::vector<const autofill::PasswordForm*>& GetFederatedForms() const { |
| 134 return passwords_data_.federated_credentials_forms(); |
| 135 } |
| 136 |
| 135 protected: | 137 protected: |
| 136 explicit ManagePasswordsUIController( | 138 explicit ManagePasswordsUIController( |
| 137 content::WebContents* web_contents); | 139 content::WebContents* web_contents); |
| 138 | 140 |
| 139 // The pieces of saving and blacklisting passwords that interact with | 141 // The pieces of saving and blacklisting passwords that interact with |
| 140 // FormManager, split off into internal functions for testing/mocking. | 142 // FormManager, split off into internal functions for testing/mocking. |
| 141 virtual void SavePasswordInternal(); | 143 virtual void SavePasswordInternal(); |
| 142 virtual void NeverSavePasswordInternal(); | 144 virtual void NeverSavePasswordInternal(); |
| 143 | 145 |
| 144 // Called when a passwordform is autofilled, when a new passwordform is | 146 // Called when a PasswordForm is autofilled, when a new PasswordForm is |
| 145 // submitted, or when a navigation occurs to update the visibility of the | 147 // submitted, or when a navigation occurs to update the visibility of the |
| 146 // manage passwords icon and bubble. | 148 // manage passwords icon and bubble. |
| 147 virtual void UpdateBubbleAndIconVisibility(); | 149 virtual void UpdateBubbleAndIconVisibility(); |
| 148 | 150 |
| 149 // Returns the time elapsed since |timer_| was initialized, | 151 // Returns the time elapsed since |timer_| was initialized, |
| 150 // or base::TimeDelta::Max() if |timer_| was not initialized. | 152 // or base::TimeDelta::Max() if |timer_| was not initialized. |
| 151 virtual base::TimeDelta Elapsed() const; | 153 virtual base::TimeDelta Elapsed() const; |
| 152 | 154 |
| 155 // Overwrites the client for |passwords_data_|. |
| 156 void set_client(password_manager::PasswordManagerClient* client) { |
| 157 passwords_data_.set_client(client); |
| 158 } |
| 159 |
| 153 // content::WebContentsObserver: | 160 // content::WebContentsObserver: |
| 154 void DidNavigateMainFrame( | 161 void DidNavigateMainFrame( |
| 155 const content::LoadCommittedDetails& details, | 162 const content::LoadCommittedDetails& details, |
| 156 const content::FrameNavigateParams& params) override; | 163 const content::FrameNavigateParams& params) override; |
| 157 void WasHidden() override; | 164 void WasHidden() override; |
| 158 | 165 |
| 159 // Sets |state_|. Protected so we can manipulate the value in tests. | |
| 160 void SetState(password_manager::ui::State state); | |
| 161 | |
| 162 // All previously stored credentials for a specific site. | |
| 163 // Protected, not private, so we can mess with the value in | |
| 164 // ManagePasswordsUIControllerMock. | |
| 165 autofill::ConstPasswordFormMap password_form_map_; | |
| 166 | |
| 167 private: | 166 private: |
| 168 friend class content::WebContentsUserData<ManagePasswordsUIController>; | 167 friend class content::WebContentsUserData<ManagePasswordsUIController>; |
| 169 | 168 |
| 170 // Shows the password bubble without user interaction. | 169 // Shows the password bubble without user interaction. |
| 171 void ShowBubbleWithoutUserInteraction(); | 170 void ShowBubbleWithoutUserInteraction(); |
| 172 | 171 |
| 173 // content::WebContentsObserver: | 172 // content::WebContentsObserver: |
| 174 void WebContentsDestroyed() override; | 173 void WebContentsDestroyed() override; |
| 175 | 174 |
| 176 // Saves the parameters and clean the previous forms. | |
| 177 void SaveForms(ScopedVector<autofill::PasswordForm> local_forms, | |
| 178 ScopedVector<autofill::PasswordForm> federated_forms); | |
| 179 | |
| 180 // Shows infobar which allows user to choose credentials. Placing this | 175 // Shows infobar which allows user to choose credentials. Placing this |
| 181 // code to separate method allows mocking. | 176 // code to separate method allows mocking. |
| 182 virtual void UpdateAndroidAccountChooserInfoBarVisibility(); | 177 virtual void UpdateAndroidAccountChooserInfoBarVisibility(); |
| 183 | 178 |
| 184 // The current state of the password manager UI. | 179 // The wrapper around current state and data. |
| 185 password_manager::ui::State state_; | 180 ManagePasswordsState passwords_data_; |
| 186 | 181 |
| 187 // Used to measure the amount of time on a page; if it's less than some | 182 // Used to measure the amount of time on a page; if it's less than some |
| 188 // reasonable limit, then don't close the bubble upon navigation. We create | 183 // reasonable limit, then don't close the bubble upon navigation. We create |
| 189 // (and destroy) the timer in DidNavigateMainFrame. | 184 // (and destroy) the timer in DidNavigateMainFrame. |
| 190 scoped_ptr<base::ElapsedTimer> timer_; | 185 scoped_ptr<base::ElapsedTimer> timer_; |
| 191 | 186 |
| 192 // TODO(vasilii): remove these data variables, use ManagePasswordsState. | |
| 193 | |
| 194 // Set by OnPasswordSubmitted() when the user submits a form containing login | |
| 195 // information. If the user responds to a subsequent "Do you want to save | |
| 196 // this password?" prompt, we ask this object to save or blacklist the | |
| 197 // associated login information in Chrome's password store. | |
| 198 scoped_ptr<password_manager::PasswordFormManager> form_manager_; | |
| 199 | |
| 200 // We create copies of PasswordForm objects that come in with unclear lifetime | |
| 201 // and store them in this vector as well as in |password_form_map_| to ensure | |
| 202 // that we destroy them correctly. If |new_password_forms_| gets cleared then | |
| 203 // |password_form_map_| is to be cleared too. | |
| 204 ScopedVector<autofill::PasswordForm> new_password_forms_; | |
| 205 | |
| 206 // Federated credentials. Stores federated credentials which will be shown | |
| 207 // when Credential Management API was used. | |
| 208 ScopedVector<autofill::PasswordForm> federated_credentials_forms_; | |
| 209 | |
| 210 // Local credentials. Stores local credentials which will be shown | |
| 211 // when Credential Management API was used. | |
| 212 ScopedVector<autofill::PasswordForm> local_credentials_forms_; | |
| 213 | |
| 214 // A callback to be invoked when user selects a credential. | |
| 215 base::Callback<void(const password_manager::CredentialInfo&)> | |
| 216 credentials_callback_; | |
| 217 | |
| 218 // Contains true if the bubble is to be popped up in the next call to | 187 // Contains true if the bubble is to be popped up in the next call to |
| 219 // UpdateBubbleAndIconVisibility(). | 188 // UpdateBubbleAndIconVisibility(). |
| 220 bool should_pop_up_bubble_; | 189 bool should_pop_up_bubble_; |
| 221 | 190 |
| 222 // The origin of the form we're currently dealing with; we'll use this to | |
| 223 // determine which PasswordStore changes we should care about when updating | |
| 224 // |password_form_map_|. | |
| 225 GURL origin_; | |
| 226 | |
| 227 DISALLOW_COPY_AND_ASSIGN(ManagePasswordsUIController); | 191 DISALLOW_COPY_AND_ASSIGN(ManagePasswordsUIController); |
| 228 }; | 192 }; |
| 229 | 193 |
| 230 #endif // CHROME_BROWSER_UI_PASSWORDS_MANAGE_PASSWORDS_UI_CONTROLLER_H_ | 194 #endif // CHROME_BROWSER_UI_PASSWORDS_MANAGE_PASSWORDS_UI_CONTROLLER_H_ |
| OLD | NEW |