Home | History | Annotate | Download | only in autofill
      1 // Copyright (c) 2011 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 <stddef.h>
     10 #include <list>
     11 #include <map>
     12 #include <string>
     13 #include <utility>
     14 #include <vector>
     15 
     16 #include "base/memory/scoped_vector.h"
     17 #include "base/time.h"
     18 #include "chrome/common/net/url_fetcher.h"
     19 
     20 #ifdef ANDROID
     21 #include "android/autofill/url_fetcher_proxy.h"
     22 #endif
     23 
     24 class AutofillMetrics;
     25 class FormStructure;
     26 class GURL;
     27 class Profile;
     28 
     29 namespace net {
     30 class URLRequestStatus;
     31 }
     32 
     33 // Handles getting and updating Autofill heuristics.
     34 class AutofillDownloadManager : public URLFetcher::Delegate {
     35  public:
     36   enum AutofillRequestType {
     37     REQUEST_QUERY,
     38     REQUEST_UPLOAD,
     39   };
     40 
     41   // An interface used to notify clients of AutofillDownloadManager.
     42   // Notifications are *not* guaranteed to be called.
     43   class Observer {
     44    public:
     45     // Called when field types are successfully received from the server.
     46     // |heuristic_xml| - server response.
     47     virtual void OnLoadedAutofillHeuristics(
     48         const std::string& heuristic_xml) = 0;
     49     // Called when heuristic either successfully considered for upload and
     50     // not send or uploaded.
     51     // |form_signature| - the signature of the requesting form.
     52     virtual void OnUploadedAutofillHeuristics(
     53         const std::string& form_signature) = 0;
     54     // Called when there was an error during the request.
     55     // |form_signature| - the signature of the requesting form.
     56     // |request_type| - type of request that failed.
     57     // |http_error| - HTTP error code.
     58     virtual void OnHeuristicsRequestError(const std::string& form_signature,
     59                                           AutofillRequestType request_type,
     60                                           int http_error) = 0;
     61    protected:
     62     virtual ~Observer() {}
     63   };
     64 
     65   // |profile| can be NULL in unit-tests only.
     66   explicit AutofillDownloadManager(Profile* profile);
     67   virtual ~AutofillDownloadManager();
     68 
     69   // |observer| - observer to notify on successful completion or error.
     70   void SetObserver(AutofillDownloadManager::Observer *observer);
     71 
     72   // Starts a query request to Autofill servers. The observer is called with the
     73   // list of the fields of all requested forms.
     74   // |forms| - array of forms aggregated in this request.
     75   bool StartQueryRequest(const ScopedVector<FormStructure>& forms,
     76                          const AutofillMetrics& metric_logger);
     77 
     78   // Start upload request if necessary. The probability of request going
     79   // over the wire are GetPositiveUploadRate() if it was matched by
     80   // Autofill, GetNegativeUploadRate() otherwise. Observer will be called
     81   // even if there was no actual trip over the wire.
     82   // |form| - form sent in this request.
     83   // |form_was_matched| - true if form was matched by the Autofill.
     84   bool StartUploadRequest(const FormStructure& form, bool form_was_matched);
     85 
     86   // Cancels pending request.
     87   // |form_signature| - signature of the form being cancelled. Warning:
     88   // for query request if more than one form sent in the request, all other
     89   // forms will be cancelled as well.
     90   // |request_type| - type of the request.
     91   bool CancelRequest(const std::string& form_signature,
     92                      AutofillRequestType request_type);
     93 
     94   // Probability of the form upload. Between 0 (no upload) and 1 (upload all).
     95   // GetPositiveUploadRate() is for matched forms,
     96   // GetNegativeUploadRate() for non matched.
     97   double GetPositiveUploadRate() const;
     98   double GetNegativeUploadRate() const;
     99   // These functions called very rarely outside of the unit-tests. With current
    100   // percentages, they would be called once per 100 auto-fillable forms filled
    101   // and submitted by user. The order of magnitude would remain similar in the
    102   // future.
    103   void SetPositiveUploadRate(double rate);
    104   void SetNegativeUploadRate(double rate);
    105 
    106  private:
    107   friend class AutofillDownloadTestHelper;  // unit-test.
    108 
    109   struct FormRequestData;
    110   typedef std::list<std::pair<std::string, std::string> > QueryRequestCache;
    111 
    112   // Initiates request to Autofill servers to download/upload heuristics.
    113   // |form_xml| - form structure XML to upload/download.
    114   // |request_data| - form signature hash(es) and indicator if it was a query.
    115   // |request_data.query| - if true the data is queried and observer notified
    116   //   with new data, if available. If false heuristic data is uploaded to our
    117   //   servers.
    118   bool StartRequest(const std::string& form_xml,
    119                     const FormRequestData& request_data);
    120 
    121   // Each request is page visited. We store last |max_form_cache_size|
    122   // request, to avoid going over the wire. Set to 16 in constructor. Warning:
    123   // the search is linear (newest first), so do not make the constant very big.
    124   void set_max_form_cache_size(size_t max_form_cache_size) {
    125     max_form_cache_size_ = max_form_cache_size;
    126   }
    127 
    128   // Caches query request. |forms_in_query| is a vector of form signatures in
    129   // the query. |query_data| is the successful data returned over the wire.
    130   void CacheQueryRequest(const std::vector<std::string>& forms_in_query,
    131                          const std::string& query_data);
    132   // Returns true if query is in the cache, while filling |query_data|, false
    133   // otherwise. |forms_in_query| is a vector of form signatures in the query.
    134   bool CheckCacheForQueryRequest(const std::vector<std::string>& forms_in_query,
    135                                  std::string* query_data) const;
    136   // Concatenates |forms_in_query| into one signature.
    137   std::string GetCombinedSignature(
    138       const std::vector<std::string>& forms_in_query) const;
    139 
    140   // URLFetcher::Delegate implementation:
    141   virtual void OnURLFetchComplete(const URLFetcher* source,
    142                                   const GURL& url,
    143                                   const net::URLRequestStatus& status,
    144                                   int response_code,
    145                                   const ResponseCookies& cookies,
    146                                   const std::string& data);
    147 
    148   // Profile for preference storage.
    149   Profile* profile_;
    150 
    151   // For each requested form for both query and upload we create a separate
    152   // request and save its info. As url fetcher is identified by its address
    153   // we use a map between fetchers and info.
    154   std::map<URLFetcher*, FormRequestData> url_fetchers_;
    155   AutofillDownloadManager::Observer *observer_;
    156 
    157   // Cached QUERY requests.
    158   QueryRequestCache cached_forms_;
    159   size_t max_form_cache_size_;
    160 
    161   // Time when next query/upload requests are allowed. If 50x HTTP received,
    162   // exponential back off is initiated, so this times will be in the future
    163   // for awhile.
    164   base::Time next_query_request_;
    165   base::Time next_upload_request_;
    166 
    167   // |positive_upload_rate_| is for matched forms,
    168   // |negative_upload_rate_| for non matched.
    169   double positive_upload_rate_;
    170   double negative_upload_rate_;
    171 
    172   // Needed for unit-test.
    173   int fetcher_id_for_unittest_;
    174 };
    175 
    176 #endif  // CHROME_BROWSER_AUTOFILL_AUTOFILL_DOWNLOAD_H_
    177 
    178