password_autofill_manager.h revision cedac228d2dd51db4b79ea1e72c7f249408ee061
1// Copyright 2013 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_AUTOFILL_MANAGER_H_
6#define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_AUTOFILL_MANAGER_H_
7
8#include <map>
9
10#include "base/gtest_prod_util.h"
11#include "components/autofill/core/browser/autofill_manager_delegate.h"
12#include "components/autofill/core/browser/autofill_popup_delegate.h"
13#include "components/autofill/core/common/password_form_fill_data.h"
14
15namespace gfx {
16class RectF;
17}
18
19namespace password_manager {
20
21class PasswordManagerClient;
22
23// This class is responsible for filling password forms.
24class PasswordAutofillManager : public autofill::AutofillPopupDelegate {
25 public:
26  PasswordAutofillManager(
27      PasswordManagerClient* password_manager_client,
28      autofill::AutofillManagerDelegate* autofill_manager_delegate);
29  virtual ~PasswordAutofillManager();
30
31  // AutofillPopupDelegate implementation.
32  virtual void OnPopupShown() OVERRIDE;
33  virtual void OnPopupHidden() OVERRIDE;
34  virtual void DidSelectSuggestion(const base::string16& value,
35                                   int identifier) OVERRIDE;
36  virtual void DidAcceptSuggestion(const base::string16& value,
37                                   int identifier) OVERRIDE;
38  virtual void RemoveSuggestion(const base::string16& value,
39                                int identifier) OVERRIDE;
40  virtual void ClearPreviewedForm() OVERRIDE;
41
42  // Invoked when a password mapping is added.
43  void OnAddPasswordFormMapping(
44      const autofill::FormFieldData& field,
45      const autofill::PasswordFormFillData& fill_data);
46
47  // Handles a request from the renderer to show a popup with the given
48  // |suggestions| from the password manager.
49  void OnShowPasswordSuggestions(
50      const autofill::FormFieldData& field,
51      const gfx::RectF& bounds,
52      const std::vector<base::string16>& suggestions,
53      const std::vector<base::string16>& realms);
54
55  // Invoked to clear any page specific cached values.
56  void Reset();
57
58  // A public version of FillSuggestion(), only for use in tests.
59  bool FillSuggestionForTest(const autofill::FormFieldData& field,
60                             const base::string16& username);
61
62  // A public version of PreviewSuggestion(), only for use in tests.
63  bool PreviewSuggestionForTest(const autofill::FormFieldData& field,
64                                const base::string16& username);
65
66 private:
67  typedef std::map<autofill::FormFieldData, autofill::PasswordFormFillData>
68      LoginToPasswordInfoMap;
69
70  // Attempts to fill the password associated with user name |username|, and
71  // returns true if it was successful.
72  bool FillSuggestion(const autofill::FormFieldData& field,
73                      const base::string16& username);
74
75  // Attempts to preview the password associated with user name |username|, and
76  // returns true if it was successful.
77  bool PreviewSuggestion(const autofill::FormFieldData& field,
78                         const base::string16& username);
79
80  // If |current_username| matches a username for one of the login mappings in
81  // |fill_data|, returns true and assigns the password to |out_password|.
82  // Otherwise, returns false and leaves |out_password| untouched.
83  bool GetPasswordForUsername(
84      const base::string16& current_username,
85      const autofill::PasswordFormFillData& fill_data,
86      base::string16* out_password);
87
88  // Finds login information for a |node| that was previously filled.
89  bool FindLoginInfo(const autofill::FormFieldData& field,
90                     autofill::PasswordFormFillData* found_password);
91
92  // The logins we have filled so far with their associated info.
93  LoginToPasswordInfoMap login_to_password_info_;
94
95  // Provides embedder-level operations on passwords. Must outlive |this|.
96  PasswordManagerClient* const password_manager_client_;  // weak
97
98  autofill::AutofillManagerDelegate* const autofill_manager_delegate_;  // weak
99
100  // The form field on which the autofill popup is shown.
101  autofill::FormFieldData form_field_;
102
103  base::WeakPtrFactory<PasswordAutofillManager> weak_ptr_factory_;
104
105  DISALLOW_COPY_AND_ASSIGN(PasswordAutofillManager);
106};
107
108}  // namespace password_manager
109
110#endif  // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_PASSWORD_AUTOFILL_MANAGER_H_
111