• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "chrome/browser/ui/webui/chromeos/login/enrollment_screen_handler.h"
6 
7 #include <algorithm>
8 
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/compiler_specific.h"
12 #include "base/macros.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/values.h"
17 #include "chrome/browser/browser_process.h"
18 #include "chrome/browser/browsing_data/browsing_data_helper.h"
19 #include "chrome/browser/browsing_data/browsing_data_remover.h"
20 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
21 #include "chrome/browser/chromeos/policy/policy_oauth2_token_fetcher.h"
22 #include "chrome/browser/extensions/signin/gaia_auth_extension_loader.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/ui/webui/chromeos/login/authenticated_user_email_retriever.h"
25 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h"
26 #include "chrome/grit/generated_resources.h"
27 #include "chromeos/network/network_state.h"
28 #include "chromeos/network/network_state_handler.h"
29 #include "components/policy/core/browser/cloud/message_util.h"
30 #include "content/public/browser/web_contents.h"
31 #include "google_apis/gaia/gaia_auth_fetcher.h"
32 #include "google_apis/gaia/gaia_auth_util.h"
33 #include "google_apis/gaia/gaia_constants.h"
34 #include "google_apis/gaia/gaia_urls.h"
35 #include "google_apis/gaia/google_service_auth_error.h"
36 #include "net/url_request/url_request_context_getter.h"
37 #include "ui/base/l10n/l10n_util.h"
38 
39 namespace chromeos {
40 namespace {
41 
42 const char kJsScreenPath[] = "login.OAuthEnrollmentScreen";
43 
44 // Enrollment step names.
45 const char kEnrollmentStepSignin[] = "signin";
46 const char kEnrollmentStepSuccess[] = "success";
47 
48 // Enrollment mode strings.
49 const char* const kModeStrings[EnrollmentScreenActor::ENROLLMENT_MODE_COUNT] =
50     { "manual", "forced", "auto", "recovery" };
51 
EnrollmentModeToString(EnrollmentScreenActor::EnrollmentMode mode)52 std::string EnrollmentModeToString(EnrollmentScreenActor::EnrollmentMode mode) {
53   CHECK(0 <= mode && mode < EnrollmentScreenActor::ENROLLMENT_MODE_COUNT);
54   return kModeStrings[mode];
55 }
56 
57 // A helper class that takes care of asynchronously revoking a given token.
58 class TokenRevoker : public GaiaAuthConsumer {
59  public:
TokenRevoker()60   TokenRevoker()
61       : gaia_fetcher_(this,
62                       GaiaConstants::kChromeOSSource,
63                       g_browser_process->system_request_context()) {}
~TokenRevoker()64   virtual ~TokenRevoker() {}
65 
Start(const std::string & token)66   void Start(const std::string& token) {
67     gaia_fetcher_.StartRevokeOAuth2Token(token);
68   }
69 
70   // GaiaAuthConsumer:
OnOAuth2RevokeTokenCompleted()71   virtual void OnOAuth2RevokeTokenCompleted() OVERRIDE {
72     base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
73   }
74 
75  private:
76   GaiaAuthFetcher gaia_fetcher_;
77 
78   DISALLOW_COPY_AND_ASSIGN(TokenRevoker);
79 };
80 
81 // Returns network name by service path.
GetNetworkName(const std::string & service_path)82 std::string GetNetworkName(const std::string& service_path) {
83   const NetworkState* network =
84       NetworkHandler::Get()->network_state_handler()->GetNetworkState(
85           service_path);
86   if (!network)
87     return std::string();
88   return network->name();
89 }
90 
IsBehindCaptivePortal(NetworkStateInformer::State state,ErrorScreenActor::ErrorReason reason)91 bool IsBehindCaptivePortal(NetworkStateInformer::State state,
92                            ErrorScreenActor::ErrorReason reason) {
93   return state == NetworkStateInformer::CAPTIVE_PORTAL ||
94          reason == ErrorScreenActor::ERROR_REASON_PORTAL_DETECTED;
95 }
96 
IsProxyError(NetworkStateInformer::State state,ErrorScreenActor::ErrorReason reason)97 bool IsProxyError(NetworkStateInformer::State state,
98                   ErrorScreenActor::ErrorReason reason) {
99   return state == NetworkStateInformer::PROXY_AUTH_REQUIRED ||
100          reason == ErrorScreenActor::ERROR_REASON_PROXY_AUTH_CANCELLED ||
101          reason == ErrorScreenActor::ERROR_REASON_PROXY_CONNECTION_FAILED;
102 }
103 
104 }  // namespace
105 
106 // EnrollmentScreenHandler, public ------------------------------
107 
EnrollmentScreenHandler(const scoped_refptr<NetworkStateInformer> & network_state_informer,ErrorScreenActor * error_screen_actor)108 EnrollmentScreenHandler::EnrollmentScreenHandler(
109     const scoped_refptr<NetworkStateInformer>& network_state_informer,
110     ErrorScreenActor* error_screen_actor)
111     : BaseScreenHandler(kJsScreenPath),
112       controller_(NULL),
113       show_on_init_(false),
114       enrollment_mode_(ENROLLMENT_MODE_MANUAL),
115       browsing_data_remover_(NULL),
116       frame_error_(net::OK),
117       network_state_informer_(network_state_informer),
118       error_screen_actor_(error_screen_actor),
119       weak_ptr_factory_(this) {
120   set_async_assets_load_id(OobeUI::kScreenOobeEnrollment);
121   DCHECK(network_state_informer_.get());
122   DCHECK(error_screen_actor_);
123   network_state_informer_->AddObserver(this);
124 
125   if (chromeos::LoginDisplayHostImpl::default_host()) {
126     chromeos::WebUILoginView* login_view =
127         chromeos::LoginDisplayHostImpl::default_host()->GetWebUILoginView();
128     if (login_view)
129       login_view->AddFrameObserver(this);
130   }
131 }
132 
~EnrollmentScreenHandler()133 EnrollmentScreenHandler::~EnrollmentScreenHandler() {
134   if (browsing_data_remover_)
135     browsing_data_remover_->RemoveObserver(this);
136   network_state_informer_->RemoveObserver(this);
137 
138   if (chromeos::LoginDisplayHostImpl::default_host()) {
139     chromeos::WebUILoginView* login_view =
140         chromeos::LoginDisplayHostImpl::default_host()->GetWebUILoginView();
141     if (login_view)
142       login_view->RemoveFrameObserver(this);
143   }
144 }
145 
146 // EnrollmentScreenHandler, WebUIMessageHandler implementation --
147 
RegisterMessages()148 void EnrollmentScreenHandler::RegisterMessages() {
149   AddCallback("oauthEnrollRetrieveAuthenticatedUserEmail",
150               &EnrollmentScreenHandler::HandleRetrieveAuthenticatedUserEmail);
151   AddCallback("oauthEnrollClose",
152               &EnrollmentScreenHandler::HandleClose);
153   AddCallback("oauthEnrollCompleteLogin",
154               &EnrollmentScreenHandler::HandleCompleteLogin);
155   AddCallback("oauthEnrollRetry",
156               &EnrollmentScreenHandler::HandleRetry);
157   AddCallback("frameLoadingCompleted",
158               &EnrollmentScreenHandler::HandleFrameLoadingCompleted);
159 }
160 
161 // EnrollmentScreenHandler
162 //      EnrollmentScreenActor implementation -----------------------------------
163 
SetParameters(Controller * controller,EnrollmentMode enrollment_mode,const std::string & management_domain)164 void EnrollmentScreenHandler::SetParameters(
165     Controller* controller,
166     EnrollmentMode enrollment_mode,
167     const std::string& management_domain) {
168   controller_ = controller;
169   enrollment_mode_ = enrollment_mode;
170   management_domain_ = management_domain;
171 }
172 
PrepareToShow()173 void EnrollmentScreenHandler::PrepareToShow() {
174 }
175 
Show()176 void EnrollmentScreenHandler::Show() {
177   if (!page_is_ready())
178     show_on_init_ = true;
179   else
180     DoShow();
181 }
182 
Hide()183 void EnrollmentScreenHandler::Hide() {
184 }
185 
FetchOAuthToken()186 void EnrollmentScreenHandler::FetchOAuthToken() {
187   Profile* profile = Profile::FromWebUI(web_ui());
188   oauth_fetcher_.reset(
189       new policy::PolicyOAuth2TokenFetcher(
190           profile->GetRequestContext(),
191           g_browser_process->system_request_context(),
192           base::Bind(&EnrollmentScreenHandler::OnTokenFetched,
193                      base::Unretained(this))));
194   oauth_fetcher_->Start();
195 }
196 
ResetAuth(const base::Closure & callback)197 void EnrollmentScreenHandler::ResetAuth(const base::Closure& callback) {
198   auth_reset_callbacks_.push_back(callback);
199   if (browsing_data_remover_)
200     return;
201 
202   if (oauth_fetcher_) {
203     if (!oauth_fetcher_->oauth2_access_token().empty())
204       (new TokenRevoker())->Start(oauth_fetcher_->oauth2_access_token());
205 
206     if (!oauth_fetcher_->oauth2_refresh_token().empty())
207       (new TokenRevoker())->Start(oauth_fetcher_->oauth2_refresh_token());
208   }
209 
210   Profile* profile = Profile::FromBrowserContext(
211       web_ui()->GetWebContents()->GetBrowserContext());
212   browsing_data_remover_ =
213       BrowsingDataRemover::CreateForUnboundedRange(profile);
214   browsing_data_remover_->AddObserver(this);
215   browsing_data_remover_->Remove(BrowsingDataRemover::REMOVE_SITE_DATA,
216                                  BrowsingDataHelper::UNPROTECTED_WEB);
217 }
218 
ShowSigninScreen()219 void EnrollmentScreenHandler::ShowSigninScreen() {
220   ShowStep(kEnrollmentStepSignin);
221 }
222 
ShowEnrollmentSpinnerScreen()223 void EnrollmentScreenHandler::ShowEnrollmentSpinnerScreen() {
224   ShowWorking(IDS_ENTERPRISE_ENROLLMENT_WORKING);
225 }
226 
ShowLoginSpinnerScreen()227 void EnrollmentScreenHandler::ShowLoginSpinnerScreen() {
228   ShowWorking(IDS_ENTERPRISE_ENROLLMENT_RESUMING_LOGIN);
229 }
230 
ShowAuthError(const GoogleServiceAuthError & error)231 void EnrollmentScreenHandler::ShowAuthError(
232     const GoogleServiceAuthError& error) {
233   switch (error.state()) {
234     case GoogleServiceAuthError::NONE:
235     case GoogleServiceAuthError::CAPTCHA_REQUIRED:
236     case GoogleServiceAuthError::TWO_FACTOR:
237     case GoogleServiceAuthError::HOSTED_NOT_ALLOWED:
238     case GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS:
239     case GoogleServiceAuthError::REQUEST_CANCELED:
240     case GoogleServiceAuthError::UNEXPECTED_SERVICE_RESPONSE:
241     case GoogleServiceAuthError::SERVICE_ERROR:
242       ShowError(IDS_ENTERPRISE_ENROLLMENT_AUTH_FATAL_ERROR, false);
243       return;
244     case GoogleServiceAuthError::USER_NOT_SIGNED_UP:
245     case GoogleServiceAuthError::ACCOUNT_DELETED:
246     case GoogleServiceAuthError::ACCOUNT_DISABLED:
247       ShowError(IDS_ENTERPRISE_ENROLLMENT_AUTH_ACCOUNT_ERROR, true);
248       return;
249     case GoogleServiceAuthError::CONNECTION_FAILED:
250     case GoogleServiceAuthError::SERVICE_UNAVAILABLE:
251       ShowError(IDS_ENTERPRISE_ENROLLMENT_AUTH_NETWORK_ERROR, true);
252       return;
253     case GoogleServiceAuthError::NUM_STATES:
254       break;
255   }
256   NOTREACHED();
257 }
258 
ShowUIError(UIError error)259 void EnrollmentScreenHandler::ShowUIError(UIError error) {
260   switch (error) {
261     case UI_ERROR_DOMAIN_MISMATCH:
262       ShowError(IDS_ENTERPRISE_ENROLLMENT_STATUS_LOCK_WRONG_USER, true);
263       return;
264     case UI_ERROR_AUTO_ENROLLMENT_BAD_MODE:
265       ShowError(IDS_ENTERPRISE_AUTO_ENROLLMENT_BAD_MODE, true);
266       return;
267     case UI_ERROR_FATAL:
268       ShowError(IDS_ENTERPRISE_ENROLLMENT_FATAL_ENROLLMENT_ERROR, true);
269       return;
270   }
271   NOTREACHED();
272 }
273 
ShowEnrollmentStatus(policy::EnrollmentStatus status)274 void EnrollmentScreenHandler::ShowEnrollmentStatus(
275     policy::EnrollmentStatus status) {
276   switch (status.status()) {
277     case policy::EnrollmentStatus::STATUS_SUCCESS:
278       ShowStep(kEnrollmentStepSuccess);
279       return;
280     case policy::EnrollmentStatus::STATUS_NO_STATE_KEYS:
281       ShowError(IDS_ENTERPRISE_ENROLLMENT_STATUS_NO_STATE_KEYS, false);
282       return;
283     case policy::EnrollmentStatus::STATUS_REGISTRATION_FAILED:
284       // Some special cases for generating a nicer message that's more helpful.
285       switch (status.client_status()) {
286         case policy::DM_STATUS_SERVICE_MANAGEMENT_NOT_SUPPORTED:
287           ShowError(IDS_ENTERPRISE_ENROLLMENT_ACCOUNT_ERROR, true);
288           break;
289         case policy::DM_STATUS_SERVICE_MISSING_LICENSES:
290           ShowError(IDS_ENTERPRISE_ENROLLMENT_MISSING_LICENSES_ERROR, true);
291           break;
292         case policy::DM_STATUS_SERVICE_DEPROVISIONED:
293           ShowError(IDS_ENTERPRISE_ENROLLMENT_DEPROVISIONED_ERROR, true);
294           break;
295         case policy::DM_STATUS_SERVICE_DOMAIN_MISMATCH:
296           ShowError(IDS_ENTERPRISE_ENROLLMENT_DOMAIN_MISMATCH_ERROR, true);
297           break;
298         default:
299           ShowErrorMessage(
300               l10n_util::GetStringFUTF8(
301                   IDS_ENTERPRISE_ENROLLMENT_STATUS_REGISTRATION_FAILED,
302                   policy::FormatDeviceManagementStatus(status.client_status())),
303               true);
304       }
305       return;
306     case policy::EnrollmentStatus::STATUS_ROBOT_AUTH_FETCH_FAILED:
307       ShowError(IDS_ENTERPRISE_ENROLLMENT_ROBOT_AUTH_FETCH_FAILED, true);
308       return;
309     case policy::EnrollmentStatus::STATUS_ROBOT_REFRESH_FETCH_FAILED:
310       ShowError(IDS_ENTERPRISE_ENROLLMENT_ROBOT_REFRESH_FETCH_FAILED, true);
311       return;
312     case policy::EnrollmentStatus::STATUS_ROBOT_REFRESH_STORE_FAILED:
313       ShowError(IDS_ENTERPRISE_ENROLLMENT_ROBOT_REFRESH_STORE_FAILED, true);
314       return;
315     case policy::EnrollmentStatus::STATUS_REGISTRATION_BAD_MODE:
316       ShowError(IDS_ENTERPRISE_ENROLLMENT_STATUS_REGISTRATION_BAD_MODE, false);
317       return;
318     case policy::EnrollmentStatus::STATUS_POLICY_FETCH_FAILED:
319       ShowErrorMessage(
320           l10n_util::GetStringFUTF8(
321               IDS_ENTERPRISE_ENROLLMENT_STATUS_POLICY_FETCH_FAILED,
322               policy::FormatDeviceManagementStatus(status.client_status())),
323           true);
324       return;
325     case policy::EnrollmentStatus::STATUS_VALIDATION_FAILED:
326       ShowErrorMessage(
327           l10n_util::GetStringFUTF8(
328               IDS_ENTERPRISE_ENROLLMENT_STATUS_VALIDATION_FAILED,
329               policy::FormatValidationStatus(status.validation_status())),
330           true);
331       return;
332     case policy::EnrollmentStatus::STATUS_LOCK_ERROR:
333       ShowError(IDS_ENTERPRISE_ENROLLMENT_STATUS_LOCK_ERROR, false);
334       return;
335     case policy::EnrollmentStatus::STATUS_LOCK_TIMEOUT:
336       ShowError(IDS_ENTERPRISE_ENROLLMENT_STATUS_LOCK_TIMEOUT, false);
337       return;
338     case policy::EnrollmentStatus::STATUS_LOCK_WRONG_USER:
339       ShowError(IDS_ENTERPRISE_ENROLLMENT_STATUS_LOCK_WRONG_USER, true);
340       return;
341     case policy::EnrollmentStatus::STATUS_STORE_ERROR:
342       ShowErrorMessage(
343           l10n_util::GetStringFUTF8(
344               IDS_ENTERPRISE_ENROLLMENT_STATUS_STORE_ERROR,
345               policy::FormatStoreStatus(status.store_status(),
346                                         status.validation_status())),
347           true);
348       return;
349     case policy::EnrollmentStatus::STATUS_STORE_TOKEN_AND_ID_FAILED:
350       // This error should not happen for enterprise enrollment.
351       ShowError(IDS_ENTERPRISE_ENROLLMENT_STATUS_STORE_TOKEN_AND_ID_FAILED,
352                 true);
353       NOTREACHED();
354       return;
355   }
356   NOTREACHED();
357 }
358 
359 // EnrollmentScreenHandler BaseScreenHandler implementation -----
360 
Initialize()361 void EnrollmentScreenHandler::Initialize() {
362   if (show_on_init_) {
363     Show();
364     show_on_init_ = false;
365   }
366 }
367 
DeclareLocalizedValues(LocalizedValuesBuilder * builder)368 void EnrollmentScreenHandler::DeclareLocalizedValues(
369     LocalizedValuesBuilder* builder) {
370   builder->Add("oauthEnrollScreenTitle",
371                IDS_ENTERPRISE_ENROLLMENT_SCREEN_TITLE);
372   builder->Add("oauthEnrollDescription", IDS_ENTERPRISE_ENROLLMENT_DESCRIPTION);
373   builder->Add("oauthEnrollReEnrollmentText",
374                IDS_ENTERPRISE_ENROLLMENT_RE_ENROLLMENT_TEXT);
375   builder->Add("oauthEnrollRetry", IDS_ENTERPRISE_ENROLLMENT_RETRY);
376   builder->Add("oauthEnrollCancel", IDS_ENTERPRISE_ENROLLMENT_CANCEL);
377   builder->Add("oauthEnrollDone", IDS_ENTERPRISE_ENROLLMENT_DONE);
378   builder->Add("oauthEnrollSuccess", IDS_ENTERPRISE_ENROLLMENT_SUCCESS);
379   builder->Add("oauthEnrollExplain", IDS_ENTERPRISE_ENROLLMENT_EXPLAIN);
380   builder->Add("oauthEnrollExplainLink",
381                IDS_ENTERPRISE_ENROLLMENT_EXPLAIN_LINK);
382   builder->Add("oauthEnrollExplainButton",
383                IDS_ENTERPRISE_ENROLLMENT_EXPLAIN_BUTTON);
384   builder->Add("oauthEnrollCancelAutoEnrollmentReally",
385                IDS_ENTERPRISE_ENROLLMENT_CANCEL_AUTO_REALLY);
386   builder->Add("oauthEnrollCancelAutoEnrollmentConfirm",
387                IDS_ENTERPRISE_ENROLLMENT_CANCEL_AUTO_CONFIRM);
388   builder->Add("oauthEnrollCancelAutoEnrollmentGoBack",
389                IDS_ENTERPRISE_ENROLLMENT_CANCEL_AUTO_GO_BACK);
390 }
391 
OnBrowsingDataRemoverDone()392 void EnrollmentScreenHandler::OnBrowsingDataRemoverDone() {
393   browsing_data_remover_->RemoveObserver(this);
394   browsing_data_remover_ = NULL;
395 
396   std::vector<base::Closure> callbacks_to_run;
397   callbacks_to_run.swap(auth_reset_callbacks_);
398   for (std::vector<base::Closure>::iterator callback(callbacks_to_run.begin());
399        callback != callbacks_to_run.end(); ++callback) {
400     callback->Run();
401   }
402 }
403 
GetCurrentScreen() const404 OobeUI::Screen EnrollmentScreenHandler::GetCurrentScreen() const {
405   OobeUI::Screen screen = OobeUI::SCREEN_UNKNOWN;
406   OobeUI* oobe_ui = static_cast<OobeUI*>(web_ui()->GetController());
407   if (oobe_ui)
408     screen = oobe_ui->current_screen();
409   return screen;
410 }
411 
IsOnEnrollmentScreen() const412 bool EnrollmentScreenHandler::IsOnEnrollmentScreen() const {
413   return (GetCurrentScreen() == OobeUI::SCREEN_OOBE_ENROLLMENT);
414 }
415 
IsEnrollmentScreenHiddenByError() const416 bool EnrollmentScreenHandler::IsEnrollmentScreenHiddenByError() const {
417   return (GetCurrentScreen() == OobeUI::SCREEN_ERROR_MESSAGE &&
418           error_screen_actor_->parent_screen() ==
419               OobeUI::SCREEN_OOBE_ENROLLMENT);
420 }
421 
422 // TODO(rsorokin): This function is mostly copied from SigninScreenHandler and
423 // should be refactored in the future.
UpdateState(ErrorScreenActor::ErrorReason reason)424 void EnrollmentScreenHandler::UpdateState(
425     ErrorScreenActor::ErrorReason reason) {
426   if (!IsOnEnrollmentScreen() && !IsEnrollmentScreenHiddenByError())
427     return;
428 
429   NetworkStateInformer::State state = network_state_informer_->state();
430   const std::string network_path = network_state_informer_->network_path();
431   const bool is_online = (state == NetworkStateInformer::ONLINE);
432   const bool is_behind_captive_portal =
433       (state == NetworkStateInformer::CAPTIVE_PORTAL);
434   const bool is_frame_error =
435       (frame_error() != net::OK) ||
436       (reason == ErrorScreenActor::ERROR_REASON_FRAME_ERROR);
437 
438   LOG(WARNING) << "EnrollmentScreenHandler::UpdateState(): "
439                << "state=" << NetworkStateInformer::StatusString(state) << ", "
440                << "reason=" << ErrorScreenActor::ErrorReasonString(reason);
441 
442   if (is_online || !is_behind_captive_portal)
443     error_screen_actor_->HideCaptivePortal();
444 
445   if (is_frame_error) {
446     LOG(WARNING) << "Retry page load";
447     // TODO(rsorokin): Too many consecutive reloads.
448     CallJS("doReload");
449   }
450 
451   if (!is_online || is_frame_error)
452     SetupAndShowOfflineMessage(state, reason);
453   else
454     HideOfflineMessage(state, reason);
455 }
456 
SetupAndShowOfflineMessage(NetworkStateInformer::State state,ErrorScreenActor::ErrorReason reason)457 void EnrollmentScreenHandler::SetupAndShowOfflineMessage(
458     NetworkStateInformer::State state,
459     ErrorScreenActor::ErrorReason reason) {
460   const std::string network_path = network_state_informer_->network_path();
461   const bool is_behind_captive_portal = IsBehindCaptivePortal(state, reason);
462   const bool is_proxy_error = IsProxyError(state, reason);
463   const bool is_frame_error =
464       (frame_error() != net::OK) ||
465       (reason == ErrorScreenActor::ERROR_REASON_FRAME_ERROR);
466 
467   if (is_proxy_error) {
468     error_screen_actor_->SetErrorState(ErrorScreen::ERROR_STATE_PROXY,
469                                        std::string());
470   } else if (is_behind_captive_portal) {
471     // Do not bother a user with obsessive captive portal showing. This
472     // check makes captive portal being shown only once: either when error
473     // screen is shown for the first time or when switching from another
474     // error screen (offline, proxy).
475     if (IsOnEnrollmentScreen() || (error_screen_actor_->error_state() !=
476                                    ErrorScreen::ERROR_STATE_PORTAL)) {
477       error_screen_actor_->FixCaptivePortal();
478     }
479     const std::string network_name = GetNetworkName(network_path);
480     error_screen_actor_->SetErrorState(ErrorScreen::ERROR_STATE_PORTAL,
481                                        network_name);
482   } else if (is_frame_error) {
483     error_screen_actor_->SetErrorState(
484         ErrorScreen::ERROR_STATE_AUTH_EXT_TIMEOUT, std::string());
485   } else {
486     error_screen_actor_->SetErrorState(ErrorScreen::ERROR_STATE_OFFLINE,
487                                        std::string());
488   }
489 
490   if (GetCurrentScreen() != OobeUI::SCREEN_ERROR_MESSAGE) {
491     base::DictionaryValue params;
492     const std::string network_type = network_state_informer_->network_type();
493     params.SetString("lastNetworkType", network_type);
494     error_screen_actor_->SetUIState(ErrorScreen::UI_STATE_SIGNIN);
495     error_screen_actor_->Show(OobeUI::SCREEN_OOBE_ENROLLMENT,
496                               &params,
497                               base::Bind(&EnrollmentScreenHandler::DoShow,
498                                          weak_ptr_factory_.GetWeakPtr()));
499   }
500 }
501 
HideOfflineMessage(NetworkStateInformer::State state,ErrorScreenActor::ErrorReason reason)502 void EnrollmentScreenHandler::HideOfflineMessage(
503     NetworkStateInformer::State state,
504     ErrorScreenActor::ErrorReason reason) {
505   if (IsEnrollmentScreenHiddenByError())
506     error_screen_actor_->Hide();
507 }
508 
OnFrameError(const std::string & frame_unique_name)509 void EnrollmentScreenHandler::OnFrameError(
510     const std::string& frame_unique_name) {
511   if (frame_unique_name == "oauth-enroll-signin-frame") {
512     HandleFrameLoadingCompleted(net::ERR_FAILED);
513   }
514 }
515 // EnrollmentScreenHandler, private -----------------------------
516 
HandleRetrieveAuthenticatedUserEmail(double attempt_token)517 void EnrollmentScreenHandler::HandleRetrieveAuthenticatedUserEmail(
518     double attempt_token) {
519   email_retriever_.reset(new AuthenticatedUserEmailRetriever(
520       base::Bind(&EnrollmentScreenHandler::CallJS<double, std::string>,
521                  base::Unretained(this),
522                  "setAuthenticatedUserEmail",
523                  attempt_token),
524       Profile::FromWebUI(web_ui())->GetRequestContext()));
525 }
526 
HandleClose(const std::string & reason)527 void EnrollmentScreenHandler::HandleClose(const std::string& reason) {
528   DCHECK(controller_);
529 
530   if (reason == "cancel" || reason == "autocancel")
531     controller_->OnCancel();
532   else if (reason == "done")
533     controller_->OnConfirmationClosed();
534   else
535     NOTREACHED();
536 }
537 
HandleCompleteLogin(const std::string & user)538 void EnrollmentScreenHandler::HandleCompleteLogin(const std::string& user) {
539   DCHECK(controller_);
540   controller_->OnLoginDone(gaia::SanitizeEmail(user));
541 }
542 
HandleRetry()543 void EnrollmentScreenHandler::HandleRetry() {
544   DCHECK(controller_);
545   controller_->OnRetry();
546 }
547 
HandleFrameLoadingCompleted(int status)548 void EnrollmentScreenHandler::HandleFrameLoadingCompleted(int status) {
549   const net::Error frame_error = static_cast<net::Error>(status);
550   frame_error_ = frame_error;
551 
552   if (network_state_informer_->state() != NetworkStateInformer::ONLINE)
553     return;
554   if (frame_error_)
555     UpdateState(ErrorScreenActor::ERROR_REASON_FRAME_ERROR);
556   else
557     UpdateState(ErrorScreenActor::ERROR_REASON_UPDATE);
558 }
559 
ShowStep(const char * step)560 void EnrollmentScreenHandler::ShowStep(const char* step) {
561   CallJS("showStep", std::string(step));
562 }
563 
ShowError(int message_id,bool retry)564 void EnrollmentScreenHandler::ShowError(int message_id, bool retry) {
565   ShowErrorMessage(l10n_util::GetStringUTF8(message_id), retry);
566 }
567 
ShowErrorMessage(const std::string & message,bool retry)568 void EnrollmentScreenHandler::ShowErrorMessage(const std::string& message,
569                                                bool retry) {
570   CallJS("showError", message, retry);
571 }
572 
ShowWorking(int message_id)573 void EnrollmentScreenHandler::ShowWorking(int message_id) {
574   CallJS("showWorking", l10n_util::GetStringUTF16(message_id));
575 }
576 
OnTokenFetched(const std::string & token,const GoogleServiceAuthError & error)577 void EnrollmentScreenHandler::OnTokenFetched(
578     const std::string& token,
579     const GoogleServiceAuthError& error) {
580   if (!controller_)
581     return;
582 
583   if (error.state() != GoogleServiceAuthError::NONE)
584     controller_->OnAuthError(error);
585   else
586     controller_->OnOAuthTokenAvailable(token);
587 }
588 
DoShow()589 void EnrollmentScreenHandler::DoShow() {
590   base::DictionaryValue screen_data;
591   screen_data.SetString(
592       "signin_url",
593       base::StringPrintf("%s/main.html", extensions::kGaiaAuthExtensionOrigin));
594   screen_data.SetString("gaiaUrl", GaiaUrls::GetInstance()->gaia_url().spec());
595   screen_data.SetString("enrollment_mode",
596                         EnrollmentModeToString(enrollment_mode_));
597   screen_data.SetString("management_domain", management_domain_);
598 
599   ShowScreen(OobeUI::kScreenOobeEnrollment, &screen_data);
600 }
601 
602 }  // namespace chromeos
603