Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / passwords / manage_passwords_ui_controller.h
blobd3f8bdbd0838b35faaa0b3bab5d5f12a6cff4030
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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_UI_PASSWORDS_MANAGE_PASSWORDS_UI_CONTROLLER_H_
6 #define CHROME_BROWSER_UI_PASSWORDS_MANAGE_PASSWORDS_UI_CONTROLLER_H_
8 #include "base/memory/scoped_vector.h"
9 #include "base/timer/elapsed_timer.h"
10 #include "chrome/browser/ui/passwords/manage_passwords_state.h"
11 #include "components/autofill/core/common/password_form.h"
12 #include "components/password_manager/core/browser/password_store.h"
13 #include "components/password_manager/core/common/password_manager_ui.h"
14 #include "content/public/browser/web_contents_observer.h"
15 #include "content/public/browser/web_contents_user_data.h"
17 namespace content {
18 class WebContents;
21 namespace password_manager {
22 enum class CredentialType;
23 struct CredentialInfo;
24 class PasswordFormManager;
27 class ManagePasswordsIcon;
29 // Per-tab class to control the Omnibox password icon and bubble.
30 class ManagePasswordsUIController
31 : public content::WebContentsObserver,
32 public content::WebContentsUserData<ManagePasswordsUIController>,
33 public password_manager::PasswordStore::Observer {
34 public:
35 ~ManagePasswordsUIController() override;
37 // Called when the user submits a form containing login information, so we
38 // can handle later requests to save or blacklist that login information.
39 // This stores the provided object and triggers the UI to prompt the user
40 // about whether they would like to save the password.
41 void OnPasswordSubmitted(
42 scoped_ptr<password_manager::PasswordFormManager> form_manager);
44 // Called when the user submits a change password form, so we can handle
45 // later requests to update stored credentials in the PasswordManager.
46 // This stores the provided object and triggers the UI to prompt the user
47 // about whether they would like to update the password.
48 void OnUpdatePasswordSubmitted(
49 scoped_ptr<password_manager::PasswordFormManager> form_manager);
51 // Called when the site asks user to choose from credentials. This triggers
52 // the UI to prompt the user. |local_credentials| and |federated_credentials|
53 // shouldn't both be empty.
54 bool OnChooseCredentials(
55 ScopedVector<autofill::PasswordForm> local_credentials,
56 ScopedVector<autofill::PasswordForm> federated_credentials,
57 const GURL& origin,
58 base::Callback<void(const password_manager::CredentialInfo&)> callback);
60 // Called when user is auto signed in to the site. |local_forms[0]| contains
61 // the credential returned to the site.
62 void OnAutoSignin(ScopedVector<autofill::PasswordForm> local_forms);
64 // Called when the password will be saved automatically, but we still wish to
65 // visually inform the user that the save has occured.
66 void OnAutomaticPasswordSave(
67 scoped_ptr<password_manager::PasswordFormManager> form_manager);
69 // Called when a form is autofilled with login information, so we can manage
70 // password credentials for the current site which are stored in
71 // |password_form_map|. This stores a copy of |password_form_map| and shows
72 // the manage password icon.
73 void OnPasswordAutofilled(const autofill::PasswordFormMap& password_form_map);
75 // TODO(vasilii): remove this method. It's obsolete.
76 void OnBlacklistBlockedAutofill(
77 const autofill::PasswordFormMap& password_form_map);
79 // PasswordStore::Observer implementation.
80 void OnLoginsChanged(
81 const password_manager::PasswordStoreChangeList& changes) override;
83 // Called from the model when the user chooses to save a password; passes the
84 // action to the |form_manager|. The controller must be in a pending state,
85 // and will be in MANAGE_STATE after this method executes.
86 virtual void SavePassword();
88 // Called from the model when the user chooses to update a password; passes
89 // the action to the |form_manager|. The controller must be in a pending
90 // state, and will be in MANAGE_STATE after this method executes.
91 virtual void UpdatePassword(const autofill::PasswordForm& password_form);
93 // Called from the model when the user chooses a credential.
94 // The controller MUST be in a pending credentials state.
95 virtual void ChooseCredential(
96 const autofill::PasswordForm& form,
97 password_manager::CredentialType credential_type);
99 // Called from the model when the user chooses to never save passwords; passes
100 // the action off to the FormManager. The controller must be in a pending
101 // state, and will state in this state.
102 virtual void NeverSavePassword();
104 // Called from the model. The controller should switch to MANAGE_STATE and pop
105 // up a bubble.
106 virtual void ManageAccounts();
108 // Open a new tab, pointing to the password manager settings page.
109 virtual void NavigateToPasswordManagerSettingsPage();
111 // Open a new tab, pointing to passwords.google.com.
112 void NavigateToExternalPasswordManager();
114 // Open a new tab, pointing to the Smart Lock help article.
115 void NavigateToSmartLockPage();
117 virtual const autofill::PasswordForm& PendingPassword() const;
119 // Set the state of the Omnibox icon, and possibly show the associated bubble
120 // without user interaction.
121 virtual void UpdateIconAndBubbleState(ManagePasswordsIcon* icon);
123 // Called from the model when the bubble is displayed.
124 void OnBubbleShown();
126 // Called from the model when the bubble is hidden.
127 void OnBubbleHidden();
129 password_manager::ui::State state() const { return passwords_data_.state(); }
131 // True if a password is sitting around, waiting for a user to decide whether
132 // or not to save it.
133 bool PasswordPendingUserDecision() const {
134 return state() == password_manager::ui::PENDING_PASSWORD_STATE;
137 const GURL& origin() const { return passwords_data_.origin(); }
139 bool IsAutomaticallyOpeningBubble() const { return should_pop_up_bubble_; }
141 // Current local forms.
142 const std::vector<const autofill::PasswordForm*>& GetCurrentForms() const {
143 return passwords_data_.GetCurrentForms();
146 // Current federated forms.
147 const std::vector<const autofill::PasswordForm*>& GetFederatedForms() const {
148 return passwords_data_.federated_credentials_forms();
151 // True if the password for previously stored account was overridden, i.e. in
152 // newly submitted form the password is different from stored one.
153 bool PasswordOverridden() const;
155 // Returns true if the multiple account selection prompt for account update
156 // should be presented.
157 bool ShouldShowMultipleAccountUpdateUI() const;
159 protected:
160 explicit ManagePasswordsUIController(
161 content::WebContents* web_contents);
163 // The pieces of saving and blacklisting passwords that interact with
164 // FormManager, split off into internal functions for testing/mocking.
165 virtual void SavePasswordInternal();
166 virtual void UpdatePasswordInternal(
167 const autofill::PasswordForm& password_form);
168 virtual void NeverSavePasswordInternal();
170 // Called when a PasswordForm is autofilled, when a new PasswordForm is
171 // submitted, or when a navigation occurs to update the visibility of the
172 // manage passwords icon and bubble.
173 virtual void UpdateBubbleAndIconVisibility();
175 // Returns the time elapsed since |timer_| was initialized,
176 // or base::TimeDelta::Max() if |timer_| was not initialized.
177 virtual base::TimeDelta Elapsed() const;
179 // Overwrites the client for |passwords_data_|.
180 void set_client(password_manager::PasswordManagerClient* client) {
181 passwords_data_.set_client(client);
184 // content::WebContentsObserver:
185 void DidNavigateMainFrame(
186 const content::LoadCommittedDetails& details,
187 const content::FrameNavigateParams& params) override;
188 void WasHidden() override;
190 private:
191 friend class content::WebContentsUserData<ManagePasswordsUIController>;
193 // Shows the password bubble without user interaction.
194 void ShowBubbleWithoutUserInteraction();
196 // content::WebContentsObserver:
197 void WebContentsDestroyed() override;
199 // Shows infobar which allows user to choose credentials. Placing this
200 // code to separate method allows mocking.
201 virtual void UpdateAndroidAccountChooserInfoBarVisibility();
203 // The wrapper around current state and data.
204 ManagePasswordsState passwords_data_;
206 // Used to measure the amount of time on a page; if it's less than some
207 // reasonable limit, then don't close the bubble upon navigation. We create
208 // (and destroy) the timer in DidNavigateMainFrame.
209 scoped_ptr<base::ElapsedTimer> timer_;
211 // Contains true if the bubble is to be popped up in the next call to
212 // UpdateBubbleAndIconVisibility().
213 bool should_pop_up_bubble_;
215 DISALLOW_COPY_AND_ASSIGN(ManagePasswordsUIController);
218 #endif // CHROME_BROWSER_UI_PASSWORDS_MANAGE_PASSWORDS_UI_CONTROLLER_H_