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/screens/host_pairing_screen.h"
6
7#include "base/command_line.h"
8#include "chrome/browser/chromeos/login/wizard_controller.h"
9#include "components/pairing/host_pairing_controller.h"
10
11namespace chromeos {
12
13using namespace host_pairing;
14using namespace pairing_chromeos;
15
16HostPairingScreen::HostPairingScreen(
17    ScreenObserver* observer,
18    HostPairingScreenActor* actor,
19    pairing_chromeos::HostPairingController* controller)
20    : WizardScreen(observer),
21      actor_(actor),
22      controller_(controller),
23      current_stage_(HostPairingController::STAGE_NONE) {
24  actor_->SetDelegate(this);
25  controller_->AddObserver(this);
26}
27
28HostPairingScreen::~HostPairingScreen() {
29  if (actor_)
30    actor_->SetDelegate(NULL);
31  controller_->RemoveObserver(this);
32}
33
34void HostPairingScreen::CommitContextChanges() {
35  if (!context_.HasChanges())
36    return;
37  base::DictionaryValue diff;
38  context_.GetChangesAndReset(&diff);
39  if (actor_)
40    actor_->OnContextChanged(diff);
41}
42
43void HostPairingScreen::PrepareToShow() {
44}
45
46void HostPairingScreen::Show() {
47  if (actor_)
48    actor_->Show();
49  PairingStageChanged(controller_->GetCurrentStage());
50}
51
52void HostPairingScreen::Hide() {
53  if (actor_)
54    actor_->Hide();
55}
56
57std::string HostPairingScreen::GetName() const {
58  return WizardController::kHostPairingScreenName;
59}
60
61void HostPairingScreen::PairingStageChanged(Stage new_stage) {
62  std::string desired_page;
63  switch (new_stage) {
64    case HostPairingController::STAGE_NONE:
65    case HostPairingController::STAGE_INITIALIZATION_ERROR: {
66      break;
67    }
68    case HostPairingController::STAGE_WAITING_FOR_CONTROLLER:
69    case HostPairingController::STAGE_WAITING_FOR_CONTROLLER_AFTER_UPDATE: {
70      desired_page = kPageWelcome;
71      break;
72    }
73    case HostPairingController::STAGE_WAITING_FOR_CODE_CONFIRMATION: {
74      desired_page = kPageCodeConfirmation;
75      context_.SetString(kContextKeyConfirmationCode,
76                         controller_->GetConfirmationCode());
77      break;
78    }
79    case HostPairingController::STAGE_UPDATING: {
80      desired_page = kPageUpdate;
81      context_.SetDouble(kContextKeyUpdateProgress, 0.0);
82      break;
83    }
84    case HostPairingController::STAGE_WAITING_FOR_CREDENTIALS: {
85      desired_page = kPageEnrollmentIntroduction;
86      break;
87    }
88    case HostPairingController::STAGE_ENROLLING: {
89      desired_page = kPageEnrollment;
90      context_.SetString(kContextKeyEnrollmentDomain,
91                         controller_->GetEnrollmentDomain());
92      break;
93    }
94    case HostPairingController::STAGE_ENROLLMENT_ERROR: {
95      desired_page = kPageEnrollmentError;
96      break;
97    }
98    case HostPairingController::STAGE_PAIRING_DONE: {
99      desired_page = kPagePairingDone;
100      break;
101    }
102    case HostPairingController::STAGE_FINISHED: {
103      // This page is closed in EnrollHost.
104      break;
105    }
106  }
107  current_stage_ = new_stage;
108  context_.SetString(kContextKeyDeviceName, controller_->GetDeviceName());
109  context_.SetString(kContextKeyPage, desired_page);
110  CommitContextChanges();
111}
112
113void HostPairingScreen::ConfigureHost(bool accepted_eula,
114                                      const std::string& lang,
115                                      const std::string& timezone,
116                                      bool send_reports,
117                                      const std::string& keyboard_layout) {
118  // TODO(zork): Get configuration from UI and send to Host.
119  // (http://crbug.com/405744)
120}
121
122void HostPairingScreen::EnrollHost(const std::string& auth_token) {
123  controller_->RemoveObserver(this);
124  WizardController::default_controller()->OnEnrollmentAuthTokenReceived(
125      auth_token);
126}
127
128void HostPairingScreen::OnActorDestroyed(HostPairingScreenActor* actor) {
129  if (actor_ == actor)
130    actor_ = NULL;
131}
132
133}  // namespace chromeos
134