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