autofill_external_delegate.h revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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_AUTOFILL_CORE_BROWSER_AUTOFILL_EXTERNAL_DELEGATE_H_
6#define COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_EXTERNAL_DELEGATE_H_
7
8#include <vector>
9
10#include "base/compiler_specific.h"
11#include "base/memory/weak_ptr.h"
12#include "base/strings/string16.h"
13#include "components/autofill/core/browser/autofill_popup_delegate.h"
14#include "components/autofill/core/browser/password_autofill_manager.h"
15#include "components/autofill/core/common/form_data.h"
16#include "components/autofill/core/common/form_field_data.h"
17#include "components/autofill/core/common/password_form_fill_data.h"
18#include "ui/gfx/rect.h"
19
20namespace gfx {
21class Rect;
22}
23
24namespace content {
25class RenderViewHost;
26class WebContents;
27}
28
29namespace autofill {
30
31class AutofillManager;
32
33// TODO(csharp): A lot of the logic in this class is copied from autofillagent.
34// Once Autofill is moved out of WebKit this class should be the only home for
35// this logic. See http://crbug.com/51644
36
37// Delegate for in-browser Autocomplete and Autofill display and selection.
38class AutofillExternalDelegate
39    : public AutofillPopupDelegate {
40 public:
41  // Creates an AutofillExternalDelegate for the specified contents; the second
42  // argument is an AutofillManager managing Autofill for that WebContents.
43  AutofillExternalDelegate(content::WebContents* web_contents,
44                           AutofillManager* autofill_manager);
45  virtual ~AutofillExternalDelegate();
46
47  // AutofillPopupDelegate implementation.
48  virtual void OnPopupShown(content::KeyboardListener* listener) OVERRIDE;
49  virtual void OnPopupHidden(content::KeyboardListener* listener) OVERRIDE;
50  virtual void DidSelectSuggestion(int identifier) OVERRIDE;
51  virtual void DidAcceptSuggestion(const base::string16& value,
52                                   int identifier) OVERRIDE;
53  virtual void RemoveSuggestion(const base::string16& value,
54                                int identifier) OVERRIDE;
55  virtual void ClearPreviewedForm() OVERRIDE;
56
57  // Records and associates a query_id with web form data.  Called
58  // when the renderer posts an Autofill query to the browser. |bounds|
59  // is window relative. |display_warning_if_disabled| tells us if we should
60  // display warnings (such as autofill is disabled, but had suggestions).
61  // We might not want to display the warning if a website has disabled
62  // Autocomplete because they have their own popup, and showing our popup
63  // on to of theirs would be a poor user experience.
64  virtual void OnQuery(int query_id,
65                       const FormData& form,
66                       const FormFieldData& field,
67                       const gfx::RectF& element_bounds,
68                       bool display_warning_if_disabled);
69
70  // Records query results and correctly formats them before sending them off
71  // to be displayed.  Called when an Autofill query result is available.
72  virtual void OnSuggestionsReturned(
73      int query_id,
74      const std::vector<base::string16>& autofill_values,
75      const std::vector<base::string16>& autofill_labels,
76      const std::vector<base::string16>& autofill_icons,
77      const std::vector<int>& autofill_unique_ids);
78
79  // Show password suggestions in the popup.
80  void OnShowPasswordSuggestions(const std::vector<base::string16>& suggestions,
81                                 const std::vector<base::string16>& realms,
82                                 const FormFieldData& field,
83                                 const gfx::RectF& bounds);
84
85  // Set the data list value associated with the current field.
86  void SetCurrentDataListValues(
87      const std::vector<base::string16>& autofill_values,
88      const std::vector<base::string16>& autofill_labels,
89      const std::vector<base::string16>& autofill_icons,
90      const std::vector<int>& autofill_unique_ids);
91
92  // Inform the delegate that the text field editing has ended. This is
93  // used to help record the metrics of when a new popup is shown.
94  void DidEndTextFieldEditing();
95
96  // Returns the delegate to its starting state by removing any page specific
97  // values or settings.
98  void Reset();
99
100  // Inform the Password Manager of a filled form.
101  void AddPasswordFormMapping(
102      const FormFieldData& form,
103      const PasswordFormFillData& fill_data);
104
105 protected:
106  content::WebContents* web_contents() { return web_contents_; }
107
108  base::WeakPtr<AutofillExternalDelegate> GetWeakPtr();
109
110 private:
111  // Fills the form with the Autofill data corresponding to |unique_id|.
112  // If |is_preview| is true then this is just a preview to show the user what
113  // would be selected and if |is_preview| is false then the user has selected
114  // this data.
115  void FillAutofillFormData(int unique_id, bool is_preview);
116
117  // Handle applying any Autofill warnings to the Autofill popup.
118  void ApplyAutofillWarnings(std::vector<base::string16>* autofill_values,
119                             std::vector<base::string16>* autofill_labels,
120                             std::vector<base::string16>* autofill_icons,
121                             std::vector<int>* autofill_unique_ids);
122
123  // Handle applying any Autofill option listings to the Autofill popup.
124  // This function should only get called when there is at least one
125  // multi-field suggestion in the list of suggestions.
126  void ApplyAutofillOptions(std::vector<base::string16>* autofill_values,
127                            std::vector<base::string16>* autofill_labels,
128                            std::vector<base::string16>* autofill_icons,
129                            std::vector<int>* autofill_unique_ids);
130
131  // Insert the data list values at the start of the given list, including
132  // any required separators.
133  void InsertDataListValues(std::vector<base::string16>* autofill_values,
134                            std::vector<base::string16>* autofill_labels,
135                            std::vector<base::string16>* autofill_icons,
136                            std::vector<int>* autofill_unique_ids);
137
138  // The web_contents associated with this delegate.
139  content::WebContents* web_contents_;  // weak; owns me.
140  AutofillManager* autofill_manager_;  // weak.
141
142  // Password Autofill manager, handles all password-related Autofilling.
143  PasswordAutofillManager password_autofill_manager_;
144
145  // The ID of the last request sent for form field Autofill.  Used to ignore
146  // out of date responses.
147  int autofill_query_id_;
148
149  // The current form and field selected by Autofill.
150  FormData autofill_query_form_;
151  FormFieldData autofill_query_field_;
152
153  // The bounds of the form field that user is interacting with.
154  gfx::RectF element_bounds_;
155
156  // Should we display a warning if Autofill is disabled?
157  bool display_warning_if_disabled_;
158
159  // Does the popup include any Autofill profile or credit card suggestions?
160  bool has_autofill_suggestion_;
161
162  // Have we already shown Autofill suggestions for the field the user is
163  // currently editing?  Used to keep track of state for metrics logging.
164  bool has_shown_autofill_popup_for_current_edit_;
165
166  // The RenderViewHost that this object has been registered with as a
167  // keyboard listener.
168  content::RenderViewHost* registered_keyboard_listener_with_;
169
170  // The current data list values.
171  std::vector<base::string16> data_list_values_;
172  std::vector<base::string16> data_list_labels_;
173  std::vector<base::string16> data_list_icons_;
174  std::vector<int> data_list_unique_ids_;
175
176  base::WeakPtrFactory<AutofillExternalDelegate> weak_ptr_factory_;
177
178  DISALLOW_COPY_AND_ASSIGN(AutofillExternalDelegate);
179};
180
181}  // namespace autofill
182
183#endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_EXTERNAL_DELEGATE_H_
184