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