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/screens/controller_pairing_screen.h" |
| 6 |
| 7 #include "base/values.h" |
| 8 #include "base/strings/string_util.h" |
| 9 #include "chrome/browser/chromeos/login/auth/user_context.h" |
| 10 #include "chrome/browser/chromeos/login/wizard_controller.h" |
| 11 #include "chromeos/pairing/fake_controller_pairing_flow.h" |
| 12 |
| 13 namespace chromeos { |
| 14 |
| 15 ControllerPairingScreen::ControllerPairingScreen( |
| 16 ScreenObserver* observer, |
| 17 ControllerPairingScreenActor* actor) |
| 18 : WizardScreen(observer), |
| 19 actor_(actor), |
| 20 flow_(new FakeControllerPairingFlow()), |
| 21 current_stage_(ControllerPairingFlow::STAGE_NONE), |
| 22 device_preselected_(false) { |
| 23 actor_->SetDelegate(this); |
| 24 flow_->AddObserver(this); |
| 25 } |
| 26 |
| 27 ControllerPairingScreen::~ControllerPairingScreen() { |
| 28 if (actor_) |
| 29 actor_->SetDelegate(NULL); |
| 30 flow_->RemoveObserver(this); |
| 31 } |
| 32 |
| 33 void ControllerPairingScreen::CommitContextChanges() { |
| 34 base::DictionaryValue diff; |
| 35 context_.GetChangesAndReset(&diff); |
| 36 if (actor_) |
| 37 actor_->OnContextChanged(diff); |
| 38 } |
| 39 |
| 40 bool ControllerPairingScreen::ExpectStageIs(Stage stage) const { |
| 41 DCHECK(stage == current_stage_); |
| 42 if (current_stage_ != stage) |
| 43 LOG(ERROR) << "Incorrect stage. Expected: " << stage |
| 44 << ", current stage: " << current_stage_; |
| 45 return stage == current_stage_; |
| 46 } |
| 47 |
| 48 void ControllerPairingScreen::PrepareToShow() { |
| 49 } |
| 50 |
| 51 void ControllerPairingScreen::Show() { |
| 52 if (actor_) |
| 53 actor_->Show(); |
| 54 flow_->StartFlow(); |
| 55 } |
| 56 |
| 57 void ControllerPairingScreen::Hide() { |
| 58 if (actor_) |
| 59 actor_->Hide(); |
| 60 } |
| 61 |
| 62 std::string ControllerPairingScreen::GetName() const { |
| 63 return WizardController::kControllerPairingScreenName; |
| 64 } |
| 65 |
| 66 // Overridden from ControllerPairingFlow::Observer: |
| 67 void ControllerPairingScreen::PairingStageChanged(Stage new_stage) { |
| 68 using namespace chromeos::controller_pairing; |
| 69 DCHECK(new_stage != current_stage_); |
| 70 |
| 71 std::string desired_page; |
| 72 switch (new_stage) { |
| 73 case Stage::STAGE_DEVICES_DISCOVERY: { |
| 74 desired_page = kPageDevicesDiscovery; |
| 75 context_.SetString(kContextKeySelectedDevice, ""); |
| 76 device_preselected_ = false; |
| 77 break; |
| 78 } |
| 79 case Stage::STAGE_DEVICE_NOT_FOUND: { |
| 80 desired_page = kPageDeviceNotFound; |
| 81 break; |
| 82 } |
| 83 case Stage::STAGE_ESTABLISHING_CONNECTION: { |
| 84 desired_page = kPageEstablishingConnection; |
| 85 break; |
| 86 } |
| 87 case Stage::STAGE_ESTABLISHING_CONNECTION_ERROR: { |
| 88 desired_page = kPageEstablishingConnectionError; |
| 89 break; |
| 90 } |
| 91 case Stage::STAGE_WAITING_FOR_CODE_CONFIRMATION: { |
| 92 desired_page = kPageCodeConfirmation; |
| 93 context_.SetString(kContextKeyConfirmationCode, |
| 94 flow_->GetConfirmationCode()); |
| 95 break; |
| 96 } |
| 97 case Stage::STAGE_HOST_UPDATE_IN_PROGRESS: { |
| 98 desired_page = kPageHostUpdate; |
| 99 break; |
| 100 } |
| 101 case Stage::STAGE_HOST_CONNECTION_LOST: { |
| 102 desired_page = kPageHostConnectionLost; |
| 103 break; |
| 104 } |
| 105 case Stage::STAGE_WAITING_FOR_CREDENTIALS: { |
| 106 desired_page = kPageEnrlollmentIntroduction; |
| 107 break; |
| 108 } |
| 109 case Stage::STAGE_HOST_ENROLLMENT_IN_PROGRESS: { |
| 110 desired_page = kPageHostEnrollment; |
| 111 break; |
| 112 } |
| 113 case Stage::STAGE_HOST_ENROLLMENT_ERROR: { |
| 114 desired_page = kPageHostEnrollmentError; |
| 115 break; |
| 116 } |
| 117 case Stage::STAGE_PAIRING_DONE: { |
| 118 desired_page = kPagePairingDone; |
| 119 break; |
| 120 } |
| 121 case Stage::STAGE_STARTING_SESSION: { |
| 122 // TODO(dzhioev): Is it needed? |
| 123 break; |
| 124 } |
| 125 case Stage::STAGE_FINISHED: { |
| 126 get_screen_observer()->OnExit( |
| 127 WizardController::CONTROLLER_PAIRING_FINISHED); |
| 128 break; |
| 129 } |
| 130 default: |
| 131 NOTREACHED(); |
| 132 } |
| 133 current_stage_ = new_stage; |
| 134 context_.SetString(controller_pairing::kContextKeyPage, desired_page); |
| 135 context_.SetBoolean(controller_pairing::kContextKeyControlsDisabled, false); |
| 136 CommitContextChanges(); |
| 137 } |
| 138 |
| 139 void ControllerPairingScreen::DiscoveredDevicesListChanged() { |
| 140 if (!ExpectStageIs(Stage::STAGE_DEVICES_DISCOVERY)) |
| 141 return; |
| 142 ControllerPairingFlow::DeviceIdList devices = flow_->GetDiscoveredDevices(); |
| 143 std::sort(devices.begin(), devices.end()); |
| 144 std::string list = JoinString(devices, '\0'); |
| 145 context_.SetString(controller_pairing::kContextKeyDevices, list); |
| 146 context_.SetString(controller_pairing::kContextKeyPage, |
| 147 devices.empty() ? controller_pairing::kPageDevicesDiscovery |
| 148 : controller_pairing::kPageDeviceSelect); |
| 149 if (devices.empty()) { |
| 150 device_preselected_ = false; |
| 151 } else if (!device_preselected_) { |
| 152 context_.SetString(controller_pairing::kContextKeySelectedDevice, |
| 153 devices.front()); |
| 154 device_preselected_ = true; |
| 155 } |
| 156 CommitContextChanges(); |
| 157 } |
| 158 |
| 159 void ControllerPairingScreen::OnActorDestroyed( |
| 160 ControllerPairingScreenActor* actor) { |
| 161 if (actor_ == actor) |
| 162 actor_ = NULL; |
| 163 } |
| 164 |
| 165 // Overridden from ControllerPairingView::Delegate: |
| 166 void ControllerPairingScreen::OnButtonClicked(const std::string& action) { |
| 167 using namespace controller_pairing; |
| 168 |
| 169 bool disable_controls = true; |
| 170 if (action == kActionChooseDevice) { |
| 171 flow_->ChooseDeviceForPairing( |
| 172 context_.GetString(kContextKeySelectedDevice)); |
| 173 } else if (action == kActionRepeatDiscovery) { |
| 174 flow_->RepeatDiscovery(); |
| 175 } else if (action == kActionAcceptCode) { |
| 176 flow_->SetConfirmationCodeIsCorrect(true); |
| 177 } else if (action == kActionRejectCode) { |
| 178 flow_->SetConfirmationCodeIsCorrect(false); |
| 179 } else if (action == kActionProceedToAuthentication) { |
| 180 context_.SetString(kContextKeyPage, kPageAuthentication); |
| 181 disable_controls = false; |
| 182 } else if (action == kActionEnroll) { |
| 183 UserContext user_context(context_.GetString(kContextKeyAccountId)); |
| 184 flow_->OnAuthenticationDone(user_context, actor_->GetBrowserContext()); |
| 185 } else if (action == kActionStartSession) { |
| 186 flow_->StartSession(); |
| 187 } |
| 188 context_.SetBoolean(kContextKeyControlsDisabled, disable_controls); |
| 189 CommitContextChanges(); |
| 190 } |
| 191 |
| 192 void ControllerPairingScreen::OnScreenContextChanged( |
| 193 const base::DictionaryValue& diff) { |
| 194 context_.ApplyChanges(diff, NULL); |
| 195 } |
| 196 |
| 197 } // namespace chromeos |
OLD | NEW |