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