1 // Copyright (c) 2012 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/enrollment/enrollment_screen.h"
8 #include "base/bind_helpers.h"
9 #include "base/callback.h"
10 #include "base/logging.h"
11 #include "base/metrics/histogram.h"
12 #include "base/timer/elapsed_timer.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/browser_process_platform_part.h"
15 #include "chrome/browser/chromeos/login/enrollment/enrollment_uma.h"
16 #include "chrome/browser/chromeos/login/screen_manager.h"
17 #include "chrome/browser/chromeos/login/screens/base_screen_delegate.h"
18 #include "chrome/browser/chromeos/login/startup_utils.h"
19 #include "chrome/browser/chromeos/login/wizard_controller.h"
20 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
21 #include "chrome/browser/chromeos/policy/enrollment_status_chromeos.h"
22 #include "chrome/browser/chromeos/profiles/profile_helper.h"
23 #include "chromeos/dbus/cryptohome_client.h"
24 #include "chromeos/dbus/dbus_method_call_status.h"
25 #include "chromeos/dbus/dbus_thread_manager.h"
26 #include "components/pairing/controller_pairing_controller.h"
27 #include "google_apis/gaia/gaia_auth_util.h"
29 using namespace pairing_chromeos
;
31 // Do not change the UMA histogram parameters without renaming the histograms!
32 #define UMA_ENROLLMENT_TIME(histogram_name, elapsed_timer) \
34 UMA_HISTOGRAM_CUSTOM_TIMES( \
36 (elapsed_timer)->Elapsed(), \
37 base::TimeDelta::FromMilliseconds(100) /* min */, \
38 base::TimeDelta::FromMinutes(15) /* max */, \
39 100 /* bucket_count */); \
44 const char * const kMetricEnrollmentTimeCancel
=
45 "Enterprise.EnrollmentTime.Cancel";
46 const char * const kMetricEnrollmentTimeFailure
=
47 "Enterprise.EnrollmentTime.Failure";
48 const char * const kMetricEnrollmentTimeSuccess
=
49 "Enterprise.EnrollmentTime.Success";
56 EnrollmentScreen
* EnrollmentScreen::Get(ScreenManager
* manager
) {
57 return static_cast<EnrollmentScreen
*>(
58 manager
->GetScreen(WizardController::kEnrollmentScreenName
));
61 EnrollmentScreen::EnrollmentScreen(BaseScreenDelegate
* base_screen_delegate
,
62 EnrollmentScreenActor
* actor
)
63 : BaseScreen(base_screen_delegate
),
64 shark_controller_(NULL
),
65 remora_controller_(NULL
),
67 enrollment_failed_once_(false),
68 weak_ptr_factory_(this) {
69 // Init the TPM if it has not been done until now (in debug build we might
70 // have not done that yet).
71 DBusThreadManager::Get()->GetCryptohomeClient()->TpmCanAttemptOwnership(
72 EmptyVoidDBusMethodCallback());
75 EnrollmentScreen::~EnrollmentScreen() {
76 if (remora_controller_
)
77 remora_controller_
->RemoveObserver(this);
78 DCHECK(!enrollment_helper_
|| g_browser_process
->IsShuttingDown());
81 void EnrollmentScreen::SetParameters(
82 const policy::EnrollmentConfig
& enrollment_config
,
83 pairing_chromeos::ControllerPairingController
* shark_controller
,
84 pairing_chromeos::HostPairingController
* remora_controller
) {
85 enrollment_config_
= enrollment_config
;
86 shark_controller_
= shark_controller
;
87 if (remora_controller_
)
88 remora_controller_
->RemoveObserver(this);
89 remora_controller_
= remora_controller
;
90 if (remora_controller_
)
91 remora_controller_
->AddObserver(this);
92 actor_
->SetParameters(this, enrollment_config_
);
95 void EnrollmentScreen::CreateEnrollmentHelper() {
96 DCHECK(!enrollment_helper_
);
97 enrollment_helper_
= EnterpriseEnrollmentHelper::Create(
98 this, enrollment_config_
, enrolling_user_domain_
);
101 void EnrollmentScreen::ClearAuth(const base::Closure
& callback
) {
102 if (!enrollment_helper_
) {
106 enrollment_helper_
->ClearAuth(base::Bind(&EnrollmentScreen::OnAuthCleared
,
107 weak_ptr_factory_
.GetWeakPtr(),
111 void EnrollmentScreen::OnAuthCleared(const base::Closure
& callback
) {
112 enrollment_helper_
.reset();
116 void EnrollmentScreen::PrepareToShow() {
117 actor_
->PrepareToShow();
120 void EnrollmentScreen::Show() {
121 UMA(policy::kMetricEnrollmentTriggered
);
122 ClearAuth(base::Bind(&EnrollmentScreen::ShowSigninScreen
,
123 weak_ptr_factory_
.GetWeakPtr()));
126 void EnrollmentScreen::Hide() {
128 weak_ptr_factory_
.InvalidateWeakPtrs();
131 std::string
EnrollmentScreen::GetName() const {
132 return WizardController::kEnrollmentScreenName
;
135 void EnrollmentScreen::PairingStageChanged(Stage new_stage
) {
136 DCHECK(remora_controller_
);
137 if (new_stage
== HostPairingController::STAGE_FINISHED
) {
138 remora_controller_
->RemoveObserver(this);
139 remora_controller_
= NULL
;
140 OnConfirmationClosed();
144 void EnrollmentScreen::ConfigureHost(bool accepted_eula
,
145 const std::string
& lang
,
146 const std::string
& timezone
,
148 const std::string
& keyboard_layout
) {
151 void EnrollmentScreen::EnrollHost(const std::string
& auth_token
) {
153 actor_
->ShowEnrollmentSpinnerScreen();
154 CreateEnrollmentHelper();
155 enrollment_helper_
->EnrollUsingToken(auth_token
);
156 if (remora_controller_
) {
157 remora_controller_
->OnEnrollmentStatusChanged(
158 HostPairingController::ENROLLMENT_STATUS_ENROLLING
);
162 void EnrollmentScreen::OnLoginDone(const std::string
& user
) {
163 elapsed_timer_
.reset(new base::ElapsedTimer());
164 enrolling_user_domain_
= gaia::ExtractDomainName(user
);
166 UMA(enrollment_failed_once_
? policy::kMetricEnrollmentRestarted
167 : policy::kMetricEnrollmentStarted
);
169 actor_
->ShowEnrollmentSpinnerScreen();
170 CreateEnrollmentHelper();
171 enrollment_helper_
->EnrollUsingProfile(
172 ProfileHelper::GetSigninProfile(),
173 shark_controller_
!= NULL
/* fetch_additional_token */);
176 void EnrollmentScreen::OnRetry() {
177 ClearAuth(base::Bind(&EnrollmentScreen::ShowSigninScreen
,
178 weak_ptr_factory_
.GetWeakPtr()));
181 void EnrollmentScreen::OnCancel() {
182 UMA(policy::kMetricEnrollmentCancelled
);
184 UMA_ENROLLMENT_TIME(kMetricEnrollmentTimeCancel
, elapsed_timer_
);
186 const BaseScreenDelegate::ExitCodes exit_code
=
187 enrollment_config_
.is_forced()
188 ? BaseScreenDelegate::ENTERPRISE_ENROLLMENT_BACK
189 : BaseScreenDelegate::ENTERPRISE_ENROLLMENT_COMPLETED
;
191 base::Bind(&EnrollmentScreen::Finish
, base::Unretained(this), exit_code
));
194 void EnrollmentScreen::OnConfirmationClosed() {
195 ClearAuth(base::Bind(&EnrollmentScreen::Finish
, base::Unretained(this),
196 BaseScreenDelegate::ENTERPRISE_ENROLLMENT_COMPLETED
));
199 void EnrollmentScreen::OnAuthError(const GoogleServiceAuthError
& error
) {
200 DCHECK(!remora_controller_
);
201 OnAnyEnrollmentError();
202 actor_
->ShowAuthError(error
);
205 void EnrollmentScreen::OnEnrollmentError(policy::EnrollmentStatus status
) {
206 OnAnyEnrollmentError();
207 actor_
->ShowEnrollmentStatus(status
);
210 void EnrollmentScreen::OnOtherError(
211 EnterpriseEnrollmentHelper::OtherError error
) {
212 OnAnyEnrollmentError();
213 actor_
->ShowOtherError(error
);
216 void EnrollmentScreen::OnDeviceEnrolled(const std::string
& additional_token
) {
217 if (!additional_token
.empty())
218 SendEnrollmentAuthToken(additional_token
);
219 StartupUtils::MarkDeviceRegistered(
220 base::Bind(&EnrollmentScreen::ShowEnrollmentStatusOnSuccess
,
221 weak_ptr_factory_
.GetWeakPtr()));
222 if (remora_controller_
) {
223 remora_controller_
->OnEnrollmentStatusChanged(
224 HostPairingController::ENROLLMENT_STATUS_SUCCESS
);
228 void EnrollmentScreen::SendEnrollmentAuthToken(const std::string
& token
) {
229 // TODO(achuith, zork): Extract and send domain.
230 DCHECK(shark_controller_
);
231 shark_controller_
->OnAuthenticationDone("", token
);
234 void EnrollmentScreen::ShowEnrollmentStatusOnSuccess() {
235 StartupUtils::MarkOobeCompleted();
237 UMA_ENROLLMENT_TIME(kMetricEnrollmentTimeSuccess
, elapsed_timer_
);
238 actor_
->ShowEnrollmentStatus(policy::EnrollmentStatus::ForStatus(
239 policy::EnrollmentStatus::STATUS_SUCCESS
));
242 void EnrollmentScreen::UMA(policy::MetricEnrollment sample
) {
243 EnrollmentUMA(sample
, enrollment_config_
.mode
);
246 void EnrollmentScreen::ShowSigninScreen() {
248 actor_
->ShowSigninScreen();
251 void EnrollmentScreen::OnAnyEnrollmentError() {
252 enrollment_failed_once_
= true;
254 UMA_ENROLLMENT_TIME(kMetricEnrollmentTimeFailure
, elapsed_timer_
);
255 if (remora_controller_
) {
256 remora_controller_
->OnEnrollmentStatusChanged(
257 HostPairingController::ENROLLMENT_STATUS_FAILURE
);
261 } // namespace chromeos