Http Cache: Include Content-length when serving HEAD requests from cached sparse...
[chromium-blink-merge.git] / components / domain_reliability / util.h
blob4256b059cbd0a4e41d76051b3c02f072ca67622a
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.
5 #ifndef COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_
6 #define COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_
8 #include <map>
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 "components/domain_reliability/uploader.h"
17 #include "net/base/backoff_entry.h"
18 #include "net/http/http_response_info.h"
19 #include "net/url_request/url_request_status.h"
21 namespace domain_reliability {
23 // 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.
26 // N.B.: This functions as the whitelist of "safe" errors to report; network-
27 // local errors are purposefully not converted to avoid revealing
28 // information about the local network to the remote server.
29 bool GetDomainReliabilityBeaconStatus(
30 int net_error,
31 int http_response_code,
32 std::string* beacon_status_out);
34 std::string GetDomainReliabilityProtocol(
35 net::HttpResponseInfo::ConnectionInfo connection_info,
36 bool ssl_info_populated);
38 // Converts a URLRequestStatus into a network error. Returns the error code for
39 // FAILED; maps SUCCESS and CANCELED to OK and ERR_ABORTED, respectively; and
40 // returns ERR_ABORTED for any other status.
41 int GetNetErrorFromURLRequestStatus(const net::URLRequestStatus& status);
43 // Based on the network error code, HTTP response code, and Retry-After value,
44 // fills |status| with the result of a report upload.
45 void GetUploadResultFromResponseDetails(
46 int net_error,
47 int http_response_code,
48 base::TimeDelta retry_after,
49 DomainReliabilityUploader::UploadResult* result);
51 // Mockable wrapper around TimeTicks::Now and Timer. Mock version is in
52 // test_util.h.
53 // TODO(ttuttle): Rename to Time{Provider,Source,?}.
54 class DOMAIN_RELIABILITY_EXPORT MockableTime {
55 public:
56 // Mockable wrapper around (a subset of) base::Timer.
57 class DOMAIN_RELIABILITY_EXPORT Timer {
58 public:
59 virtual ~Timer();
61 virtual void Start(const tracked_objects::Location& posted_from,
62 base::TimeDelta delay,
63 const base::Closure& user_task) = 0;
64 virtual void Stop() = 0;
65 virtual bool IsRunning() = 0;
67 protected:
68 Timer();
71 virtual ~MockableTime();
73 // Returns base::Time::Now() or a mocked version thereof.
74 virtual base::Time Now() = 0;
75 // Returns base::TimeTicks::Now() or a mocked version thereof.
76 virtual base::TimeTicks NowTicks() = 0;
77 // Returns a new Timer, or a mocked version thereof.
78 virtual scoped_ptr<MockableTime::Timer> CreateTimer() = 0;
80 protected:
81 MockableTime();
83 private:
84 DISALLOW_COPY_AND_ASSIGN(MockableTime);
87 // Implementation of MockableTime that passes through to
88 // base::Time{,Ticks}::Now() and base::Timer.
89 class DOMAIN_RELIABILITY_EXPORT ActualTime : public MockableTime {
90 public:
91 ActualTime();
93 ~ActualTime() override;
95 // MockableTime implementation:
96 base::Time Now() override;
97 base::TimeTicks NowTicks() override;
98 scoped_ptr<MockableTime::Timer> CreateTimer() override;
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);
107 ~MockableTimeBackoffEntry() override;
109 protected:
110 base::TimeTicks ImplGetTimeNow() const override;
112 private:
113 MockableTime* time_;
116 } // namespace domain_reliability
118 #endif // COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_