blob: 8937557fd1a9a4039e0a9b67dcf8d241bf9a86ba [file] [log] [blame]
[email protected]27a112c2011-01-06 04:19:301// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]427b7da72010-03-22 21:23:182// 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]608132372010-12-15 04:45:2019class AutoFillMetrics;
[email protected]db163472010-04-02 22:01:2020class Profile;
21
[email protected]427b7da72010-03-22 21:23:1822// Handles getting and updating AutoFill heuristics.
23class AutoFillDownloadManager : public URLFetcher::Delegate {
24 public:
[email protected]61d96bf2010-03-29 05:50:3725 enum AutoFillRequestType {
26 REQUEST_QUERY,
27 REQUEST_UPLOAD,
28 };
[email protected]6ee50a82010-11-24 07:04:0329
[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.
[email protected]608132372010-12-15 04:45:2064 bool StartQueryRequest(const ScopedVector<FormStructure>& forms,
65 const AutoFillMetrics& metric_logger);
[email protected]61d96bf2010-03-29 05:50:3766
67 // Start upload request if necessary. The probability of request going
[email protected]db163472010-04-02 22:01:2068 // over the wire are GetPositiveUploadRate() if it was matched by
69 // AutoFill, GetNegativeUploadRate() otherwise. Observer will be called
[email protected]61d96bf2010-03-29 05:50:3770 // even if there was no actual trip over the wire.
71 // |form| - form sent in this request.
72 // |form_was_matched| - true if form was matched by the AutoFill.
73 bool StartUploadRequest(const FormStructure& form, bool form_was_matched);
74
75 // Cancels pending request.
76 // |form_signature| - signature of the form being cancelled. Warning:
77 // for query request if more than one form sent in the request, all other
78 // forms will be cancelled as well.
79 // |request_type| - type of the request.
80 bool CancelRequest(const std::string& form_signature,
81 AutoFillRequestType request_type);
82
[email protected]db163472010-04-02 22:01:2083 // Probability of the form upload. Between 0 (no upload) and 1 (upload all).
84 // GetPositiveUploadRate() is for matched forms,
85 // GetNegativeUploadRate() for non matched.
86 double GetPositiveUploadRate() const;
87 double GetNegativeUploadRate() const;
88 // These functions called very rarely outside of theunit-tests. With current
89 // percentages, they would be called once per 100 auto-fillable forms filled
90 // and submitted by user. The order of magnitude would remain similar in the
91 // future.
92 void SetPositiveUploadRate(double rate);
93 void SetNegativeUploadRate(double rate);
[email protected]61d96bf2010-03-29 05:50:3794
95 private:
96 friend class AutoFillDownloadTestHelper; // unit-test.
[email protected]8e383412010-10-19 16:57:0397
98 struct FormRequestData;
[email protected]61d96bf2010-03-29 05:50:3799
100 // Initiates request to AutoFill servers to download/upload heuristics.
[email protected]427b7da72010-03-22 21:23:18101 // |form_xml| - form structure XML to upload/download.
[email protected]61d96bf2010-03-29 05:50:37102 // |request_data| - form signature hash(es) and indicator if it was a query.
103 // |request_data.query| - if true the data is queried and observer notified
104 // with new data, if available. If false heuristic data is uploaded to our
105 // servers.
[email protected]427b7da72010-03-22 21:23:18106 bool StartRequest(const std::string& form_xml,
[email protected]61d96bf2010-03-29 05:50:37107 const FormRequestData& request_data);
[email protected]427b7da72010-03-22 21:23:18108
[email protected]427b7da72010-03-22 21:23:18109 // URLFetcher::Delegate implementation:
110 virtual void OnURLFetchComplete(const URLFetcher* source,
111 const GURL& url,
[email protected]27a112c2011-01-06 04:19:30112 const net::URLRequestStatus& status,
[email protected]427b7da72010-03-22 21:23:18113 int response_code,
114 const ResponseCookies& cookies,
115 const std::string& data);
116
[email protected]db163472010-04-02 22:01:20117 // Profile for preference storage.
118 Profile* profile_;
119
[email protected]427b7da72010-03-22 21:23:18120 // For each requested form for both query and upload we create a separate
121 // request and save its info. As url fetcher is identified by its address
122 // we use a map between fetchers and info.
[email protected]61d96bf2010-03-29 05:50:37123 std::map<URLFetcher*, FormRequestData> url_fetchers_;
[email protected]427b7da72010-03-22 21:23:18124 AutoFillDownloadManager::Observer *observer_;
[email protected]61d96bf2010-03-29 05:50:37125
[email protected]db163472010-04-02 22:01:20126 // Time when next query/upload requests are allowed. If 50x HTTP received,
127 // exponential back off is initiated, so this times will be in the future
128 // for awhile.
129 base::Time next_query_request_;
130 base::Time next_upload_request_;
131
[email protected]61d96bf2010-03-29 05:50:37132 // |positive_upload_rate_| is for matched forms,
133 // |negative_upload_rate_| for non matched.
134 double positive_upload_rate_;
135 double negative_upload_rate_;
136
137 // Needed for unit-test.
138 int fetcher_id_for_unittest_;
139 bool is_testing_;
[email protected]427b7da72010-03-22 21:23:18140};
141
142#endif // CHROME_BROWSER_AUTOFILL_AUTOFILL_DOWNLOAD_H_
143