Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(449)

Side by Side Diff: chromeos/pairing/fake_controller_pairing_flow.cc

Issue 343433003: Implemented fake controller pairing flow. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chromeos/pairing/fake_controller_pairing_flow.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 "chromeos/pairing/fake_controller_pairing_flow.h"
6
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/rand_util.h"
11
12 namespace chromeos {
13
14 const char* FakeControllerPairingFlow::kNotFoundDeviceID = "not_found";
15
16 FakeControllerPairingFlow::FakeControllerPairingFlow()
17 : current_stage_(STAGE_NONE),
18 async_duration_(base::TimeDelta::FromSeconds(3)),
19 should_fail_on_connecting_(false),
20 connection_lost_begin_(STAGE_NONE),
21 connection_lost_end_(STAGE_NONE),
22 enrollment_should_fail_(false) {
23 SetDiscoveryScenario(GetDefaultScenario());
24 AddObserver(this);
25 }
26
27 FakeControllerPairingFlow::~FakeControllerPairingFlow() {
28 RemoveObserver(this);
29 }
30
31 void FakeControllerPairingFlow::SetShouldFailOnConnecting() {
32 should_fail_on_connecting_ = true;
33 }
34
35 void FakeControllerPairingFlow::SetShouldLoseConnection(Stage stage_begin,
36 Stage stage_end) {
37 connection_lost_begin_ = stage_begin;
38 connection_lost_end_ = stage_end;
39 }
40
41 void FakeControllerPairingFlow::SetEnrollmentShouldFail() {
42 enrollment_should_fail_ = true;
43 }
44
45 void FakeControllerPairingFlow::SetDiscoveryScenario(
46 const DiscoveryScenario& discovery_scenario) {
47 discovery_scenario_ = discovery_scenario;
48 // Check that scenario is valid.
49 std::set<std::string> devices;
50 for (DiscoveryScenario::const_iterator event = discovery_scenario_.begin();
51 event != discovery_scenario_.end();
52 ++event) {
53 if (event->second == kNotFoundDeviceID) {
54 CHECK(devices.empty());
55 CHECK(++event == discovery_scenario_.end());
56 return;
57 } else if (event->first == DEVICE_FOUND) {
58 devices.insert(event->second);
59 } else {
60 CHECK(devices.count(event->second));
61 devices.erase(event->second);
62 }
63 }
64 }
65
66 void FakeControllerPairingFlow::AddObserver(Observer* observer) {
67 observers_.AddObserver(observer);
68 }
69
70 void FakeControllerPairingFlow::RemoveObserver(Observer* observer) {
71 observers_.RemoveObserver(observer);
72 }
73
74 ControllerPairingFlow::Stage FakeControllerPairingFlow::GetCurrentStage() {
75 return current_stage_;
76 }
77
78 void FakeControllerPairingFlow::StartFlow() {
79 CHECK(current_stage_ == STAGE_NONE);
80 ChangeStage(STAGE_DEVICES_DISCOVERY);
81 }
82
83 ControllerPairingFlow::DeviceIdList
84 FakeControllerPairingFlow::GetDiscoveredDevices() {
85 CHECK(current_stage_ == STAGE_DEVICES_DISCOVERY);
86 return DeviceIdList(discovered_devices_.begin(), discovered_devices_.end());
87 }
88
89 void FakeControllerPairingFlow::ChooseDeviceForPairing(
90 const std::string& device_id) {
91 CHECK(current_stage_ == STAGE_DEVICES_DISCOVERY);
92 CHECK(discovered_devices_.count(device_id));
93 choosen_device_ = device_id;
94 ChangeStage(STAGE_ESTABLISHING_CONNECTION);
95 }
96
97 void FakeControllerPairingFlow::RepeatDiscovery() {
98 CHECK(current_stage_ == STAGE_DEVICE_NOT_FOUND ||
99 current_stage_ == STAGE_ESTABLISHING_CONNECTION_ERROR ||
100 current_stage_ == STAGE_HOST_ENROLLMENT_ERROR);
101 ChangeStage(STAGE_DEVICES_DISCOVERY);
102 }
103
104 std::string FakeControllerPairingFlow::GetConfirmationCode() {
105 CHECK(current_stage_ == STAGE_WAITING_FOR_CODE_CONFIRMATION);
106 if (confirmation_code_.empty())
107 for (int i = 0; i < 6; ++i)
108 confirmation_code_.push_back(base::RandInt('0', '9'));
Dmitry Polukhin 2014/06/18 10:46:51 I would avoid randomness in test. I think you can
109 return confirmation_code_;
110 }
111
112 void FakeControllerPairingFlow::SetConfirmationCodeIsCorrect(bool correct) {
113 CHECK(current_stage_ == STAGE_WAITING_FOR_CODE_CONFIRMATION);
114 if (correct)
115 ChangeStage(STAGE_HOST_UPDATE_IN_PROGRESS);
116 else
117 ChangeStage(STAGE_DEVICES_DISCOVERY);
118 }
119
120 void FakeControllerPairingFlow::OnAuthenticationDone(
121 const chromeos::UserContext& user_context,
122 content::BrowserContext* browser_context) {
123 CHECK(current_stage_ == STAGE_WAITING_FOR_CREDENTIALS);
124 ChangeStage(STAGE_HOST_ENROLLMENT_IN_PROGRESS);
125 }
126
127 void FakeControllerPairingFlow::StartSession() {
128 CHECK(current_stage_ == STAGE_PAIRING_DONE);
129 ChangeStage(STAGE_FINISHED);
130 }
131
132 void FakeControllerPairingFlow::ChangeStage(Stage new_stage) {
133 if (current_stage_ == new_stage)
134 return;
135 current_stage_ = new_stage;
136 FOR_EACH_OBSERVER(Observer, observers_, PairingStageChanged(new_stage));
137 }
138
139 void FakeControllerPairingFlow::ChangeStageLater(Stage new_stage) {
140 base::MessageLoop::current()->PostDelayedTask(
141 FROM_HERE,
142 base::Bind(&FakeControllerPairingFlow::ChangeStage,
143 base::Unretained(this),
144 new_stage),
145 async_duration_);
146 }
147
148 void FakeControllerPairingFlow::ExecuteDiscoveryEvent(size_t event_position) {
149 if (current_stage_ != STAGE_DEVICES_DISCOVERY)
150 return;
151 CHECK(event_position < discovery_scenario_.size());
152 const DiscoveryEvent& event = discovery_scenario_[event_position];
153 if (event.second == kNotFoundDeviceID) {
154 ChangeStage(STAGE_DEVICE_NOT_FOUND);
155 return;
156 } else if (event.first == DEVICE_FOUND) {
157 DeviceFound(event.second);
158 } else {
159 DeviceLost(event.second);
160 }
161 if (++event_position == discovery_scenario_.size()) {
162 return;
163 }
164 base::MessageLoop::current()->PostDelayedTask(
165 FROM_HERE,
166 base::Bind(&FakeControllerPairingFlow::ExecuteDiscoveryEvent,
167 base::Unretained(this),
168 event_position),
169 async_duration_);
170 }
171
172 void FakeControllerPairingFlow::DeviceFound(const std::string& device_id) {
173 CHECK(current_stage_ == STAGE_DEVICES_DISCOVERY);
174 discovered_devices_.insert(device_id);
175 FOR_EACH_OBSERVER(Observer, observers_, DiscoveredDevicesListChanged());
176 }
177
178 void FakeControllerPairingFlow::DeviceLost(const std::string& device_id) {
179 CHECK(current_stage_ == STAGE_DEVICES_DISCOVERY);
180 discovered_devices_.erase(device_id);
181 FOR_EACH_OBSERVER(Observer, observers_, DiscoveredDevicesListChanged());
182 }
183
184 FakeControllerPairingFlow::DiscoveryScenario
185 FakeControllerPairingFlow::GetDefaultScenario() const {
186 DiscoveryScenario scenario;
187 scenario.push_back(std::make_pair(DEVICE_FOUND, "Device_1"));
188 scenario.push_back(std::make_pair(DEVICE_FOUND, "Device_5"));
189 scenario.push_back(std::make_pair(DEVICE_FOUND, "Device_3"));
190 scenario.push_back(std::make_pair(DEVICE_LOST, "Device_3"));
191 scenario.push_back(std::make_pair(DEVICE_LOST, "Device_1"));
192 scenario.push_back(std::make_pair(DEVICE_FOUND, "Device_1"));
193 return scenario;
194 }
195
196 void FakeControllerPairingFlow::PairingStageChanged(Stage new_stage) {
197 Stage next_stage = STAGE_NONE;
198 switch (new_stage) {
199 case STAGE_DEVICES_DISCOVERY: {
200 discovered_devices_.clear();
201 base::MessageLoop::current()->PostDelayedTask(
202 FROM_HERE,
203 base::Bind(&FakeControllerPairingFlow::ExecuteDiscoveryEvent,
204 base::Unretained(this),
205 0),
206 async_duration_);
207 break;
208 }
209 case STAGE_ESTABLISHING_CONNECTION: {
210 if (should_fail_on_connecting_) {
211 next_stage = STAGE_ESTABLISHING_CONNECTION_ERROR;
212 should_fail_on_connecting_ = false;
213 } else {
214 confirmation_code_.clear();
215 next_stage = STAGE_WAITING_FOR_CODE_CONFIRMATION;
216 }
217 break;
218 }
219 case STAGE_HOST_UPDATE_IN_PROGRESS: {
220 next_stage = STAGE_WAITING_FOR_CREDENTIALS;
221 break;
222 }
223 case STAGE_HOST_ENROLLMENT_IN_PROGRESS: {
224 if (enrollment_should_fail_) {
225 enrollment_should_fail_ = false;
226 next_stage = STAGE_HOST_ENROLLMENT_ERROR;
227 } else {
228 next_stage = STAGE_PAIRING_DONE;
229 }
230 break;
231 }
232 case STAGE_HOST_CONNECTION_LOST: {
233 next_stage = connection_lost_end_;
234 connection_lost_end_ = STAGE_NONE;
235 break;
236 }
237 default:
238 break;
239 }
240 if (new_stage == connection_lost_begin_) {
241 connection_lost_begin_ = STAGE_NONE;
242 next_stage = STAGE_HOST_CONNECTION_LOST;
243 }
244 if (next_stage != STAGE_NONE)
245 ChangeStageLater(next_stage);
246 }
247
248 void FakeControllerPairingFlow::DiscoveredDevicesListChanged() {
249 }
250
251 } // namespace chromeos
OLDNEW
« no previous file with comments | « chromeos/pairing/fake_controller_pairing_flow.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698