blob: 397aae7ebea2a42f53b17ebf64fb9984ecdb87e9 [file] [log] [blame]
[email protected]427b7da72010-03-22 21:23:181// Copyright (c) 2010 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 CHROME_BROWSER_AUTOFILL_AUTOFILL_DOWNLOAD_H_
6#define CHROME_BROWSER_AUTOFILL_AUTOFILL_DOWNLOAD_H_
7
8#include <map>
[email protected]61d96bf2010-03-29 05:50:379#include <vector>
[email protected]427b7da72010-03-22 21:23:1810#include <string>
11
12#include "base/scoped_ptr.h"
13#include "base/scoped_vector.h"
14#include "base/string16.h"
[email protected]db163472010-04-02 22:01:2015#include "base/time.h"
[email protected]427b7da72010-03-22 21:23:1816#include "chrome/browser/autofill/autofill_profile.h"
17#include "chrome/browser/autofill/field_types.h"
18#include "chrome/browser/autofill/form_structure.h"
19#include "chrome/browser/net/url_fetcher.h"
20
[email protected]db163472010-04-02 22:01:2021class Profile;
22
[email protected]427b7da72010-03-22 21:23:1823// Handles getting and updating AutoFill heuristics.
24class AutoFillDownloadManager : public URLFetcher::Delegate {
25 public:
[email protected]61d96bf2010-03-29 05:50:3726 enum AutoFillRequestType {
27 REQUEST_QUERY,
28 REQUEST_UPLOAD,
29 };
[email protected]427b7da72010-03-22 21:23:1830 // An interface used to notify clients of AutoFillDownloadManager.
[email protected]db163472010-04-02 22:01:2031 // Notifications are *not* guaranteed to be called.
[email protected]427b7da72010-03-22 21:23:1832 class Observer {
33 public:
[email protected]1eb341ad2010-05-06 00:22:3334 // Called when field types are successfully received from the server.
[email protected]427b7da72010-03-22 21:23:1835 // |heuristic_xml| - server response.
36 virtual void OnLoadedAutoFillHeuristics(
[email protected]427b7da72010-03-22 21:23:1837 const std::string& heuristic_xml) = 0;
38 // Called when heuristic either successfully considered for upload and
39 // not send or uploaded.
40 // |form_signature| - the signature of the requesting form.
41 virtual void OnUploadedAutoFillHeuristics(
42 const std::string& form_signature) = 0;
43 // Called when there was an error during the request.
44 // |form_signature| - the signature of the requesting form.
[email protected]61d96bf2010-03-29 05:50:3745 // |request_type| - type of request that failed.
[email protected]1eb341ad2010-05-06 00:22:3346 // |http_error| - HTTP error code.
[email protected]427b7da72010-03-22 21:23:1847 virtual void OnHeuristicsRequestError(const std::string& form_signature,
[email protected]61d96bf2010-03-29 05:50:3748 AutoFillRequestType request_type,
[email protected]427b7da72010-03-22 21:23:1849 int http_error) = 0;
50 protected:
51 virtual ~Observer() {}
52 };
53
[email protected]db163472010-04-02 22:01:2054 // |profile| can be NULL in unit-tests only.
55 explicit AutoFillDownloadManager(Profile* profile);
[email protected]427b7da72010-03-22 21:23:1856 virtual ~AutoFillDownloadManager();
57
58 // |observer| - observer to notify on successful completion or error.
59 void SetObserver(AutoFillDownloadManager::Observer *observer);
60
[email protected]61d96bf2010-03-29 05:50:3761 // Starts a query request to AutoFill servers. The observer is called with the
62 // list of the fields of all requested forms.
63 // |forms| - array of forms aggregated in this request.
64 bool StartQueryRequest(const ScopedVector<FormStructure>& forms);
65
66 // Start upload request if necessary. The probability of request going
[email protected]db163472010-04-02 22:01:2067 // over the wire are GetPositiveUploadRate() if it was matched by
68 // AutoFill, GetNegativeUploadRate() otherwise. Observer will be called
[email protected]61d96bf2010-03-29 05:50:3769 // even if there was no actual trip over the wire.
70 // |form| - form sent in this request.
71 // |form_was_matched| - true if form was matched by the AutoFill.
72 bool StartUploadRequest(const FormStructure& form, bool form_was_matched);
73
74 // Cancels pending request.
75 // |form_signature| - signature of the form being cancelled. Warning:
76 // for query request if more than one form sent in the request, all other
77 // forms will be cancelled as well.
78 // |request_type| - type of the request.
79 bool CancelRequest(const std::string& form_signature,
80 AutoFillRequestType request_type);
81
[email protected]db163472010-04-02 22:01:2082 // Probability of the form upload. Between 0 (no upload) and 1 (upload all).
83 // GetPositiveUploadRate() is for matched forms,
84 // GetNegativeUploadRate() for non matched.
85 double GetPositiveUploadRate() const;
86 double GetNegativeUploadRate() const;
87 // These functions called very rarely outside of theunit-tests. With current
88 // percentages, they would be called once per 100 auto-fillable forms filled
89 // and submitted by user. The order of magnitude would remain similar in the
90 // future.
91 void SetPositiveUploadRate(double rate);
92 void SetNegativeUploadRate(double rate);
[email protected]61d96bf2010-03-29 05:50:3793
94 private:
95 friend class AutoFillDownloadTestHelper; // unit-test.
96 struct FormRequestData {
97 std::vector<std::string> form_signatures;
98 AutoFillRequestType request_type;
99 };
100
101 // Initiates request to AutoFill servers to download/upload heuristics.
[email protected]427b7da72010-03-22 21:23:18102 // |form_xml| - form structure XML to upload/download.
[email protected]61d96bf2010-03-29 05:50:37103 // |request_data| - form signature hash(es) and indicator if it was a query.
104 // |request_data.query| - if true the data is queried and observer notified
105 // with new data, if available. If false heuristic data is uploaded to our
106 // servers.
[email protected]427b7da72010-03-22 21:23:18107 bool StartRequest(const std::string& form_xml,
[email protected]61d96bf2010-03-29 05:50:37108 const FormRequestData& request_data);
[email protected]427b7da72010-03-22 21:23:18109
[email protected]427b7da72010-03-22 21:23:18110 // URLFetcher::Delegate implementation:
111 virtual void OnURLFetchComplete(const URLFetcher* source,
112 const GURL& url,
113 const URLRequestStatus& status,
114 int response_code,
115 const ResponseCookies& cookies,
116 const std::string& data);
117
[email protected]db163472010-04-02 22:01:20118 // Profile for preference storage.
119 Profile* profile_;
120
[email protected]427b7da72010-03-22 21:23:18121 // For each requested form for both query and upload we create a separate
122 // request and save its info. As url fetcher is identified by its address
123 // we use a map between fetchers and info.
[email protected]61d96bf2010-03-29 05:50:37124 std::map<URLFetcher*, FormRequestData> url_fetchers_;
[email protected]427b7da72010-03-22 21:23:18125 AutoFillDownloadManager::Observer *observer_;
[email protected]61d96bf2010-03-29 05:50:37126
[email protected]db163472010-04-02 22:01:20127 // Time when next query/upload requests are allowed. If 50x HTTP received,
128 // exponential back off is initiated, so this times will be in the future
129 // for awhile.
130 base::Time next_query_request_;
131 base::Time next_upload_request_;
132
[email protected]61d96bf2010-03-29 05:50:37133 // |positive_upload_rate_| is for matched forms,
134 // |negative_upload_rate_| for non matched.
135 double positive_upload_rate_;
136 double negative_upload_rate_;
137
138 // Needed for unit-test.
139 int fetcher_id_for_unittest_;
140 bool is_testing_;
[email protected]427b7da72010-03-22 21:23:18141};
142
143#endif // CHROME_BROWSER_AUTOFILL_AUTOFILL_DOWNLOAD_H_
144