blob: b0fba9d5e555adc2d20ebc50330b51246138216b [file] [log] [blame]
[email protected]45de676a2014-03-18 23:52:021// 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
5#ifndef COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_
6#define COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_
7
8#include <map>
dcheng04a35cd2016-04-22 15:07:249#include <memory>
[email protected]45de676a2014-03-18 23:52:0210
[email protected]b5c2b742014-06-14 22:21:4211#include "base/callback_forward.h"
[email protected]45de676a2014-03-18 23:52:0212#include "base/compiler_specific.h"
avibc5337b2015-12-25 23:16:3313#include "base/macros.h"
johnmedce40c32015-04-20 17:06:2014#include "base/time/clock.h"
15#include "base/time/tick_clock.h"
[email protected]45de676a2014-03-18 23:52:0216#include "base/time/time.h"
[email protected]45de676a2014-03-18 23:52:0217#include "components/domain_reliability/domain_reliability_export.h"
ttuttle280a73d2014-11-13 21:38:0418#include "components/domain_reliability/uploader.h"
[email protected]414617b2014-06-30 16:49:1319#include "net/http/http_response_info.h"
ttuttle280a73d2014-11-13 21:38:0420#include "net/url_request/url_request_status.h"
[email protected]45de676a2014-03-18 23:52:0221
Brett Wilson0241e02b2017-09-15 22:45:3522namespace base {
23class Location;
24}
25
[email protected]45de676a2014-03-18 23:52:0226namespace domain_reliability {
27
[email protected]84d2a492014-05-09 22:18:5028// Attempts to convert a net error and an HTTP response code into the status
29// string that should be recorded in a beacon. Returns true if it could.
30//
31// N.B.: This functions as the whitelist of "safe" errors to report; network-
32// local errors are purposefully not converted to avoid revealing
33// information about the local network to the remote server.
34bool GetDomainReliabilityBeaconStatus(
35 int net_error,
36 int http_response_code,
37 std::string* beacon_status_out);
[email protected]45de676a2014-03-18 23:52:0238
[email protected]414617b2014-06-30 16:49:1339std::string GetDomainReliabilityProtocol(
40 net::HttpResponseInfo::ConnectionInfo connection_info,
41 bool ssl_info_populated);
42
ttuttle280a73d2014-11-13 21:38:0443// Converts a URLRequestStatus into a network error. Returns the error code for
44// FAILED; maps SUCCESS and CANCELED to OK and ERR_ABORTED, respectively; and
45// returns ERR_ABORTED for any other status.
46int GetNetErrorFromURLRequestStatus(const net::URLRequestStatus& status);
47
48// Based on the network error code, HTTP response code, and Retry-After value,
49// fills |status| with the result of a report upload.
50void GetUploadResultFromResponseDetails(
51 int net_error,
52 int http_response_code,
53 base::TimeDelta retry_after,
54 DomainReliabilityUploader::UploadResult* result);
55
leon.hancf889c72017-01-14 02:31:2256GURL SanitizeURLForReport(
57 const GURL& beacon_url,
58 const GURL& collector_url,
59 const std::vector<std::unique_ptr<std::string>>& path_prefixes);
ttuttleccdd6cc52015-11-22 06:09:4060
[email protected]45de676a2014-03-18 23:52:0261// Mockable wrapper around TimeTicks::Now and Timer. Mock version is in
62// test_util.h.
ttuttle960fcbf2016-04-19 13:26:3263// TODO(juliatuttle): Rename to Time{Provider,Source,?}.
johnmedce40c32015-04-20 17:06:2064class DOMAIN_RELIABILITY_EXPORT MockableTime : public base::Clock,
65 public base::TickClock {
[email protected]45de676a2014-03-18 23:52:0266 public:
67 // Mockable wrapper around (a subset of) base::Timer.
68 class DOMAIN_RELIABILITY_EXPORT Timer {
69 public:
70 virtual ~Timer();
71
Brett Wilson190fd2ac2017-09-12 05:04:3072 virtual void Start(const base::Location& posted_from,
[email protected]45de676a2014-03-18 23:52:0273 base::TimeDelta delay,
74 const base::Closure& user_task) = 0;
75 virtual void Stop() = 0;
76 virtual bool IsRunning() = 0;
77
78 protected:
79 Timer();
80 };
81
johnmedce40c32015-04-20 17:06:2082 ~MockableTime() override;
[email protected]45de676a2014-03-18 23:52:0283
johnmedce40c32015-04-20 17:06:2084 // Clock impl; returns base::Time::Now() or a mocked version thereof.
tzikee78e4962018-04-13 12:25:4685 base::Time Now() const override = 0;
johnmedce40c32015-04-20 17:06:2086 // TickClock impl; returns base::TimeTicks::Now() or a mocked version thereof.
Greg Thompson821f0e3382018-03-27 03:15:0387 base::TimeTicks NowTicks() const override = 0;
johnmedce40c32015-04-20 17:06:2088
[email protected]45de676a2014-03-18 23:52:0289 // Returns a new Timer, or a mocked version thereof.
dcheng04a35cd2016-04-22 15:07:2490 virtual std::unique_ptr<MockableTime::Timer> CreateTimer() = 0;
[email protected]45de676a2014-03-18 23:52:0291
92 protected:
93 MockableTime();
94
95 private:
96 DISALLOW_COPY_AND_ASSIGN(MockableTime);
97};
98
[email protected]a5553ace2014-04-15 03:42:2899// Implementation of MockableTime that passes through to
100// base::Time{,Ticks}::Now() and base::Timer.
[email protected]45de676a2014-03-18 23:52:02101class DOMAIN_RELIABILITY_EXPORT ActualTime : public MockableTime {
102 public:
103 ActualTime();
[email protected]84d2a492014-05-09 22:18:50104
dcheng00ea022b2014-10-21 11:24:56105 ~ActualTime() override;
[email protected]45de676a2014-03-18 23:52:02106
[email protected]bda8e362014-03-24 18:21:03107 // MockableTime implementation:
tzikee78e4962018-04-13 12:25:46108 base::Time Now() const override;
Greg Thompson821f0e3382018-03-27 03:15:03109 base::TimeTicks NowTicks() const override;
dcheng04a35cd2016-04-22 15:07:24110 std::unique_ptr<MockableTime::Timer> CreateTimer() override;
[email protected]45de676a2014-03-18 23:52:02111};
112
[email protected]45de676a2014-03-18 23:52:02113} // namespace domain_reliability
114
115#endif // COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_