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