summaryrefslogtreecommitdiff
path: root/chromium/components/password_manager/core/browser/credential_manager_pending_request_task.cc
blob: cd89fb0e8944e82d0672935d01da590f1cdb4e90 (plain)
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/password_manager/core/browser/credential_manager_pending_request_task.h"

#include <algorithm>
#include <iterator>
#include <map>
#include <utility>

#include "base/memory/ptr_util.h"
#include "base/metrics/user_metrics.h"
#include "base/stl_util.h"
#include "components/autofill/core/common/password_form.h"
#include "components/password_manager/core/browser/affiliated_match_helper.h"
#include "components/password_manager/core/browser/password_bubble_experiment.h"
#include "components/password_manager/core/browser/password_manager_client.h"
#include "components/password_manager/core/browser/password_manager_metrics_util.h"
#include "components/password_manager/core/browser/password_manager_util.h"
#include "components/password_manager/core/common/credential_manager_types.h"
#include "url/gurl.h"
#include "url/origin.h"

namespace password_manager {
namespace {

// Send a UMA histogram about if |local_results| has empty or duplicate
// usernames.
void ReportAccountChooserUsabilityMetrics(
    const std::vector<std::unique_ptr<autofill::PasswordForm>>& forms,
    bool had_duplicates,
    bool had_empty_username) {
  metrics_util::AccountChooserUsabilityMetric metric;
  if (had_empty_username && had_duplicates)
    metric = metrics_util::ACCOUNT_CHOOSER_EMPTY_USERNAME_AND_DUPLICATES;
  else if (had_empty_username)
    metric = metrics_util::ACCOUNT_CHOOSER_EMPTY_USERNAME;
  else if (had_duplicates)
    metric = metrics_util::ACCOUNT_CHOOSER_DUPLICATES;
  else
    metric = metrics_util::ACCOUNT_CHOOSER_LOOKS_OK;

  int count_empty_icons =
      std::count_if(forms.begin(), forms.end(),
                    [](const std::unique_ptr<autofill::PasswordForm>& form) {
                      return !form->icon_url.is_valid();
                    });
  metrics_util::LogAccountChooserUsability(metric, count_empty_icons,
                                           forms.size());
}

// Returns true iff |form1| is better suitable for showing in the account
// chooser than |form2|. Inspired by PasswordFormManager::ScoreResult.
bool IsBetterMatch(const autofill::PasswordForm& form1,
                   const autofill::PasswordForm& form2) {
  if (!form1.is_public_suffix_match && form2.is_public_suffix_match)
    return true;
  if (form1.preferred && !form2.preferred)
    return true;
  return form1.date_created > form2.date_created;
}

// Remove duplicates in |forms| before displaying them in the account chooser.
void FilterDuplicates(
    std::vector<std::unique_ptr<autofill::PasswordForm>>* forms) {
  std::vector<std::unique_ptr<autofill::PasswordForm>> federated_forms;
  // The key is [username, signon_realm]. signon_realm is used only for PSL
  // matches because those entries have it in the UI.
  std::map<std::pair<base::string16, std::string>,
           std::unique_ptr<autofill::PasswordForm>>
      credentials;
  for (auto& form : *forms) {
    if (!form->federation_origin.unique()) {
      federated_forms.push_back(std::move(form));
    } else {
      auto key = std::make_pair(
          form->username_value,
          form->is_public_suffix_match ? form->signon_realm : std::string());
      auto it = credentials.find(key);
      if (it == credentials.end() || IsBetterMatch(*form, *it->second))
        credentials[key] = std::move(form);
    }
  }
  forms->clear();
  for (auto& form_pair : credentials)
    forms->push_back(std::move(form_pair.second));
  std::move(federated_forms.begin(), federated_forms.end(),
            std::back_inserter(*forms));
}

// Sift |forms| for the account chooser so it doesn't have empty usernames or
// duplicates.
void FilterDuplicatesAndEmptyUsername(
    std::vector<std::unique_ptr<autofill::PasswordForm>>* forms,
    bool* has_empty_username,
    bool* has_duplicates) {
  // Remove empty usernames from the list.
  size_t size_before = forms->size();
  base::EraseIf(*forms,
                [](const std::unique_ptr<autofill::PasswordForm>& form) {
                  return form->username_value.empty();
                });
  *has_empty_username = (size_before != forms->size());

  size_before = forms->size();
  FilterDuplicates(forms);
  *has_duplicates = (size_before != forms->size());
}

}  // namespace

CredentialManagerPendingRequestTask::CredentialManagerPendingRequestTask(
    CredentialManagerPendingRequestTaskDelegate* delegate,
    const SendCredentialCallback& callback,
    bool request_zero_click_only,
    bool include_passwords,
    const std::vector<GURL>& request_federations)
    : delegate_(delegate),
      send_callback_(callback),
      zero_click_only_(request_zero_click_only),
      origin_(delegate_->GetOrigin()),
      include_passwords_(include_passwords) {
  CHECK(!delegate_->client()->DidLastPageLoadEncounterSSLErrors());
  for (const GURL& federation : request_federations)
    federations_.insert(url::Origin(federation.GetOrigin()).Serialize());
}

CredentialManagerPendingRequestTask::~CredentialManagerPendingRequestTask() =
    default;

void CredentialManagerPendingRequestTask::OnGetPasswordStoreResults(
    std::vector<std::unique_ptr<autofill::PasswordForm>> results) {
  // localhost is a secure origin but not https.
  if (results.empty() && origin_.SchemeIs(url::kHttpsScheme)) {
    // Try to migrate the HTTP passwords and process them later.
    http_migrator_ = base::MakeUnique<HttpPasswordStoreMigrator>(
        origin_, delegate_->client(), this);
    return;
  }
  ProcessForms(std::move(results));
}

void CredentialManagerPendingRequestTask::ProcessMigratedForms(
    std::vector<std::unique_ptr<autofill::PasswordForm>> forms) {
  ProcessForms(std::move(forms));
}

void CredentialManagerPendingRequestTask::ProcessForms(
    std::vector<std::unique_ptr<autofill::PasswordForm>> results) {
  using metrics_util::LogCredentialManagerGetResult;
  metrics_util::CredentialManagerGetMediation mediation_status =
      zero_click_only_ ? metrics_util::CREDENTIAL_MANAGER_GET_UNMEDIATED
                       : metrics_util::CREDENTIAL_MANAGER_GET_MEDIATED;
  if (delegate_->GetOrigin() != origin_) {
    LogCredentialManagerGetResult(metrics_util::CREDENTIAL_MANAGER_GET_NONE,
                                  mediation_status);
    delegate_->SendCredential(send_callback_, CredentialInfo());
    return;
  }

  std::vector<std::unique_ptr<autofill::PasswordForm>> local_results;
  std::vector<std::unique_ptr<autofill::PasswordForm>> psl_results;
  for (auto& form : results) {
    // Ensure that the form we're looking at matches the password and
    // federation filters provided.
    if (!((form->federation_origin.unique() && include_passwords_) ||
          (!form->federation_origin.unique() &&
           federations_.count(form->federation_origin.Serialize())))) {
      continue;
    }

    // PasswordFrom and GURL have different definition of origin.
    // PasswordForm definition: scheme, host, port and path.
    // GURL definition: scheme, host, and port.
    // So we can't compare them directly.
    if (form->is_affiliation_based_match ||
        form->origin.GetOrigin() == origin_.GetOrigin()) {
      local_results.push_back(std::move(form));
    } else if (form->is_public_suffix_match) {
      psl_results.push_back(std::move(form));
    }
  }

  bool has_empty_username = false;
  bool has_duplicates = false;
  FilterDuplicatesAndEmptyUsername(&local_results, &has_empty_username,
                                   &has_duplicates);

  // We only perform zero-click sign-in when the result is completely
  // unambigious. If there is one and only one entry, and zero-click is
  // enabled for that entry, return it.
  //
  // Moreover, we only return such a credential if the user has opted-in via the
  // first-run experience.
  const bool can_use_autosignin =
      local_results.size() == 1u && delegate_->IsZeroClickAllowed();
  if (can_use_autosignin && !local_results[0]->skip_zero_click &&
      !password_bubble_experiment::ShouldShowAutoSignInPromptFirstRunExperience(
          delegate_->client()->GetPrefs())) {
    CredentialInfo info(*local_results[0],
                        local_results[0]->federation_origin.unique()
                            ? CredentialType::CREDENTIAL_TYPE_PASSWORD
                            : CredentialType::CREDENTIAL_TYPE_FEDERATED);
    delegate_->client()->NotifyUserAutoSignin(std::move(local_results),
                                              origin_);
    base::RecordAction(base::UserMetricsAction("CredentialManager_Autosignin"));
    LogCredentialManagerGetResult(
        metrics_util::CREDENTIAL_MANAGER_GET_AUTOSIGNIN, mediation_status);
    delegate_->SendCredential(send_callback_, info);
    return;
  }

  if (zero_click_only_) {
    metrics_util::CredentialManagerGetResult get_result;
    if (local_results.empty())
      get_result = metrics_util::CREDENTIAL_MANAGER_GET_NONE_EMPTY_STORE;
    else if (!can_use_autosignin)
      get_result = metrics_util::CREDENTIAL_MANAGER_GET_NONE_MANY_CREDENTIALS;
    else if (local_results[0]->skip_zero_click)
      get_result = metrics_util::CREDENTIAL_MANAGER_GET_NONE_SIGNED_OUT;
    else
      get_result = metrics_util::CREDENTIAL_MANAGER_GET_NONE_FIRST_RUN;

    if (can_use_autosignin) {
      // The user had credentials, but either chose not to share them with the
      // site, or was prevented from doing so by lack of zero-click (or the
      // first-run experience). So, notify the client that we could potentially
      // have used zero-click.
      delegate_->client()->NotifyUserCouldBeAutoSignedIn(
          std::move(local_results[0]));
    }

    LogCredentialManagerGetResult(get_result, mediation_status);
    delegate_->SendCredential(send_callback_, CredentialInfo());
    return;
  }

  // Time to show the account chooser. If |local_results| is empty then it
  // should list the PSL matches.
  if (local_results.empty()) {
    local_results = std::move(psl_results);
    FilterDuplicatesAndEmptyUsername(&local_results, &has_empty_username,
                                     &has_duplicates);
  }

  if (local_results.empty()) {
    LogCredentialManagerGetResult(
        metrics_util::CREDENTIAL_MANAGER_GET_NONE_EMPTY_STORE,
        mediation_status);
    delegate_->SendCredential(send_callback_, CredentialInfo());
    return;
  }

  ReportAccountChooserUsabilityMetrics(local_results, has_duplicates,
                                       has_empty_username);
  if (!delegate_->client()->PromptUserToChooseCredentials(
          std::move(local_results), origin_,
          base::Bind(
              &CredentialManagerPendingRequestTaskDelegate::SendPasswordForm,
              base::Unretained(delegate_), send_callback_))) {
    LogCredentialManagerGetResult(metrics_util::CREDENTIAL_MANAGER_GET_NONE,
                                  mediation_status);
    delegate_->SendCredential(send_callback_, CredentialInfo());
  }
}

}  // namespace password_manager