• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "chrome/browser/password_manager/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 "chrome/common/chrome_switches.h"
11 #include "components/autofill/core/common/password_form.h"
12 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
13 #include "url/gurl.h"
14 
15 using autofill::PasswordForm;
16 
17 bool PSLMatchingHelper::psl_enabled_override_ = false;
18 
PSLMatchingHelper()19 PSLMatchingHelper::PSLMatchingHelper() : psl_enabled_(DeterminePSLEnabled()) {}
20 
~PSLMatchingHelper()21 PSLMatchingHelper::~PSLMatchingHelper() {}
22 
IsMatchingEnabled() const23 bool PSLMatchingHelper::IsMatchingEnabled() const {
24   return psl_enabled_override_ || psl_enabled_;
25 }
26 
ShouldPSLDomainMatchingApply(const std::string & registry_controlled_domain) const27 bool PSLMatchingHelper::ShouldPSLDomainMatchingApply(
28     const std::string& registry_controlled_domain) const {
29   return IsMatchingEnabled() && registry_controlled_domain != "google.com";
30 }
31 
32 // static
IsPublicSuffixDomainMatch(const std::string & url1,const std::string & url2)33 bool PSLMatchingHelper::IsPublicSuffixDomainMatch(const std::string& url1,
34                                                   const std::string& url2) {
35   GURL gurl1(url1);
36   GURL gurl2(url2);
37   return gurl1.scheme() == gurl2.scheme() &&
38          GetRegistryControlledDomain(gurl1) ==
39              GetRegistryControlledDomain(gurl2) &&
40          gurl1.port() == gurl2.port();
41 }
42 
43 // static
GetRegistryControlledDomain(const GURL & signon_realm)44 std::string PSLMatchingHelper::GetRegistryControlledDomain(
45     const GURL& signon_realm) {
46   return net::registry_controlled_domains::GetDomainAndRegistry(
47       signon_realm,
48       net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
49 }
50 
51 // static
EnablePublicSuffixDomainMatchingForTesting()52 void PSLMatchingHelper::EnablePublicSuffixDomainMatchingForTesting() {
53   psl_enabled_override_ = true;
54 }
55 
56 // static
DeterminePSLEnabled()57 bool PSLMatchingHelper::DeterminePSLEnabled() {
58 #if defined(OS_ANDROID)
59   // Default choice is "enabled", so we do not need to check for
60   // kEnablePasswordAutofillPublicSuffixDomainMatching.
61   bool enabled = true;
62   if (CommandLine::ForCurrentProcess()->HasSwitch(
63           switches::kDisablePasswordAutofillPublicSuffixDomainMatching)) {
64     enabled = false;
65   }
66   return enabled;
67 #else
68   return false;
69 #endif
70 }
71