• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/chromeos/app_mode/kiosk_profile_loader.h"
6 
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"
24 
25 using content::BrowserThread;
26 
27 namespace chromeos {
28 
29 namespace {
30 
LoginFailureToKioskAppLaunchError(const LoginFailure & error)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;
44   }
45 }
46 
47 }  // namespace
48 
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.
55 
56 class KioskProfileLoader::CryptohomedChecker
57     : public base::SupportsWeakPtr<CryptohomedChecker> {
58  public:
CryptohomedChecker(KioskProfileLoader * loader)59   explicit CryptohomedChecker(KioskProfileLoader* loader)
60       : loader_(loader),
61         retry_count_(0) {
62   }
~CryptohomedChecker()63   ~CryptohomedChecker() {}
64 
StartCheck()65   void StartCheck() {
66     chromeos::DBusThreadManager::Get()->GetCryptohomeClient()->IsMounted(
67         base::Bind(&CryptohomedChecker::OnCryptohomeIsMounted,
68                    AsWeakPtr()));
69   }
70 
71  private:
OnCryptohomeIsMounted(chromeos::DBusMethodCallStatus call_status,bool is_mounted)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;
81       }
82 
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;
89     }
90 
91     if (is_mounted)
92       LOG(ERROR) << "Cryptohome is mounted before launching kiosk app.";
93 
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);
99   }
100 
ReportCheckResult(KioskAppLaunchError::Error error)101   void ReportCheckResult(KioskAppLaunchError::Error error) {
102     if (error == KioskAppLaunchError::NONE)
103       loader_->LoginAsKioskAccount();
104     else
105       loader_->ReportLaunchResult(error);
106   }
107 
108   KioskProfileLoader* loader_;
109   int retry_count_;
110 
111   DISALLOW_COPY_AND_ASSIGN(CryptohomedChecker);
112 };
113 
114 
115 ////////////////////////////////////////////////////////////////////////////////
116 // KioskProfileLoader
117 
KioskProfileLoader(KioskAppManager * kiosk_app_manager,const std::string & app_id,Delegate * delegate)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;
127 }
128 
~KioskProfileLoader()129 KioskProfileLoader::~KioskProfileLoader() {}
130 
Start()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();
136 }
137 
LoginAsKioskAccount()138 void KioskProfileLoader::LoginAsKioskAccount() {
139   login_performer_.reset(new LoginPerformer(this));
140   login_performer_->LoginAsKioskAccount(user_id_);
141 }
142 
ReportLaunchResult(KioskAppLaunchError::Error error)143 void KioskProfileLoader::ReportLaunchResult(KioskAppLaunchError::Error error) {
144   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
145 
146   if (error != KioskAppLaunchError::NONE) {
147     delegate_->OnProfileLoadFailed(error);
148   }
149 }
150 
OnLoginSuccess(const UserContext & user_context)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());
155 
156   LoginUtils::Get()->PrepareProfile(user_context,
157                                     std::string(),  // display email
158                                     false,  // has_cookies
159                                     false,  // has_active_session
160                                     this);
161 }
162 
OnLoginFailure(const LoginFailure & error)163 void KioskProfileLoader::OnLoginFailure(const LoginFailure& error) {
164   ReportLaunchResult(LoginFailureToKioskAppLaunchError(error));
165 }
166 
WhiteListCheckFailed(const std::string & email)167 void KioskProfileLoader::WhiteListCheckFailed(const std::string& email) {
168   NOTREACHED();
169 }
170 
PolicyLoadFailed()171 void KioskProfileLoader::PolicyLoadFailed() {
172   ReportLaunchResult(KioskAppLaunchError::POLICY_LOAD_FAILED);
173 }
174 
OnOnlineChecked(const std::string & email,bool success)175 void KioskProfileLoader::OnOnlineChecked(
176     const std::string& email, bool success) {
177   NOTREACHED();
178 }
179 
OnProfilePrepared(Profile * profile)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);
184 
185   delegate_->OnProfileLoaded(profile);
186   ReportLaunchResult(KioskAppLaunchError::NONE);
187 }
188 
189 }  // namespace chromeos
190