summaryrefslogtreecommitdiff
path: root/chromium/components/domain_reliability/config.cc
blob: 6266afc0e57e0860cd96c5a5ac9d6f4a9bfbef21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/domain_reliability/config.h"

#include <utility>

#include "base/json/json_reader.h"
#include "base/optional.h"
#include "url/url_constants.h"

namespace {

bool ConvertURL(const base::Value* value, GURL* url) {
  std::string url_string;
  if (!value->GetAsString(&url_string))
    return false;
  *url = GURL(url_string);
  return url->is_valid();
}

bool ConvertOrigin(const base::Value* value, GURL* url) {
  return ConvertURL(value, url) && !url->has_username() &&
         !url->has_password() && url->SchemeIs(url::kHttpsScheme) &&
         url->path_piece() == "/" && !url->has_query() && !url->has_ref();
}

bool IsValidSampleRate(double p) {
  return p >= 0.0 && p <= 1.0;
}

}  // namespace

namespace domain_reliability {

DomainReliabilityConfig::DomainReliabilityConfig()
    : include_subdomains(false),
      success_sample_rate(-1.0),
      failure_sample_rate(-1.0) {
}
DomainReliabilityConfig::~DomainReliabilityConfig() {}

// static
std::unique_ptr<const DomainReliabilityConfig>
DomainReliabilityConfig::FromJSON(const base::StringPiece& json) {
  base::Optional<base::Value> value = base::JSONReader::Read(json);
  if (!value)
    return nullptr;

  base::JSONValueConverter<DomainReliabilityConfig> converter;
  auto config = std::make_unique<DomainReliabilityConfig>();

  // If we can parse and convert the JSON into a valid config, return that.
  if (converter.Convert(*value, config.get()) && config->IsValid())
    return config;

  return nullptr;
}

bool DomainReliabilityConfig::IsValid() const {
  if (!origin.is_valid() || collectors.empty() ||
      !IsValidSampleRate(success_sample_rate) ||
      !IsValidSampleRate(failure_sample_rate)) {
    return false;
  }

  for (const auto& url : collectors) {
    if (!url->SchemeIs(url::kHttpsScheme) || !url->is_valid())
      return false;
  }

  return true;
}

double DomainReliabilityConfig::GetSampleRate(bool request_successful) const {
  return request_successful ? success_sample_rate : failure_sample_rate;
}

// static
void DomainReliabilityConfig::RegisterJSONConverter(
    base::JSONValueConverter<DomainReliabilityConfig>* converter) {
  converter->RegisterCustomValueField<GURL>(
      "origin", &DomainReliabilityConfig::origin, &ConvertOrigin);
  converter->RegisterBoolField("include_subdomains",
                               &DomainReliabilityConfig::include_subdomains);
  converter->RegisterRepeatedCustomValue(
      "collectors", &DomainReliabilityConfig::collectors, &ConvertURL);
  converter->RegisterRepeatedString("path_prefixes",
                                    &DomainReliabilityConfig::path_prefixes);
  converter->RegisterDoubleField("success_sample_rate",
                                 &DomainReliabilityConfig::success_sample_rate);
  converter->RegisterDoubleField("failure_sample_rate",
                                 &DomainReliabilityConfig::failure_sample_rate);
}

}  // namespace domain_reliability