Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / components / password_manager / core / browser / password_form_manager.h
1 // Copyright (c) 2012 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 COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_FORM_MANAGER_H_
6 #define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_FORM_MANAGER_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "build/build_config.h"
12
13 #include "base/stl_util.h"
14 #include "components/autofill/core/common/password_form.h"
15 #include "components/password_manager/core/browser/password_manager_driver.h"
16 #include "components/password_manager/core/browser/password_store.h"
17 #include "components/password_manager/core/browser/password_store_consumer.h"
18
19 namespace content {
20 class WebContents;
21 }  // namespace content
22
23 class PasswordManager;
24 class PasswordManagerClient;
25
26 // Per-password-form-{on-page, dialog} class responsible for interactions
27 // between a given form, the per-tab PasswordManager, and the PasswordStore.
28 class PasswordFormManager : public PasswordStoreConsumer {
29  public:
30   // |password_manager| owns this object
31   // |form_on_page| is the form that may be submitted and could need login data.
32   // |ssl_valid| represents the security of the page containing observed_form,
33   //           used to filter login results from database.
34   PasswordFormManager(PasswordManager* password_manager,
35                       PasswordManagerClient* client,
36                       PasswordManagerDriver* driver,
37                       const autofill::PasswordForm& observed_form,
38                       bool ssl_valid);
39   virtual ~PasswordFormManager();
40
41   enum ActionMatch {
42     ACTION_MATCH_REQUIRED,
43     ACTION_MATCH_NOT_REQUIRED
44   };
45
46   enum OtherPossibleUsernamesAction {
47     ALLOW_OTHER_POSSIBLE_USERNAMES,
48     IGNORE_OTHER_POSSIBLE_USERNAMES
49   };
50
51   // Compare basic data of observed_form_ with argument. Only check the action
52   // URL when action match is required.
53   bool DoesManage(const autofill::PasswordForm& form,
54                   ActionMatch action_match) const;
55
56   // Retrieves potential matching logins from the database.
57   // |prompt_policy| indicates whether it's permissible to prompt the user to
58   // authorize access to locked passwords. This argument is only used on
59   // platforms that support prompting the user for access (such as Mac OS).
60   void FetchMatchingLoginsFromPasswordStore(
61       PasswordStore::AuthorizationPromptPolicy prompt_policy);
62
63   // Simple state-check to verify whether this object as received a callback
64   // from the PasswordStore and completed its matching phase. Note that the
65   // callback in question occurs on the same (and only) main thread from which
66   // instances of this class are ever used, but it is required since it is
67   // conceivable that a user (or ui test) could attempt to submit a login
68   // prompt before the callback has occured, which would InvokeLater a call to
69   // PasswordManager::ProvisionallySave, which would interact with this object
70   // before the db has had time to answer with matching password entries.
71   // This is intended to be a one-time check; if the return value is false the
72   // expectation is caller will give up. This clearly won't work if you put it
73   // in a loop and wait for matching to complete; you're (supposed to be) on
74   // the same thread!
75   bool HasCompletedMatching();
76
77   // Determines if the user opted to 'never remember' passwords for this form.
78   bool IsBlacklisted();
79
80   // Used by PasswordManager to determine whether or not to display
81   // a SavePasswordBar when given the green light to save the PasswordForm
82   // managed by this.
83   bool IsNewLogin();
84
85   // Returns true if the current pending credentials were found using
86   // origin matching of the public suffix, instead of the signon realm of the
87   // form.
88   bool IsPendingCredentialsPublicSuffixMatch();
89
90   // Checks if the form is a valid password form. Forms which lack either
91   // login or password field are not considered valid.
92   bool HasValidPasswordForm();
93
94   // These functions are used to determine if this form has had it's password
95   // auto generated by the browser.
96   bool HasGeneratedPassword();
97   void SetHasGeneratedPassword();
98
99   // Determines if we need to autofill given the results of the query.
100   // Takes ownership of the elements in |result|.
101   void OnRequestDone(const std::vector<autofill::PasswordForm*>& result);
102
103   virtual void OnGetPasswordStoreResults(
104       const std::vector<autofill::PasswordForm*>& results) OVERRIDE;
105
106   // A user opted to 'never remember' passwords for this form.
107   // Blacklist it so that from now on when it is seen we ignore it.
108   // TODO: Make this private once we switch to the new UI.
109   void PermanentlyBlacklist();
110
111   // Sets whether the password form should use additional password
112   // authentication if available before being used for autofill.
113   void SetUseAdditionalPasswordAuthentication(
114       bool use_additional_authentication);
115
116   // If the user has submitted observed_form_, provisionally hold on to
117   // the submitted credentials until we are told by PasswordManager whether
118   // or not the login was successful. |action| describes how we deal with
119   // possible usernames. If |action| is ALLOW_OTHER_POSSIBLE_USERNAMES we will
120   // treat a possible usernames match as a sign that our original heuristics
121   // were wrong and that the user selected the correct username from the
122   // Autofill UI.
123   void ProvisionallySave(const autofill::PasswordForm& credentials,
124                          OtherPossibleUsernamesAction action);
125
126   // Handles save-as-new or update of the form managed by this manager.
127   // Note the basic data of updated_credentials must match that of
128   // observed_form_ (e.g DoesManage(pending_credentials_) == true).
129   // TODO: Make this private once we switch to the new UI.
130   void Save();
131
132   // Call these if/when we know the form submission worked or failed.
133   // These routines are used to update internal statistics ("ActionsTaken").
134   void SubmitPassed();
135   void SubmitFailed();
136
137   // Returns the username associated with the credentials.
138   const base::string16& associated_username() const {
139     return pending_credentials_.username_value;
140   }
141
142   // Returns the pending credentials.
143   const autofill::PasswordForm pending_credentials() const {
144     return pending_credentials_;
145   }
146
147   // Returns the best matches.
148   const autofill::PasswordFormMap best_matches() const {
149     return best_matches_;
150   }
151
152   // Returns the realm URL for the form managed my this manager.
153   const std::string& realm() const {
154     return pending_credentials_.signon_realm;
155   }
156
157  private:
158   friend class PasswordFormManagerTest;
159
160   // ManagerAction - What does the manager do with this form? Either it
161   // fills it, or it doesn't. If it doesn't fill it, that's either
162   // because it has no match, or it is blacklisted, or it is disabled
163   // via the AUTOCOMPLETE=off attribute. Note that if we don't have
164   // an exact match, we still provide candidates that the user may
165   // end up choosing.
166   enum ManagerAction {
167     kManagerActionNone = 0,
168     kManagerActionAutofilled,
169     kManagerActionBlacklisted,
170     kManagerActionMax
171   };
172
173   // UserAction - What does the user do with this form? If he or she
174   // does nothing (either by accepting what the password manager did, or
175   // by simply (not typing anything at all), you get None. If there were
176   // multiple choices and the user selects one other than the default,
177   // you get Choose, if user selects an entry from matching against the Public
178   // Suffix List you get ChoosePslMatch, if the user types in a new value
179   // for just the password you get OverridePassword, and if the user types in a
180   // new value for the username and password you get
181   // OverrideUsernameAndPassword.
182   enum UserAction {
183     kUserActionNone = 0,
184     kUserActionChoose,
185     kUserActionChoosePslMatch,
186     kUserActionOverridePassword,
187     kUserActionOverrideUsernameAndPassword,
188     kUserActionMax
189   };
190
191   // Result - What happens to the form?
192   enum SubmitResult {
193     kSubmitResultNotSubmitted = 0,
194     kSubmitResultFailed,
195     kSubmitResultPassed,
196     kSubmitResultMax
197   };
198
199   // The maximum number of combinations of the three preceding enums.
200   // This is used when recording the actions taken by the form in UMA.
201   static const int kMaxNumActionsTaken = kManagerActionMax * kUserActionMax *
202                                          kSubmitResultMax;
203
204   // Helper for OnGetPasswordStoreResults to determine whether or not
205   // the given result form is worth scoring.
206   bool IgnoreResult(const autofill::PasswordForm& form) const;
207
208   // Helper for Save in the case that best_matches.size() == 0, meaning
209   // we have no prior record of this form/username/password and the user
210   // has opted to 'Save Password'. If |reset_preferred_login| is set,
211   // the previously preferred login from |best_matches_| will be reset.
212   void SaveAsNewLogin(bool reset_preferred_login);
213
214   // Helper for OnGetPasswordStoreResults to score an individual result
215   // against the observed_form_.
216   int ScoreResult(const autofill::PasswordForm& form) const;
217
218   // Helper for Save in the case that best_matches.size() > 0, meaning
219   // we have at least one match for this form/username/password. This
220   // Updates the form managed by this object, as well as any matching forms
221   // that now need to have preferred bit changed, since updated_credentials
222   // is now implicitly 'preferred'.
223   void UpdateLogin();
224
225   // Check to see if |pending| corresponds to an account creation form. If we
226   // think that it does, we label it as such and upload this state to the
227   // Autofill server, so that we will trigger password generation in the future.
228   void CheckForAccountCreationForm(const autofill::PasswordForm& pending,
229                                    const autofill::PasswordForm& observed);
230
231   // Update all login matches to reflect new preferred state - preferred flag
232   // will be reset on all matched logins that different than the current
233   // |pending_credentials_|.
234   void UpdatePreferredLoginState(PasswordStore* password_store);
235
236   // Returns true if |username| is one of the other possible usernames for a
237   // password form in |best_matches_| and sets |pending_credentials_| to the
238   // match which had this username.
239   bool UpdatePendingCredentialsIfOtherPossibleUsername(
240       const base::string16& username);
241
242   // Converts the "ActionsTaken" fields into an int so they can be logged to
243   // UMA.
244   int GetActionsTaken();
245
246   // Remove possible_usernames that may contains sensitive information and
247   // duplicates.
248   void SanitizePossibleUsernames(autofill::PasswordForm* form);
249
250   // Set of PasswordForms from the DB that best match the form
251   // being managed by this. Use a map instead of vector, because we most
252   // frequently require lookups by username value in IsNewLogin.
253   autofill::PasswordFormMap best_matches_;
254
255   // Cleans up when best_matches_ goes out of scope.
256   STLValueDeleter<autofill::PasswordFormMap> best_matches_deleter_;
257
258   // The PasswordForm from the page or dialog managed by this.
259   autofill::PasswordForm observed_form_;
260
261   // The origin url path of observed_form_ tokenized, for convenience when
262   // scoring.
263   std::vector<std::string> form_path_tokens_;
264
265   // Stores updated credentials when the form was submitted but success is
266   // still unknown.
267   autofill::PasswordForm pending_credentials_;
268
269   // Whether pending_credentials_ stores a new login or is an update
270   // to an existing one.
271   bool is_new_login_;
272
273   // Whether this form has an auto generated password.
274   bool has_generated_password_;
275
276   // Set if the user has selected one of the other possible usernames in
277   // |pending_credentials_|.
278   base::string16 selected_username_;
279
280   // PasswordManager owning this.
281   const PasswordManager* const password_manager_;
282
283   // Convenience pointer to entry in best_matches_ that is marked
284   // as preferred. This is only allowed to be null if there are no best matches
285   // at all, since there will always be one preferred login when there are
286   // multiple matches (when first saved, a login is marked preferred).
287   const autofill::PasswordForm* preferred_match_;
288
289   typedef enum {
290     PRE_MATCHING_PHASE,      // Have not yet invoked a GetLogins query to find
291                              // matching login information from password store.
292     MATCHING_PHASE,          // We've made a GetLogins request, but
293                              // haven't received or finished processing result.
294     POST_MATCHING_PHASE      // We've queried the DB and processed matching
295                              // login results.
296   } PasswordFormManagerState;
297
298   // State of matching process, used to verify that we don't call methods
299   // assuming we've already processed the request for matching logins,
300   // when we actually haven't.
301   PasswordFormManagerState state_;
302
303   // The client which implements embedder-specific PasswordManager operations.
304   PasswordManagerClient* client_;
305
306   // The driver which implements platform-specific PasswordManager operations.
307   PasswordManagerDriver* driver_;
308
309   // These three fields record the "ActionsTaken" by the browser and
310   // the user with this form, and the result. They are combined and
311   // recorded in UMA when the manager is destroyed.
312   ManagerAction manager_action_;
313   UserAction user_action_;
314   SubmitResult submit_result_;
315
316   DISALLOW_COPY_AND_ASSIGN(PasswordFormManager);
317 };
318 #endif  // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_FORM_MANAGER_H_