Ignore title parameter for navigator.registerProtocolHandler
[chromium-blink-merge.git] / components / domain_reliability / config.cc
blobc8e9793501551109d5a4c7fc5c67a50312b5d99c
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 // Make sure stdint.h includes SIZE_MAX. (See C89, p259, footnote 221.)
6 #ifndef __STDC_LIMIT_MACROS
7 #define __STDC_LIMIT_MACROS 1
8 #endif
10 #include "components/domain_reliability/config.h"
12 #include <stdint.h>
14 #include "base/json/json_reader.h"
15 #include "base/json/json_value_converter.h"
16 #include "base/rand_util.h"
17 #include "base/strings/string_util.h"
19 namespace {
21 bool ConvertURL(const base::StringPiece& string_piece, GURL* url) {
22 *url = GURL(string_piece.as_string());
23 return url->is_valid();
26 bool IsValidSampleRate(double p) { return p >= 0.0 && p <= 1.0; }
28 } // namespace
30 namespace domain_reliability {
32 // static
33 const size_t DomainReliabilityConfig::kInvalidResourceIndex = SIZE_MAX;
35 DomainReliabilityConfig::Resource::Resource() {
37 DomainReliabilityConfig::Resource::~Resource() {}
39 bool DomainReliabilityConfig::Resource::MatchesUrl(const GURL& url) const {
40 const std::string& spec = url.spec();
42 ScopedVector<std::string>::const_iterator it;
43 for (it = url_patterns.begin(); it != url_patterns.end(); it++) {
44 if (MatchPattern(spec, **it))
45 return true;
48 return false;
51 bool DomainReliabilityConfig::Resource::DecideIfShouldReportRequest(
52 bool success) const {
53 double sample_rate = success ? success_sample_rate : failure_sample_rate;
54 DCHECK(IsValidSampleRate(sample_rate));
55 return base::RandDouble() < sample_rate;
58 // static
59 void DomainReliabilityConfig::Resource::RegisterJSONConverter(
60 base::JSONValueConverter<DomainReliabilityConfig::Resource>* converter) {
61 converter->RegisterStringField("resource_name", &Resource::name);
62 converter->RegisterRepeatedString("url_patterns", &Resource::url_patterns);
63 converter->RegisterDoubleField("success_sample_rate",
64 &Resource::success_sample_rate);
65 converter->RegisterDoubleField("failure_sample_rate",
66 &Resource::failure_sample_rate);
69 bool DomainReliabilityConfig::Resource::IsValid() const {
70 return !name.empty() && !url_patterns.empty() &&
71 IsValidSampleRate(success_sample_rate) &&
72 IsValidSampleRate(failure_sample_rate);
75 DomainReliabilityConfig::Collector::Collector() {}
76 DomainReliabilityConfig::Collector::~Collector() {}
78 // static
79 void DomainReliabilityConfig::Collector::RegisterJSONConverter(
80 base::JSONValueConverter<DomainReliabilityConfig::Collector>* converter) {
81 converter->RegisterCustomField<GURL>("upload_url", &Collector::upload_url,
82 &ConvertURL);
85 bool DomainReliabilityConfig::Collector::IsValid() const {
86 return upload_url.is_valid();
89 DomainReliabilityConfig::DomainReliabilityConfig() : valid_until(0.0) {}
90 DomainReliabilityConfig::~DomainReliabilityConfig() {}
92 // static
93 scoped_ptr<const DomainReliabilityConfig> DomainReliabilityConfig::FromJSON(
94 const base::StringPiece& json) {
95 scoped_ptr<base::Value> value(base::JSONReader::Read(json));
96 base::JSONValueConverter<DomainReliabilityConfig> converter;
97 DomainReliabilityConfig* config = new DomainReliabilityConfig();
99 // If we can parse and convert the JSON into a valid config, return that.
100 if (value && converter.Convert(*value, config) && config->IsValid())
101 return scoped_ptr<const DomainReliabilityConfig>(config);
102 else
103 return scoped_ptr<const DomainReliabilityConfig>();
106 bool DomainReliabilityConfig::IsValid() const {
107 if (valid_until == 0.0 || domain.empty() || version.empty() ||
108 resources.empty() || collectors.empty()) {
109 return false;
112 for (size_t i = 0; i < resources.size(); ++i) {
113 if (!resources[i]->IsValid())
114 return false;
117 for (size_t i = 0; i < collectors.size(); ++i) {
118 if (!collectors[i]->IsValid())
119 return false;
122 return true;
125 bool DomainReliabilityConfig::IsExpired(base::Time now) const {
126 DCHECK_NE(0.0, valid_until);
127 base::Time valid_until_time = base::Time::FromDoubleT(valid_until);
128 return now > valid_until_time;
131 size_t DomainReliabilityConfig::GetResourceIndexForUrl(const GURL& url) const {
132 // Removes username, password, and fragment.
133 GURL sanitized_url = url.GetAsReferrer();
135 for (size_t i = 0; i < resources.size(); ++i) {
136 if (resources[i]->MatchesUrl(sanitized_url))
137 return i;
140 return kInvalidResourceIndex;
143 // static
144 void DomainReliabilityConfig::RegisterJSONConverter(
145 base::JSONValueConverter<DomainReliabilityConfig>* converter) {
146 converter->RegisterStringField("config_version",
147 &DomainReliabilityConfig::version);
148 converter->RegisterDoubleField("config_valid_until",
149 &DomainReliabilityConfig::valid_until);
150 converter->RegisterStringField("monitored_domain",
151 &DomainReliabilityConfig::domain);
152 converter->RegisterRepeatedMessage("monitored_resources",
153 &DomainReliabilityConfig::resources);
154 converter->RegisterRepeatedMessage("collectors",
155 &DomainReliabilityConfig::collectors);
158 } // namespace domain_reliability