1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chromeos/dbus/nfc_device_client.h"
8 #include "base/memory/weak_ptr.h"
9 #include "base/observer_list.h"
10 #include "base/strings/stringprintf.h"
11 #include "chromeos/dbus/nfc_adapter_client.h"
13 #include "dbus/message.h"
14 #include "third_party/cros_system_api/dbus/service_constants.h"
16 using chromeos::nfc_client_helpers::DBusObjectMap
;
17 using chromeos::nfc_client_helpers::ObjectProxyTree
;
21 NfcDeviceClient::Properties::Properties(
22 dbus::ObjectProxy
* object_proxy
,
23 const PropertyChangedCallback
& callback
)
24 : NfcPropertySet(object_proxy
,
25 nfc_device::kNfcDeviceInterface
,
27 RegisterProperty(nfc_device::kRecordsProperty
, &records
);
30 NfcDeviceClient::Properties::~Properties() {
33 // The NfcDeviceClient implementation used in production.
34 class NfcDeviceClientImpl
: public NfcDeviceClient
,
35 public NfcAdapterClient::Observer
,
36 public DBusObjectMap::Delegate
{
38 explicit NfcDeviceClientImpl(NfcAdapterClient
* adapter_client
)
40 adapter_client_(adapter_client
),
41 weak_ptr_factory_(this) {
42 DCHECK(adapter_client
);
45 virtual ~NfcDeviceClientImpl() {
46 DCHECK(adapter_client_
);
47 adapter_client_
->RemoveObserver(this);
50 // NfcDeviceClient override.
51 virtual void AddObserver(NfcDeviceClient::Observer
* observer
) OVERRIDE
{
53 observers_
.AddObserver(observer
);
56 // NfcDeviceClient override.
57 virtual void RemoveObserver(NfcDeviceClient::Observer
* observer
) OVERRIDE
{
59 observers_
.RemoveObserver(observer
);
62 // NfcDeviceClient override.
63 virtual std::vector
<dbus::ObjectPath
> GetDevicesForAdapter(
64 const dbus::ObjectPath
& adapter_path
) OVERRIDE
{
65 DBusObjectMap
* object_map
=
66 adapters_to_object_maps_
.GetObjectMap(adapter_path
);
68 return std::vector
<dbus::ObjectPath
>();
69 return object_map
->GetObjectPaths();
72 // NfcDeviceClient override.
73 virtual Properties
* GetProperties(
74 const dbus::ObjectPath
& object_path
) OVERRIDE
{
75 return static_cast<Properties
*>(
76 adapters_to_object_maps_
.FindObjectProperties(object_path
));
79 // NfcDeviceClient override.
81 const dbus::ObjectPath
& object_path
,
82 const base::DictionaryValue
& attributes
,
83 const base::Closure
& callback
,
84 const nfc_client_helpers::ErrorCallback
& error_callback
) OVERRIDE
{
85 dbus::ObjectProxy
* object_proxy
=
86 adapters_to_object_maps_
.FindObjectProxy(object_path
);
88 std::string error_message
=
90 "NFC device with object path \"%s\" does not exist.",
91 object_path
.value().c_str());
92 LOG(ERROR
) << error_message
;
93 error_callback
.Run(nfc_client_helpers::kUnknownObjectError
,
98 // |attributes| should not be empty.
99 if (attributes
.empty()) {
100 std::string error_message
=
101 "Cannot push data to device with empty arguments.";
102 LOG(ERROR
) << error_message
;
103 error_callback
.Run(nfc_error::kInvalidArguments
, error_message
);
107 // Create the arguments.
108 dbus::MethodCall
method_call(nfc_device::kNfcDeviceInterface
,
110 dbus::MessageWriter
writer(&method_call
);
111 dbus::MessageWriter
array_writer(NULL
);
112 dbus::MessageWriter
dict_entry_writer(NULL
);
113 writer
.OpenArray("{sv}", &array_writer
);
114 for (base::DictionaryValue::Iterator
iter(attributes
);
115 !iter
.IsAtEnd(); iter
.Advance()) {
116 array_writer
.OpenDictEntry(&dict_entry_writer
);
117 dict_entry_writer
.AppendString(iter
.key());
118 nfc_client_helpers::AppendValueDataAsVariant(&dict_entry_writer
,
120 array_writer
.CloseContainer(&dict_entry_writer
);
122 writer
.CloseContainer(&array_writer
);
124 object_proxy
->CallMethodWithErrorCallback(
126 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT
,
127 base::Bind(&nfc_client_helpers::OnSuccess
, callback
),
128 base::Bind(&nfc_client_helpers::OnError
, error_callback
));
132 // DBusClient override.
133 virtual void Init(dbus::Bus
* bus
) OVERRIDE
{
134 VLOG(1) << "Creating NfcDeviceClientImpl";
137 DCHECK(adapter_client_
);
138 adapter_client_
->AddObserver(this);
142 // NfcAdapterClient::Observer override.
143 virtual void AdapterAdded(const dbus::ObjectPath
& object_path
) OVERRIDE
{
144 VLOG(1) << "Adapter added. Creating map for device proxies belonging to "
145 << "adapter: " << object_path
.value();
146 adapters_to_object_maps_
.CreateObjectMap(
147 object_path
, nfc_device::kNfcDeviceServiceName
, this, bus_
);
150 // NfcAdapterClient::Observer override.
151 virtual void AdapterRemoved(const dbus::ObjectPath
& object_path
) OVERRIDE
{
152 // Neard doesn't send out property changed signals for the devices that
153 // are removed when the adapter they belong to is removed. Clean up the
154 // object proxies for devices that are managed by the removed adapter.
155 // Note: DBusObjectMap guarantees that the Properties structure for the
156 // removed adapter will be valid before this method returns.
157 VLOG(1) << "Adapter removed. Cleaning up device proxies belonging to "
158 << "adapter: " << object_path
.value();
159 adapters_to_object_maps_
.RemoveObjectMap(object_path
);
162 // NfcAdapterClient::Observer override.
163 virtual void AdapterPropertyChanged(
164 const dbus::ObjectPath
& object_path
,
165 const std::string
& property_name
) OVERRIDE
{
166 DCHECK(adapter_client_
);
167 NfcAdapterClient::Properties
*adapter_properties
=
168 adapter_client_
->GetProperties(object_path
);
169 DCHECK(adapter_properties
);
171 // Ignore changes to properties other than "Devices".
172 if (property_name
!= adapter_properties
->devices
.name())
175 // Update the known devices.
176 VLOG(1) << "NFC devices changed.";
177 const std::vector
<dbus::ObjectPath
>& received_devices
=
178 adapter_properties
->devices
.value();
179 DBusObjectMap
* object_map
=
180 adapters_to_object_maps_
.GetObjectMap(object_path
);
182 object_map
->UpdateObjects(received_devices
);
185 // nfc_client_helpers::DBusObjectMap::Delegate override.
186 virtual NfcPropertySet
* CreateProperties(
187 dbus::ObjectProxy
* object_proxy
) OVERRIDE
{
188 return new Properties(
190 base::Bind(&NfcDeviceClientImpl::OnPropertyChanged
,
191 weak_ptr_factory_
.GetWeakPtr(),
192 object_proxy
->object_path()));
195 // nfc_client_helpers::DBusObjectMap::Delegate override.
196 virtual void ObjectAdded(const dbus::ObjectPath
& object_path
) OVERRIDE
{
197 FOR_EACH_OBSERVER(NfcDeviceClient::Observer
, observers_
,
198 DeviceAdded(object_path
));
201 virtual void ObjectRemoved(const dbus::ObjectPath
& object_path
) OVERRIDE
{
202 FOR_EACH_OBSERVER(NfcDeviceClient::Observer
, observers_
,
203 DeviceRemoved(object_path
));
206 // Called by NfcPropertySet when a property value is changed, either by
207 // result of a signal or response to a GetAll() or Get() call.
208 void OnPropertyChanged(const dbus::ObjectPath
& object_path
,
209 const std::string
& property_name
) {
210 VLOG(1) << "Device property changed; Path: " << object_path
.value()
211 << " Property: " << property_name
;
212 FOR_EACH_OBSERVER(NfcDeviceClient::Observer
, observers_
,
213 DevicePropertyChanged(object_path
, property_name
));
216 // We maintain a pointer to the bus to be able to request proxies for
217 // new NFC devices that appear.
220 // List of observers interested in event notifications.
221 ObserverList
<NfcDeviceClient::Observer
> observers_
;
223 // Mapping from object paths to object proxies and properties structures that
224 // were already created by us. This stucture stores a different DBusObjectMap
225 // for each known NFC adapter object path.
226 ObjectProxyTree adapters_to_object_maps_
;
228 // The adapter client that we listen to events notifications from.
229 NfcAdapterClient
* adapter_client_
;
231 // Weak pointer factory for generating 'this' pointers that might live longer
233 // Note: This should remain the last member so it'll be destroyed and
234 // invalidate its weak pointers before any other members are destroyed.
235 base::WeakPtrFactory
<NfcDeviceClientImpl
> weak_ptr_factory_
;
237 DISALLOW_COPY_AND_ASSIGN(NfcDeviceClientImpl
);
240 NfcDeviceClient::NfcDeviceClient() {
243 NfcDeviceClient::~NfcDeviceClient() {
246 NfcDeviceClient
* NfcDeviceClient::Create(NfcAdapterClient
* adapter_client
) {
247 return new NfcDeviceClientImpl(adapter_client
);
250 } // namespace chromeos