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