diff options
author | georgey@chromium.org <georgey@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-26 01:37:23 +0000 |
---|---|---|
committer | georgey@chromium.org <georgey@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-01-26 01:37:23 +0000 |
commit | fc8893b93c962a89b1d3ce9a8450b68b0e6e95f3 (patch) | |
tree | f3764e23d57726380bf0d5229b6b8f795e18b393 /chrome/browser/autofill/autofill_download.cc | |
parent | 0ba43cc217485a48137ecf2ce6c1ce54cdc67844 (diff) | |
download | chromium_src-fc8893b93c962a89b1d3ce9a8450b68b0e6e95f3.zip chromium_src-fc8893b93c962a89b1d3ce9a8450b68b0e6e95f3.tar.gz chromium_src-fc8893b93c962a89b1d3ce9a8450b68b0e6e95f3.tar.bz2 |
Fix for: Autofill should not ping the server again for the same form
BUG=67039
TEST=unit-tested. Any network sniffer could be used to test that there no subsequent calls to the web for the same form.
Review URL: http://codereview.chromium.org/6366014
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@72585 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/autofill/autofill_download.cc')
-rw-r--r-- | chrome/browser/autofill/autofill_download.cc | 72 |
1 files changed, 71 insertions, 1 deletions
diff --git a/chrome/browser/autofill/autofill_download.cc b/chrome/browser/autofill/autofill_download.cc index cc7e119..2c9cc4c 100644 --- a/chrome/browser/autofill/autofill_download.cc +++ b/chrome/browser/autofill/autofill_download.cc @@ -27,6 +27,10 @@ #define AUTO_FILL_QUERY_SERVER_NAME_START_IN_HEADER "SOMESERVER/" #endif +namespace { +const size_t kMaxFormCacheSize = 16; +}; + struct AutoFillDownloadManager::FormRequestData { std::vector<std::string> form_signatures; AutoFillRequestType request_type; @@ -35,6 +39,7 @@ struct AutoFillDownloadManager::FormRequestData { AutoFillDownloadManager::AutoFillDownloadManager(Profile* profile) : profile_(profile), observer_(NULL), + max_form_cache_size_(kMaxFormCacheSize), next_query_request_(base::Time::Now()), next_upload_request_(base::Time::Now()), positive_upload_rate_(0), @@ -76,12 +81,22 @@ bool AutoFillDownloadManager::StartQueryRequest( std::string form_xml; FormRequestData request_data; if (!FormStructure::EncodeQueryRequest(forms, &request_data.form_signatures, - &form_xml)) + &form_xml)) { return false; + } request_data.request_type = AutoFillDownloadManager::REQUEST_QUERY; metric_logger.Log(AutoFillMetrics::QUERY_SENT); + std::string query_data; + if (CheckCacheForQueryRequest(request_data.form_signatures, &query_data)) { + VLOG(1) << "AutoFillDownloadManager: query request has been retrieved from" + << "the cache"; + if (observer_) + observer_->OnLoadedAutoFillHeuristics(query_data); + return true; + } + return StartRequest(form_xml, request_data); } @@ -188,6 +203,60 @@ bool AutoFillDownloadManager::StartRequest( return true; } +void AutoFillDownloadManager::CacheQueryRequest( + const std::vector<std::string>& forms_in_query, + const std::string& query_data) { + std::string signature = GetCombinedSignature(forms_in_query); + for (QueryRequestCache::iterator it = cached_forms_.begin(); + it != cached_forms_.end(); ++it) { + if (it->first == signature) { + // We hit the cache, move to the first position and return. + std::pair<std::string, std::string> data = *it; + cached_forms_.erase(it); + cached_forms_.push_front(data); + return; + } + } + std::pair<std::string, std::string> data; + data.first = signature; + data.second = query_data; + cached_forms_.push_front(data); + while (cached_forms_.size() > max_form_cache_size_) + cached_forms_.pop_back(); +} + +bool AutoFillDownloadManager::CheckCacheForQueryRequest( + const std::vector<std::string>& forms_in_query, + std::string* query_data) const { + std::string signature = GetCombinedSignature(forms_in_query); + for (QueryRequestCache::const_iterator it = cached_forms_.begin(); + it != cached_forms_.end(); ++it) { + if (it->first == signature) { + // We hit the cache, fill the data and return. + *query_data = it->second; + return true; + } + } + return false; +} + +std::string AutoFillDownloadManager::GetCombinedSignature( + const std::vector<std::string>& forms_in_query) const { + size_t total_size = forms_in_query.size(); + for (size_t i = 0; i < forms_in_query.size(); ++i) + total_size += forms_in_query[i].length(); + std::string signature; + + signature.reserve(total_size); + + for (size_t i = 0; i < forms_in_query.size(); ++i) { + if (i) + signature.append(","); + signature.append(forms_in_query[i]); + } + return signature; +} + void AutoFillDownloadManager::OnURLFetchComplete( const URLFetcher* source, const GURL& url, @@ -250,6 +319,7 @@ void AutoFillDownloadManager::OnURLFetchComplete( VLOG(1) << "AutoFillDownloadManager: " << type_of_request << " request has succeeded"; if (it->second.request_type == AutoFillDownloadManager::REQUEST_QUERY) { + CacheQueryRequest(it->second.form_signatures, data); if (observer_) observer_->OnLoadedAutoFillHeuristics(data); } else { |