Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / signin / chrome_proximity_auth_client.cc
blobf8e13d69073ab3dd91b9e72fcaf3373d54c604db
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/signin/chrome_proximity_auth_client.h"
7 #include "base/logging.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/sys_info.h"
10 #include "base/version.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/profiles/profile_window.h"
13 #include "chrome/browser/signin/easy_unlock_service.h"
14 #include "chrome/browser/signin/easy_unlock_service_regular.h"
15 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
16 #include "chrome/browser/signin/signin_manager_factory.h"
17 #include "components/proximity_auth/cryptauth/cryptauth_client_impl.h"
18 #include "components/proximity_auth/cryptauth/cryptauth_device_manager.h"
19 #include "components/proximity_auth/cryptauth/cryptauth_enrollment_manager.h"
20 #include "components/proximity_auth/cryptauth/secure_message_delegate.h"
21 #include "components/signin/core/browser/profile_oauth2_token_service.h"
22 #include "components/signin/core/browser/signin_manager_base.h"
23 #include "components/version_info/version_info.h"
25 #if defined(OS_CHROMEOS)
26 #include "chrome/browser/chromeos/login/easy_unlock/secure_message_delegate_chromeos.h"
27 #endif
29 using proximity_auth::ScreenlockState;
31 ChromeProximityAuthClient::ChromeProximityAuthClient(Profile* profile)
32 : profile_(profile) {
35 ChromeProximityAuthClient::~ChromeProximityAuthClient() {
38 std::string ChromeProximityAuthClient::GetAuthenticatedUsername() const {
39 const SigninManagerBase* signin_manager =
40 SigninManagerFactory::GetForProfileIfExists(profile_);
41 // |profile_| has to be a signed-in profile with SigninManager already
42 // created. Otherwise, just crash to collect stack.
43 DCHECK(signin_manager);
44 return signin_manager->GetAuthenticatedAccountInfo().email;
47 void ChromeProximityAuthClient::UpdateScreenlockState(ScreenlockState state) {
48 EasyUnlockService* service = EasyUnlockService::Get(profile_);
49 if (service)
50 service->UpdateScreenlockState(state);
53 void ChromeProximityAuthClient::FinalizeUnlock(bool success) {
54 EasyUnlockService* service = EasyUnlockService::Get(profile_);
55 if (service)
56 service->FinalizeUnlock(success);
59 void ChromeProximityAuthClient::FinalizeSignin(const std::string& secret) {
60 EasyUnlockService* service = EasyUnlockService::Get(profile_);
61 if (service)
62 service->FinalizeSignin(secret);
65 PrefService* ChromeProximityAuthClient::GetPrefService() {
66 return profile_->GetPrefs();
69 scoped_ptr<proximity_auth::SecureMessageDelegate>
70 ChromeProximityAuthClient::CreateSecureMessageDelegate() {
71 #if defined(OS_CHROMEOS)
72 return make_scoped_ptr(new chromeos::SecureMessageDelegateChromeOS());
73 #else
74 return nullptr;
75 #endif
78 scoped_ptr<proximity_auth::CryptAuthClientFactory>
79 ChromeProximityAuthClient::CreateCryptAuthClientFactory() {
80 return make_scoped_ptr(new proximity_auth::CryptAuthClientFactoryImpl(
81 ProfileOAuth2TokenServiceFactory::GetForProfile(profile_), GetAccountId(),
82 profile_->GetRequestContext(), GetDeviceClassifier()));
85 cryptauth::DeviceClassifier ChromeProximityAuthClient::GetDeviceClassifier() {
86 cryptauth::DeviceClassifier device_classifier;
88 #if defined(OS_CHROMEOS)
89 int32 major_version, minor_version, bugfix_version;
90 // TODO(tengs): base::OperatingSystemVersionNumbers only works for ChromeOS.
91 // We need to get different numbers for other platforms.
92 base::SysInfo::OperatingSystemVersionNumbers(&major_version, &minor_version,
93 &bugfix_version);
94 device_classifier.set_device_os_version_code(major_version);
95 device_classifier.set_device_type(cryptauth::CHROME);
96 #endif
98 const std::vector<uint32_t>& version_components =
99 base::Version(version_info::GetVersionNumber()).components();
100 if (version_components.size() > 0)
101 device_classifier.set_device_software_version_code(version_components[0]);
103 device_classifier.set_device_software_package(version_info::GetProductName());
104 return device_classifier;
107 std::string ChromeProximityAuthClient::GetAccountId() {
108 return SigninManagerFactory::GetForProfile(profile_)
109 ->GetAuthenticatedAccountId();
112 proximity_auth::CryptAuthEnrollmentManager*
113 ChromeProximityAuthClient::GetCryptAuthEnrollmentManager() {
114 EasyUnlockServiceRegular* easy_unlock_service = GetEasyUnlockServiceRegular();
115 if (!easy_unlock_service)
116 return nullptr;
117 return easy_unlock_service->GetCryptAuthEnrollmentManager();
120 proximity_auth::CryptAuthDeviceManager*
121 ChromeProximityAuthClient::GetCryptAuthDeviceManager() {
122 EasyUnlockServiceRegular* easy_unlock_service = GetEasyUnlockServiceRegular();
123 if (!easy_unlock_service)
124 return nullptr;
125 return easy_unlock_service->GetCryptAuthDeviceManager();
128 EasyUnlockServiceRegular*
129 ChromeProximityAuthClient::GetEasyUnlockServiceRegular() {
130 EasyUnlockService* easy_unlock_service = EasyUnlockService::Get(profile_);
131 if (easy_unlock_service->GetType() == EasyUnlockService::TYPE_REGULAR)
132 return static_cast<EasyUnlockServiceRegular*>(easy_unlock_service);
133 else
134 return nullptr;