summaryrefslogtreecommitdiffstats
path: root/chromeos/dbus/fake_nfc_tag_client.cc
blob: 7b63c3b143d1488469032ac401009f4e27f894b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/dbus/fake_nfc_tag_client.h"

#include "base/location.h"
#include "base/logging.h"
#include "base/single_thread_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/fake_nfc_adapter_client.h"
#include "chromeos/dbus/fake_nfc_record_client.h"
#include "dbus/object_path.h"
#include "third_party/cros_system_api/dbus/service_constants.h"

namespace chromeos {

using nfc_client_helpers::ObjectPathVector;

const char FakeNfcTagClient::kTagPath[] = "/fake/tag0";
const int FakeNfcTagClient::kDefaultSimulationTimeoutMilliseconds = 20000;

FakeNfcTagClient::Properties::Properties(
    const PropertyChangedCallback& callback)
    : NfcTagClient::Properties(NULL, callback) {
}

FakeNfcTagClient::Properties::~Properties() {
}

void FakeNfcTagClient::Properties::Get(
    dbus::PropertyBase* property,
    dbus::PropertySet::GetCallback callback) {
  VLOG(1) << "Get " << property->name();
  callback.Run(false);
}

void FakeNfcTagClient::Properties::GetAll() {
  VLOG(1) << "GetAll";
}

void FakeNfcTagClient::Properties::Set(
    dbus::PropertyBase* property,
    dbus::PropertySet::SetCallback callback) {
  VLOG(1) << "Set " << property->name();
  callback.Run(false);
}

FakeNfcTagClient::FakeNfcTagClient()
    : pairing_started_(false),
      tag_visible_(false),
      simulation_timeout_(kDefaultSimulationTimeoutMilliseconds) {
  VLOG(1) << "Creating FakeNfcTagClient";
  properties_.reset(new Properties(
      base::Bind(&FakeNfcTagClient::OnPropertyChanged,
                 base::Unretained(this),
                 dbus::ObjectPath(kTagPath))));
}

FakeNfcTagClient::~FakeNfcTagClient() {
}

void FakeNfcTagClient::Init(dbus::Bus* bus) {
}

void FakeNfcTagClient::AddObserver(Observer* observer) {
  observers_.AddObserver(observer);
}

void FakeNfcTagClient::RemoveObserver(Observer* observer) {
  observers_.RemoveObserver(observer);
}

std::vector<dbus::ObjectPath> FakeNfcTagClient::GetTagsForAdapter(
    const dbus::ObjectPath& adapter_path) {
  std::vector<dbus::ObjectPath> tag_paths;
  if (tag_visible_ &&
      adapter_path.value() == FakeNfcAdapterClient::kAdapterPath0)
    tag_paths.push_back(dbus::ObjectPath(kTagPath));
  return tag_paths;
}

FakeNfcTagClient::Properties*
FakeNfcTagClient::GetProperties(const dbus::ObjectPath& object_path) {
  if (!tag_visible_)
    return NULL;
  return properties_.get();
}

void FakeNfcTagClient::Write(
    const dbus::ObjectPath& object_path,
    const base::DictionaryValue& attributes,
    const base::Closure& callback,
    const nfc_client_helpers::ErrorCallback& error_callback) {
  VLOG(1) << "FakeNfcTagClient::Write called. Nothing happened.";

  if (!tag_visible_ || object_path.value() != kTagPath) {
    LOG(ERROR) << "No such tag: " << object_path.value();
    error_callback.Run(nfc_error::kDoesNotExist, "No such tag.");
    return;
  }

  FakeNfcRecordClient* record_client = static_cast<FakeNfcRecordClient*>(
      DBusThreadManager::Get()->GetNfcRecordClient());
  if (!record_client->WriteTagRecord(attributes)) {
    LOG(ERROR) << "Failed to tag: " << object_path.value();
    error_callback.Run(nfc_error::kFailed, "Failed.");
    return;
  }

  // Success!
  callback.Run();
}

void FakeNfcTagClient::BeginPairingSimulation(int visibility_delay) {
  if (pairing_started_) {
    VLOG(1) << "Simulation already started.";
    return;
  }
  DCHECK(!tag_visible_);
  DCHECK(visibility_delay >= 0);

  pairing_started_ = true;

  base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
      FROM_HERE,
      base::Bind(&FakeNfcTagClient::MakeTagVisible, base::Unretained(this)),
      base::TimeDelta::FromMilliseconds(visibility_delay));
}

void FakeNfcTagClient::EndPairingSimulation() {
  if (!pairing_started_) {
    VLOG(1) << "No simulation started.";
    return;
  }

  pairing_started_ = false;
  if (!tag_visible_)
    return;

  // Remove records, if they were added.
  if (!properties_->records.value().empty()) {
    FakeNfcRecordClient* record_client =
        static_cast<FakeNfcRecordClient*>(
            DBusThreadManager::Get()->GetNfcRecordClient());
    record_client->SetTagRecordsVisible(false);
  }
  // Remove the tag.
  FOR_EACH_OBSERVER(Observer, observers_,
                    TagRemoved(dbus::ObjectPath(kTagPath)));
  FakeNfcAdapterClient* adapter_client =
      static_cast<FakeNfcAdapterClient*>(
          DBusThreadManager::Get()->GetNfcAdapterClient());
  adapter_client->UnsetTag(dbus::ObjectPath(kTagPath));
  tag_visible_ = false;
}

void FakeNfcTagClient::EnableSimulationTimeout(int simulation_timeout) {
  DCHECK(simulation_timeout >= 0);
  simulation_timeout_ = simulation_timeout;
}

void FakeNfcTagClient::DisableSimulationTimeout() {
  simulation_timeout_ = -1;
}

void FakeNfcTagClient::SetRecords(
    const std::vector<dbus::ObjectPath>& record_paths) {
  if (!tag_visible_) {
    VLOG(1) << "Tag not visible.";
    return;
  }
  properties_->records.ReplaceValue(record_paths);
}

void FakeNfcTagClient::ClearRecords() {
  ObjectPathVector records;
  SetRecords(records);
}

void FakeNfcTagClient::OnPropertyChanged(const dbus::ObjectPath& object_path,
                                         const std::string& property_name) {
  FOR_EACH_OBSERVER(Observer, observers_,
                    TagPropertyChanged(object_path, property_name));
}

void FakeNfcTagClient::MakeTagVisible() {
  if (!pairing_started_) {
    VLOG(1) << "Tag pairing was cancelled.";
    return;
  }
  tag_visible_ = true;

  // Set the tag properties.
  FakeNfcAdapterClient* adapter_client =
      static_cast<FakeNfcAdapterClient*>(
          DBusThreadManager::Get()->GetNfcAdapterClient());
  adapter_client->SetTag(dbus::ObjectPath(kTagPath));
  FOR_EACH_OBSERVER(Observer, observers_,
                    TagAdded(dbus::ObjectPath(kTagPath)));

  properties_->type.ReplaceValue(nfc_tag::kTagType2);
  properties_->protocol.ReplaceValue(nfc_common::kProtocolNfcDep);
  properties_->read_only.ReplaceValue(false);
  FOR_EACH_OBSERVER(Observer, observers_,
                    TagPropertiesReceived(dbus::ObjectPath(kTagPath)));

  if (simulation_timeout_ >= 0) {
    base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
        FROM_HERE, base::Bind(&FakeNfcTagClient::HandleSimulationTimeout,
                              base::Unretained(this)),
        base::TimeDelta::FromMilliseconds(simulation_timeout_));
    return;
  }
}

void FakeNfcTagClient::HandleSimulationTimeout() {
  if (simulation_timeout_ < 0) {
    VLOG(1) << "Simulation timeout was cancelled. Nothing to do.";
    return;
  }
  EndPairingSimulation();
}

}  // namespace chromeos