[chromedriver] Drop support for M39 and add support for M42.
[chromium-blink-merge.git] / chromeos / dbus / nfc_device_client.cc
blob8d185c257e41b023367cf12c5429be9a7f04920c
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"
7 #include "base/bind.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"
12 #include "dbus/bus.h"
13 #include "dbus/message.h"
14 #include "dbus/values_util.h"
15 #include "third_party/cros_system_api/dbus/service_constants.h"
17 using chromeos::nfc_client_helpers::DBusObjectMap;
18 using chromeos::nfc_client_helpers::ObjectProxyTree;
20 namespace chromeos {
22 NfcDeviceClient::Properties::Properties(
23 dbus::ObjectProxy* object_proxy,
24 const PropertyChangedCallback& callback)
25 : NfcPropertySet(object_proxy,
26 nfc_device::kNfcDeviceInterface,
27 callback) {
28 RegisterProperty(nfc_device::kRecordsProperty, &records);
31 NfcDeviceClient::Properties::~Properties() {
34 // The NfcDeviceClient implementation used in production.
35 class NfcDeviceClientImpl : public NfcDeviceClient,
36 public NfcAdapterClient::Observer,
37 public DBusObjectMap::Delegate {
38 public:
39 explicit NfcDeviceClientImpl(NfcAdapterClient* adapter_client)
40 : bus_(NULL),
41 adapter_client_(adapter_client),
42 weak_ptr_factory_(this) {
43 DCHECK(adapter_client);
46 ~NfcDeviceClientImpl() override {
47 DCHECK(adapter_client_);
48 adapter_client_->RemoveObserver(this);
51 // NfcDeviceClient override.
52 void AddObserver(NfcDeviceClient::Observer* observer) override {
53 DCHECK(observer);
54 observers_.AddObserver(observer);
57 // NfcDeviceClient override.
58 void RemoveObserver(NfcDeviceClient::Observer* observer) override {
59 DCHECK(observer);
60 observers_.RemoveObserver(observer);
63 // NfcDeviceClient override.
64 std::vector<dbus::ObjectPath> GetDevicesForAdapter(
65 const dbus::ObjectPath& adapter_path) override {
66 DBusObjectMap* object_map =
67 adapters_to_object_maps_.GetObjectMap(adapter_path);
68 if (!object_map)
69 return std::vector<dbus::ObjectPath>();
70 return object_map->GetObjectPaths();
73 // NfcDeviceClient override.
74 Properties* GetProperties(const dbus::ObjectPath& object_path) override {
75 return static_cast<Properties*>(
76 adapters_to_object_maps_.FindObjectProperties(object_path));
79 // NfcDeviceClient override.
80 void Push(const dbus::ObjectPath& object_path,
81 const base::DictionaryValue& attributes,
82 const base::Closure& callback,
83 const nfc_client_helpers::ErrorCallback& error_callback) override {
84 dbus::ObjectProxy* object_proxy =
85 adapters_to_object_maps_.FindObjectProxy(object_path);
86 if (!object_proxy) {
87 std::string error_message =
88 base::StringPrintf(
89 "NFC device with object path \"%s\" does not exist.",
90 object_path.value().c_str());
91 LOG(ERROR) << error_message;
92 error_callback.Run(nfc_client_helpers::kUnknownObjectError,
93 error_message);
94 return;
97 // |attributes| should not be empty.
98 if (attributes.empty()) {
99 std::string error_message =
100 "Cannot push data to device with empty arguments.";
101 LOG(ERROR) << error_message;
102 error_callback.Run(nfc_error::kInvalidArguments, error_message);
103 return;
106 // Create the arguments.
107 dbus::MethodCall method_call(nfc_device::kNfcDeviceInterface,
108 nfc_device::kPush);
109 dbus::MessageWriter writer(&method_call);
110 dbus::AppendValueData(&writer, attributes);
112 object_proxy->CallMethodWithErrorCallback(
113 &method_call,
114 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
115 base::Bind(&nfc_client_helpers::OnSuccess, callback),
116 base::Bind(&nfc_client_helpers::OnError, error_callback));
119 protected:
120 // DBusClient override.
121 void Init(dbus::Bus* bus) override {
122 VLOG(1) << "Creating NfcDeviceClientImpl";
123 DCHECK(bus);
124 bus_ = bus;
125 DCHECK(adapter_client_);
126 adapter_client_->AddObserver(this);
129 private:
130 // NfcAdapterClient::Observer override.
131 void AdapterAdded(const dbus::ObjectPath& object_path) override {
132 VLOG(1) << "Adapter added. Creating map for device proxies belonging to "
133 << "adapter: " << object_path.value();
134 adapters_to_object_maps_.CreateObjectMap(
135 object_path, nfc_device::kNfcDeviceServiceName, this, bus_);
138 // NfcAdapterClient::Observer override.
139 void AdapterRemoved(const dbus::ObjectPath& object_path) override {
140 // Neard doesn't send out property changed signals for the devices that
141 // are removed when the adapter they belong to is removed. Clean up the
142 // object proxies for devices that are managed by the removed adapter.
143 // Note: DBusObjectMap guarantees that the Properties structure for the
144 // removed adapter will be valid before this method returns.
145 VLOG(1) << "Adapter removed. Cleaning up device proxies belonging to "
146 << "adapter: " << object_path.value();
147 adapters_to_object_maps_.RemoveObjectMap(object_path);
150 // NfcAdapterClient::Observer override.
151 void AdapterPropertyChanged(const dbus::ObjectPath& object_path,
152 const std::string& property_name) override {
153 DCHECK(adapter_client_);
154 NfcAdapterClient::Properties *adapter_properties =
155 adapter_client_->GetProperties(object_path);
156 DCHECK(adapter_properties);
158 // Ignore changes to properties other than "Devices".
159 if (property_name != adapter_properties->devices.name())
160 return;
162 // Update the known devices.
163 VLOG(1) << "NFC devices changed.";
164 const std::vector<dbus::ObjectPath>& received_devices =
165 adapter_properties->devices.value();
166 DBusObjectMap* object_map =
167 adapters_to_object_maps_.GetObjectMap(object_path);
168 DCHECK(object_map);
169 object_map->UpdateObjects(received_devices);
172 // nfc_client_helpers::DBusObjectMap::Delegate override.
173 NfcPropertySet* CreateProperties(dbus::ObjectProxy* object_proxy) override {
174 return new Properties(
175 object_proxy,
176 base::Bind(&NfcDeviceClientImpl::OnPropertyChanged,
177 weak_ptr_factory_.GetWeakPtr(),
178 object_proxy->object_path()));
181 // nfc_client_helpers::DBusObjectMap::Delegate override.
182 void ObjectAdded(const dbus::ObjectPath& object_path) override {
183 FOR_EACH_OBSERVER(NfcDeviceClient::Observer, observers_,
184 DeviceAdded(object_path));
187 void ObjectRemoved(const dbus::ObjectPath& object_path) override {
188 FOR_EACH_OBSERVER(NfcDeviceClient::Observer, observers_,
189 DeviceRemoved(object_path));
192 // Called by NfcPropertySet when a property value is changed, either by
193 // result of a signal or response to a GetAll() or Get() call.
194 void OnPropertyChanged(const dbus::ObjectPath& object_path,
195 const std::string& property_name) {
196 VLOG(1) << "Device property changed; Path: " << object_path.value()
197 << " Property: " << property_name;
198 FOR_EACH_OBSERVER(NfcDeviceClient::Observer, observers_,
199 DevicePropertyChanged(object_path, property_name));
202 // We maintain a pointer to the bus to be able to request proxies for
203 // new NFC devices that appear.
204 dbus::Bus* bus_;
206 // List of observers interested in event notifications.
207 ObserverList<NfcDeviceClient::Observer> observers_;
209 // Mapping from object paths to object proxies and properties structures that
210 // were already created by us. This stucture stores a different DBusObjectMap
211 // for each known NFC adapter object path.
212 ObjectProxyTree adapters_to_object_maps_;
214 // The adapter client that we listen to events notifications from.
215 NfcAdapterClient* adapter_client_;
217 // Weak pointer factory for generating 'this' pointers that might live longer
218 // than we do.
219 // Note: This should remain the last member so it'll be destroyed and
220 // invalidate its weak pointers before any other members are destroyed.
221 base::WeakPtrFactory<NfcDeviceClientImpl> weak_ptr_factory_;
223 DISALLOW_COPY_AND_ASSIGN(NfcDeviceClientImpl);
226 NfcDeviceClient::NfcDeviceClient() {
229 NfcDeviceClient::~NfcDeviceClient() {
232 NfcDeviceClient* NfcDeviceClient::Create(NfcAdapterClient* adapter_client) {
233 return new NfcDeviceClientImpl(adapter_client);
236 } // namespace chromeos