File: credential_manager_dialog_controller_impl.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (217 lines) | stat: -rw-r--r-- 8,137 bytes parent folder | download | duplicates (5)
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright 2016 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/ui/passwords/credential_manager_dialog_controller_impl.h"

#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/sync_service_factory.h"
#include "chrome/browser/ui/passwords/password_dialog_prompts.h"
#include "chrome/browser/ui/passwords/passwords_model_delegate.h"
#include "chrome/browser/ui/passwords/ui_utils.h"
#include "chrome/grit/branded_strings.h"
#include "chrome/grit/generated_resources.h"
#include "components/password_manager/core/browser/password_bubble_experiment.h"
#include "components/password_manager/core/browser/password_feature_manager.h"
#include "components/password_manager/core/browser/password_form.h"
#include "components/password_manager/core/browser/password_manager_metrics_util.h"
#include "components/password_manager/core/browser/password_sync_util.h"
#include "components/password_manager/core/browser/password_ui_utils.h"
#include "components/password_manager/core/common/password_manager_pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/strings/grit/components_strings.h"
#include "components/sync/service/sync_service.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/l10n/l10n_util.h"

namespace {

#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_CHROMEOS)
std::u16string GetAuthenticationMessage(PasswordsModelDelegate* delegate) {
  std::u16string message;
  if (!delegate || !delegate->GetWebContents()) {
    return u"";
  }
  const std::u16string origin = base::UTF8ToUTF16(
      password_manager::GetShownOrigin(delegate->GetWebContents()
                                           ->GetPrimaryMainFrame()
                                           ->GetLastCommittedOrigin()));
  message =
      l10n_util::GetStringFUTF16(IDS_PASSWORD_MANAGER_FILLING_REAUTH, origin);
  return message;
}
#endif  // BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_CHROMEOS)

}  // namespace

CredentialManagerDialogControllerImpl::CredentialManagerDialogControllerImpl(
    Profile* profile,
    PasswordsModelDelegate* delegate)
    : profile_(profile),
      delegate_(delegate),
      account_chooser_dialog_(nullptr),
      autosignin_dialog_(nullptr) {}

CredentialManagerDialogControllerImpl::
    ~CredentialManagerDialogControllerImpl() {
  ResetDialog();
}

void CredentialManagerDialogControllerImpl::ShowAccountChooser(
    AccountChooserPrompt* dialog,
    std::vector<std::unique_ptr<password_manager::PasswordForm>> locals) {
  DCHECK(!account_chooser_dialog_);
  DCHECK(!autosignin_dialog_);
  DCHECK(dialog);
  local_credentials_.swap(locals);
  account_chooser_dialog_ = dialog;
  account_chooser_dialog_->ShowAccountChooser();
}

void CredentialManagerDialogControllerImpl::ShowAutosigninPrompt(
    AutoSigninFirstRunPrompt* dialog) {
  DCHECK(!account_chooser_dialog_);
  DCHECK(!autosignin_dialog_);
  DCHECK(dialog);
  autosignin_dialog_ = dialog;
  autosignin_dialog_->ShowAutoSigninPrompt();
}

bool CredentialManagerDialogControllerImpl::IsShowingAccountChooser() const {
  return !!account_chooser_dialog_;
}

const CredentialManagerDialogController::FormsVector&
CredentialManagerDialogControllerImpl::GetLocalForms() const {
  return local_credentials_;
}

std::u16string CredentialManagerDialogControllerImpl::GetAccountChooserTitle()
    const {
  return l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_ACCOUNT_CHOOSER_TITLE);
}

bool CredentialManagerDialogControllerImpl::ShouldShowSignInButton() const {
  return local_credentials_.size() == 1;
}

std::u16string CredentialManagerDialogControllerImpl::GetAutoSigninPromoTitle()
    const {
  int message_id = IsSyncingAutosignSetting(profile_)
                       ? IDS_AUTO_SIGNIN_FIRST_RUN_TITLE_MANY_DEVICES
                       : IDS_AUTO_SIGNIN_FIRST_RUN_TITLE_LOCAL_DEVICE;
  return l10n_util::GetStringUTF16(message_id);
}

std::u16string CredentialManagerDialogControllerImpl::GetAutoSigninText()
    const {
  return l10n_util::GetStringFUTF16(
      IDS_AUTO_SIGNIN_FIRST_RUN_TEXT,
      l10n_util::GetStringUTF16(IDS_PASSWORD_MANAGER_TITLE_BRAND));
}

bool CredentialManagerDialogControllerImpl::ShouldShowFooter() const {
  const syncer::SyncService* sync_service =
      SyncServiceFactory::GetForProfile(profile_);
  // TODO(crbug.com/40067296): Migrate away from `ConsentLevel::kSync` on
  // desktop platforms and remove #ifdef below.
#if BUILDFLAG(IS_ANDROID)
#error If this code is built on Android, please update TODO above.
#endif  // BUILDFLAG(IS_ANDROID)
  return password_manager::sync_util::IsSyncFeatureEnabledIncludingPasswords(
      sync_service);
}

void CredentialManagerDialogControllerImpl::OnChooseCredentials(
    const password_manager::PasswordForm& password_form,
    password_manager::CredentialType credential_type) {
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_CHROMEOS)
  if (delegate_->GetPasswordFeatureManager()
          ->IsBiometricAuthenticationBeforeFillingEnabled()) {
    delegate_->AuthenticateUserWithMessage(
        GetAuthenticationMessage(delegate_),
        base::BindOnce(
            &CredentialManagerDialogControllerImpl::OnBiometricReauthCompleted,
            weak_ptr_factory_.GetWeakPtr(), password_form, credential_type));
    return;
  }
#endif
  ResetDialog();
  delegate_->ChooseCredential(password_form, credential_type);
}

void CredentialManagerDialogControllerImpl::OnSignInClicked() {
  CHECK_EQ(1u, local_credentials_.size());
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_WIN) || BUILDFLAG(IS_CHROMEOS)
  if (delegate_->GetPasswordFeatureManager()
          ->IsBiometricAuthenticationBeforeFillingEnabled()) {
    delegate_->AuthenticateUserWithMessage(
        GetAuthenticationMessage(delegate_),
        base::BindOnce(
            &CredentialManagerDialogControllerImpl::OnBiometricReauthCompleted,
            weak_ptr_factory_.GetWeakPtr(), *local_credentials_[0],
            password_manager::CredentialType::CREDENTIAL_TYPE_PASSWORD));
    return;
  }
#endif
  ResetDialog();
  delegate_->ChooseCredential(
      *local_credentials_[0],
      password_manager::CredentialType::CREDENTIAL_TYPE_PASSWORD);
}

void CredentialManagerDialogControllerImpl::OnAutoSigninOK() {
  password_bubble_experiment::RecordAutoSignInPromptFirstRunExperienceWasShown(
      profile_->GetPrefs());
  password_manager::metrics_util::LogAutoSigninPromoUserAction(
      password_manager::metrics_util::AUTO_SIGNIN_OK_GOT_IT);
  ResetDialog();
  OnCloseDialog();
}

void CredentialManagerDialogControllerImpl::OnAutoSigninTurnOff() {
  profile_->GetPrefs()->SetBoolean(
      password_manager::prefs::kCredentialsEnableAutosignin, false);
  password_bubble_experiment::RecordAutoSignInPromptFirstRunExperienceWasShown(
      profile_->GetPrefs());
  password_manager::metrics_util::LogAutoSigninPromoUserAction(
      password_manager::metrics_util::AUTO_SIGNIN_TURN_OFF);
  ResetDialog();
  OnCloseDialog();
}

void CredentialManagerDialogControllerImpl::OnCloseDialog() {
  if (account_chooser_dialog_) {
    account_chooser_dialog_ = nullptr;
  }
  if (autosignin_dialog_) {
    password_manager::metrics_util::LogAutoSigninPromoUserAction(
        password_manager::metrics_util::AUTO_SIGNIN_NO_ACTION);
    autosignin_dialog_ = nullptr;
  }
  delegate_->OnDialogHidden();
}

void CredentialManagerDialogControllerImpl::ResetDialog() {
  if (account_chooser_dialog_) {
    account_chooser_dialog_->ControllerGone();
    account_chooser_dialog_ = nullptr;
  }
  if (autosignin_dialog_) {
    autosignin_dialog_->ControllerGone();
    autosignin_dialog_ = nullptr;
  }
}

void CredentialManagerDialogControllerImpl::OnBiometricReauthCompleted(
    password_manager::PasswordForm password_form,
    password_manager::CredentialType credential_type,
    bool result) {
  if (!result) {
    return;
  }
  ResetDialog();
  delegate_->ChooseCredential(password_form, credential_type);
}