summaryrefslogtreecommitdiffstats
path: root/chrome/browser/metrics/rappor/sampling.cc
blob: 01ecb358cf0afe820907fa0395c32e7c3f5eabee (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
// 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 "chrome/browser/metrics/rappor/sampling.h"

#include "chrome/browser/browser_process.h"
#include "components/rappor/rappor_service.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "url/gurl.h"

namespace rappor {

std::string GetDomainAndRegistrySampleFromGURL(const GURL& gurl) {
  if (gurl.SchemeIsHTTPOrHTTPS()) {
    return net::registry_controlled_domains::GetDomainAndRegistry(
        gurl, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
  }
  if (gurl.SchemeIsFile())
    return gurl.scheme() + "://";
  return gurl.scheme() + "://" + gurl.host();
}

void SampleDomainAndRegistryFromGURL(const std::string& metric,
                                     const GURL& gurl) {
  if (!g_browser_process->rappor_service())
    return;
  g_browser_process->rappor_service()->RecordSample(
      metric,
      rappor::ETLD_PLUS_ONE_RAPPOR_TYPE,
      GetDomainAndRegistrySampleFromGURL(gurl));
}

void SampleDomainAndRegistryFromHost(const std::string& metric,
                                     const std::string& host) {
  if (!g_browser_process->rappor_service())
    return;
  g_browser_process->rappor_service()->RecordSample(
      metric,
      rappor::ETLD_PLUS_ONE_RAPPOR_TYPE,
      net::registry_controlled_domains::GetDomainAndRegistry(
          host, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES));
}

}  // namespace rappor