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 "chromeos/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"
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 (base::StringPairs::const_iterator event
= events
.begin();
86 event
!= events
.end();
88 std::string type
= event
->first
;
89 std::string device_id
= event
->second
;
90 CHECK(type
== "F" || type
== "L" || type
== "N")
91 << "Wrong discovery event type.";
92 CHECK(!device_id
.empty() || type
== "N") << "Empty device ID.";
93 scenario
.push_back(DiscoveryEvent(
94 type
== "F" ? DEVICE_FOUND
: type
== "L" ? DEVICE_LOST
: NOTHING_FOUND
,
97 SetDiscoveryScenario(scenario
);
99 preset_confirmation_code_
= dict
["code"];
100 CHECK(preset_confirmation_code_
.empty() ||
101 (preset_confirmation_code_
.length() == 6 &&
102 preset_confirmation_code_
.find_first_not_of("0123456789") ==
104 << "Wrong 'code' format.";
107 void FakeControllerPairingController::SetShouldFailOnConnecting() {
108 should_fail_on_connecting_
= true;
111 void FakeControllerPairingController::SetShouldLoseConnection(Stage stage_begin
,
113 connection_lost_begin_
= stage_begin
;
114 connection_lost_end_
= stage_end
;
117 void FakeControllerPairingController::SetEnrollmentShouldFail() {
118 enrollment_should_fail_
= true;
121 void FakeControllerPairingController::SetDiscoveryScenario(
122 const DiscoveryScenario
& discovery_scenario
) {
123 discovery_scenario_
= discovery_scenario
;
124 // Check that scenario is valid.
125 std::set
<std::string
> devices
;
126 for (DiscoveryScenario::const_iterator event
= discovery_scenario_
.begin();
127 event
!= discovery_scenario_
.end();
129 switch (event
->first
) {
131 devices
.insert(event
->second
);
135 CHECK(devices
.count(event
->second
));
136 devices
.erase(event
->second
);
139 case NOTHING_FOUND
: {
140 CHECK(++event
== discovery_scenario_
.end());
147 void FakeControllerPairingController::AddObserver(Observer
* observer
) {
148 observers_
.AddObserver(observer
);
151 void FakeControllerPairingController::RemoveObserver(Observer
* observer
) {
152 observers_
.RemoveObserver(observer
);
155 ControllerPairingController::Stage
156 FakeControllerPairingController::GetCurrentStage() {
157 return current_stage_
;
160 void FakeControllerPairingController::StartPairing() {
161 CHECK(current_stage_
== STAGE_NONE
);
162 ChangeStage(STAGE_DEVICES_DISCOVERY
);
165 ControllerPairingController::DeviceIdList
166 FakeControllerPairingController::GetDiscoveredDevices() {
167 CHECK(current_stage_
== STAGE_DEVICES_DISCOVERY
);
168 return DeviceIdList(discovered_devices_
.begin(), discovered_devices_
.end());
171 void FakeControllerPairingController::ChooseDeviceForPairing(
172 const std::string
& device_id
) {
173 CHECK(current_stage_
== STAGE_DEVICES_DISCOVERY
);
174 CHECK(discovered_devices_
.count(device_id
));
175 choosen_device_
= device_id
;
176 ChangeStage(STAGE_ESTABLISHING_CONNECTION
);
179 void FakeControllerPairingController::RepeatDiscovery() {
180 CHECK(current_stage_
== STAGE_DEVICE_NOT_FOUND
||
181 current_stage_
== STAGE_ESTABLISHING_CONNECTION_ERROR
||
182 current_stage_
== STAGE_HOST_ENROLLMENT_ERROR
);
183 ChangeStage(STAGE_DEVICES_DISCOVERY
);
186 std::string
FakeControllerPairingController::GetConfirmationCode() {
187 CHECK(current_stage_
== STAGE_WAITING_FOR_CODE_CONFIRMATION
);
188 if (confirmation_code_
.empty()) {
189 if (preset_confirmation_code_
.empty()) {
190 for (int i
= 0; i
< 6; ++i
)
191 confirmation_code_
.push_back(base::RandInt('0', '9'));
193 confirmation_code_
= preset_confirmation_code_
;
196 return confirmation_code_
;
199 void FakeControllerPairingController::SetConfirmationCodeIsCorrect(
201 CHECK(current_stage_
== STAGE_WAITING_FOR_CODE_CONFIRMATION
);
203 ChangeStage(STAGE_HOST_UPDATE_IN_PROGRESS
);
205 ChangeStage(STAGE_DEVICES_DISCOVERY
);
208 void FakeControllerPairingController::OnAuthenticationDone(
209 const chromeos::UserContext
& user_context
,
210 content::BrowserContext
* browser_context
) {
211 CHECK(current_stage_
== STAGE_WAITING_FOR_CREDENTIALS
);
212 ChangeStage(STAGE_HOST_ENROLLMENT_IN_PROGRESS
);
215 void FakeControllerPairingController::StartSession() {
216 CHECK(current_stage_
== STAGE_PAIRING_DONE
);
217 ChangeStage(STAGE_FINISHED
);
220 void FakeControllerPairingController::ChangeStage(Stage new_stage
) {
221 if (current_stage_
== new_stage
)
223 current_stage_
= new_stage
;
224 FOR_EACH_OBSERVER(Observer
, observers_
, PairingStageChanged(new_stage
));
227 void FakeControllerPairingController::ChangeStageLater(Stage new_stage
) {
228 base::MessageLoop::current()->PostDelayedTask(
230 base::Bind(&FakeControllerPairingController::ChangeStage
,
231 base::Unretained(this),
236 void FakeControllerPairingController::ExecuteDiscoveryEvent(
237 size_t event_position
) {
238 if (current_stage_
!= STAGE_DEVICES_DISCOVERY
)
240 CHECK(event_position
< discovery_scenario_
.size());
241 const DiscoveryEvent
& event
= discovery_scenario_
[event_position
];
242 switch (event
.first
) {
244 DeviceFound(event
.second
);
248 DeviceLost(event
.second
);
251 case NOTHING_FOUND
: {
252 ChangeStage(STAGE_DEVICE_NOT_FOUND
);
256 if (++event_position
== discovery_scenario_
.size()) {
259 base::MessageLoop::current()->PostDelayedTask(
261 base::Bind(&FakeControllerPairingController::ExecuteDiscoveryEvent
,
262 base::Unretained(this),
267 void FakeControllerPairingController::DeviceFound(
268 const std::string
& device_id
) {
269 CHECK(current_stage_
== STAGE_DEVICES_DISCOVERY
);
270 discovered_devices_
.insert(device_id
);
271 FOR_EACH_OBSERVER(Observer
, observers_
, DiscoveredDevicesListChanged());
274 void FakeControllerPairingController::DeviceLost(const std::string
& device_id
) {
275 CHECK(current_stage_
== STAGE_DEVICES_DISCOVERY
);
276 discovered_devices_
.erase(device_id
);
277 FOR_EACH_OBSERVER(Observer
, observers_
, DiscoveredDevicesListChanged());
280 void FakeControllerPairingController::PairingStageChanged(Stage new_stage
) {
281 Stage next_stage
= STAGE_NONE
;
283 case STAGE_DEVICES_DISCOVERY
: {
284 discovered_devices_
.clear();
285 base::MessageLoop::current()->PostDelayedTask(
287 base::Bind(&FakeControllerPairingController::ExecuteDiscoveryEvent
,
288 base::Unretained(this),
293 case STAGE_ESTABLISHING_CONNECTION
: {
294 if (should_fail_on_connecting_
) {
295 next_stage
= STAGE_ESTABLISHING_CONNECTION_ERROR
;
296 should_fail_on_connecting_
= false;
298 confirmation_code_
.clear();
299 next_stage
= STAGE_WAITING_FOR_CODE_CONFIRMATION
;
303 case STAGE_HOST_UPDATE_IN_PROGRESS
: {
304 next_stage
= STAGE_WAITING_FOR_CREDENTIALS
;
307 case STAGE_HOST_ENROLLMENT_IN_PROGRESS
: {
308 if (enrollment_should_fail_
) {
309 enrollment_should_fail_
= false;
310 next_stage
= STAGE_HOST_ENROLLMENT_ERROR
;
312 next_stage
= STAGE_PAIRING_DONE
;
316 case STAGE_HOST_CONNECTION_LOST
: {
317 next_stage
= connection_lost_end_
;
318 connection_lost_end_
= STAGE_NONE
;
324 if (new_stage
== connection_lost_begin_
) {
325 connection_lost_begin_
= STAGE_NONE
;
326 next_stage
= STAGE_HOST_CONNECTION_LOST
;
328 if (next_stage
!= STAGE_NONE
)
329 ChangeStageLater(next_stage
);
332 void FakeControllerPairingController::DiscoveredDevicesListChanged() {
335 } // namespace chromeos