Chromium Code Reviews| 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/browser/renderer_host/render_view_host.h" | 11 #include "content/browser/renderer_host/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 AutofillExternalDelegate::~AutofillExternalDelegate() { | 17 AutofillExternalDelegate::~AutofillExternalDelegate() { |
| 18 } | 18 } |
| 19 | 19 |
| 20 AutofillExternalDelegate::AutofillExternalDelegate( | 20 AutofillExternalDelegate::AutofillExternalDelegate( |
| 21 TabContentsWrapper* tab_contents_wrapper, | 21 TabContentsWrapper* tab_contents_wrapper, |
| 22 AutofillManager* autofill_manager) | 22 AutofillManager* autofill_manager) |
| 23 : tab_contents_wrapper_(tab_contents_wrapper), | 23 : tab_contents_wrapper_(tab_contents_wrapper), |
| 24 autofill_manager_(autofill_manager), | 24 autofill_manager_(autofill_manager), |
| 25 autofill_query_id_(0), | 25 autofill_query_id_(0), |
| 26 display_warning_if_disabled_(false), | 26 display_warning_if_disabled_(false), |
| 27 has_shown_autofill_popup_for_current_edit_(false) { | 27 has_shown_autofill_popup_for_current_edit_(false), |
| 28 suggestions_clear_index_(-1), | |
| 29 suggestions_options_index_(-1) { | |
| 28 } | 30 } |
| 29 | 31 |
| 30 void AutofillExternalDelegate::SelectAutofillSuggestionAtIndex(int listIndex) { | 32 void AutofillExternalDelegate::SelectAutofillSuggestionAtIndex(int unique_id, |
| 31 RenderViewHost* host = | 33 int list_index) { |
| 32 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); | 34 if (list_index == suggestions_options_index_ || |
| 33 host->Send(new AutofillMsg_SelectAutofillSuggestionAtIndex( | 35 list_index == suggestions_clear_index_ || |
| 34 host->routing_id(), | 36 unique_id == -1) |
| 35 listIndex)); | 37 return; |
| 38 | |
| 39 FillAutofillFormData(unique_id, true); | |
| 36 } | 40 } |
| 37 | 41 |
| 38 void AutofillExternalDelegate::OnQuery(int query_id, | 42 void AutofillExternalDelegate::OnQuery(int query_id, |
| 39 const webkit::forms::FormData& form, | 43 const webkit::forms::FormData& form, |
| 40 const webkit::forms::FormField& field, | 44 const webkit::forms::FormField& field, |
| 41 const gfx::Rect& bounds, | 45 const gfx::Rect& bounds, |
| 42 bool display_warning_if_disabled) { | 46 bool display_warning_if_disabled) { |
| 47 autofill_query_form_ = form; | |
| 43 autofill_query_field_ = field; | 48 autofill_query_field_ = field; |
| 44 display_warning_if_disabled_ = display_warning_if_disabled; | 49 display_warning_if_disabled_ = display_warning_if_disabled; |
| 45 autofill_query_id_ = query_id; | 50 autofill_query_id_ = query_id; |
| 46 | 51 |
| 47 OnQueryPlatformSpecific(query_id, form, field, bounds); | 52 OnQueryPlatformSpecific(query_id, form, field, bounds); |
| 48 } | 53 } |
| 49 | 54 |
| 50 void AutofillExternalDelegate::DidEndTextFieldEditing() { | |
| 51 has_shown_autofill_popup_for_current_edit_ = false; | |
| 52 } | |
| 53 | |
| 54 void AutofillExternalDelegate::OnSuggestionsReturned( | 55 void AutofillExternalDelegate::OnSuggestionsReturned( |
| 55 int query_id, | 56 int query_id, |
| 56 const std::vector<string16>& values, | 57 const std::vector<string16>& values, |
| 57 const std::vector<string16>& labels, | 58 const std::vector<string16>& labels, |
| 58 const std::vector<string16>& icons, | 59 const std::vector<string16>& icons, |
| 59 const std::vector<int>& unique_ids) { | 60 const std::vector<int>& unique_ids) { |
| 60 if (query_id != autofill_query_id_) | 61 if (query_id != autofill_query_id_) |
| 61 return; | 62 return; |
| 62 | 63 |
| 63 if (values.empty()) { | 64 if (values.empty()) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 } | 104 } |
| 104 | 105 |
| 105 // The form has been auto-filled, so give the user the chance to clear the | 106 // The form has been auto-filled, so give the user the chance to clear the |
| 106 // form. Append the 'Clear form' menu item. | 107 // form. Append the 'Clear form' menu item. |
| 107 if (has_autofill_item && | 108 if (has_autofill_item && |
| 108 autofill_query_field_.is_autofilled) { | 109 autofill_query_field_.is_autofilled) { |
| 109 v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_CLEAR_FORM_MENU_ITEM)); | 110 v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_CLEAR_FORM_MENU_ITEM)); |
| 110 l.push_back(string16()); | 111 l.push_back(string16()); |
| 111 i.push_back(string16()); | 112 i.push_back(string16()); |
| 112 ids.push_back(0); | 113 ids.push_back(0); |
| 114 suggestions_clear_index_ = v.size() - 1; | |
| 113 separator_index = v.size() - 1; | 115 separator_index = v.size() - 1; |
| 114 } | 116 } |
| 115 | 117 |
| 116 if (has_autofill_item) { | 118 if (has_autofill_item) { |
| 117 // Append the 'Chrome Autofill options' menu item; | 119 // Append the 'Chrome Autofill options' menu item; |
| 118 v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_OPTIONS_POPUP)); | 120 v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_OPTIONS_POPUP)); |
| 119 l.push_back(string16()); | 121 l.push_back(string16()); |
| 120 i.push_back(string16()); | 122 i.push_back(string16()); |
| 121 ids.push_back(0); | 123 ids.push_back(0); |
| 124 suggestions_options_index_ = v.size() - 1; | |
| 122 separator_index = values.size(); | 125 separator_index = values.size(); |
| 123 } | 126 } |
| 124 | 127 |
| 125 // Send to display. | 128 // Send to display. |
| 126 if (!v.empty() && autofill_query_field_.is_focusable) | 129 if (!v.empty() && autofill_query_field_.is_focusable) |
| 127 ApplyAutofillSuggestions(v, l, i, ids, separator_index); | 130 ApplyAutofillSuggestions(v, l, i, ids, separator_index); |
| 128 | 131 |
| 129 tab_contents_wrapper_->autofill_manager()->OnDidShowAutofillSuggestions( | 132 tab_contents_wrapper_->autofill_manager()->OnDidShowAutofillSuggestions( |
| 130 has_autofill_item && !has_shown_autofill_popup_for_current_edit_); | 133 has_autofill_item && !has_shown_autofill_popup_for_current_edit_); |
| 131 has_shown_autofill_popup_for_current_edit_ |= has_autofill_item; | 134 has_shown_autofill_popup_for_current_edit_ |= has_autofill_item; |
| 132 } | 135 } |
| 133 | 136 |
| 137 void AutofillExternalDelegate::DidEndTextFieldEditing() { | |
| 138 has_shown_autofill_popup_for_current_edit_ = false; | |
| 139 } | |
| 140 | |
| 141 void AutofillExternalDelegate::DidAcceptAutofillSuggestions(string16 value, | |
| 142 int unique_id, | |
| 143 unsigned index) { | |
| 144 // If the selected element has a negative unique id we don't want to do | |
| 145 // anything. | |
|
Ilya Sherman
2012/02/07 23:35:55
nit: "has a negative unique id" -> "is a warning"
csharp
2012/02/08 16:11:16
Done.
| |
| 146 if (unique_id < 0) | |
| 147 return; | |
| 148 | |
| 149 // TODO(csharp): Add the password autofill manager. | |
| 150 // if (password_autofill_manager_->DidAcceptAutofillSuggestion(node, value)) | |
| 151 // return; | |
| 152 | |
| 153 if (suggestions_options_index_ != -1 && | |
| 154 index == static_cast<unsigned>(suggestions_options_index_)) { | |
| 155 // User selected 'Autofill Options'. | |
| 156 autofill_manager_->OnShowAutofillDialog(); | |
| 157 } else if (suggestions_clear_index_ != -1 && | |
| 158 index == static_cast<unsigned>(suggestions_clear_index_)) { | |
| 159 // User selected 'Clear form'. | |
| 160 RenderViewHost* host = | |
| 161 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); | |
| 162 host->Send(new AutofillMsg_ClearForm(host->routing_id())); | |
| 163 } else if (!unique_id) { | |
| 164 // User selected an Autocomplete entry, so we fill directly. | |
| 165 RenderViewHost* host = | |
| 166 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); | |
| 167 host->Send(new AutofillMsg_SetNodeText( | |
| 168 host->routing_id(), | |
| 169 value)); | |
| 170 } else { | |
| 171 FillAutofillFormData(unique_id, false); | |
| 172 } | |
| 173 | |
| 174 HideAutofillPopup(); | |
| 175 } | |
| 176 | |
| 177 void AutofillExternalDelegate::ClearPreviewedForm() { | |
| 178 RenderViewHost* host = | |
| 179 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); | |
| 180 host->Send(new AutofillMsg_ClearPreviewedForm(host->routing_id())); | |
| 181 } | |
| 182 | |
| 183 void AutofillExternalDelegate::HideAutofillPopup() { | |
| 184 suggestions_clear_index_ = -1; | |
| 185 suggestions_options_index_ = -1; | |
| 186 | |
| 187 HideAutofillPopupInternal(); | |
| 188 } | |
| 189 | |
| 190 void AutofillExternalDelegate::FillAutofillFormData(int unique_id, | |
| 191 bool is_preview) { | |
| 192 RenderViewHost* host = | |
| 193 tab_contents_wrapper_->web_contents()->GetRenderViewHost(); | |
| 194 | |
| 195 if (is_preview) { | |
| 196 host->Send(new AutofillMsg_SetAutofillActionPreview( | |
| 197 host->routing_id())); | |
| 198 } else { | |
| 199 host->Send(new AutofillMsg_SetAutofillActionFill( | |
| 200 host->routing_id())); | |
| 201 } | |
| 202 | |
| 203 // Fill the values for the whole form. | |
| 204 autofill_manager_->OnFillAutofillFormData(autofill_query_id_, | |
| 205 autofill_query_form_, | |
| 206 autofill_query_field_, | |
| 207 unique_id); | |
| 208 } | |
| 134 | 209 |
| 135 // Add a "!defined(OS_YOUROS) for each platform that implements this | 210 // Add a "!defined(OS_YOUROS) for each platform that implements this |
| 136 // in an autofill_external_delegate_YOUROS.cc. Currently there are | 211 // in an autofill_external_delegate_YOUROS.cc. Currently there are |
| 137 // none, so all platforms use the default. | 212 // none, so all platforms use the default. |
| 138 | 213 |
| 139 #if !defined(OS_ANDROID) && !defined(TOOLKIT_GTK) | 214 #if !defined(OS_ANDROID) && !defined(TOOLKIT_GTK) |
| 140 | 215 |
| 141 AutofillExternalDelegate* AutofillExternalDelegate::Create( | 216 AutofillExternalDelegate* AutofillExternalDelegate::Create( |
| 142 TabContentsWrapper*, AutofillManager*) { | 217 TabContentsWrapper*, AutofillManager*) { |
| 143 return NULL; | 218 return NULL; |
| 144 } | 219 } |
| 145 | 220 |
| 146 #endif | 221 #endif |
| OLD | NEW |