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_controller_pairing_controller.h"
10 #include "base/logging.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/rand_util.h"
13 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/string_util.h"
17 namespace pairing_chromeos
{
19 FakeControllerPairingController::FakeControllerPairingController(
20 const std::string
& config
)
21 : current_stage_(STAGE_NONE
),
22 should_fail_on_connecting_(false),
23 connection_lost_begin_(STAGE_NONE
),
24 connection_lost_end_(STAGE_NONE
),
25 enrollment_should_fail_(false) {
30 FakeControllerPairingController::~FakeControllerPairingController() {
34 void FakeControllerPairingController::ApplyConfig(const std::string
& config
) {
35 typedef std::vector
<std::string
> Tokens
;
37 base::StringPairs kv_pairs
;
38 CHECK(base::SplitStringIntoKeyValuePairs(config
, ':', ',', &kv_pairs
))
39 << "Wrong config format.";
40 std::map
<std::string
, std::string
> dict(kv_pairs
.begin(), kv_pairs
.end());
42 if (dict
.count("async_duration")) {
44 CHECK(base::StringToInt(dict
["async_duration"], &ms
))
45 << "Wrong 'async_duration' format.";
46 async_duration_
= base::TimeDelta::FromMilliseconds(ms
);
48 async_duration_
= base::TimeDelta::FromMilliseconds(3000);
51 should_fail_on_connecting_
=
52 dict
.count("fail_connecting") && (dict
["fail_connecting"] == "1");
54 enrollment_should_fail_
=
55 dict
.count("fail_enrollment") && (dict
["fail_enrollment"] == "1");
57 if (dict
.count("connection_lost")) {
58 Tokens lost_begin_end
;
59 CHECK(Tokenize(dict
["connection_lost"], "-", &lost_begin_end
) == 2)
60 << "Wrong 'connection_lost' format.";
63 CHECK(base::StringToInt(lost_begin_end
[0], &begin
) &&
64 base::StringToInt(lost_begin_end
[1], &end
))
65 << "Wrong 'connection_lost' format.";
66 CHECK((begin
== 0 && end
== 0) ||
67 (STAGE_WAITING_FOR_CODE_CONFIRMATION
<= begin
&& begin
<= end
&&
68 end
<= STAGE_HOST_ENROLLMENT_ERROR
))
69 << "Wrong 'connection_lost' interval.";
70 connection_lost_begin_
= static_cast<Stage
>(begin
);
71 connection_lost_end_
= static_cast<Stage
>(end
);
73 connection_lost_begin_
= connection_lost_end_
= STAGE_NONE
;
76 if (!dict
.count("discovery")) {
78 "F-Device_1~F-Device_5~F-Device_3~L-Device_3~L-Device_1~F-Device_1";
80 base::StringPairs events
;
82 base::SplitStringIntoKeyValuePairs(dict
["discovery"], '-', '~', &events
))
83 << "Wrong 'discovery' format.";
84 DiscoveryScenario scenario
;
85 for (const auto& event
: events
) {
86 const std::string
& type
= event
.first
;
87 const std::string
& device_id
= event
.second
;
88 CHECK(type
== "F" || type
== "L" || type
== "N")
89 << "Wrong discovery event type.";
90 CHECK(!device_id
.empty() || type
== "N") << "Empty device ID.";
91 scenario
.push_back(DiscoveryEvent(
92 type
== "F" ? DEVICE_FOUND
: type
== "L" ? DEVICE_LOST
: NOTHING_FOUND
,
95 SetDiscoveryScenario(scenario
);
97 preset_confirmation_code_
= dict
["code"];
98 CHECK(preset_confirmation_code_
.empty() ||
99 (preset_confirmation_code_
.length() == 6 &&
100 preset_confirmation_code_
.find_first_not_of("0123456789") ==
102 << "Wrong 'code' format.";
105 void FakeControllerPairingController::SetShouldFailOnConnecting() {
106 should_fail_on_connecting_
= true;
109 void FakeControllerPairingController::SetShouldLoseConnection(Stage stage_begin
,
111 connection_lost_begin_
= stage_begin
;
112 connection_lost_end_
= stage_end
;
115 void FakeControllerPairingController::SetEnrollmentShouldFail() {
116 enrollment_should_fail_
= true;
119 void FakeControllerPairingController::SetDiscoveryScenario(
120 const DiscoveryScenario
& discovery_scenario
) {
121 discovery_scenario_
= discovery_scenario
;
122 // Check that scenario is valid.
123 std::set
<std::string
> devices
;
124 for (DiscoveryScenario::const_iterator event
= discovery_scenario_
.begin();
125 event
!= discovery_scenario_
.end();
127 switch (event
->first
) {
129 devices
.insert(event
->second
);
133 CHECK(devices
.count(event
->second
));
134 devices
.erase(event
->second
);
137 case NOTHING_FOUND
: {
138 CHECK(++event
== discovery_scenario_
.end());
145 void FakeControllerPairingController::AddObserver(Observer
* observer
) {
146 observers_
.AddObserver(observer
);
149 void FakeControllerPairingController::RemoveObserver(Observer
* observer
) {
150 observers_
.RemoveObserver(observer
);
153 ControllerPairingController::Stage
154 FakeControllerPairingController::GetCurrentStage() {
155 return current_stage_
;
158 void FakeControllerPairingController::StartPairing() {
159 CHECK(current_stage_
== STAGE_NONE
);
160 ChangeStage(STAGE_DEVICES_DISCOVERY
);
163 ControllerPairingController::DeviceIdList
164 FakeControllerPairingController::GetDiscoveredDevices() {
165 CHECK(current_stage_
== STAGE_DEVICES_DISCOVERY
);
166 return DeviceIdList(discovered_devices_
.begin(), discovered_devices_
.end());
169 void FakeControllerPairingController::ChooseDeviceForPairing(
170 const std::string
& device_id
) {
171 CHECK(current_stage_
== STAGE_DEVICES_DISCOVERY
);
172 CHECK(discovered_devices_
.count(device_id
));
173 choosen_device_
= device_id
;
174 ChangeStage(STAGE_ESTABLISHING_CONNECTION
);
177 void FakeControllerPairingController::RepeatDiscovery() {
178 CHECK(current_stage_
== STAGE_DEVICE_NOT_FOUND
||
179 current_stage_
== STAGE_ESTABLISHING_CONNECTION_ERROR
||
180 current_stage_
== STAGE_HOST_ENROLLMENT_ERROR
);
181 ChangeStage(STAGE_DEVICES_DISCOVERY
);
184 std::string
FakeControllerPairingController::GetConfirmationCode() {
185 CHECK(current_stage_
== STAGE_WAITING_FOR_CODE_CONFIRMATION
);
186 if (confirmation_code_
.empty()) {
187 if (preset_confirmation_code_
.empty()) {
188 for (int i
= 0; i
< 6; ++i
)
189 confirmation_code_
.push_back(base::RandInt('0', '9'));
191 confirmation_code_
= preset_confirmation_code_
;
194 return confirmation_code_
;
197 void FakeControllerPairingController::SetConfirmationCodeIsCorrect(
199 CHECK(current_stage_
== STAGE_WAITING_FOR_CODE_CONFIRMATION
);
201 ChangeStage(STAGE_HOST_UPDATE_IN_PROGRESS
);
203 ChangeStage(STAGE_DEVICES_DISCOVERY
);
206 void FakeControllerPairingController::SetHostConfiguration(
208 const std::string
& lang
,
209 const std::string
& timezone
,
211 const std::string
& keyboard_layout
) {
214 void FakeControllerPairingController::OnAuthenticationDone(
215 const std::string
& domain
,
216 const std::string
& auth_token
) {
217 CHECK(current_stage_
== STAGE_WAITING_FOR_CREDENTIALS
);
218 ChangeStage(STAGE_HOST_ENROLLMENT_IN_PROGRESS
);
221 void FakeControllerPairingController::StartSession() {
222 CHECK(current_stage_
== STAGE_HOST_ENROLLMENT_SUCCESS
);
223 ChangeStage(STAGE_FINISHED
);
226 void FakeControllerPairingController::ChangeStage(Stage new_stage
) {
227 if (current_stage_
== new_stage
)
229 current_stage_
= new_stage
;
230 FOR_EACH_OBSERVER(Observer
, observers_
, PairingStageChanged(new_stage
));
233 void FakeControllerPairingController::ChangeStageLater(Stage new_stage
) {
234 base::MessageLoop::current()->PostDelayedTask(
236 base::Bind(&FakeControllerPairingController::ChangeStage
,
237 base::Unretained(this),
242 void FakeControllerPairingController::ExecuteDiscoveryEvent(
243 size_t event_position
) {
244 if (current_stage_
!= STAGE_DEVICES_DISCOVERY
)
246 CHECK(event_position
< discovery_scenario_
.size());
247 const DiscoveryEvent
& event
= discovery_scenario_
[event_position
];
248 switch (event
.first
) {
250 DeviceFound(event
.second
);
254 DeviceLost(event
.second
);
257 case NOTHING_FOUND
: {
258 ChangeStage(STAGE_DEVICE_NOT_FOUND
);
262 if (++event_position
== discovery_scenario_
.size()) {
265 base::MessageLoop::current()->PostDelayedTask(
267 base::Bind(&FakeControllerPairingController::ExecuteDiscoveryEvent
,
268 base::Unretained(this),
273 void FakeControllerPairingController::DeviceFound(
274 const std::string
& device_id
) {
275 CHECK(current_stage_
== STAGE_DEVICES_DISCOVERY
);
276 discovered_devices_
.insert(device_id
);
277 FOR_EACH_OBSERVER(Observer
, observers_
, DiscoveredDevicesListChanged());
280 void FakeControllerPairingController::DeviceLost(const std::string
& device_id
) {
281 CHECK(current_stage_
== STAGE_DEVICES_DISCOVERY
);
282 discovered_devices_
.erase(device_id
);
283 FOR_EACH_OBSERVER(Observer
, observers_
, DiscoveredDevicesListChanged());
286 void FakeControllerPairingController::PairingStageChanged(Stage new_stage
) {
287 Stage next_stage
= STAGE_NONE
;
289 case STAGE_DEVICES_DISCOVERY
: {
290 discovered_devices_
.clear();
291 base::MessageLoop::current()->PostDelayedTask(
293 base::Bind(&FakeControllerPairingController::ExecuteDiscoveryEvent
,
294 base::Unretained(this),
299 case STAGE_ESTABLISHING_CONNECTION
: {
300 if (should_fail_on_connecting_
) {
301 next_stage
= STAGE_ESTABLISHING_CONNECTION_ERROR
;
302 should_fail_on_connecting_
= false;
304 confirmation_code_
.clear();
305 next_stage
= STAGE_WAITING_FOR_CODE_CONFIRMATION
;
309 case STAGE_HOST_UPDATE_IN_PROGRESS
: {
310 next_stage
= STAGE_WAITING_FOR_CREDENTIALS
;
313 case STAGE_HOST_ENROLLMENT_IN_PROGRESS
: {
314 if (enrollment_should_fail_
) {
315 enrollment_should_fail_
= false;
316 next_stage
= STAGE_HOST_ENROLLMENT_ERROR
;
318 next_stage
= STAGE_HOST_ENROLLMENT_SUCCESS
;
322 case STAGE_HOST_CONNECTION_LOST
: {
323 next_stage
= connection_lost_end_
;
324 connection_lost_end_
= STAGE_NONE
;
330 if (new_stage
== connection_lost_begin_
) {
331 connection_lost_begin_
= STAGE_NONE
;
332 next_stage
= STAGE_HOST_CONNECTION_LOST
;
334 if (next_stage
!= STAGE_NONE
)
335 ChangeStageLater(next_stage
);
338 void FakeControllerPairingController::DiscoveredDevicesListChanged() {
341 } // namespace pairing_chromeos