OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/autocomplete_history_manager.h" | 5 #include "chrome/browser/autocomplete_history_manager.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/string16.h" | 9 #include "base/string16.h" |
10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 &serial) || serial == 0) | 66 &serial) || serial == 0) |
67 return false; | 67 return false; |
68 | 68 |
69 return true; | 69 return true; |
70 } | 70 } |
71 | 71 |
72 } // namespace | 72 } // namespace |
73 | 73 |
74 AutocompleteHistoryManager::AutocompleteHistoryManager( | 74 AutocompleteHistoryManager::AutocompleteHistoryManager( |
75 TabContents* tab_contents) : tab_contents_(tab_contents), | 75 TabContents* tab_contents) : tab_contents_(tab_contents), |
76 pending_query_handle_(0), | 76 pending_query_handle_(0) { |
77 query_id_(0) { | |
78 DCHECK(tab_contents); | 77 DCHECK(tab_contents); |
79 | 78 |
80 profile_ = tab_contents_->profile(); | 79 profile_ = tab_contents_->profile(); |
81 DCHECK(profile_); | 80 DCHECK(profile_); |
82 | 81 |
83 web_data_service_ = profile_->GetWebDataService(Profile::EXPLICIT_ACCESS); | 82 web_data_service_ = profile_->GetWebDataService(Profile::EXPLICIT_ACCESS); |
84 DCHECK(web_data_service_); | 83 DCHECK(web_data_service_); |
85 | 84 |
86 autofill_enabled_.Init(prefs::kAutoFillEnabled, profile_->GetPrefs(), NULL); | 85 autofill_enabled_.Init(prefs::kAutoFillEnabled, profile_->GetPrefs(), NULL); |
87 } | 86 } |
88 | 87 |
89 AutocompleteHistoryManager::~AutocompleteHistoryManager() { | 88 AutocompleteHistoryManager::~AutocompleteHistoryManager() { |
90 CancelPendingQuery(); | 89 CancelPendingQuery(); |
91 } | 90 } |
92 | 91 |
93 void AutocompleteHistoryManager::FormSubmitted(const FormData& form) { | 92 void AutocompleteHistoryManager::FormSubmitted(const FormData& form) { |
94 StoreFormEntriesInWebDatabase(form); | 93 StoreFormEntriesInWebDatabase(form); |
95 } | 94 } |
96 | 95 |
97 bool AutocompleteHistoryManager::GetAutocompleteSuggestions( | 96 void AutocompleteHistoryManager::GetAutocompleteSuggestions( |
98 int query_id, const string16& name, const string16& prefix) { | 97 const string16& name, const string16& prefix) { |
99 if (!*autofill_enabled_) | 98 if (!*autofill_enabled_) { |
100 return false; | 99 SendSuggestions(NULL); |
| 100 return; |
| 101 } |
101 | 102 |
102 CancelPendingQuery(); | 103 CancelPendingQuery(); |
103 | 104 |
104 query_id_ = query_id; | |
105 pending_query_handle_ = web_data_service_->GetFormValuesForElementName( | 105 pending_query_handle_ = web_data_service_->GetFormValuesForElementName( |
106 name, prefix, kMaxAutocompleteMenuItems, this); | 106 name, prefix, kMaxAutocompleteMenuItems, this); |
107 return true; | |
108 } | 107 } |
109 | 108 |
110 void AutocompleteHistoryManager::RemoveAutocompleteEntry( | 109 void AutocompleteHistoryManager::RemoveAutocompleteEntry( |
111 const string16& name, const string16& value) { | 110 const string16& name, const string16& value) { |
112 web_data_service_->RemoveFormValueForElementName(name, value); | 111 web_data_service_->RemoveFormValueForElementName(name, value); |
113 } | 112 } |
114 | 113 |
115 void AutocompleteHistoryManager::OnWebDataServiceRequestDone( | 114 void AutocompleteHistoryManager::OnWebDataServiceRequestDone( |
116 WebDataService::Handle h, | 115 WebDataService::Handle h, |
117 const WDTypedResult* result) { | 116 const WDTypedResult* result) { |
118 DCHECK(pending_query_handle_); | 117 DCHECK(pending_query_handle_); |
119 pending_query_handle_ = 0; | 118 pending_query_handle_ = 0; |
120 | 119 |
121 if (*autofill_enabled_) { | 120 if (*autofill_enabled_) { |
122 DCHECK(result); | 121 DCHECK(result); |
123 SendSuggestions(result); | 122 SendSuggestions(result); |
124 } else { | 123 } else { |
125 SendSuggestions(NULL); | 124 SendSuggestions(NULL); |
126 } | 125 } |
127 } | 126 } |
128 | 127 |
129 AutocompleteHistoryManager::AutocompleteHistoryManager( | 128 AutocompleteHistoryManager::AutocompleteHistoryManager( |
130 Profile* profile, WebDataService* wds) : tab_contents_(NULL), | 129 Profile* profile, WebDataService* wds) : tab_contents_(NULL), |
131 profile_(profile), | 130 profile_(profile), |
132 web_data_service_(wds), | 131 web_data_service_(wds), |
133 pending_query_handle_(0), | 132 pending_query_handle_(0) { |
134 query_id_(0) { | |
135 autofill_enabled_.Init( | 133 autofill_enabled_.Init( |
136 prefs::kAutoFillEnabled, profile_->GetPrefs(), NULL); | 134 prefs::kAutoFillEnabled, profile_->GetPrefs(), NULL); |
137 } | 135 } |
138 | 136 |
139 void AutocompleteHistoryManager::CancelPendingQuery() { | 137 void AutocompleteHistoryManager::CancelPendingQuery() { |
140 if (pending_query_handle_) { | 138 if (pending_query_handle_) { |
141 SendSuggestions(NULL); | 139 SendSuggestions(NULL); |
142 web_data_service_->CancelRequest(pending_query_handle_); | 140 web_data_service_->CancelRequest(pending_query_handle_); |
143 } | 141 } |
144 pending_query_handle_ = 0; | 142 pending_query_handle_ = 0; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 | 178 |
181 void AutocompleteHistoryManager::SendSuggestions(const WDTypedResult* result) { | 179 void AutocompleteHistoryManager::SendSuggestions(const WDTypedResult* result) { |
182 RenderViewHost* host = tab_contents_->render_view_host(); | 180 RenderViewHost* host = tab_contents_->render_view_host(); |
183 if (!host) | 181 if (!host) |
184 return; | 182 return; |
185 | 183 |
186 if (result) { | 184 if (result) { |
187 DCHECK(result->GetType() == AUTOFILL_VALUE_RESULT); | 185 DCHECK(result->GetType() == AUTOFILL_VALUE_RESULT); |
188 const WDResult<std::vector<string16> >* autofill_result = | 186 const WDResult<std::vector<string16> >* autofill_result = |
189 static_cast<const WDResult<std::vector<string16> >*>(result); | 187 static_cast<const WDResult<std::vector<string16> >*>(result); |
190 host->AutocompleteSuggestionsReturned( | 188 host->AutocompleteSuggestionsReturned(autofill_result->GetValue()); |
191 query_id_, autofill_result->GetValue()); | |
192 } else { | 189 } else { |
193 host->AutocompleteSuggestionsReturned( | 190 host->AutocompleteSuggestionsReturned(std::vector<string16>()); |
194 query_id_, std::vector<string16>()); | |
195 } | 191 } |
196 } | 192 } |
OLD | NEW |