| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_ | 5 #ifndef COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_ |
| 6 #define COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_ | 6 #define COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/callback_forward.h" | 10 #include "base/callback_forward.h" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/time/clock.h" |
| 14 #include "base/time/tick_clock.h" |
| 13 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 14 #include "base/tracked_objects.h" | 16 #include "base/tracked_objects.h" |
| 15 #include "components/domain_reliability/domain_reliability_export.h" | 17 #include "components/domain_reliability/domain_reliability_export.h" |
| 16 #include "components/domain_reliability/uploader.h" | 18 #include "components/domain_reliability/uploader.h" |
| 17 #include "net/base/backoff_entry.h" | |
| 18 #include "net/http/http_response_info.h" | 19 #include "net/http/http_response_info.h" |
| 19 #include "net/url_request/url_request_status.h" | 20 #include "net/url_request/url_request_status.h" |
| 20 | 21 |
| 21 namespace domain_reliability { | 22 namespace domain_reliability { |
| 22 | 23 |
| 23 // Attempts to convert a net error and an HTTP response code into the status | 24 // Attempts to convert a net error and an HTTP response code into the status |
| 24 // string that should be recorded in a beacon. Returns true if it could. | 25 // string that should be recorded in a beacon. Returns true if it could. |
| 25 // | 26 // |
| 26 // N.B.: This functions as the whitelist of "safe" errors to report; network- | 27 // N.B.: This functions as the whitelist of "safe" errors to report; network- |
| 27 // local errors are purposefully not converted to avoid revealing | 28 // local errors are purposefully not converted to avoid revealing |
| (...skipping 16 matching lines...) Expand all Loading... |
| 44 // fills |status| with the result of a report upload. | 45 // fills |status| with the result of a report upload. |
| 45 void GetUploadResultFromResponseDetails( | 46 void GetUploadResultFromResponseDetails( |
| 46 int net_error, | 47 int net_error, |
| 47 int http_response_code, | 48 int http_response_code, |
| 48 base::TimeDelta retry_after, | 49 base::TimeDelta retry_after, |
| 49 DomainReliabilityUploader::UploadResult* result); | 50 DomainReliabilityUploader::UploadResult* result); |
| 50 | 51 |
| 51 // Mockable wrapper around TimeTicks::Now and Timer. Mock version is in | 52 // Mockable wrapper around TimeTicks::Now and Timer. Mock version is in |
| 52 // test_util.h. | 53 // test_util.h. |
| 53 // TODO(ttuttle): Rename to Time{Provider,Source,?}. | 54 // TODO(ttuttle): Rename to Time{Provider,Source,?}. |
| 54 class DOMAIN_RELIABILITY_EXPORT MockableTime { | 55 class DOMAIN_RELIABILITY_EXPORT MockableTime : public base::Clock, |
| 56 public base::TickClock { |
| 55 public: | 57 public: |
| 56 // Mockable wrapper around (a subset of) base::Timer. | 58 // Mockable wrapper around (a subset of) base::Timer. |
| 57 class DOMAIN_RELIABILITY_EXPORT Timer { | 59 class DOMAIN_RELIABILITY_EXPORT Timer { |
| 58 public: | 60 public: |
| 59 virtual ~Timer(); | 61 virtual ~Timer(); |
| 60 | 62 |
| 61 virtual void Start(const tracked_objects::Location& posted_from, | 63 virtual void Start(const tracked_objects::Location& posted_from, |
| 62 base::TimeDelta delay, | 64 base::TimeDelta delay, |
| 63 const base::Closure& user_task) = 0; | 65 const base::Closure& user_task) = 0; |
| 64 virtual void Stop() = 0; | 66 virtual void Stop() = 0; |
| 65 virtual bool IsRunning() = 0; | 67 virtual bool IsRunning() = 0; |
| 66 | 68 |
| 67 protected: | 69 protected: |
| 68 Timer(); | 70 Timer(); |
| 69 }; | 71 }; |
| 70 | 72 |
| 71 virtual ~MockableTime(); | 73 ~MockableTime() override; |
| 72 | 74 |
| 73 // Returns base::Time::Now() or a mocked version thereof. | 75 // Clock impl; returns base::Time::Now() or a mocked version thereof. |
| 74 virtual base::Time Now() = 0; | 76 base::Time Now() override = 0; |
| 75 // Returns base::TimeTicks::Now() or a mocked version thereof. | 77 // TickClock impl; returns base::TimeTicks::Now() or a mocked version thereof. |
| 76 virtual base::TimeTicks NowTicks() = 0; | 78 base::TimeTicks NowTicks() override = 0; |
| 79 |
| 77 // Returns a new Timer, or a mocked version thereof. | 80 // Returns a new Timer, or a mocked version thereof. |
| 78 virtual scoped_ptr<MockableTime::Timer> CreateTimer() = 0; | 81 virtual scoped_ptr<MockableTime::Timer> CreateTimer() = 0; |
| 79 | 82 |
| 80 protected: | 83 protected: |
| 81 MockableTime(); | 84 MockableTime(); |
| 82 | 85 |
| 83 private: | 86 private: |
| 84 DISALLOW_COPY_AND_ASSIGN(MockableTime); | 87 DISALLOW_COPY_AND_ASSIGN(MockableTime); |
| 85 }; | 88 }; |
| 86 | 89 |
| 87 // Implementation of MockableTime that passes through to | 90 // Implementation of MockableTime that passes through to |
| 88 // base::Time{,Ticks}::Now() and base::Timer. | 91 // base::Time{,Ticks}::Now() and base::Timer. |
| 89 class DOMAIN_RELIABILITY_EXPORT ActualTime : public MockableTime { | 92 class DOMAIN_RELIABILITY_EXPORT ActualTime : public MockableTime { |
| 90 public: | 93 public: |
| 91 ActualTime(); | 94 ActualTime(); |
| 92 | 95 |
| 93 ~ActualTime() override; | 96 ~ActualTime() override; |
| 94 | 97 |
| 95 // MockableTime implementation: | 98 // MockableTime implementation: |
| 96 base::Time Now() override; | 99 base::Time Now() override; |
| 97 base::TimeTicks NowTicks() override; | 100 base::TimeTicks NowTicks() override; |
| 98 scoped_ptr<MockableTime::Timer> CreateTimer() override; | 101 scoped_ptr<MockableTime::Timer> CreateTimer() override; |
| 99 }; | 102 }; |
| 100 | 103 |
| 101 // A subclass of BackoffEntry that uses a MockableTime to keep track of time. | |
| 102 class MockableTimeBackoffEntry : public net::BackoffEntry { | |
| 103 public: | |
| 104 MockableTimeBackoffEntry(const net::BackoffEntry::Policy* const policy, | |
| 105 MockableTime* time); | |
| 106 | |
| 107 ~MockableTimeBackoffEntry() override; | |
| 108 | |
| 109 protected: | |
| 110 base::TimeTicks ImplGetTimeNow() const override; | |
| 111 | |
| 112 private: | |
| 113 MockableTime* time_; | |
| 114 }; | |
| 115 | |
| 116 } // namespace domain_reliability | 104 } // namespace domain_reliability |
| 117 | 105 |
| 118 #endif // COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_ | 106 #endif // COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_ |
| OLD | NEW |