[email protected] | 45de676a | 2014-03-18 23:52:02 | [diff] [blame] | 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 | |
| 5 | #ifndef COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_ |
| 6 | #define COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_ |
| 7 | |
| 8 | #include <map> |
dcheng | 04a35cd | 2016-04-22 15:07:24 | [diff] [blame] | 9 | #include <memory> |
[email protected] | 45de676a | 2014-03-18 23:52:02 | [diff] [blame] | 10 | |
[email protected] | b5c2b74 | 2014-06-14 22:21:42 | [diff] [blame] | 11 | #include "base/callback_forward.h" |
[email protected] | 45de676a | 2014-03-18 23:52:02 | [diff] [blame] | 12 | #include "base/compiler_specific.h" |
avi | bc5337b | 2015-12-25 23:16:33 | [diff] [blame] | 13 | #include "base/macros.h" |
johnme | dce40c3 | 2015-04-20 17:06:20 | [diff] [blame] | 14 | #include "base/time/clock.h" |
| 15 | #include "base/time/tick_clock.h" |
[email protected] | 45de676a | 2014-03-18 23:52:02 | [diff] [blame] | 16 | #include "base/time/time.h" |
[email protected] | 45de676a | 2014-03-18 23:52:02 | [diff] [blame] | 17 | #include "components/domain_reliability/domain_reliability_export.h" |
ttuttle | 280a73d | 2014-11-13 21:38:04 | [diff] [blame] | 18 | #include "components/domain_reliability/uploader.h" |
[email protected] | 414617b | 2014-06-30 16:49:13 | [diff] [blame] | 19 | #include "net/http/http_response_info.h" |
ttuttle | 280a73d | 2014-11-13 21:38:04 | [diff] [blame] | 20 | #include "net/url_request/url_request_status.h" |
[email protected] | 45de676a | 2014-03-18 23:52:02 | [diff] [blame] | 21 | |
Brett Wilson | 0241e02b | 2017-09-15 22:45:35 | [diff] [blame] | 22 | namespace base { |
| 23 | class Location; |
| 24 | } |
| 25 | |
[email protected] | 45de676a | 2014-03-18 23:52:02 | [diff] [blame] | 26 | namespace domain_reliability { |
| 27 | |
[email protected] | 84d2a49 | 2014-05-09 22:18:50 | [diff] [blame] | 28 | // 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. |
| 34 | bool GetDomainReliabilityBeaconStatus( |
| 35 | int net_error, |
| 36 | int http_response_code, |
| 37 | std::string* beacon_status_out); |
[email protected] | 45de676a | 2014-03-18 23:52:02 | [diff] [blame] | 38 | |
[email protected] | 414617b | 2014-06-30 16:49:13 | [diff] [blame] | 39 | std::string GetDomainReliabilityProtocol( |
| 40 | net::HttpResponseInfo::ConnectionInfo connection_info, |
| 41 | bool ssl_info_populated); |
| 42 | |
ttuttle | 280a73d | 2014-11-13 21:38:04 | [diff] [blame] | 43 | // 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. |
| 46 | int 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. |
| 50 | void GetUploadResultFromResponseDetails( |
| 51 | int net_error, |
| 52 | int http_response_code, |
| 53 | base::TimeDelta retry_after, |
| 54 | DomainReliabilityUploader::UploadResult* result); |
| 55 | |
leon.han | cf889c7 | 2017-01-14 02:31:22 | [diff] [blame] | 56 | GURL SanitizeURLForReport( |
| 57 | const GURL& beacon_url, |
| 58 | const GURL& collector_url, |
| 59 | const std::vector<std::unique_ptr<std::string>>& path_prefixes); |
ttuttle | ccdd6cc5 | 2015-11-22 06:09:40 | [diff] [blame] | 60 | |
[email protected] | 45de676a | 2014-03-18 23:52:02 | [diff] [blame] | 61 | // Mockable wrapper around TimeTicks::Now and Timer. Mock version is in |
| 62 | // test_util.h. |
ttuttle | 960fcbf | 2016-04-19 13:26:32 | [diff] [blame] | 63 | // TODO(juliatuttle): Rename to Time{Provider,Source,?}. |
johnme | dce40c3 | 2015-04-20 17:06:20 | [diff] [blame] | 64 | class DOMAIN_RELIABILITY_EXPORT MockableTime : public base::Clock, |
| 65 | public base::TickClock { |
[email protected] | 45de676a | 2014-03-18 23:52:02 | [diff] [blame] | 66 | public: |
| 67 | // Mockable wrapper around (a subset of) base::Timer. |
| 68 | class DOMAIN_RELIABILITY_EXPORT Timer { |
| 69 | public: |
| 70 | virtual ~Timer(); |
| 71 | |
Brett Wilson | 190fd2ac | 2017-09-12 05:04:30 | [diff] [blame] | 72 | virtual void Start(const base::Location& posted_from, |
[email protected] | 45de676a | 2014-03-18 23:52:02 | [diff] [blame] | 73 | 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 | |
johnme | dce40c3 | 2015-04-20 17:06:20 | [diff] [blame] | 82 | ~MockableTime() override; |
[email protected] | 45de676a | 2014-03-18 23:52:02 | [diff] [blame] | 83 | |
johnme | dce40c3 | 2015-04-20 17:06:20 | [diff] [blame] | 84 | // Clock impl; returns base::Time::Now() or a mocked version thereof. |
tzik | ee78e496 | 2018-04-13 12:25:46 | [diff] [blame] | 85 | base::Time Now() const override = 0; |
johnme | dce40c3 | 2015-04-20 17:06:20 | [diff] [blame] | 86 | // TickClock impl; returns base::TimeTicks::Now() or a mocked version thereof. |
Greg Thompson | 821f0e338 | 2018-03-27 03:15:03 | [diff] [blame] | 87 | base::TimeTicks NowTicks() const override = 0; |
johnme | dce40c3 | 2015-04-20 17:06:20 | [diff] [blame] | 88 | |
[email protected] | 45de676a | 2014-03-18 23:52:02 | [diff] [blame] | 89 | // Returns a new Timer, or a mocked version thereof. |
dcheng | 04a35cd | 2016-04-22 15:07:24 | [diff] [blame] | 90 | virtual std::unique_ptr<MockableTime::Timer> CreateTimer() = 0; |
[email protected] | 45de676a | 2014-03-18 23:52:02 | [diff] [blame] | 91 | |
| 92 | protected: |
| 93 | MockableTime(); |
| 94 | |
| 95 | private: |
| 96 | DISALLOW_COPY_AND_ASSIGN(MockableTime); |
| 97 | }; |
| 98 | |
[email protected] | a5553ace | 2014-04-15 03:42:28 | [diff] [blame] | 99 | // Implementation of MockableTime that passes through to |
| 100 | // base::Time{,Ticks}::Now() and base::Timer. |
[email protected] | 45de676a | 2014-03-18 23:52:02 | [diff] [blame] | 101 | class DOMAIN_RELIABILITY_EXPORT ActualTime : public MockableTime { |
| 102 | public: |
| 103 | ActualTime(); |
[email protected] | 84d2a49 | 2014-05-09 22:18:50 | [diff] [blame] | 104 | |
dcheng | 00ea022b | 2014-10-21 11:24:56 | [diff] [blame] | 105 | ~ActualTime() override; |
[email protected] | 45de676a | 2014-03-18 23:52:02 | [diff] [blame] | 106 | |
[email protected] | bda8e36 | 2014-03-24 18:21:03 | [diff] [blame] | 107 | // MockableTime implementation: |
tzik | ee78e496 | 2018-04-13 12:25:46 | [diff] [blame] | 108 | base::Time Now() const override; |
Greg Thompson | 821f0e338 | 2018-03-27 03:15:03 | [diff] [blame] | 109 | base::TimeTicks NowTicks() const override; |
dcheng | 04a35cd | 2016-04-22 15:07:24 | [diff] [blame] | 110 | std::unique_ptr<MockableTime::Timer> CreateTimer() override; |
[email protected] | 45de676a | 2014-03-18 23:52:02 | [diff] [blame] | 111 | }; |
| 112 | |
[email protected] | 45de676a | 2014-03-18 23:52:02 | [diff] [blame] | 113 | } // namespace domain_reliability |
| 114 | |
| 115 | #endif // COMPONENTS_DOMAIN_RELIABILITY_UTIL_H_ |