[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_ |
| 7 | |
| 8 | #include <map> |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 9 | #include <vector> |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 10 | #include <string> |
| 11 | |
| 12 | #include "base/scoped_ptr.h" |
| 13 | #include "base/scoped_vector.h" |
| 14 | #include "base/string16.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" |
| 19 | #include "chrome/browser/net/url_fetcher.h" |
| 20 | |
[email protected] | db16347 | 2010-04-02 22:01:20 | [diff] [blame] | 21 | class Profile; |
| 22 | |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 23 | // Handles getting and updating AutoFill heuristics. |
| 24 | class AutoFillDownloadManager : public URLFetcher::Delegate { |
| 25 | public: |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 26 | enum AutoFillRequestType { |
| 27 | REQUEST_QUERY, |
| 28 | REQUEST_UPLOAD, |
| 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. |
| 64 | bool StartQueryRequest(const ScopedVector<FormStructure>& forms); |
| 65 | |
| 66 | // Start upload request if necessary. The probability of request going |
[email protected] | db16347 | 2010-04-02 22:01:20 | [diff] [blame] | 67 | // over the wire are GetPositiveUploadRate() if it was matched by |
| 68 | // AutoFill, GetNegativeUploadRate() otherwise. Observer will be called |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 69 | // even if there was no actual trip over the wire. |
| 70 | // |form| - form sent in this request. |
| 71 | // |form_was_matched| - true if form was matched by the AutoFill. |
| 72 | bool StartUploadRequest(const FormStructure& form, bool form_was_matched); |
| 73 | |
| 74 | // Cancels pending request. |
| 75 | // |form_signature| - signature of the form being cancelled. Warning: |
| 76 | // for query request if more than one form sent in the request, all other |
| 77 | // forms will be cancelled as well. |
| 78 | // |request_type| - type of the request. |
| 79 | bool CancelRequest(const std::string& form_signature, |
| 80 | AutoFillRequestType request_type); |
| 81 | |
[email protected] | db16347 | 2010-04-02 22:01:20 | [diff] [blame] | 82 | // Probability of the form upload. Between 0 (no upload) and 1 (upload all). |
| 83 | // GetPositiveUploadRate() is for matched forms, |
| 84 | // GetNegativeUploadRate() for non matched. |
| 85 | double GetPositiveUploadRate() const; |
| 86 | double GetNegativeUploadRate() const; |
| 87 | // These functions called very rarely outside of theunit-tests. With current |
| 88 | // percentages, they would be called once per 100 auto-fillable forms filled |
| 89 | // and submitted by user. The order of magnitude would remain similar in the |
| 90 | // future. |
| 91 | void SetPositiveUploadRate(double rate); |
| 92 | void SetNegativeUploadRate(double rate); |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 93 | |
| 94 | private: |
| 95 | friend class AutoFillDownloadTestHelper; // unit-test. |
| 96 | struct FormRequestData { |
| 97 | std::vector<std::string> form_signatures; |
| 98 | AutoFillRequestType request_type; |
| 99 | }; |
| 100 | |
| 101 | // Initiates request to AutoFill servers to download/upload heuristics. |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 102 | // |form_xml| - form structure XML to upload/download. |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 103 | // |request_data| - form signature hash(es) and indicator if it was a query. |
| 104 | // |request_data.query| - if true the data is queried and observer notified |
| 105 | // with new data, if available. If false heuristic data is uploaded to our |
| 106 | // servers. |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 107 | bool StartRequest(const std::string& form_xml, |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 108 | const FormRequestData& request_data); |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 109 | |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 110 | // URLFetcher::Delegate implementation: |
| 111 | virtual void OnURLFetchComplete(const URLFetcher* source, |
| 112 | const GURL& url, |
| 113 | const URLRequestStatus& status, |
| 114 | int response_code, |
| 115 | const ResponseCookies& cookies, |
| 116 | const std::string& data); |
| 117 | |
[email protected] | db16347 | 2010-04-02 22:01:20 | [diff] [blame] | 118 | // Profile for preference storage. |
| 119 | Profile* profile_; |
| 120 | |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 121 | // For each requested form for both query and upload we create a separate |
| 122 | // request and save its info. As url fetcher is identified by its address |
| 123 | // we use a map between fetchers and info. |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 124 | std::map<URLFetcher*, FormRequestData> url_fetchers_; |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 125 | AutoFillDownloadManager::Observer *observer_; |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 126 | |
[email protected] | db16347 | 2010-04-02 22:01:20 | [diff] [blame] | 127 | // Time when next query/upload requests are allowed. If 50x HTTP received, |
| 128 | // exponential back off is initiated, so this times will be in the future |
| 129 | // for awhile. |
| 130 | base::Time next_query_request_; |
| 131 | base::Time next_upload_request_; |
| 132 | |
[email protected] | 61d96bf | 2010-03-29 05:50:37 | [diff] [blame] | 133 | // |positive_upload_rate_| is for matched forms, |
| 134 | // |negative_upload_rate_| for non matched. |
| 135 | double positive_upload_rate_; |
| 136 | double negative_upload_rate_; |
| 137 | |
| 138 | // Needed for unit-test. |
| 139 | int fetcher_id_for_unittest_; |
| 140 | bool is_testing_; |
[email protected] | 427b7da7 | 2010-03-22 21:23:18 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | #endif // CHROME_BROWSER_AUTOFILL_AUTOFILL_DOWNLOAD_H_ |
| 144 | |