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