15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_FORM_MANAGER_H_
6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_FORM_MANAGER_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "build/build_config.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/stl_util.h"
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/autofill/core/browser/field_types.h"
1558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "components/autofill/core/common/password_form.h"
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/password_manager/core/browser/password_manager_driver.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/password_manager/core/browser/password_store.h"
185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "components/password_manager/core/browser/password_store_consumer.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class WebContents;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace content
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
24c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochnamespace password_manager {
25c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class PasswordManager;
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class PasswordManagerClient;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Per-password-form-{on-page, dialog} class responsible for interactions
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// between a given form, the per-tab PasswordManager, and the PasswordStore.
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class PasswordFormManager : public PasswordStoreConsumer {
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |password_manager| owns this object
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |form_on_page| is the form that may be submitted and could need login data.
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |ssl_valid| represents the security of the page containing observed_form,
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //           used to filter login results from database.
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PasswordFormManager(PasswordManager* password_manager,
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                      PasswordManagerClient* client,
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                      PasswordManagerDriver* driver,
4058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                      const autofill::PasswordForm& observed_form,
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      bool ssl_valid);
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~PasswordFormManager();
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Flags describing the result of comparing two forms as performed by
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // DoesMatch. Individual flags are only relevant for HTML forms, but
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // RESULT_COMPLETE_MATCH will also be returned to indicate non-HTML forms
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // completely matching.
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  enum MatchResultFlags {
49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    RESULT_NO_MATCH = 0,
50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    RESULT_MANDATORY_ATTRIBUTES_MATCH = 1 << 0,  // Bare minimum to be a match.
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    RESULT_ACTION_MATCH = 1 << 1,                // Action URLs match too.
52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    RESULT_COMPLETE_MATCH =
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        RESULT_MANDATORY_ATTRIBUTES_MATCH | RESULT_ACTION_MATCH
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Use MatchResultMask to contain combinations of MatchResultFlags values.
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // It's a signed int rather than unsigned to avoid signed/unsigned mismatch
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // caused by the enum values implicitly converting to signed int.
58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  typedef int MatchResultMask;
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  enum OtherPossibleUsernamesAction {
61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    ALLOW_OTHER_POSSIBLE_USERNAMES,
62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)    IGNORE_OTHER_POSSIBLE_USERNAMES
63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  };
64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Compares basic data of |observed_form_| with |form| and returns how much
66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // they match. The return value is a MatchResultMask bitmask.
67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  MatchResultMask DoesManage(const autofill::PasswordForm& form) const;
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Retrieves potential matching logins from the database.
705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |prompt_policy| indicates whether it's permissible to prompt the user to
715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // authorize access to locked passwords. This argument is only used on
725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // platforms that support prompting the user for access (such as Mac OS).
735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void FetchMatchingLoginsFromPasswordStore(
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      PasswordStore::AuthorizationPromptPolicy prompt_policy);
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Simple state-check to verify whether this object as received a callback
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // from the PasswordStore and completed its matching phase. Note that the
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // callback in question occurs on the same (and only) main thread from which
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // instances of this class are ever used, but it is required since it is
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // conceivable that a user (or ui test) could attempt to submit a login
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // prompt before the callback has occured, which would InvokeLater a call to
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // PasswordManager::ProvisionallySave, which would interact with this object
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // before the db has had time to answer with matching password entries.
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This is intended to be a one-time check; if the return value is false the
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // expectation is caller will give up. This clearly won't work if you put it
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in a loop and wait for matching to complete; you're (supposed to be) on
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the same thread!
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasCompletedMatching();
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Determines if the user opted to 'never remember' passwords for this form.
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsBlacklisted();
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Used by PasswordManager to determine whether or not to display
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // a SavePasswordBar when given the green light to save the PasswordForm
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // managed by this.
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsNewLogin();
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
98eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Returns true if the current pending credentials were found using
99eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // origin matching of the public suffix, instead of the signon realm of the
100eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // form.
101eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  bool IsPendingCredentialsPublicSuffixMatch();
102eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Checks if the form is a valid password form. Forms which lack password
1041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // field are not considered valid.
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasValidPasswordForm();
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // These functions are used to determine if this form has had it's password
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // auto generated by the browser.
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool HasGeneratedPassword();
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetHasGeneratedPassword();
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Determines if we need to autofill given the results of the query.
1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Takes ownership of the elements in |result|.
11458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void OnRequestDone(const std::vector<autofill::PasswordForm*>& result);
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void OnGetPasswordStoreResults(
11758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      const std::vector<autofill::PasswordForm*>& results) OVERRIDE;
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A user opted to 'never remember' passwords for this form.
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Blacklist it so that from now on when it is seen we ignore it.
12158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // TODO: Make this private once we switch to the new UI.
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void PermanentlyBlacklist();
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Sets whether the password form should use additional password
1255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // authentication if available before being used for autofill.
1265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void SetUseAdditionalPasswordAuthentication(
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      bool use_additional_authentication);
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If the user has submitted observed_form_, provisionally hold on to
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the submitted credentials until we are told by PasswordManager whether
131868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // or not the login was successful. |action| describes how we deal with
132868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // possible usernames. If |action| is ALLOW_OTHER_POSSIBLE_USERNAMES we will
133868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // treat a possible usernames match as a sign that our original heuristics
134868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // were wrong and that the user selected the correct username from the
135868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Autofill UI.
13658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void ProvisionallySave(const autofill::PasswordForm& credentials,
137868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                         OtherPossibleUsernamesAction action);
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Handles save-as-new or update of the form managed by this manager.
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Note the basic data of updated_credentials must match that of
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // observed_form_ (e.g DoesManage(pending_credentials_) == true).
14258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  // TODO: Make this private once we switch to the new UI.
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Save();
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Call these if/when we know the form submission worked or failed.
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // These routines are used to update internal statistics ("ActionsTaken").
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SubmitPassed();
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SubmitFailed();
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
150f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Returns the username associated with the credentials.
151a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  const base::string16& associated_username() const {
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return pending_credentials_.username_value;
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Returns the pending credentials.
156a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  const autofill::PasswordForm& pending_credentials() const {
157f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return pending_credentials_;
158f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
159f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
160f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Returns the best matches.
161116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const autofill::PasswordFormMap& best_matches() const {
162f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return best_matches_;
163f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  }
164f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
1655f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  const autofill::PasswordForm* preferred_match() const {
1665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return preferred_match_;
1675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  }
1685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the realm URL for the form managed my this manager.
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const std::string& realm() const {
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return pending_credentials_.signon_realm;
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class PasswordFormManagerTest;
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // ManagerAction - What does the manager do with this form? Either it
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // fills it, or it doesn't. If it doesn't fill it, that's either
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // because it has no match, or it is blacklisted, or it is disabled
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // via the AUTOCOMPLETE=off attribute. Note that if we don't have
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // an exact match, we still provide candidates that the user may
1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // end up choosing.
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum ManagerAction {
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kManagerActionNone = 0,
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kManagerActionAutofilled,
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kManagerActionBlacklisted,
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kManagerActionMax
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // UserAction - What does the user do with this form? If he or she
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // does nothing (either by accepting what the password manager did, or
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // by simply (not typing anything at all), you get None. If there were
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // multiple choices and the user selects one other than the default,
1944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // you get Choose, if user selects an entry from matching against the Public
195effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // Suffix List you get ChoosePslMatch, if the user types in a new value
196effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // for just the password you get OverridePassword, and if the user types in a
197effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // new value for the username and password you get
198effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // OverrideUsernameAndPassword.
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum UserAction {
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kUserActionNone = 0,
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kUserActionChoose,
2024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    kUserActionChoosePslMatch,
203effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    kUserActionOverridePassword,
204effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch    kUserActionOverrideUsernameAndPassword,
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kUserActionMax
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Result - What happens to the form?
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum SubmitResult {
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kSubmitResultNotSubmitted = 0,
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kSubmitResultFailed,
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kSubmitResultPassed,
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kSubmitResultMax
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The maximum number of combinations of the three preceding enums.
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This is used when recording the actions taken by the form in UMA.
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static const int kMaxNumActionsTaken = kManagerActionMax * kUserActionMax *
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                         kSubmitResultMax;
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Helper for OnGetPasswordStoreResults to determine whether or not
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the given result form is worth scoring.
2236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  bool ShouldIgnoreResult(const autofill::PasswordForm& form) const;
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper for Save in the case that best_matches.size() == 0, meaning
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // we have no prior record of this form/username/password and the user
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // has opted to 'Save Password'. If |reset_preferred_login| is set,
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the previously preferred login from |best_matches_| will be reset.
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SaveAsNewLogin(bool reset_preferred_login);
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Helper for OnGetPasswordStoreResults to score an individual result
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // against the observed_form_.
23358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  int ScoreResult(const autofill::PasswordForm& form) const;
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper for Save in the case that best_matches.size() > 0, meaning
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // we have at least one match for this form/username/password. This
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Updates the form managed by this object, as well as any matching forms
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // that now need to have preferred bit changed, since updated_credentials
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is now implicitly 'preferred'.
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UpdateLogin();
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
242d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Check to see if |pending| corresponds to an account creation form. If we
243d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // think that it does, we label it as such and upload this state to the
244d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Autofill server, so that we will trigger password generation in the future.
245d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  void CheckForAccountCreationForm(const autofill::PasswordForm& pending,
246d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                   const autofill::PasswordForm& observed);
247d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Update all login matches to reflect new preferred state - preferred flag
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // will be reset on all matched logins that different than the current
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |pending_credentials_|.
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UpdatePreferredLoginState(PasswordStore* password_store);
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
253868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Returns true if |username| is one of the other possible usernames for a
254868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // password form in |best_matches_| and sets |pending_credentials_| to the
255868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // match which had this username.
256868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool UpdatePendingCredentialsIfOtherPossibleUsername(
257a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      const base::string16& username);
258868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Converts the "ActionsTaken" fields into an int so they can be logged to
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // UMA.
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int GetActionsTaken();
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
263c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Remove possible_usernames that may contains sensitive information and
264c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // duplicates.
26558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  void SanitizePossibleUsernames(autofill::PasswordForm* form);
266c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Helper function to delegate uploading to the AutofillManager.
2681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual void UploadPasswordForm(
2691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      const autofill::FormData& form_data,
2701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      const autofill::ServerFieldType& password_type);
2711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set of PasswordForms from the DB that best match the form
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // being managed by this. Use a map instead of vector, because we most
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // frequently require lookups by username value in IsNewLogin.
27558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  autofill::PasswordFormMap best_matches_;
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Cleans up when best_matches_ goes out of scope.
27858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  STLValueDeleter<autofill::PasswordFormMap> best_matches_deleter_;
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
280116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // The PasswordForm from the page or dialog managed by |this|.
281116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const autofill::PasswordForm observed_form_;
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The origin url path of observed_form_ tokenized, for convenience when
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // scoring.
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<std::string> form_path_tokens_;
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Stores updated credentials when the form was submitted but success is
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // still unknown.
28958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  autofill::PasswordForm pending_credentials_;
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Whether pending_credentials_ stores a new login or is an update
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to an existing one.
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_new_login_;
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Whether this form has an auto generated password.
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool has_generated_password_;
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
298868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Set if the user has selected one of the other possible usernames in
299868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // |pending_credentials_|.
300a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 selected_username_;
301868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // PasswordManager owning this.
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const PasswordManager* const password_manager_;
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Convenience pointer to entry in best_matches_ that is marked
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // as preferred. This is only allowed to be null if there are no best matches
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // at all, since there will always be one preferred login when there are
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // multiple matches (when first saved, a login is marked preferred).
30958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  const autofill::PasswordForm* preferred_match_;
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef enum {
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PRE_MATCHING_PHASE,      // Have not yet invoked a GetLogins query to find
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             // matching login information from password store.
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MATCHING_PHASE,          // We've made a GetLogins request, but
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             // haven't received or finished processing result.
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    POST_MATCHING_PHASE      // We've queried the DB and processed matching
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             // login results.
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  } PasswordFormManagerState;
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // State of matching process, used to verify that we don't call methods
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // assuming we've already processed the request for matching logins,
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // when we actually haven't.
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  PasswordFormManagerState state_;
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The client which implements embedder-specific PasswordManager operations.
3265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PasswordManagerClient* client_;
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // The driver which implements platform-specific PasswordManager operations.
3295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  PasswordManagerDriver* driver_;
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // These three fields record the "ActionsTaken" by the browser and
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the user with this form, and the result. They are combined and
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // recorded in UMA when the manager is destroyed.
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ManagerAction manager_action_;
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  UserAction user_action_;
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SubmitResult submit_result_;
337d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(PasswordFormManager);
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
340c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
341c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}  // namespace password_manager
342c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
343a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif  // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_FORM_MANAGER_H_
344