nfc_manager_client.cc revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Copyright 2013 The Chromium Authors. All rights reserved.
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// Use of this source code is governed by a BSD-style license that can be
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// found in the LICENSE file.
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "chromeos/dbus/nfc_manager_client.h"
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "base/memory/scoped_ptr.h"
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "base/memory/weak_ptr.h"
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "base/observer_list.h"
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "dbus/bus.h"
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "third_party/cros_system_api/dbus/service_constants.h"
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace chromeos {
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
153c827367444ee418f129b2c238299f49d3264554Jarkko PoyryNfcManagerClient::Properties::Properties(
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    dbus::ObjectProxy* object_proxy,
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    const PropertyChangedCallback& callback)
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    : NfcPropertySet(object_proxy,
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                     nfc_manager::kNfcManagerInterface,
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                     callback) {
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  RegisterProperty(nfc_manager::kAdaptersProperty, &adapters);
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
243c827367444ee418f129b2c238299f49d3264554Jarkko PoyryNfcManagerClient::Properties::~Properties() {
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// The NfcManagerClient implementation used in production.
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass NfcManagerClientImpl : public NfcManagerClient {
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry public:
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  NfcManagerClientImpl()
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      : object_proxy_(NULL),
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        weak_ptr_factory_(this) {
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  virtual ~NfcManagerClientImpl() {
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // NfcManagerClient override.
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  virtual void AddObserver(Observer* observer) OVERRIDE {
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    DCHECK(observer);
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    observers_.AddObserver(observer);
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // NfcManagerClient override.
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  virtual void RemoveObserver(Observer* observer) OVERRIDE {
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    DCHECK(observer);
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    observers_.RemoveObserver(observer);
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // NfcManagerClient override.
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  virtual Properties* GetProperties() OVERRIDE {
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    return properties_.get();
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry protected:
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // DBusClient override.
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  virtual void Init(dbus::Bus* bus) OVERRIDE {
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    VLOG(1) << "Creating NfcManagerClientImpl";
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // Create the object proxy.
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    object_proxy_ = bus->GetObjectProxy(
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        nfc_manager::kNfcManagerServiceName,
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        dbus::ObjectPath(nfc_manager::kNfcManagerServicePath));
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // Set up the signal handlers.
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    object_proxy_->ConnectToSignal(
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        nfc_manager::kNfcManagerInterface,
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        nfc_manager::kAdapterAddedSignal,
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        base::Bind(&NfcManagerClientImpl::AdapterAddedReceived,
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                   weak_ptr_factory_.GetWeakPtr()),
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        base::Bind(&NfcManagerClientImpl::AdapterAddedConnected,
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                   weak_ptr_factory_.GetWeakPtr()));
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    object_proxy_->ConnectToSignal(
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        nfc_manager::kNfcManagerInterface,
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        nfc_manager::kAdapterRemovedSignal,
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        base::Bind(&NfcManagerClientImpl::AdapterRemovedReceived,
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                   weak_ptr_factory_.GetWeakPtr()),
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        base::Bind(&NfcManagerClientImpl::AdapterRemovedConnected,
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                   weak_ptr_factory_.GetWeakPtr()));
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    // Create the properties structure.
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    properties_.reset(new Properties(
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        object_proxy_,
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry        base::Bind(&NfcManagerClientImpl::OnPropertyChanged,
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                   weak_ptr_factory_.GetWeakPtr())));
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    properties_->ConnectSignals();
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    properties_->GetAll();
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry private:
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // NFC manager signal handlers.
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  void OnPropertyChanged(const std::string& property_name) {
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    VLOG(1) << "NFC Manager property changed: " << property_name;
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    FOR_EACH_OBSERVER(Observer, observers_,
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                      ManagerPropertyChanged(property_name));
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Called by dbus:: when an "AdapterAdded" signal is received..
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  void AdapterAddedReceived(dbus::Signal* signal) {
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    DCHECK(signal);
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    dbus::MessageReader reader(signal);
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    dbus::ObjectPath object_path;
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (!reader.PopObjectPath(&object_path)) {
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      LOG(WARNING) << "AdapterAdded signal has incorrect parameters: "
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                   << signal->ToString();
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      return;
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    VLOG(1) << "Adapter added: " << object_path.value();
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    FOR_EACH_OBSERVER(Observer, observers_, AdapterAdded(object_path));
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Called by dbus:: when the "AdapterAdded" signal is initially connected.
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  void AdapterAddedConnected(const std::string& interface_name,
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                             const std::string& signal_name,
1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                             bool success) {
1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    LOG_IF(WARNING, !success) << "Failed to connect to AdapterAdded signal.";
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Called by dbus:: when an "AdapterRemoved" signal is received..
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  void AdapterRemovedReceived(dbus::Signal* signal) {
1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    DCHECK(signal);
1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    dbus::MessageReader reader(signal);
1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    dbus::ObjectPath object_path;
1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    if (!reader.PopObjectPath(&object_path)) {
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      LOG(WARNING) << "AdapterRemoved signal has incorrect parameters: "
1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                   << signal->ToString();
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry      return;
1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    }
1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    VLOG(1) << "Adapter removed: " << object_path.value();
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    FOR_EACH_OBSERVER(Observer, observers_, AdapterRemoved(object_path));
1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Called by dbus:: when the "AdapterAdded" signal is initially connected.
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  void AdapterRemovedConnected(const std::string& interface_name,
1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                               const std::string& signal_name,
1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry                               bool success) {
1403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry    LOG_IF(WARNING, !success) << "Failed to connect to AdapterRemoved signal.";
1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  }
1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // D-Bus proxy for neard Manager interface.
1443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  dbus::ObjectProxy* object_proxy_;
1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Properties for neard Manager interface.
1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  scoped_ptr<Properties> properties_;
1483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // List of observers interested in event notifications.
1503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  ObserverList<NfcManagerClient::Observer> observers_;
1513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Weak pointer factory for generating 'this' pointers that might live longer
1533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // than we do.
1543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // Note: This should remain the last member so it'll be destroyed and
1553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  // invalidate its weak pointers before any other members are destroyed.
1563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  base::WeakPtrFactory<NfcManagerClientImpl> weak_ptr_factory_;
1573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  DISALLOW_COPY_AND_ASSIGN(NfcManagerClientImpl);
1593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
1603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1613c827367444ee418f129b2c238299f49d3264554Jarkko PoyryNfcManagerClient::NfcManagerClient() {
1623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1643c827367444ee418f129b2c238299f49d3264554Jarkko PoyryNfcManagerClient::~NfcManagerClient() {
1653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry// static
1683c827367444ee418f129b2c238299f49d3264554Jarkko PoyryNfcManagerClient* NfcManagerClient::Create() {
1693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry  return new NfcManagerClientImpl();
1703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}  // namespace chromeos
1733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry