• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 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 COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_DOWNLOAD_H_
6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_DOWNLOAD_H_
7 
8 #include <stddef.h>
9 #include <list>
10 #include <map>
11 #include <string>
12 #include <utility>
13 #include <vector>
14 
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"
20 
21 class PrefService;
22 
23 namespace net {
24 class URLFetcher;
25 }  // namespace net
26 
27 namespace autofill {
28 
29 class AutofillDriver;
30 class AutofillMetrics;
31 class FormStructure;
32 
33 // Handles getting and updating Autofill heuristics.
34 class AutofillDownloadManager : public net::URLFetcherDelegate {
35  public:
36   enum AutofillRequestType {
37     REQUEST_QUERY,
38     REQUEST_UPLOAD,
39   };
40 
41   // An interface used to notify clients of AutofillDownloadManager.
42   class Observer {
43    public:
44     // Called when field type predictions are successfully received from the
45     // server.  |response_xml| contains the server response.
46     virtual void OnLoadedServerPredictions(const std::string& response_xml) = 0;
47 
48     // These notifications are used to help with testing.
49     // Called when heuristic either successfully considered for upload and
50     // not send or uploaded.
OnUploadedPossibleFieldTypes()51     virtual void OnUploadedPossibleFieldTypes() {}
52     // Called when there was an error during the request.
53     // |form_signature| - the signature of the requesting form.
54     // |request_type| - type of request that failed.
55     // |http_error| - HTTP error code.
OnServerRequestError(const std::string & form_signature,AutofillRequestType request_type,int http_error)56     virtual void OnServerRequestError(const std::string& form_signature,
57                                       AutofillRequestType request_type,
58                                       int http_error) {}
59 
60    protected:
~Observer()61     virtual ~Observer() {}
62   };
63 
64   // |driver| and |pref_service| must outlive this instance.
65   // |observer| - observer to notify on successful completion or error.
66   AutofillDownloadManager(AutofillDriver* driver,
67                           PrefService* pref_service,
68                           Observer* observer);
69   virtual ~AutofillDownloadManager();
70 
71   // Starts a query request to Autofill servers. The observer is called with the
72   // list of the fields of all requested forms.
73   // |forms| - array of forms aggregated in this request.
74   bool StartQueryRequest(const std::vector<FormStructure*>& forms,
75                          const AutofillMetrics& metric_logger);
76 
77   // Starts an upload request for the given |form|, unless throttled by the
78   // server. The probability of the request going over the wire is
79   // GetPositiveUploadRate() if |form_was_autofilled| is true, or
80   // GetNegativeUploadRate() otherwise. The observer will be called even if
81   // there was no actual trip over the wire.
82   // |available_field_types| should contain the types for which we have data
83   // stored on the local client.
84   bool StartUploadRequest(const FormStructure& form,
85                           bool form_was_autofilled,
86                           const ServerFieldTypeSet& available_field_types);
87 
88  private:
89   friend class AutofillDownloadTest;
90   FRIEND_TEST_ALL_PREFIXES(AutofillDownloadTest, QueryAndUploadTest);
91 
92   static std::string AutofillRequestTypeToString(const AutofillRequestType);
93 
94   struct FormRequestData;
95   typedef std::list<std::pair<std::string, std::string> > QueryRequestCache;
96 
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);
105 
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.
set_max_form_cache_size(size_t max_form_cache_size)109   void set_max_form_cache_size(size_t max_form_cache_size) {
110     max_form_cache_size_ = max_form_cache_size;
111   }
112 
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;
124 
125   // net::URLFetcherDelegate implementation:
126   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
127 
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);
135 
136   // The AutofillDriver that this instance will use. Must not be null, and must
137   // outlive this instance.
138   AutofillDriver* const driver_;  // WEAK
139 
140   // The PrefService that this instance will use. Must not be null, and must
141   // outlive this instance.
142   PrefService* const pref_service_;  // WEAK
143 
144   // The observer to notify when server predictions are successfully received.
145   // Must not be null.
146   AutofillDownloadManager::Observer* const observer_;  // WEAK
147 
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_;
152 
153   // Cached QUERY requests.
154   QueryRequestCache cached_forms_;
155   size_t max_form_cache_size_;
156 
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_;
162 
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_;
167 
168   // Needed for unit-test.
169   int fetcher_id_for_unittest_;
170 };
171 
172 }  // namespace autofill
173 
174 #endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_DOWNLOAD_H_
175