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