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