Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / components / password_manager / core / browser / psl_matching_helper.cc
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
16 using autofill::PasswordForm;
17
18 #if !defined(OS_ANDROID) && !defined(OS_IOS)
19 namespace {
20
21 const char kPSLMatchingDesktopFieldTrialName[] = "PSLMatchingDesktop";
22 const char kPSLMatchingDesktopFieldTrialDisabledGroupName[] = "Disabled";
23
24 }  // namespace
25 #endif
26
27 bool PSLMatchingHelper::psl_enabled_override_ = false;
28
29 PSLMatchingHelper::PSLMatchingHelper() : psl_enabled_(DeterminePSLEnabled()) {}
30
31 PSLMatchingHelper::~PSLMatchingHelper() {}
32
33 bool PSLMatchingHelper::IsMatchingEnabled() const {
34   return psl_enabled_override_ || psl_enabled_;
35 }
36
37 bool PSLMatchingHelper::ShouldPSLDomainMatchingApply(
38     const std::string& registry_controlled_domain) const {
39   return IsMatchingEnabled() && registry_controlled_domain != "google.com";
40 }
41
42 // static
43 bool 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
54 std::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
62 void PSLMatchingHelper::EnablePublicSuffixDomainMatchingForTesting() {
63   psl_enabled_override_ = true;
64 }
65
66 // static
67 bool PSLMatchingHelper::DeterminePSLEnabled() {
68   // Default choice is "enabled", so we do not need to check for
69   // kEnablePasswordAutofillPublicSuffixDomainMatching.
70   bool enabled = true;
71 #if !defined(OS_ANDROID) && !defined(OS_IOS)
72   if (base::FieldTrialList::FindFullName(kPSLMatchingDesktopFieldTrialName) ==
73       kPSLMatchingDesktopFieldTrialDisabledGroupName) {
74     enabled = false;
75   }
76 #endif
77   if (CommandLine::ForCurrentProcess()->HasSwitch(
78           switches::kDisablePasswordAutofillPublicSuffixDomainMatching)) {
79     enabled = false;
80   }
81   return enabled;
82 }