1 // Copyright 2013 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/app_mode/kiosk_profile_loader.h"
7 #include "base/logging.h"
8 #include "base/memory/weak_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string_util.h"
11 #include "base/sys_info.h"
12 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
13 #include "chrome/browser/chromeos/login/login_display_host_impl.h"
14 #include "chrome/browser/chromeos/login/login_status_consumer.h"
15 #include "chrome/browser/chromeos/login/login_utils.h"
16 #include "chrome/browser/chromeos/login/user_manager.h"
17 #include "chrome/browser/chromeos/settings/cros_settings.h"
18 #include "chrome/browser/lifetime/application_lifetime.h"
19 #include "chromeos/cryptohome/async_method_caller.h"
20 #include "chromeos/dbus/cryptohome_client.h"
21 #include "chromeos/dbus/dbus_thread_manager.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "google_apis/gaia/gaia_auth_util.h"
25 using content::BrowserThread
;
31 KioskAppLaunchError::Error
LoginFailureToKioskAppLaunchError(
32 const LoginFailure
& error
) {
33 switch (error
.reason()) {
34 case LoginFailure::COULD_NOT_MOUNT_TMPFS
:
35 case LoginFailure::COULD_NOT_MOUNT_CRYPTOHOME
:
36 return KioskAppLaunchError::UNABLE_TO_MOUNT
;
37 case LoginFailure::DATA_REMOVAL_FAILED
:
38 return KioskAppLaunchError::UNABLE_TO_REMOVE
;
39 case LoginFailure::USERNAME_HASH_FAILED
:
40 return KioskAppLaunchError::UNABLE_TO_RETRIEVE_HASH
;
43 return KioskAppLaunchError::UNABLE_TO_MOUNT
;
49 ////////////////////////////////////////////////////////////////////////////////
50 // KioskProfileLoader::CryptohomedChecker ensures cryptohome daemon is up
51 // and running by issuing an IsMounted call. If the call does not go through
52 // and chromeos::DBUS_METHOD_CALL_SUCCESS is not returned, it will retry after
53 // some time out and at the maximum five times before it gives up. Upon
54 // success, it resumes the launch by logging in as a kiosk mode account.
56 class KioskProfileLoader::CryptohomedChecker
57 : public base::SupportsWeakPtr
<CryptohomedChecker
> {
59 explicit CryptohomedChecker(KioskProfileLoader
* loader
)
63 ~CryptohomedChecker() {}
66 chromeos::DBusThreadManager::Get()->GetCryptohomeClient()->IsMounted(
67 base::Bind(&CryptohomedChecker::OnCryptohomeIsMounted
,
72 void OnCryptohomeIsMounted(chromeos::DBusMethodCallStatus call_status
,
74 if (call_status
!= chromeos::DBUS_METHOD_CALL_SUCCESS
) {
75 const int kMaxRetryTimes
= 5;
77 if (retry_count_
> kMaxRetryTimes
) {
78 LOG(ERROR
) << "Could not talk to cryptohomed for launching kiosk app.";
79 ReportCheckResult(KioskAppLaunchError::CRYPTOHOMED_NOT_RUNNING
);
83 const int retry_delay_in_milliseconds
= 500 * (1 << retry_count_
);
84 base::MessageLoop::current()->PostDelayedTask(
86 base::Bind(&CryptohomedChecker::StartCheck
, AsWeakPtr()),
87 base::TimeDelta::FromMilliseconds(retry_delay_in_milliseconds
));
92 LOG(ERROR
) << "Cryptohome is mounted before launching kiosk app.";
94 // Proceed only when cryptohome is not mounded or running on dev box.
95 if (!is_mounted
|| !base::SysInfo::IsRunningOnChromeOS())
96 ReportCheckResult(KioskAppLaunchError::NONE
);
98 ReportCheckResult(KioskAppLaunchError::ALREADY_MOUNTED
);
101 void ReportCheckResult(KioskAppLaunchError::Error error
) {
102 if (error
== KioskAppLaunchError::NONE
)
103 loader_
->LoginAsKioskAccount();
105 loader_
->ReportLaunchResult(error
);
108 KioskProfileLoader
* loader_
;
111 DISALLOW_COPY_AND_ASSIGN(CryptohomedChecker
);
115 ////////////////////////////////////////////////////////////////////////////////
116 // KioskProfileLoader
118 KioskProfileLoader::KioskProfileLoader(KioskAppManager
* kiosk_app_manager
,
119 const std::string
& app_id
,
121 : kiosk_app_manager_(kiosk_app_manager
),
123 delegate_(delegate
) {
124 KioskAppManager::App app
;
125 CHECK(kiosk_app_manager_
->GetApp(app_id_
, &app
));
126 user_id_
= app
.user_id
;
129 KioskProfileLoader::~KioskProfileLoader() {}
131 void KioskProfileLoader::Start() {
132 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
133 login_performer_
.reset();
134 cryptohomed_checker_
.reset(new CryptohomedChecker(this));
135 cryptohomed_checker_
->StartCheck();
138 void KioskProfileLoader::LoginAsKioskAccount() {
139 login_performer_
.reset(new LoginPerformer(this));
140 login_performer_
->LoginAsKioskAccount(user_id_
);
143 void KioskProfileLoader::ReportLaunchResult(KioskAppLaunchError::Error error
) {
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
146 if (error
!= KioskAppLaunchError::NONE
) {
147 delegate_
->OnProfileLoadFailed(error
);
151 void KioskProfileLoader::OnLoginSuccess(const UserContext
& user_context
) {
152 // LoginPerformer will delete itself.
153 login_performer_
->set_delegate(NULL
);
154 ignore_result(login_performer_
.release());
156 LoginUtils::Get()->PrepareProfile(user_context
,
157 std::string(), // display email
158 false, // has_cookies
159 false, // has_active_session
163 void KioskProfileLoader::OnLoginFailure(const LoginFailure
& error
) {
164 ReportLaunchResult(LoginFailureToKioskAppLaunchError(error
));
167 void KioskProfileLoader::WhiteListCheckFailed(const std::string
& email
) {
171 void KioskProfileLoader::PolicyLoadFailed() {
172 ReportLaunchResult(KioskAppLaunchError::POLICY_LOAD_FAILED
);
175 void KioskProfileLoader::OnOnlineChecked(
176 const std::string
& email
, bool success
) {
180 void KioskProfileLoader::OnProfilePrepared(Profile
* profile
) {
181 // This object could be deleted any time after successfully reporting
182 // a profile load, so invalidate the LoginUtils delegate now.
183 LoginUtils::Get()->DelegateDeleted(this);
185 delegate_
->OnProfileLoaded(profile
);
186 ReportLaunchResult(KioskAppLaunchError::NONE
);
189 } // namespace chromeos