1 // Copyright (c) 2011 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 #pragma once 8 9 #include "base/memory/scoped_ptr.h" 10 #include "base/stl_util-inl.h" 11 #include "chrome/browser/password_manager/password_form_manager.h" 12 #include "chrome/browser/prefs/pref_member.h" 13 #include "chrome/browser/ui/login/login_model.h" 14 #include "content/browser/tab_contents/tab_contents_observer.h" 15 #include "webkit/glue/password_form.h" 16 #include "webkit/glue/password_form_dom_manager.h" 17 18 class PasswordManagerDelegate; 19 class PasswordManagerTest; 20 class PasswordFormManager; 21 class PrefService; 22 23 // Per-tab password manager. Handles creation and management of UI elements, 24 // receiving password form data from the renderer and managing the password 25 // database through the WebDataService. The PasswordManager is a LoginModel 26 // for purposes of supporting HTTP authentication dialogs. 27 class PasswordManager : public LoginModel, 28 public TabContentsObserver { 29 public: 30 static void RegisterUserPrefs(PrefService* prefs); 31 32 // The delegate passed in is required to outlive the PasswordManager. 33 PasswordManager(TabContents* tab_contents, 34 PasswordManagerDelegate* delegate); 35 virtual ~PasswordManager(); 36 37 // Called by a PasswordFormManager when it decides a form can be autofilled 38 // on the page. 39 void Autofill(const webkit_glue::PasswordForm& form_for_autofill, 40 const webkit_glue::PasswordFormMap& best_matches, 41 const webkit_glue::PasswordForm* const preferred_match, 42 bool wait_for_username) const; 43 44 // LoginModel implementation. 45 virtual void SetObserver(LoginModelObserver* observer); 46 47 // When a form is submitted, we prepare to save the password but wait 48 // until we decide the user has successfully logged in. This is step 1 49 // of 2 (see SavePassword). 50 void ProvisionallySavePassword(webkit_glue::PasswordForm form); 51 52 // TabContentsObserver overrides. 53 virtual void DidStopLoading(); 54 virtual void DidNavigateAnyFramePostCommit( 55 const NavigationController::LoadCommittedDetails& details, 56 const ViewHostMsg_FrameNavigate_Params& params); 57 virtual bool OnMessageReceived(const IPC::Message& message); 58 59 void OnPasswordFormsFound( 60 const std::vector<webkit_glue::PasswordForm>& forms); 61 void OnPasswordFormsVisible( 62 const std::vector<webkit_glue::PasswordForm>& visible_forms); 63 64 private: 65 FRIEND_TEST_ALL_PREFIXES(PasswordManagerTest, FormSeenThenLeftPage); 66 67 // Note about how a PasswordFormManager can transition from 68 // pending_login_managers_ to provisional_save_manager_ and the infobar. 69 // 70 // 1. form "seen" 71 // | new 72 // | ___ Infobar 73 // pending_login -- form submit --> provisional_save ___/ 74 // ^ | \___ (update DB) 75 // | fail 76 // |-----------<------<---------| !new 77 // 78 // When a form is "seen" on a page, a PasswordFormManager is created 79 // and stored in this collection until user navigates away from page. 80 81 // Clear any pending saves 82 void ClearProvisionalSave(); 83 84 // Notification that the user navigated away from the current page. 85 // Unless this is a password form submission, for our purposes this 86 // means we're done with the current page, so we can clean-up. 87 void DidNavigate(); 88 89 typedef std::vector<PasswordFormManager*> LoginManagers; 90 LoginManagers pending_login_managers_; 91 92 // Deleter for pending_login_managers_ when PasswordManager is deleted (e.g 93 // tab closes) on a page with a password form, thus containing login managers. 94 STLElementDeleter<LoginManagers> login_managers_deleter_; 95 96 // When the user submits a password/credential, this contains the 97 // PasswordFormManager for the form in question until we deem the login 98 // attempt to have succeeded (as in valid credentials). If it fails, we 99 // send the PasswordFormManager back to the pending_login_managers_ set. 100 // Scoped in case PasswordManager gets deleted (e.g tab closes) between the 101 // time a user submits a login form and gets to the next page. 102 scoped_ptr<PasswordFormManager> provisional_save_manager_; 103 104 // Our delegate for carrying out external operations. This is typically the 105 // containing TabContents. 106 PasswordManagerDelegate* delegate_; 107 108 // The LoginModelObserver (i.e LoginView) requiring autofill. 109 LoginModelObserver* observer_; 110 111 // Set to false to disable the password manager (will no longer fill 112 // passwords or ask you if you want to save passwords). 113 BooleanPrefMember password_manager_enabled_; 114 115 DISALLOW_COPY_AND_ASSIGN(PasswordManager); 116 }; 117 118 #endif // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_MANAGER_H_ 119