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
diff --git a/chrome/browser/autofill/autofill_download.h b/chrome/browser/autofill/autofill_download.h
index 8937557f..157062c 100644
--- a/chrome/browser/autofill/autofill_download.h
+++ b/chrome/browser/autofill/autofill_download.h
@@ -6,8 +6,10 @@
 #define CHROME_BROWSER_AUTOFILL_AUTOFILL_DOWNLOAD_H_
 #pragma once
 
+#include <list>
 #include <map>
 #include <string>
+#include <vector>
 
 #include "base/scoped_vector.h"
 #include "base/time.h"
@@ -96,6 +98,7 @@
   friend class AutoFillDownloadTestHelper;  // unit-test.
 
   struct FormRequestData;
+  typedef std::list<std::pair<std::string, std::string> > QueryRequestCache;
 
   // Initiates request to AutoFill servers to download/upload heuristics.
   // |form_xml| - form structure XML to upload/download.
@@ -106,6 +109,25 @@
   bool StartRequest(const std::string& form_xml,
                     const FormRequestData& request_data);
 
+  // Each request is page visited. We store last |max_form_cache_size|
+  // request, to avoid going over the wire. Set to 16 in constructor. Warning:
+  // the search is linear (newest first), so do not make the constant very big.
+  void set_max_form_cache_size(size_t max_form_cache_size) {
+    max_form_cache_size_ = max_form_cache_size;
+  }
+
+  // Caches query request. |forms_in_query| is a vector of form signatures in
+  // the query. |query_data| is the successful data returned over the wire.
+  void CacheQueryRequest(const std::vector<std::string>& forms_in_query,
+                         const std::string& query_data);
+  // Returns true if query is in the cache, while filling |query_data|, false
+  // otherwise. |forms_in_query| is a vector of form signatures in the query.
+  bool CheckCacheForQueryRequest(const std::vector<std::string>& forms_in_query,
+                                 std::string* query_data) const;
+  // Concatenates |forms_in_query| into one signature.
+  std::string GetCombinedSignature(
+      const std::vector<std::string>& forms_in_query) const;
+
   // URLFetcher::Delegate implementation:
   virtual void OnURLFetchComplete(const URLFetcher* source,
                                   const GURL& url,
@@ -123,6 +145,10 @@
   std::map<URLFetcher*, FormRequestData> url_fetchers_;
   AutoFillDownloadManager::Observer *observer_;
 
+  // Cached QUERY requests.
+  QueryRequestCache cached_forms_;
+  size_t max_form_cache_size_;
+
   // Time when next query/upload requests are allowed. If 50x HTTP received,
   // exponential back off is initiated, so this times will be in the future
   // for awhile.