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/clock.h"
14 #include "base/time/tick_clock.h"
15 #include "base/time/time.h"
16 #include "base/tracked_objects.h"
17 #include "components/domain_reliability/domain_reliability_export.h"
18 #include "components/domain_reliability/uploader.h"
19 #include "net/http/http_response_info.h"
20 #include "net/url_request/url_request_status.h"
22 namespace domain_reliability
{
24 // Attempts to convert a net error and an HTTP response code into the status
25 // string that should be recorded in a beacon. Returns true if it could.
27 // N.B.: This functions as the whitelist of "safe" errors to report; network-
28 // local errors are purposefully not converted to avoid revealing
29 // information about the local network to the remote server.
30 bool GetDomainReliabilityBeaconStatus(
32 int http_response_code
,
33 std::string
* beacon_status_out
);
35 std::string
GetDomainReliabilityProtocol(
36 net::HttpResponseInfo::ConnectionInfo connection_info
,
37 bool ssl_info_populated
);
39 // Converts a URLRequestStatus into a network error. Returns the error code for
40 // FAILED; maps SUCCESS and CANCELED to OK and ERR_ABORTED, respectively; and
41 // returns ERR_ABORTED for any other status.
42 int GetNetErrorFromURLRequestStatus(const net::URLRequestStatus
& status
);
44 // Based on the network error code, HTTP response code, and Retry-After value,
45 // fills |status| with the result of a report upload.
46 void GetUploadResultFromResponseDetails(
48 int http_response_code
,
49 base::TimeDelta retry_after
,
50 DomainReliabilityUploader::UploadResult
* result
);
52 // Mockable wrapper around TimeTicks::Now and Timer. Mock version is in
54 // TODO(ttuttle): Rename to Time{Provider,Source,?}.
55 class DOMAIN_RELIABILITY_EXPORT MockableTime
: public base::Clock
,
56 public base::TickClock
{
58 // Mockable wrapper around (a subset of) base::Timer.
59 class DOMAIN_RELIABILITY_EXPORT Timer
{
63 virtual void Start(const tracked_objects::Location
& posted_from
,
64 base::TimeDelta delay
,
65 const base::Closure
& user_task
) = 0;
66 virtual void Stop() = 0;
67 virtual bool IsRunning() = 0;
73 ~MockableTime() override
;
75 // Clock impl; returns base::Time::Now() or a mocked version thereof.
76 base::Time
Now() override
= 0;
77 // TickClock impl; returns base::TimeTicks::Now() or a mocked version thereof.
78 base::TimeTicks
NowTicks() override
= 0;
80 // Returns a new Timer, or a mocked version thereof.
81 virtual scoped_ptr
<MockableTime::Timer
> CreateTimer() = 0;
87 DISALLOW_COPY_AND_ASSIGN(MockableTime
);
90 // Implementation of MockableTime that passes through to
91 // base::Time{,Ticks}::Now() and base::Timer.
92 class DOMAIN_RELIABILITY_EXPORT ActualTime
: public MockableTime
{
96 ~ActualTime() override
;
98 // MockableTime implementation:
99 base::Time
Now() override
;
100 base::TimeTicks
NowTicks() override
;
101 scoped_ptr
<MockableTime::Timer
> CreateTimer() override
;
104 } // namespace domain_reliability
106 #endif // COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_