1116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Copyright 2014 The Chromium Authors. All rights reserved.
2116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// found in the LICENSE file.
4116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
5116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/password_manager/sync_metrics.h"
6116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
7116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/signin/signin_manager_factory.h"
8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/sync/profile_sync_service.h"
9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/sync/profile_sync_service_factory.h"
10116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/signin/core/browser/signin_manager.h"
11116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "google_apis/gaia/gaia_auth_util.h"
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "google_apis/gaia/gaia_urls.h"
13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
14116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace password_manager_sync_metrics {
15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdochstd::string GetSyncUsername(Profile* profile) {
176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // If sync is set up, return early if we aren't syncing passwords.
186e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (ProfileSyncServiceFactory::HasProfileSyncService(profile)) {
196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    ProfileSyncService* sync_service =
206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        ProfileSyncServiceFactory::GetForProfile(profile);
216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    if (!sync_service->GetPreferredDataTypes().Has(syncer::PASSWORDS))
226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      return std::string();
236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
25116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  SigninManagerBase* signin_manager =
26116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      SigninManagerFactory::GetForProfile(profile);
276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
28116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (!signin_manager)
296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return std::string();
30116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return signin_manager->GetAuthenticatedUsername();
32116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
33116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
34116680a4aac90f2aa7413d9095a592090648e557Ben Murdochbool IsSyncAccountCredential(Profile* profile,
35116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                             const std::string& username,
36116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                             const std::string& origin) {
37116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (origin != GaiaUrls::GetInstance()->gaia_url().GetOrigin().spec())
38116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    return false;
39116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
40116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return gaia::AreEmailsSame(username, GetSyncUsername(profile));
41116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
42116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace password_manager_sync_metrics
44