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.
5 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_EXTERNAL_DELEGATE_H_
6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_EXTERNAL_DELEGATE_H_
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/common/form_data.h"
15 #include "components/autofill/core/common/form_field_data.h"
16 #include "ui/gfx/rect.h"
21 class AutofillManager;
23 // TODO(csharp): A lot of the logic in this class is copied from autofillagent.
24 // Once Autofill is moved out of WebKit this class should be the only home for
25 // this logic. See http://crbug.com/51644
27 // Delegate for in-browser Autocomplete and Autofill display and selection.
28 class AutofillExternalDelegate
29 : public AutofillPopupDelegate {
31 // Creates an AutofillExternalDelegate for the specified AutofillManager and
33 AutofillExternalDelegate(AutofillManager* manager,
34 AutofillDriver* driver);
35 virtual ~AutofillExternalDelegate();
37 // AutofillPopupDelegate implementation.
38 virtual void OnPopupShown() OVERRIDE;
39 virtual void OnPopupHidden() OVERRIDE;
40 virtual void DidSelectSuggestion(const base::string16& value,
41 int identifier) OVERRIDE;
42 virtual void DidAcceptSuggestion(const base::string16& value,
43 int identifier) OVERRIDE;
44 virtual void RemoveSuggestion(const base::string16& value,
45 int identifier) OVERRIDE;
46 virtual void ClearPreviewedForm() OVERRIDE;
48 // Records and associates a query_id with web form data. Called
49 // when the renderer posts an Autofill query to the browser. |bounds|
50 // is window relative. |display_warning_if_disabled| tells us if we should
51 // display warnings (such as autofill is disabled, but had suggestions).
52 // We might not want to display the warning if a website has disabled
53 // Autocomplete because they have their own popup, and showing our popup
54 // on to of theirs would be a poor user experience.
55 virtual void OnQuery(int query_id,
57 const FormFieldData& field,
58 const gfx::RectF& element_bounds,
59 bool display_warning_if_disabled);
61 // Records query results and correctly formats them before sending them off
62 // to be displayed. Called when an Autofill query result is available.
63 virtual void OnSuggestionsReturned(
65 const std::vector<base::string16>& values,
66 const std::vector<base::string16>& labels,
67 const std::vector<base::string16>& icons,
68 const std::vector<int>& unique_ids);
70 // Set the data list value associated with the current field.
71 void SetCurrentDataListValues(
72 const std::vector<base::string16>& data_list_values,
73 const std::vector<base::string16>& data_list_labels);
75 // Inform the delegate that the text field editing has ended. This is
76 // used to help record the metrics of when a new popup is shown.
77 void DidEndTextFieldEditing();
79 // Returns the delegate to its starting state by removing any page specific
80 // values or settings.
84 base::WeakPtr<AutofillExternalDelegate> GetWeakPtr();
87 // Fills the form with the Autofill data corresponding to |unique_id|.
88 // If |is_preview| is true then this is just a preview to show the user what
89 // would be selected and if |is_preview| is false then the user has selected
91 void FillAutofillFormData(int unique_id, bool is_preview);
93 // Handle applying any Autofill warnings to the Autofill popup.
94 void ApplyAutofillWarnings(std::vector<base::string16>* values,
95 std::vector<base::string16>* labels,
96 std::vector<base::string16>* icons,
97 std::vector<int>* unique_ids);
99 // Handle applying any Autofill option listings to the Autofill popup.
100 // This function should only get called when there is at least one
101 // multi-field suggestion in the list of suggestions.
102 void ApplyAutofillOptions(std::vector<base::string16>* values,
103 std::vector<base::string16>* labels,
104 std::vector<base::string16>* icons,
105 std::vector<int>* unique_ids);
107 // Insert the data list values at the start of the given list, including
108 // any required separators.
109 void InsertDataListValues(std::vector<base::string16>* values,
110 std::vector<base::string16>* labels,
111 std::vector<base::string16>* icons,
112 std::vector<int>* unique_ids);
114 AutofillManager* manager_; // weak.
116 // Provides driver-level context to the shared code of the component. Must
117 // outlive this object.
118 AutofillDriver* driver_; // weak
120 // The ID of the last request sent for form field Autofill. Used to ignore
121 // out of date responses.
124 // The current form and field selected by Autofill.
125 FormData query_form_;
126 FormFieldData query_field_;
128 // The bounds of the form field that user is interacting with.
129 gfx::RectF element_bounds_;
131 // Should we display a warning if Autofill is disabled?
132 bool display_warning_if_disabled_;
134 // Does the popup include any Autofill profile or credit card suggestions?
135 bool has_suggestion_;
137 // Have we already shown Autofill suggestions for the field the user is
138 // currently editing? Used to keep track of state for metrics logging.
139 bool has_shown_popup_for_current_edit_;
141 // The current data list values.
142 std::vector<base::string16> data_list_values_;
143 std::vector<base::string16> data_list_labels_;
145 base::WeakPtrFactory<AutofillExternalDelegate> weak_ptr_factory_;
147 DISALLOW_COPY_AND_ASSIGN(AutofillExternalDelegate);
150 } // namespace autofill
152 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_EXTERNAL_DELEGATE_H_