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 void OnPopupShown() override;
39 void OnPopupHidden() override;
40 void DidSelectSuggestion(const base::string16& value,
41 int identifier) override;
42 void DidAcceptSuggestion(const base::string16& value,
43 int identifier) override;
44 void RemoveSuggestion(const base::string16& value, int identifier) override;
45 void ClearPreviewedForm() override;
47 // Records and associates a query_id with web form data. Called
48 // when the renderer posts an Autofill query to the browser. |bounds|
49 // is window relative. |display_warning_if_disabled| tells us if we should
50 // display warnings (such as autofill is disabled, but had suggestions).
51 // We might not want to display the warning if a website has disabled
52 // Autocomplete because they have their own popup, and showing our popup
53 // on to of theirs would be a poor user experience.
54 virtual void OnQuery(int query_id,
56 const FormFieldData& field,
57 const gfx::RectF& element_bounds,
58 bool display_warning_if_disabled);
60 // Records query results and correctly formats them before sending them off
61 // to be displayed. Called when an Autofill query result is available.
62 virtual void OnSuggestionsReturned(
64 const std::vector<base::string16>& values,
65 const std::vector<base::string16>& labels,
66 const std::vector<base::string16>& icons,
67 const std::vector<int>& unique_ids);
69 // Set the data list value associated with the current field.
70 void SetCurrentDataListValues(
71 const std::vector<base::string16>& data_list_values,
72 const std::vector<base::string16>& data_list_labels);
74 // Inform the delegate that the text field editing has ended. This is
75 // used to help record the metrics of when a new popup is shown.
76 void DidEndTextFieldEditing();
78 // Returns the delegate to its starting state by removing any page specific
79 // values or settings.
82 // The renderer sent an IPC acknowledging an earlier ping IPC.
86 base::WeakPtr<AutofillExternalDelegate> GetWeakPtr();
89 // Fills the form with the Autofill data corresponding to |unique_id|.
90 // If |is_preview| is true then this is just a preview to show the user what
91 // would be selected and if |is_preview| is false then the user has selected
93 void FillAutofillFormData(int unique_id, bool is_preview);
95 // Handle applying any Autofill warnings to the Autofill popup.
96 void ApplyAutofillWarnings(std::vector<base::string16>* values,
97 std::vector<base::string16>* labels,
98 std::vector<base::string16>* icons,
99 std::vector<int>* unique_ids);
101 // Handle applying any Autofill option listings to the Autofill popup.
102 // This function should only get called when there is at least one
103 // multi-field suggestion in the list of suggestions.
104 void ApplyAutofillOptions(std::vector<base::string16>* values,
105 std::vector<base::string16>* labels,
106 std::vector<base::string16>* icons,
107 std::vector<int>* unique_ids);
109 // Insert the data list values at the start of the given list, including
110 // any required separators.
111 void InsertDataListValues(std::vector<base::string16>* values,
112 std::vector<base::string16>* labels,
113 std::vector<base::string16>* icons,
114 std::vector<int>* unique_ids);
116 #if defined(OS_MACOSX) && !defined(OS_IOS)
117 // Pings the renderer.
119 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
121 AutofillManager* manager_; // weak.
123 // Provides driver-level context to the shared code of the component. Must
124 // outlive this object.
125 AutofillDriver* driver_; // weak
127 // The ID of the last request sent for form field Autofill. Used to ignore
128 // out of date responses.
131 // The current form and field selected by Autofill.
132 FormData query_form_;
133 FormFieldData query_field_;
135 // The bounds of the form field that user is interacting with.
136 gfx::RectF element_bounds_;
138 // Should we display a warning if Autofill is disabled?
139 bool display_warning_if_disabled_;
141 // Does the popup include any Autofill profile or credit card suggestions?
142 bool has_suggestion_;
144 // Have we already shown Autofill suggestions for the field the user is
145 // currently editing? Used to keep track of state for metrics logging.
146 bool has_shown_popup_for_current_edit_;
148 // The current data list values.
149 std::vector<base::string16> data_list_values_;
150 std::vector<base::string16> data_list_labels_;
152 // Whether the access Address Book prompt has ever been shown for the current
153 // |query_form_|. This variable is only used on OSX.
154 bool has_shown_address_book_prompt;
156 base::WeakPtrFactory<AutofillExternalDelegate> weak_ptr_factory_;
158 DISALLOW_COPY_AND_ASSIGN(AutofillExternalDelegate);
161 } // namespace autofill
163 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_EXTERNAL_DELEGATE_H_