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 base::StringPairs kv_pairs
;
40 CHECK(base::SplitStringIntoKeyValuePairs(config
, ':', ',', &kv_pairs
))
41 << "Wrong config format.";
42 std::map
<std::string
, std::string
> dict(kv_pairs
.begin(), kv_pairs
.end());
44 if (dict
.count("async_duration")) {
46 CHECK(base::StringToInt(dict
["async_duration"], &ms
))
47 << "Wrong 'async_duration' format.";
48 async_duration_
= base::TimeDelta::FromMilliseconds(ms
);
51 base::TimeDelta::FromMilliseconds(kDefaultAsyncDurationMs
);
54 start_after_update_
= dict
["start_after_update"] == "1";
56 enrollment_should_fail_
= dict
["fail_enrollment"] == "1";
58 if (dict
.count("code")) {
59 confirmation_code_
= dict
["code"];
61 confirmation_code_
.clear();
62 for (size_t i
= 0; i
< kCodeLength
; ++i
)
63 confirmation_code_
.push_back(base::RandInt('0', '9'));
65 CHECK(confirmation_code_
.length() == kCodeLength
&&
66 confirmation_code_
.find_first_not_of("0123456789") == std::string::npos
)
67 << "Wrong 'code' format.";
70 dict
.count("device_name") ? dict
["device_name"] : "Chromebox-01";
72 enrollment_domain_
= dict
.count("domain") ? dict
["domain"] : "example.com";
75 void FakeHostPairingController::ChangeStage(Stage new_stage
) {
76 if (current_stage_
== new_stage
)
78 current_stage_
= new_stage
;
79 FOR_EACH_OBSERVER(Observer
, observers_
, PairingStageChanged(new_stage
));
82 void FakeHostPairingController::ChangeStageLater(Stage new_stage
) {
83 base::MessageLoop::current()->PostDelayedTask(
85 base::Bind(&FakeHostPairingController::ChangeStage
,
86 base::Unretained(this),
91 void FakeHostPairingController::AddObserver(Observer
* observer
) {
92 observers_
.AddObserver(observer
);
95 void FakeHostPairingController::RemoveObserver(Observer
* observer
) {
96 observers_
.RemoveObserver(observer
);
99 HostPairingController::Stage
FakeHostPairingController::GetCurrentStage() {
100 return current_stage_
;
103 void FakeHostPairingController::StartPairing() {
104 CHECK(current_stage_
== STAGE_NONE
);
105 if (start_after_update_
) {
106 ChangeStage(STAGE_WAITING_FOR_CONTROLLER_AFTER_UPDATE
);
108 ChangeStage(STAGE_WAITING_FOR_CONTROLLER
);
112 std::string
FakeHostPairingController::GetDeviceName() {
116 std::string
FakeHostPairingController::GetConfirmationCode() {
117 CHECK(current_stage_
== STAGE_WAITING_FOR_CODE_CONFIRMATION
);
118 return confirmation_code_
;
121 std::string
FakeHostPairingController::GetEnrollmentDomain() {
122 return enrollment_domain_
;
125 void FakeHostPairingController::OnUpdateStatusChanged(
126 UpdateStatus update_status
) {
129 void FakeHostPairingController::OnEnrollmentStatusChanged(
130 EnrollmentStatus enrollment_status
) {
133 void FakeHostPairingController::SetPermanentId(
134 const std::string
& permanent_id
) {
137 void FakeHostPairingController::PairingStageChanged(Stage new_stage
) {
139 case STAGE_WAITING_FOR_CONTROLLER
: {
140 ChangeStageLater(STAGE_WAITING_FOR_CODE_CONFIRMATION
);
143 case STAGE_WAITING_FOR_CODE_CONFIRMATION
: {
144 ChangeStageLater(STAGE_WAITING_FOR_CONTROLLER_AFTER_UPDATE
);
147 case STAGE_WAITING_FOR_CONTROLLER_AFTER_UPDATE
: {
148 ChangeStageLater(STAGE_WAITING_FOR_CREDENTIALS
);
151 case STAGE_WAITING_FOR_CREDENTIALS
: {
152 ChangeStageLater(STAGE_ENROLLING
);
155 case STAGE_ENROLLING
: {
156 if (enrollment_should_fail_
) {
157 enrollment_should_fail_
= false;
158 ChangeStageLater(STAGE_ENROLLMENT_ERROR
);
160 ChangeStageLater(STAGE_ENROLLMENT_SUCCESS
);
164 case STAGE_ENROLLMENT_ERROR
: {
165 ChangeStageLater(STAGE_WAITING_FOR_CONTROLLER_AFTER_UPDATE
);
168 case STAGE_ENROLLMENT_SUCCESS
: {
169 ChangeStageLater(STAGE_FINISHED
);
176 } // namespace pairing_chromeos