diff options
author | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-02 19:55:45 +0000 |
---|---|---|
committer | jhawkins@chromium.org <jhawkins@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-03-02 19:55:45 +0000 |
commit | 6db0def9b89c58b9ade972ac1e9b0d4f8f88d858 (patch) | |
tree | d9e0a78de0fd72ec56e424750f3b399e89010f70 /chrome/browser/autocomplete_history_manager.cc | |
parent | 7fce6e9a1e243fe31b686ef2123c453f7f210450 (diff) | |
download | chromium_src-6db0def9b89c58b9ade972ac1e9b0d4f8f88d858.zip chromium_src-6db0def9b89c58b9ade972ac1e9b0d4f8f88d858.tar.gz chromium_src-6db0def9b89c58b9ade972ac1e9b0d4f8f88d858.tar.bz2 |
Rename FormFieldHistoryManager to AutocompleteHistory manager to better reflect its relation to the Autocomplete feature.
BUG=33032
TEST=none
Review URL: http://codereview.chromium.org/661323
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@40408 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/autocomplete_history_manager.cc')
-rw-r--r-- | chrome/browser/autocomplete_history_manager.cc | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/chrome/browser/autocomplete_history_manager.cc b/chrome/browser/autocomplete_history_manager.cc new file mode 100644 index 0000000..4b98399 --- /dev/null +++ b/chrome/browser/autocomplete_history_manager.cc @@ -0,0 +1,151 @@ +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/autocomplete_history_manager.h" + +#include <vector> + +#include "base/string_util.h" +#include "chrome/browser/pref_service.h" +#include "chrome/browser/profile.h" +#include "chrome/browser/renderer_host/render_view_host.h" +#include "chrome/browser/tab_contents/tab_contents.h" +#include "chrome/common/pref_names.h" +#include "webkit/glue/form_field_values.h" + +// Limit on the number of suggestions to appear in the pop-up menu under an +// text input element in a form. +static const int kMaxAutofillMenuItems = 6; + +// static +void AutocompleteHistoryManager::RegisterUserPrefs(PrefService* prefs) { + prefs->RegisterBooleanPref(prefs::kFormAutofillEnabled, true); +} + +AutocompleteHistoryManager::AutocompleteHistoryManager( + TabContents* tab_contents) : tab_contents_(tab_contents), + pending_query_handle_(0), + query_id_(0) { + form_autofill_enabled_.Init(prefs::kFormAutofillEnabled, + profile()->GetPrefs(), NULL); +} + +AutocompleteHistoryManager::~AutocompleteHistoryManager() { + CancelPendingQuery(); +} + +void AutocompleteHistoryManager::CancelPendingQuery() { + if (pending_query_handle_) { + SendSuggestions(NULL); + WebDataService* web_data_service = + profile()->GetWebDataService(Profile::EXPLICIT_ACCESS); + if (!web_data_service) { + NOTREACHED(); + return; + } + web_data_service->CancelRequest(pending_query_handle_); + } + pending_query_handle_ = 0; +} + +Profile* AutocompleteHistoryManager::profile() { + return tab_contents_->profile(); +} + +void AutocompleteHistoryManager::FormFieldValuesSubmitted( + const webkit_glue::FormFieldValues& form) { + StoreFormEntriesInWebDatabase(form); +} + +bool AutocompleteHistoryManager::GetFormFieldHistorySuggestions(int query_id, + const string16& name, + const string16& prefix) { + if (!*form_autofill_enabled_) + return false; + + WebDataService* web_data_service = + profile()->GetWebDataService(Profile::EXPLICIT_ACCESS); + if (!web_data_service) { + NOTREACHED(); + return false; + } + + CancelPendingQuery(); + + query_id_ = query_id; + + pending_query_handle_ = web_data_service->GetFormValuesForElementName( + name, prefix, kMaxAutofillMenuItems, this); + return true; +} + +void AutocompleteHistoryManager::RemoveFormFieldHistoryEntry( + const string16& name, const string16& value) { + WebDataService* web_data_service = + profile()->GetWebDataService(Profile::EXPLICIT_ACCESS); + if (!web_data_service) { + NOTREACHED(); + return; + } + + web_data_service->RemoveFormValueForElementName(name, value); +} + +void AutocompleteHistoryManager::OnWebDataServiceRequestDone( + WebDataService::Handle h, + const WDTypedResult* result) { + DCHECK(pending_query_handle_); + pending_query_handle_ = 0; + + if (*form_autofill_enabled_) { + DCHECK(result); + SendSuggestions(result); + } else { + SendSuggestions(NULL); + } +} + +void AutocompleteHistoryManager::StoreFormEntriesInWebDatabase( + const webkit_glue::FormFieldValues& form) { + if (!*form_autofill_enabled_) + return; + + if (profile()->IsOffTheRecord()) + return; + + // We put the following restriction on stored FormFields: + // - input_type() == WebInputElement::Text + // - non-empty name + // - non-empty value + // - non-empty form_control_type() + std::vector<webkit_glue::FormField> values; + for (std::vector<webkit_glue::FormField>::const_iterator iter = + form.elements.begin(); + iter != form.elements.end(); ++iter) { + if (iter->input_type() == WebKit::WebInputElement::Text && + !iter->value().empty() && + !iter->name().empty() && + !iter->form_control_type().empty()) + values.push_back(*iter); + } + + profile()->GetWebDataService(Profile::EXPLICIT_ACCESS)-> + AddFormFieldValues(form.elements); +} + +void AutocompleteHistoryManager::SendSuggestions(const WDTypedResult* result) { + RenderViewHost* host = tab_contents_->render_view_host(); + if (!host) + return; + if (result) { + DCHECK(result->GetType() == AUTOFILL_VALUE_RESULT); + const WDResult<std::vector<string16> >* autofill_result = + static_cast<const WDResult<std::vector<string16> >*>(result); + host->AutocompleteSuggestionsReturned( + query_id_, autofill_result->GetValue(), -1); + } else { + host->AutocompleteSuggestionsReturned( + query_id_, std::vector<string16>(), -1); + } +} |