ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / components / autofill / core / browser / autofill_download_manager.h
blob7bfe92f767b766ae4b46d4ceaac572e97dad06fb
1 // Copyright 2014 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.
5 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_DOWNLOAD_MANAGER_H_
6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_DOWNLOAD_MANAGER_H_
8 #include <stddef.h>
9 #include <list>
10 #include <map>
11 #include <string>
12 #include <utility>
13 #include <vector>
15 #include "base/compiler_specific.h"
16 #include "base/gtest_prod_util.h"
17 #include "base/time/time.h"
18 #include "components/autofill/core/browser/autofill_type.h"
19 #include "net/url_request/url_fetcher_delegate.h"
21 class PrefService;
23 namespace net {
24 class URLFetcher;
25 } // namespace net
27 namespace autofill {
29 class AutofillDriver;
30 class FormStructure;
32 // Handles getting and updating Autofill heuristics.
33 class AutofillDownloadManager : public net::URLFetcherDelegate {
34 public:
35 enum RequestType { REQUEST_QUERY, REQUEST_UPLOAD, };
37 // An interface used to notify clients of AutofillDownloadManager.
38 class Observer {
39 public:
40 // Called when field type predictions are successfully received from the
41 // server. |response_xml| contains the server response.
42 virtual void OnLoadedServerPredictions(const std::string& response_xml) = 0;
44 // These notifications are used to help with testing.
45 // Called when heuristic either successfully considered for upload and
46 // not send or uploaded.
47 virtual void OnUploadedPossibleFieldTypes() {}
48 // Called when there was an error during the request.
49 // |form_signature| - the signature of the requesting form.
50 // |request_type| - type of request that failed.
51 // |http_error| - HTTP error code.
52 virtual void OnServerRequestError(const std::string& form_signature,
53 RequestType request_type,
54 int http_error) {}
56 protected:
57 virtual ~Observer() {}
60 // |driver| and |pref_service| must outlive this instance.
61 // |observer| - observer to notify on successful completion or error.
62 AutofillDownloadManager(AutofillDriver* driver,
63 PrefService* pref_service,
64 Observer* observer);
65 ~AutofillDownloadManager() override;
67 // Starts a query request to Autofill servers. The observer is called with the
68 // list of the fields of all requested forms.
69 // |forms| - array of forms aggregated in this request.
70 bool StartQueryRequest(const std::vector<FormStructure*>& forms);
72 // 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 ServerFieldTypeSet& available_field_types);
83 private:
84 friend class AutofillDownloadTest;
85 FRIEND_TEST_ALL_PREFIXES(AutofillDownloadTest, QueryAndUploadTest);
87 struct FormRequestData;
88 typedef std::list<std::pair<std::string, std::string> > QueryRequestCache;
90 // Initiates request to Autofill servers to download/upload heuristics.
91 // |form_xml| - form structure XML to upload/download.
92 // |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.
96 bool StartRequest(const std::string& form_xml,
97 const FormRequestData& request_data);
99 // 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;
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;
118 // net::URLFetcherDelegate implementation:
119 void OnURLFetchComplete(const net::URLFetcher* source) override;
121 // 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);
129 // The AutofillDriver that this instance will use. Must not be null, and must
130 // outlive this instance.
131 AutofillDriver* const driver_; // WEAK
133 // The PrefService that this instance will use. Must not be null, and must
134 // outlive this instance.
135 PrefService* const pref_service_; // WEAK
137 // The observer to notify when server predictions are successfully received.
138 // Must not be null.
139 AutofillDownloadManager::Observer* const observer_; // WEAK
141 // For each requested form for both query and upload we create a separate
142 // request and save its info. As url fetcher is identified by its address
143 // we use a map between fetchers and info.
144 std::map<net::URLFetcher*, FormRequestData> url_fetchers_;
146 // Cached QUERY requests.
147 QueryRequestCache cached_forms_;
148 size_t max_form_cache_size_;
150 // Time when next query/upload requests are allowed. If 50x HTTP received,
151 // exponential back off is initiated, so this times will be in the future
152 // for awhile.
153 base::Time next_query_request_;
154 base::Time next_upload_request_;
156 // |positive_upload_rate_| is for matched forms,
157 // |negative_upload_rate_| for non matched.
158 double positive_upload_rate_;
159 double negative_upload_rate_;
161 // Needed for unit-test.
162 int fetcher_id_for_unittest_;
165 } // namespace autofill
167 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_DOWNLOAD_MANAGER_H_