blob: 620ac6f3029c2230ddfe4d9772f27515bc8be507 [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"
15#include "chrome/browser/autofill/autofill_profile.h"
16#include "chrome/browser/autofill/field_types.h"
17#include "chrome/browser/autofill/form_structure.h"
18#include "chrome/browser/net/url_fetcher.h"
19
20// Handles getting and updating AutoFill heuristics.
21class AutoFillDownloadManager : public URLFetcher::Delegate {
22 public:
[email protected]61d96bf2010-03-29 05:50:3723 enum AutoFillRequestType {
24 REQUEST_QUERY,
25 REQUEST_UPLOAD,
26 };
[email protected]427b7da72010-03-22 21:23:1827 // An interface used to notify clients of AutoFillDownloadManager.
28 class Observer {
29 public:
30 // Called when heuristic successfully received from server.
[email protected]61d96bf2010-03-29 05:50:3731 // |form_signatures| - the signatures of the requesting forms.
[email protected]427b7da72010-03-22 21:23:1832 // |heuristic_xml| - server response.
33 virtual void OnLoadedAutoFillHeuristics(
[email protected]61d96bf2010-03-29 05:50:3734 const std::vector<std::string>& form_signatures,
[email protected]427b7da72010-03-22 21:23:1835 const std::string& heuristic_xml) = 0;
36 // Called when heuristic either successfully considered for upload and
37 // not send or uploaded.
38 // |form_signature| - the signature of the requesting form.
39 virtual void OnUploadedAutoFillHeuristics(
40 const std::string& form_signature) = 0;
41 // Called when there was an error during the request.
42 // |form_signature| - the signature of the requesting form.
[email protected]61d96bf2010-03-29 05:50:3743 // |request_type| - type of request that failed.
[email protected]427b7da72010-03-22 21:23:1844 // |http_error| - http error code.
45 virtual void OnHeuristicsRequestError(const std::string& form_signature,
[email protected]61d96bf2010-03-29 05:50:3746 AutoFillRequestType request_type,
[email protected]427b7da72010-03-22 21:23:1847 int http_error) = 0;
48 protected:
49 virtual ~Observer() {}
50 };
51
52 AutoFillDownloadManager();
53 virtual ~AutoFillDownloadManager();
54
55 // |observer| - observer to notify on successful completion or error.
56 void SetObserver(AutoFillDownloadManager::Observer *observer);
57
[email protected]61d96bf2010-03-29 05:50:3758 // Starts a query request to AutoFill servers. The observer is called with the
59 // list of the fields of all requested forms.
60 // |forms| - array of forms aggregated in this request.
61 bool StartQueryRequest(const ScopedVector<FormStructure>& forms);
62
63 // Start upload request if necessary. The probability of request going
64 // over the wire are |positive_upload_rate_| if it was matched by
65 // AutoFill, |negative_download_rate_| otherwise. Observer will be called
66 // even if there was no actual trip over the wire.
67 // |form| - form sent in this request.
68 // |form_was_matched| - true if form was matched by the AutoFill.
69 bool StartUploadRequest(const FormStructure& form, bool form_was_matched);
70
71 // Cancels pending request.
72 // |form_signature| - signature of the form being cancelled. Warning:
73 // for query request if more than one form sent in the request, all other
74 // forms will be cancelled as well.
75 // |request_type| - type of the request.
76 bool CancelRequest(const std::string& form_signature,
77 AutoFillRequestType request_type);
78
79 void SetPositiveUploadRate(double rate) {
80 DCHECK(rate >= 0.0 && rate <= 1.0);
81 positive_upload_rate_ = rate;
82 }
83
84 void SetNegativeUploadRate(double rate) {
85 DCHECK(rate >= 0.0 && rate <= 1.0);
86 negative_upload_rate_ = rate;
87 }
88
89 private:
90 friend class AutoFillDownloadTestHelper; // unit-test.
91 struct FormRequestData {
92 std::vector<std::string> form_signatures;
93 AutoFillRequestType request_type;
94 };
95
96 // Initiates request to AutoFill servers to download/upload heuristics.
[email protected]427b7da72010-03-22 21:23:1897 // |form_xml| - form structure XML to upload/download.
[email protected]61d96bf2010-03-29 05:50:3798 // |request_data| - form signature hash(es) and indicator if it was a query.
99 // |request_data.query| - if true the data is queried and observer notified
100 // with new data, if available. If false heuristic data is uploaded to our
101 // servers.
[email protected]427b7da72010-03-22 21:23:18102 bool StartRequest(const std::string& form_xml,
[email protected]61d96bf2010-03-29 05:50:37103 const FormRequestData& request_data);
[email protected]427b7da72010-03-22 21:23:18104
[email protected]427b7da72010-03-22 21:23:18105 // URLFetcher::Delegate implementation:
106 virtual void OnURLFetchComplete(const URLFetcher* source,
107 const GURL& url,
108 const URLRequestStatus& status,
109 int response_code,
110 const ResponseCookies& cookies,
111 const std::string& data);
112
[email protected]427b7da72010-03-22 21:23:18113 // For each requested form for both query and upload we create a separate
114 // request and save its info. As url fetcher is identified by its address
115 // we use a map between fetchers and info.
[email protected]61d96bf2010-03-29 05:50:37116 std::map<URLFetcher*, FormRequestData> url_fetchers_;
[email protected]427b7da72010-03-22 21:23:18117 AutoFillDownloadManager::Observer *observer_;
[email protected]61d96bf2010-03-29 05:50:37118
119 // Probability of the form upload. Between 0 (no upload) and 1 (upload all).
120 // |positive_upload_rate_| is for matched forms,
121 // |negative_upload_rate_| for non matched.
122 double positive_upload_rate_;
123 double negative_upload_rate_;
124
125 // Needed for unit-test.
126 int fetcher_id_for_unittest_;
127 bool is_testing_;
[email protected]427b7da72010-03-22 21:23:18128};
129
130#endif // CHROME_BROWSER_AUTOFILL_AUTOFILL_DOWNLOAD_H_
131