Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / passwords / manage_passwords_ui_controller.h
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.
4
5 #ifndef CHROME_BROWSER_UI_PASSWORDS_MANAGE_PASSWORDS_UI_CONTROLLER_H_
6 #define CHROME_BROWSER_UI_PASSWORDS_MANAGE_PASSWORDS_UI_CONTROLLER_H_
7
8 #include "base/gtest_prod_util.h"
9 #include "base/memory/scoped_vector.h"
10 #include "base/timer/elapsed_timer.h"
11 #include "components/autofill/core/common/password_form.h"
12 #include "components/password_manager/core/browser/password_form_manager.h"
13 #include "components/password_manager/core/browser/password_store.h"
14 #include "components/password_manager/core/browser/password_store_change.h"
15 #include "components/password_manager/core/common/password_manager_ui.h"
16 #include "content/public/browser/navigation_details.h"
17 #include "content/public/browser/web_contents_observer.h"
18 #include "content/public/browser/web_contents_user_data.h"
19
20 namespace content {
21 class WebContents;
22 }
23
24 class ManagePasswordsIcon;
25
26 // Per-tab class to control the Omnibox password icon and bubble.
27 class ManagePasswordsUIController
28     : public content::WebContentsObserver,
29       public content::WebContentsUserData<ManagePasswordsUIController>,
30       public password_manager::PasswordStore::Observer {
31  public:
32   virtual ~ManagePasswordsUIController();
33
34   // Called when the user submits a form containing login information, so we
35   // can handle later requests to save or blacklist that login information.
36   // This stores the provided object in form_manager_ and triggers the UI to
37   // prompt the user about whether they would like to save the password.
38   void OnPasswordSubmitted(
39       scoped_ptr<password_manager::PasswordFormManager> form_manager);
40
41   // Called when the password will be saved automatically, but we still wish to
42   // visually inform the user that the save has occured.
43   void OnAutomaticPasswordSave(
44       scoped_ptr<password_manager::PasswordFormManager> form_manager);
45
46   // Called when a form is autofilled with login information, so we can manage
47   // password credentials for the current site which are stored in
48   // |password_form_map|. This stores a copy of |password_form_map| and shows
49   // the manage password icon.
50   void OnPasswordAutofilled(const autofill::PasswordFormMap& password_form_map);
51
52   // Called when a form is _not_ autofilled due to user blacklisting. This
53   // stores a copy of |password_form_map| so that we can offer the user the
54   // ability to reenable the manager for this form.
55   void OnBlacklistBlockedAutofill(
56       const autofill::PasswordFormMap& password_form_map);
57
58   // PasswordStore::Observer implementation.
59   virtual void OnLoginsChanged(
60       const password_manager::PasswordStoreChangeList& changes) OVERRIDE;
61
62   // Called from the model when the user chooses to save a password; passes the
63   // action off to the FormManager. The controller MUST be in a pending state,
64   // and WILL be in MANAGE_STATE after this method executes.
65   virtual void SavePassword();
66
67   // Called from the model when the user chooses to never save passwords; passes
68   // the action off to the FormManager. The controller MUST be in a pending
69   // state, and WILL be in BLACKLIST_STATE after this method executes.
70   virtual void NeverSavePassword();
71
72   // Called from the model when the user chooses to unblacklist the site. The
73   // controller MUST be in BLACKLIST_STATE, and WILL be in MANAGE_STATE after
74   // this method executes.
75   virtual void UnblacklistSite();
76
77   // Open a new tab, pointing to the password manager settings page.
78   virtual void NavigateToPasswordManagerSettingsPage();
79
80   // Open a new tab, pointing to the Google manage passwords website.
81   // TODO(gcasto): Change this to navigate to account central once passwords
82   // are visible there. Currently goes to the Chrome support page.
83   virtual void NavigateToAccountCentralManagementPage();
84
85   virtual const autofill::PasswordForm& PendingCredentials() const;
86
87   // Set the state of the Omnibox icon, and possibly show the associated bubble
88   // without user interaction.
89   virtual void UpdateIconAndBubbleState(ManagePasswordsIcon* icon);
90
91   password_manager::ui::State state() const { return state_; }
92
93   // True if a password is sitting around, waiting for a user to decide whether
94   // or not to save it.
95   bool PasswordPendingUserDecision() const;
96
97   const autofill::ConstPasswordFormMap& best_matches() const {
98     return password_form_map_;
99   }
100
101   const GURL& origin() const { return origin_; }
102
103  protected:
104   explicit ManagePasswordsUIController(
105       content::WebContents* web_contents);
106
107   // The pieces of saving and blacklisting passwords that interact with
108   // FormManager, split off into internal functions for testing/mocking.
109   virtual void SavePasswordInternal();
110   virtual void NeverSavePasswordInternal();
111
112   // content::WebContentsObserver:
113   virtual void DidNavigateMainFrame(
114       const content::LoadCommittedDetails& details,
115       const content::FrameNavigateParams& params) OVERRIDE;
116
117   // We create copies of PasswordForm objects that come in with unclear lifetime
118   // and store them in this vector as well as in |password_form_map_| to ensure
119   // that we destroy them correctly. If |new_password_forms_| gets cleared then
120   // |password_form_map_| is to be cleared too.
121   ScopedVector<autofill::PasswordForm> new_password_forms_;
122
123   // All previously stored credentials for a specific site.
124   // Protected, not private, so we can mess with the value in
125   // ManagePasswordsUIControllerMock.
126   autofill::ConstPasswordFormMap password_form_map_;
127
128   // The current state of the password manager. Protected so we can manipulate
129   // the value in tests.
130   password_manager::ui::State state_;
131
132   // Used to measure the amount of time on a page; if it's less than some
133   // reasonable limit, then don't close the bubble upon navigation. We create
134   // (and destroy) the timer in DidNavigateMainFrame.
135   scoped_ptr<base::ElapsedTimer> timer_;
136
137  private:
138   friend class content::WebContentsUserData<ManagePasswordsUIController>;
139
140   // Shows the password bubble without user interaction. The controller MUST
141   // be in PENDING_PASSWORD_AND_BUBBLE_STATE.
142   void ShowBubbleWithoutUserInteraction();
143
144   // Called when a passwordform is autofilled, when a new passwordform is
145   // submitted, or when a navigation occurs to update the visibility of the
146   // manage passwords icon and bubble.
147   void UpdateBubbleAndIconVisibility();
148
149   // content::WebContentsObserver:
150   virtual void WebContentsDestroyed() OVERRIDE;
151
152   // Set by OnPasswordSubmitted() when the user submits a form containing login
153   // information.  If the user responds to a subsequent "Do you want to save
154   // this password?" prompt, we ask this object to save or blacklist the
155   // associated login information in Chrome's password store.
156   scoped_ptr<password_manager::PasswordFormManager> form_manager_;
157
158   // Stores whether autofill was blocked due to a user's decision to blacklist
159   // the current site ("Never save passwords for this site").
160   bool autofill_blocked_;
161
162   // The origin of the form we're currently dealing with; we'll use this to
163   // determine which PasswordStore changes we should care about when updating
164   // |password_form_map_|.
165   GURL origin_;
166
167   DISALLOW_COPY_AND_ASSIGN(ManagePasswordsUIController);
168 };
169
170 #endif  // CHROME_BROWSER_UI_PASSWORDS_MANAGE_PASSWORDS_UI_CONTROLLER_H_