psl_matching_helper.cc revision 23730a6e56a168d1879203e4b3819bb36e3d8f1f
1// Copyright 2013 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#include "components/password_manager/core/browser/psl_matching_helper.h"
6
7#include "base/command_line.h"
8#include "base/logging.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/metrics/field_trial.h"
11#include "components/autofill/core/common/password_form.h"
12#include "components/password_manager/core/common/password_manager_switches.h"
13#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
14#include "url/gurl.h"
15
16using autofill::PasswordForm;
17
18#if !defined(OS_IOS)
19namespace {
20
21const char kPSLMatchingDesktopFieldTrialName[] = "PSLMatchingDesktop";
22const char kPSLMatchingDesktopFieldTrialDisabledGroupName[] = "Disabled";
23
24}  // namespace
25#endif
26
27bool PSLMatchingHelper::psl_enabled_override_ = false;
28
29PSLMatchingHelper::PSLMatchingHelper() : psl_enabled_(DeterminePSLEnabled()) {}
30
31PSLMatchingHelper::~PSLMatchingHelper() {}
32
33bool PSLMatchingHelper::IsMatchingEnabled() const {
34  return psl_enabled_override_ || psl_enabled_;
35}
36
37bool PSLMatchingHelper::ShouldPSLDomainMatchingApply(
38    const std::string& registry_controlled_domain) const {
39  return IsMatchingEnabled() && registry_controlled_domain != "google.com";
40}
41
42// static
43bool PSLMatchingHelper::IsPublicSuffixDomainMatch(const std::string& url1,
44                                                  const std::string& url2) {
45  GURL gurl1(url1);
46  GURL gurl2(url2);
47  return gurl1.scheme() == gurl2.scheme() &&
48         GetRegistryControlledDomain(gurl1) ==
49             GetRegistryControlledDomain(gurl2) &&
50         gurl1.port() == gurl2.port();
51}
52
53// static
54std::string PSLMatchingHelper::GetRegistryControlledDomain(
55    const GURL& signon_realm) {
56  return net::registry_controlled_domains::GetDomainAndRegistry(
57      signon_realm,
58      net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
59}
60
61// static
62void PSLMatchingHelper::EnablePublicSuffixDomainMatchingForTesting() {
63  psl_enabled_override_ = true;
64}
65
66// static
67bool PSLMatchingHelper::DeterminePSLEnabled() {
68  bool enabled = true;
69#if !defined(OS_IOS)
70  if (base::FieldTrialList::FindFullName(kPSLMatchingDesktopFieldTrialName) ==
71      kPSLMatchingDesktopFieldTrialDisabledGroupName) {
72    enabled = false;
73  }
74#endif
75  return enabled;
76}
77