autofill_download.h revision a7a33092974a68bc3adbf1073175540899a3d932
1// 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#pragma once
8
9#include <map>
10#include <string>
11
12#include "base/scoped_vector.h"
13#include "base/time.h"
14#include "chrome/browser/autofill/autofill_profile.h"
15#include "chrome/browser/autofill/field_types.h"
16#include "chrome/browser/autofill/form_structure.h"
17#include "chrome/common/net/url_fetcher.h"
18
19#ifdef ANDROID
20#include "android/autofill/url_fetcher_proxy.h"
21#endif
22
23class AutoFillMetrics;
24class Profile;
25
26// Handles getting and updating AutoFill heuristics.
27class AutoFillDownloadManager : public URLFetcher::Delegate {
28 public:
29  enum AutoFillRequestType {
30    REQUEST_QUERY,
31    REQUEST_UPLOAD,
32  };
33
34  // An interface used to notify clients of AutoFillDownloadManager.
35  // Notifications are *not* guaranteed to be called.
36  class Observer {
37   public:
38    // Called when field types are successfully received from the server.
39    // |heuristic_xml| - server response.
40    virtual void OnLoadedAutoFillHeuristics(
41        const std::string& heuristic_xml) = 0;
42    // Called when heuristic either successfully considered for upload and
43    // not send or uploaded.
44    // |form_signature| - the signature of the requesting form.
45    virtual void OnUploadedAutoFillHeuristics(
46        const std::string& form_signature) = 0;
47    // Called when there was an error during the request.
48    // |form_signature| - the signature of the requesting form.
49    // |request_type| - type of request that failed.
50    // |http_error| - HTTP error code.
51    virtual void OnHeuristicsRequestError(const std::string& form_signature,
52                                          AutoFillRequestType request_type,
53                                          int http_error) = 0;
54   protected:
55    virtual ~Observer() {}
56  };
57
58  // |profile| can be NULL in unit-tests only.
59  explicit AutoFillDownloadManager(Profile* profile);
60  virtual ~AutoFillDownloadManager();
61
62  // |observer| - observer to notify on successful completion or error.
63  void SetObserver(AutoFillDownloadManager::Observer *observer);
64
65  // Starts a query request to AutoFill servers. The observer is called with the
66  // list of the fields of all requested forms.
67  // |forms| - array of forms aggregated in this request.
68  bool StartQueryRequest(const ScopedVector<FormStructure>& forms,
69                         const AutoFillMetrics& metric_logger);
70
71  // Start upload request if necessary. The probability of request going
72  // over the wire are GetPositiveUploadRate() if it was matched by
73  // AutoFill, GetNegativeUploadRate() otherwise. Observer will be called
74  // even if there was no actual trip over the wire.
75  // |form| - form sent in this request.
76  // |form_was_matched| - true if form was matched by the AutoFill.
77  bool StartUploadRequest(const FormStructure& form, bool form_was_matched);
78
79  // Cancels pending request.
80  // |form_signature| - signature of the form being cancelled. Warning:
81  // for query request if more than one form sent in the request, all other
82  // forms will be cancelled as well.
83  // |request_type| - type of the request.
84  bool CancelRequest(const std::string& form_signature,
85                     AutoFillRequestType request_type);
86
87  // Probability of the form upload. Between 0 (no upload) and 1 (upload all).
88  // GetPositiveUploadRate() is for matched forms,
89  // GetNegativeUploadRate() for non matched.
90  double GetPositiveUploadRate() const;
91  double GetNegativeUploadRate() const;
92  // These functions called very rarely outside of theunit-tests. With current
93  // percentages, they would be called once per 100 auto-fillable forms filled
94  // and submitted by user. The order of magnitude would remain similar in the
95  // future.
96  void SetPositiveUploadRate(double rate);
97  void SetNegativeUploadRate(double rate);
98
99 private:
100  friend class AutoFillDownloadTestHelper;  // unit-test.
101
102  struct FormRequestData;
103
104  // Initiates request to AutoFill servers to download/upload heuristics.
105  // |form_xml| - form structure XML to upload/download.
106  // |request_data| - form signature hash(es) and indicator if it was a query.
107  // |request_data.query| - if true the data is queried and observer notified
108  //   with new data, if available. If false heuristic data is uploaded to our
109  //   servers.
110  bool StartRequest(const std::string& form_xml,
111                    const FormRequestData& request_data);
112
113  // URLFetcher::Delegate implementation:
114  virtual void OnURLFetchComplete(const URLFetcher* source,
115                                  const GURL& url,
116                                  const URLRequestStatus& status,
117                                  int response_code,
118                                  const ResponseCookies& cookies,
119                                  const std::string& data);
120
121  // Profile for preference storage.
122  Profile* profile_;
123
124  // For each requested form for both query and upload we create a separate
125  // request and save its info. As url fetcher is identified by its address
126  // we use a map between fetchers and info.
127  std::map<URLFetcher*, FormRequestData> url_fetchers_;
128  AutoFillDownloadManager::Observer *observer_;
129
130  // Time when next query/upload requests are allowed. If 50x HTTP received,
131  // exponential back off is initiated, so this times will be in the future
132  // for awhile.
133  base::Time next_query_request_;
134  base::Time next_upload_request_;
135
136  // |positive_upload_rate_| is for matched forms,
137  // |negative_upload_rate_| for non matched.
138  double positive_upload_rate_;
139  double negative_upload_rate_;
140
141  // Needed for unit-test.
142  int fetcher_id_for_unittest_;
143  bool is_testing_;
144};
145
146#endif  // CHROME_BROWSER_AUTOFILL_AUTOFILL_DOWNLOAD_H_
147
148