util.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #ifndef COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_
  5. #define COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/callback_forward.h"
  10. #include "base/compiler_specific.h"
  11. #include "base/time/tick_clock.h"
  12. #include "base/time/time.h"
  13. #include "components/domain_reliability/domain_reliability_export.h"
  14. #include "components/domain_reliability/uploader.h"
  15. #include "net/http/http_response_info.h"
  16. namespace base {
  17. class Location;
  18. }
  19. namespace domain_reliability {
  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. std::string GetDomainReliabilityProtocol(
  31. net::HttpResponseInfo::ConnectionInfo connection_info,
  32. bool ssl_info_populated);
  33. // Based on the network error code, HTTP response code, and Retry-After value,
  34. // returns the result of a report upload.
  35. DomainReliabilityUploader::UploadResult GetUploadResultFromResponseDetails(
  36. int net_error,
  37. int http_response_code,
  38. base::TimeDelta retry_after);
  39. GURL SanitizeURLForReport(
  40. const GURL& beacon_url,
  41. const GURL& collector_url,
  42. const std::vector<std::unique_ptr<std::string>>& path_prefixes);
  43. // Mockable wrapper around TimeTicks::Now and Timer. Mock version is in
  44. // test_util.h.
  45. // TODO(juliatuttle): Rename to Time{Provider,Source,?}.
  46. class DOMAIN_RELIABILITY_EXPORT MockableTime {
  47. public:
  48. // Mockable wrapper around (a subset of) base::Timer.
  49. class DOMAIN_RELIABILITY_EXPORT Timer {
  50. public:
  51. virtual ~Timer();
  52. virtual void Start(const base::Location& posted_from,
  53. base::TimeDelta delay,
  54. base::OnceClosure user_task) = 0;
  55. virtual void Stop() = 0;
  56. virtual bool IsRunning() = 0;
  57. protected:
  58. Timer();
  59. };
  60. MockableTime(const MockableTime&) = delete;
  61. MockableTime& operator=(const MockableTime&) = delete;
  62. virtual ~MockableTime();
  63. virtual base::Time Now() const = 0;
  64. virtual base::TimeTicks NowTicks() const = 0;
  65. // Returns a new Timer, or a mocked version thereof.
  66. virtual std::unique_ptr<MockableTime::Timer> CreateTimer() = 0;
  67. virtual const base::TickClock* AsTickClock() const = 0;
  68. protected:
  69. MockableTime();
  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. ~ActualTime() override;
  77. // MockableTime implementation:
  78. base::Time Now() const override;
  79. base::TimeTicks NowTicks() const override;
  80. std::unique_ptr<MockableTime::Timer> CreateTimer() override;
  81. const base::TickClock* AsTickClock() const override;
  82. };
  83. } // namespace domain_reliability
  84. #endif // COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_