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