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