4f0e2a33227fd0e4c0e3de545b16c6015b86c903
[platform/framework/web/crosswalk.git] / src / components / autofill / core / browser / autofill_download_manager.h
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.
4
5 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_DOWNLOAD_MANAGER_H_
6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_DOWNLOAD_MANAGER_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 RequestType { REQUEST_QUERY, REQUEST_UPLOAD, };
37
38   // An interface used to notify clients of AutofillDownloadManager.
39   class Observer {
40    public:
41     // Called when field type predictions are successfully received from the
42     // server.  |response_xml| contains the server response.
43     virtual void OnLoadedServerPredictions(const std::string& response_xml) = 0;
44
45     // These notifications are used to help with testing.
46     // Called when heuristic either successfully considered for upload and
47     // not send or uploaded.
48     virtual void OnUploadedPossibleFieldTypes() {}
49     // Called when there was an error during the request.
50     // |form_signature| - the signature of the requesting form.
51     // |request_type| - type of request that failed.
52     // |http_error| - HTTP error code.
53     virtual void OnServerRequestError(const std::string& form_signature,
54                                       RequestType request_type,
55                                       int http_error) {}
56
57    protected:
58     virtual ~Observer() {}
59   };
60
61   // |driver| and |pref_service| must outlive this instance.
62   // |observer| - observer to notify on successful completion or error.
63   AutofillDownloadManager(AutofillDriver* driver,
64                           PrefService* pref_service,
65                           Observer* observer);
66   ~AutofillDownloadManager() override;
67
68   // Starts a query request to Autofill servers. The observer is called with the
69   // list of the fields of all requested forms.
70   // |forms| - array of forms aggregated in this request.
71   bool StartQueryRequest(const std::vector<FormStructure*>& forms,
72                          const AutofillMetrics& metric_logger);
73
74   // Starts an upload request for the given |form|, unless throttled by the
75   // server. The probability of the request going over the wire is
76   // GetPositiveUploadRate() if |form_was_autofilled| is true, or
77   // GetNegativeUploadRate() otherwise. The observer will be called even if
78   // there was no actual trip over the wire.
79   // |available_field_types| should contain the types for which we have data
80   // stored on the local client.
81   bool StartUploadRequest(const FormStructure& form,
82                           bool form_was_autofilled,
83                           const ServerFieldTypeSet& available_field_types);
84
85  private:
86   friend class AutofillDownloadTest;
87   FRIEND_TEST_ALL_PREFIXES(AutofillDownloadTest, QueryAndUploadTest);
88
89   struct FormRequestData;
90   typedef std::list<std::pair<std::string, std::string> > QueryRequestCache;
91
92   // Initiates request to Autofill servers to download/upload heuristics.
93   // |form_xml| - form structure XML to upload/download.
94   // |request_data| - form signature hash(es) and indicator if it was a query.
95   // |request_data.query| - if true the data is queried and observer notified
96   //   with new data, if available. If false heuristic data is uploaded to our
97   //   servers.
98   bool StartRequest(const std::string& form_xml,
99                     const FormRequestData& request_data);
100
101   // Each request is page visited. We store last |max_form_cache_size|
102   // request, to avoid going over the wire. Set to 16 in constructor. Warning:
103   // the search is linear (newest first), so do not make the constant very big.
104   void set_max_form_cache_size(size_t max_form_cache_size) {
105     max_form_cache_size_ = max_form_cache_size;
106   }
107
108   // Caches query request. |forms_in_query| is a vector of form signatures in
109   // the query. |query_data| is the successful data returned over the wire.
110   void CacheQueryRequest(const std::vector<std::string>& forms_in_query,
111                          const std::string& query_data);
112   // Returns true if query is in the cache, while filling |query_data|, false
113   // otherwise. |forms_in_query| is a vector of form signatures in the query.
114   bool CheckCacheForQueryRequest(const std::vector<std::string>& forms_in_query,
115                                  std::string* query_data) const;
116   // Concatenates |forms_in_query| into one signature.
117   std::string GetCombinedSignature(
118       const std::vector<std::string>& forms_in_query) const;
119
120   // net::URLFetcherDelegate implementation:
121   void OnURLFetchComplete(const net::URLFetcher* source) override;
122
123   // Probability of the form upload. Between 0 (no upload) and 1 (upload all).
124   // GetPositiveUploadRate() is for matched forms,
125   // GetNegativeUploadRate() for non-matched.
126   double GetPositiveUploadRate() const;
127   double GetNegativeUploadRate() const;
128   void SetPositiveUploadRate(double rate);
129   void SetNegativeUploadRate(double rate);
130
131   // The AutofillDriver that this instance will use. Must not be null, and must
132   // outlive this instance.
133   AutofillDriver* const driver_;  // WEAK
134
135   // The PrefService that this instance will use. Must not be null, and must
136   // outlive this instance.
137   PrefService* const pref_service_;  // WEAK
138
139   // The observer to notify when server predictions are successfully received.
140   // Must not be null.
141   AutofillDownloadManager::Observer* const observer_;  // WEAK
142
143   // For each requested form for both query and upload we create a separate
144   // request and save its info. As url fetcher is identified by its address
145   // we use a map between fetchers and info.
146   std::map<net::URLFetcher*, FormRequestData> url_fetchers_;
147
148   // Cached QUERY requests.
149   QueryRequestCache cached_forms_;
150   size_t max_form_cache_size_;
151
152   // 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
158   // |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 };
166
167 }  // namespace autofill
168
169 #endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_DOWNLOAD_MANAGER_H_