diff options
author | armansito@chromium.org <armansito@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-22 18:43:40 +0000 |
---|---|---|
committer | armansito@chromium.org <armansito@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-22 18:43:40 +0000 |
commit | 7d5e8cd54f9f99f20360385ab1f77b4c19d7ab16 (patch) | |
tree | 0a3f07ed29608a398bc335e04ff8f2ace10d2527 /chromeos/dbus | |
parent | 8cef993adaf14fb9bc6ce5f924255503c2f90cc7 (diff) | |
download | chromium_src-7d5e8cd54f9f99f20360385ab1f77b4c19d7ab16.zip chromium_src-7d5e8cd54f9f99f20360385ab1f77b4c19d7ab16.tar.gz chromium_src-7d5e8cd54f9f99f20360385ab1f77b4c19d7ab16.tar.bz2 |
nfc: Add D-Bus client for NFC Manager.
Added D-Bus client code for the NFC Manager interface.
BUG=305386
TEST=1. chromeos_unittests
2. Build for Linux with "chromeos=1", check logs to make sure
that fake device gets added.
3. Build for Chrome OS. Repeatedly insert and remove USB NFC adapter
dongle to make sure that the "AdapterAdded" and "AdapterRemoved"
signals are received from neard by looking at the logs.
R=keybuk@chromium.org, stevenjb@chromium.org
Review URL: https://codereview.chromium.org/26871005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@230169 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chromeos/dbus')
-rw-r--r-- | chromeos/dbus/dbus_client.h | 2 | ||||
-rw-r--r-- | chromeos/dbus/dbus_thread_manager.cc | 8 | ||||
-rw-r--r-- | chromeos/dbus/dbus_thread_manager.h | 2 | ||||
-rw-r--r-- | chromeos/dbus/fake_dbus_thread_manager.cc | 6 | ||||
-rw-r--r-- | chromeos/dbus/fake_dbus_thread_manager.h | 7 | ||||
-rw-r--r-- | chromeos/dbus/fake_nfc_manager_client.cc | 100 | ||||
-rw-r--r-- | chromeos/dbus/fake_nfc_manager_client.h | 69 | ||||
-rw-r--r-- | chromeos/dbus/mock_dbus_thread_manager.cc | 4 | ||||
-rw-r--r-- | chromeos/dbus/mock_dbus_thread_manager.h | 6 | ||||
-rw-r--r-- | chromeos/dbus/nfc_manager_client.cc | 176 | ||||
-rw-r--r-- | chromeos/dbus/nfc_manager_client.h | 73 | ||||
-rw-r--r-- | chromeos/dbus/nfc_property_set.cc | 58 | ||||
-rw-r--r-- | chromeos/dbus/nfc_property_set.h | 41 |
13 files changed, 552 insertions, 0 deletions
diff --git a/chromeos/dbus/dbus_client.h b/chromeos/dbus/dbus_client.h index 902d533..c9ac671 100644 --- a/chromeos/dbus/dbus_client.h +++ b/chromeos/dbus/dbus_client.h @@ -5,6 +5,8 @@ #ifndef CHROMEOS_DBUS_DBUS_CLIENT_H_ #define CHROMEOS_DBUS_DBUS_CLIENT_H_ +#include "base/basictypes.h" + namespace dbus { class Bus; }; diff --git a/chromeos/dbus/dbus_thread_manager.cc b/chromeos/dbus/dbus_thread_manager.cc index 46753c1..b23e2f9 100644 --- a/chromeos/dbus/dbus_thread_manager.cc +++ b/chromeos/dbus/dbus_thread_manager.cc @@ -29,6 +29,7 @@ #include "chromeos/dbus/image_burner_client.h" #include "chromeos/dbus/introspectable_client.h" #include "chromeos/dbus/modem_messaging_client.h" +#include "chromeos/dbus/nfc_manager_client.h" #include "chromeos/dbus/permission_broker_client.h" #include "chromeos/dbus/power_manager_client.h" #include "chromeos/dbus/power_policy_controller.h" @@ -101,6 +102,7 @@ class DBusThreadManagerImpl : public DBusThreadManager { InitClient(image_burner_client_.get()); InitClient(introspectable_client_.get()); InitClient(modem_messaging_client_.get()); + InitClient(nfc_manager_client_.get()); InitClient(permission_broker_client_.get()); InitClient(power_manager_client_.get()); InitClient(session_manager_client_.get()); @@ -261,6 +263,10 @@ class DBusThreadManagerImpl : public DBusThreadManager { return modem_messaging_client_.get(); } + virtual NfcManagerClient* GetNfcManagerClient() OVERRIDE { + return nfc_manager_client_.get(); + } + virtual PermissionBrokerClient* GetPermissionBrokerClient() OVERRIDE { return permission_broker_client_.get(); } @@ -354,6 +360,7 @@ class DBusThreadManagerImpl : public DBusThreadManager { image_burner_client_.reset(ImageBurnerClient::Create(client_type)); introspectable_client_.reset(IntrospectableClient::Create(client_type)); modem_messaging_client_.reset(ModemMessagingClient::Create(client_type)); + nfc_manager_client_.reset(NfcManagerClient::Create(client_type)); permission_broker_client_.reset( PermissionBrokerClient::Create(client_type)); power_manager_client_.reset( @@ -389,6 +396,7 @@ class DBusThreadManagerImpl : public DBusThreadManager { scoped_ptr<ImageBurnerClient> image_burner_client_; scoped_ptr<IntrospectableClient> introspectable_client_; scoped_ptr<ModemMessagingClient> modem_messaging_client_; + scoped_ptr<NfcManagerClient> nfc_manager_client_; scoped_ptr<PermissionBrokerClient> permission_broker_client_; scoped_ptr<SystemClockClient> system_clock_client_; scoped_ptr<PowerManagerClient> power_manager_client_; diff --git a/chromeos/dbus/dbus_thread_manager.h b/chromeos/dbus/dbus_thread_manager.h index 65f0333..78a57d1 100644 --- a/chromeos/dbus/dbus_thread_manager.h +++ b/chromeos/dbus/dbus_thread_manager.h @@ -42,6 +42,7 @@ class IBusEngineService; class ImageBurnerClient; class IntrospectableClient; class ModemMessagingClient; +class NfcManagerClient; class PermissionBrokerClient; class PowerManagerClient; class PowerPolicyController; @@ -134,6 +135,7 @@ class CHROMEOS_EXPORT DBusThreadManager { virtual ImageBurnerClient* GetImageBurnerClient() = 0; virtual IntrospectableClient* GetIntrospectableClient() = 0; virtual ModemMessagingClient* GetModemMessagingClient() = 0; + virtual NfcManagerClient* GetNfcManagerClient() = 0; virtual PermissionBrokerClient* GetPermissionBrokerClient() = 0; virtual PowerManagerClient* GetPowerManagerClient() = 0; virtual PowerPolicyController* GetPowerPolicyController() = 0; diff --git a/chromeos/dbus/fake_dbus_thread_manager.cc b/chromeos/dbus/fake_dbus_thread_manager.cc index 4378d1c..22e9ac3 100644 --- a/chromeos/dbus/fake_dbus_thread_manager.cc +++ b/chromeos/dbus/fake_dbus_thread_manager.cc @@ -14,6 +14,7 @@ #include "chromeos/dbus/fake_cryptohome_client.h" #include "chromeos/dbus/fake_gsm_sms_client.h" #include "chromeos/dbus/fake_image_burner_client.h" +#include "chromeos/dbus/fake_nfc_manager_client.h" #include "chromeos/dbus/fake_power_manager_client.h" #include "chromeos/dbus/fake_session_manager_client.h" #include "chromeos/dbus/fake_shill_device_client.h" @@ -38,6 +39,7 @@ FakeDBusThreadManager::FakeDBusThreadManager() fake_cryptohome_client_(new FakeCryptohomeClient), fake_gsm_sms_client_(new FakeGsmSMSClient), fake_image_burner_client_(new FakeImageBurnerClient), + fake_nfc_manager_client_(new FakeNfcManagerClient()), fake_session_manager_client_(new FakeSessionManagerClient), fake_shill_device_client_(new FakeShillDeviceClient), fake_shill_manager_client_(new FakeShillManagerClient), @@ -174,6 +176,10 @@ ModemMessagingClient* return NULL; } +NfcManagerClient* FakeDBusThreadManager::GetNfcManagerClient() { + return fake_nfc_manager_client_.get(); +} + PermissionBrokerClient* FakeDBusThreadManager::GetPermissionBrokerClient() { NOTIMPLEMENTED(); diff --git a/chromeos/dbus/fake_dbus_thread_manager.h b/chromeos/dbus/fake_dbus_thread_manager.h index 13addee..27ead27 100644 --- a/chromeos/dbus/fake_dbus_thread_manager.h +++ b/chromeos/dbus/fake_dbus_thread_manager.h @@ -27,6 +27,7 @@ class FakeBluetoothProfileManagerClient; class FakeCrosDisksClient; class FakeCryptohomeClient; class FakeGsmSMSClient; +class FakeNfcManagerClient; class FakePowerManagerClient; class FakeImageBurnerClient; class FakeSessionManagerClient; @@ -72,6 +73,7 @@ class FakeDBusThreadManager : public DBusThreadManager { virtual ImageBurnerClient* GetImageBurnerClient() OVERRIDE; virtual IntrospectableClient* GetIntrospectableClient() OVERRIDE; virtual ModemMessagingClient* GetModemMessagingClient() OVERRIDE; + virtual NfcManagerClient* GetNfcManagerClient() OVERRIDE; virtual PermissionBrokerClient* GetPermissionBrokerClient() OVERRIDE; virtual PowerManagerClient* GetPowerManagerClient() OVERRIDE; virtual PowerPolicyController* GetPowerPolicyController() OVERRIDE; @@ -122,6 +124,10 @@ class FakeDBusThreadManager : public DBusThreadManager { return fake_image_burner_client_.get(); } + FakeNfcManagerClient* fake_nfc_manager_client() { + return fake_nfc_manager_client_.get(); + } + FakeSessionManagerClient* fake_session_manager_client() { return fake_session_manager_client_.get(); } @@ -179,6 +185,7 @@ class FakeDBusThreadManager : public DBusThreadManager { scoped_ptr<FakeCryptohomeClient> fake_cryptohome_client_; scoped_ptr<FakeGsmSMSClient> fake_gsm_sms_client_; scoped_ptr<FakeImageBurnerClient> fake_image_burner_client_; + scoped_ptr<FakeNfcManagerClient> fake_nfc_manager_client_; scoped_ptr<FakeSessionManagerClient> fake_session_manager_client_; scoped_ptr<FakeShillDeviceClient> fake_shill_device_client_; scoped_ptr<FakeShillManagerClient> fake_shill_manager_client_; diff --git a/chromeos/dbus/fake_nfc_manager_client.cc b/chromeos/dbus/fake_nfc_manager_client.cc new file mode 100644 index 0000000..3c8564a --- /dev/null +++ b/chromeos/dbus/fake_nfc_manager_client.cc @@ -0,0 +1,100 @@ +// Copyright (c) 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_manager_client.h" + +#include "base/bind.h" +#include "base/logging.h" +#include "dbus/object_path.h" +#include "third_party/cros_system_api/dbus/service_constants.h" + +namespace chromeos { + +const char FakeNfcManagerClient::kDefaultAdapterPath[] = "/fake/nfc0"; + +FakeNfcManagerClient::Properties::Properties( + const PropertyChangedCallback& callback) + : NfcManagerClient::Properties(NULL, callback) { +} + +FakeNfcManagerClient::Properties::~Properties() { +} + +void FakeNfcManagerClient::Properties::Get( + dbus::PropertyBase* property, + dbus::PropertySet::GetCallback callback) { + VLOG(1) << "Get " << property->name(); + callback.Run(false); +} + +void FakeNfcManagerClient::Properties::GetAll() { + VLOG(1) << "GetAll"; +} + +void FakeNfcManagerClient::Properties::Set( + dbus::PropertyBase* property, + dbus::PropertySet::SetCallback callback) { + VLOG(1) << "Set " << property->name(); + callback.Run(false); +} + +FakeNfcManagerClient::FakeNfcManagerClient() { + properties_.reset(new Properties(base::Bind( + &FakeNfcManagerClient::OnPropertyChanged, base::Unretained(this)))); + AddAdapter(kDefaultAdapterPath); +} + +FakeNfcManagerClient::~FakeNfcManagerClient() { +} + +void FakeNfcManagerClient::Init(dbus::Bus* bus) { +} + +void FakeNfcManagerClient::AddObserver(Observer* observer) { + observers_.AddObserver(observer); +} + +void FakeNfcManagerClient::RemoveObserver(Observer* observer) { + observers_.RemoveObserver(observer); +} + +FakeNfcManagerClient::Properties* FakeNfcManagerClient::GetProperties() { + return properties_.get(); +} + +void FakeNfcManagerClient::AddAdapter(const std::string& adapter_path) { + VLOG(1) << "Adding NFC adapter: " << adapter_path; + dbus::ObjectPath new_adapter(adapter_path); + std::pair<std::set<dbus::ObjectPath>::iterator, bool> result = + adapters_.insert(new_adapter); + if (!result.second) { + VLOG(1) << "Adapter \"" << adapter_path << "\" already exists."; + return; + } + // Create a vector containing all object paths in the set |adapters_|. This + // will copy all members of |adapters_| to |adapters|. + std::vector<dbus::ObjectPath> adapters(adapters_.begin(), adapters_.end()); + properties_->adapters.ReplaceValue(adapters); + FOR_EACH_OBSERVER(Observer, observers_, AdapterAdded(new_adapter)); +} + +void FakeNfcManagerClient::RemoveAdapter(const std::string& adapter_path) { + VLOG(1) << "Removing NFC adapter: " << adapter_path; + dbus::ObjectPath to_remove(adapter_path); + if (adapters_.erase(to_remove) == 0) { + VLOG(1) << "No such adapter: \"" << adapter_path << "\""; + return; + } + std::vector<dbus::ObjectPath> adapters(adapters_.begin(), adapters_.end()); + properties_->adapters.ReplaceValue(adapters); + FOR_EACH_OBSERVER(Observer, observers_, AdapterRemoved(to_remove)); +} + +void FakeNfcManagerClient::OnPropertyChanged( + const std::string& property_name) { + FOR_EACH_OBSERVER(Observer, observers_, + ManagerPropertyChanged(property_name)); +} + +} // namespace chromeos diff --git a/chromeos/dbus/fake_nfc_manager_client.h b/chromeos/dbus/fake_nfc_manager_client.h new file mode 100644 index 0000000..2393fa2 --- /dev/null +++ b/chromeos/dbus/fake_nfc_manager_client.h @@ -0,0 +1,69 @@ +// Copyright (c) 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. + +#ifndef CHROMEOS_DBUS_FAKE_NFC_MANAGER_CLIENT_H_ +#define CHROMEOS_DBUS_FAKE_NFC_MANAGER_CLIENT_H_ + +#include <set> +#include <string> + +#include "base/observer_list.h" +#include "chromeos/chromeos_export.h" +#include "chromeos/dbus/nfc_manager_client.h" +#include "dbus/property.h" + +namespace chromeos { + +// FakeNfcManagerClient simulates the behavior of the NFC Daemon manager object +// and is used both in test cases in place of a mock and on the Linux desktop. +class CHROMEOS_EXPORT FakeNfcManagerClient : public NfcManagerClient { + public: + struct Properties : public NfcManagerClient::Properties { + explicit Properties(const PropertyChangedCallback& callback); + virtual ~Properties(); + + // dbus::PropertySet overrides. + virtual void Get(dbus::PropertyBase* property, + dbus::PropertySet::GetCallback callback) OVERRIDE; + virtual void GetAll() OVERRIDE; + virtual void Set(dbus::PropertyBase* property, + dbus::PropertySet::SetCallback callback) OVERRIDE; + }; + + FakeNfcManagerClient(); + virtual ~FakeNfcManagerClient(); + + // NfcManagerClient overrides. + virtual void Init(dbus::Bus* bus) OVERRIDE; + virtual void AddObserver(Observer* observer) OVERRIDE; + virtual void RemoveObserver(Observer* observer) OVERRIDE; + virtual Properties* GetProperties() OVERRIDE; + + // Methods to simulate adapters getting added and removed. + void AddAdapter(const std::string& adapter_path); + void RemoveAdapter(const std::string& adapter_path); + + // Default path of an adapter that is simulated for testing. + static const char kDefaultAdapterPath[]; + + private: + // Property callback passed when we create Properties* structures. + void OnPropertyChanged(const std::string& property_name); + + // List of observers interested in event notifications. + ObserverList<Observer> observers_; + + // Set containing the currently simulated adapters. + std::set<dbus::ObjectPath> adapters_; + + // Fake properties object. This gets updated whenever AddAdapter or + // RemoveAdapter gets called. + scoped_ptr<Properties> properties_; + + DISALLOW_COPY_AND_ASSIGN(FakeNfcManagerClient); +}; + +} // namespace chromeos + +#endif // CHROMEOS_DBUS_FAKE_NFC_MANAGER_CLIENT_H_ diff --git a/chromeos/dbus/mock_dbus_thread_manager.cc b/chromeos/dbus/mock_dbus_thread_manager.cc index a6eb16b..61cac7e 100644 --- a/chromeos/dbus/mock_dbus_thread_manager.cc +++ b/chromeos/dbus/mock_dbus_thread_manager.cc @@ -11,6 +11,7 @@ #include "chromeos/dbus/fake_bluetooth_input_client.h" #include "chromeos/dbus/fake_bluetooth_profile_manager_client.h" #include "chromeos/dbus/fake_gsm_sms_client.h" +#include "chromeos/dbus/fake_nfc_manager_client.h" #include "chromeos/dbus/fake_shill_device_client.h" #include "chromeos/dbus/fake_shill_ipconfig_client.h" #include "chromeos/dbus/ibus/mock_ibus_client.h" @@ -55,6 +56,7 @@ MockDBusThreadManager::MockDBusThreadManager() fake_bluetooth_profile_manager_client_( new FakeBluetoothProfileManagerClient), fake_gsm_sms_client_(new FakeGsmSMSClient), + fake_nfc_manager_client_(new FakeNfcManagerClient()), fake_shill_device_client_(new FakeShillDeviceClient), fake_shill_ipconfig_client_(new FakeShillIPConfigClient), mock_cryptohome_client_(new MockCryptohomeClient), @@ -74,6 +76,8 @@ MockDBusThreadManager::MockDBusThreadManager() .WillRepeatedly(Return(fake_bluetooth_input_client_.get())); EXPECT_CALL(*this, GetBluetoothProfileManagerClient()) .WillRepeatedly(Return(fake_bluetooth_profile_manager_client())); + EXPECT_CALL(*this, GetNfcManagerClient()) + .WillRepeatedly(Return(fake_nfc_manager_client())); EXPECT_CALL(*this, GetShillDeviceClient()) .WillRepeatedly(Return(fake_shill_device_client())); EXPECT_CALL(*this, GetShillIPConfigClient()) diff --git a/chromeos/dbus/mock_dbus_thread_manager.h b/chromeos/dbus/mock_dbus_thread_manager.h index 80ea9cf..029ff4e 100644 --- a/chromeos/dbus/mock_dbus_thread_manager.h +++ b/chromeos/dbus/mock_dbus_thread_manager.h @@ -26,6 +26,7 @@ class FakeBluetoothDeviceClient; class FakeBluetoothInputClient; class FakeBluetoothProfileManagerClient; class FakeGsmSMSClient; +class FakeNfcManagerClient; class FakeShillDeviceClient; class FakeShillIPConfigClient; class MockCryptohomeClient; @@ -71,6 +72,7 @@ class MockDBusThreadManager : public DBusThreadManager { MOCK_METHOD0(GetImageBurnerClient, ImageBurnerClient*(void)); MOCK_METHOD0(GetIntrospectableClient, IntrospectableClient*(void)); MOCK_METHOD0(GetModemMessagingClient, ModemMessagingClient*(void)); + MOCK_METHOD0(GetNfcManagerClient, NfcManagerClient*(void)); MOCK_METHOD0(GetPermissionBrokerClient, PermissionBrokerClient*(void)); MOCK_METHOD0(GetPowerManagerClient, PowerManagerClient*(void)); MOCK_METHOD0(GetPowerPolicyController, PowerPolicyController*(void)); @@ -103,6 +105,9 @@ class MockDBusThreadManager : public DBusThreadManager { FakeGsmSMSClient* fake_gsm_sms_client() { return fake_gsm_sms_client_.get(); } + FakeNfcManagerClient* fake_nfc_manager_client() { + return fake_nfc_manager_client_.get(); + } FakeShillDeviceClient* fake_shill_device_client() { return fake_shill_device_client_.get(); } @@ -138,6 +143,7 @@ class MockDBusThreadManager : public DBusThreadManager { scoped_ptr<FakeBluetoothProfileManagerClient> fake_bluetooth_profile_manager_client_; scoped_ptr<FakeGsmSMSClient> fake_gsm_sms_client_; + scoped_ptr<FakeNfcManagerClient> fake_nfc_manager_client_; scoped_ptr<FakeShillDeviceClient> fake_shill_device_client_; scoped_ptr<FakeShillIPConfigClient> fake_shill_ipconfig_client_; scoped_ptr<MockCryptohomeClient> mock_cryptohome_client_; diff --git a/chromeos/dbus/nfc_manager_client.cc b/chromeos/dbus/nfc_manager_client.cc new file mode 100644 index 0000000..6ee43a8 --- /dev/null +++ b/chromeos/dbus/nfc_manager_client.cc @@ -0,0 +1,176 @@ +// 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/nfc_manager_client.h" + +#include "base/memory/scoped_ptr.h" +#include "base/memory/weak_ptr.h" +#include "base/observer_list.h" +#include "chromeos/dbus/fake_nfc_manager_client.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() { +} + + +// The NfcManagerClient implementation used in production. +class NfcManagerClientImpl : public NfcManagerClient { + public: + NfcManagerClientImpl() + : object_proxy_(NULL), + weak_ptr_factory_(this) { + } + + virtual ~NfcManagerClientImpl() { + } + + // NfcManagerClient override. + virtual void AddObserver(Observer* observer) OVERRIDE { + DCHECK(observer); + observers_.AddObserver(observer); + } + + // NfcManagerClient override. + virtual void RemoveObserver(Observer* observer) OVERRIDE { + DCHECK(observer); + observers_.RemoveObserver(observer); + } + + // NfcManagerClient override. + virtual Properties* GetProperties() OVERRIDE { + return properties_.get(); + } + + protected: + // DBusClient override. + virtual void Init(dbus::Bus* bus) OVERRIDE { + VLOG(1) << "Creating NfcManagerClientImpl"; + + // Create the object proxy. + object_proxy_ = bus->GetObjectProxy( + nfc_manager::kNfcManagerServiceName, + dbus::ObjectPath(nfc_manager::kNfcManagerServicePath)); + + // Set up the signal handlers. + 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())); + + // Create the properties structure. + properties_.reset(new Properties( + object_proxy_, + base::Bind(&NfcManagerClientImpl::OnPropertyChanged, + weak_ptr_factory_.GetWeakPtr()))); + + properties_->ConnectSignals(); + properties_->GetAll(); + } + + private: + // NFC manager signal handlers. + void OnPropertyChanged(const std::string& property_name) { + VLOG(1) << "NFC Manager property changed: " << property_name; + FOR_EACH_OBSERVER(Observer, observers_, + ManagerPropertyChanged(property_name)); + } + + // Called by dbus:: when an "AdapterAdded" signal is received.. + 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)); + } + + // Called by dbus:: when the "AdapterAdded" signal is initially connected. + void AdapterAddedConnected(const std::string& interface_name, + const std::string& signal_name, + bool success) { + LOG_IF(WARNING, !success) << "Failed to connect to AdapterAdded signal."; + } + + // Called by dbus:: when an "AdapterRemoved" signal is received.. + 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)); + } + + // Called by dbus:: when the "AdapterAdded" signal is initially connected. + void AdapterRemovedConnected(const std::string& interface_name, + const std::string& signal_name, + bool success) { + LOG_IF(WARNING, !success) << "Failed to connect to AdapterRemoved signal."; + } + + // D-Bus proxy for neard Manager interface. + dbus::ObjectProxy* object_proxy_; + + // Properties for neard Manager interface. + scoped_ptr<Properties> properties_; + + // List of observers interested in event notifications. + ObserverList<NfcManagerClient::Observer> observers_; + + // Weak pointer factory for generating 'this' pointers that might live longer + // than we do. + // Note: This should remain the last member so it'll be destroyed and + // invalidate its weak pointers before any other members are destroyed. + base::WeakPtrFactory<NfcManagerClientImpl> weak_ptr_factory_; + + DISALLOW_COPY_AND_ASSIGN(NfcManagerClientImpl); +}; + +NfcManagerClient::NfcManagerClient() { +} + +NfcManagerClient::~NfcManagerClient() { +} + +// static +NfcManagerClient* NfcManagerClient::Create(DBusClientImplementationType type) { + if (type == REAL_DBUS_CLIENT_IMPLEMENTATION) + return new NfcManagerClientImpl(); + DCHECK_EQ(STUB_DBUS_CLIENT_IMPLEMENTATION, type); + return new FakeNfcManagerClient(); +} + +} // namespace chromeos diff --git a/chromeos/dbus/nfc_manager_client.h b/chromeos/dbus/nfc_manager_client.h new file mode 100644 index 0000000..0fa8f1f --- /dev/null +++ b/chromeos/dbus/nfc_manager_client.h @@ -0,0 +1,73 @@ +// 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. + +#ifndef CHROMEOS_DBUS_NFC_MANAGER_CLIENT_H_ +#define CHROMEOS_DBUS_NFC_MANAGER_CLIENT_H_ + +#include <vector> + +#include "chromeos/chromeos_export.h" +#include "chromeos/dbus/dbus_client.h" +#include "chromeos/dbus/dbus_client_implementation_type.h" +#include "chromeos/dbus/nfc_property_set.h" +#include "dbus/object_path.h" +#include "dbus/object_proxy.h" +#include "dbus/property.h" + +namespace chromeos { + +// NfcManagerClient is used to communicate with the neard Manager service. +class CHROMEOS_EXPORT NfcManagerClient : public DBusClient { + public: + // Structure of properties associated with the NFC manager. + struct Properties : public NfcPropertySet { + // List of Adapter object paths. + dbus::Property<std::vector<dbus::ObjectPath> > adapters; + + Properties(dbus::ObjectProxy* object_proxy, + const PropertyChangedCallback& callback); + virtual ~Properties(); + }; + + // Interface for observing changes to the NFC manager. Use this interface + // to be notified when NFC adapters get added or removed. + class Observer { + public: + virtual ~Observer() {} + + // Called when a new adapter with object path |object_path| is added to the + // system. + virtual void AdapterAdded(const dbus::ObjectPath& object_path) {} + + // Called when an adapter with object path |object_path| is removed from the + // system. + virtual void AdapterRemoved(const dbus::ObjectPath& object_path) {} + + // Called when the manager property with name |property_name| has acquired + // a new value. + virtual void ManagerPropertyChanged(const std::string& property_name) {} + }; + + virtual ~NfcManagerClient(); + + // Adds and removes observers for events on the NFC manager. + virtual void AddObserver(Observer* observer) = 0; + virtual void RemoveObserver(Observer* observer) = 0; + + // Obtains the properties of the NFC manager service. + virtual Properties* GetProperties() = 0; + + // Creates the instance. + static NfcManagerClient* Create(DBusClientImplementationType type); + + protected: + NfcManagerClient(); + + private: + DISALLOW_COPY_AND_ASSIGN(NfcManagerClient); +}; + +} // namespace chromeos + +#endif // CHROMEOS_DBUS_NFC_MANAGER_CLIENT_H_ diff --git a/chromeos/dbus/nfc_property_set.cc b/chromeos/dbus/nfc_property_set.cc new file mode 100644 index 0000000..2814bcf --- /dev/null +++ b/chromeos/dbus/nfc_property_set.cc @@ -0,0 +1,58 @@ +// 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/nfc_property_set.h" + +#include "base/bind.h" +#include "third_party/cros_system_api/dbus/service_constants.h" + +namespace chromeos { + +NfcPropertySet::NfcPropertySet(dbus::ObjectProxy* object_proxy, + const std::string& interface, + const PropertyChangedCallback& callback) + : dbus::PropertySet(object_proxy, interface, callback) { +} + +void NfcPropertySet::ConnectSignals() { + object_proxy()->ConnectToSignal( + interface(), + nfc_common::kPropertyChangedSignal, + base::Bind(&dbus::PropertySet::ChangedReceived, GetWeakPtr()), + base::Bind(&dbus::PropertySet::ChangedConnected, GetWeakPtr())); +} + +void NfcPropertySet::Get(dbus::PropertyBase* property, + GetCallback callback) { + NOTREACHED() << "neard does not implement Get for properties."; +} + +void NfcPropertySet::GetAll() { + dbus::MethodCall method_call( + interface(), nfc_common::kGetProperties); + object_proxy()->CallMethod(&method_call, + dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, + base::Bind(&dbus::PropertySet::OnGetAll, + GetWeakPtr())); +} + +void NfcPropertySet::Set(dbus::PropertyBase* property, + SetCallback callback) { + dbus::MethodCall method_call( + interface(), nfc_common::kSetProperty); + object_proxy()->CallMethod(&method_call, + dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, + base::Bind(&dbus::PropertySet::OnSet, + GetWeakPtr(), + property, + callback)); +} + +void NfcPropertySet::ChangedReceived(dbus::Signal* signal) { + DCHECK(signal); + dbus::MessageReader reader(signal); + UpdatePropertyFromReader(&reader); +} + +} // namespace chromeos diff --git a/chromeos/dbus/nfc_property_set.h b/chromeos/dbus/nfc_property_set.h new file mode 100644 index 0000000..6560d4d --- /dev/null +++ b/chromeos/dbus/nfc_property_set.h @@ -0,0 +1,41 @@ +// 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. + +#ifndef CHROMEOS_DBUS_NFC_PROPERTY_SET_H_ +#define CHROMEOS_DBUS_NFC_PROPERTY_SET_H_ + +#include <string> + +#include "dbus/message.h" +#include "dbus/object_proxy.h" +#include "dbus/property.h" + +namespace chromeos { + +// neard doesn't use the standard D-Bus interfaces for property access and +// instead defines property accessor methods in each D-Bus interface. This +// class customizes dbus::PropertySet to generate the correct method call to +// get all properties, connect to the correct signal and parse it correctly. +class NfcPropertySet : public dbus::PropertySet { + public: + NfcPropertySet(dbus::ObjectProxy* object_proxy, + const std::string& interface, + const PropertyChangedCallback& callback); + + // dbus::PropertySet overrides + virtual void ConnectSignals() OVERRIDE; + virtual void Get(dbus::PropertyBase* property, + GetCallback callback) OVERRIDE; + virtual void GetAll() OVERRIDE; + virtual void Set(dbus::PropertyBase* property, + SetCallback callback) OVERRIDE; + virtual void ChangedReceived(dbus::Signal* signal) OVERRIDE; + + private: + DISALLOW_COPY_AND_ASSIGN(NfcPropertySet); +}; + +} // namespace chromeos + +#endif // CHROMEOS_DBUS_NFC_PROPERTY_SET_H_ |