This source file includes following definitions.
- weak_ptr_factory_
- AddObserver
- RemoveObserver
- GetProperties
- Init
- OnPropertyChanged
- AdapterAddedReceived
- AdapterAddedConnected
- AdapterRemovedReceived
- AdapterRemovedConnected
- Create
#include "chromeos/dbus/nfc_manager_client.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "dbus/bus.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
namespace chromeos {
NfcManagerClient::Properties::Properties(
dbus::ObjectProxy* object_proxy,
const PropertyChangedCallback& callback)
: NfcPropertySet(object_proxy,
nfc_manager::kNfcManagerInterface,
callback) {
RegisterProperty(nfc_manager::kAdaptersProperty, &adapters);
}
NfcManagerClient::Properties::~Properties() {
}
class NfcManagerClientImpl : public NfcManagerClient {
public:
NfcManagerClientImpl()
: object_proxy_(NULL),
weak_ptr_factory_(this) {
}
virtual ~NfcManagerClientImpl() {
}
virtual void AddObserver(Observer* observer) OVERRIDE {
DCHECK(observer);
observers_.AddObserver(observer);
}
virtual void RemoveObserver(Observer* observer) OVERRIDE {
DCHECK(observer);
observers_.RemoveObserver(observer);
}
virtual Properties* GetProperties() OVERRIDE {
return properties_.get();
}
protected:
virtual void Init(dbus::Bus* bus) OVERRIDE {
VLOG(1) << "Creating NfcManagerClientImpl";
object_proxy_ = bus->GetObjectProxy(
nfc_manager::kNfcManagerServiceName,
dbus::ObjectPath(nfc_manager::kNfcManagerServicePath));
object_proxy_->ConnectToSignal(
nfc_manager::kNfcManagerInterface,
nfc_manager::kAdapterAddedSignal,
base::Bind(&NfcManagerClientImpl::AdapterAddedReceived,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(&NfcManagerClientImpl::AdapterAddedConnected,
weak_ptr_factory_.GetWeakPtr()));
object_proxy_->ConnectToSignal(
nfc_manager::kNfcManagerInterface,
nfc_manager::kAdapterRemovedSignal,
base::Bind(&NfcManagerClientImpl::AdapterRemovedReceived,
weak_ptr_factory_.GetWeakPtr()),
base::Bind(&NfcManagerClientImpl::AdapterRemovedConnected,
weak_ptr_factory_.GetWeakPtr()));
properties_.reset(new Properties(
object_proxy_,
base::Bind(&NfcManagerClientImpl::OnPropertyChanged,
weak_ptr_factory_.GetWeakPtr())));
properties_->ConnectSignals();
properties_->GetAll();
}
private:
void OnPropertyChanged(const std::string& property_name) {
VLOG(1) << "NFC Manager property changed: " << property_name;
FOR_EACH_OBSERVER(Observer, observers_,
ManagerPropertyChanged(property_name));
}
void AdapterAddedReceived(dbus::Signal* signal) {
DCHECK(signal);
dbus::MessageReader reader(signal);
dbus::ObjectPath object_path;
if (!reader.PopObjectPath(&object_path)) {
LOG(WARNING) << "AdapterAdded signal has incorrect parameters: "
<< signal->ToString();
return;
}
VLOG(1) << "Adapter added: " << object_path.value();
FOR_EACH_OBSERVER(Observer, observers_, AdapterAdded(object_path));
}
void AdapterAddedConnected(const std::string& interface_name,
const std::string& signal_name,
bool success) {
LOG_IF(WARNING, !success) << "Failed to connect to AdapterAdded signal.";
}
void AdapterRemovedReceived(dbus::Signal* signal) {
DCHECK(signal);
dbus::MessageReader reader(signal);
dbus::ObjectPath object_path;
if (!reader.PopObjectPath(&object_path)) {
LOG(WARNING) << "AdapterRemoved signal has incorrect parameters: "
<< signal->ToString();
return;
}
VLOG(1) << "Adapter removed: " << object_path.value();
FOR_EACH_OBSERVER(Observer, observers_, AdapterRemoved(object_path));
}
void AdapterRemovedConnected(const std::string& interface_name,
const std::string& signal_name,
bool success) {
LOG_IF(WARNING, !success) << "Failed to connect to AdapterRemoved signal.";
}
dbus::ObjectProxy* object_proxy_;
scoped_ptr<Properties> properties_;
ObserverList<NfcManagerClient::Observer> observers_;
base::WeakPtrFactory<NfcManagerClientImpl> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(NfcManagerClientImpl);
};
NfcManagerClient::NfcManagerClient() {
}
NfcManagerClient::~NfcManagerClient() {
}
NfcManagerClient* NfcManagerClient::Create() {
return new NfcManagerClientImpl();
}
}