Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / passwords / password_manager_presenter.h
1 // Copyright 2013 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_PASSWORD_MANAGER_PRESENTER_H_
6 #define CHROME_BROWSER_UI_PASSWORDS_PASSWORD_MANAGER_PRESENTER_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "base/memory/scoped_vector.h"
12 #include "base/prefs/pref_member.h"
13 #include "components/password_manager/core/browser/password_store.h"
14 #include "components/password_manager/core/browser/password_store_consumer.h"
15
16 namespace autofill {
17 struct PasswordForm;
18 }
19
20 class PasswordUIView;
21
22 class Profile;
23
24 // Contains the common logic used by a PasswordUIView to
25 // interact with PasswordStore. It provides completion callbacks for
26 // PasswordStore operations and updates the view on PasswordStore changes.
27 class PasswordManagerPresenter
28     : public password_manager::PasswordStore::Observer {
29  public:
30   // |password_view| the UI view that owns this presenter, must not be NULL.
31   explicit PasswordManagerPresenter(PasswordUIView* password_view);
32   virtual ~PasswordManagerPresenter();
33
34   // PasswordStore::Observer implementation.
35   virtual void OnLoginsChanged(
36       const password_manager::PasswordStoreChangeList& changes) OVERRIDE;
37
38   // Repopulates the password and exception entries.
39   void UpdatePasswordLists();
40
41   void Initialize();
42
43   // Gets the password entry at |index|.
44   const autofill::PasswordForm* GetPassword(size_t index);
45
46   // Gets the password exception entry at |index|.
47   const autofill::PasswordForm* GetPasswordException(size_t index);
48
49   // Removes the saved password entry at |index|.
50   // |index| the entry index to be removed.
51   void RemoveSavedPassword(size_t index);
52
53   // Removes the saved password exception entry at |index|.
54   // |index| the entry index to be removed.
55   void RemovePasswordException(size_t index);
56
57   // Requests the plain text password for entry at |index| to be revealed.
58   // |index| The index of the entry.
59   void RequestShowPassword(size_t index);
60
61  private:
62   friend class PasswordManagerPresenterTest;
63
64   // Returns the password store associated with the currently active profile.
65   password_manager::PasswordStore* GetPasswordStore();
66
67   // Returns true if the user needs to be authenticated before a plaintext
68   // password is revealed.
69   bool IsAuthenticationRequired();
70
71   // Sets the password and exception list of the UI view.
72   void SetPasswordList();
73   void SetPasswordExceptionList();
74
75   // A short class to mediate requests to the password store.
76   class ListPopulater : public password_manager::PasswordStoreConsumer {
77    public:
78     explicit ListPopulater(PasswordManagerPresenter* page);
79     virtual ~ListPopulater();
80
81     // Send a query to the password store to populate a list.
82     virtual void Populate() = 0;
83
84    protected:
85     PasswordManagerPresenter* page_;
86   };
87
88   // A short class to mediate requests to the password store for passwordlist.
89   class PasswordListPopulater : public ListPopulater {
90    public:
91     explicit PasswordListPopulater(PasswordManagerPresenter* page);
92
93     // Send a query to the password store to populate a password list.
94     virtual void Populate() OVERRIDE;
95
96     // Send the password store's reply back to the handler.
97     virtual void OnGetPasswordStoreResults(
98         const std::vector<autofill::PasswordForm*>& results) OVERRIDE;
99   };
100
101   // A short class to mediate requests to the password store for exceptions.
102   class PasswordExceptionListPopulater : public ListPopulater {
103    public:
104     explicit PasswordExceptionListPopulater(PasswordManagerPresenter* page);
105
106     // Send a query to the password store to populate a passwordException list.
107     virtual void Populate() OVERRIDE;
108
109     // Send the password store's reply back to the handler.
110     virtual void OnGetPasswordStoreResults(
111         const std::vector<autofill::PasswordForm*>& results) OVERRIDE;
112   };
113
114   // Password store consumer for populating the password list and exceptions.
115   PasswordListPopulater populater_;
116   PasswordExceptionListPopulater exception_populater_;
117
118   ScopedVector<autofill::PasswordForm> password_list_;
119   ScopedVector<autofill::PasswordForm> password_exception_list_;
120
121   // Whether to show stored passwords or not.
122   BooleanPrefMember show_passwords_;
123
124   // Indicates whether or not the password manager should require the user to
125   // reauthenticate before revealing plaintext passwords.
126   bool require_reauthentication_;
127
128   // The last time the user was successfully authenticated.
129   // Used to determine whether or not to reveal plaintext passwords.
130   base::TimeTicks last_authentication_time_;
131
132   // UI view that owns this presenter.
133   PasswordUIView* password_view_;
134
135   DISALLOW_COPY_AND_ASSIGN(PasswordManagerPresenter);
136 };
137
138 #endif  // CHROME_BROWSER_UI_PASSWORDS_PASSWORD_MANAGER_PRESENTER_H_