[email protected] | 10c2d69 | 2012-05-11 05:32:23 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
[email protected] | e4b2fa3 | 2013-03-09 22:56:56 | [diff] [blame] | 5 | #ifndef COMPONENTS_AUTOFILL_BROWSER_AUTOFILL_DOWNLOAD_H_ |
| 6 | #define COMPONENTS_AUTOFILL_BROWSER_AUTOFILL_DOWNLOAD_H_ |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 7 | |
[email protected] | 8f608c5 | 2011-03-30 16:16:31 | [diff] [blame] | 8 | #include <stddef.h> |
[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] | 8f608c5 | 2011-03-30 16:16:31 | [diff] [blame] | 12 | #include <utility> |
[email protected] | fc8893b | 2011-01-26 01:37:23 | [diff] [blame] | 13 | #include <vector> |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 14 | |
[email protected] | c530c85 | 2011-10-24 18:18:34 | [diff] [blame] | 15 | #include "base/compiler_specific.h" |
[email protected] | 76b6d1d5 | 2011-06-15 07:00:36 | [diff] [blame] | 16 | #include "base/gtest_prod_util.h" |
[email protected] | db16347 | 2010-04-02 22:01:20 | [diff] [blame] | 17 | #include "base/time.h" |
[email protected] | e4b2fa3 | 2013-03-09 22:56:56 | [diff] [blame] | 18 | #include "components/autofill/browser/autofill_type.h" |
[email protected] | 15fb2aa | 2012-05-22 22:52:59 | [diff] [blame] | 19 | #include "net/url_request/url_fetcher_delegate.h" |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 20 | |
[email protected] | 6bb2d37 | 2012-09-13 20:27:03 | [diff] [blame] | 21 | namespace content { |
| 22 | class BrowserContext; |
| 23 | } // namespace content |
[email protected] | db16347 | 2010-04-02 22:01:20 | [diff] [blame] | 24 | |
[email protected] | 10c2d69 | 2012-05-11 05:32:23 | [diff] [blame] | 25 | namespace net { |
| 26 | class URLFetcher; |
| 27 | } // namespace net |
| 28 | |
[email protected] | e217c563 | 2013-04-12 19:11:48 | [diff] [blame^] | 29 | namespace autofill { |
| 30 | |
| 31 | class AutofillMetrics; |
| 32 | class FormStructure; |
| 33 | |
[email protected] | d9acdfd | 2011-02-28 00:35:44 | [diff] [blame] | 34 | // Handles getting and updating Autofill heuristics. |
[email protected] | 15fb2aa | 2012-05-22 22:52:59 | [diff] [blame] | 35 | class AutofillDownloadManager : public net::URLFetcherDelegate { |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 36 | public: |
[email protected] | d9acdfd | 2011-02-28 00:35:44 | [diff] [blame] | 37 | enum AutofillRequestType { |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 38 | REQUEST_QUERY, |
| 39 | REQUEST_UPLOAD, |
| 40 | }; |
[email protected] | 6ee50a8 | 2010-11-24 07:04:03 | [diff] [blame] | 41 | |
[email protected] | d9acdfd | 2011-02-28 00:35:44 | [diff] [blame] | 42 | // An interface used to notify clients of AutofillDownloadManager. |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 43 | class Observer { |
| 44 | public: |
[email protected] | 76b6d1d5 | 2011-06-15 07:00:36 | [diff] [blame] | 45 | // Called when field type predictions are successfully received from the |
[email protected] | cc97e48 | 2011-10-28 02:55:19 | [diff] [blame] | 46 | // server. |response_xml| contains the server response. |
[email protected] | 76b6d1d5 | 2011-06-15 07:00:36 | [diff] [blame] | 47 | virtual void OnLoadedServerPredictions(const std::string& response_xml) = 0; |
[email protected] | cc97e48 | 2011-10-28 02:55:19 | [diff] [blame] | 48 | |
| 49 | // These notifications are used to help with testing. |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 50 | // Called when heuristic either successfully considered for upload and |
| 51 | // not send or uploaded. |
[email protected] | cc97e48 | 2011-10-28 02:55:19 | [diff] [blame] | 52 | virtual void OnUploadedPossibleFieldTypes() {} |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 53 | // Called when there was an error during the request. |
| 54 | // |form_signature| - the signature of the requesting form. |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 55 | // |request_type| - type of request that failed. |
[email protected] | 1eb341ad | 2010-05-06 00:22:33 | [diff] [blame] | 56 | // |http_error| - HTTP error code. |
[email protected] | 76b6d1d5 | 2011-06-15 07:00:36 | [diff] [blame] | 57 | virtual void OnServerRequestError(const std::string& form_signature, |
| 58 | AutofillRequestType request_type, |
[email protected] | cc97e48 | 2011-10-28 02:55:19 | [diff] [blame] | 59 | int http_error) {} |
| 60 | |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 61 | protected: |
| 62 | virtual ~Observer() {} |
| 63 | }; |
| 64 | |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 65 | // |observer| - observer to notify on successful completion or error. |
[email protected] | 6bb2d37 | 2012-09-13 20:27:03 | [diff] [blame] | 66 | AutofillDownloadManager(content::BrowserContext* context, |
| 67 | Observer* observer); |
[email protected] | cc97e48 | 2011-10-28 02:55:19 | [diff] [blame] | 68 | virtual ~AutofillDownloadManager(); |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 69 | |
[email protected] | d9acdfd | 2011-02-28 00:35:44 | [diff] [blame] | 70 | // Starts a query request to Autofill servers. The observer is called with the |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 71 | // list of the fields of all requested forms. |
| 72 | // |forms| - array of forms aggregated in this request. |
[email protected] | ddf907ca | 2011-10-21 23:08:28 | [diff] [blame] | 73 | bool StartQueryRequest(const std::vector<FormStructure*>& forms, |
[email protected] | f5c722b | 2011-03-01 01:01:52 | [diff] [blame] | 74 | const AutofillMetrics& metric_logger); |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 75 | |
[email protected] | ca64d23 | 2011-05-06 05:46:00 | [diff] [blame] | 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); |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 86 | |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 87 | private: |
[email protected] | 7ac65175 | 2011-09-16 01:55:50 | [diff] [blame] | 88 | friend class AutofillDownloadTest; |
[email protected] | 76b6d1d5 | 2011-06-15 07:00:36 | [diff] [blame] | 89 | FRIEND_TEST_ALL_PREFIXES(AutofillDownloadTest, QueryAndUploadTest); |
[email protected] | 8e38341 | 2010-10-19 16:57:03 | [diff] [blame] | 90 | |
[email protected] | 14b128b | 2013-03-02 16:49:43 | [diff] [blame] | 91 | static std::string AutofillRequestTypeToString(const AutofillRequestType); |
| 92 | |
[email protected] | 8e38341 | 2010-10-19 16:57:03 | [diff] [blame] | 93 | struct FormRequestData; |
[email protected] | fc8893b | 2011-01-26 01:37:23 | [diff] [blame] | 94 | typedef std::list<std::pair<std::string, std::string> > QueryRequestCache; |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 95 | |
[email protected] | d9acdfd | 2011-02-28 00:35:44 | [diff] [blame] | 96 | // Initiates request to Autofill servers to download/upload heuristics. |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 97 | // |form_xml| - form structure XML to upload/download. |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 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. |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 102 | bool StartRequest(const std::string& form_xml, |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 103 | const FormRequestData& request_data); |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 104 | |
[email protected] | fc8893b | 2011-01-26 01:37:23 | [diff] [blame] | 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 | |
[email protected] | 15fb2aa | 2012-05-22 22:52:59 | [diff] [blame] | 124 | // net::URLFetcherDelegate implementation: |
[email protected] | 10c2d69 | 2012-05-11 05:32:23 | [diff] [blame] | 125 | virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 126 | |
[email protected] | 76b6d1d5 | 2011-06-15 07:00:36 | [diff] [blame] | 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 | |
[email protected] | 6bb2d37 | 2012-09-13 20:27:03 | [diff] [blame] | 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 | |
[email protected] | cc97e48 | 2011-10-28 02:55:19 | [diff] [blame] | 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 |
[email protected] | db16347 | 2010-04-02 22:01:20 | [diff] [blame] | 143 | |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 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. |
[email protected] | 10c2d69 | 2012-05-11 05:32:23 | [diff] [blame] | 147 | std::map<net::URLFetcher*, FormRequestData> url_fetchers_; |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 148 | |
[email protected] | fc8893b | 2011-01-26 01:37:23 | [diff] [blame] | 149 | // Cached QUERY requests. |
| 150 | QueryRequestCache cached_forms_; |
| 151 | size_t max_form_cache_size_; |
| 152 | |
[email protected] | db16347 | 2010-04-02 22:01:20 | [diff] [blame] | 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 | |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 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_; |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 166 | }; |
| 167 | |
[email protected] | e217c563 | 2013-04-12 19:11:48 | [diff] [blame^] | 168 | } // namespace autofill |
| 169 | |
[email protected] | e4b2fa3 | 2013-03-09 22:56:56 | [diff] [blame] | 170 | #endif // COMPONENTS_AUTOFILL_BROWSER_AUTOFILL_DOWNLOAD_H_ |