Home | History | Annotate | Download | only in domain_reliability
      1 // Copyright 2014 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_DOMAIN_RELIABILITY_UTIL_H_
      6 #define COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_
      7 
      8 #include <map>
      9 
     10 #include "base/callback_forward.h"
     11 #include "base/compiler_specific.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/time/time.h"
     14 #include "base/tracked_objects.h"
     15 #include "components/domain_reliability/domain_reliability_export.h"
     16 #include "net/http/http_response_info.h"
     17 
     18 namespace domain_reliability {
     19 
     20 // Attempts to convert a net error and an HTTP response code into the status
     21 // string that should be recorded in a beacon. Returns true if it could.
     22 //
     23 // N.B.: This functions as the whitelist of "safe" errors to report; network-
     24 //       local errors are purposefully not converted to avoid revealing
     25 //       information about the local network to the remote server.
     26 bool GetDomainReliabilityBeaconStatus(
     27     int net_error,
     28     int http_response_code,
     29     std::string* beacon_status_out);
     30 
     31 std::string GetDomainReliabilityProtocol(
     32     net::HttpResponseInfo::ConnectionInfo connection_info,
     33     bool ssl_info_populated);
     34 
     35 // Mockable wrapper around TimeTicks::Now and Timer. Mock version is in
     36 // test_util.h.
     37 // TODO(ttuttle): Rename to Time{Provider,Source,?}.
     38 class DOMAIN_RELIABILITY_EXPORT MockableTime {
     39  public:
     40   // Mockable wrapper around (a subset of) base::Timer.
     41   class DOMAIN_RELIABILITY_EXPORT Timer {
     42    public:
     43     virtual ~Timer();
     44 
     45     virtual void Start(const tracked_objects::Location& posted_from,
     46                        base::TimeDelta delay,
     47                        const base::Closure& user_task) = 0;
     48     virtual void Stop() = 0;
     49     virtual bool IsRunning() = 0;
     50 
     51    protected:
     52     Timer();
     53   };
     54 
     55   virtual ~MockableTime();
     56 
     57   // Returns base::Time::Now() or a mocked version thereof.
     58   virtual base::Time Now() = 0;
     59   // Returns base::TimeTicks::Now() or a mocked version thereof.
     60   virtual base::TimeTicks NowTicks() = 0;
     61   // Returns a new Timer, or a mocked version thereof.
     62   virtual scoped_ptr<MockableTime::Timer> CreateTimer() = 0;
     63 
     64  protected:
     65   MockableTime();
     66 
     67  private:
     68   DISALLOW_COPY_AND_ASSIGN(MockableTime);
     69 };
     70 
     71 // Implementation of MockableTime that passes through to
     72 // base::Time{,Ticks}::Now() and base::Timer.
     73 class DOMAIN_RELIABILITY_EXPORT ActualTime : public MockableTime {
     74  public:
     75   ActualTime();
     76 
     77   virtual ~ActualTime();
     78 
     79   // MockableTime implementation:
     80   virtual base::Time Now() OVERRIDE;
     81   virtual base::TimeTicks NowTicks() OVERRIDE;
     82   virtual scoped_ptr<MockableTime::Timer> CreateTimer() OVERRIDE;
     83 };
     84 
     85 }  // namespace domain_reliability
     86 
     87 #endif  // COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_
     88