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_
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(
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(
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
53 // TODO(ttuttle): Rename to Time{Provider,Source,?}.
54 class DOMAIN_RELIABILITY_EXPORT MockableTime
{
56 // Mockable wrapper around (a subset of) base::Timer.
57 class DOMAIN_RELIABILITY_EXPORT 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;
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;
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
{
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
{
104 MockableTimeBackoffEntry(const net::BackoffEntry::Policy
* const policy
,
107 ~MockableTimeBackoffEntry() override
;
110 base::TimeTicks
ImplGetTimeNow() const override
;
116 } // namespace domain_reliability
118 #endif // COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_