OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/browser/autofill/autofill_external_delegate.h" | 6 #include "chrome/browser/autofill/autofill_external_delegate.h" |
| 7 #include "chrome/browser/autofill/autofill_manager.h" |
6 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 8 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
7 #include "chrome/common/autofill_messages.h" | 9 #include "chrome/common/autofill_messages.h" |
| 10 #include "chrome/common/chrome_constants.h" |
8 #include "content/browser/renderer_host/render_view_host.h" | 11 #include "content/browser/renderer_host/render_view_host.h" |
| 12 #include "grit/generated_resources.h" |
| 13 #include "ui/base/l10n/l10n_util.h" |
9 | 14 |
10 AutofillExternalDelegate::~AutofillExternalDelegate() { | 15 AutofillExternalDelegate::~AutofillExternalDelegate() { |
11 } | 16 } |
12 | 17 |
13 AutofillExternalDelegate::AutofillExternalDelegate( | 18 AutofillExternalDelegate::AutofillExternalDelegate( |
14 TabContentsWrapper* tab_contents_wrapper) | 19 TabContentsWrapper* tab_contents_wrapper) |
15 : tab_contents_wrapper_(tab_contents_wrapper) { | 20 : tab_contents_wrapper_(tab_contents_wrapper), |
| 21 autofill_query_id_(0), |
| 22 display_warning_if_disabled_(false), |
| 23 has_shown_autofill_popup_for_current_edit_(false) { |
16 } | 24 } |
17 | 25 |
18 void AutofillExternalDelegate::SelectAutofillSuggestionAtIndex(int listIndex) { | 26 void AutofillExternalDelegate::SelectAutofillSuggestionAtIndex(int listIndex) { |
19 RenderViewHost* host = tab_contents_wrapper_->render_view_host(); | 27 RenderViewHost* host = tab_contents_wrapper_->render_view_host(); |
20 host->Send(new AutofillMsg_SelectAutofillSuggestionAtIndex( | 28 host->Send(new AutofillMsg_SelectAutofillSuggestionAtIndex( |
21 host->routing_id(), | 29 host->routing_id(), |
22 listIndex)); | 30 listIndex)); |
23 } | 31 } |
24 | 32 |
| 33 void AutofillExternalDelegate::OnQuery(int query_id, |
| 34 const webkit_glue::FormData& form, |
| 35 const webkit_glue::FormField& field, |
| 36 const gfx::Rect& bounds, |
| 37 bool display_warning_if_disabled) { |
| 38 autofill_query_field_ = field; |
| 39 display_warning_if_disabled_ = display_warning_if_disabled; |
| 40 autofill_query_id_ = query_id; |
| 41 |
| 42 OnQueryPlatformSpecific(query_id, form, field); |
| 43 } |
| 44 |
| 45 void AutofillExternalDelegate::DidEndTextFieldEditing() { |
| 46 has_shown_autofill_popup_for_current_edit_ = false; |
| 47 } |
| 48 |
| 49 void AutofillExternalDelegate::OnSuggestionsReturned( |
| 50 int query_id, |
| 51 const std::vector<string16>& values, |
| 52 const std::vector<string16>& labels, |
| 53 const std::vector<string16>& icons, |
| 54 const std::vector<int>& unique_ids) { |
| 55 if (query_id != autofill_query_id_) |
| 56 return; |
| 57 |
| 58 if (values.empty()) { |
| 59 // No suggestions, any popup currently showing is obsolete. |
| 60 HideAutofillPopup(); |
| 61 return; |
| 62 } |
| 63 |
| 64 std::vector<string16> v(values); |
| 65 std::vector<string16> l(labels); |
| 66 std::vector<string16> i(icons); |
| 67 std::vector<int> ids(unique_ids); |
| 68 int separator_index = -1; |
| 69 |
| 70 DCHECK_GT(ids.size(), 0U); |
| 71 if (!autofill_query_field_.should_autocomplete) { |
| 72 // If autofill is disabled and we had suggestions, show a warning instead. |
| 73 v.assign(1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED)); |
| 74 l.assign(1, string16()); |
| 75 i.assign(1, string16()); |
| 76 ids.assign(1, -1); |
| 77 } else if (ids[0] < 0 && ids.size() > 1) { |
| 78 // If we received a warning instead of suggestions from autofill but regular |
| 79 // suggestions from autocomplete, don't show the autofill warning. |
| 80 v.erase(v.begin()); |
| 81 l.erase(l.begin()); |
| 82 i.erase(i.begin()); |
| 83 ids.erase(ids.begin()); |
| 84 } |
| 85 |
| 86 // If we were about to show a warning and we shouldn't, don't. |
| 87 if (ids[0] < 0 && !display_warning_if_disabled_) |
| 88 return; |
| 89 |
| 90 // Only include "Autofill Options" special menu item if we have Autofill |
| 91 // items, identified by |unique_ids| having at least one valid value. |
| 92 bool has_autofill_item = false; |
| 93 for (size_t i = 0; i < ids.size(); ++i) { |
| 94 if (ids[i] > 0) { |
| 95 has_autofill_item = true; |
| 96 break; |
| 97 } |
| 98 } |
| 99 |
| 100 // The form has been auto-filled, so give the user the chance to clear the |
| 101 // form. Append the 'Clear form' menu item. |
| 102 if (has_autofill_item && |
| 103 autofill_query_field_.is_autofilled) { |
| 104 v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_CLEAR_FORM_MENU_ITEM)); |
| 105 l.push_back(string16()); |
| 106 i.push_back(string16()); |
| 107 ids.push_back(0); |
| 108 separator_index = v.size() - 1; |
| 109 } |
| 110 |
| 111 if (has_autofill_item) { |
| 112 // Append the 'Chrome Autofill options' menu item; |
| 113 v.push_back(l10n_util::GetStringFUTF16(IDS_AUTOFILL_OPTIONS_POPUP, |
| 114 WideToUTF16(chrome::kBrowserAppName))); |
| 115 l.push_back(string16()); |
| 116 i.push_back(string16()); |
| 117 ids.push_back(0); |
| 118 separator_index = values.size(); |
| 119 } |
| 120 |
| 121 // Send to display. |
| 122 if (!v.empty() && autofill_query_field_.is_focusable) |
| 123 ApplyAutofillSuggestions(v, l, i, ids, separator_index); |
| 124 |
| 125 tab_contents_wrapper_->autofill_manager()->OnDidShowAutofillSuggestions( |
| 126 has_autofill_item && !has_shown_autofill_popup_for_current_edit_); |
| 127 has_shown_autofill_popup_for_current_edit_ |= has_autofill_item; |
| 128 } |
| 129 |
25 | 130 |
26 // Add a "!defined(OS_YOUROS) for each platform that implements this | 131 // Add a "!defined(OS_YOUROS) for each platform that implements this |
27 // in an autofill_external_delegate_YOUROS.cc. Currently there are | 132 // in an autofill_external_delegate_YOUROS.cc. Currently there are |
28 // none, so all platforms use the default. | 133 // none, so all platforms use the default. |
29 | 134 |
30 #if !defined(OS_ANDROID) | 135 #if !defined(OS_ANDROID) |
31 | 136 |
32 AutofillExternalDelegate* AutofillExternalDelegate::Create( | 137 AutofillExternalDelegate* AutofillExternalDelegate::Create( |
33 TabContentsWrapper*, AutofillManager*) { | 138 TabContentsWrapper*, AutofillManager*) { |
34 return NULL; | 139 return NULL; |
35 } | 140 } |
36 | 141 |
37 #endif | 142 #endif |
OLD | NEW |