OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/chromeos/login/enrollment/auto_enrollment_check_step.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/command_line.h" | |
10 #include "base/logging.h" | |
11 #include "chrome/browser/chromeos/login/screens/screen_observer.h" | |
12 #include "chromeos/chromeos_switches.h" | |
13 #include "chromeos/network/network_state.h" | |
14 #include "chromeos/network/network_state_handler.h" | |
15 | |
16 namespace chromeos { | |
17 | |
18 AutoEnrollmentCheckStep::AutoEnrollmentCheckStep( | |
19 ScreenObserver* screen_observer, | |
20 AutoEnrollmentController* auto_enrollment_controller) | |
21 : screen_observer_(screen_observer), | |
22 auto_enrollment_controller_(auto_enrollment_controller), | |
23 captive_portal_status_( | |
24 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN), | |
25 auto_enrollment_state_(policy::AUTO_ENROLLMENT_STATE_IDLE) {} | |
26 | |
27 AutoEnrollmentCheckStep::~AutoEnrollmentCheckStep() { | |
28 NetworkPortalDetector::Get()->RemoveObserver(this); | |
29 } | |
30 | |
31 void AutoEnrollmentCheckStep::Start() { | |
32 if (AutoEnrollmentController::GetMode() != | |
33 AutoEnrollmentController::MODE_FORCED_RE_ENROLLMENT) { | |
34 SignalCompletion(); | |
35 return; | |
36 } | |
37 | |
38 // Make sure the auto-enrollment client is running. | |
39 auto_enrollment_controller_->Start(); | |
40 | |
41 auto_enrollment_progress_subscription_ = | |
42 auto_enrollment_controller_->RegisterProgressCallback( | |
43 base::Bind(&AutoEnrollmentCheckStep::OnAutoEnrollmentCheckProgressed, | |
44 base::Unretained(this))); | |
45 auto_enrollment_state_ = auto_enrollment_controller_->state(); | |
46 | |
47 // NB: AddAndFireObserver below call back into OnPortalDetectionCompleted. | |
48 // This guarantees that the UI gets synced to current state. | |
49 NetworkPortalDetector* portal_detector = NetworkPortalDetector::Get(); | |
50 portal_detector->StartDetectionIfIdle(); | |
51 portal_detector->AddAndFireObserver(this); | |
52 } | |
53 | |
54 void AutoEnrollmentCheckStep::OnPortalDetectionCompleted( | |
55 const NetworkState* /* network */, | |
56 const NetworkPortalDetector::CaptivePortalState& state) { | |
57 UpdateState(state.status, auto_enrollment_state_); | |
58 } | |
59 | |
60 void AutoEnrollmentCheckStep::OnAutoEnrollmentCheckProgressed( | |
61 policy::AutoEnrollmentState state) { | |
62 UpdateState(captive_portal_status_, state); | |
63 } | |
64 | |
65 void AutoEnrollmentCheckStep::UpdateState( | |
66 NetworkPortalDetector::CaptivePortalStatus new_captive_portal_status, | |
67 policy::AutoEnrollmentState new_auto_enrollment_state) { | |
68 // Configure the error screen to show the approriate error message. | |
69 if (!UpdateCaptivePortalStatus(new_captive_portal_status)) | |
70 UpdateAutoEnrollmentState(new_auto_enrollment_state); | |
71 | |
72 // Update the connecting indicator. | |
73 ErrorScreen* error_screen = screen_observer_->GetErrorScreen(); | |
74 error_screen->ShowConnectingIndicator( | |
75 new_auto_enrollment_state == policy::AUTO_ENROLLMENT_STATE_PENDING); | |
76 | |
77 // Determine whether a retry is in order. | |
78 bool retry = (new_captive_portal_status == | |
79 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE) && | |
80 (captive_portal_status_ != | |
81 NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE); | |
82 | |
83 // Save the new state. | |
84 captive_portal_status_ = new_captive_portal_status; | |
85 auto_enrollment_state_ = new_auto_enrollment_state; | |
86 | |
87 // Check whether a decision got made. | |
88 switch (new_auto_enrollment_state) { | |
89 case policy::AUTO_ENROLLMENT_STATE_IDLE: | |
90 NOTREACHED(); | |
91 // fall through. | |
92 case policy::AUTO_ENROLLMENT_STATE_PENDING: | |
93 case policy::AUTO_ENROLLMENT_STATE_CONNECTION_ERROR: | |
94 break; | |
95 case policy::AUTO_ENROLLMENT_STATE_SERVER_ERROR: | |
96 // Server errors don't block OOBE. | |
97 case policy::AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT: | |
98 case policy::AUTO_ENROLLMENT_STATE_NO_ENROLLMENT: | |
99 // Decision made, ready to proceed. | |
100 SignalCompletion(); | |
101 return; | |
102 } | |
103 | |
104 // Retry if applicable. This is last so eventual callbacks find consistent | |
105 // state. | |
106 if (retry) | |
107 auto_enrollment_controller_->Retry(); | |
108 } | |
109 | |
110 bool AutoEnrollmentCheckStep::UpdateCaptivePortalStatus( | |
111 NetworkPortalDetector::CaptivePortalStatus new_captive_portal_status) { | |
112 switch (new_captive_portal_status) { | |
113 case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_UNKNOWN: | |
114 case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_ONLINE: | |
115 return false; | |
116 case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE: | |
117 ShowErrorScreen(ErrorScreen::ERROR_STATE_OFFLINE); | |
118 return true; | |
119 case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PORTAL: | |
120 ShowErrorScreen(ErrorScreen::ERROR_STATE_PORTAL); | |
121 if (captive_portal_status_ != new_captive_portal_status) | |
122 screen_observer_->GetErrorScreen()->FixCaptivePortal(); | |
123 return true; | |
124 case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED: | |
125 ShowErrorScreen(ErrorScreen::ERROR_STATE_PROXY); | |
126 return true; | |
127 case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_COUNT: | |
128 // Trigger NOTREACHED() below. | |
129 break; | |
130 } | |
131 | |
132 NOTREACHED() << "Bad status " << new_captive_portal_status; | |
133 return false; | |
134 } | |
135 | |
136 bool AutoEnrollmentCheckStep::UpdateAutoEnrollmentState( | |
137 policy::AutoEnrollmentState new_auto_enrollment_state) { | |
138 switch (new_auto_enrollment_state) { | |
139 case policy::AUTO_ENROLLMENT_STATE_IDLE: | |
140 // The client should have been started already. | |
141 NOTREACHED(); | |
142 return false; | |
143 case policy::AUTO_ENROLLMENT_STATE_PENDING: | |
144 case policy::AUTO_ENROLLMENT_STATE_SERVER_ERROR: | |
145 case policy::AUTO_ENROLLMENT_STATE_TRIGGER_ENROLLMENT: | |
146 case policy::AUTO_ENROLLMENT_STATE_NO_ENROLLMENT: | |
147 return false; | |
148 case policy::AUTO_ENROLLMENT_STATE_CONNECTION_ERROR: | |
149 ShowErrorScreen(ErrorScreen::ERROR_STATE_OFFLINE); | |
150 return true; | |
151 } | |
152 | |
153 NOTREACHED() << "bad state " << new_auto_enrollment_state; | |
154 return false; | |
155 } | |
156 | |
157 void AutoEnrollmentCheckStep::ShowErrorScreen( | |
158 ErrorScreen::ErrorState error_state) { | |
159 const NetworkState* network = | |
160 NetworkHandler::Get()->network_state_handler()->DefaultNetwork(); | |
161 ErrorScreen* error_screen = screen_observer_->GetErrorScreen(); | |
162 error_screen->SetUIState(ErrorScreen::UI_STATE_AUTO_ENROLLMENT_ERROR); | |
163 error_screen->AllowGuestSignin(true); | |
164 error_screen->SetErrorState(error_state, | |
165 network ? network->name() : std::string()); | |
166 screen_observer_->ShowErrorScreen(); | |
167 } | |
168 | |
169 void AutoEnrollmentCheckStep::SignalCompletion() { | |
170 NetworkPortalDetector::Get()->RemoveObserver(this); | |
171 auto_enrollment_progress_subscription_.reset(); | |
172 screen_observer_->OnExit( | |
173 ScreenObserver::ENTERPRISE_AUTO_ENROLLMENT_CHECK_COMPLETED); | |
174 } | |
175 | |
176 } // namespace chromeos | |
OLD | NEW |