blob: 5987c9dcacede6a2c450e781fe5bccb969691c59 [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]6ee50a82010-11-24 07:04:0328
[email protected]427b7da72010-03-22 21:23:1829 // An interface used to notify clients of AutoFillDownloadManager.
[email protected]db163472010-04-02 22:01:2030 // Notifications are *not* guaranteed to be called.
[email protected]427b7da72010-03-22 21:23:1831 class Observer {
32 public:
[email protected]1eb341ad2010-05-06 00:22:3333 // Called when field types are successfully received from the server.
[email protected]427b7da72010-03-22 21:23:1834 // |heuristic_xml| - server response.
35 virtual void OnLoadedAutoFillHeuristics(
[email protected]427b7da72010-03-22 21:23:1836 const std::string& heuristic_xml) = 0;
37 // Called when heuristic either successfully considered for upload and
38 // not send or uploaded.
39 // |form_signature| - the signature of the requesting form.
40 virtual void OnUploadedAutoFillHeuristics(
41 const std::string& form_signature) = 0;
42 // Called when there was an error during the request.
43 // |form_signature| - the signature of the requesting form.
[email protected]61d96bf2010-03-29 05:50:3744 // |request_type| - type of request that failed.
[email protected]1eb341ad2010-05-06 00:22:3345 // |http_error| - HTTP error code.
[email protected]427b7da72010-03-22 21:23:1846 virtual void OnHeuristicsRequestError(const std::string& form_signature,
[email protected]61d96bf2010-03-29 05:50:3747 AutoFillRequestType request_type,
[email protected]427b7da72010-03-22 21:23:1848 int http_error) = 0;
49 protected:
50 virtual ~Observer() {}
51 };
52
[email protected]db163472010-04-02 22:01:2053 // |profile| can be NULL in unit-tests only.
54 explicit AutoFillDownloadManager(Profile* profile);
[email protected]427b7da72010-03-22 21:23:1855 virtual ~AutoFillDownloadManager();
56
57 // |observer| - observer to notify on successful completion or error.
58 void SetObserver(AutoFillDownloadManager::Observer *observer);
59
[email protected]61d96bf2010-03-29 05:50:3760 // Starts a query request to AutoFill servers. The observer is called with the
61 // list of the fields of all requested forms.
62 // |forms| - array of forms aggregated in this request.
63 bool StartQueryRequest(const ScopedVector<FormStructure>& forms);
64
65 // Start upload request if necessary. The probability of request going
[email protected]db163472010-04-02 22:01:2066 // over the wire are GetPositiveUploadRate() if it was matched by
67 // AutoFill, GetNegativeUploadRate() otherwise. Observer will be called
[email protected]61d96bf2010-03-29 05:50:3768 // even if there was no actual trip over the wire.
69 // |form| - form sent in this request.
70 // |form_was_matched| - true if form was matched by the AutoFill.
71 bool StartUploadRequest(const FormStructure& form, bool form_was_matched);
72
73 // Cancels pending request.
74 // |form_signature| - signature of the form being cancelled. Warning:
75 // for query request if more than one form sent in the request, all other
76 // forms will be cancelled as well.
77 // |request_type| - type of the request.
78 bool CancelRequest(const std::string& form_signature,
79 AutoFillRequestType request_type);
80
[email protected]db163472010-04-02 22:01:2081 // Probability of the form upload. Between 0 (no upload) and 1 (upload all).
82 // GetPositiveUploadRate() is for matched forms,
83 // GetNegativeUploadRate() for non matched.
84 double GetPositiveUploadRate() const;
85 double GetNegativeUploadRate() const;
86 // These functions called very rarely outside of theunit-tests. With current
87 // percentages, they would be called once per 100 auto-fillable forms filled
88 // and submitted by user. The order of magnitude would remain similar in the
89 // future.
90 void SetPositiveUploadRate(double rate);
91 void SetNegativeUploadRate(double rate);
[email protected]61d96bf2010-03-29 05:50:3792
93 private:
94 friend class AutoFillDownloadTestHelper; // unit-test.
[email protected]8e383412010-10-19 16:57:0395
96 struct FormRequestData;
[email protected]61d96bf2010-03-29 05:50:3797
98 // Initiates request to AutoFill servers to download/upload heuristics.
[email protected]427b7da72010-03-22 21:23:1899 // |form_xml| - form structure XML to upload/download.
[email protected]61d96bf2010-03-29 05:50:37100 // |request_data| - form signature hash(es) and indicator if it was a query.
101 // |request_data.query| - if true the data is queried and observer notified
102 // with new data, if available. If false heuristic data is uploaded to our
103 // servers.
[email protected]427b7da72010-03-22 21:23:18104 bool StartRequest(const std::string& form_xml,
[email protected]61d96bf2010-03-29 05:50:37105 const FormRequestData& request_data);
[email protected]427b7da72010-03-22 21:23:18106
[email protected]427b7da72010-03-22 21:23:18107 // URLFetcher::Delegate implementation:
108 virtual void OnURLFetchComplete(const URLFetcher* source,
109 const GURL& url,
110 const URLRequestStatus& status,
111 int response_code,
112 const ResponseCookies& cookies,
113 const std::string& data);
114
[email protected]db163472010-04-02 22:01:20115 // Profile for preference storage.
116 Profile* profile_;
117
[email protected]427b7da72010-03-22 21:23:18118 // For each requested form for both query and upload we create a separate
119 // request and save its info. As url fetcher is identified by its address
120 // we use a map between fetchers and info.
[email protected]61d96bf2010-03-29 05:50:37121 std::map<URLFetcher*, FormRequestData> url_fetchers_;
[email protected]427b7da72010-03-22 21:23:18122 AutoFillDownloadManager::Observer *observer_;
[email protected]61d96bf2010-03-29 05:50:37123
[email protected]db163472010-04-02 22:01:20124 // Time when next query/upload requests are allowed. If 50x HTTP received,
125 // exponential back off is initiated, so this times will be in the future
126 // for awhile.
127 base::Time next_query_request_;
128 base::Time next_upload_request_;
129
[email protected]61d96bf2010-03-29 05:50:37130 // |positive_upload_rate_| is for matched forms,
131 // |negative_upload_rate_| for non matched.
132 double positive_upload_rate_;
133 double negative_upload_rate_;
134
135 // Needed for unit-test.
136 int fetcher_id_for_unittest_;
137 bool is_testing_;
[email protected]427b7da72010-03-22 21:23:18138};
139
140#endif // CHROME_BROWSER_AUTOFILL_AUTOFILL_DOWNLOAD_H_
141