| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chromeos/dbus/nfc_device_client.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/macros.h" | |
| 9 #include "base/memory/weak_ptr.h" | |
| 10 #include "base/observer_list.h" | |
| 11 #include "base/strings/stringprintf.h" | |
| 12 #include "chromeos/dbus/nfc_adapter_client.h" | |
| 13 #include "dbus/bus.h" | |
| 14 #include "dbus/message.h" | |
| 15 #include "dbus/values_util.h" | |
| 16 #include "third_party/cros_system_api/dbus/service_constants.h" | |
| 17 | |
| 18 using chromeos::nfc_client_helpers::DBusObjectMap; | |
| 19 using chromeos::nfc_client_helpers::ObjectProxyTree; | |
| 20 | |
| 21 namespace chromeos { | |
| 22 | |
| 23 NfcDeviceClient::Properties::Properties( | |
| 24 dbus::ObjectProxy* object_proxy, | |
| 25 const PropertyChangedCallback& callback) | |
| 26 : NfcPropertySet(object_proxy, | |
| 27 nfc_device::kNfcDeviceInterface, | |
| 28 callback) { | |
| 29 RegisterProperty(nfc_device::kRecordsProperty, &records); | |
| 30 } | |
| 31 | |
| 32 NfcDeviceClient::Properties::~Properties() { | |
| 33 } | |
| 34 | |
| 35 // The NfcDeviceClient implementation used in production. | |
| 36 class NfcDeviceClientImpl : public NfcDeviceClient, | |
| 37 public NfcAdapterClient::Observer, | |
| 38 public DBusObjectMap::Delegate { | |
| 39 public: | |
| 40 explicit NfcDeviceClientImpl(NfcAdapterClient* adapter_client) | |
| 41 : bus_(NULL), | |
| 42 adapter_client_(adapter_client), | |
| 43 weak_ptr_factory_(this) { | |
| 44 DCHECK(adapter_client); | |
| 45 } | |
| 46 | |
| 47 ~NfcDeviceClientImpl() override { | |
| 48 DCHECK(adapter_client_); | |
| 49 adapter_client_->RemoveObserver(this); | |
| 50 } | |
| 51 | |
| 52 // NfcDeviceClient override. | |
| 53 void AddObserver(NfcDeviceClient::Observer* observer) override { | |
| 54 DCHECK(observer); | |
| 55 observers_.AddObserver(observer); | |
| 56 } | |
| 57 | |
| 58 // NfcDeviceClient override. | |
| 59 void RemoveObserver(NfcDeviceClient::Observer* observer) override { | |
| 60 DCHECK(observer); | |
| 61 observers_.RemoveObserver(observer); | |
| 62 } | |
| 63 | |
| 64 // NfcDeviceClient override. | |
| 65 std::vector<dbus::ObjectPath> GetDevicesForAdapter( | |
| 66 const dbus::ObjectPath& adapter_path) override { | |
| 67 DBusObjectMap* object_map = | |
| 68 adapters_to_object_maps_.GetObjectMap(adapter_path); | |
| 69 if (!object_map) | |
| 70 return std::vector<dbus::ObjectPath>(); | |
| 71 return object_map->GetObjectPaths(); | |
| 72 } | |
| 73 | |
| 74 // NfcDeviceClient override. | |
| 75 Properties* GetProperties(const dbus::ObjectPath& object_path) override { | |
| 76 return static_cast<Properties*>( | |
| 77 adapters_to_object_maps_.FindObjectProperties(object_path)); | |
| 78 } | |
| 79 | |
| 80 // NfcDeviceClient override. | |
| 81 void Push(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); | |
| 87 if (!object_proxy) { | |
| 88 std::string error_message = | |
| 89 base::StringPrintf( | |
| 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, | |
| 94 error_message); | |
| 95 return; | |
| 96 } | |
| 97 | |
| 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); | |
| 104 return; | |
| 105 } | |
| 106 | |
| 107 // Create the arguments. | |
| 108 dbus::MethodCall method_call(nfc_device::kNfcDeviceInterface, | |
| 109 nfc_device::kPush); | |
| 110 dbus::MessageWriter writer(&method_call); | |
| 111 dbus::AppendValueData(&writer, attributes); | |
| 112 | |
| 113 object_proxy->CallMethodWithErrorCallback( | |
| 114 &method_call, | |
| 115 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 116 base::Bind(&nfc_client_helpers::OnSuccess, callback), | |
| 117 base::Bind(&nfc_client_helpers::OnError, error_callback)); | |
| 118 } | |
| 119 | |
| 120 protected: | |
| 121 // DBusClient override. | |
| 122 void Init(dbus::Bus* bus) override { | |
| 123 VLOG(1) << "Creating NfcDeviceClientImpl"; | |
| 124 DCHECK(bus); | |
| 125 bus_ = bus; | |
| 126 DCHECK(adapter_client_); | |
| 127 adapter_client_->AddObserver(this); | |
| 128 } | |
| 129 | |
| 130 private: | |
| 131 // NfcAdapterClient::Observer override. | |
| 132 void AdapterAdded(const dbus::ObjectPath& object_path) override { | |
| 133 VLOG(1) << "Adapter added. Creating map for device proxies belonging to " | |
| 134 << "adapter: " << object_path.value(); | |
| 135 adapters_to_object_maps_.CreateObjectMap( | |
| 136 object_path, nfc_device::kNfcDeviceServiceName, this, bus_); | |
| 137 } | |
| 138 | |
| 139 // NfcAdapterClient::Observer override. | |
| 140 void AdapterRemoved(const dbus::ObjectPath& object_path) override { | |
| 141 // Neard doesn't send out property changed signals for the devices that | |
| 142 // are removed when the adapter they belong to is removed. Clean up the | |
| 143 // object proxies for devices that are managed by the removed adapter. | |
| 144 // Note: DBusObjectMap guarantees that the Properties structure for the | |
| 145 // removed adapter will be valid before this method returns. | |
| 146 VLOG(1) << "Adapter removed. Cleaning up device proxies belonging to " | |
| 147 << "adapter: " << object_path.value(); | |
| 148 adapters_to_object_maps_.RemoveObjectMap(object_path); | |
| 149 } | |
| 150 | |
| 151 // NfcAdapterClient::Observer override. | |
| 152 void AdapterPropertyChanged(const dbus::ObjectPath& object_path, | |
| 153 const std::string& property_name) override { | |
| 154 DCHECK(adapter_client_); | |
| 155 NfcAdapterClient::Properties *adapter_properties = | |
| 156 adapter_client_->GetProperties(object_path); | |
| 157 DCHECK(adapter_properties); | |
| 158 | |
| 159 // Ignore changes to properties other than "Devices". | |
| 160 if (property_name != adapter_properties->devices.name()) | |
| 161 return; | |
| 162 | |
| 163 // Update the known devices. | |
| 164 VLOG(1) << "NFC devices changed."; | |
| 165 const std::vector<dbus::ObjectPath>& received_devices = | |
| 166 adapter_properties->devices.value(); | |
| 167 DBusObjectMap* object_map = | |
| 168 adapters_to_object_maps_.GetObjectMap(object_path); | |
| 169 DCHECK(object_map); | |
| 170 object_map->UpdateObjects(received_devices); | |
| 171 } | |
| 172 | |
| 173 // nfc_client_helpers::DBusObjectMap::Delegate override. | |
| 174 NfcPropertySet* CreateProperties(dbus::ObjectProxy* object_proxy) override { | |
| 175 return new Properties( | |
| 176 object_proxy, | |
| 177 base::Bind(&NfcDeviceClientImpl::OnPropertyChanged, | |
| 178 weak_ptr_factory_.GetWeakPtr(), | |
| 179 object_proxy->object_path())); | |
| 180 } | |
| 181 | |
| 182 // nfc_client_helpers::DBusObjectMap::Delegate override. | |
| 183 void ObjectAdded(const dbus::ObjectPath& object_path) override { | |
| 184 FOR_EACH_OBSERVER(NfcDeviceClient::Observer, observers_, | |
| 185 DeviceAdded(object_path)); | |
| 186 } | |
| 187 | |
| 188 void ObjectRemoved(const dbus::ObjectPath& object_path) override { | |
| 189 FOR_EACH_OBSERVER(NfcDeviceClient::Observer, observers_, | |
| 190 DeviceRemoved(object_path)); | |
| 191 } | |
| 192 | |
| 193 // Called by NfcPropertySet when a property value is changed, either by | |
| 194 // result of a signal or response to a GetAll() or Get() call. | |
| 195 void OnPropertyChanged(const dbus::ObjectPath& object_path, | |
| 196 const std::string& property_name) { | |
| 197 VLOG(1) << "Device property changed; Path: " << object_path.value() | |
| 198 << " Property: " << property_name; | |
| 199 FOR_EACH_OBSERVER(NfcDeviceClient::Observer, observers_, | |
| 200 DevicePropertyChanged(object_path, property_name)); | |
| 201 } | |
| 202 | |
| 203 // We maintain a pointer to the bus to be able to request proxies for | |
| 204 // new NFC devices that appear. | |
| 205 dbus::Bus* bus_; | |
| 206 | |
| 207 // List of observers interested in event notifications. | |
| 208 base::ObserverList<NfcDeviceClient::Observer> observers_; | |
| 209 | |
| 210 // Mapping from object paths to object proxies and properties structures that | |
| 211 // were already created by us. This stucture stores a different DBusObjectMap | |
| 212 // for each known NFC adapter object path. | |
| 213 ObjectProxyTree adapters_to_object_maps_; | |
| 214 | |
| 215 // The adapter client that we listen to events notifications from. | |
| 216 NfcAdapterClient* adapter_client_; | |
| 217 | |
| 218 // Weak pointer factory for generating 'this' pointers that might live longer | |
| 219 // than we do. | |
| 220 // Note: This should remain the last member so it'll be destroyed and | |
| 221 // invalidate its weak pointers before any other members are destroyed. | |
| 222 base::WeakPtrFactory<NfcDeviceClientImpl> weak_ptr_factory_; | |
| 223 | |
| 224 DISALLOW_COPY_AND_ASSIGN(NfcDeviceClientImpl); | |
| 225 }; | |
| 226 | |
| 227 NfcDeviceClient::NfcDeviceClient() { | |
| 228 } | |
| 229 | |
| 230 NfcDeviceClient::~NfcDeviceClient() { | |
| 231 } | |
| 232 | |
| 233 NfcDeviceClient* NfcDeviceClient::Create(NfcAdapterClient* adapter_client) { | |
| 234 return new NfcDeviceClientImpl(adapter_client); | |
| 235 } | |
| 236 | |
| 237 } // namespace chromeos | |
| OLD | NEW |