OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/utf_string_conversions.h" | 5 #include "base/utf_string_conversions.h" |
6 #include "chrome/browser/autofill/autofill_external_delegate.h" | 6 #include "chrome/browser/autofill/autofill_external_delegate.h" |
7 #include "chrome/browser/autofill/autofill_manager.h" | 7 #include "chrome/browser/autofill/autofill_manager.h" |
8 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 8 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
9 #include "chrome/common/autofill_messages.h" | 9 #include "chrome/common/autofill_messages.h" |
10 #include "chrome/common/chrome_constants.h" | 10 #include "chrome/common/chrome_constants.h" |
11 #include "content/public/browser/render_view_host.h" | 11 #include "content/public/browser/render_view_host.h" |
12 #include "content/public/browser/web_contents.h" | 12 #include "content/public/browser/web_contents.h" |
13 #include "grit/chromium_strings.h" | 13 #include "grit/chromium_strings.h" |
14 #include "grit/generated_resources.h" | 14 #include "grit/generated_resources.h" |
15 #include "ui/base/l10n/l10n_util.h" | 15 #include "ui/base/l10n/l10n_util.h" |
16 | 16 |
17 using content::RenderViewHost; | 17 using content::RenderViewHost; |
18 | 18 |
19 namespace { | |
20 | |
21 // The value to give as the unique id for all warnings. | |
22 const int kWarningId = -1; | |
23 | |
24 // The value to give as the unique id for all password entries. | |
25 const int kPasswordEntryId = -2; | |
26 | |
27 } // namespace | |
28 | |
19 AutofillExternalDelegate::~AutofillExternalDelegate() { | 29 AutofillExternalDelegate::~AutofillExternalDelegate() { |
20 } | 30 } |
21 | 31 |
22 AutofillExternalDelegate::AutofillExternalDelegate( | 32 AutofillExternalDelegate::AutofillExternalDelegate( |
23 TabContentsWrapper* tab_contents_wrapper, | 33 TabContentsWrapper* tab_contents_wrapper, |
24 AutofillManager* autofill_manager) | 34 AutofillManager* autofill_manager) |
25 : tab_contents_wrapper_(tab_contents_wrapper), | 35 : tab_contents_wrapper_(tab_contents_wrapper), |
26 autofill_manager_(autofill_manager), | 36 autofill_manager_(autofill_manager), |
37 password_autofill_manager_(tab_contents_wrapper->web_contents()), | |
27 autofill_query_id_(0), | 38 autofill_query_id_(0), |
28 display_warning_if_disabled_(false), | 39 display_warning_if_disabled_(false), |
29 has_shown_autofill_popup_for_current_edit_(false), | 40 has_shown_autofill_popup_for_current_edit_(false), |
30 suggestions_clear_index_(-1), | 41 suggestions_clear_index_(-1), |
31 suggestions_options_index_(-1) { | 42 suggestions_options_index_(-1) { |
32 } | 43 } |
33 | 44 |
34 void AutofillExternalDelegate::SelectAutofillSuggestionAtIndex(int unique_id, | 45 void AutofillExternalDelegate::SelectAutofillSuggestionAtIndex(int unique_id, |
35 int list_index) { | 46 int list_index) { |
47 if (password_autofill_manager_.DidSelectAutofillSuggestion( | |
48 autofill_query_field_)) | |
49 return; | |
50 | |
36 if (list_index == suggestions_options_index_ || | 51 if (list_index == suggestions_options_index_ || |
37 list_index == suggestions_clear_index_ || | 52 list_index == suggestions_clear_index_ || |
38 unique_id == -1) | 53 unique_id == kWarningId) |
39 return; | 54 return; |
40 | 55 |
41 FillAutofillFormData(unique_id, true); | 56 FillAutofillFormData(unique_id, true); |
42 } | 57 } |
43 | 58 |
44 void AutofillExternalDelegate::OnQuery(int query_id, | 59 void AutofillExternalDelegate::OnQuery(int query_id, |
45 const webkit::forms::FormData& form, | 60 const webkit::forms::FormData& form, |
46 const webkit::forms::FormField& field, | 61 const webkit::forms::FormField& field, |
47 const gfx::Rect& bounds, | 62 const gfx::Rect& bounds, |
48 bool display_warning_if_disabled) { | 63 bool display_warning_if_disabled) { |
(...skipping 25 matching lines...) Expand all Loading... | |
74 std::vector<string16> i(icons); | 89 std::vector<string16> i(icons); |
75 std::vector<int> ids(unique_ids); | 90 std::vector<int> ids(unique_ids); |
76 int separator_index = -1; | 91 int separator_index = -1; |
77 | 92 |
78 DCHECK_GT(ids.size(), 0U); | 93 DCHECK_GT(ids.size(), 0U); |
79 if (!autofill_query_field_.should_autocomplete) { | 94 if (!autofill_query_field_.should_autocomplete) { |
80 // If autofill is disabled and we had suggestions, show a warning instead. | 95 // If autofill is disabled and we had suggestions, show a warning instead. |
81 v.assign(1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED)); | 96 v.assign(1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED)); |
82 l.assign(1, string16()); | 97 l.assign(1, string16()); |
83 i.assign(1, string16()); | 98 i.assign(1, string16()); |
84 ids.assign(1, -1); | 99 ids.assign(1, kWarningId); |
85 } else if (ids[0] < 0 && ids.size() > 1) { | 100 } else if (ids[0] < 0 && ids.size() > 1) { |
86 // If we received a warning instead of suggestions from autofill but regular | 101 // If we received a warning instead of suggestions from autofill but regular |
87 // suggestions from autocomplete, don't show the autofill warning. | 102 // suggestions from autocomplete, don't show the autofill warning. |
88 v.erase(v.begin()); | 103 v.erase(v.begin()); |
89 l.erase(l.begin()); | 104 l.erase(l.begin()); |
90 i.erase(i.begin()); | 105 i.erase(i.begin()); |
91 ids.erase(ids.begin()); | 106 ids.erase(ids.begin()); |
92 } | 107 } |
93 | 108 |
94 // If we were about to show a warning and we shouldn't, don't. | 109 // If we were about to show a warning and we shouldn't, don't. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 | 144 |
130 // Send to display. | 145 // Send to display. |
131 if (!v.empty() && autofill_query_field_.is_focusable) | 146 if (!v.empty() && autofill_query_field_.is_focusable) |
132 ApplyAutofillSuggestions(v, l, i, ids, separator_index); | 147 ApplyAutofillSuggestions(v, l, i, ids, separator_index); |
133 | 148 |
134 tab_contents_wrapper_->autofill_manager()->OnDidShowAutofillSuggestions( | 149 tab_contents_wrapper_->autofill_manager()->OnDidShowAutofillSuggestions( |
135 has_autofill_item && !has_shown_autofill_popup_for_current_edit_); | 150 has_autofill_item && !has_shown_autofill_popup_for_current_edit_); |
136 has_shown_autofill_popup_for_current_edit_ |= has_autofill_item; | 151 has_shown_autofill_popup_for_current_edit_ |= has_autofill_item; |
137 } | 152 } |
138 | 153 |
154 void AutofillExternalDelegate::OnShowPasswordSuggestions( | |
155 const std::vector<string16>& suggestions, | |
156 const webkit::forms::FormField& field, | |
157 const gfx::Rect& bounds) { | |
158 autofill_query_field_ = field; | |
159 | |
160 if (suggestions.empty()) { | |
161 HideAutofillPopup(); | |
162 return; | |
163 } | |
164 | |
165 SetBounds(bounds); | |
166 | |
167 std::vector<string16> empty(suggestions.size()); | |
168 std::vector<int> password_ids(suggestions.size(), kPasswordEntryId); | |
169 ApplyAutofillSuggestions(suggestions, empty, empty, password_ids, -1); | |
170 } | |
171 | |
139 void AutofillExternalDelegate::DidEndTextFieldEditing() { | 172 void AutofillExternalDelegate::DidEndTextFieldEditing() { |
140 has_shown_autofill_popup_for_current_edit_ = false; | 173 has_shown_autofill_popup_for_current_edit_ = false; |
141 } | 174 } |
142 | 175 |
143 bool AutofillExternalDelegate::DidAcceptAutofillSuggestions( | 176 bool AutofillExternalDelegate::DidAcceptAutofillSuggestions( |
144 const string16& value, | 177 const string16& value, |
145 int unique_id, | 178 int unique_id, |
146 unsigned index) { | 179 unsigned index) { |
147 // If the selected element is a warning we don't want to do anything. | 180 // If the selected element is a warning we don't want to do anything. |
148 if (unique_id < 0) | 181 if (unique_id == kWarningId) |
149 return false; | 182 return false; |
150 | 183 |
151 // TODO(csharp): Add the password autofill manager. | |
152 // if (password_autofill_manager_->DidAcceptAutofillSuggestion(node, value)) | |
153 // return; | |
154 | |
155 if (suggestions_options_index_ != -1 && | 184 if (suggestions_options_index_ != -1 && |
156 index == static_cast<unsigned>(suggestions_options_index_)) { | 185 index == static_cast<unsigned>(suggestions_options_index_)) { |
157 // User selected 'Autofill Options'. | 186 // User selected 'Autofill Options'. |
158 autofill_manager_->OnShowAutofillDialog(); | 187 autofill_manager_->OnShowAutofillDialog(); |
159 } else if (suggestions_clear_index_ != -1 && | 188 } else if (suggestions_clear_index_ != -1 && |
160 index == static_cast<unsigned>(suggestions_clear_index_)) { | 189 index == static_cast<unsigned>(suggestions_clear_index_)) { |
161 // User selected 'Clear form'. | 190 // User selected 'Clear form'. |
162 RenderViewHost* host = | 191 RenderViewHost* host = |
163 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); | 192 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); |
164 host->Send(new AutofillMsg_ClearForm(host->GetRoutingID())); | 193 host->Send(new AutofillMsg_ClearForm(host->GetRoutingID())); |
194 } else if (password_autofill_manager_.DidAcceptAutofillSuggestion( | |
195 autofill_query_field_, value)) { | |
196 // DidAcceptAutofillSuggestion has already handled the work to fill in | |
197 // the page as required. | |
165 } else if (!unique_id) { | 198 } else if (!unique_id) { |
166 // User selected an Autocomplete entry, so we fill directly. | 199 // User selected an Autocomplete or Password entry, so we fill directly. |
Ilya Sherman
2012/04/02 22:07:29
nit: Is it actually possible for the user to selec
csharp
2012/04/03 12:25:55
Nope, forget to clean up comment when I moved the
| |
167 RenderViewHost* host = | 200 RenderViewHost* host = |
168 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); | 201 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); |
169 host->Send(new AutofillMsg_SetNodeText( | 202 host->Send(new AutofillMsg_SetNodeText( |
170 host->GetRoutingID(), | 203 host->GetRoutingID(), |
171 value)); | 204 value)); |
172 } else { | 205 } else { |
173 FillAutofillFormData(unique_id, false); | 206 FillAutofillFormData(unique_id, false); |
174 } | 207 } |
175 | 208 |
176 HideAutofillPopup(); | 209 HideAutofillPopup(); |
177 | 210 |
178 return true; | 211 return true; |
179 } | 212 } |
180 | 213 |
181 void AutofillExternalDelegate::ClearPreviewedForm() { | 214 void AutofillExternalDelegate::ClearPreviewedForm() { |
215 if (password_autofill_manager_.DidClearAutofillSelection( | |
216 autofill_query_field_)) | |
217 return; | |
218 | |
182 RenderViewHost* host = | 219 RenderViewHost* host = |
183 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); | 220 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); |
184 host->Send(new AutofillMsg_ClearPreviewedForm(host->GetRoutingID())); | 221 host->Send(new AutofillMsg_ClearPreviewedForm(host->GetRoutingID())); |
185 } | 222 } |
186 | 223 |
187 void AutofillExternalDelegate::HideAutofillPopup() { | 224 void AutofillExternalDelegate::HideAutofillPopup() { |
188 suggestions_clear_index_ = -1; | 225 suggestions_clear_index_ = -1; |
189 suggestions_options_index_ = -1; | 226 suggestions_options_index_ = -1; |
190 | 227 |
191 HideAutofillPopupInternal(); | 228 HideAutofillPopupInternal(); |
192 } | 229 } |
193 | 230 |
231 void AutofillExternalDelegate::Reset() { | |
232 HideAutofillPopup(); | |
233 | |
234 password_autofill_manager_.Reset(); | |
235 } | |
236 | |
237 void AutofillExternalDelegate::AddPasswordFormMapping( | |
238 const webkit::forms::FormField& form, | |
239 const webkit::forms::PasswordFormFillData& fill_data) { | |
240 password_autofill_manager_.AddPasswordFormMapping(form, fill_data); | |
241 } | |
242 | |
194 void AutofillExternalDelegate::FillAutofillFormData(int unique_id, | 243 void AutofillExternalDelegate::FillAutofillFormData(int unique_id, |
195 bool is_preview) { | 244 bool is_preview) { |
196 RenderViewHost* host = | 245 RenderViewHost* host = |
197 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); | 246 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); |
198 | 247 |
199 if (is_preview) { | 248 if (is_preview) { |
200 host->Send(new AutofillMsg_SetAutofillActionPreview( | 249 host->Send(new AutofillMsg_SetAutofillActionPreview( |
201 host->GetRoutingID())); | 250 host->GetRoutingID())); |
202 } else { | 251 } else { |
203 host->Send(new AutofillMsg_SetAutofillActionFill( | 252 host->Send(new AutofillMsg_SetAutofillActionFill( |
(...skipping 12 matching lines...) Expand all Loading... | |
216 // none, so all platforms use the default. | 265 // none, so all platforms use the default. |
217 | 266 |
218 #if !defined(OS_ANDROID) && !defined(TOOLKIT_GTK) | 267 #if !defined(OS_ANDROID) && !defined(TOOLKIT_GTK) |
219 | 268 |
220 AutofillExternalDelegate* AutofillExternalDelegate::Create( | 269 AutofillExternalDelegate* AutofillExternalDelegate::Create( |
221 TabContentsWrapper*, AutofillManager*) { | 270 TabContentsWrapper*, AutofillManager*) { |
222 return NULL; | 271 return NULL; |
223 } | 272 } |
224 | 273 |
225 #endif | 274 #endif |
OLD | NEW |