Only grant permissions to new extensions from sync if they have the expected version
[chromium-blink-merge.git] / components / autofill / core / browser / autofill_download_manager.h
blob1c72eaf6b6c4ef2e028474ecba421181415b3433
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 // |login_form_signature| may be empty. It is non-empty when the user fills
80 // and submits a login form using a generated password. In this case,
81 // |login_form_signature| should be set to the submitted form's signature.
82 // Note that in this case, |form.FormSignature()| gives the signature for the
83 // registration form on which the password was generated, rather than the
84 // submitted form's signature.
85 bool StartUploadRequest(const FormStructure& form,
86 bool form_was_autofilled,
87 const ServerFieldTypeSet& available_field_types,
88 const std::string& login_form_signature);
90 private:
91 friend class AutofillDownloadTest;
92 FRIEND_TEST_ALL_PREFIXES(AutofillDownloadTest, QueryAndUploadTest);
94 struct FormRequestData;
95 typedef std::list<std::pair<std::string, std::string> > QueryRequestCache;
97 // Initiates request to Autofill servers to download/upload heuristics.
98 // |form_xml| - form structure XML to upload/download.
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.
103 bool StartRequest(const std::string& form_xml,
104 const FormRequestData& request_data);
106 // Each request is page visited. We store last |max_form_cache_size|
107 // request, to avoid going over the wire. Set to 16 in constructor. Warning:
108 // the search is linear (newest first), so do not make the constant very big.
109 void set_max_form_cache_size(size_t max_form_cache_size) {
110 max_form_cache_size_ = max_form_cache_size;
113 // Caches query request. |forms_in_query| is a vector of form signatures in
114 // the query. |query_data| is the successful data returned over the wire.
115 void CacheQueryRequest(const std::vector<std::string>& forms_in_query,
116 const std::string& query_data);
117 // Returns true if query is in the cache, while filling |query_data|, false
118 // otherwise. |forms_in_query| is a vector of form signatures in the query.
119 bool CheckCacheForQueryRequest(const std::vector<std::string>& forms_in_query,
120 std::string* query_data) const;
121 // Concatenates |forms_in_query| into one signature.
122 std::string GetCombinedSignature(
123 const std::vector<std::string>& forms_in_query) const;
125 // net::URLFetcherDelegate implementation:
126 void OnURLFetchComplete(const net::URLFetcher* source) override;
128 // Probability of the form upload. Between 0 (no upload) and 1 (upload all).
129 // GetPositiveUploadRate() is for matched forms,
130 // GetNegativeUploadRate() for non-matched.
131 double GetPositiveUploadRate() const;
132 double GetNegativeUploadRate() const;
133 void SetPositiveUploadRate(double rate);
134 void SetNegativeUploadRate(double rate);
136 // The AutofillDriver that this instance will use. Must not be null, and must
137 // outlive this instance.
138 AutofillDriver* const driver_; // WEAK
140 // The PrefService that this instance will use. Must not be null, and must
141 // outlive this instance.
142 PrefService* const pref_service_; // WEAK
144 // The observer to notify when server predictions are successfully received.
145 // Must not be null.
146 AutofillDownloadManager::Observer* const observer_; // WEAK
148 // For each requested form for both query and upload we create a separate
149 // request and save its info. As url fetcher is identified by its address
150 // we use a map between fetchers and info.
151 std::map<net::URLFetcher*, FormRequestData> url_fetchers_;
153 // Cached QUERY requests.
154 QueryRequestCache cached_forms_;
155 size_t max_form_cache_size_;
157 // Time when next query/upload requests are allowed. If 50x HTTP received,
158 // exponential back off is initiated, so this times will be in the future
159 // for awhile.
160 base::Time next_query_request_;
161 base::Time next_upload_request_;
163 // |positive_upload_rate_| is for matched forms,
164 // |negative_upload_rate_| for non matched.
165 double positive_upload_rate_;
166 double negative_upload_rate_;
168 // Needed for unit-test.
169 int fetcher_id_for_unittest_;
172 } // namespace autofill
174 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_DOWNLOAD_MANAGER_H_