Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / chromeos / app_mode / kiosk_profile_loader.cc
blob62ca6334d3fb948a1f91934ab548e1f67fbbd50a
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;
27 namespace chromeos {
29 namespace {
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;
41 default:
42 NOTREACHED();
43 return KioskAppLaunchError::UNABLE_TO_MOUNT;
47 } // namespace
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> {
58 public:
59 explicit CryptohomedChecker(KioskProfileLoader* loader)
60 : loader_(loader),
61 retry_count_(0) {
63 ~CryptohomedChecker() {}
65 void StartCheck() {
66 chromeos::DBusThreadManager::Get()->GetCryptohomeClient()->IsMounted(
67 base::Bind(&CryptohomedChecker::OnCryptohomeIsMounted,
68 AsWeakPtr()));
71 private:
72 void OnCryptohomeIsMounted(chromeos::DBusMethodCallStatus call_status,
73 bool is_mounted) {
74 if (call_status != chromeos::DBUS_METHOD_CALL_SUCCESS) {
75 const int kMaxRetryTimes = 5;
76 ++retry_count_;
77 if (retry_count_ > kMaxRetryTimes) {
78 LOG(ERROR) << "Could not talk to cryptohomed for launching kiosk app.";
79 ReportCheckResult(KioskAppLaunchError::CRYPTOHOMED_NOT_RUNNING);
80 return;
83 const int retry_delay_in_milliseconds = 500 * (1 << retry_count_);
84 base::MessageLoop::current()->PostDelayedTask(
85 FROM_HERE,
86 base::Bind(&CryptohomedChecker::StartCheck, AsWeakPtr()),
87 base::TimeDelta::FromMilliseconds(retry_delay_in_milliseconds));
88 return;
91 if (is_mounted)
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);
97 else
98 ReportCheckResult(KioskAppLaunchError::ALREADY_MOUNTED);
101 void ReportCheckResult(KioskAppLaunchError::Error error) {
102 if (error == KioskAppLaunchError::NONE)
103 loader_->LoginAsKioskAccount();
104 else
105 loader_->ReportLaunchResult(error);
108 KioskProfileLoader* loader_;
109 int retry_count_;
111 DISALLOW_COPY_AND_ASSIGN(CryptohomedChecker);
115 ////////////////////////////////////////////////////////////////////////////////
116 // KioskProfileLoader
118 KioskProfileLoader::KioskProfileLoader(KioskAppManager* kiosk_app_manager,
119 const std::string& app_id,
120 Delegate* delegate)
121 : kiosk_app_manager_(kiosk_app_manager),
122 app_id_(app_id),
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
160 this);
163 void KioskProfileLoader::OnLoginFailure(const LoginFailure& error) {
164 ReportLaunchResult(LoginFailureToKioskAppLaunchError(error));
167 void KioskProfileLoader::WhiteListCheckFailed(const std::string& email) {
168 NOTREACHED();
171 void KioskProfileLoader::PolicyLoadFailed() {
172 ReportLaunchResult(KioskAppLaunchError::POLICY_LOAD_FAILED);
175 void KioskProfileLoader::OnOnlineChecked(
176 const std::string& email, bool success) {
177 NOTREACHED();
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