blob: 6144b90d460def0168ef7eb71e9ff693ae807f76 [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]8f608c52011-03-30 16:16:319#include <stddef.h>
[email protected]fc8893b2011-01-26 01:37:2310#include <list>
[email protected]427b7da72010-03-22 21:23:1811#include <map>
12#include <string>
[email protected]8f608c52011-03-30 16:16:3113#include <utility>
[email protected]fc8893b2011-01-26 01:37:2314#include <vector>
[email protected]427b7da72010-03-22 21:23:1815
[email protected]c530c852011-10-24 18:18:3416#include "base/compiler_specific.h"
[email protected]76b6d1d52011-06-15 07:00:3617#include "base/gtest_prod_util.h"
[email protected]db163472010-04-02 22:01:2018#include "base/time.h"
[email protected]ca64d232011-05-06 05:46:0019#include "chrome/browser/autofill/autofill_type.h"
[email protected]c530c852011-10-24 18:18:3420#include "content/public/common/url_fetcher_delegate.h"
[email protected]427b7da72010-03-22 21:23:1821
[email protected]f5c722b2011-03-01 01:01:5222class AutofillMetrics;
[email protected]8f608c52011-03-30 16:16:3123class FormStructure;
24class GURL;
[email protected]db163472010-04-02 22:01:2025class Profile;
26
[email protected]8f608c52011-03-30 16:16:3127namespace net {
28class URLRequestStatus;
29}
30
[email protected]d9acdfd2011-02-28 00:35:4431// Handles getting and updating Autofill heuristics.
[email protected]c530c852011-10-24 18:18:3432class AutofillDownloadManager : public content::URLFetcherDelegate {
[email protected]427b7da72010-03-22 21:23:1833 public:
[email protected]d9acdfd2011-02-28 00:35:4434 enum AutofillRequestType {
[email protected]61d96bf2010-03-29 05:50:3735 REQUEST_QUERY,
36 REQUEST_UPLOAD,
37 };
[email protected]6ee50a82010-11-24 07:04:0338
[email protected]d9acdfd2011-02-28 00:35:4439 // An interface used to notify clients of AutofillDownloadManager.
[email protected]427b7da72010-03-22 21:23:1840 class Observer {
41 public:
[email protected]76b6d1d52011-06-15 07:00:3642 // Called when field type predictions are successfully received from the
[email protected]cc97e482011-10-28 02:55:1943 // server. |response_xml| contains the server response.
[email protected]76b6d1d52011-06-15 07:00:3644 virtual void OnLoadedServerPredictions(const std::string& response_xml) = 0;
[email protected]cc97e482011-10-28 02:55:1945
46 // These notifications are used to help with testing.
[email protected]427b7da72010-03-22 21:23:1847 // Called when heuristic either successfully considered for upload and
48 // not send or uploaded.
[email protected]cc97e482011-10-28 02:55:1949 virtual void OnUploadedPossibleFieldTypes() {}
[email protected]427b7da72010-03-22 21:23:1850 // Called when there was an error during the request.
51 // |form_signature| - the signature of the requesting form.
[email protected]61d96bf2010-03-29 05:50:3752 // |request_type| - type of request that failed.
[email protected]1eb341ad2010-05-06 00:22:3353 // |http_error| - HTTP error code.
[email protected]76b6d1d52011-06-15 07:00:3654 virtual void OnServerRequestError(const std::string& form_signature,
55 AutofillRequestType request_type,
[email protected]cc97e482011-10-28 02:55:1956 int http_error) {}
57
[email protected]427b7da72010-03-22 21:23:1858 protected:
59 virtual ~Observer() {}
60 };
61
[email protected]427b7da72010-03-22 21:23:1862 // |observer| - observer to notify on successful completion or error.
[email protected]cc97e482011-10-28 02:55:1963 AutofillDownloadManager(Profile* profile, Observer* observer);
64 virtual ~AutofillDownloadManager();
[email protected]427b7da72010-03-22 21:23:1865
[email protected]d9acdfd2011-02-28 00:35:4466 // Starts a query request to Autofill servers. The observer is called with the
[email protected]61d96bf2010-03-29 05:50:3767 // list of the fields of all requested forms.
68 // |forms| - array of forms aggregated in this request.
[email protected]ddf907ca2011-10-21 23:08:2869 bool StartQueryRequest(const std::vector<FormStructure*>& forms,
[email protected]f5c722b2011-03-01 01:01:5270 const AutofillMetrics& metric_logger);
[email protected]61d96bf2010-03-29 05:50:3771
[email protected]ca64d232011-05-06 05:46:0072 // Starts an upload request for the given |form|, unless throttled by the
73 // server. The probability of the request going over the wire is
74 // GetPositiveUploadRate() if |form_was_autofilled| is true, or
75 // GetNegativeUploadRate() otherwise. The observer will be called even if
76 // there was no actual trip over the wire.
77 // |available_field_types| should contain the types for which we have data
78 // stored on the local client.
79 bool StartUploadRequest(const FormStructure& form,
80 bool form_was_autofilled,
81 const FieldTypeSet& available_field_types);
[email protected]61d96bf2010-03-29 05:50:3782
[email protected]61d96bf2010-03-29 05:50:3783 private:
[email protected]7ac651752011-09-16 01:55:5084 friend class AutofillDownloadTest;
[email protected]76b6d1d52011-06-15 07:00:3685 FRIEND_TEST_ALL_PREFIXES(AutofillDownloadTest, QueryAndUploadTest);
[email protected]8e383412010-10-19 16:57:0386
87 struct FormRequestData;
[email protected]fc8893b2011-01-26 01:37:2388 typedef std::list<std::pair<std::string, std::string> > QueryRequestCache;
[email protected]61d96bf2010-03-29 05:50:3789
[email protected]d9acdfd2011-02-28 00:35:4490 // Initiates request to Autofill servers to download/upload heuristics.
[email protected]427b7da72010-03-22 21:23:1891 // |form_xml| - form structure XML to upload/download.
[email protected]61d96bf2010-03-29 05:50:3792 // |request_data| - form signature hash(es) and indicator if it was a query.
93 // |request_data.query| - if true the data is queried and observer notified
94 // with new data, if available. If false heuristic data is uploaded to our
95 // servers.
[email protected]427b7da72010-03-22 21:23:1896 bool StartRequest(const std::string& form_xml,
[email protected]61d96bf2010-03-29 05:50:3797 const FormRequestData& request_data);
[email protected]427b7da72010-03-22 21:23:1898
[email protected]fc8893b2011-01-26 01:37:2399 // Each request is page visited. We store last |max_form_cache_size|
100 // request, to avoid going over the wire. Set to 16 in constructor. Warning:
101 // the search is linear (newest first), so do not make the constant very big.
102 void set_max_form_cache_size(size_t max_form_cache_size) {
103 max_form_cache_size_ = max_form_cache_size;
104 }
105
106 // Caches query request. |forms_in_query| is a vector of form signatures in
107 // the query. |query_data| is the successful data returned over the wire.
108 void CacheQueryRequest(const std::vector<std::string>& forms_in_query,
109 const std::string& query_data);
110 // Returns true if query is in the cache, while filling |query_data|, false
111 // otherwise. |forms_in_query| is a vector of form signatures in the query.
112 bool CheckCacheForQueryRequest(const std::vector<std::string>& forms_in_query,
113 std::string* query_data) const;
114 // Concatenates |forms_in_query| into one signature.
115 std::string GetCombinedSignature(
116 const std::vector<std::string>& forms_in_query) const;
117
[email protected]c530c852011-10-24 18:18:34118 // content::URLFetcherDelegate implementation:
[email protected]7cc6e5632011-10-25 17:56:12119 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE;
[email protected]427b7da72010-03-22 21:23:18120
[email protected]76b6d1d52011-06-15 07:00:36121 // Probability of the form upload. Between 0 (no upload) and 1 (upload all).
122 // GetPositiveUploadRate() is for matched forms,
123 // GetNegativeUploadRate() for non-matched.
124 double GetPositiveUploadRate() const;
125 double GetNegativeUploadRate() const;
126 void SetPositiveUploadRate(double rate);
127 void SetNegativeUploadRate(double rate);
128
[email protected]cc97e482011-10-28 02:55:19129 // Profile for preference storage. The pointer value is const, so this can
130 // only be set in the constructor. Must not be null.
131 Profile* const profile_; // WEAK
132 // The observer to notify when server predictions are successfully received.
133 // The pointer value is const, so this can only be set in the constructor.
134 // Must not be null.
135 AutofillDownloadManager::Observer* const observer_; // WEAK
[email protected]db163472010-04-02 22:01:20136
[email protected]427b7da72010-03-22 21:23:18137 // For each requested form for both query and upload we create a separate
138 // request and save its info. As url fetcher is identified by its address
139 // we use a map between fetchers and info.
[email protected]7cc6e5632011-10-25 17:56:12140 std::map<content::URLFetcher*, FormRequestData> url_fetchers_;
[email protected]61d96bf2010-03-29 05:50:37141
[email protected]fc8893b2011-01-26 01:37:23142 // Cached QUERY requests.
143 QueryRequestCache cached_forms_;
144 size_t max_form_cache_size_;
145
[email protected]db163472010-04-02 22:01:20146 // Time when next query/upload requests are allowed. If 50x HTTP received,
147 // exponential back off is initiated, so this times will be in the future
148 // for awhile.
149 base::Time next_query_request_;
150 base::Time next_upload_request_;
151
[email protected]61d96bf2010-03-29 05:50:37152 // |positive_upload_rate_| is for matched forms,
153 // |negative_upload_rate_| for non matched.
154 double positive_upload_rate_;
155 double negative_upload_rate_;
156
157 // Needed for unit-test.
158 int fetcher_id_for_unittest_;
[email protected]427b7da72010-03-22 21:23:18159};
160
161#endif // CHROME_BROWSER_AUTOFILL_AUTOFILL_DOWNLOAD_H_