| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/pairing/fake_controller_pairing_controller.h" | 5 #include "components/pairing/fake_controller_pairing_controller.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
| 13 #include "base/single_thread_task_runner.h" |
| 13 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_split.h" | 15 #include "base/strings/string_split.h" |
| 15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "base/threading/thread_task_runner_handle.h" |
| 16 | 18 |
| 17 namespace pairing_chromeos { | 19 namespace pairing_chromeos { |
| 18 | 20 |
| 19 FakeControllerPairingController::FakeControllerPairingController( | 21 FakeControllerPairingController::FakeControllerPairingController( |
| 20 const std::string& config) | 22 const std::string& config) |
| 21 : current_stage_(STAGE_NONE), | 23 : current_stage_(STAGE_NONE), |
| 22 should_fail_on_connecting_(false), | 24 should_fail_on_connecting_(false), |
| 23 connection_lost_begin_(STAGE_NONE), | 25 connection_lost_begin_(STAGE_NONE), |
| 24 connection_lost_end_(STAGE_NONE), | 26 connection_lost_end_(STAGE_NONE), |
| 25 enrollment_should_fail_(false) { | 27 enrollment_should_fail_(false) { |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 228 } | 230 } |
| 229 | 231 |
| 230 void FakeControllerPairingController::ChangeStage(Stage new_stage) { | 232 void FakeControllerPairingController::ChangeStage(Stage new_stage) { |
| 231 if (current_stage_ == new_stage) | 233 if (current_stage_ == new_stage) |
| 232 return; | 234 return; |
| 233 current_stage_ = new_stage; | 235 current_stage_ = new_stage; |
| 234 FOR_EACH_OBSERVER(Observer, observers_, PairingStageChanged(new_stage)); | 236 FOR_EACH_OBSERVER(Observer, observers_, PairingStageChanged(new_stage)); |
| 235 } | 237 } |
| 236 | 238 |
| 237 void FakeControllerPairingController::ChangeStageLater(Stage new_stage) { | 239 void FakeControllerPairingController::ChangeStageLater(Stage new_stage) { |
| 238 base::MessageLoop::current()->PostDelayedTask( | 240 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 239 FROM_HERE, | 241 FROM_HERE, base::Bind(&FakeControllerPairingController::ChangeStage, |
| 240 base::Bind(&FakeControllerPairingController::ChangeStage, | 242 base::Unretained(this), new_stage), |
| 241 base::Unretained(this), | |
| 242 new_stage), | |
| 243 async_duration_); | 243 async_duration_); |
| 244 } | 244 } |
| 245 | 245 |
| 246 void FakeControllerPairingController::ExecuteDiscoveryEvent( | 246 void FakeControllerPairingController::ExecuteDiscoveryEvent( |
| 247 size_t event_position) { | 247 size_t event_position) { |
| 248 if (current_stage_ != STAGE_DEVICES_DISCOVERY) | 248 if (current_stage_ != STAGE_DEVICES_DISCOVERY) |
| 249 return; | 249 return; |
| 250 CHECK(event_position < discovery_scenario_.size()); | 250 CHECK(event_position < discovery_scenario_.size()); |
| 251 const DiscoveryEvent& event = discovery_scenario_[event_position]; | 251 const DiscoveryEvent& event = discovery_scenario_[event_position]; |
| 252 switch (event.first) { | 252 switch (event.first) { |
| 253 case DEVICE_FOUND: { | 253 case DEVICE_FOUND: { |
| 254 DeviceFound(event.second); | 254 DeviceFound(event.second); |
| 255 break; | 255 break; |
| 256 } | 256 } |
| 257 case DEVICE_LOST: { | 257 case DEVICE_LOST: { |
| 258 DeviceLost(event.second); | 258 DeviceLost(event.second); |
| 259 break; | 259 break; |
| 260 } | 260 } |
| 261 case NOTHING_FOUND: { | 261 case NOTHING_FOUND: { |
| 262 ChangeStage(STAGE_DEVICE_NOT_FOUND); | 262 ChangeStage(STAGE_DEVICE_NOT_FOUND); |
| 263 break; | 263 break; |
| 264 } | 264 } |
| 265 } | 265 } |
| 266 if (++event_position == discovery_scenario_.size()) { | 266 if (++event_position == discovery_scenario_.size()) { |
| 267 return; | 267 return; |
| 268 } | 268 } |
| 269 base::MessageLoop::current()->PostDelayedTask( | 269 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 270 FROM_HERE, | 270 FROM_HERE, |
| 271 base::Bind(&FakeControllerPairingController::ExecuteDiscoveryEvent, | 271 base::Bind(&FakeControllerPairingController::ExecuteDiscoveryEvent, |
| 272 base::Unretained(this), | 272 base::Unretained(this), event_position), |
| 273 event_position), | |
| 274 async_duration_); | 273 async_duration_); |
| 275 } | 274 } |
| 276 | 275 |
| 277 void FakeControllerPairingController::DeviceFound( | 276 void FakeControllerPairingController::DeviceFound( |
| 278 const std::string& device_id) { | 277 const std::string& device_id) { |
| 279 CHECK(current_stage_ == STAGE_DEVICES_DISCOVERY); | 278 CHECK(current_stage_ == STAGE_DEVICES_DISCOVERY); |
| 280 discovered_devices_.insert(device_id); | 279 discovered_devices_.insert(device_id); |
| 281 FOR_EACH_OBSERVER(Observer, observers_, DiscoveredDevicesListChanged()); | 280 FOR_EACH_OBSERVER(Observer, observers_, DiscoveredDevicesListChanged()); |
| 282 } | 281 } |
| 283 | 282 |
| 284 void FakeControllerPairingController::DeviceLost(const std::string& device_id) { | 283 void FakeControllerPairingController::DeviceLost(const std::string& device_id) { |
| 285 CHECK(current_stage_ == STAGE_DEVICES_DISCOVERY); | 284 CHECK(current_stage_ == STAGE_DEVICES_DISCOVERY); |
| 286 discovered_devices_.erase(device_id); | 285 discovered_devices_.erase(device_id); |
| 287 FOR_EACH_OBSERVER(Observer, observers_, DiscoveredDevicesListChanged()); | 286 FOR_EACH_OBSERVER(Observer, observers_, DiscoveredDevicesListChanged()); |
| 288 } | 287 } |
| 289 | 288 |
| 290 void FakeControllerPairingController::PairingStageChanged(Stage new_stage) { | 289 void FakeControllerPairingController::PairingStageChanged(Stage new_stage) { |
| 291 Stage next_stage = STAGE_NONE; | 290 Stage next_stage = STAGE_NONE; |
| 292 switch (new_stage) { | 291 switch (new_stage) { |
| 293 case STAGE_DEVICES_DISCOVERY: { | 292 case STAGE_DEVICES_DISCOVERY: { |
| 294 discovered_devices_.clear(); | 293 discovered_devices_.clear(); |
| 295 base::MessageLoop::current()->PostDelayedTask( | 294 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 296 FROM_HERE, | 295 FROM_HERE, |
| 297 base::Bind(&FakeControllerPairingController::ExecuteDiscoveryEvent, | 296 base::Bind(&FakeControllerPairingController::ExecuteDiscoveryEvent, |
| 298 base::Unretained(this), | 297 base::Unretained(this), 0), |
| 299 0), | |
| 300 async_duration_); | 298 async_duration_); |
| 301 break; | 299 break; |
| 302 } | 300 } |
| 303 case STAGE_ESTABLISHING_CONNECTION: { | 301 case STAGE_ESTABLISHING_CONNECTION: { |
| 304 if (should_fail_on_connecting_) { | 302 if (should_fail_on_connecting_) { |
| 305 next_stage = STAGE_ESTABLISHING_CONNECTION_ERROR; | 303 next_stage = STAGE_ESTABLISHING_CONNECTION_ERROR; |
| 306 should_fail_on_connecting_ = false; | 304 should_fail_on_connecting_ = false; |
| 307 } else { | 305 } else { |
| 308 confirmation_code_.clear(); | 306 confirmation_code_.clear(); |
| 309 next_stage = STAGE_WAITING_FOR_CODE_CONFIRMATION; | 307 next_stage = STAGE_WAITING_FOR_CODE_CONFIRMATION; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 336 next_stage = STAGE_HOST_CONNECTION_LOST; | 334 next_stage = STAGE_HOST_CONNECTION_LOST; |
| 337 } | 335 } |
| 338 if (next_stage != STAGE_NONE) | 336 if (next_stage != STAGE_NONE) |
| 339 ChangeStageLater(next_stage); | 337 ChangeStageLater(next_stage); |
| 340 } | 338 } |
| 341 | 339 |
| 342 void FakeControllerPairingController::DiscoveredDevicesListChanged() { | 340 void FakeControllerPairingController::DiscoveredDevicesListChanged() { |
| 343 } | 341 } |
| 344 | 342 |
| 345 } // namespace pairing_chromeos | 343 } // namespace pairing_chromeos |
| OLD | NEW |