autofill_download.h revision 1677284a2f4414f9a4563d58bc955d6749c4cc2a
1// Copyright (c) 2011 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#ifndef CHROME_BROWSER_AUTOFILL_AUTOFILL_DOWNLOAD_H_ 6#define CHROME_BROWSER_AUTOFILL_AUTOFILL_DOWNLOAD_H_ 7#pragma once 8 9#include <list> 10#include <map> 11#include <string> 12#include <vector> 13 14#include "base/scoped_vector.h" 15#include "base/time.h" 16#include "chrome/browser/autofill/autofill_profile.h" 17#include "chrome/browser/autofill/field_types.h" 18#include "chrome/browser/autofill/form_structure.h" 19#include "chrome/common/net/url_fetcher.h" 20 21#ifdef ANDROID 22#include "android/autofill/url_fetcher_proxy.h" 23#endif 24 25class AutofillMetrics; 26class Profile; 27 28// Handles getting and updating Autofill heuristics. 29class AutofillDownloadManager : public URLFetcher::Delegate { 30 public: 31 enum AutofillRequestType { 32 REQUEST_QUERY, 33 REQUEST_UPLOAD, 34 }; 35 36 // An interface used to notify clients of AutofillDownloadManager. 37 // Notifications are *not* guaranteed to be called. 38 class Observer { 39 public: 40 // Called when field types are successfully received from the server. 41 // |heuristic_xml| - server response. 42 virtual void OnLoadedAutofillHeuristics( 43 const std::string& heuristic_xml) = 0; 44 // Called when heuristic either successfully considered for upload and 45 // not send or uploaded. 46 // |form_signature| - the signature of the requesting form. 47 virtual void OnUploadedAutofillHeuristics( 48 const std::string& form_signature) = 0; 49 // Called when there was an error during the request. 50 // |form_signature| - the signature of the requesting form. 51 // |request_type| - type of request that failed. 52 // |http_error| - HTTP error code. 53 virtual void OnHeuristicsRequestError(const std::string& form_signature, 54 AutofillRequestType request_type, 55 int http_error) = 0; 56 protected: 57 virtual ~Observer() {} 58 }; 59 60 // |profile| can be NULL in unit-tests only. 61 explicit AutofillDownloadManager(Profile* profile); 62 virtual ~AutofillDownloadManager(); 63 64 // |observer| - observer to notify on successful completion or error. 65 void SetObserver(AutofillDownloadManager::Observer *observer); 66 67 // Starts a query request to Autofill servers. The observer is called with the 68 // list of the fields of all requested forms. 69 // |forms| - array of forms aggregated in this request. 70 bool StartQueryRequest(const ScopedVector<FormStructure>& forms, 71 const AutofillMetrics& metric_logger); 72 73 // Start upload request if necessary. The probability of request going 74 // over the wire are GetPositiveUploadRate() if it was matched by 75 // Autofill, GetNegativeUploadRate() otherwise. Observer will be called 76 // even if there was no actual trip over the wire. 77 // |form| - form sent in this request. 78 // |form_was_matched| - true if form was matched by the Autofill. 79 bool StartUploadRequest(const FormStructure& form, bool form_was_matched); 80 81 // Cancels pending request. 82 // |form_signature| - signature of the form being cancelled. Warning: 83 // for query request if more than one form sent in the request, all other 84 // forms will be cancelled as well. 85 // |request_type| - type of the request. 86 bool CancelRequest(const std::string& form_signature, 87 AutofillRequestType request_type); 88 89 // Probability of the form upload. Between 0 (no upload) and 1 (upload all). 90 // GetPositiveUploadRate() is for matched forms, 91 // GetNegativeUploadRate() for non matched. 92 double GetPositiveUploadRate() const; 93 double GetNegativeUploadRate() const; 94 // These functions called very rarely outside of the unit-tests. With current 95 // percentages, they would be called once per 100 auto-fillable forms filled 96 // and submitted by user. The order of magnitude would remain similar in the 97 // future. 98 void SetPositiveUploadRate(double rate); 99 void SetNegativeUploadRate(double rate); 100 101 private: 102 friend class AutofillDownloadTestHelper; // unit-test. 103 104 struct FormRequestData; 105 typedef std::list<std::pair<std::string, std::string> > QueryRequestCache; 106 107 // Initiates request to Autofill servers to download/upload heuristics. 108 // |form_xml| - form structure XML to upload/download. 109 // |request_data| - form signature hash(es) and indicator if it was a query. 110 // |request_data.query| - if true the data is queried and observer notified 111 // with new data, if available. If false heuristic data is uploaded to our 112 // servers. 113 bool StartRequest(const std::string& form_xml, 114 const FormRequestData& request_data); 115 116 // Each request is page visited. We store last |max_form_cache_size| 117 // request, to avoid going over the wire. Set to 16 in constructor. Warning: 118 // the search is linear (newest first), so do not make the constant very big. 119 void set_max_form_cache_size(size_t max_form_cache_size) { 120 max_form_cache_size_ = max_form_cache_size; 121 } 122 123 // Caches query request. |forms_in_query| is a vector of form signatures in 124 // the query. |query_data| is the successful data returned over the wire. 125 void CacheQueryRequest(const std::vector<std::string>& forms_in_query, 126 const std::string& query_data); 127 // Returns true if query is in the cache, while filling |query_data|, false 128 // otherwise. |forms_in_query| is a vector of form signatures in the query. 129 bool CheckCacheForQueryRequest(const std::vector<std::string>& forms_in_query, 130 std::string* query_data) const; 131 // Concatenates |forms_in_query| into one signature. 132 std::string GetCombinedSignature( 133 const std::vector<std::string>& forms_in_query) const; 134 135 // URLFetcher::Delegate implementation: 136 virtual void OnURLFetchComplete(const URLFetcher* source, 137 const GURL& url, 138 const net::URLRequestStatus& status, 139 int response_code, 140 const ResponseCookies& cookies, 141 const std::string& data); 142 143 // Profile for preference storage. 144 Profile* profile_; 145 146 // For each requested form for both query and upload we create a separate 147 // request and save its info. As url fetcher is identified by its address 148 // we use a map between fetchers and info. 149 std::map<URLFetcher*, FormRequestData> url_fetchers_; 150 AutofillDownloadManager::Observer *observer_; 151 152 // Cached QUERY requests. 153 QueryRequestCache cached_forms_; 154 size_t max_form_cache_size_; 155 156 // Time when next query/upload requests are allowed. If 50x HTTP received, 157 // exponential back off is initiated, so this times will be in the future 158 // for awhile. 159 base::Time next_query_request_; 160 base::Time next_upload_request_; 161 162 // |positive_upload_rate_| is for matched forms, 163 // |negative_upload_rate_| for non matched. 164 double positive_upload_rate_; 165 double negative_upload_rate_; 166 167 // Needed for unit-test. 168 int fetcher_id_for_unittest_; 169}; 170 171#endif // CHROME_BROWSER_AUTOFILL_AUTOFILL_DOWNLOAD_H_ 172 173