[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 | |
| 9 | #include <map> |
| 10 | #include <string> |
| 11 | |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 12 | #include "base/scoped_vector.h" |
[email protected] | db16347 | 2010-04-02 22:01:20 | [diff] [blame] | 13 | #include "base/time.h" |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 14 | #include "chrome/browser/autofill/autofill_profile.h" |
| 15 | #include "chrome/browser/autofill/field_types.h" |
| 16 | #include "chrome/browser/autofill/form_structure.h" |
[email protected] | 68d2a05f | 2010-05-07 21:39:55 | [diff] [blame] | 17 | #include "chrome/common/net/url_fetcher.h" |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 18 | |
[email protected] | 60813237 | 2010-12-15 04:45:20 | [diff] [blame] | 19 | class AutoFillMetrics; |
[email protected] | db16347 | 2010-04-02 22:01:20 | [diff] [blame] | 20 | class Profile; |
| 21 | |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 22 | // Handles getting and updating AutoFill heuristics. |
| 23 | class AutoFillDownloadManager : public URLFetcher::Delegate { |
| 24 | public: |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 25 | enum AutoFillRequestType { |
| 26 | REQUEST_QUERY, |
| 27 | REQUEST_UPLOAD, |
| 28 | }; |
[email protected] | 6ee50a8 | 2010-11-24 07:04:03 | [diff] [blame] | 29 | |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 30 | // An interface used to notify clients of AutoFillDownloadManager. |
[email protected] | db16347 | 2010-04-02 22:01:20 | [diff] [blame] | 31 | // Notifications are *not* guaranteed to be called. |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 32 | class Observer { |
| 33 | public: |
[email protected] | 1eb341ad | 2010-05-06 00:22:33 | [diff] [blame] | 34 | // Called when field types are successfully received from the server. |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 35 | // |heuristic_xml| - server response. |
| 36 | virtual void OnLoadedAutoFillHeuristics( |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 37 | const std::string& heuristic_xml) = 0; |
| 38 | // Called when heuristic either successfully considered for upload and |
| 39 | // not send or uploaded. |
| 40 | // |form_signature| - the signature of the requesting form. |
| 41 | virtual void OnUploadedAutoFillHeuristics( |
| 42 | const std::string& form_signature) = 0; |
| 43 | // Called when there was an error during the request. |
| 44 | // |form_signature| - the signature of the requesting form. |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 45 | // |request_type| - type of request that failed. |
[email protected] | 1eb341ad | 2010-05-06 00:22:33 | [diff] [blame] | 46 | // |http_error| - HTTP error code. |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 47 | virtual void OnHeuristicsRequestError(const std::string& form_signature, |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 48 | AutoFillRequestType request_type, |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 49 | int http_error) = 0; |
| 50 | protected: |
| 51 | virtual ~Observer() {} |
| 52 | }; |
| 53 | |
[email protected] | db16347 | 2010-04-02 22:01:20 | [diff] [blame] | 54 | // |profile| can be NULL in unit-tests only. |
| 55 | explicit AutoFillDownloadManager(Profile* profile); |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 56 | virtual ~AutoFillDownloadManager(); |
| 57 | |
| 58 | // |observer| - observer to notify on successful completion or error. |
| 59 | void SetObserver(AutoFillDownloadManager::Observer *observer); |
| 60 | |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 61 | // Starts a query request to AutoFill servers. The observer is called with the |
| 62 | // list of the fields of all requested forms. |
| 63 | // |forms| - array of forms aggregated in this request. |
[email protected] | 60813237 | 2010-12-15 04:45:20 | [diff] [blame] | 64 | bool StartQueryRequest(const ScopedVector<FormStructure>& forms, |
| 65 | const AutoFillMetrics& metric_logger); |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 66 | |
| 67 | // Start upload request if necessary. The probability of request going |
[email protected] | db16347 | 2010-04-02 22:01:20 | [diff] [blame] | 68 | // over the wire are GetPositiveUploadRate() if it was matched by |
| 69 | // AutoFill, GetNegativeUploadRate() otherwise. Observer will be called |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 70 | // even if there was no actual trip over the wire. |
| 71 | // |form| - form sent in this request. |
| 72 | // |form_was_matched| - true if form was matched by the AutoFill. |
| 73 | bool StartUploadRequest(const FormStructure& form, bool form_was_matched); |
| 74 | |
| 75 | // Cancels pending request. |
| 76 | // |form_signature| - signature of the form being cancelled. Warning: |
| 77 | // for query request if more than one form sent in the request, all other |
| 78 | // forms will be cancelled as well. |
| 79 | // |request_type| - type of the request. |
| 80 | bool CancelRequest(const std::string& form_signature, |
| 81 | AutoFillRequestType request_type); |
| 82 | |
[email protected] | db16347 | 2010-04-02 22:01:20 | [diff] [blame] | 83 | // Probability of the form upload. Between 0 (no upload) and 1 (upload all). |
| 84 | // GetPositiveUploadRate() is for matched forms, |
| 85 | // GetNegativeUploadRate() for non matched. |
| 86 | double GetPositiveUploadRate() const; |
| 87 | double GetNegativeUploadRate() const; |
| 88 | // These functions called very rarely outside of theunit-tests. With current |
| 89 | // percentages, they would be called once per 100 auto-fillable forms filled |
| 90 | // and submitted by user. The order of magnitude would remain similar in the |
| 91 | // future. |
| 92 | void SetPositiveUploadRate(double rate); |
| 93 | void SetNegativeUploadRate(double rate); |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 94 | |
| 95 | private: |
| 96 | friend class AutoFillDownloadTestHelper; // unit-test. |
[email protected] | 8e38341 | 2010-10-19 16:57:03 | [diff] [blame] | 97 | |
| 98 | struct FormRequestData; |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 99 | |
| 100 | // Initiates request to AutoFill servers to download/upload heuristics. |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 101 | // |form_xml| - form structure XML to upload/download. |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 102 | // |request_data| - form signature hash(es) and indicator if it was a query. |
| 103 | // |request_data.query| - if true the data is queried and observer notified |
| 104 | // with new data, if available. If false heuristic data is uploaded to our |
| 105 | // servers. |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 106 | bool StartRequest(const std::string& form_xml, |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 107 | const FormRequestData& request_data); |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 108 | |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 109 | // URLFetcher::Delegate implementation: |
| 110 | virtual void OnURLFetchComplete(const URLFetcher* source, |
| 111 | const GURL& url, |
[email protected] | 27a112c | 2011-01-06 04:19:30 | [diff] [blame] | 112 | const net::URLRequestStatus& status, |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 113 | int response_code, |
| 114 | const ResponseCookies& cookies, |
| 115 | const std::string& data); |
| 116 | |
[email protected] | db16347 | 2010-04-02 22:01:20 | [diff] [blame] | 117 | // Profile for preference storage. |
| 118 | Profile* profile_; |
| 119 | |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 120 | // For each requested form for both query and upload we create a separate |
| 121 | // request and save its info. As url fetcher is identified by its address |
| 122 | // we use a map between fetchers and info. |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 123 | std::map<URLFetcher*, FormRequestData> url_fetchers_; |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 124 | AutoFillDownloadManager::Observer *observer_; |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 125 | |
[email protected] | db16347 | 2010-04-02 22:01:20 | [diff] [blame] | 126 | // Time when next query/upload requests are allowed. If 50x HTTP received, |
| 127 | // exponential back off is initiated, so this times will be in the future |
| 128 | // for awhile. |
| 129 | base::Time next_query_request_; |
| 130 | base::Time next_upload_request_; |
| 131 | |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 132 | // |positive_upload_rate_| is for matched forms, |
| 133 | // |negative_upload_rate_| for non matched. |
| 134 | double positive_upload_rate_; |
| 135 | double negative_upload_rate_; |
| 136 | |
| 137 | // Needed for unit-test. |
| 138 | int fetcher_id_for_unittest_; |
| 139 | bool is_testing_; |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | #endif // CHROME_BROWSER_AUTOFILL_AUTOFILL_DOWNLOAD_H_ |
| 143 | |