This source file includes following definitions.
- weak_ptr_factory_
- AddObserver
- RemoveObserver
- GetRecordsForDevice
- GetRecordsForTag
- GetProperties
- Init
- DeviceAdded
- DeviceRemoved
- DevicePropertyChanged
- TagAdded
- TagRemoved
- TagPropertyChanged
- CreateProperties
- ObjectAdded
- ObjectRemoved
- OnPropertyChanged
- OnPropertiesReceived
- Create
#include "chromeos/dbus/nfc_record_client.h"
#include "base/bind.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "chromeos/dbus/nfc_device_client.h"
#include "chromeos/dbus/nfc_tag_client.h"
#include "dbus/bus.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 {
NfcRecordClient::Properties::Properties(
dbus::ObjectProxy* object_proxy,
const PropertyChangedCallback& callback)
: NfcPropertySet(object_proxy,
nfc_record::kNfcRecordInterface,
callback) {
RegisterProperty(nfc_record::kTypeProperty, &type);
RegisterProperty(nfc_record::kLanguageProperty, &language);
RegisterProperty(nfc_record::kEncodingProperty, &encoding);
RegisterProperty(nfc_record::kRepresentationProperty, &representation);
RegisterProperty(nfc_record::kUriProperty, &uri);
RegisterProperty(nfc_record::kMimeTypeProperty, &mime_type);
RegisterProperty(nfc_record::kSizeProperty, &size);
RegisterProperty(nfc_record::kActionProperty, &action);
}
NfcRecordClient::Properties::~Properties() {
}
class NfcRecordClientImpl : public NfcRecordClient,
public NfcDeviceClient::Observer,
public NfcTagClient::Observer,
public DBusObjectMap::Delegate {
public:
explicit NfcRecordClientImpl(NfcDeviceClient* device_client,
NfcTagClient* tag_client)
: bus_(NULL),
device_client_(device_client),
tag_client_(tag_client),
weak_ptr_factory_(this) {
DCHECK(device_client);
DCHECK(tag_client);
}
virtual ~NfcRecordClientImpl() {
DCHECK(device_client_);
DCHECK(tag_client_);
device_client_->RemoveObserver(this);
tag_client_->RemoveObserver(this);
}
virtual void AddObserver(NfcRecordClient::Observer* observer) OVERRIDE {
DCHECK(observer);
observers_.AddObserver(observer);
}
virtual void RemoveObserver(NfcRecordClient::Observer* observer) OVERRIDE {
DCHECK(observer);
observers_.RemoveObserver(observer);
}
virtual std::vector<dbus::ObjectPath> GetRecordsForDevice(
const dbus::ObjectPath& device_path) OVERRIDE {
DBusObjectMap* object_map =
devices_and_tags_to_object_maps_.GetObjectMap(device_path);
if (!object_map)
return std::vector<dbus::ObjectPath>();
return object_map->GetObjectPaths();
}
virtual std::vector<dbus::ObjectPath> GetRecordsForTag(
const dbus::ObjectPath& tag_path) OVERRIDE {
return GetRecordsForDevice(tag_path);
}
virtual Properties* GetProperties(
const dbus::ObjectPath& object_path) OVERRIDE {
return static_cast<Properties*>(
devices_and_tags_to_object_maps_.FindObjectProperties(object_path));
}
protected:
virtual void Init(dbus::Bus* bus) OVERRIDE {
VLOG(1) << "Creating NfcRecordClient impl";
DCHECK(bus);
bus_ = bus;
DCHECK(device_client_);
DCHECK(tag_client_);
device_client_->AddObserver(this);
tag_client_->AddObserver(this);
}
private:
virtual void DeviceAdded(const dbus::ObjectPath& object_path) OVERRIDE {
VLOG(1) << "Device added. Creating map for record proxies belonging to "
<< "device: " << object_path.value();
devices_and_tags_to_object_maps_.CreateObjectMap(
object_path, nfc_record::kNfcRecordServiceName, this, bus_);
}
virtual void DeviceRemoved(const dbus::ObjectPath& object_path) OVERRIDE {
VLOG(1) << "Device removed. Cleaning up record proxies belonging to "
<< "device: " << object_path.value();
devices_and_tags_to_object_maps_.RemoveObjectMap(object_path);
}
virtual void DevicePropertyChanged(
const dbus::ObjectPath& object_path,
const std::string& property_name) OVERRIDE {
DCHECK(device_client_);
NfcDeviceClient::Properties* device_properties =
device_client_->GetProperties(object_path);
if (property_name != device_properties->records.name())
return;
VLOG(1) << "NFC records changed.";
const std::vector<dbus::ObjectPath>& received_records =
device_properties->records.value();
DBusObjectMap* object_map =
devices_and_tags_to_object_maps_.GetObjectMap(object_path);
DCHECK(object_map);
object_map->UpdateObjects(received_records);
}
virtual void TagAdded(const dbus::ObjectPath& object_path) OVERRIDE {
VLOG(1) << "Tag added. Creating map for record proxies belonging to "
<< "tag: " << object_path.value();
devices_and_tags_to_object_maps_.CreateObjectMap(
object_path, nfc_record::kNfcRecordServiceName, this, bus_);
}
virtual void TagRemoved(const dbus::ObjectPath& object_path) OVERRIDE {
VLOG(1) << "Tag removed. Cleaning up record proxies belonging to "
<< "tag: " << object_path.value();
devices_and_tags_to_object_maps_.RemoveObjectMap(object_path);
}
virtual void TagPropertyChanged(const dbus::ObjectPath& object_path,
const std::string& property_name) OVERRIDE {
DCHECK(device_client_);
NfcTagClient::Properties* tag_properties =
tag_client_->GetProperties(object_path);
if (property_name != tag_properties->records.name())
return;
VLOG(1) << "NFC records changed.";
const std::vector<dbus::ObjectPath>& received_records =
tag_properties->records.value();
DBusObjectMap* object_map =
devices_and_tags_to_object_maps_.GetObjectMap(object_path);
DCHECK(object_map);
object_map->UpdateObjects(received_records);
VLOG(1) << "Fetch properties for all records.";
object_map->RefreshAllProperties();
}
virtual NfcPropertySet* CreateProperties(
dbus::ObjectProxy* object_proxy) OVERRIDE {
Properties* properties = new Properties(
object_proxy,
base::Bind(&NfcRecordClientImpl::OnPropertyChanged,
weak_ptr_factory_.GetWeakPtr(),
object_proxy->object_path()));
properties->SetAllPropertiesReceivedCallback(
base::Bind(&NfcRecordClientImpl::OnPropertiesReceived,
weak_ptr_factory_.GetWeakPtr(),
object_proxy->object_path()));
return properties;
}
virtual void ObjectAdded(const dbus::ObjectPath& object_path) OVERRIDE {
FOR_EACH_OBSERVER(NfcRecordClient::Observer, observers_,
RecordAdded(object_path));
}
virtual void ObjectRemoved(const dbus::ObjectPath& object_path) OVERRIDE {
FOR_EACH_OBSERVER(NfcRecordClient::Observer, observers_,
RecordRemoved(object_path));
}
void OnPropertyChanged(const dbus::ObjectPath& object_path,
const std::string& property_name) {
VLOG(1) << "Record property changed; Path: " << object_path.value()
<< " Property: " << property_name;
FOR_EACH_OBSERVER(NfcRecordClient::Observer, observers_,
RecordPropertyChanged(object_path, property_name));
}
void OnPropertiesReceived(const dbus::ObjectPath& object_path) {
VLOG(1) << "All record properties received; Path: " << object_path.value();
FOR_EACH_OBSERVER(NfcRecordClient::Observer, observers_,
RecordPropertiesReceived(object_path));
}
dbus::Bus* bus_;
ObserverList<NfcRecordClient::Observer> observers_;
ObjectProxyTree devices_and_tags_to_object_maps_;
NfcDeviceClient* device_client_;
NfcTagClient* tag_client_;
base::WeakPtrFactory<NfcRecordClientImpl> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(NfcRecordClientImpl);
};
NfcRecordClient::NfcRecordClient() {
}
NfcRecordClient::~NfcRecordClient() {
}
NfcRecordClient* NfcRecordClient::Create(NfcDeviceClient* device_client,
NfcTagClient* tag_client) {
return new NfcRecordClientImpl(device_client, tag_client);
}
}