1 // Copyright 2014 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/auto_enrollment_check_screen.h"
8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
10 #include "base/logging.h"
11 #include "chrome/browser/chromeos/login/error_screens_histogram_helper.h"
12 #include "chrome/browser/chromeos/login/screen_manager.h"
13 #include "chrome/browser/chromeos/login/screens/base_screen_delegate.h"
14 #include "chrome/browser/chromeos/login/wizard_controller.h"
15 #include "chromeos/chromeos_switches.h"
16 #include "chromeos/network/network_state.h"
17 #include "chromeos/network/network_state_handler.h"
22 AutoEnrollmentCheckScreen* AutoEnrollmentCheckScreen::Get(
23 ScreenManager* manager) {
24 return static_cast<AutoEnrollmentCheckScreen*>(
25 manager->GetScreen(WizardController::kAutoEnrollmentCheckScreenName));
28 AutoEnrollmentCheckScreen::AutoEnrollmentCheckScreen(
29 BaseScreenDelegate* base_screen_delegate,
30 AutoEnrollmentCheckScreenActor* actor)
31 : BaseScreen(base_screen_delegate),
33 captive_portal_status_(
34 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN),
35 auto_enrollment_state_(policy::AUTO_ENROLLMENT_STATE_IDLE),
36 histogram_helper_(new ErrorScreensHistogramHelper("Enrollment")) {
38 actor_->SetDelegate(this);
41 AutoEnrollmentCheckScreen::~AutoEnrollmentCheckScreen() {
42 NetworkPortalDetector::Get()->RemoveObserver(this);
44 actor_->SetDelegate(NULL);
47 void AutoEnrollmentCheckScreen::Start() {
51 // Make sure the auto-enrollment client is running.
52 auto_enrollment_controller_->Start();
54 auto_enrollment_progress_subscription_ =
55 auto_enrollment_controller_->RegisterProgressCallback(
57 &AutoEnrollmentCheckScreen::OnAutoEnrollmentCheckProgressed,
58 base::Unretained(this)));
59 auto_enrollment_state_ = auto_enrollment_controller_->state();
61 // NB: AddAndFireObserver below call back into OnPortalDetectionCompleted.
62 // This guarantees that the UI gets synced to current state.
63 NetworkPortalDetector* portal_detector = NetworkPortalDetector::Get();
64 portal_detector->StartDetectionIfIdle();
65 portal_detector->AddAndFireObserver(this);
68 void AutoEnrollmentCheckScreen::ClearState() {
69 auto_enrollment_state_ = policy::AUTO_ENROLLMENT_STATE_IDLE;
72 bool AutoEnrollmentCheckScreen::IsStartNeeded() {
73 // Check that forced reenrollment is wanted and if the check is needed or we
74 // already know the outcome.
75 if (AutoEnrollmentController::GetMode() !=
76 AutoEnrollmentController::MODE_FORCED_RE_ENROLLMENT ||
77 auto_enrollment_state_ ==
78 policy::AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT ||
79 auto_enrollment_state_ == policy::AUTO_ENROLLMENT_STATE_NO_ENROLLMENT) {
86 void AutoEnrollmentCheckScreen::PrepareToShow() {
89 void AutoEnrollmentCheckScreen::Show() {
90 if (IsStartNeeded()) {
94 histogram_helper_->OnScreenShow();
98 void AutoEnrollmentCheckScreen::Hide() {
101 std::string AutoEnrollmentCheckScreen::GetName() const {
102 return WizardController::kAutoEnrollmentCheckScreenName;
105 void AutoEnrollmentCheckScreen::OnExit() {
106 get_base_screen_delegate()->OnExit(
107 BaseScreenDelegate::ENTERPRISE_AUTO_ENROLLMENT_CHECK_COMPLETED);
110 void AutoEnrollmentCheckScreen::OnActorDestroyed(
111 AutoEnrollmentCheckScreenActor* actor) {
116 void AutoEnrollmentCheckScreen::OnPortalDetectionCompleted(
117 const NetworkState* /* network */,
118 const NetworkPortalDetector::CaptivePortalState& state) {
119 UpdateState(state.status, auto_enrollment_state_);
122 void AutoEnrollmentCheckScreen::OnAutoEnrollmentCheckProgressed(
123 policy::AutoEnrollmentState state) {
124 UpdateState(captive_portal_status_, state);
127 void AutoEnrollmentCheckScreen::UpdateState(
128 NetworkPortalDetector::CaptivePortalStatus new_captive_portal_status,
129 policy::AutoEnrollmentState new_auto_enrollment_state) {
130 // Configure the error screen to show the approriate error message.
131 if (!UpdateCaptivePortalStatus(new_captive_portal_status))
132 UpdateAutoEnrollmentState(new_auto_enrollment_state);
134 // Update the connecting indicator.
135 ErrorScreen* error_screen = get_base_screen_delegate()->GetErrorScreen();
136 error_screen->ShowConnectingIndicator(
137 new_auto_enrollment_state == policy::AUTO_ENROLLMENT_STATE_PENDING);
139 // Determine whether a retry is in order.
140 bool retry = (new_captive_portal_status ==
141 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE) &&
142 (captive_portal_status_ !=
143 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE);
145 // Save the new state.
146 captive_portal_status_ = new_captive_portal_status;
147 auto_enrollment_state_ = new_auto_enrollment_state;
149 // Check whether a decision got made.
150 switch (new_auto_enrollment_state) {
151 case policy::AUTO_ENROLLMENT_STATE_IDLE:
154 case policy::AUTO_ENROLLMENT_STATE_PENDING:
155 case policy::AUTO_ENROLLMENT_STATE_CONNECTION_ERROR:
157 case policy::AUTO_ENROLLMENT_STATE_SERVER_ERROR:
158 // Server errors don't block OOBE.
159 case policy::AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT:
160 case policy::AUTO_ENROLLMENT_STATE_NO_ENROLLMENT:
161 // Decision made, ready to proceed.
166 // Retry if applicable. This is last so eventual callbacks find consistent
169 auto_enrollment_controller_->Retry();
172 bool AutoEnrollmentCheckScreen::UpdateCaptivePortalStatus(
173 NetworkPortalDetector::CaptivePortalStatus new_captive_portal_status) {
174 switch (new_captive_portal_status) {
175 case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN:
176 case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE:
178 case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE:
179 ShowErrorScreen(ErrorScreen::ERROR_STATE_OFFLINE);
181 case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL:
182 ShowErrorScreen(ErrorScreen::ERROR_STATE_PORTAL);
183 if (captive_portal_status_ != new_captive_portal_status)
184 get_base_screen_delegate()->GetErrorScreen()->FixCaptivePortal();
186 case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED:
187 ShowErrorScreen(ErrorScreen::ERROR_STATE_PROXY);
189 case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_COUNT:
190 NOTREACHED() << "Bad status: CAPTIVE_PORTAL_STATUS_COUNT";
194 // Return is required to avoid compiler warning.
195 NOTREACHED() << "Bad status " << new_captive_portal_status;
199 bool AutoEnrollmentCheckScreen::UpdateAutoEnrollmentState(
200 policy::AutoEnrollmentState new_auto_enrollment_state) {
201 switch (new_auto_enrollment_state) {
202 case policy::AUTO_ENROLLMENT_STATE_IDLE:
203 // The client should have been started already.
206 case policy::AUTO_ENROLLMENT_STATE_PENDING:
207 case policy::AUTO_ENROLLMENT_STATE_SERVER_ERROR:
208 case policy::AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT:
209 case policy::AUTO_ENROLLMENT_STATE_NO_ENROLLMENT:
211 case policy::AUTO_ENROLLMENT_STATE_CONNECTION_ERROR:
212 ShowErrorScreen(ErrorScreen::ERROR_STATE_OFFLINE);
216 // Return is required to avoid compiler warning.
217 NOTREACHED() << "bad state " << new_auto_enrollment_state;
221 void AutoEnrollmentCheckScreen::ShowErrorScreen(
222 ErrorScreen::ErrorState error_state) {
223 const NetworkState* network =
224 NetworkHandler::Get()->network_state_handler()->DefaultNetwork();
225 ErrorScreen* error_screen = get_base_screen_delegate()->GetErrorScreen();
226 error_screen->SetUIState(ErrorScreen::UI_STATE_AUTO_ENROLLMENT_ERROR);
227 error_screen->AllowGuestSignin(true);
228 error_screen->SetErrorState(error_state,
229 network ? network->name() : std::string());
230 get_base_screen_delegate()->ShowErrorScreen();
231 histogram_helper_->OnErrorShow(error_state);
234 void AutoEnrollmentCheckScreen::SignalCompletion() {
235 NetworkPortalDetector::Get()->RemoveObserver(this);
236 auto_enrollment_progress_subscription_.reset();
237 get_base_screen_delegate()->OnExit(
238 BaseScreenDelegate::ENTERPRISE_AUTO_ENROLLMENT_CHECK_COMPLETED);
241 } // namespace chromeos