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/gtest_prod_util.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/strings/string16.h"
14 #include "components/autofill/core/browser/autofill_popup_delegate.h"
15 #include "components/autofill/core/browser/suggestion.h"
16 #include "components/autofill/core/common/form_data.h"
17 #include "components/autofill/core/common/form_field_data.h"
18 #include "ui/gfx/geometry/rect.h"
23 class AutofillManager
;
25 // TODO(csharp): A lot of the logic in this class is copied from autofillagent.
26 // Once Autofill is moved out of WebKit this class should be the only home for
27 // this logic. See http://crbug.com/51644
29 // Delegate for in-browser Autocomplete and Autofill display and selection.
30 class AutofillExternalDelegate
: public AutofillPopupDelegate
{
32 // Creates an AutofillExternalDelegate for the specified AutofillManager and
34 AutofillExternalDelegate(AutofillManager
* manager
,
35 AutofillDriver
* driver
);
36 virtual ~AutofillExternalDelegate();
38 // AutofillPopupDelegate implementation.
39 void OnPopupShown() override
;
40 void OnPopupHidden() override
;
41 void DidSelectSuggestion(const base::string16
& value
,
42 int identifier
) override
;
43 void DidAcceptSuggestion(const base::string16
& value
,
44 int identifier
) override
;
45 void RemoveSuggestion(const base::string16
& value
, int identifier
) override
;
46 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
<Suggestion
>& suggestions
);
67 // Set the data list value associated with the current field.
68 void SetCurrentDataListValues(
69 const std::vector
<base::string16
>& data_list_values
,
70 const std::vector
<base::string16
>& data_list_labels
);
72 // Inform the delegate that the text field editing has ended. This is
73 // used to help record the metrics of when a new popup is shown.
74 void DidEndTextFieldEditing();
76 // Returns the delegate to its starting state by removing any page specific
77 // values or settings.
80 // The renderer sent an IPC acknowledging an earlier ping IPC.
84 base::WeakPtr
<AutofillExternalDelegate
> GetWeakPtr();
87 FRIEND_TEST_ALL_PREFIXES(AutofillExternalDelegateUnitTest
,
90 // Called when a credit card is scanned using device camera.
91 void OnCreditCardScanned(const base::string16
& card_number
,
95 // Fills the form with the Autofill data corresponding to |unique_id|.
96 // If |is_preview| is true then this is just a preview to show the user what
97 // would be selected and if |is_preview| is false then the user has selected
99 void FillAutofillFormData(int unique_id
, bool is_preview
);
101 // Handle applying any Autofill warnings to the Autofill popup.
102 void ApplyAutofillWarnings(std::vector
<Suggestion
>* suggestions
);
104 // Handle applying any Autofill option listings to the Autofill popup.
105 // This function should only get called when there is at least one
106 // multi-field suggestion in the list of suggestions.
107 void ApplyAutofillOptions(std::vector
<Suggestion
>* suggestions
);
109 // Insert the data list values at the start of the given list, including
110 // any required separators.
111 void InsertDataListValues(std::vector
<Suggestion
>* suggestions
);
113 #if defined(OS_MACOSX) && !defined(OS_IOS)
114 // Pings the renderer.
116 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
118 AutofillManager
* manager_
; // weak.
120 // Provides driver-level context to the shared code of the component. Must
121 // outlive this object.
122 AutofillDriver
* driver_
; // weak
124 // The ID of the last request sent for form field Autofill. Used to ignore
125 // out of date responses.
128 // The current form and field selected by Autofill.
129 FormData query_form_
;
130 FormFieldData query_field_
;
132 // The bounds of the form field that user is interacting with.
133 gfx::RectF element_bounds_
;
135 // Should we display a warning if Autofill is disabled?
136 bool display_warning_if_disabled_
;
138 // Does the popup include any Autofill profile or credit card suggestions?
139 bool has_suggestion_
;
141 // Have we already shown Autofill suggestions for the field the user is
142 // currently editing? Used to keep track of state for metrics logging.
143 bool has_shown_popup_for_current_edit_
;
146 bool should_show_scan_credit_card_
;
148 // Whether the access Address Book prompt has ever been shown for the current
149 // |query_form_|. This variable is only used on OSX.
150 bool has_shown_address_book_prompt
;
152 // The current data list values.
153 std::vector
<base::string16
> data_list_values_
;
154 std::vector
<base::string16
> data_list_labels_
;
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_