Home | History | Annotate | Download | only in autocheckout
      1 // Copyright 2013 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 COMPONENTS_AUTOFILL_CONTENT_BROWSER_AUTOCHECKOUT_WHITELIST_MANAGER_H_
      6 #define COMPONENTS_AUTOFILL_CONTENT_BROWSER_AUTOCHECKOUT_WHITELIST_MANAGER_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/timer/timer.h"
     12 #include "components/autofill/core/browser/autofill_metrics.h"
     13 #include "net/base/backoff_entry.h"
     14 #include "net/url_request/url_fetcher_delegate.h"
     15 
     16 class GURL;
     17 
     18 namespace content {
     19 class BrowserContext;
     20 }
     21 
     22 namespace net {
     23 class URLFetcher;
     24 class URLRequestContextGetter;
     25 }
     26 
     27 namespace autofill {
     28 namespace autocheckout {
     29 
     30 // Downloads and caches the list of URL prefixes whitelisted for use with
     31 // Autocheckout.
     32 class WhitelistManager : public net::URLFetcherDelegate {
     33  public:
     34   WhitelistManager();
     35   virtual ~WhitelistManager();
     36 
     37   // Schedule a fetch of the Autocheckout whitelist file if it's not already
     38   // loaded. This helps ensure that the whitelist will be available by the time
     39   // the user navigates to a form on which Autocheckout should be enabled.
     40   void Init(net::URLRequestContextGetter* context_getter);
     41 
     42   // Matches the url with whitelist and return the matched url prefix.
     43   // Returns empty string when it is not matched.
     44   std::string GetMatchedURLPrefix(const GURL& url) const;
     45 
     46  protected:
     47   // Schedules a future call to TriggerDownload if one isn't already pending.
     48   virtual void ScheduleDownload(base::TimeDelta interval);
     49 
     50   // Start the download timer. It is called by ScheduleDownload(), and exposed
     51   // as a separate method for mocking out in tests.
     52   virtual void StartDownloadTimer(base::TimeDelta interval);
     53 
     54   // Returns the |AutofillMetrics| instance that should be used for logging
     55   // Autocheckout whitelist file downloading.
     56   virtual const AutofillMetrics& GetMetricLogger() const;
     57 
     58   // Timer callback indicating it's time to download whitelist from server.
     59   void TriggerDownload();
     60 
     61   // Used by tests only.
     62   void StopDownloadTimer();
     63 
     64   const std::vector<std::string>& url_prefixes() const {
     65     return url_prefixes_;
     66   }
     67 
     68  private:
     69   // Implements net::URLFetcherDelegate.
     70   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
     71 
     72   // Parse whitelist data and build whitelist.
     73   void BuildWhitelist(const std::string& data);
     74 
     75   // A list of whitelisted url prefixes.
     76   std::vector<std::string> url_prefixes_;
     77 
     78   base::OneShotTimer<WhitelistManager> download_timer_;
     79 
     80   // Indicates that the last triggered download hasn't resolved yet.
     81   bool callback_is_pending_;
     82 
     83   // The context for the request.
     84   net::URLRequestContextGetter* context_getter_;  // WEAK
     85 
     86   // State of the kEnableExperimentalFormFilling flag.
     87   const bool experimental_form_filling_enabled_;
     88 
     89   // State of the kBypassAutocheckoutWhitelist flag.
     90   const bool bypass_autocheckout_whitelist_;
     91 
     92   // Exponential back-off delay to retry a failed download.
     93   net::BackoffEntry retry_entry_;
     94 
     95   // Logger for UMA metrics.
     96   AutofillMetrics metrics_logger_;
     97 
     98   // When the whitelist download started. Used to track download latency.
     99   base::Time request_started_timestamp_;
    100 
    101   // The request object.
    102   scoped_ptr<net::URLFetcher> request_;
    103 
    104   DISALLOW_COPY_AND_ASSIGN(WhitelistManager);
    105 };
    106 
    107 }  // namespace autocheckout
    108 }  // namespace autofill
    109 
    110 #endif  // COMPONENTS_AUTOFILL_CONTENT_BROWSER_AUTOCHECKOUT_WHITELIST_MANAGER_H_
    111 
    112