Extension syncing: Introduce a NeedsSync pref
[chromium-blink-merge.git] / components / pairing / fake_host_pairing_controller.cc
blobebd708474931c0e378e086f340e6fe89e9f8ae1b
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"
7 #include <map>
8 #include <vector>
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"
17 namespace {
19 const int kDefaultAsyncDurationMs = 3000;
20 const size_t kCodeLength = 6;
22 } // namespace
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) {
30 ApplyConfig(config);
31 AddObserver(this);
34 FakeHostPairingController::~FakeHostPairingController() {
35 RemoveObserver(this);
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")) {
45 int ms = 0;
46 CHECK(base::StringToInt(dict["async_duration"], &ms))
47 << "Wrong 'async_duration' format.";
48 async_duration_ = base::TimeDelta::FromMilliseconds(ms);
49 } else {
50 async_duration_ =
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"];
60 } else {
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.";
69 device_name_ =
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)
77 return;
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(
84 FROM_HERE,
85 base::Bind(&FakeHostPairingController::ChangeStage,
86 base::Unretained(this),
87 new_stage),
88 async_duration_);
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);
107 } else {
108 ChangeStage(STAGE_WAITING_FOR_CONTROLLER);
112 std::string FakeHostPairingController::GetDeviceName() {
113 return device_name_;
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) {
138 switch (new_stage) {
139 case STAGE_WAITING_FOR_CONTROLLER: {
140 ChangeStageLater(STAGE_WAITING_FOR_CODE_CONFIRMATION);
141 break;
143 case STAGE_WAITING_FOR_CODE_CONFIRMATION: {
144 ChangeStageLater(STAGE_WAITING_FOR_CONTROLLER_AFTER_UPDATE);
145 break;
147 case STAGE_WAITING_FOR_CONTROLLER_AFTER_UPDATE: {
148 ChangeStageLater(STAGE_WAITING_FOR_CREDENTIALS);
149 break;
151 case STAGE_WAITING_FOR_CREDENTIALS: {
152 ChangeStageLater(STAGE_ENROLLING);
153 break;
155 case STAGE_ENROLLING: {
156 if (enrollment_should_fail_) {
157 enrollment_should_fail_ = false;
158 ChangeStageLater(STAGE_ENROLLMENT_ERROR);
159 } else {
160 ChangeStageLater(STAGE_ENROLLMENT_SUCCESS);
162 break;
164 case STAGE_ENROLLMENT_ERROR: {
165 ChangeStageLater(STAGE_WAITING_FOR_CONTROLLER_AFTER_UPDATE);
166 break;
168 case STAGE_ENROLLMENT_SUCCESS: {
169 ChangeStageLater(STAGE_FINISHED);
170 break;
172 default: { break; }
176 } // namespace pairing_chromeos