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 AutofillExternalDelegate::~AutofillExternalDelegate() { | 19 AutofillExternalDelegate::~AutofillExternalDelegate() { |
20 } | 20 } |
21 | 21 |
22 AutofillExternalDelegate::AutofillExternalDelegate( | 22 AutofillExternalDelegate::AutofillExternalDelegate( |
23 TabContentsWrapper* tab_contents_wrapper, | 23 TabContentsWrapper* tab_contents_wrapper, |
24 AutofillManager* autofill_manager) | 24 AutofillManager* autofill_manager) |
25 : tab_contents_wrapper_(tab_contents_wrapper), | 25 : tab_contents_wrapper_(tab_contents_wrapper), |
26 autofill_manager_(autofill_manager), | 26 autofill_manager_(autofill_manager), |
| 27 password_autofill_manager_( |
| 28 tab_contents_wrapper->web_contents()->GetRenderViewHost()), |
27 autofill_query_id_(0), | 29 autofill_query_id_(0), |
28 display_warning_if_disabled_(false), | 30 display_warning_if_disabled_(false), |
29 has_shown_autofill_popup_for_current_edit_(false), | 31 has_shown_autofill_popup_for_current_edit_(false), |
30 suggestions_clear_index_(-1), | 32 suggestions_clear_index_(-1), |
31 suggestions_options_index_(-1) { | 33 suggestions_options_index_(-1) { |
32 } | 34 } |
33 | 35 |
34 void AutofillExternalDelegate::SelectAutofillSuggestionAtIndex(int unique_id, | 36 void AutofillExternalDelegate::SelectAutofillSuggestionAtIndex(int unique_id, |
35 int list_index) { | 37 int list_index) { |
| 38 if (unique_id == kPasswordEntryIds) |
| 39 return; |
| 40 |
36 if (list_index == suggestions_options_index_ || | 41 if (list_index == suggestions_options_index_ || |
37 list_index == suggestions_clear_index_ || | 42 list_index == suggestions_clear_index_ || |
38 unique_id == -1) | 43 unique_id == kWarningIds) |
39 return; | 44 return; |
40 | 45 |
41 FillAutofillFormData(unique_id, true); | 46 FillAutofillFormData(unique_id, true); |
42 } | 47 } |
43 | 48 |
44 void AutofillExternalDelegate::OnQuery(int query_id, | 49 void AutofillExternalDelegate::OnQuery(int query_id, |
45 const webkit::forms::FormData& form, | 50 const webkit::forms::FormData& form, |
46 const webkit::forms::FormField& field, | 51 const webkit::forms::FormField& field, |
47 const gfx::Rect& bounds, | 52 const gfx::Rect& bounds, |
48 bool display_warning_if_disabled) { | 53 bool display_warning_if_disabled) { |
(...skipping 25 matching lines...) Expand all Loading... |
74 std::vector<string16> i(icons); | 79 std::vector<string16> i(icons); |
75 std::vector<int> ids(unique_ids); | 80 std::vector<int> ids(unique_ids); |
76 int separator_index = -1; | 81 int separator_index = -1; |
77 | 82 |
78 DCHECK_GT(ids.size(), 0U); | 83 DCHECK_GT(ids.size(), 0U); |
79 if (!autofill_query_field_.should_autocomplete) { | 84 if (!autofill_query_field_.should_autocomplete) { |
80 // If autofill is disabled and we had suggestions, show a warning instead. | 85 // If autofill is disabled and we had suggestions, show a warning instead. |
81 v.assign(1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED)); | 86 v.assign(1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED)); |
82 l.assign(1, string16()); | 87 l.assign(1, string16()); |
83 i.assign(1, string16()); | 88 i.assign(1, string16()); |
84 ids.assign(1, -1); | 89 ids.assign(1, kWarningIds); |
85 } else if (ids[0] < 0 && ids.size() > 1) { | 90 } else if (ids[0] < 0 && ids.size() > 1) { |
86 // If we received a warning instead of suggestions from autofill but regular | 91 // If we received a warning instead of suggestions from autofill but regular |
87 // suggestions from autocomplete, don't show the autofill warning. | 92 // suggestions from autocomplete, don't show the autofill warning. |
88 v.erase(v.begin()); | 93 v.erase(v.begin()); |
89 l.erase(l.begin()); | 94 l.erase(l.begin()); |
90 i.erase(i.begin()); | 95 i.erase(i.begin()); |
91 ids.erase(ids.begin()); | 96 ids.erase(ids.begin()); |
92 } | 97 } |
93 | 98 |
94 // If we were about to show a warning and we shouldn't, don't. | 99 // 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 | 134 |
130 // Send to display. | 135 // Send to display. |
131 if (!v.empty() && autofill_query_field_.is_focusable) | 136 if (!v.empty() && autofill_query_field_.is_focusable) |
132 ApplyAutofillSuggestions(v, l, i, ids, separator_index); | 137 ApplyAutofillSuggestions(v, l, i, ids, separator_index); |
133 | 138 |
134 tab_contents_wrapper_->autofill_manager()->OnDidShowAutofillSuggestions( | 139 tab_contents_wrapper_->autofill_manager()->OnDidShowAutofillSuggestions( |
135 has_autofill_item && !has_shown_autofill_popup_for_current_edit_); | 140 has_autofill_item && !has_shown_autofill_popup_for_current_edit_); |
136 has_shown_autofill_popup_for_current_edit_ |= has_autofill_item; | 141 has_shown_autofill_popup_for_current_edit_ |= has_autofill_item; |
137 } | 142 } |
138 | 143 |
| 144 void AutofillExternalDelegate::ShowPasswordSuggestions( |
| 145 const std::vector<string16>& suggestions) { |
| 146 if (suggestions.empty()) { |
| 147 HideAutofillPopup(); |
| 148 return; |
| 149 } |
| 150 |
| 151 std::vector<string16> labels(suggestions.size()); |
| 152 std::vector<string16> icons(suggestions.size()); |
| 153 std::vector<int> ids(suggestions.size(), kPasswordEntryIds); |
| 154 ApplyAutofillSuggestions(suggestions, labels, icons, ids, -1); |
| 155 } |
| 156 |
139 void AutofillExternalDelegate::DidEndTextFieldEditing() { | 157 void AutofillExternalDelegate::DidEndTextFieldEditing() { |
140 has_shown_autofill_popup_for_current_edit_ = false; | 158 has_shown_autofill_popup_for_current_edit_ = false; |
141 } | 159 } |
142 | 160 |
143 void AutofillExternalDelegate::DidAcceptAutofillSuggestions( | 161 void AutofillExternalDelegate::DidAcceptAutofillSuggestions( |
144 const string16& value, | 162 const string16& value, |
145 int unique_id, | 163 int unique_id, |
146 unsigned index) { | 164 unsigned index) { |
147 // If the selected element is a warning we don't want to do anything. | 165 // If the selected element is a warning we don't want to do anything. |
148 if (unique_id < 0) | 166 if (unique_id == kWarningIds) |
149 return; | 167 return; |
150 | 168 |
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 && | 169 if (suggestions_options_index_ != -1 && |
156 index == static_cast<unsigned>(suggestions_options_index_)) { | 170 index == static_cast<unsigned>(suggestions_options_index_)) { |
157 // User selected 'Autofill Options'. | 171 // User selected 'Autofill Options'. |
158 autofill_manager_->OnShowAutofillDialog(); | 172 autofill_manager_->OnShowAutofillDialog(); |
159 } else if (suggestions_clear_index_ != -1 && | 173 } else if (suggestions_clear_index_ != -1 && |
160 index == static_cast<unsigned>(suggestions_clear_index_)) { | 174 index == static_cast<unsigned>(suggestions_clear_index_)) { |
161 // User selected 'Clear form'. | 175 // User selected 'Clear form'. |
162 RenderViewHost* host = | 176 RenderViewHost* host = |
163 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); | 177 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); |
164 host->Send(new AutofillMsg_ClearForm(host->GetRoutingID())); | 178 host->Send(new AutofillMsg_ClearForm(host->GetRoutingID())); |
165 } else if (!unique_id) { | 179 } else if (!unique_id || unique_id == kPasswordEntryIds) { |
166 // User selected an Autocomplete entry, so we fill directly. | 180 // User selected an Autocomplete or Password entry, so we fill directly. |
167 RenderViewHost* host = | 181 RenderViewHost* host = |
168 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); | 182 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); |
169 host->Send(new AutofillMsg_SetNodeText( | 183 host->Send(new AutofillMsg_SetNodeText( |
170 host->GetRoutingID(), | 184 host->GetRoutingID(), |
171 value)); | 185 value)); |
172 } else { | 186 } else { |
173 FillAutofillFormData(unique_id, false); | 187 FillAutofillFormData(unique_id, false); |
174 } | 188 } |
175 | 189 |
176 HideAutofillPopup(); | 190 HideAutofillPopup(); |
177 } | 191 } |
178 | 192 |
179 void AutofillExternalDelegate::ClearPreviewedForm() { | 193 void AutofillExternalDelegate::ClearPreviewedForm() { |
| 194 if (password_autofill_manager_.DidClearAutofillSelection( |
| 195 autofill_query_field_)) |
| 196 return; |
| 197 |
180 RenderViewHost* host = | 198 RenderViewHost* host = |
181 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); | 199 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); |
182 host->Send(new AutofillMsg_ClearPreviewedForm(host->GetRoutingID())); | 200 host->Send(new AutofillMsg_ClearPreviewedForm(host->GetRoutingID())); |
183 } | 201 } |
184 | 202 |
185 void AutofillExternalDelegate::HideAutofillPopup() { | 203 void AutofillExternalDelegate::HideAutofillPopup() { |
186 suggestions_clear_index_ = -1; | 204 suggestions_clear_index_ = -1; |
187 suggestions_options_index_ = -1; | 205 suggestions_options_index_ = -1; |
188 | 206 |
189 HideAutofillPopupInternal(); | 207 HideAutofillPopupInternal(); |
190 } | 208 } |
191 | 209 |
| 210 void AutofillExternalDelegate::Reset() { |
| 211 HideAutofillPopup(); |
| 212 |
| 213 password_autofill_manager_.Reset(); |
| 214 } |
| 215 |
| 216 void AutofillExternalDelegate::AddPasswordFormMapping( |
| 217 const webkit::forms::FormField& form, |
| 218 const webkit::forms::PasswordFormFillData& fill_data) { |
| 219 password_autofill_manager_.AddPasswordFormMapping(form, fill_data); |
| 220 } |
| 221 |
192 void AutofillExternalDelegate::FillAutofillFormData(int unique_id, | 222 void AutofillExternalDelegate::FillAutofillFormData(int unique_id, |
193 bool is_preview) { | 223 bool is_preview) { |
194 RenderViewHost* host = | 224 RenderViewHost* host = |
195 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); | 225 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); |
196 | 226 |
197 if (is_preview) { | 227 if (is_preview) { |
198 host->Send(new AutofillMsg_SetAutofillActionPreview( | 228 host->Send(new AutofillMsg_SetAutofillActionPreview( |
199 host->GetRoutingID())); | 229 host->GetRoutingID())); |
200 } else { | 230 } else { |
201 host->Send(new AutofillMsg_SetAutofillActionFill( | 231 host->Send(new AutofillMsg_SetAutofillActionFill( |
(...skipping 12 matching lines...) Expand all Loading... |
214 // none, so all platforms use the default. | 244 // none, so all platforms use the default. |
215 | 245 |
216 #if !defined(OS_ANDROID) && !defined(TOOLKIT_GTK) | 246 #if !defined(OS_ANDROID) && !defined(TOOLKIT_GTK) |
217 | 247 |
218 AutofillExternalDelegate* AutofillExternalDelegate::Create( | 248 AutofillExternalDelegate* AutofillExternalDelegate::Create( |
219 TabContentsWrapper*, AutofillManager*) { | 249 TabContentsWrapper*, AutofillManager*) { |
220 return NULL; | 250 return NULL; |
221 } | 251 } |
222 | 252 |
223 #endif | 253 #endif |
OLD | NEW |