File: password_reuse_manager_factory.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (140 lines) | stat: -rw-r--r-- 5,609 bytes parent folder | download | duplicates (3)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/password_manager/password_reuse_manager_factory.h"

#include <memory>

#include "base/no_destructor.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/password_manager/account_password_store_factory.h"
#include "chrome/browser/password_manager/profile_password_store_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "components/password_manager/core/browser/features/password_features.h"
#include "components/password_manager/core/browser/password_reuse_detector_impl.h"
#include "components/password_manager/core/browser/password_reuse_manager_impl.h"
#include "components/password_manager/core/browser/password_reuse_manager_signin_notifier_impl.h"
#include "components/password_manager/core/browser/password_store/password_store_interface.h"
#include "components/password_manager/core/browser/shared_preferences_delegate.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/storage_partition.h"

#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/password_manager/android/shared_preferences_delegate_android.h"
#endif

namespace {

using password_manager::metrics_util::SignInState;

SignInState GetSignInStateForMetrics(Profile* profile) {
  auto* identity_manager =
      IdentityManagerFactory::GetForProfileIfExists(profile);
  if (!identity_manager) {
    return SignInState::kSignedOut;
  }

  // TODO(crbug.com/40066949): Simplify once kSync becomes unreachable or is
  // deleted from the codebase. See ConsentLevel::kSync documentation for
  // details.
  const std::string sync_username =
      identity_manager->GetPrimaryAccountInfo(signin::ConsentLevel::kSync)
          .email;
  if (!sync_username.empty()) {
    return SignInState::kSyncing;
  }

  const bool is_signed_in =
      !identity_manager->GetAccountsWithRefreshTokens().empty();
  return is_signed_in ? SignInState::kSignedInSyncDisabled
                      : SignInState::kSignedOut;
}

}  // namespace

PasswordReuseManagerFactory::PasswordReuseManagerFactory()
    : ProfileKeyedServiceFactory(
          "PasswordReuseManager",
          ProfileSelections::Builder()
              .WithRegular(ProfileSelection::kRedirectedToOriginal)
              // TODO(crbug.com/40257657): Check if this service is needed in
              // Guest mode.
              .WithGuest(ProfileSelection::kRedirectedToOriginal)
              // TODO(crbug.com/41488885): Check if this service is needed for
              // Ash Internals.
              .WithAshInternals(ProfileSelection::kRedirectedToOriginal)
              .Build()) {
  DependsOn(IdentityManagerFactory::GetInstance());
  DependsOn(ProfilePasswordStoreFactory::GetInstance());
  DependsOn(AccountPasswordStoreFactory::GetInstance());
}

PasswordReuseManagerFactory::~PasswordReuseManagerFactory() = default;

PasswordReuseManagerFactory* PasswordReuseManagerFactory::GetInstance() {
  static base::NoDestructor<PasswordReuseManagerFactory> instance;
  return instance.get();
}

password_manager::PasswordReuseManager*
PasswordReuseManagerFactory::GetForProfile(Profile* profile) {
  return static_cast<password_manager::PasswordReuseManager*>(
      GetInstance()->GetServiceForBrowserContext(profile, true));
}

std::unique_ptr<KeyedService>
PasswordReuseManagerFactory::BuildServiceInstanceForBrowserContext(
    content::BrowserContext* context) const {
  Profile* profile = Profile::FromBrowserContext(context);

  password_manager::PasswordStoreInterface* store =
      ProfilePasswordStoreFactory::GetForProfile(
          profile, ServiceAccessType::EXPLICIT_ACCESS)
          .get();
  // Incognito, guest, or system profiles doesn't have PasswordStore so
  // PasswordReuseManager shouldn't be created as well.
  if (!store) {
    return nullptr;
  }

  signin::IdentityManager* identity_manager =
      IdentityManagerFactory::GetForProfile(profile);
  std::unique_ptr<password_manager::SharedPreferencesDelegate>
      shared_pref_delegate;

#if BUILDFLAG(IS_ANDROID)
  if (base::FeatureList::IsEnabled(
          password_manager::features::kFetchGaiaHashOnSignIn)) {
    shared_pref_delegate = std::make_unique<SharedPreferencesDelegateAndroid>();
  }
#endif
  auto reuse_manager =
      std::make_unique<password_manager::PasswordReuseManagerImpl>();
  reuse_manager->Init(
      profile->GetPrefs(), g_browser_process->local_state(),
      ProfilePasswordStoreFactory::GetForProfile(
          profile, ServiceAccessType::EXPLICIT_ACCESS)
          .get(),
      AccountPasswordStoreFactory::GetForProfile(
          profile, ServiceAccessType::EXPLICIT_ACCESS)
          .get(),
      std::make_unique<password_manager::PasswordReuseDetectorImpl>(),
      identity_manager, std::move(shared_pref_delegate));

  // Prepare password hash data for reuse detection.
  reuse_manager->PreparePasswordHashData(GetSignInStateForMetrics(profile));

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
  std::unique_ptr<password_manager::PasswordReuseManagerSigninNotifier>
      notifier = std::make_unique<
          password_manager::PasswordReuseManagerSigninNotifierImpl>(
          IdentityManagerFactory::GetForProfile(profile));
  reuse_manager->SetPasswordReuseManagerSigninNotifier(std::move(notifier));
#endif

  return reuse_manager;
}