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_CONFIG_H_
6#define COMPONENTS_DOMAIN_RELIABILITY_CONFIG_H_
7
8#include <string>
9#include <vector>
10
11#include "base/json/json_value_converter.h"
12#include "base/macros.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/strings/string_piece.h"
15#include "base/time/time.h"
16#include "base/values.h"
17#include "components/domain_reliability/domain_reliability_export.h"
18#include "url/gurl.h"
19
20namespace domain_reliability {
21
22// The configuration that controls which requests are measured and reported,
23// with what frequency, and where the beacons are uploaded.
24class DOMAIN_RELIABILITY_EXPORT DomainReliabilityConfig {
25 public:
26  static const size_t kInvalidResourceIndex;
27
28  // A particular resource named in the config -- includes a set of URL
29  // patterns that the resource will match, along with sample rates for
30  // successful and unsuccessful requests.
31  class DOMAIN_RELIABILITY_EXPORT Resource {
32   public:
33    Resource();
34    ~Resource();
35
36    // Returns whether |url_string| matches at least one of the |url_patterns|
37    // in this Resource.
38    bool MatchesUrl(const GURL& url) const;
39
40    // Returns whether a request (that was successful if |success| is true)
41    // should be reported with a full beacon. (The output is non-deterministic;
42    // it |success_sample_rate| or |failure_sample_rate| to a random number.)
43    bool DecideIfShouldReportRequest(bool success) const;
44
45    // Registers with the JSONValueConverter so it will know how to convert the
46    // JSON for a named resource into the struct.
47    static void RegisterJSONConverter(
48        base::JSONValueConverter<Resource>* converter);
49
50    bool IsValid() const;
51
52    // Name of the Resource, as will be reported in uploads.
53    std::string name;
54
55    // List of URL patterns to assign requests to this Resource.
56    ScopedVector<std::string> url_patterns;
57
58    // Sample rates for successful and unsuccessful requests, respectively.
59    // 0.0 reports no requests, and 1.0 reports every request.
60    double success_sample_rate;
61    double failure_sample_rate;
62
63   private:
64    DISALLOW_COPY_AND_ASSIGN(Resource);
65  };
66
67  // A particular endpoint for report uploads. Includes the URL to upload
68  // reports to. May include a verification URL or backoff/load management
69  // configuration in the future.
70  struct DOMAIN_RELIABILITY_EXPORT Collector {
71   public:
72    Collector();
73    ~Collector();
74
75    // Registers with the JSONValueConverter so it will know how to convert the
76    // JSON for a collector into the struct.
77    static void RegisterJSONConverter(
78        base::JSONValueConverter<Collector>* converter);
79
80    bool IsValid() const;
81
82    GURL upload_url;
83
84   private:
85    DISALLOW_COPY_AND_ASSIGN(Collector);
86  };
87
88  DomainReliabilityConfig();
89  ~DomainReliabilityConfig();
90
91  // Uses the JSONValueConverter to parse the JSON for a config into a struct.
92  static scoped_ptr<const DomainReliabilityConfig> FromJSON(
93      const base::StringPiece& json);
94
95  bool IsValid() const;
96
97  // Checks whether |now| is past the expiration time provided in the config.
98  bool IsExpired(base::Time now) const;
99
100  // Finds the index (in resources) of the first Resource that matches a
101  // particular URL. Returns kInvalidResourceIndex if it is not matched by any
102  // Resources.
103  size_t GetResourceIndexForUrl(const GURL& url) const;
104
105  // Registers with the JSONValueConverter so it will know how to convert the
106  // JSON for a config into the struct.
107  static void RegisterJSONConverter(
108      base::JSONValueConverter<DomainReliabilityConfig>* converter);
109
110  std::string version;
111  double valid_until;
112  std::string domain;
113  ScopedVector<Resource> resources;
114  ScopedVector<Collector> collectors;
115
116 private:
117  DISALLOW_COPY_AND_ASSIGN(DomainReliabilityConfig);
118};
119
120}  // namespace domain_reliability
121
122#endif  // COMPONENTS_DOMAIN_RELIABILITY_CONFIG_H_
123