OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chromeos/dbus/fake_nfc_device_client.h" | |
6 | |
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/threading/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" | |
18 | |
19 namespace chromeos { | |
20 | |
21 using nfc_client_helpers::ObjectPathVector; | |
22 | |
23 const char FakeNfcDeviceClient::kDevicePath[] = "/fake/device0"; | |
24 const int FakeNfcDeviceClient::kDefaultSimulationTimeoutMilliseconds = 10000; | |
25 | |
26 FakeNfcDeviceClient::Properties::Properties( | |
27 const PropertyChangedCallback& callback) | |
28 : NfcDeviceClient::Properties(NULL, callback) { | |
29 } | |
30 | |
31 FakeNfcDeviceClient::Properties::~Properties() { | |
32 } | |
33 | |
34 void FakeNfcDeviceClient::Properties::Get( | |
35 dbus::PropertyBase* property, | |
36 dbus::PropertySet::GetCallback callback) { | |
37 VLOG(1) << "Get " << property->name(); | |
38 callback.Run(false); | |
39 } | |
40 | |
41 void FakeNfcDeviceClient::Properties::GetAll() { | |
42 VLOG(1) << "GetAll"; | |
43 } | |
44 | |
45 void FakeNfcDeviceClient::Properties::Set( | |
46 dbus::PropertyBase* property, | |
47 dbus::PropertySet::SetCallback callback) { | |
48 VLOG(1) << "Set " << property->name(); | |
49 callback.Run(false); | |
50 } | |
51 | |
52 FakeNfcDeviceClient::FakeNfcDeviceClient() | |
53 : pairing_started_(false), | |
54 device_visible_(false), | |
55 simulation_timeout_(kDefaultSimulationTimeoutMilliseconds) { | |
56 VLOG(1) << "Creating FakeNfcDeviceClient"; | |
57 | |
58 properties_.reset(new Properties( | |
59 base::Bind(&FakeNfcDeviceClient::OnPropertyChanged, | |
60 base::Unretained(this), | |
61 dbus::ObjectPath(kDevicePath)))); | |
62 } | |
63 | |
64 FakeNfcDeviceClient::~FakeNfcDeviceClient() { | |
65 } | |
66 | |
67 void FakeNfcDeviceClient::Init(dbus::Bus* bus) { | |
68 } | |
69 | |
70 void FakeNfcDeviceClient::AddObserver(Observer* observer) { | |
71 observers_.AddObserver(observer); | |
72 } | |
73 | |
74 void FakeNfcDeviceClient::RemoveObserver(Observer* observer) { | |
75 observers_.RemoveObserver(observer); | |
76 } | |
77 | |
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; | |
85 } | |
86 | |
87 FakeNfcDeviceClient::Properties* | |
88 FakeNfcDeviceClient::GetProperties(const dbus::ObjectPath& object_path) { | |
89 if (!device_visible_) | |
90 return NULL; | |
91 return properties_.get(); | |
92 } | |
93 | |
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."; | |
100 | |
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; | |
106 } | |
107 callback.Run(); | |
108 } | |
109 | |
110 void FakeNfcDeviceClient::BeginPairingSimulation(int visibility_delay, | |
111 int record_push_delay) { | |
112 if (pairing_started_) { | |
113 VLOG(1) << "Simulation already started."; | |
114 return; | |
115 } | |
116 DCHECK(!device_visible_); | |
117 DCHECK(visibility_delay >= 0); | |
118 | |
119 pairing_started_ = true; | |
120 | |
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)); | |
125 } | |
126 | |
127 void FakeNfcDeviceClient::EndPairingSimulation() { | |
128 if (!pairing_started_) { | |
129 VLOG(1) << "No simulation started."; | |
130 return; | |
131 } | |
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); | |
139 } | |
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; | |
148 } | |
149 pairing_started_ = false; | |
150 } | |
151 | |
152 void FakeNfcDeviceClient::EnableSimulationTimeout(int simulation_timeout) { | |
153 simulation_timeout_ = simulation_timeout; | |
154 } | |
155 | |
156 void FakeNfcDeviceClient::DisableSimulationTimeout() { | |
157 simulation_timeout_ = -1; | |
158 } | |
159 | |
160 void FakeNfcDeviceClient::SetRecords( | |
161 const std::vector<dbus::ObjectPath>& record_paths) { | |
162 if (!device_visible_) { | |
163 VLOG(1) << "Device not visible."; | |
164 return; | |
165 } | |
166 properties_->records.ReplaceValue(record_paths); | |
167 } | |
168 | |
169 void FakeNfcDeviceClient::ClearRecords() { | |
170 ObjectPathVector records; | |
171 SetRecords(records); | |
172 } | |
173 | |
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)); | |
179 } | |
180 | |
181 void FakeNfcDeviceClient::MakeDeviceVisible(int record_push_delay) { | |
182 if (!pairing_started_) { | |
183 VLOG(1) << "Device pairing was cancelled."; | |
184 return; | |
185 } | |
186 device_visible_ = true; | |
187 | |
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))); | |
194 | |
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_)); | |
202 } | |
203 return; | |
204 } | |
205 | |
206 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
207 FROM_HERE, base::Bind(&FakeNfcDeviceClient::MakeRecordsVisible, | |
208 base::Unretained(this)), | |
209 base::TimeDelta::FromMilliseconds(record_push_delay)); | |
210 } | |
211 | |
212 void FakeNfcDeviceClient::MakeRecordsVisible() { | |
213 if (!pairing_started_) { | |
214 VLOG(1) << "Pairing was cancelled"; | |
215 return; | |
216 } | |
217 DCHECK(device_visible_); | |
218 FakeNfcRecordClient* record_client = | |
219 static_cast<FakeNfcRecordClient*>( | |
220 DBusThreadManager::Get()->GetNfcRecordClient()); | |
221 record_client->SetDeviceRecordsVisible(true); | |
222 | |
223 if (simulation_timeout_ < 0) | |
224 return; | |
225 | |
226 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | |
227 FROM_HERE, base::Bind(&FakeNfcDeviceClient::HandleSimulationTimeout, | |
228 base::Unretained(this)), | |
229 base::TimeDelta::FromMilliseconds(simulation_timeout_)); | |
230 } | |
231 | |
232 void FakeNfcDeviceClient::HandleSimulationTimeout() { | |
233 if (simulation_timeout_ < 0) { | |
234 VLOG(1) << "Simulation timeout was cancelled. Nothing to do."; | |
235 return; | |
236 } | |
237 EndPairingSimulation(); | |
238 } | |
239 | |
240 } // namespace chromeos | |
OLD | NEW |