Elim cr-checkbox
[chromium-blink-merge.git] / chrome / browser / chromeos / login / easy_unlock / bootstrap_user_flow.cc
blob64a6b33af1d011e248fd3398b986aee680ee0a1f
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/chromeos/login/easy_unlock/bootstrap_user_flow.h"
7 #include "base/bind.h"
8 #include "base/sys_info.h"
9 #include "chrome/browser/chromeos/login/easy_unlock/bootstrap_manager.h"
10 #include "chrome/browser/chromeos/login/easy_unlock/easy_unlock_key_manager.h"
11 #include "chrome/browser/chromeos/login/session/user_session_manager.h"
12 #include "chrome/browser/chromeos/login/users/chrome_user_manager.h"
13 #include "chrome/browser/lifetime/application_lifetime.h"
14 #include "chrome/browser/signin/easy_unlock_service.h"
15 #include "chrome/browser/signin/easy_unlock_service_regular.h"
16 #include "components/proximity_auth/screenlock_bridge.h"
18 namespace chromeos {
20 BootstrapUserFlow::BootstrapUserFlow(const UserContext& user_context,
21 bool is_new_account)
22 : ExtendedUserFlow(user_context.GetUserID()),
23 user_context_(user_context),
24 is_new_account_(is_new_account),
25 finished_(false),
26 user_profile_(nullptr),
27 weak_ptr_factory_(this) {
28 ChromeUserManager::Get()->GetBootstrapManager()->AddPendingBootstrap(
29 user_context_.GetUserID());
32 BootstrapUserFlow::~BootstrapUserFlow() {
35 void BootstrapUserFlow::StartAutoPairing() {
36 DCHECK(user_profile_);
38 VLOG(2) << "BootstrapUserFlow StartAutoPairing";
40 EasyUnlockService* service = EasyUnlockService::Get(user_profile_);
41 if (!service->IsAllowed()) {
42 // TODO(xiyuan): Show error UI.
43 LOG(ERROR) << "Bootstrapped failed because EasyUnlock service not allowed.";
44 chrome::AttemptUserExit();
45 return;
48 service->StartAutoPairing(base::Bind(&BootstrapUserFlow::SetAutoPairingResult,
49 weak_ptr_factory_.GetWeakPtr()));
52 void BootstrapUserFlow::SetAutoPairingResult(bool success,
53 const std::string& error_message) {
54 VLOG(2) << "BootstrapUserFlow::SetAutoPairingResult"
55 << ", success=" << success;
57 if (!success) {
58 // TODO(xiyuan): Show error UI.
59 LOG(ERROR) << "Easy bootstrap auto pairing failed, err=" << error_message;
60 chrome::AttemptUserExit();
61 return;
64 // TODO(xiyuan): Remove this and properly mock EasyUnlockKeyManager.
65 if (!base::SysInfo::IsRunningOnChromeOS()) {
66 Finish();
67 return;
70 chromeos::UserSessionManager::GetInstance()
71 ->GetEasyUnlockKeyManager()
72 ->RefreshKeys(
73 user_context_,
74 *EasyUnlockService::Get(user_profile_)->GetRemoteDevices(),
75 base::Bind(&BootstrapUserFlow::OnKeysRefreshed,
76 weak_ptr_factory_.GetWeakPtr()));
79 void BootstrapUserFlow::OnKeysRefreshed(bool success) {
80 if (!success) {
81 // TODO(xiyuan): Show error UI.
82 LOG(ERROR) << "Easy bootstrap failed to refresh cryptohome keys.";
83 chrome::AttemptUserExit();
84 return;
87 EasyUnlockService::Get(user_profile_)
88 ->SetHardlockState(EasyUnlockScreenlockStateHandler::NO_HARDLOCK);
89 RemoveBootstrapRandomKey();
92 void BootstrapUserFlow::RemoveBootstrapRandomKey() {
93 scoped_refptr<ExtendedAuthenticator> authenticator(
94 ExtendedAuthenticator::Create(this));
96 const char kLegacyKeyLabel[] = "legacy-0";
97 authenticator->RemoveKey(
98 user_context_,
99 kLegacyKeyLabel,
100 base::Bind(&BootstrapUserFlow::OnBootstrapRandomKeyRemoved,
101 weak_ptr_factory_.GetWeakPtr()));
104 void BootstrapUserFlow::OnBootstrapRandomKeyRemoved() {
105 Finish();
108 void BootstrapUserFlow::Finish() {
109 VLOG(2) << "BootstrapUserFlow::Finish";
110 finished_ = true;
112 ChromeUserManager::Get()->GetBootstrapManager()->FinishPendingBootstrap(
113 user_context_.GetUserID());
114 UserSessionManager::GetInstance()->DoBrowserLaunch(user_profile_, host());
116 user_profile_ = nullptr;
117 UnregisterFlowSoon();
120 bool BootstrapUserFlow::CanLockScreen() {
121 return false;
124 bool BootstrapUserFlow::ShouldLaunchBrowser() {
125 return finished_;
128 bool BootstrapUserFlow::ShouldSkipPostLoginScreens() {
129 return true;
132 bool BootstrapUserFlow::HandleLoginFailure(
133 const chromeos::AuthFailure& failure) {
134 NOTREACHED();
135 return false;
138 void BootstrapUserFlow::HandleLoginSuccess(
139 const chromeos::UserContext& context) {
142 bool BootstrapUserFlow::HandlePasswordChangeDetected() {
143 NOTREACHED();
144 return false;
147 void BootstrapUserFlow::HandleOAuthTokenStatusChange(
148 user_manager::User::OAuthTokenStatus status) {
149 if (status == user_manager::User::OAUTH2_TOKEN_STATUS_INVALID) {
150 // TODO(xiyuan): Show error UI.
151 LOG(ERROR) << "Bootstrapped failed because of bad refresh token.";
152 chrome::AttemptUserExit();
153 return;
157 void BootstrapUserFlow::LaunchExtraSteps(Profile* user_profile) {
158 user_profile_ = user_profile;
160 // Auto pairing only when a random key is used to boostrap cryptohome.
161 if (is_new_account_) {
162 StartAutoPairing();
163 return;
166 Finish();
169 bool BootstrapUserFlow::SupportsEarlyRestartToApplyFlags() {
170 return false;
173 void BootstrapUserFlow::OnAuthenticationFailure(
174 ExtendedAuthenticator::AuthState state) {
175 // TODO(xiyuan): Show error UI.
176 LOG(ERROR) << "Bootstrapped failed because authenticator falure"
177 << ", state=" << state;
178 chrome::AttemptUserExit();
181 } // namespace chromeos