summaryrefslogtreecommitdiffstats
path: root/chrome/browser/password_manager/sync_metrics.cc
blob: 304bb6d92954ef2d4e4b11df73cdf10adb9550b4 (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
// Copyright 2014 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 "chrome/browser/password_manager/sync_metrics.h"

#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "components/signin/core/browser/signin_manager.h"
#include "google_apis/gaia/gaia_auth_util.h"
#include "google_apis/gaia/gaia_urls.h"

namespace password_manager_sync_metrics {

std::string GetSyncUsername(Profile* profile) {
  // If sync is set up, return early if we aren't syncing passwords.
  if (ProfileSyncServiceFactory::HasProfileSyncService(profile)) {
    ProfileSyncService* sync_service =
        ProfileSyncServiceFactory::GetForProfile(profile);
    if (!sync_service->GetPreferredDataTypes().Has(syncer::PASSWORDS))
      return std::string();
  }

  SigninManagerBase* signin_manager =
      SigninManagerFactory::GetForProfile(profile);

  if (!signin_manager)
    return std::string();

  return signin_manager->GetAuthenticatedUsername();
}

bool IsSyncAccountCredential(Profile* profile,
                             const std::string& username,
                             const std::string& origin) {
  if (origin != GaiaUrls::GetInstance()->gaia_url().GetOrigin().spec())
    return false;

  return gaia::AreEmailsSame(username, GetSyncUsername(profile));
}

}  // namespace password_manager_sync_metrics