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_CONFIG_H_
6 #define COMPONENTS_DOMAIN_RELIABILITY_CONFIG_H_
11 #include "base/compiler_specific.h"
12 #include "base/json/json_value_converter.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/string_piece.h"
15 #include "base/values.h"
16 #include "components/domain_reliability/domain_reliability_export.h"
19 namespace domain_reliability
{
21 // The configuration that controls which requests are measured and reported,
22 // with what frequency, and where the beacons are uploaded.
23 class DOMAIN_RELIABILITY_EXPORT DomainReliabilityConfig
{
25 // A particular resource named in the config -- includes a set of URL
26 // patterns that the resource will match, along with sample rates for
27 // successful and unsuccessful requests.
28 class DOMAIN_RELIABILITY_EXPORT Resource
{
33 // Returns whether |url_string| matches at least one of the |url_patterns|
35 bool MatchesUrlString(const std::string
& url_string
) const;
37 // Returns whether a request (that was successful if |success| is true)
38 // should be reported (with a full beacon). (The output is random; it
39 // compares a random number to |success_sample_rate| or
40 // |failure_sample_rate|.)
41 bool DecideIfShouldReportRequest(bool success
) const;
43 // Registers with the JSONValueConverter so it will know how to convert the
44 // JSON for a named resource into the struct.
45 static void RegisterJSONConverter(
46 base::JSONValueConverter
<Resource
>* converter
);
48 // Name of the Resource, as will be reported in uploads.
51 // List of URL patterns to assign requests to this Resource.
52 ScopedVector
<std::string
> url_patterns
;
54 // Sample rates for successful and unsuccessful requests, respectively.
55 // 0.0 reports no requests, and 1.0 reports every request.
56 double success_sample_rate
;
57 double failure_sample_rate
;
60 DISALLOW_COPY_AND_ASSIGN(Resource
);
63 // A particular endpoint for report uploads. Includes the URL to upload
64 // reports to. May include a verification URL or backoff/load management
65 // configuration in the future.
66 struct DOMAIN_RELIABILITY_EXPORT Collector
{
71 // Registers with the JSONValueConverter so it will know how to convert the
72 // JSON for a collector into the struct.
73 static void RegisterJSONConverter(
74 base::JSONValueConverter
<Collector
>* converter
);
79 DISALLOW_COPY_AND_ASSIGN(Collector
);
82 DomainReliabilityConfig();
83 ~DomainReliabilityConfig();
85 // Uses the JSONValueConverter to parse the JSON for a config into a struct.
86 static scoped_ptr
<const DomainReliabilityConfig
> FromJSON(
87 const base::StringPiece
& json
);
89 // Finds the index (in resources) of the first Resource that matches a
90 // particular URL. Returns -1 if the URL is not matched by any Resources.
91 int GetResourceIndexForUrl(const GURL
& url
) const;
93 // Registers with the JSONValueConverter so it will know how to convert the
94 // JSON for a config into the struct.
95 static void RegisterJSONConverter(
96 base::JSONValueConverter
<DomainReliabilityConfig
>* converter
);
98 std::string config_version
;
100 ScopedVector
<Resource
> resources
;
101 ScopedVector
<Collector
> collectors
;
103 // TODO(ttuttle): Add config_valid_util when fetching and expiring configs
107 DISALLOW_COPY_AND_ASSIGN(DomainReliabilityConfig
);
110 } // namespace domain_reliability
112 #endif // COMPONENTS_DOMAIN_RELIABILITY_CONFIG_H_