summaryrefslogtreecommitdiffstats
path: root/chrome/browser/autofill/autofill_external_delegate.h
blob: 8d853ad38b53f2eb69142993eeb6b807a3a103e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_AUTOFILL_AUTOFILL_EXTERNAL_DELEGATE_H_
#define CHROME_BROWSER_AUTOFILL_AUTOFILL_EXTERNAL_DELEGATE_H_

#include <vector>

#include "base/compiler_specific.h"
#include "base/string16.h"
#include "chrome/browser/autofill/password_autofill_manager.h"
#include "chrome/browser/ui/tab_contents/tab_contents.h"
#include "webkit/forms/form_data.h"
#include "webkit/forms/form_field.h"
#include "webkit/forms/password_form_dom_manager.h"

class AutofillManager;

namespace gfx {
class Rect;
}

namespace content {
class WebContents;
}

// TODO(csharp): A lot of the logic in this class is copied from autofillagent.
// Once Autofill is moved out of WebKit this class should be the only home for
// this logic. See http://crbug.com/51644

// Delegate for external processing of Autocomplete and Autofill
// display and selection.
class AutofillExternalDelegate {
 public:
  virtual ~AutofillExternalDelegate();

  // When using an external Autofill delegate.  Allows Chrome to tell
  // WebKit which Autofill selection has been chosen.
  virtual void SelectAutofillSuggestionAtIndex(int unique_id);

  // Records and associates a query_id with web form data.  Called
  // when the renderer posts an Autofill query to the browser. |bounds|
  // is window relative. |display_warning_if_disabled| tells us if we should
  // display warnings (such as autofill is disabled, but had suggestions).
  // We might not want to display the warning if a website has disabled
  // Autocomplete because they have their own popup, and showing our popup
  // on to of theirs would be a poor user experience.
  virtual void OnQuery(int query_id,
                       const webkit::forms::FormData& form,
                       const webkit::forms::FormField& field,
                       const gfx::Rect& bounds,
                       bool display_warning_if_disabled);

  // Records query results and correctly formats them before sending them off
  // to be displayed.  Called when an Autofill query result is available.
  virtual void OnSuggestionsReturned(
      int query_id,
      const std::vector<string16>& autofill_values,
      const std::vector<string16>& autofill_labels,
      const std::vector<string16>& autofill_icons,
      const std::vector<int>& autofill_unique_ids);

  // Show password suggestions in the popup.
  void OnShowPasswordSuggestions(const std::vector<string16>& suggestions,
                                 const webkit::forms::FormField& field,
                                 const gfx::Rect& bounds);

  // Set the data list value associated with the current field.
  void SetCurrentDataListValues(const std::vector<string16>& autofill_values,
                                const std::vector<string16>& autofill_labels,
                                const std::vector<string16>& autofill_icons,
                                const std::vector<int>& autofill_unique_ids);

  // Remove the given Autocomplete entry from the DB.
  virtual void RemoveAutocompleteEntry(const string16& value);

  // Remove the given Autofill profile or credit credit.
  virtual void RemoveAutofillProfileOrCreditCard(int unique_id);

  // Inform the delegate that the text field editing has ended, this is
  // used to help record the metrics of when a new popup is shown.
  void DidEndTextFieldEditing();

  // Inform the delegate that an autofill suggestion have been chosen. Returns
  // true if the suggestion was selected.
  bool DidAcceptAutofillSuggestions(const string16& value,
                                    int unique_id,
                                    unsigned index);

  // Informs the delegate that the Autofill previewed form should be cleared.
  virtual void ClearPreviewedForm();

  // Hide the Autofill poup.
  virtual void HideAutofillPopup();

  // Returns the delegate to its starting state by removing any page specific
  // values or settings.
  void Reset();

  // Inform the Password Manager of a filled form.
  void AddPasswordFormMapping(
      const webkit::forms::FormField& form,
      const webkit::forms::PasswordFormFillData& fill_data);

  // Platforms that wish to implement an external Autofill delegate
  // MUST implement this.  The 1st arg is the tab contents that owns
  // this delegate; the second is the Autofill manager owned by the
  // tab contents.
  static AutofillExternalDelegate* Create(TabContents*,
                                          AutofillManager*);
 protected:
  explicit AutofillExternalDelegate(TabContents* tab_contents,
                                    AutofillManager* autofill_manager);

  // Displays the the Autofill results to the user with an external
  // Autofill popup that lives completely in the browser.  The suggestions
  // have be correctly formatted by this point.
  virtual void ApplyAutofillSuggestions(
      const std::vector<string16>& autofill_values,
      const std::vector<string16>& autofill_labels,
      const std::vector<string16>& autofill_icons,
      const std::vector<int>& autofill_unique_ids) = 0;

  // Handle instance specific OnQueryCode.
  virtual void OnQueryPlatformSpecific(int query_id,
                                       const webkit::forms::FormData& form,
                                       const webkit::forms::FormField& field,
                                       const gfx::Rect& bounds) = 0;

  // Handle platform-dependent hiding.
  virtual void HideAutofillPopupInternal() = 0;

  // Set the bounds of the Autofill element being worked with.
  virtual void SetBounds(const gfx::Rect& bounds) = 0;

  // Return the profile that this autofill delegate is currently working with.
  Profile* profile() { return tab_contents_->profile(); }

  // Return the web_contents assoicated with this delegate.
  content::WebContents* web_contents() { return tab_contents_->web_contents(); }

 private:
  // Fills the form with the Autofill data corresponding to |unique_id|.
  // If |is_preview| is true then this is just a preview to show the user what
  // would be selected and if |is_preview| is false then the user has selected
  // this data.
  void FillAutofillFormData(int unique_id, bool is_preview);

  // Handle applying any Autofill warnings to the Autofill popup.
  void ApplyAutofillWarnings(std::vector<string16>* autofill_values,
                             std::vector<string16>* autofill_labels,
                             std::vector<string16>* autofill_icons,
                             std::vector<int>* autofill_unique_ids);

  // Handle applying any Autofill option listings to the Autofill popup.
  // This function should only get called when there is at least one
  // multi-field suggestion in the list of suggestions.
  void ApplyAutofillOptions(std::vector<string16>* autofill_values,
                            std::vector<string16>* autofill_labels,
                            std::vector<string16>* autofill_icons,
                            std::vector<int>* autofill_unique_ids);

  // Insert the data list values at the start of the given list, including
  // any required separators.
  void InsertDataListValues(std::vector<string16>* autofill_values,
                            std::vector<string16>* autofill_labels,
                            std::vector<string16>* autofill_icons,
                            std::vector<int>* autofill_unique_ids);

  TabContents* tab_contents_;  // weak; owns me.
  AutofillManager* autofill_manager_;  // weak.

  // Password Autofill manager, handles all password-related Autofilling.
  PasswordAutofillManager password_autofill_manager_;

  // The ID of the last request sent for form field Autofill.  Used to ignore
  // out of date responses.
  int autofill_query_id_;

  // The current form and field selected by Autofill.
  webkit::forms::FormData autofill_query_form_;
  webkit::forms::FormField autofill_query_field_;

  // Should we display a warning if Autofill is disabled?
  bool display_warning_if_disabled_;

  // Have we already shown Autofill suggestions for the field the user is
  // currently editing?  Used to keep track of state for metrics logging.
  bool has_shown_autofill_popup_for_current_edit_;

  // Used to indicate if a popup is currently being shown or not.
  bool popup_visible_;

  // The current data list values.
  std::vector<string16> data_list_values_;
  std::vector<string16> data_list_labels_;
  std::vector<string16> data_list_icons_;
  std::vector<int> data_list_unique_ids_;

  DISALLOW_COPY_AND_ASSIGN(AutofillExternalDelegate);
};

#endif  // CHROME_BROWSER_AUTOFILL_AUTOFILL_EXTERNAL_DELEGATE_H_