blob: 3b972a1b35fb5933859abe6a88b59a7b023e4a51 [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_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]427b7da72010-03-22 21:23:188
9#include <map>
10#include <string>
11
[email protected]427b7da72010-03-22 21:23:1812#include "base/scoped_vector.h"
[email protected]db163472010-04-02 22:01:2013#include "base/time.h"
[email protected]427b7da72010-03-22 21:23:1814#include "chrome/browser/autofill/autofill_profile.h"
15#include "chrome/browser/autofill/field_types.h"
16#include "chrome/browser/autofill/form_structure.h"
[email protected]68d2a05f2010-05-07 21:39:5517#include "chrome/common/net/url_fetcher.h"
[email protected]427b7da72010-03-22 21:23:1818
[email protected]db163472010-04-02 22:01:2019class Profile;
20
[email protected]427b7da72010-03-22 21:23:1821// Handles getting and updating AutoFill heuristics.
22class AutoFillDownloadManager : public URLFetcher::Delegate {
23 public:
[email protected]61d96bf2010-03-29 05:50:3724 enum AutoFillRequestType {
25 REQUEST_QUERY,
26 REQUEST_UPLOAD,
27 };
[email protected]427b7da72010-03-22 21:23:1828 // An interface used to notify clients of AutoFillDownloadManager.
[email protected]db163472010-04-02 22:01:2029 // Notifications are *not* guaranteed to be called.
[email protected]427b7da72010-03-22 21:23:1830 class Observer {
31 public:
[email protected]1eb341ad2010-05-06 00:22:3332 // Called when field types are successfully received from the server.
[email protected]427b7da72010-03-22 21:23:1833 // |heuristic_xml| - server response.
34 virtual void OnLoadedAutoFillHeuristics(
[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]1eb341ad2010-05-06 00:22:3344 // |http_error| - HTTP error code.
[email protected]427b7da72010-03-22 21:23:1845 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
[email protected]db163472010-04-02 22:01:2052 // |profile| can be NULL in unit-tests only.
53 explicit AutoFillDownloadManager(Profile* profile);
[email protected]427b7da72010-03-22 21:23:1854 virtual ~AutoFillDownloadManager();
55
56 // |observer| - observer to notify on successful completion or error.
57 void SetObserver(AutoFillDownloadManager::Observer *observer);
58
[email protected]61d96bf2010-03-29 05:50:3759 // Starts a query request to AutoFill servers. The observer is called with the
60 // list of the fields of all requested forms.
61 // |forms| - array of forms aggregated in this request.
62 bool StartQueryRequest(const ScopedVector<FormStructure>& forms);
63
64 // Start upload request if necessary. The probability of request going
[email protected]db163472010-04-02 22:01:2065 // over the wire are GetPositiveUploadRate() if it was matched by
66 // AutoFill, GetNegativeUploadRate() otherwise. Observer will be called
[email protected]61d96bf2010-03-29 05:50:3767 // even if there was no actual trip over the wire.
68 // |form| - form sent in this request.
69 // |form_was_matched| - true if form was matched by the AutoFill.
70 bool StartUploadRequest(const FormStructure& form, bool form_was_matched);
71
72 // Cancels pending request.
73 // |form_signature| - signature of the form being cancelled. Warning:
74 // for query request if more than one form sent in the request, all other
75 // forms will be cancelled as well.
76 // |request_type| - type of the request.
77 bool CancelRequest(const std::string& form_signature,
78 AutoFillRequestType request_type);
79
[email protected]db163472010-04-02 22:01:2080 // Probability of the form upload. Between 0 (no upload) and 1 (upload all).
81 // GetPositiveUploadRate() is for matched forms,
82 // GetNegativeUploadRate() for non matched.
83 double GetPositiveUploadRate() const;
84 double GetNegativeUploadRate() const;
85 // These functions called very rarely outside of theunit-tests. With current
86 // percentages, they would be called once per 100 auto-fillable forms filled
87 // and submitted by user. The order of magnitude would remain similar in the
88 // future.
89 void SetPositiveUploadRate(double rate);
90 void SetNegativeUploadRate(double rate);
[email protected]61d96bf2010-03-29 05:50:3791
92 private:
93 friend class AutoFillDownloadTestHelper; // unit-test.
[email protected]8e383412010-10-19 16:57:0394
95 struct FormRequestData;
[email protected]61d96bf2010-03-29 05:50:3796
97 // Initiates request to AutoFill servers to download/upload heuristics.
[email protected]427b7da72010-03-22 21:23:1898 // |form_xml| - form structure XML to upload/download.
[email protected]61d96bf2010-03-29 05:50:3799 // |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.
[email protected]427b7da72010-03-22 21:23:18103 bool StartRequest(const std::string& form_xml,
[email protected]61d96bf2010-03-29 05:50:37104 const FormRequestData& request_data);
[email protected]427b7da72010-03-22 21:23:18105
[email protected]427b7da72010-03-22 21:23:18106 // URLFetcher::Delegate implementation:
107 virtual void OnURLFetchComplete(const URLFetcher* source,
108 const GURL& url,
109 const URLRequestStatus& status,
110 int response_code,
111 const ResponseCookies& cookies,
112 const std::string& data);
113
[email protected]db163472010-04-02 22:01:20114 // Profile for preference storage.
115 Profile* profile_;
116
[email protected]427b7da72010-03-22 21:23:18117 // For each requested form for both query and upload we create a separate
118 // request and save its info. As url fetcher is identified by its address
119 // we use a map between fetchers and info.
[email protected]61d96bf2010-03-29 05:50:37120 std::map<URLFetcher*, FormRequestData> url_fetchers_;
[email protected]427b7da72010-03-22 21:23:18121 AutoFillDownloadManager::Observer *observer_;
[email protected]61d96bf2010-03-29 05:50:37122
[email protected]db163472010-04-02 22:01:20123 // Time when next query/upload requests are allowed. If 50x HTTP received,
124 // exponential back off is initiated, so this times will be in the future
125 // for awhile.
126 base::Time next_query_request_;
127 base::Time next_upload_request_;
128
[email protected]61d96bf2010-03-29 05:50:37129 // |positive_upload_rate_| is for matched forms,
130 // |negative_upload_rate_| for non matched.
131 double positive_upload_rate_;
132 double negative_upload_rate_;
133
134 // Needed for unit-test.
135 int fetcher_id_for_unittest_;
136 bool is_testing_;
[email protected]427b7da72010-03-22 21:23:18137};
138
139#endif // CHROME_BROWSER_AUTOFILL_AUTOFILL_DOWNLOAD_H_
140