Home | History | Annotate | Download | only in password_manager
      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 CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_MANAGER_H_
      6 #define CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_MANAGER_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/callback.h"
     11 #include "base/memory/scoped_ptr.h"
     12 #include "base/memory/scoped_vector.h"
     13 #include "base/observer_list.h"
     14 #include "base/prefs/pref_member.h"
     15 #include "base/stl_util.h"
     16 #include "chrome/browser/password_manager/password_form_manager.h"
     17 #include "chrome/browser/ui/login/login_model.h"
     18 #include "components/autofill/core/common/password_form.h"
     19 #include "components/autofill/core/common/password_form_fill_data.h"
     20 #include "content/public/browser/web_contents_observer.h"
     21 #include "content/public/browser/web_contents_user_data.h"
     22 
     23 class PasswordManagerDelegate;
     24 class PasswordManagerTest;
     25 class PasswordFormManager;
     26 class PrefRegistrySimple;
     27 
     28 namespace user_prefs {
     29 class PrefRegistrySyncable;
     30 }
     31 
     32 // Per-tab password manager. Handles creation and management of UI elements,
     33 // receiving password form data from the renderer and managing the password
     34 // database through the PasswordStore. The PasswordManager is a LoginModel
     35 // for purposes of supporting HTTP authentication dialogs.
     36 class PasswordManager : public LoginModel,
     37                         public content::WebContentsObserver,
     38                         public content::WebContentsUserData<PasswordManager> {
     39  public:
     40   static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
     41 #if defined(OS_WIN)
     42   static void RegisterLocalPrefs(PrefRegistrySimple* registry);
     43 #endif
     44   static void CreateForWebContentsAndDelegate(
     45       content::WebContents* contents,
     46       PasswordManagerDelegate* delegate);
     47   virtual ~PasswordManager();
     48 
     49   typedef base::Callback<void(const autofill::PasswordForm&)>
     50       PasswordSubmittedCallback;
     51 
     52   // There is no corresponding remove function as currently all of the
     53   // owners of these callbacks have sufficient lifetimes so that the callbacks
     54   // should always be valid when called.
     55   void AddSubmissionCallback(const PasswordSubmittedCallback& callback);
     56 
     57   // Is saving new data for password autofill enabled for the current profile?
     58   // For example, saving new data is disabled in Incognito mode, whereas filling
     59   // data is not.
     60   bool IsSavingEnabled() const;
     61 
     62   // Called by a PasswordFormManager when it decides a form can be autofilled
     63   // on the page.
     64   virtual void Autofill(const autofill::PasswordForm& form_for_autofill,
     65                         const autofill::PasswordFormMap& best_matches,
     66                         const autofill::PasswordForm& preferred_match,
     67                         bool wait_for_username) const;
     68 
     69   // LoginModel implementation.
     70   virtual void AddObserver(LoginModelObserver* observer) OVERRIDE;
     71   virtual void RemoveObserver(LoginModelObserver* observer) OVERRIDE;
     72 
     73   // Mark this form as having a generated password.
     74   void SetFormHasGeneratedPassword(const autofill::PasswordForm& form);
     75 
     76   // TODO(isherman): This should not be public, but is currently being used by
     77   // the LoginPrompt code.
     78   // When a form is submitted, we prepare to save the password but wait
     79   // until we decide the user has successfully logged in. This is step 1
     80   // of 2 (see SavePassword).
     81   void ProvisionallySavePassword(const autofill::PasswordForm& form);
     82 
     83   // content::WebContentsObserver overrides.
     84   virtual void DidNavigateMainFrame(
     85       const content::LoadCommittedDetails& details,
     86       const content::FrameNavigateParams& params) OVERRIDE;
     87   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     88 
     89   // TODO(isherman): This should not be public, but is currently being used by
     90   // the LoginPrompt code.
     91   void OnPasswordFormsParsed(
     92       const std::vector<autofill::PasswordForm>& forms);
     93   void OnPasswordFormsRendered(
     94       const std::vector<autofill::PasswordForm>& visible_forms);
     95 
     96  protected:
     97   // Subclassed for unit tests.
     98   PasswordManager(content::WebContents* web_contents,
     99                   PasswordManagerDelegate* delegate);
    100 
    101   // Handle notification that a password form was submitted.
    102   virtual void OnPasswordFormSubmitted(
    103       const autofill::PasswordForm& password_form);
    104 
    105  private:
    106   friend class content::WebContentsUserData<PasswordManager>;
    107 
    108   enum ProvisionalSaveFailure {
    109     SAVING_DISABLED,
    110     EMPTY_PASSWORD,
    111     NO_MATCHING_FORM,
    112     MATCHING_NOT_COMPLETE,
    113     FORM_BLACKLISTED,
    114     INVALID_FORM,
    115     AUTOCOMPLETE_OFF,
    116     MAX_FAILURE_VALUE
    117   };
    118 
    119   // Log failure for UMA. Logs additional metrics if the |form_origin|
    120   // corresponds to one of the top, explicitly monitored websites.
    121   void RecordFailure(ProvisionalSaveFailure failure,
    122                      const std::string& form_origin);
    123 
    124   // Possibly set up FieldTrial for testing other possible usernames. This only
    125   // happens if there are other_possible_usernames to be shown and the
    126   // experiment hasn't already been initialized. We setup the experiment at
    127   // such a late time because this experiment will only affect a small number
    128   // of users so we want to include a larger fraction of these users than the
    129   // normal 10%.
    130   void PossiblyInitializeUsernamesExperiment(
    131       const autofill::PasswordFormMap& matches) const;
    132 
    133   // Returns true if we can show possible usernames to users in cases where
    134   // the username for the form is ambigious.
    135   bool OtherPossibleUsernamesEnabled() const;
    136 
    137   // Returns true if we should show an infobar instead of automatically saving
    138   // the password, based on inspecting the state of |provisional_save_manager_|.
    139   bool ShouldShowSavePasswordInfoBar() const;
    140 
    141   // Note about how a PasswordFormManager can transition from
    142   // pending_login_managers_ to provisional_save_manager_ and the infobar.
    143   //
    144   // 1. form "seen"
    145   //       |                                             new
    146   //       |                                               ___ Infobar
    147   // pending_login -- form submit --> provisional_save ___/
    148   //             ^                            |           \___ (update DB)
    149   //             |                           fail
    150   //             |-----------<------<---------|          !new
    151   //
    152   // When a form is "seen" on a page, a PasswordFormManager is created
    153   // and stored in this collection until user navigates away from page.
    154 
    155   ScopedVector<PasswordFormManager> pending_login_managers_;
    156 
    157   // When the user submits a password/credential, this contains the
    158   // PasswordFormManager for the form in question until we deem the login
    159   // attempt to have succeeded (as in valid credentials). If it fails, we
    160   // send the PasswordFormManager back to the pending_login_managers_ set.
    161   // Scoped in case PasswordManager gets deleted (e.g tab closes) between the
    162   // time a user submits a login form and gets to the next page.
    163   scoped_ptr<PasswordFormManager> provisional_save_manager_;
    164 
    165   // Our delegate for carrying out external operations.  This is typically the
    166   // containing WebContents.
    167   PasswordManagerDelegate* const delegate_;
    168 
    169   // Set to false to disable the password manager (will no longer ask if you
    170   // want to save passwords but will continue to fill passwords).
    171   BooleanPrefMember password_manager_enabled_;
    172 
    173   // Observers to be notified of LoginModel events.  This is mutable to allow
    174   // notification in const member functions.
    175   mutable ObserverList<LoginModelObserver> observers_;
    176 
    177   // Callbacks to be notified when a password form has been submitted.
    178   std::vector<PasswordSubmittedCallback> submission_callbacks_;
    179 
    180   DISALLOW_COPY_AND_ASSIGN(PasswordManager);
    181 };
    182 
    183 #endif  // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_MANAGER_H_
    184