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