Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / login / enrollment / auto_enrollment_check_screen.cc
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_screen.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/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"
18
19 namespace chromeos {
20
21 // static
22 AutoEnrollmentCheckScreen* AutoEnrollmentCheckScreen::Get(
23     ScreenManager* manager) {
24   return static_cast<AutoEnrollmentCheckScreen*>(
25       manager->GetScreen(WizardController::kAutoEnrollmentCheckScreenName));
26 }
27
28 AutoEnrollmentCheckScreen::AutoEnrollmentCheckScreen(
29     BaseScreenDelegate* base_screen_delegate,
30     AutoEnrollmentCheckScreenActor* actor)
31     : BaseScreen(base_screen_delegate),
32       actor_(actor),
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")) {
37   if (actor_)
38     actor_->SetDelegate(this);
39 }
40
41 AutoEnrollmentCheckScreen::~AutoEnrollmentCheckScreen() {
42   NetworkPortalDetector::Get()->RemoveObserver(this);
43   if (actor_)
44     actor_->SetDelegate(NULL);
45 }
46
47 void AutoEnrollmentCheckScreen::Start() {
48   if (!IsStartNeeded())
49     return;
50
51   // Make sure the auto-enrollment client is running.
52   auto_enrollment_controller_->Start();
53
54   auto_enrollment_progress_subscription_ =
55       auto_enrollment_controller_->RegisterProgressCallback(
56           base::Bind(
57               &AutoEnrollmentCheckScreen::OnAutoEnrollmentCheckProgressed,
58               base::Unretained(this)));
59   auto_enrollment_state_ = auto_enrollment_controller_->state();
60
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);
66 }
67
68 void AutoEnrollmentCheckScreen::ClearState() {
69    auto_enrollment_state_ = policy::AUTO_ENROLLMENT_STATE_IDLE;
70 }
71
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) {
80     SignalCompletion();
81     return false;
82   }
83   return true;
84 }
85
86 void AutoEnrollmentCheckScreen::PrepareToShow() {
87 }
88
89 void AutoEnrollmentCheckScreen::Show() {
90   if (IsStartNeeded()) {
91     Start();
92     if (actor_)
93       actor_->Show();
94     histogram_helper_->OnScreenShow();
95   }
96 }
97
98 void AutoEnrollmentCheckScreen::Hide() {
99 }
100
101 std::string AutoEnrollmentCheckScreen::GetName() const {
102   return WizardController::kAutoEnrollmentCheckScreenName;
103 }
104
105 void AutoEnrollmentCheckScreen::OnExit() {
106   get_base_screen_delegate()->OnExit(
107       BaseScreenDelegate::ENTERPRISE_AUTO_ENROLLMENT_CHECK_COMPLETED);
108 }
109
110 void AutoEnrollmentCheckScreen::OnActorDestroyed(
111     AutoEnrollmentCheckScreenActor* actor) {
112   if (actor_ == actor)
113     actor_ = NULL;
114 }
115
116 void AutoEnrollmentCheckScreen::OnPortalDetectionCompleted(
117     const NetworkState* /* network */,
118     const NetworkPortalDetector::CaptivePortalState& state) {
119   UpdateState(state.status, auto_enrollment_state_);
120 }
121
122 void AutoEnrollmentCheckScreen::OnAutoEnrollmentCheckProgressed(
123     policy::AutoEnrollmentState state) {
124   UpdateState(captive_portal_status_, state);
125 }
126
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);
133
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);
138
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);
144
145   // Save the new state.
146   captive_portal_status_ = new_captive_portal_status;
147   auto_enrollment_state_ = new_auto_enrollment_state;
148
149   // Check whether a decision got made.
150   switch (new_auto_enrollment_state) {
151     case policy::AUTO_ENROLLMENT_STATE_IDLE:
152       NOTREACHED();
153       // fall through.
154     case policy::AUTO_ENROLLMENT_STATE_PENDING:
155     case policy::AUTO_ENROLLMENT_STATE_CONNECTION_ERROR:
156       break;
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.
162       SignalCompletion();
163       return;
164   }
165
166   // Retry if applicable. This is last so eventual callbacks find consistent
167   // state.
168   if (retry)
169     auto_enrollment_controller_->Retry();
170 }
171
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:
177       return false;
178     case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_OFFLINE:
179       ShowErrorScreen(ErrorScreen::ERROR_STATE_OFFLINE);
180       return true;
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();
185       return true;
186     case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_PROXY_AUTH_REQUIRED:
187       ShowErrorScreen(ErrorScreen::ERROR_STATE_PROXY);
188       return true;
189     case NetworkPortalDetector::CAPTIVE_PORTAL_STATUS_COUNT:
190       NOTREACHED() << "Bad status: CAPTIVE_PORTAL_STATUS_COUNT";
191       return false;
192   }
193
194   // Return is required to avoid compiler warning.
195   NOTREACHED() << "Bad status " << new_captive_portal_status;
196   return false;
197 }
198
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.
204       NOTREACHED();
205       return false;
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:
210       return false;
211     case policy::AUTO_ENROLLMENT_STATE_CONNECTION_ERROR:
212       ShowErrorScreen(ErrorScreen::ERROR_STATE_OFFLINE);
213       return true;
214   }
215
216   // Return is required to avoid compiler warning.
217   NOTREACHED() << "bad state " << new_auto_enrollment_state;
218   return false;
219 }
220
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);
232 }
233
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);
239 }
240
241 }  // namespace chromeos