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) { | |
Ilya Sherman
2011/11/17 02:24:37
Don't forget to initialize other member variables,
csharp
2011/11/18 18:15:10
Done.
| |
16 } | 22 } |
17 | 23 |
18 void AutofillExternalDelegate::SelectAutofillSuggestionAtIndex(int listIndex) { | 24 void AutofillExternalDelegate::SelectAutofillSuggestionAtIndex(int listIndex) { |
19 RenderViewHost* host = tab_contents_wrapper_->render_view_host(); | 25 RenderViewHost* host = tab_contents_wrapper_->render_view_host(); |
20 host->Send(new AutofillMsg_SelectAutofillSuggestionAtIndex( | 26 host->Send(new AutofillMsg_SelectAutofillSuggestionAtIndex( |
21 host->routing_id(), | 27 host->routing_id(), |
22 listIndex)); | 28 listIndex)); |
23 } | 29 } |
24 | 30 |
31 void AutofillExternalDelegate::OnQuery(int query_id, | |
32 const webkit_glue::FormData& form, | |
33 const webkit_glue::FormField& field, | |
34 bool display_warning) { | |
35 autofill_query_field_ = field; | |
36 display_warning_if_disabled_ = display_warning; | |
37 autofill_query_id_ = query_id; | |
Ilya Sherman
2011/11/17 02:24:37
Why not simply pass these values into OnSuggestion
csharp
2011/11/18 18:15:10
I don't think there would be an easy way to do tha
Ilya Sherman
2011/11/18 23:16:04
Fair enough -- I keep forgetting that the Autocomp
| |
38 | |
39 OnQueryPlatformSpecific(query_id, form, field); | |
40 } | |
41 | |
42 void AutofillExternalDelegate::SetDidEndTextFieldEditing() { | |
43 has_shown_autofill_popup_for_current_edit_ = false; | |
44 } | |
45 | |
46 void AutofillExternalDelegate::OnSuggestionsReturned( | |
47 int query_id, | |
48 const std::vector<string16>& values, | |
49 const std::vector<string16>& labels, | |
50 const std::vector<string16>& icons, | |
51 const std::vector<int>& unique_ids) { | |
52 if (query_id != autofill_query_id_) | |
53 return; | |
Ilya Sherman
2011/11/17 02:24:37
This case shouldn't be reachable, since this commu
csharp
2011/11/18 18:15:10
I'm not sure that this all occurs in a single task
Ilya Sherman
2011/11/18 23:16:04
Yep, you're right -- again, I forgot that the Auto
| |
54 | |
55 if (values.empty()) { | |
56 // No suggestions, any popup currently showing is obsolete. | |
57 // TODO(csharp): Hide the popup once my other code is merged with this. | |
58 // hidePopups(); | |
59 return; | |
60 } | |
61 | |
62 std::vector<string16> v(values); | |
63 std::vector<string16> l(labels); | |
64 std::vector<string16> i(icons); | |
65 std::vector<int> ids(unique_ids); | |
66 int separator_index = -1; | |
67 | |
68 DCHECK_GT(ids.size(), 0U); | |
69 if (!autofill_query_field_.autocomplete) { | |
70 // If autofill is disabled and we had suggestions, show a warning instead. | |
71 v.assign(1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED)); | |
72 l.assign(1, string16()); | |
73 i.assign(1, string16()); | |
74 ids.assign(1, -1); | |
75 } else if (ids[0] < 0 && ids.size() > 1) { | |
76 // If we received a warning instead of suggestions from autofill but regular | |
77 // suggestions from autocomplete, don't show the autofill warning. | |
78 v.erase(v.begin()); | |
79 l.erase(l.begin()); | |
80 i.erase(i.begin()); | |
81 ids.erase(ids.begin()); | |
82 } | |
83 | |
84 // If we were about to show a warning and we shouldn't, don't. | |
85 if (ids[0] < 0 && !display_warning_if_disabled_) | |
86 return; | |
87 | |
88 // Only include "Autofill Options" special menu item if we have Autofill | |
89 // items, identified by |unique_ids| having at least one valid value. | |
90 bool has_autofill_item = false; | |
91 for (size_t i = 0; i < ids.size(); ++i) { | |
92 if (ids[i] > 0) { | |
93 has_autofill_item = true; | |
94 break; | |
95 } | |
96 } | |
97 | |
98 // The form has been auto-filled, so give the user the chance to clear the | |
99 // form. Append the 'Clear form' menu item. | |
100 if (has_autofill_item && | |
101 autofill_query_field_.is_autofilled) { | |
102 v.push_back(l10n_util::GetStringUTF16(IDS_AUTOFILL_CLEAR_FORM_MENU_ITEM)); | |
103 l.push_back(string16()); | |
104 i.push_back(string16()); | |
105 ids.push_back(0); | |
106 suggestions_clear_index_ = v.size() - 1; | |
107 separator_index = v.size() - 1; | |
108 } | |
109 | |
110 if (has_autofill_item) { | |
111 // Append the 'Chrome Autofill options' menu item; | |
112 v.push_back(l10n_util::GetStringFUTF16(IDS_AUTOFILL_OPTIONS_POPUP, | |
113 WideToUTF16(chrome::kBrowserAppName))); | |
114 l.push_back(string16()); | |
115 i.push_back(string16()); | |
116 ids.push_back(0); | |
117 suggestions_options_index_ = v.size() - 1; | |
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 } | |
Ilya Sherman
2011/11/17 02:24:37
nit: No need for curly braces since this is a one-
csharp
2011/11/18 18:15:10
Done.
| |
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 |