Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chromeos / dbus / fake_nfc_device_client.cc
blob6c1ec23da87c82b5ba4281327e0bc6de1a1b452b
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/location.h"
9 #include "base/logging.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/time/time.h"
13 #include "chromeos/dbus/dbus_thread_manager.h"
14 #include "chromeos/dbus/fake_nfc_adapter_client.h"
15 #include "chromeos/dbus/fake_nfc_record_client.h"
16 #include "dbus/object_path.h"
17 #include "third_party/cros_system_api/dbus/service_constants.h"
19 namespace chromeos {
21 using nfc_client_helpers::ObjectPathVector;
23 const char FakeNfcDeviceClient::kDevicePath[] = "/fake/device0";
24 const int FakeNfcDeviceClient::kDefaultSimulationTimeoutMilliseconds = 10000;
26 FakeNfcDeviceClient::Properties::Properties(
27 const PropertyChangedCallback& callback)
28 : NfcDeviceClient::Properties(NULL, callback) {
31 FakeNfcDeviceClient::Properties::~Properties() {
34 void FakeNfcDeviceClient::Properties::Get(
35 dbus::PropertyBase* property,
36 dbus::PropertySet::GetCallback callback) {
37 VLOG(1) << "Get " << property->name();
38 callback.Run(false);
41 void FakeNfcDeviceClient::Properties::GetAll() {
42 VLOG(1) << "GetAll";
45 void FakeNfcDeviceClient::Properties::Set(
46 dbus::PropertyBase* property,
47 dbus::PropertySet::SetCallback callback) {
48 VLOG(1) << "Set " << property->name();
49 callback.Run(false);
52 FakeNfcDeviceClient::FakeNfcDeviceClient()
53 : pairing_started_(false),
54 device_visible_(false),
55 simulation_timeout_(kDefaultSimulationTimeoutMilliseconds) {
56 VLOG(1) << "Creating FakeNfcDeviceClient";
58 properties_.reset(new Properties(
59 base::Bind(&FakeNfcDeviceClient::OnPropertyChanged,
60 base::Unretained(this),
61 dbus::ObjectPath(kDevicePath))));
64 FakeNfcDeviceClient::~FakeNfcDeviceClient() {
67 void FakeNfcDeviceClient::Init(dbus::Bus* bus) {
70 void FakeNfcDeviceClient::AddObserver(Observer* observer) {
71 observers_.AddObserver(observer);
74 void FakeNfcDeviceClient::RemoveObserver(Observer* observer) {
75 observers_.RemoveObserver(observer);
78 std::vector<dbus::ObjectPath> FakeNfcDeviceClient::GetDevicesForAdapter(
79 const dbus::ObjectPath& adapter_path) {
80 std::vector<dbus::ObjectPath> device_paths;
81 if (device_visible_ &&
82 adapter_path.value() == FakeNfcAdapterClient::kAdapterPath0)
83 device_paths.push_back(dbus::ObjectPath(kDevicePath));
84 return device_paths;
87 FakeNfcDeviceClient::Properties*
88 FakeNfcDeviceClient::GetProperties(const dbus::ObjectPath& object_path) {
89 if (!device_visible_)
90 return NULL;
91 return properties_.get();
94 void FakeNfcDeviceClient::Push(
95 const dbus::ObjectPath& object_path,
96 const base::DictionaryValue& attributes,
97 const base::Closure& callback,
98 const nfc_client_helpers::ErrorCallback& error_callback) {
99 VLOG(1) << "FakeNfcDeviceClient::Write called.";
101 // Success!
102 if (!device_visible_) {
103 LOG(ERROR) << "Device not visible. Cannot push record.";
104 error_callback.Run(nfc_error::kDoesNotExist, "No such device.");
105 return;
107 callback.Run();
110 void FakeNfcDeviceClient::BeginPairingSimulation(int visibility_delay,
111 int record_push_delay) {
112 if (pairing_started_) {
113 VLOG(1) << "Simulation already started.";
114 return;
116 DCHECK(!device_visible_);
117 DCHECK(visibility_delay >= 0);
119 pairing_started_ = true;
121 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
122 FROM_HERE, base::Bind(&FakeNfcDeviceClient::MakeDeviceVisible,
123 base::Unretained(this), 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::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
199 FROM_HERE, base::Bind(&FakeNfcDeviceClient::HandleSimulationTimeout,
200 base::Unretained(this)),
201 base::TimeDelta::FromMilliseconds(simulation_timeout_));
203 return;
206 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
207 FROM_HERE, base::Bind(&FakeNfcDeviceClient::MakeRecordsVisible,
208 base::Unretained(this)),
209 base::TimeDelta::FromMilliseconds(record_push_delay));
212 void FakeNfcDeviceClient::MakeRecordsVisible() {
213 if (!pairing_started_) {
214 VLOG(1) << "Pairing was cancelled";
215 return;
217 DCHECK(device_visible_);
218 FakeNfcRecordClient* record_client =
219 static_cast<FakeNfcRecordClient*>(
220 DBusThreadManager::Get()->GetNfcRecordClient());
221 record_client->SetDeviceRecordsVisible(true);
223 if (simulation_timeout_ < 0)
224 return;
226 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
227 FROM_HERE, base::Bind(&FakeNfcDeviceClient::HandleSimulationTimeout,
228 base::Unretained(this)),
229 base::TimeDelta::FromMilliseconds(simulation_timeout_));
232 void FakeNfcDeviceClient::HandleSimulationTimeout() {
233 if (simulation_timeout_ < 0) {
234 VLOG(1) << "Simulation timeout was cancelled. Nothing to do.";
235 return;
237 EndPairingSimulation();
240 } // namespace chromeos