Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / chromeos / dbus / fake_nfc_device_client.cc
blobdf036c153b230d014b77f706937edb54deead3b0
1 // Copyright 2013 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/dbus/fake_nfc_device_client.h"
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/time/time.h"
11 #include "chromeos/dbus/dbus_thread_manager.h"
12 #include "chromeos/dbus/fake_nfc_adapter_client.h"
13 #include "chromeos/dbus/fake_nfc_record_client.h"
14 #include "dbus/object_path.h"
15 #include "third_party/cros_system_api/dbus/service_constants.h"
17 namespace chromeos {
19 using nfc_client_helpers::ObjectPathVector;
21 const char FakeNfcDeviceClient::kDevicePath[] = "/fake/device0";
22 const int FakeNfcDeviceClient::kDefaultSimulationTimeoutMilliseconds = 10000;
24 FakeNfcDeviceClient::Properties::Properties(
25 const PropertyChangedCallback& callback)
26 : NfcDeviceClient::Properties(NULL, callback) {
29 FakeNfcDeviceClient::Properties::~Properties() {
32 void FakeNfcDeviceClient::Properties::Get(
33 dbus::PropertyBase* property,
34 dbus::PropertySet::GetCallback callback) {
35 VLOG(1) << "Get " << property->name();
36 callback.Run(false);
39 void FakeNfcDeviceClient::Properties::GetAll() {
40 VLOG(1) << "GetAll";
43 void FakeNfcDeviceClient::Properties::Set(
44 dbus::PropertyBase* property,
45 dbus::PropertySet::SetCallback callback) {
46 VLOG(1) << "Set " << property->name();
47 callback.Run(false);
50 FakeNfcDeviceClient::FakeNfcDeviceClient()
51 : pairing_started_(false),
52 device_visible_(false),
53 simulation_timeout_(kDefaultSimulationTimeoutMilliseconds) {
54 VLOG(1) << "Creating FakeNfcDeviceClient";
56 properties_.reset(new Properties(
57 base::Bind(&FakeNfcDeviceClient::OnPropertyChanged,
58 base::Unretained(this),
59 dbus::ObjectPath(kDevicePath))));
62 FakeNfcDeviceClient::~FakeNfcDeviceClient() {
65 void FakeNfcDeviceClient::Init(dbus::Bus* bus) {
68 void FakeNfcDeviceClient::AddObserver(Observer* observer) {
69 observers_.AddObserver(observer);
72 void FakeNfcDeviceClient::RemoveObserver(Observer* observer) {
73 observers_.RemoveObserver(observer);
76 std::vector<dbus::ObjectPath> FakeNfcDeviceClient::GetDevicesForAdapter(
77 const dbus::ObjectPath& adapter_path) {
78 std::vector<dbus::ObjectPath> device_paths;
79 if (device_visible_ &&
80 adapter_path.value() == FakeNfcAdapterClient::kAdapterPath0)
81 device_paths.push_back(dbus::ObjectPath(kDevicePath));
82 return device_paths;
85 FakeNfcDeviceClient::Properties*
86 FakeNfcDeviceClient::GetProperties(const dbus::ObjectPath& object_path) {
87 if (!device_visible_)
88 return NULL;
89 return properties_.get();
92 void FakeNfcDeviceClient::Push(
93 const dbus::ObjectPath& object_path,
94 const base::DictionaryValue& attributes,
95 const base::Closure& callback,
96 const nfc_client_helpers::ErrorCallback& error_callback) {
97 VLOG(1) << "FakeNfcDeviceClient::Write called.";
99 // Success!
100 if (!device_visible_) {
101 LOG(ERROR) << "Device not visible. Cannot push record.";
102 error_callback.Run(nfc_error::kDoesNotExist, "No such device.");
103 return;
105 callback.Run();
108 void FakeNfcDeviceClient::BeginPairingSimulation(int visibility_delay,
109 int record_push_delay) {
110 if (pairing_started_) {
111 VLOG(1) << "Simulation already started.";
112 return;
114 DCHECK(!device_visible_);
115 DCHECK(visibility_delay >= 0);
117 pairing_started_ = true;
119 base::MessageLoop::current()->PostDelayedTask(
120 FROM_HERE,
121 base::Bind(&FakeNfcDeviceClient::MakeDeviceVisible,
122 base::Unretained(this),
123 record_push_delay),
124 base::TimeDelta::FromMilliseconds(visibility_delay));
127 void FakeNfcDeviceClient::EndPairingSimulation() {
128 if (!pairing_started_) {
129 VLOG(1) << "No simulation started.";
130 return;
132 if (device_visible_) {
133 // Remove records, if they were added.
134 if (!properties_->records.value().empty()) {
135 FakeNfcRecordClient* record_client =
136 static_cast<FakeNfcRecordClient*>(
137 DBusThreadManager::Get()->GetNfcRecordClient());
138 record_client->SetDeviceRecordsVisible(false);
140 // Remove the device.
141 FOR_EACH_OBSERVER(Observer, observers_,
142 DeviceRemoved(dbus::ObjectPath(kDevicePath)));
143 FakeNfcAdapterClient* adapter_client =
144 static_cast<FakeNfcAdapterClient*>(
145 DBusThreadManager::Get()->GetNfcAdapterClient());
146 adapter_client->UnsetDevice(dbus::ObjectPath(kDevicePath));
147 device_visible_ = false;
149 pairing_started_ = false;
152 void FakeNfcDeviceClient::EnableSimulationTimeout(int simulation_timeout) {
153 simulation_timeout_ = simulation_timeout;
156 void FakeNfcDeviceClient::DisableSimulationTimeout() {
157 simulation_timeout_ = -1;
160 void FakeNfcDeviceClient::SetRecords(
161 const std::vector<dbus::ObjectPath>& record_paths) {
162 if (!device_visible_) {
163 VLOG(1) << "Device not visible.";
164 return;
166 properties_->records.ReplaceValue(record_paths);
169 void FakeNfcDeviceClient::ClearRecords() {
170 ObjectPathVector records;
171 SetRecords(records);
174 void FakeNfcDeviceClient::OnPropertyChanged(
175 const dbus::ObjectPath& object_path,
176 const std::string& property_name) {
177 FOR_EACH_OBSERVER(NfcDeviceClient::Observer, observers_,
178 DevicePropertyChanged(object_path, property_name));
181 void FakeNfcDeviceClient::MakeDeviceVisible(int record_push_delay) {
182 if (!pairing_started_) {
183 VLOG(1) << "Device pairing was cancelled.";
184 return;
186 device_visible_ = true;
188 FakeNfcAdapterClient* adapter_client =
189 static_cast<FakeNfcAdapterClient*>(
190 DBusThreadManager::Get()->GetNfcAdapterClient());
191 adapter_client->SetDevice(dbus::ObjectPath(kDevicePath));
192 FOR_EACH_OBSERVER(Observer, observers_,
193 DeviceAdded(dbus::ObjectPath(kDevicePath)));
195 if (record_push_delay < 0) {
196 // Don't simulate record push. Instead, skip directly to the timeout step.
197 if (simulation_timeout_ >= 0) {
198 base::MessageLoop::current()->PostDelayedTask(
199 FROM_HERE,
200 base::Bind(&FakeNfcDeviceClient::HandleSimulationTimeout,
201 base::Unretained(this)),
202 base::TimeDelta::FromMilliseconds(simulation_timeout_));
204 return;
207 base::MessageLoop::current()->PostDelayedTask(
208 FROM_HERE,
209 base::Bind(&FakeNfcDeviceClient::MakeRecordsVisible,
210 base::Unretained(this)),
211 base::TimeDelta::FromMilliseconds(record_push_delay));
214 void FakeNfcDeviceClient::MakeRecordsVisible() {
215 if (!pairing_started_) {
216 VLOG(1) << "Pairing was cancelled";
217 return;
219 DCHECK(device_visible_);
220 FakeNfcRecordClient* record_client =
221 static_cast<FakeNfcRecordClient*>(
222 DBusThreadManager::Get()->GetNfcRecordClient());
223 record_client->SetDeviceRecordsVisible(true);
225 if (simulation_timeout_ < 0)
226 return;
228 base::MessageLoop::current()->PostDelayedTask(
229 FROM_HERE,
230 base::Bind(&FakeNfcDeviceClient::HandleSimulationTimeout,
231 base::Unretained(this)),
232 base::TimeDelta::FromMilliseconds(simulation_timeout_));
235 void FakeNfcDeviceClient::HandleSimulationTimeout() {
236 if (simulation_timeout_ < 0) {
237 VLOG(1) << "Simulation timeout was cancelled. Nothing to do.";
238 return;
240 EndPairingSimulation();
243 } // namespace chromeos