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.
5 #include "components/pairing/fake_host_pairing_controller.h"
10 #include "base/bind.h"
11 #include "base/logging.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/rand_util.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_split.h"
19 const int kDefaultAsyncDurationMs = 3000;
20 const size_t kCodeLength = 6;
24 namespace pairing_chromeos {
26 FakeHostPairingController::FakeHostPairingController(const std::string& config)
27 : current_stage_(STAGE_NONE),
28 enrollment_should_fail_(false),
29 start_after_update_(false) {
34 FakeHostPairingController::~FakeHostPairingController() {
38 void FakeHostPairingController::ApplyConfig(const std::string& config) {
39 typedef std::vector<std::string> Tokens;
41 base::StringPairs kv_pairs;
42 CHECK(base::SplitStringIntoKeyValuePairs(config, ':', ',', &kv_pairs))
43 << "Wrong config format.";
44 std::map<std::string, std::string> dict(kv_pairs.begin(), kv_pairs.end());
46 if (dict.count("async_duration")) {
48 CHECK(base::StringToInt(dict["async_duration"], &ms))
49 << "Wrong 'async_duration' format.";
50 async_duration_ = base::TimeDelta::FromMilliseconds(ms);
53 base::TimeDelta::FromMilliseconds(kDefaultAsyncDurationMs);
56 start_after_update_ = dict["start_after_update"] == "1";
58 enrollment_should_fail_ = dict["fail_enrollment"] == "1";
60 if (dict.count("code")) {
61 confirmation_code_ = dict["code"];
63 confirmation_code_.clear();
64 for (size_t i = 0; i < kCodeLength; ++i)
65 confirmation_code_.push_back(base::RandInt('0', '9'));
67 CHECK(confirmation_code_.length() == kCodeLength &&
68 confirmation_code_.find_first_not_of("0123456789") == std::string::npos)
69 << "Wrong 'code' format.";
72 dict.count("device_name") ? dict["device_name"] : "Chromebox-01";
74 enrollment_domain_ = dict.count("domain") ? dict["domain"] : "example.com";
77 void FakeHostPairingController::ChangeStage(Stage new_stage) {
78 if (current_stage_ == new_stage)
80 current_stage_ = new_stage;
81 FOR_EACH_OBSERVER(Observer, observers_, PairingStageChanged(new_stage));
84 void FakeHostPairingController::ChangeStageLater(Stage new_stage) {
85 base::MessageLoop::current()->PostDelayedTask(
87 base::Bind(&FakeHostPairingController::ChangeStage,
88 base::Unretained(this),
93 void FakeHostPairingController::AddObserver(Observer* observer) {
94 observers_.AddObserver(observer);
97 void FakeHostPairingController::RemoveObserver(Observer* observer) {
98 observers_.RemoveObserver(observer);
101 HostPairingController::Stage FakeHostPairingController::GetCurrentStage() {
102 return current_stage_;
105 void FakeHostPairingController::StartPairing() {
106 CHECK(current_stage_ == STAGE_NONE);
107 if (start_after_update_) {
108 ChangeStage(STAGE_WAITING_FOR_CONTROLLER_AFTER_UPDATE);
110 ChangeStage(STAGE_WAITING_FOR_CONTROLLER);
114 std::string FakeHostPairingController::GetDeviceName() {
118 std::string FakeHostPairingController::GetConfirmationCode() {
119 CHECK(current_stage_ == STAGE_WAITING_FOR_CODE_CONFIRMATION);
120 return confirmation_code_;
123 std::string FakeHostPairingController::GetEnrollmentDomain() {
124 return enrollment_domain_;
127 void FakeHostPairingController::OnUpdateStatusChanged(
128 UpdateStatus update_status) {
131 void FakeHostPairingController::OnEnrollmentStatusChanged(
132 EnrollmentStatus enrollment_status) {
135 void FakeHostPairingController::PairingStageChanged(Stage new_stage) {
137 case STAGE_WAITING_FOR_CONTROLLER: {
138 ChangeStageLater(STAGE_WAITING_FOR_CODE_CONFIRMATION);
141 case STAGE_WAITING_FOR_CODE_CONFIRMATION: {
142 ChangeStageLater(STAGE_WAITING_FOR_CONTROLLER_AFTER_UPDATE);
145 case STAGE_WAITING_FOR_CONTROLLER_AFTER_UPDATE: {
146 ChangeStageLater(STAGE_WAITING_FOR_CREDENTIALS);
149 case STAGE_WAITING_FOR_CREDENTIALS: {
150 ChangeStageLater(STAGE_ENROLLING);
153 case STAGE_ENROLLING: {
154 if (enrollment_should_fail_) {
155 enrollment_should_fail_ = false;
156 ChangeStageLater(STAGE_ENROLLMENT_ERROR);
158 ChangeStageLater(STAGE_ENROLLMENT_SUCCESS);
162 case STAGE_ENROLLMENT_ERROR: {
163 ChangeStageLater(STAGE_WAITING_FOR_CONTROLLER_AFTER_UPDATE);
166 case STAGE_ENROLLMENT_SUCCESS: {
167 ChangeStageLater(STAGE_FINISHED);
174 void FakeHostPairingController::ConfigureHost(
176 const std::string& lang,
177 const std::string& timezone,
179 const std::string& keyboard_layout) {
182 void FakeHostPairingController::EnrollHost(const std::string& auth_token) {
185 } // namespace pairing_chromeos