| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 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 | |
| 8 #include <stddef.h> | |
| 9 #include <list> | |
| 10 #include <map> | |
| 11 #include <string> | |
| 12 #include <utility> | |
| 13 #include <vector> | |
| 14 | |
| 15 #include "base/compiler_specific.h" | |
| 16 #include "base/gtest_prod_util.h" | |
| 17 #include "base/time.h" | |
| 18 #include "chrome/browser/autofill/autofill_type.h" | |
| 19 #include "net/url_request/url_fetcher_delegate.h" | |
| 20 | |
| 21 class AutofillMetrics; | |
| 22 class FormStructure; | |
| 23 | |
| 24 namespace content { | |
| 25 class BrowserContext; | |
| 26 } // namespace content | |
| 27 | |
| 28 namespace net { | |
| 29 class URLFetcher; | |
| 30 } // namespace net | |
| 31 | |
| 32 // Handles getting and updating Autofill heuristics. | |
| 33 class AutofillDownloadManager : public net::URLFetcherDelegate { | |
| 34 public: | |
| 35 enum AutofillRequestType { | |
| 36 REQUEST_QUERY, | |
| 37 REQUEST_UPLOAD, | |
| 38 }; | |
| 39 | |
| 40 // An interface used to notify clients of AutofillDownloadManager. | |
| 41 class Observer { | |
| 42 public: | |
| 43 // Called when field type predictions are successfully received from the | |
| 44 // server. |response_xml| contains the server response. | |
| 45 virtual void OnLoadedServerPredictions(const std::string& response_xml) = 0; | |
| 46 | |
| 47 // These notifications are used to help with testing. | |
| 48 // Called when heuristic either successfully considered for upload and | |
| 49 // not send or uploaded. | |
| 50 virtual void OnUploadedPossibleFieldTypes() {} | |
| 51 // Called when there was an error during the request. | |
| 52 // |form_signature| - the signature of the requesting form. | |
| 53 // |request_type| - type of request that failed. | |
| 54 // |http_error| - HTTP error code. | |
| 55 virtual void OnServerRequestError(const std::string& form_signature, | |
| 56 AutofillRequestType request_type, | |
| 57 int http_error) {} | |
| 58 | |
| 59 protected: | |
| 60 virtual ~Observer() {} | |
| 61 }; | |
| 62 | |
| 63 // |observer| - observer to notify on successful completion or error. | |
| 64 AutofillDownloadManager(content::BrowserContext* context, | |
| 65 Observer* observer); | |
| 66 virtual ~AutofillDownloadManager(); | |
| 67 | |
| 68 // Starts a query request to Autofill servers. The observer is called with the | |
| 69 // list of the fields of all requested forms. | |
| 70 // |forms| - array of forms aggregated in this request. | |
| 71 bool StartQueryRequest(const std::vector<FormStructure*>& forms, | |
| 72 const AutofillMetrics& metric_logger); | |
| 73 | |
| 74 // Starts an upload request for the given |form|, unless throttled by the | |
| 75 // server. The probability of the request going over the wire is | |
| 76 // GetPositiveUploadRate() if |form_was_autofilled| is true, or | |
| 77 // GetNegativeUploadRate() otherwise. The observer will be called even if | |
| 78 // there was no actual trip over the wire. | |
| 79 // |available_field_types| should contain the types for which we have data | |
| 80 // stored on the local client. | |
| 81 bool StartUploadRequest(const FormStructure& form, | |
| 82 bool form_was_autofilled, | |
| 83 const FieldTypeSet& available_field_types); | |
| 84 | |
| 85 private: | |
| 86 friend class AutofillDownloadTest; | |
| 87 FRIEND_TEST_ALL_PREFIXES(AutofillDownloadTest, QueryAndUploadTest); | |
| 88 | |
| 89 static std::string AutofillRequestTypeToString(const AutofillRequestType); | |
| 90 | |
| 91 struct FormRequestData; | |
| 92 typedef std::list<std::pair<std::string, std::string> > QueryRequestCache; | |
| 93 | |
| 94 // Initiates request to Autofill servers to download/upload heuristics. | |
| 95 // |form_xml| - form structure XML to upload/download. | |
| 96 // |request_data| - form signature hash(es) and indicator if it was a query. | |
| 97 // |request_data.query| - if true the data is queried and observer notified | |
| 98 // with new data, if available. If false heuristic data is uploaded to our | |
| 99 // servers. | |
| 100 bool StartRequest(const std::string& form_xml, | |
| 101 const FormRequestData& request_data); | |
| 102 | |
| 103 // Each request is page visited. We store last |max_form_cache_size| | |
| 104 // request, to avoid going over the wire. Set to 16 in constructor. Warning: | |
| 105 // the search is linear (newest first), so do not make the constant very big. | |
| 106 void set_max_form_cache_size(size_t max_form_cache_size) { | |
| 107 max_form_cache_size_ = max_form_cache_size; | |
| 108 } | |
| 109 | |
| 110 // Caches query request. |forms_in_query| is a vector of form signatures in | |
| 111 // the query. |query_data| is the successful data returned over the wire. | |
| 112 void CacheQueryRequest(const std::vector<std::string>& forms_in_query, | |
| 113 const std::string& query_data); | |
| 114 // Returns true if query is in the cache, while filling |query_data|, false | |
| 115 // otherwise. |forms_in_query| is a vector of form signatures in the query. | |
| 116 bool CheckCacheForQueryRequest(const std::vector<std::string>& forms_in_query, | |
| 117 std::string* query_data) const; | |
| 118 // Concatenates |forms_in_query| into one signature. | |
| 119 std::string GetCombinedSignature( | |
| 120 const std::vector<std::string>& forms_in_query) const; | |
| 121 | |
| 122 // net::URLFetcherDelegate implementation: | |
| 123 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
| 124 | |
| 125 // Probability of the form upload. Between 0 (no upload) and 1 (upload all). | |
| 126 // GetPositiveUploadRate() is for matched forms, | |
| 127 // GetNegativeUploadRate() for non-matched. | |
| 128 double GetPositiveUploadRate() const; | |
| 129 double GetNegativeUploadRate() const; | |
| 130 void SetPositiveUploadRate(double rate); | |
| 131 void SetNegativeUploadRate(double rate); | |
| 132 | |
| 133 // The pointer value is const, so this can only be set in the | |
| 134 // constructor. Must not be null. | |
| 135 content::BrowserContext* const browser_context_; // WEAK | |
| 136 | |
| 137 // The observer to notify when server predictions are successfully received. | |
| 138 // The pointer value is const, so this can only be set in the constructor. | |
| 139 // Must not be null. | |
| 140 AutofillDownloadManager::Observer* const observer_; // WEAK | |
| 141 | |
| 142 // For each requested form for both query and upload we create a separate | |
| 143 // request and save its info. As url fetcher is identified by its address | |
| 144 // we use a map between fetchers and info. | |
| 145 std::map<net::URLFetcher*, FormRequestData> url_fetchers_; | |
| 146 | |
| 147 // Cached QUERY requests. | |
| 148 QueryRequestCache cached_forms_; | |
| 149 size_t max_form_cache_size_; | |
| 150 | |
| 151 // Time when next query/upload requests are allowed. If 50x HTTP received, | |
| 152 // exponential back off is initiated, so this times will be in the future | |
| 153 // for awhile. | |
| 154 base::Time next_query_request_; | |
| 155 base::Time next_upload_request_; | |
| 156 | |
| 157 // |positive_upload_rate_| is for matched forms, | |
| 158 // |negative_upload_rate_| for non matched. | |
| 159 double positive_upload_rate_; | |
| 160 double negative_upload_rate_; | |
| 161 | |
| 162 // Needed for unit-test. | |
| 163 int fetcher_id_for_unittest_; | |
| 164 }; | |
| 165 | |
| 166 #endif // CHROME_BROWSER_AUTOFILL_AUTOFILL_DOWNLOAD_H_ | |
| OLD | NEW |