This source file includes following definitions.
- weak_ptr_factory_
- AddObserver
- RemoveObserver
- GetTagsForAdapter
- GetProperties
- Write
- Init
- AdapterAdded
- AdapterRemoved
- AdapterPropertyChanged
- CreateProperties
- ObjectAdded
- ObjectRemoved
- OnPropertyChanged
- OnPropertiesReceived
- Create
#include "chromeos/dbus/nfc_tag_client.h"
#include "base/bind.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/strings/stringprintf.h"
#include "chromeos/dbus/nfc_adapter_client.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
using chromeos::nfc_client_helpers::DBusObjectMap;
using chromeos::nfc_client_helpers::ObjectProxyTree;
namespace chromeos {
NfcTagClient::Properties::Properties(
dbus::ObjectProxy* object_proxy,
const PropertyChangedCallback& callback)
: NfcPropertySet(object_proxy,
nfc_tag::kNfcTagInterface,
callback) {
RegisterProperty(nfc_tag::kTypeProperty, &type);
RegisterProperty(nfc_tag::kProtocolProperty, &protocol);
RegisterProperty(nfc_tag::kRecordsProperty, &records);
RegisterProperty(nfc_tag::kReadOnlyProperty, &read_only);
}
NfcTagClient::Properties::~Properties() {
}
class NfcTagClientImpl : public NfcTagClient,
public NfcAdapterClient::Observer,
public DBusObjectMap::Delegate {
public:
explicit NfcTagClientImpl(NfcAdapterClient* adapter_client)
: bus_(NULL),
adapter_client_(adapter_client),
weak_ptr_factory_(this) {
DCHECK(adapter_client);
}
virtual ~NfcTagClientImpl() {
DCHECK(adapter_client_);
adapter_client_->RemoveObserver(this);
}
virtual void AddObserver(NfcTagClient::Observer* observer) OVERRIDE {
DCHECK(observer);
observers_.AddObserver(observer);
}
virtual void RemoveObserver(NfcTagClient::Observer* observer) OVERRIDE {
DCHECK(observer);
observers_.RemoveObserver(observer);
}
virtual std::vector<dbus::ObjectPath> GetTagsForAdapter(
const dbus::ObjectPath& adapter_path) OVERRIDE {
DBusObjectMap* object_map =
adapters_to_object_maps_.GetObjectMap(adapter_path);
if (!object_map)
return std::vector<dbus::ObjectPath>();
return object_map->GetObjectPaths();
}
virtual Properties* GetProperties(
const dbus::ObjectPath& object_path) OVERRIDE {
return static_cast<Properties*>(
adapters_to_object_maps_.FindObjectProperties(object_path));
}
virtual void Write(
const dbus::ObjectPath& object_path,
const base::DictionaryValue& attributes,
const base::Closure& callback,
const nfc_client_helpers::ErrorCallback& error_callback) OVERRIDE {
dbus::ObjectProxy* object_proxy =
adapters_to_object_maps_.FindObjectProxy(object_path);
if (!object_proxy) {
std::string error_message =
base::StringPrintf("NFC tag with object path \"%s\" does not exist.",
object_path.value().c_str());
LOG(ERROR) << error_message;
error_callback.Run(nfc_client_helpers::kUnknownObjectError,
error_message);
return;
}
if (attributes.empty()) {
std::string error_message =
"Cannot write data to tag with empty arguments.";
LOG(ERROR) << error_message;
error_callback.Run(nfc_error::kInvalidArguments, error_message);
return;
}
dbus::MethodCall method_call(nfc_tag::kNfcTagInterface, nfc_tag::kWrite);
dbus::MessageWriter writer(&method_call);
dbus::MessageWriter array_writer(NULL);
dbus::MessageWriter dict_entry_writer(NULL);
writer.OpenArray("{sv}", &array_writer);
for (base::DictionaryValue::Iterator iter(attributes);
!iter.IsAtEnd(); iter.Advance()) {
array_writer.OpenDictEntry(&dict_entry_writer);
dict_entry_writer.AppendString(iter.key());
nfc_client_helpers::AppendValueDataAsVariant(&dict_entry_writer,
iter.value());
array_writer.CloseContainer(&dict_entry_writer);
}
writer.CloseContainer(&array_writer);
object_proxy->CallMethodWithErrorCallback(
&method_call,
dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
base::Bind(&nfc_client_helpers::OnSuccess, callback),
base::Bind(&nfc_client_helpers::OnError, error_callback));
}
protected:
virtual void Init(dbus::Bus* bus) OVERRIDE {
VLOG(1) << "Creating NfcTagClientImpl";
DCHECK(bus);
bus_ = bus;
DCHECK(adapter_client_);
adapter_client_->AddObserver(this);
}
private:
virtual void AdapterAdded(const dbus::ObjectPath& object_path) OVERRIDE {
VLOG(1) << "Adapter added. Creating map for tag proxies belonging to "
<< "adapter: " << object_path.value();
adapters_to_object_maps_.CreateObjectMap(
object_path, nfc_tag::kNfcTagServiceName, this, bus_);
}
virtual void AdapterRemoved(const dbus::ObjectPath& object_path) OVERRIDE {
VLOG(1) << "Adapter removed. Cleaning up tag proxies belonging to "
<< "adapter: " << object_path.value();
adapters_to_object_maps_.RemoveObjectMap(object_path);
}
virtual void AdapterPropertyChanged(
const dbus::ObjectPath& object_path,
const std::string& property_name) OVERRIDE {
DCHECK(adapter_client_);
NfcAdapterClient::Properties *adapter_properties =
adapter_client_->GetProperties(object_path);
DCHECK(adapter_properties);
if (!adapter_properties) {
LOG(ERROR) << "No property structure found for adapter: "
<< object_path.value();
return;
}
if (property_name != adapter_properties->tags.name())
return;
VLOG(1) << "NFC tags changed.";
const std::vector<dbus::ObjectPath>& received_tags =
adapter_properties->tags.value();
DBusObjectMap* object_map =
adapters_to_object_maps_.GetObjectMap(object_path);
DCHECK(object_map);
object_map->UpdateObjects(received_tags);
}
virtual NfcPropertySet* CreateProperties(
dbus::ObjectProxy* object_proxy) OVERRIDE {
Properties* properties = new Properties(
object_proxy,
base::Bind(&NfcTagClientImpl::OnPropertyChanged,
weak_ptr_factory_.GetWeakPtr(),
object_proxy->object_path()));
properties->SetAllPropertiesReceivedCallback(
base::Bind(&NfcTagClientImpl::OnPropertiesReceived,
weak_ptr_factory_.GetWeakPtr(),
object_proxy->object_path()));
return properties;
}
virtual void ObjectAdded(const dbus::ObjectPath& object_path) OVERRIDE {
FOR_EACH_OBSERVER(NfcTagClient::Observer, observers_,
TagAdded(object_path));
}
virtual void ObjectRemoved(const dbus::ObjectPath& object_path) OVERRIDE {
FOR_EACH_OBSERVER(NfcTagClient::Observer, observers_,
TagRemoved(object_path));
}
void OnPropertyChanged(const dbus::ObjectPath& object_path,
const std::string& property_name) {
VLOG(1) << "Tag property changed; Path: " << object_path.value()
<< " Property: " << property_name;
FOR_EACH_OBSERVER(NfcTagClient::Observer, observers_,
TagPropertyChanged(object_path, property_name));
}
void OnPropertiesReceived(const dbus::ObjectPath& object_path) {
VLOG(1) << "All tag properties received; Path: " << object_path.value();
FOR_EACH_OBSERVER(NfcTagClient::Observer, observers_,
TagPropertiesReceived(object_path));
}
dbus::Bus* bus_;
ObserverList<NfcTagClient::Observer> observers_;
ObjectProxyTree adapters_to_object_maps_;
NfcAdapterClient* adapter_client_;
base::WeakPtrFactory<NfcTagClientImpl> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(NfcTagClientImpl);
};
NfcTagClient::NfcTagClient() {
}
NfcTagClient::~NfcTagClient() {
}
NfcTagClient* NfcTagClient::Create(NfcAdapterClient* adapter_client) {
return new NfcTagClientImpl(adapter_client);
}
}