blob: 157062c105583acf64603fe79624a59a440cb0f9 [file] [log] [blame]
[email protected]27a112c2011-01-06 04:19:301// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]427b7da72010-03-22 21:23:182// 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]32b76ef2010-07-26 23:08:247#pragma once
[email protected]427b7da72010-03-22 21:23:188
[email protected]fc8893b2011-01-26 01:37:239#include <list>
[email protected]427b7da72010-03-22 21:23:1810#include <map>
11#include <string>
[email protected]fc8893b2011-01-26 01:37:2312#include <vector>
[email protected]427b7da72010-03-22 21:23:1813
[email protected]427b7da72010-03-22 21:23:1814#include "base/scoped_vector.h"
[email protected]db163472010-04-02 22:01:2015#include "base/time.h"
[email protected]427b7da72010-03-22 21:23:1816#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]68d2a05f2010-05-07 21:39:5519#include "chrome/common/net/url_fetcher.h"
[email protected]427b7da72010-03-22 21:23:1820
[email protected]608132372010-12-15 04:45:2021class AutoFillMetrics;
[email protected]db163472010-04-02 22:01:2022class Profile;
23
[email protected]427b7da72010-03-22 21:23:1824// Handles getting and updating AutoFill heuristics.
25class AutoFillDownloadManager : public URLFetcher::Delegate {
26 public:
[email protected]61d96bf2010-03-29 05:50:3727 enum AutoFillRequestType {
28 REQUEST_QUERY,
29 REQUEST_UPLOAD,
30 };
[email protected]6ee50a82010-11-24 07:04:0331
[email protected]427b7da72010-03-22 21:23:1832 // An interface used to notify clients of AutoFillDownloadManager.
[email protected]db163472010-04-02 22:01:2033 // Notifications are *not* guaranteed to be called.
[email protected]427b7da72010-03-22 21:23:1834 class Observer {
35 public:
[email protected]1eb341ad2010-05-06 00:22:3336 // Called when field types are successfully received from the server.
[email protected]427b7da72010-03-22 21:23:1837 // |heuristic_xml| - server response.
38 virtual void OnLoadedAutoFillHeuristics(
[email protected]427b7da72010-03-22 21:23:1839 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.
43 virtual void OnUploadedAutoFillHeuristics(
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]61d96bf2010-03-29 05:50:3747 // |request_type| - type of request that failed.
[email protected]1eb341ad2010-05-06 00:22:3348 // |http_error| - HTTP error code.
[email protected]427b7da72010-03-22 21:23:1849 virtual void OnHeuristicsRequestError(const std::string& form_signature,
[email protected]61d96bf2010-03-29 05:50:3750 AutoFillRequestType request_type,
[email protected]427b7da72010-03-22 21:23:1851 int http_error) = 0;
52 protected:
53 virtual ~Observer() {}
54 };
55
[email protected]db163472010-04-02 22:01:2056 // |profile| can be NULL in unit-tests only.
57 explicit AutoFillDownloadManager(Profile* profile);
[email protected]427b7da72010-03-22 21:23:1858 virtual ~AutoFillDownloadManager();
59
60 // |observer| - observer to notify on successful completion or error.
61 void SetObserver(AutoFillDownloadManager::Observer *observer);
62
[email protected]61d96bf2010-03-29 05:50:3763 // Starts a query request to AutoFill servers. The observer is called with the
64 // list of the fields of all requested forms.
65 // |forms| - array of forms aggregated in this request.
[email protected]608132372010-12-15 04:45:2066 bool StartQueryRequest(const ScopedVector<FormStructure>& forms,
67 const AutoFillMetrics& metric_logger);
[email protected]61d96bf2010-03-29 05:50:3768
69 // Start upload request if necessary. The probability of request going
[email protected]db163472010-04-02 22:01:2070 // over the wire are GetPositiveUploadRate() if it was matched by
71 // AutoFill, GetNegativeUploadRate() otherwise. Observer will be called
[email protected]61d96bf2010-03-29 05:50:3772 // even if there was no actual trip over the wire.
73 // |form| - form sent in this request.
74 // |form_was_matched| - true if form was matched by the AutoFill.
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,
83 AutoFillRequestType request_type);
84
[email protected]db163472010-04-02 22:01:2085 // 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;
90 // These functions called very rarely outside of theunit-tests. With current
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]61d96bf2010-03-29 05:50:3796
97 private:
98 friend class AutoFillDownloadTestHelper; // unit-test.
[email protected]8e383412010-10-19 16:57:0399
100 struct FormRequestData;
[email protected]fc8893b2011-01-26 01:37:23101 typedef std::list<std::pair<std::string, std::string> > QueryRequestCache;
[email protected]61d96bf2010-03-29 05:50:37102
103 // Initiates request to AutoFill servers to download/upload heuristics.
[email protected]427b7da72010-03-22 21:23:18104 // |form_xml| - form structure XML to upload/download.
[email protected]61d96bf2010-03-29 05:50:37105 // |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]427b7da72010-03-22 21:23:18109 bool StartRequest(const std::string& form_xml,
[email protected]61d96bf2010-03-29 05:50:37110 const FormRequestData& request_data);
[email protected]427b7da72010-03-22 21:23:18111
[email protected]fc8893b2011-01-26 01:37:23112 // 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]427b7da72010-03-22 21:23:18131 // URLFetcher::Delegate implementation:
132 virtual void OnURLFetchComplete(const URLFetcher* source,
133 const GURL& url,
[email protected]27a112c2011-01-06 04:19:30134 const net::URLRequestStatus& status,
[email protected]427b7da72010-03-22 21:23:18135 int response_code,
136 const ResponseCookies& cookies,
137 const std::string& data);
138
[email protected]db163472010-04-02 22:01:20139 // Profile for preference storage.
140 Profile* profile_;
141
[email protected]427b7da72010-03-22 21:23:18142 // 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]61d96bf2010-03-29 05:50:37145 std::map<URLFetcher*, FormRequestData> url_fetchers_;
[email protected]427b7da72010-03-22 21:23:18146 AutoFillDownloadManager::Observer *observer_;
[email protected]61d96bf2010-03-29 05:50:37147
[email protected]fc8893b2011-01-26 01:37:23148 // Cached QUERY requests.
149 QueryRequestCache cached_forms_;
150 size_t max_form_cache_size_;
151
[email protected]db163472010-04-02 22:01:20152 // 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]61d96bf2010-03-29 05:50:37158 // |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_;
165 bool is_testing_;
[email protected]427b7da72010-03-22 21:23:18166};
167
168#endif // CHROME_BROWSER_AUTOFILL_AUTOFILL_DOWNLOAD_H_
169