autofill_external_delegate.h revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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/common/form_data.h"
15#include "components/autofill/core/common/form_field_data.h"
16#include "ui/gfx/rect.h"
17
18namespace autofill {
19
20class AutofillDriver;
21class AutofillManager;
22
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
26
27// Delegate for in-browser Autocomplete and Autofill display and selection.
28class AutofillExternalDelegate
29    : public AutofillPopupDelegate {
30 public:
31  // Creates an AutofillExternalDelegate for the specified AutofillManager and
32  // AutofillDriver.
33  AutofillExternalDelegate(AutofillManager* manager,
34                           AutofillDriver* driver);
35  virtual ~AutofillExternalDelegate();
36
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;
47
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,
56                       const FormData& form,
57                       const FormFieldData& field,
58                       const gfx::RectF& element_bounds,
59                       bool display_warning_if_disabled);
60
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(
64      int query_id,
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);
69
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);
74
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();
78
79  // Returns the delegate to its starting state by removing any page specific
80  // values or settings.
81  void Reset();
82
83  // The renderer sent an IPC acknowledging an earlier ping IPC.
84  void OnPingAck();
85
86 protected:
87  base::WeakPtr<AutofillExternalDelegate> GetWeakPtr();
88
89 private:
90  // Fills the form with the Autofill data corresponding to |unique_id|.
91  // If |is_preview| is true then this is just a preview to show the user what
92  // would be selected and if |is_preview| is false then the user has selected
93  // this data.
94  void FillAutofillFormData(int unique_id, bool is_preview);
95
96  // Handle applying any Autofill warnings to the Autofill popup.
97  void ApplyAutofillWarnings(std::vector<base::string16>* values,
98                             std::vector<base::string16>* labels,
99                             std::vector<base::string16>* icons,
100                             std::vector<int>* unique_ids);
101
102  // Handle applying any Autofill option listings to the Autofill popup.
103  // This function should only get called when there is at least one
104  // multi-field suggestion in the list of suggestions.
105  void ApplyAutofillOptions(std::vector<base::string16>* values,
106                            std::vector<base::string16>* labels,
107                            std::vector<base::string16>* icons,
108                            std::vector<int>* unique_ids);
109
110  // Insert the data list values at the start of the given list, including
111  // any required separators.
112  void InsertDataListValues(std::vector<base::string16>* values,
113                            std::vector<base::string16>* labels,
114                            std::vector<base::string16>* icons,
115                            std::vector<int>* unique_ids);
116
117#if defined(OS_MACOSX) && !defined(OS_IOS)
118  // Pings the renderer.
119  void PingRenderer();
120#endif  // defined(OS_MACOSX) && !defined(OS_IOS)
121
122  AutofillManager* manager_;  // weak.
123
124  // Provides driver-level context to the shared code of the component. Must
125  // outlive this object.
126  AutofillDriver* driver_;  // weak
127
128  // The ID of the last request sent for form field Autofill.  Used to ignore
129  // out of date responses.
130  int query_id_;
131
132  // The current form and field selected by Autofill.
133  FormData query_form_;
134  FormFieldData query_field_;
135
136  // The bounds of the form field that user is interacting with.
137  gfx::RectF element_bounds_;
138
139  // Should we display a warning if Autofill is disabled?
140  bool display_warning_if_disabled_;
141
142  // Does the popup include any Autofill profile or credit card suggestions?
143  bool has_suggestion_;
144
145  // Have we already shown Autofill suggestions for the field the user is
146  // currently editing?  Used to keep track of state for metrics logging.
147  bool has_shown_popup_for_current_edit_;
148
149  // The current data list values.
150  std::vector<base::string16> data_list_values_;
151  std::vector<base::string16> data_list_labels_;
152
153  base::WeakPtrFactory<AutofillExternalDelegate> weak_ptr_factory_;
154
155  DISALLOW_COPY_AND_ASSIGN(AutofillExternalDelegate);
156};
157
158}  // namespace autofill
159
160#endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_EXTERNAL_DELEGATE_H_
161