Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / chromeos / login / enrollment / enrollment_screen.cc
blobd5e7b50f8a097cf2a435f38218eae29e1ef29e7f
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"
7 #include "base/bind.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) \
33 do { \
34 UMA_HISTOGRAM_CUSTOM_TIMES( \
35 (histogram_name), \
36 (elapsed_timer)->Elapsed(), \
37 base::TimeDelta::FromMilliseconds(100) /* min */, \
38 base::TimeDelta::FromMinutes(15) /* max */, \
39 100 /* bucket_count */); \
40 } while (0)
42 namespace {
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";
51 } // namespace
53 namespace chromeos {
55 // static
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),
66 actor_(actor),
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_) {
103 callback.Run();
104 return;
106 enrollment_helper_->ClearAuth(base::Bind(&EnrollmentScreen::OnAuthCleared,
107 weak_ptr_factory_.GetWeakPtr(),
108 callback));
111 void EnrollmentScreen::OnAuthCleared(const base::Closure& callback) {
112 enrollment_helper_.reset();
113 callback.Run();
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() {
127 actor_->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::EnrollHostRequested(const std::string& auth_token) {
145 actor_->Show();
146 actor_->ShowEnrollmentSpinnerScreen();
147 CreateEnrollmentHelper();
148 enrollment_helper_->EnrollUsingToken(auth_token);
149 if (remora_controller_) {
150 remora_controller_->OnEnrollmentStatusChanged(
151 HostPairingController::ENROLLMENT_STATUS_ENROLLING);
155 void EnrollmentScreen::OnLoginDone(const std::string& user,
156 const std::string& auth_code) {
157 LOG_IF(ERROR, auth_code.empty()) << "Auth code is empty.";
158 elapsed_timer_.reset(new base::ElapsedTimer());
159 enrolling_user_domain_ = gaia::ExtractDomainName(user);
161 UMA(enrollment_failed_once_ ? policy::kMetricEnrollmentRestarted
162 : policy::kMetricEnrollmentStarted);
164 actor_->ShowEnrollmentSpinnerScreen();
165 CreateEnrollmentHelper();
166 enrollment_helper_->EnrollUsingAuthCode(
167 auth_code, shark_controller_ != NULL /* fetch_additional_token */);
170 void EnrollmentScreen::OnRetry() {
171 ClearAuth(base::Bind(&EnrollmentScreen::ShowSigninScreen,
172 weak_ptr_factory_.GetWeakPtr()));
175 void EnrollmentScreen::OnCancel() {
176 UMA(policy::kMetricEnrollmentCancelled);
177 if (elapsed_timer_)
178 UMA_ENROLLMENT_TIME(kMetricEnrollmentTimeCancel, elapsed_timer_);
180 const BaseScreenDelegate::ExitCodes exit_code =
181 enrollment_config_.is_forced()
182 ? BaseScreenDelegate::ENTERPRISE_ENROLLMENT_BACK
183 : BaseScreenDelegate::ENTERPRISE_ENROLLMENT_COMPLETED;
184 ClearAuth(
185 base::Bind(&EnrollmentScreen::Finish, base::Unretained(this), exit_code));
188 void EnrollmentScreen::OnConfirmationClosed() {
189 ClearAuth(base::Bind(&EnrollmentScreen::Finish, base::Unretained(this),
190 BaseScreenDelegate::ENTERPRISE_ENROLLMENT_COMPLETED));
193 void EnrollmentScreen::OnAuthError(const GoogleServiceAuthError& error) {
194 DCHECK(!remora_controller_);
195 OnAnyEnrollmentError();
196 actor_->ShowAuthError(error);
199 void EnrollmentScreen::OnEnrollmentError(policy::EnrollmentStatus status) {
200 OnAnyEnrollmentError();
201 actor_->ShowEnrollmentStatus(status);
204 void EnrollmentScreen::OnOtherError(
205 EnterpriseEnrollmentHelper::OtherError error) {
206 OnAnyEnrollmentError();
207 actor_->ShowOtherError(error);
210 void EnrollmentScreen::OnDeviceEnrolled(const std::string& additional_token) {
211 if (!additional_token.empty())
212 SendEnrollmentAuthToken(additional_token);
214 enrollment_helper_->GetDeviceAttributeUpdatePermission();
217 void EnrollmentScreen::OnDeviceAttributeProvided(const std::string& asset_id,
218 const std::string& location) {
219 enrollment_helper_->UpdateDeviceAttributes(asset_id, location);
222 void EnrollmentScreen::OnDeviceAttributeUpdatePermission(bool granted) {
223 // If user is permitted to update device attributes
224 // Show attribute prompt screen
225 if (granted) {
226 StartupUtils::MarkDeviceRegistered(
227 base::Bind(&EnrollmentScreen::ShowAttributePromptScreen,
228 weak_ptr_factory_.GetWeakPtr()));
229 } else {
230 StartupUtils::MarkDeviceRegistered(
231 base::Bind(&EnrollmentScreen::ShowEnrollmentStatusOnSuccess,
232 weak_ptr_factory_.GetWeakPtr()));
235 if (remora_controller_) {
236 policy::BrowserPolicyConnectorChromeOS* connector =
237 g_browser_process->platform_part()->browser_policy_connector_chromeos();
238 const enterprise_management::PolicyData* policy =
239 connector->GetDeviceCloudPolicyManager()->core()->store()->policy();
241 remora_controller_->SetPermanentId(policy->directory_api_id());
242 remora_controller_->OnEnrollmentStatusChanged(
243 HostPairingController::ENROLLMENT_STATUS_SUCCESS);
247 void EnrollmentScreen::OnDeviceAttributeUploadCompleted(bool success) {
248 if (success) {
249 // If the device attributes have been successfully uploaded, fetch policy.
250 policy::BrowserPolicyConnectorChromeOS* connector =
251 g_browser_process->platform_part()->browser_policy_connector_chromeos();
252 connector->GetDeviceCloudPolicyManager()->core()->RefreshSoon();
253 actor_->ShowEnrollmentStatus(policy::EnrollmentStatus::ForStatus(
254 policy::EnrollmentStatus::STATUS_SUCCESS));
255 } else {
256 actor_->ShowEnrollmentStatus(policy::EnrollmentStatus::ForStatus(
257 policy::EnrollmentStatus::STATUS_ATTRIBUTE_UPDATE_FAILED));
261 void EnrollmentScreen::ShowAttributePromptScreen() {
262 policy::BrowserPolicyConnectorChromeOS* connector =
263 g_browser_process->platform_part()->browser_policy_connector_chromeos();
264 policy::DeviceCloudPolicyManagerChromeOS* policy_manager =
265 connector->GetDeviceCloudPolicyManager();
267 policy::CloudPolicyStore* store = policy_manager->core()->store();
269 const enterprise_management::PolicyData* policy = store->policy();
271 std::string asset_id = policy ? policy->annotated_asset_id() : std::string();
272 std::string location = policy ? policy->annotated_location() : std::string();
273 actor_->ShowAttributePromptScreen(asset_id, location);
276 void EnrollmentScreen::SendEnrollmentAuthToken(const std::string& token) {
277 // TODO(achuith, zork): Extract and send domain.
278 DCHECK(shark_controller_);
279 shark_controller_->OnAuthenticationDone("", token);
282 void EnrollmentScreen::ShowEnrollmentStatusOnSuccess() {
283 if (elapsed_timer_)
284 UMA_ENROLLMENT_TIME(kMetricEnrollmentTimeSuccess, elapsed_timer_);
285 actor_->ShowEnrollmentStatus(policy::EnrollmentStatus::ForStatus(
286 policy::EnrollmentStatus::STATUS_SUCCESS));
289 void EnrollmentScreen::UMA(policy::MetricEnrollment sample) {
290 EnrollmentUMA(sample, enrollment_config_.mode);
293 void EnrollmentScreen::ShowSigninScreen() {
294 actor_->Show();
295 actor_->ShowSigninScreen();
298 void EnrollmentScreen::OnAnyEnrollmentError() {
299 enrollment_failed_once_ = true;
300 if (elapsed_timer_)
301 UMA_ENROLLMENT_TIME(kMetricEnrollmentTimeFailure, elapsed_timer_);
302 if (remora_controller_) {
303 remora_controller_->OnEnrollmentStatusChanged(
304 HostPairingController::ENROLLMENT_STATUS_FAILURE);
308 } // namespace chromeos